function poll_view_results

Generates a graphical representation of the results of a poll.

See also

poll_block_latest_poll_view()

poll_view()

2 calls to poll_view_results()
poll_block_latest_poll_view in drupal/core/modules/poll/poll.module
Returns the content for the 'latest poll' block.
poll_view in drupal/core/modules/poll/poll.module
Implements hook_view().

File

drupal/core/modules/poll/poll.module, line 840
Collects votes on different topics in the form of multiple choice questions.

Code

function poll_view_results($node, $view_mode, $block = FALSE) {

  // Make sure that choices are ordered by their weight.
  uasort($node->choice, 'drupal_sort_weight');

  // Count the votes and find the maximum.
  $total_votes = 0;
  $max_votes = 0;
  foreach ($node->choice as $choice) {
    if (isset($choice['chvotes'])) {
      $total_votes += $choice['chvotes'];
      $max_votes = max($max_votes, $choice['chvotes']);
    }
  }
  $poll_results = array();
  foreach ($node->choice as $i => $choice) {
    $chvotes = isset($choice['chvotes']) ? $choice['chvotes'] : NULL;
    $percentage = round($chvotes * 100 / max($total_votes, 1));
    $display_votes = !$block ? ' (' . format_plural($chvotes, '1 vote', '@count votes') . ')' : '';
    $poll_results[] = array(
      '#theme' => 'meter',
      '#prefix' => '<dt class="choice-title">' . check_plain($choice['chtext']) . "</dt>\n" . '<dd class="choice-result">',
      '#suffix' => "</dd>\n",
      '#display_value' => t('!percentage%', array(
        '!percentage' => $percentage,
      )) . $display_votes,
      '#min' => 0,
      '#max' => $total_votes,
      '#value' => $chvotes,
      '#percentage' => $percentage,
      '#attributes' => array(
        'class' => 'bar',
      ),
    );
  }
  return theme('poll_results', array(
    'raw_title' => $node
      ->label(),
    'results' => drupal_render($poll_results),
    'votes' => $total_votes,
    'raw_links' => isset($node->links) ? $node->links : array(),
    'block' => $block,
    'nid' => $node->nid,
    'vote' => isset($node->vote) ? $node->vote : NULL,
  ));
}