You are here

function _advpoll_vote_response in Advanced Poll 6

Same name and namespace in other branches
  1. 5 advpoll.module \_advpoll_vote_response()
  2. 6.3 advpoll.module \_advpoll_vote_response()
  3. 6.2 advpoll.module \_advpoll_vote_response()
2 calls to _advpoll_vote_response()
advpoll_voting_binary_form_submit in modes/binary.inc
Registers the vote as a key for this node using votingapi_set_vote().
advpoll_voting_ranking_form_submit in modes/ranking.inc
Implementation of the vote hook for the runoff module.

File

./advpoll.module, line 1231
Advanced Poll - a sophisticated polling module for voting, elections, and group decision-making.

Code

function _advpoll_vote_response($node, $form_state) {
  $msg = t('Your vote was registered.');

  // Ajax response
  if ($form_state['values']['ajax']) {

    // Unset the array of choices so duplicates aren't shown.
    unset($node->choice);

    // Get all choices from database. This is necessary to get information about
    // newly submitted write-in choices.
    $result = db_query('SELECT cid, weight, label, writein FROM {advpoll_choices} WHERE nid = %d ORDER BY weight', $node->nid);
    while ($choice = db_fetch_array($result)) {
      $node->choice[$choice['cid']] = $choice;
    }

    // Update the number of choices.
    $node->choices = count($node->choice);

    // Get updated total number of votes from database.
    $result = db_query("SELECT value FROM {votingapi_cache} WHERE content_type = 'advpoll' AND content_id = %d AND tag = '_advpoll' AND function = 'total_votes'", $node->nid);
    if ($cache = db_fetch_object($result)) {
      $node->votes = $cache->value;
    }
    else {
      $node->votes = 0;
    }
    list($node->voted, $node->cancel_vote) = _advpoll_user_voted($node->nid);
    $ajax_output = '';
    if (user_access('show vote results')) {
      $ajax_output .= advpoll_view_results($node, NULL, NULL);
    }
    elseif (user_access('cancel own vote')) {
      $ajax_output .= _advpoll_show_cancel_form($node);
    }

    // Remove linebreaks as they will break jQuery's insert-HTML methods.
    $ajax_output = str_replace("\n", '', $ajax_output);
    drupal_set_header('Content-Type: text/plain; charset=utf-8');
    print drupal_to_js(array(
      'statusMsgs' => '<div class="messages status">' . $msg . '</div>',
      'response' => $ajax_output,
    ));
    exit;
  }
  else {
    drupal_set_message($msg);
  }
}