You are here

function advpoll_display_results in Advanced Poll 7.3

Same name and namespace in other branches
  1. 7 advpoll.module \advpoll_display_results()
  2. 7.2 advpoll.module \advpoll_display_results()

Determines how to theme poll results.

Parameters

int $nid: Node id of the poll.

object $data: Data from the node formatted by one of the helper functions in the advpoll_helper.inc document.

bool $page: A boolean value that indicates whether the request is to be displayed normally (0) or on the results page (1).

4 calls to advpoll_display_results()
advpoll_form_submit in ./advpoll.module
Submit handler for voting.
advpoll_node_view in ./advpoll.module
Implements hook_node_view().
advpoll_ranking_results_page in advpoll_ranking/advpoll_ranking.module
Determines how to display the votes based on its type.
advpoll_results_page in includes/advpoll.pages.inc
Display the poll's results for users with appropriate permissions.

File

./advpoll.module, line 434

Code

function advpoll_display_results($nid, $data, $page = 0) {
  $output = '';
  $form = NULL;
  $expired = FALSE;
  $not_open_yet = FALSE;
  if ($data->state == 'close') {
    $expired = TRUE;
  }
  if ($data->start_date && $data->start_date > REQUEST_TIME) {
    $not_open_yet = TRUE;
  }
  if ($data->end_date && $data->end_date < REQUEST_TIME) {
    $expired = TRUE;
  }

  // Get user's votes if they're logged in and if voting is normal
  $votes = array();
  if ($data->mode == 'normal') {
    $votes = advpoll_get_user_votes($nid);
  }
  if (user_access('cancel own vote') && $votes && !$expired) {
    $form = drupal_get_form('advpoll_cancel_vote_form', $nid);
  }
  $rendered_form = drupal_render($form);
  if (!$page && $not_open_yet) {
    $output .= theme('advpoll_closed', array(
      'data' => $data,
    ));
  }
  elseif (!$page && !$votes && $data->electoral && ($data->show_results == 'afterclose' || $data->show_results == 'never') && !$expired) {
    $output .= theme('advpoll_ineligible', array(
      'data' => $data,
    ));
  }
  elseif (!$page && ($data->show_results == 'never' || $data->show_results == 'afterclose' && !$expired)) {
    $output .= theme('advpoll_noresults', array(
      'data' => $data,
      'votes' => $votes,
      'nid' => $nid,
      'cancel_form' => $rendered_form,
    ));
  }
  else {
    $results = advpoll_get_votes($nid, $data);
    $bars = '';
    $final = advpoll_update_choices($data->choices, $results['choices']);

    /* @TODO:
     * - Update title to indicate that the source of a vote is a write in.
     * - Allow permission to determine whether or not to display a write in vote
     *   in results.
     */
    foreach ($final as $item) {
      $voted = FALSE;
      if (in_array($item['tag'], $votes)) {
        $voted = TRUE;
      }
      $title = $item['title'];
      $show_bar = TRUE;
      if ($item['write_in']) {
        $title .= ' ' . t('(Write in)');
        $show_bar = _advpoll_show_writeins_access();
      }
      if ($show_bar) {
        $bars .= theme('advpoll_bar', array(
          'title' => filter_xss($title),
          'percentage' => $item['percentage'],
          'votes' => $item['votes'],
          'voted' => $voted,
        ));
      }
    }
    $output .= theme('advpoll_results', array(
      'bars' => $bars,
      'total' => $results['total'],
      'voted' => $votes,
      'nid' => $nid,
      'cancel_form' => $rendered_form,
    ));
  }
  return $output;
}