You are here

function advpoll_view_results_ranking in Advanced Poll 6.2

Same name and namespace in other branches
  1. 5 modes/ranking.inc \advpoll_view_results_ranking()
  2. 6.3 modes/ranking.inc \advpoll_view_results_ranking()
  3. 6 modes/ranking.inc \advpoll_view_results_ranking()

File

modes/ranking.inc, line 196
Handle ranking votes, e.g. choice A is preferred over choice B, which in turn is preferred over choice C.

Code

function advpoll_view_results_ranking($node, $teaser, $page) {
  $results = votingapi_select_results(array(
    'content_type' => 'advpoll',
    'content_id' => $node->nid,
  ));
  $round_table = '';

  // If no one has voted, $results = array() and thus is empty.
  if (!empty($results)) {

    // Temporary list of choices indexes for the ranking.
    $ranking_list = array();

    // Result object
    $ranking = array();
    $choices = array();
    $poll = array();
    $rounds = array();
    foreach ($results as $result) {
      $tag = $result['tag'];
      if ($tag == '_advpoll') {

        // Poll-wide cached value.
        $poll[$result['function']] = $result['value'];
      }
      else {
        if (strstr($tag, '_rounds_')) {

          // Re-construct round data and extract the round from the tag.
          $round = str_replace('_rounds_', '', $tag);
          if (!isset($rounds[$round])) {
            $rounds[$round] = array();
          }

          // $result->function actually stores $choice.
          $rounds[$round][$result['function']] = $result['value'];
        }
        else {
          if (isset($node->choice[$tag])) {

            // Note: choices that have been removed will not pass the previous
            // line's test even though their values are still in the vote table.
            // Choice-specific cached value.
            if ($result['function'] == 'ranking') {
              $ranking_list[$result['value']][] = $tag;
            }
            else {
              if (!isset($node->choice[$result['function']])) {
                $choices[$tag][$result['function']] = $result['value'];
              }
            }
          }
        }
      }
    }

    // Re-construct the rankings object.
    foreach ($ranking_list as $i => $choice_list) {
      $ranking[$i]->choices = array();
      foreach ($choice_list as $choice_i) {
        $ranking[$i]->choices[] = $choice_i;
        if (isset($choices[$choice_i]['view_score'])) {
          $ranking[$i]->view_score = $choices[$choice_i]['view_score'];
        }
        if (isset($choices[$choice_i]['raw_score'])) {
          $ranking[$i]->raw_score = $choices[$choice_i]['raw_score'];
        }
        if (isset($choices[$choice_i]['percentage'])) {
          $ranking[$i]->percentage = $choices[$choice_i]['percentage'];
        }
      }
    }
    $output = '';
    if ($node->algorithm == 'borda_count') {
      for ($i = 0; $i < count($ranking); $i++) {
        $first_one = TRUE;
        $this_rank = '';

        // Loop through all choices with this ranking.
        foreach ($ranking[$i]->choices as $choice) {
          $label = isset($node->choice[$choice]) ? _advpoll_choice_markup($node->choice[$choice]['label'], $node->format, FALSE) . ($node->choice[$choice]['writein'] ? ' ' . t('(write-in)') : '') : t('(deleted)');
          $this_rank .= ($first_one ? '' : ', ') . $label;
          $first_one = FALSE;
        }
        $percentage = round(100 * (isset($ranking[$i]->percentage) ? $ranking[$i]->percentage : 0), 0);
        $output .= theme('advpoll_bar', $this_rank, $percentage, $ranking[$i]->view_score);
      }
    }
    else {
      $output .= '<ol>';
      for ($i = 0; $i < count($ranking); $i++) {
        $output .= '<li> ';
        $first_one = TRUE;

        // If previewing check the format against the current users permissions.
        $check = $node->build_mode == NODE_BUILD_PREVIEW;

        // Loop through all choices with this ranking.
        foreach ($ranking[$i]->choices as $choice) {
          $label = isset($node->choice[$choice]) ? _advpoll_choice_markup($node->choice[$choice]['label'], $node->format, FALSE) . ($node->choice[$choice]['writein'] ? ' ' . t('(write-in)') : '') : t('(deleted)');
          $output .= ($first_one ? '' : ', ') . $label;
          $first_one = FALSE;
        }

        // Show the ranking's score if it exists (depends on algorithm).
        if (isset($ranking[$i]->view_score)) {
          $output .= ' (' . $ranking[$i]->view_score . '%)';
        }
        $output .= '</li>';
      }
    }
    $output .= '</ol>';
    if (user_access('inspect all votes') && isset($rounds)) {
      if (count($rounds) > 0) {
        $header[0] = t('Rounds');
        $total_rounds = count($rounds);
        for ($i = 0; $i < count($rounds); $i++) {
          $choices = $rounds[$i];
          if ($i + 1 == $total_rounds) {

            // This is the last round.
            $header[$i + 1] = t('Final');
          }
          else {
            $header[$i + 1] = $i + 1;
          }
          if ($i == 0) {
            $rows = array();
          }
          foreach ($node->choice as $key => $data) {
            $rows[$key][0] = $data['label'];
            $rows[$key][$i + 1] = isset($choices[$key]) && $choices[$key] ? $choices[$key] : '';
          }
        }
        $round_table = theme('table', $header, $rows, array(), t('Per-round breakdown of votes for each choice'));
      }
    }
  }
  $output .= $round_table;
  return array(
    'results' => $output,
    'votes' => $poll['total_votes'],
  );
}