You are here

function advpoll_view_results_binary in Advanced Poll 6

Same name and namespace in other branches
  1. 5 modes/binary.inc \advpoll_view_results_binary()
  2. 6.3 modes/binary.inc \advpoll_view_results_binary()
  3. 6.2 modes/binary.inc \advpoll_view_results_binary()

File

modes/binary.inc, line 123
Handle binary (true/false) votes.

Code

function advpoll_view_results_binary($node, $teaser, $page) {
  $results = votingapi_select_results(array(
    'content_type' => 'advpoll',
    'content_id' => $node->nid,
  ));
  $votes = array();
  $output = '';
  foreach ($results as $result) {
    $vote_value = $result['tag'];
    if ($vote_value == '_advpoll') {
      if ($result['function'] == 'total_votes') {
        $total_votes = $result['value'];
      }
    }
    else {
      if (isset($node->choice[$vote_value])) {
        if (!isset($votes[$vote_value])) {
          $votes[$vote_value] = 0;
        }
        $votes[$vote_value] = $result['value'];
      }
    }
  }
  if ($node->choice && $total_votes > 0) {

    // Add in any choices that received no votes.
    foreach ($node->choice as $i => $choice) {
      if (!isset($votes[$i])) {
        $votes[$i] = 0;
      }
    }

    // Sort results by votes, descending.
    arsort($votes);

    // Display results for each possible choice
    foreach ($votes as $i => $count) {
      $choice = $node->choice[$i];
      $percentage = round(100 * $votes[$i] / $total_votes, 0);
      $output .= theme('advpoll_bar', _advpoll_choice_markup($choice['label'], $node->format, FALSE), $percentage, format_plural($count, '1 vote', '@count votes'), $choice);
    }
  }
  return array(
    'results' => $output,
    'votes' => $total_votes,
  );
}