You are here

function advpoll_display_runoff_results in Advanced Poll 7.3

Same name and namespace in other branches
  1. 7 advpoll_ranking/advpoll_ranking.module \advpoll_display_runoff_results()
  2. 7.2 advpoll_ranking/advpoll_ranking.module \advpoll_display_runoff_results()

Determines how to theme poll results for Instant Run-off.

The display for run-off results is unique and does not use the bars.

Parameters

$nid: Node id of the poll.

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

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

Return value

Returns appropriate markup for displaying the run-off poll.

4 calls to advpoll_display_runoff_results()
advpoll_draggable_submit in advpoll_ranking/advpoll_ranking.module
Submit handler for ranking polls.
advpoll_ranking_node_view in advpoll_ranking/advpoll_ranking.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_ranking_submit in advpoll_ranking/advpoll_ranking.module
Submit handler for ranking polls.

File

advpoll_ranking/advpoll_ranking.module, line 559

Code

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

  // 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 (user_access('cancel own vote') && $uservotes && !$expired) {
    $form = drupal_get_form('advpoll_ranking_cancel_form', $nid);
  }
  $rendered_form = drupal_render($form);
  if (!$page && !$uservotes && $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' => $uservotes,
      'nid' => $nid,
      'cancel_form' => $rendered_form,
    ));
  }
  else {
    $criteria = array();
    $criteria['entity_id'] = $nid;
    $criteria['entity_type'] = 'node';
    $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();
    $write_in = 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'];
      $write_in[$choice['choice_id']] = $choice['write_in'];
    }

    // 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,
          'write_in' => $write_in[$key],
        );
      }
      else {
        $rows[] = array(
          'id' => $key,
          'total' => $vote,
          'votes' => $tally[$key],
          'choice' => $all_by_key[$key],
          'user_choice' => FALSE,
          'write_in' => $write_in[$key],
        );
      }

      /* 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]);
    }
    $divisor = count($voters) * 100;
    $percentage = 0;
    if ($divisor > 0) {
      $percentage = advpoll_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,
          'write_in' => $write_in[$key],
        );
      }
    }

    // Pass to template for markup.
    $output .= theme('advpoll_runoff', array(
      'total' => count($voters),
      'rows' => $rows,
      'percentage' => $percentage,
      'nid' => $nid,
      'cancel_form' => $rendered_form,
    ));
  }
  return $output;
}