function _advpoll_writeins_voting_form_submit in Advanced Poll 5
Same name and namespace in other branches
- 6.3 advpoll.module \_advpoll_writeins_voting_form_submit()
- 6 advpoll.module \_advpoll_writeins_voting_form_submit()
- 6.2 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 1508 - Advanced Poll - a sophisticated polling module for voting, elections, and group decision-making.
Code
function _advpoll_writeins_voting_form_submit($node, $form_values, &$votes, $vote_value) {
// A write-in vote is being made.
if ($form_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_values['writein_choice']);
// If there's more than one match, redo the query, being more exact.
if (db_num_rows($result) > 1) {
$result = db_query("SELECT cid FROM {advpoll_choices} WHERE nid = %d AND label = '%s'", $node->nid, $form_values['writein_choice']);
}
// 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_num_rows($result)) {
$obj = db_fetch_object($result);
$existing_choice = $obj->cid;
// Set a vote
$vote = new stdClass();
$vote->value = $vote_value;
$vote->tag = $existing_choice;
$vote->value_type = 'option';
$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_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_values['writein_choice']));
// Add vote
$vote = new stdClass();
$vote->value = $vote_value;
$vote->tag = $next_cid;
$vote->value_type = 'option';
$votes[] = $vote;
}
}
}