You are here

function plus1_vote in Plus 1 6

Same name and namespace in other branches
  1. 6.2 plus1.module \plus1_vote()
  2. 7 plus1.module \plus1_vote()

Called by jQuery. This submits the vote request and returns JSON to be parsed by jQuery.

1 string reference to 'plus1_vote'
plus1_menu in ./plus1.module
Implementation of hook_menu().

File

./plus1.module, line 81
A simple +1 voting widget module.

Code

function plus1_vote($nid) {
  global $user;

  // Authors may not vote on their own posts.
  $is_author = db_result(db_query('SELECT uid FROM {node} WHERE nid = %d AND uid = %d', $nid, $user->uid));

  // Before processing the vote we check that the user is logged in.
  // We have a node ID, and the user is not the author of the node.
  if ($user->uid && $nid > 0 && !$is_author) {
    $vote = plus1_get_vote($nid, $user->uid);
    if (!$vote) {
      $values = array(
        'uid' => $user->uid,
        'nid' => $nid,
        'vote' => 1,
      );
      plus1_vote_save($values);
      $score = plus1_get_score($nid);

      // This print statement will return results to jQuery's request.
      // drupal_json has replaced drupal_to_js in Drupal 6 (it adds a header).
      print drupal_json(array(
        'score' => $score,
        'voted' => t('<small>You voted</small>'),
      ));
    }
  }

  // Out we go, we're not returning anything
  exit;
}