You are here

function advpoll_view_results_binary in Advanced Poll 6.3

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

File

modes/binary.inc, line 142
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 = '';
  $total_votes = 0;
  $advpollSettings = variable_get('advpoll_settings', array());
  $mc = $advpollSettings['multiple_choice'];
  foreach ($results as $result) {
    if ($mc) {
      if ($result['function'] != 'total_votes') {
        $total_votes += $result['value'];
        $votes[$result['tag']] = $result['value'];
      }
    }
    else {
      $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 = floor(100 * $votes[$i] / $total_votes);
      $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,
  );
}