You are here

function advpoll_calculate_results_ranking in Advanced Poll 6

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

Calculate the results for a ranking poll based on the algorithm.

Parameters

$node: The node object for the current poll

Return value

Should return an object that include the following attributes -results : 2d array listing the aggregate preference, including ties -rounds : 2d array listing the per-choice vote count for each round and a status message indicating who was eliminated -totalVoters : the total number of voters who participated

File

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

Code

function advpoll_calculate_results_ranking(&$cache, $node) {
  if ($node->algorithm == 'borda_count') {
    $results = _advpoll_calculate_bordacount($node);
  }
  else {
    $results = _advpoll_calculate_instantrunoff($node);
  }

  // Cache rankings.
  // API: $cache[$tag][$type][$function] = $value (0 is the default $type)
  if (isset($results->ranking)) {
    for ($i = 0; $i < count($results->ranking); $i++) {
      foreach ($results->ranking[$i]['choices'] as $choice) {
        $cache[$choice][0]['ranking'] = $i;
        if (isset($results->ranking[$i]['raw_score'])) {
          $cache[$choice][0]['raw_score'] = $results->ranking[$i]['raw_score'];
        }
        if (isset($results->ranking[$i]['view_score'])) {
          $cache[$choice][0]['view_score'] = $results->ranking[$i]['view_score'];
        }
        if (isset($results->ranking[$i]['percentage'])) {
          $cache[$choice][0]['percentage'] = $results->ranking[$i]['percentage'];
        }
      }
    }
  }

  // Cache round results.
  if (isset($results->matrix)) {
    foreach ($results->matrix as $i => $round) {
      $key = '_rounds_' . $i;
      $cache[$key] = array();
      foreach ($round as $choice => $votes) {
        $cache[$key][0][$choice] = count($votes);
      }
    }
  }

  // Cache total votes.
  $cache['_advpoll'][0]['total_votes'] = isset($results->total_votes) ? $results->total_votes : 0;

  // Cache total points (if it exists).
  if (isset($results->total_points)) {
    $cache['_advpoll'][0]['total_points'] = $results->total_points;
  }
}