You are here

function advpoll_cancel_ranking in Advanced Poll 5

Same name and namespace in other branches
  1. 6.3 modes/ranking.inc \advpoll_cancel_ranking()
  2. 6 modes/ranking.inc \advpoll_cancel_ranking()
  3. 6.2 modes/ranking.inc \advpoll_cancel_ranking()

Hook to handle a cancelled vote for a ranking poll.

File

modes/ranking.inc, line 769

Code

function advpoll_cancel_ranking($node, $user_vote) {

  // Remove choice if this was the last vote for a write-in.
  if ($node->writeins) {
    $recalculate = FALSE;
    foreach ($user_vote as $vote) {
      if ($node->choice[$vote->tag]['writein']) {

        // Check if there are any other votes for this write-in.
        $count = db_result(db_query('SELECT COUNT(1) FROM {votingapi_vote} WHERE content_id = %d AND tag = %d', $node->nid, $vote->tag));
        if ($count == 0) {

          // Delete the write-in because no one else voted for it.
          db_query('DELETE FROM {advpoll_choices} WHERE cid = %d', $vote->tag);
          $recalculate = TRUE;
          watchdog('content', t('Removed write-in choice %choice after the last vote was cancelled.', array(
            '%choice' => $node->choice[$vote->tag]['label'],
          )));
        }
      }
    }
    if ($recalculate) {
      votingapi_recalculate_results('advpoll', $node->nid);
    }
  }
}