poll_vote

Versions
4.6 – 5
poll_vote(&$node)
6 – 7
poll_vote($form, &$form_state)

Callback for processing a vote

Code

modules/poll/poll.module, line 512

<?php
function poll_vote(&$node) {
  global $user;
  $nid = arg(1);

  if ($node = node_load($nid)) {
    $edit = $_POST;
    $choice = $edit['choice'];
    $vote = $_POST['vote'];

    if (isset($choice) && isset($node->choice[$choice])) {
      if ($node->allowvotes) {
        // Record the vote by this user or host.
        if ($user->uid) {
          db_query('INSERT INTO {poll_votes} (nid, chorder, uid) VALUES (%d, %d, %d)', $node->nid, $choice, $user->uid);
        }
        else {
          db_query("INSERT INTO {poll_votes} (nid, chorder, hostname) VALUES (%d, %d, '%s')", $node->nid, $choice, $_SERVER['REMOTE_ADDR']);
        }

        // Add one to the votes.
        db_query("UPDATE {poll_choices} SET chvotes = chvotes + 1 WHERE nid = %d AND chorder = %d", $node->nid, $choice);

        $node->allowvotes = FALSE;
        $node->choice[$choice]['chvotes']++;
        cache_clear_all();
        drupal_set_message(t('Your vote was recorded.'));
      }
      else {
        drupal_set_message(t("You are not allowed to vote on this poll."), 'error');
      }
    }
    else {
      drupal_set_message(t("You did not specify a valid poll choice."), 'error');
    }
    drupal_goto('node/'. $nid);
  }
  else {
    drupal_not_found();
  }
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.