function advpoll_writein_merge_form_submit in Advanced Poll 5
Same name and namespace in other branches
- 6.3 advpoll.module \advpoll_writein_merge_form_submit()
- 6 advpoll.module \advpoll_writein_merge_form_submit()
- 6.2 advpoll.module \advpoll_writein_merge_form_submit()
File
- ./
advpoll.module, line 1612 - Advanced Poll - a sophisticated polling module for voting, elections, and group decision-making.
Code
function advpoll_writein_merge_form_submit($form_id, $form_values) {
// Get a list of votes in this node.
$raw_votes = db_query('SELECT * FROM {votingapi_vote} WHERE content_id = %d', $form_values['nid']);
$voters = array();
$affected_voters = array();
while ($vote = db_fetch_object($raw_votes)) {
$key = $vote->uid . '-' . $vote->hostname;
if (!isset($voters[$key])) {
$voters[$key] = array();
}
array_push($voters[$key], $vote);
if ($vote->tag == $form_values['source']) {
// This voter is affected by the merge; save the index of the source vote.
$affected_voters[$key] = count($voters[$key]) - 1;
}
}
// Now fix the affected voters.
foreach ($affected_voters as $key => $source_index) {
// Find out if they voted for the destination or not.
$voted_for_destination = FALSE;
foreach ($voters[$key] as $index => $vote) {
if ($vote->tag == $form_values['destination']) {
$voted_for_destination = TRUE;
break;
}
}
if ($voted_for_destination) {
// Since they already voted for the destination choice, delete the vote
// for the source.
db_query('DELETE FROM {votingapi_vote} WHERE vote_id = %d AND tag = %d', $voters[$key][$index]->vote_id, $form_values['source']);
}
else {
// They didn't already vote for the destination, so transfer the vote for
// the source to the destination.
db_query('UPDATE {votingapi_vote} SET tag = %d WHERE vote_id = %d AND tag = %d', $form_values['destination'], $voters[$key][$index]->vote_id, $form_values['source']);
}
}
// Delete the merged choice.
db_query('DELETE FROM {advpoll_choices} WHERE cid = %d', $form_values['source']);
votingapi_recalculate_results('advpoll', $form_values['nid']);
drupal_set_message(t('Write-in merged.'));
// Unset destination form element so that drupal_goto() doesn't use it
// mistakenly.
unset($_REQUEST['destination']);
drupal_goto('node/' . $form_values['nid'] . '/writeins');
}