function advpoll_display_runoff_results in Advanced Poll 7.2
Same name and namespace in other branches
- 7.3 advpoll_ranking/advpoll_ranking.module \advpoll_display_runoff_results()
- 7 advpoll_ranking/advpoll_ranking.module \advpoll_display_runoff_results()
3 calls to advpoll_display_runoff_results()
- advpoll_ranking_node_view in advpoll_ranking/
advpoll_ranking.module - advpoll_ranking_results_page in advpoll_ranking/
advpoll_ranking.module - advpoll_ranking_submit in advpoll_ranking/
advpoll_ranking.module
File
- advpoll_ranking/
advpoll_ranking.module, line 470
Code
function advpoll_display_runoff_results($nid, $data) {
$output = '';
$form = NULL;
if (user_access('cancel own vote')) {
$rendered_form = drupal_get_form('advpoll_ranking_cancel_form', $nid);
$form = drupal_render($rendered_form);
}
// get user's votes if they're logged in and if voting is normal
$uservotes = array();
if ($data->mode == 'normal') {
$uservotes = advpoll_get_user_votes($nid);
}
if ($data->show_results == 'never' || $data->show_results == 'afterclose' && $data->end_date > time()) {
$output .= theme('advpoll_noresults', array(
'data' => $data,
'votes' => $uservotes,
'nid' => $nid,
'cancel_form' => $form,
));
}
else {
$criteria = array();
$criteria['entity_id'] = $nid;
$criteria['entity_type'] = 'advpoll';
$results = votingapi_select_votes($criteria);
$voters = array();
$votes = array();
$tally = array();
$user = '';
// for run-off voting each vote from a user counts as one vote regardless of how
// many candidates they selected.
foreach ($results as $result) {
// get votes per user to get total votes
$result['uid'] ? $user = $result['uid'] : ($user = $result['vote_source']);
if (isset($voters[$user])) {
$voters[$user]++;
}
else {
$voters[$user] = 1;
}
// total value of all votes for each choice
if (isset($votes[$result['tag']])) {
$votes[$result['tag']] += $result['value'];
}
else {
$votes[$result['tag']] = $result['value'];
}
// total individual votes for each choice
if (isset($tally[$result['tag']])) {
$tally[$result['tag']]++;
}
else {
$tally[$result['tag']] = 1;
}
}
// order poll by votes
asort($votes);
// get votes in descending order
$votes = array_reverse($votes, TRUE);
$rows = array();
$all_by_key = array();
// get all choices with unique ID as key
foreach ($data->choices as $choice) {
// store all choices by key
$all_by_key[$choice['choice_id']] = $choice['choice'];
}
// build rows for table
foreach ($votes as $key => $vote) {
if ($uservotes && in_array($key, $uservotes)) {
$rows[] = array(
'id' => $key,
'total' => $vote,
'votes' => $tally[$key],
'choice' => $all_by_key[$key],
'user_choice' => TRUE,
);
}
else {
$rows[] = array(
'id' => $key,
'total' => $vote,
'votes' => $tally[$key],
'choice' => $all_by_key[$key],
'user_choice' => FALSE,
);
}
// remove from keyed list of choices so we can ensure all choices display even if there
// are no votes for that choice
unset($all_by_key[$key]);
}
$percentage = round($rows[0]['votes'] / count($voters) * 100, 1);
// add on items that did not receive votes
if ($all_by_key) {
foreach ($all_by_key as $key => $choice) {
$rows[] = array(
'id' => $key,
'total' => 0,
'votes' => 0,
'choice' => $choice,
'user_choice' => FALSE,
);
}
}
// pass to template for markup.
$output .= theme('advpoll_runoff', array(
'total' => count($voters),
'rows' => $rows,
'percentage' => $percentage,
'nid' => $nid,
'cancel_form' => $form,
));
}
return $output;
}