You are here

function poll_vote in Drupal 7

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

Submit handler for processing a vote.

4 string references to 'poll_vote'
poll_update_7001 in modules/poll/poll.install
Use the poll_choice primary key to record votes in poll_votes rather than the choice order. Rename chorder to weight.
poll_update_7002 in modules/poll/poll.install
Add timestamp field to {poll_vote}.
poll_update_7004 in modules/poll/poll.install
Update the database to match the schema.
poll_view_voting in modules/poll/poll.module
Generates the voting form for a poll.

File

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

Code

function poll_vote($form, &$form_state) {
  $node = $form['#node'];
  $choice = $form_state['values']['choice'];
  global $user;
  db_insert('poll_vote')
    ->fields(array(
    'nid' => $node->nid,
    'chid' => $choice,
    'uid' => $user->uid,
    'hostname' => ip_address(),
    'timestamp' => REQUEST_TIME,
  ))
    ->execute();

  // Add one to the votes.
  db_update('poll_choice')
    ->expression('chvotes', 'chvotes + 1')
    ->condition('chid', $choice)
    ->execute();
  cache_clear_all();
  if (!$user->uid) {

    // The vote is recorded so the user gets the result view instead of the
    // voting form when viewing the poll. Saving a value in $_SESSION has the
    // convenient side effect of preventing the user from hitting the page
    // cache. When anonymous voting is allowed, the page cache should only
    // contain the voting form, not the results.
    $_SESSION['poll_vote'][$node->nid] = $choice;
  }
  drupal_set_message(t('Your vote was recorded.'));

  // Return the user to whatever page they voted from.
}