function advpoll_writein_merge_form_submit in Advanced Poll 6.2
Same name and namespace in other branches
- 5 advpoll.module \advpoll_writein_merge_form_submit()
- 6.3 advpoll.module \advpoll_writein_merge_form_submit()
- 6 advpoll.module \advpoll_writein_merge_form_submit()
Process advpoll_writein_merge form submissions.
File
- ./
advpoll.module, line 1530 - Advanced Poll - a sophisticated polling module for voting, elections, and group decision-making.
Code
function advpoll_writein_merge_form_submit($form, &$form_state) {
// Get a list of votes in this node.
$node = $form['#node'];
$raw_votes = db_query('SELECT * FROM {votingapi_vote} WHERE content_id = %d', $form_state['values']['nid']);
$voters = array();
$affected_voters = array();
while ($vote = db_fetch_object($raw_votes)) {
$key = $vote->uid . '-' . $vote->vote_source;
if (!isset($voters[$key])) {
$voters[$key] = array();
}
array_push($voters[$key], $vote);
if ($vote->tag == $form_state['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_state['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_state['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_state['values']['destination'], $voters[$key][$index]->vote_id, $form_state['values']['source']);
}
}
// Delete the merged choice.
db_query('DELETE FROM {advpoll_choices} WHERE cid = %d', $form_state['values']['source']);
votingapi_recalculate_results('advpoll', $form_state['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']);
$writeins_remaining = FALSE;
foreach ($node->choice as $choice) {
if ($choice['writein'] && $choice['cid'] != $form_state['values']['source']) {
$writeins_remaining = TRUE;
break;
}
}
drupal_goto('node/' . $form_state['values']['nid'] . ($writeins_remaining ? '/writeins' : ''));
}