You are here

function plus1_undo_vote in Plus 1 7

1 string reference to 'plus1_undo_vote'
plus1_menu in ./plus1.module
Implements hook_menu().

File

./plus1.module, line 174

Code

function plus1_undo_vote($entity_type, $entity_id, $tag = 'plus1_node_vote') {
  global $user;
  if (!drupal_valid_token($_GET['token'], $entity_id, true)) {
    watchdog('plus1', 'Voting form error: Invalid token.');
    return drupal_access_denied();
  }
  $voted = plus1_get_votes($entity_type, $entity_id, $user->uid, $tag);
  $can_undo_vote = variable_get('plus1_' . $entity_type . '_undo_vote', 0);

  // If the voter has already voted and he can undo his vote.
  if ($voted && $can_undo_vote) {
    $criteria['entity_type'] = $entity_type;
    $criteria['entity_id'] = $entity_id;
    $criteria['value_type'] = 'points';
    if ($user->uid) {
      $criteria['uid'] = $user->uid;
    }
    $criteria['vote_source'] = $user->uid ? $user->uid : ip_address();

    //Allow vote source to be altered by other modules
    drupal_alter('plus1_source', $criteria['vote_source']);
    if (isset($tag) && $tag != "") {
      $criteria['tag'] = $tag;
    }
    $votes = votingapi_select_votes($criteria);
    votingapi_delete_votes($votes);
    votingapi_recalculate_results($entity_type, $entity_id, TRUE);
    $criteria = array(
      'entity_type' => $entity_type,
      'entity_id' => $entity_id,
      'function' => 'sum',
      'tag' => $tag ? $tag : 'plus1_' . $entity_type . '_vote',
    );
    $results = votingapi_select_results($criteria);
    $score = isset($results[0]['value']) ? $results[0]['value'] : 0;
    module_invoke_all('plus1_voted', 'undo_vote', $entity_type, $entity_id, $tag, $score, $user);
    if (isset($_GET['json'])) {
      drupal_json_output(theme('plus1_json_response__' . $entity_type . '__' . $tag, array(
        'entity_type' => $entity_type,
        'entity_id' => $entity_id,
        'tag' => $tag,
        'score' => $score,
        'vote_type' => 'undo_vote',
      )));
    }
    else {
      drupal_set_message(t('Thank you for your vote.'));

      // go to the page where user pressed vote button
      drupal_goto();
    }
  }
}