You are here

function vote_up_down_vote in Vote Up/Down 6

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

Function for the main voting 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 535

Code

function vote_up_down_vote($type, $cid, $value, $tag = NULL, $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}")) {
    $vote = array();

    // 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';
    $tag = $tag ? $tag : variable_get('vote_up_down_tag', 'vote');
    $vote['tag'] = $tag;

    // 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));
      }
      $criteria = array(
        'content_type' => $type,
        'content_id' => $cid,
        'uid' => $user->uid,
        'tag' => $tag,
      );
      $user_vote = votingapi_select_single_vote_value($criteria);
      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 ($vote['value'] == 0) {
      $criteria = array(
        'content_type' => $type,
        'content_id' => $cid,
        'tag' => $tag,
      );
      $criteria += votingapi_current_user_identifier();
      votingapi_delete_votes(votingapi_select_votes($criteria));
      votingapi_recalculate_results($type, $cid);
    }
    else {
      $vote['content_id'] = $cid;
      $vote['content_type'] = $type;
      $votes = array(
        0 => $vote,
      );
      votingapi_set_votes($votes);
    }
    if ($ajax) {
      if ($alt == TRUE) {
        print theme('vote_up_down_points_alt', $cid, $type, NULL, 'alt');
      }
      else {
        print theme('vote_up_down_points', $cid, $type, NULL, '');
      }
      exit;
    }
    else {
      drupal_goto(drupal_get_destination());
    }
  }
}