poll_vote

5 poll.module poll_vote(&$node)
6 poll.module poll_vote($form, &$form_state)
7 poll.module poll_vote($form, &$form_state)
8 poll.module poll_vote($form, &$form_state)

Callback for processing a vote

1 call to poll_vote()

1 string reference to 'poll_vote'

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();
  }
}
Login or register to post comments