You are here

function vote_up_down_vote in Vote Up/Down 5

Same name and namespace in other branches
  1. 6 vote_up_down.module \vote_up_down_vote()

An voting get handler with AJAX support

1 string reference to 'vote_up_down_vote'
vote_up_down_menu in ./vote_up_down.module
Implementation of hook_menu().

File

./vote_up_down.module, line 439
vote_up_down is a module that adds a widget for +1/-1 votes on nodes. It depends upon Voting API. It's based upon "simplevote.module".

Code

function vote_up_down_vote($type, $cid, $value, $ajax = FALSE, $alt = FALSE) {
  if (is_numeric($cid) && is_numeric($value) && isset($_REQUEST['token']) && drupal_valid_token($_REQUEST['token'], "vote_up_down/{$type}/{$cid}/{$value}")) {

    // Sanity-check the incoming values.
    if ($value > 0) {
      $vote->value = 1;
    }
    else {
      if ($value < 0) {
        $vote->value = -1;
      }
      else {
        $vote->value = 0;
      }
    }
    $vote->value_type = 'points';
    $vote->tag = variable_get('vote_up_down_tag', 'vote');

    // Userpoints integration.
    if (module_exists('userpoints')) {
      global $user;
      if ($type == 'node') {
        $node = db_fetch_object(db_query(db_rewrite_sql('SELECT n.uid FROM {node} n WHERE n.nid = %d'), $cid));
      }
      else {
        $node = db_fetch_object(db_query(db_rewrite_sql('SELECT c.uid FROM {comments} c WHERE c.cid = %d', 'c', 'cid'), $cid));
      }
      $user_vote = votingapi_get_user_votes($type, $cid, $user->uid);
      if ($user->uid && !$user_vote && $node->uid != $user->uid && $vote->value != 0) {
        userpoints_userpointsapi('points', variable_get('userpoints_vote_up_down', 0), $user->uid, 'Vote up/down points');
      }
    }

    // Do the voting via voting api.
    if ($uid = _vote_up_down_get_uid()) {
      if ($vote->value == 0) {
        votingapi_unset_vote($type, $cid, $uid);
      }
      else {
        votingapi_set_vote($type, $cid, $vote, $uid);
      }
    }
    if ($ajax) {
      $style = $alt == TRUE ? '_alt' : '';
      print theme("vote_up_down_points{$style}", $cid, $type);
      exit;
    }
    else {
      drupal_goto(drupal_get_destination());
    }
  }
}