You are here

function _advpoll_writeins_voting_form_submit in Advanced Poll 6.2

Same name and namespace in other branches
  1. 5 advpoll.module \_advpoll_writeins_voting_form_submit()
  2. 6.3 advpoll.module \_advpoll_writeins_voting_form_submit()
  3. 6 advpoll.module \_advpoll_writeins_voting_form_submit()

Voting form submission logic specific to writeins. This has been abstracted away from includes in the modes directory.

2 calls to _advpoll_writeins_voting_form_submit()
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 1423
Advanced Poll - a sophisticated polling module for voting, elections, and group decision-making.

Code

function _advpoll_writeins_voting_form_submit($node, $form_state, &$votes, $vote_value = 1) {

  // A write-in vote is being made.
  if (isset($form_state['values']['writein_choice']) && $form_state['values']['writein_choice']) {

    // Check if someone has previously voted for this choice.
    $result = db_query("SELECT cid FROM {advpoll_choices} WHERE nid = %d AND LOWER(label) = LOWER('%s')", $node->nid, $form_state['values']['writein_choice']);
    $db_vote = db_fetch_object($result);

    // If there's more than one match, redo the query, being more exact.
    if ($db_vote && ($second_vote = db_fetch_object($result))) {
      $result = db_query("SELECT cid FROM {advpoll_choices} WHERE nid = %d AND label = '%s'", $node->nid, $form_state['values']['writein_choice']);
      $db_vote = db_fetch_object($result);
    }

    // If there is at least one match, add a vote for the first one returned. It
    // should be rare to find more than one choice for any one node with a given
    // label.
    if ($db_vote) {
      $existing_choice = $db_vote->cid;

      // Set a vote
      $vote = array();

      // TODO: confirm this works for ranking polls.
      $vote['value'] = $vote_value;
      $vote['tag'] = $existing_choice;
      $vote['value_type'] = 'option';
      $vote['content_type'] = 'advpoll';
      $vote['content_id'] = $node->nid;
      $votes[] = $vote;
    }
    else {

      // Get highest weight for this node.
      $highest_weight = db_result(db_query("SELECT MAX(weight) FROM {advpoll_choices} WHERE nid = %d", $node->nid));
      $next_weight = $highest_weight ? $highest_weight + 1 : 1;

      // Insert new choice. We do a check_plain() on the label to ensure that
      // there is no possibility of insecure data getting into the database;
      // if HTML is needed the admin can edit the writein choice manually.
      db_query("INSERT INTO {advpoll_choices} (nid, label, weight, writein) VALUES (%d, '%s', %d, 1)", $node->nid, check_plain($form_state['values']['writein_choice']), $next_weight);

      // Get newest choice id.
      $next_cid = db_result(db_query("SELECT cid FROM {advpoll_choices} WHERE nid = %d AND label = '%s'", $node->nid, $form_state['values']['writein_choice']));

      // Add vote
      $vote = array();

      // TODO: confirm this works for ranking polls.
      $vote['value'] = $vote_value;
      $vote['tag'] = $next_cid;
      $vote['value_type'] = 'option';
      $vote['content_type'] = 'advpoll';
      $vote['content_id'] = $node->nid;
      $votes[] = $vote;
    }
  }
}