You are here

function poll_vote in Drupal 4

Same name and namespace in other branches
  1. 5 modules/poll/poll.module \poll_vote()
  2. 6 modules/poll/poll.module \poll_vote()
  3. 7 modules/poll/poll.module \poll_vote()

Callback for processing a vote

1 call to poll_vote()
poll_view_voting in modules/poll.module
Generates the voting form for a poll.
1 string reference to 'poll_vote'
poll_menu in modules/poll.module
Implementation of hook_menu().

File

modules/poll.module, line 403
Enables your site to capture votes on different topics in the form of multiple choice questions.

Code

function poll_vote(&$node) {
  global $user;
  $nid = arg(1);
  if ($node = node_load($nid)) {
    $edit = $_POST['edit'];
    $choice = $edit['choice'];
    $vote = $_POST['vote'];
    if (isset($choice) && isset($node->choice[$choice])) {
      if ($node->allowvotes) {

        // Mark the user or host as having voted.
        if ($user->uid) {
          db_query('INSERT INTO {poll_votes} (nid, uid) VALUES (%d, %d)', $node->nid, $user->uid);
        }
        else {
          db_query("INSERT INTO {poll_votes} (nid, hostname) VALUES (%d, '%s')", $node->nid, $_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're not allowed to vote on this poll."), 'error');
      }
    }
    else {
      drupal_set_message(t("You didn't specify a valid poll choice."), 'error');
    }
    drupal_goto('node/' . $nid);
  }
  else {
    drupal_not_found();
  }
}