You are here

function advpoll_display_borda_results in Advanced Poll 7

Same name and namespace in other branches
  1. 7.3 advpoll_ranking/advpoll_ranking.module \advpoll_display_borda_results()
  2. 7.2 advpoll_ranking/advpoll_ranking.module \advpoll_display_borda_results()
3 calls to advpoll_display_borda_results()
advpoll_ranking_node_view in advpoll_ranking/advpoll_ranking.module
advpoll_ranking_results_page in advpoll_ranking/advpoll_ranking.module
advpoll_ranking_submit in advpoll_ranking/advpoll_ranking.module

File

advpoll_ranking/advpoll_ranking.module, line 549

Code

function advpoll_display_borda_results($nid, $data) {
  $output = '';
  $form = null;
  if (user_access('cancel own vote')) {
    $form = drupal_render(drupal_get_form('advpoll_ranking_cancel_form', $nid));
  }

  // 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 ($data->show_results == 'never' || $data->show_results == 'afterclose' && $data->end_date > time()) {
    $output .= theme('advpoll_noresults', array(
      'data' => $data,
      'votes' => $votes,
      'nid' => $nid,
      'cancel_form' => $form,
    ));
  }
  else {
    $results = advpoll_get_votes($nid, $data->behavior);
    $bars = '';
    $final = advpoll_update_choices($data->choices, $results['choices']);
    $reordered = array();
    foreach ($final as $item) {
      $voted = false;
      if (in_array($item['tag'], $votes)) {
        $voted = true;
      }
      if (!isset($reordered[$item['percentage']])) {
        $reordered[$item['percentage']] = array();
      }
      $reordered[$item['percentage']][] = array(
        'title' => $item['title'],
        'votes' => $item['votes'],
        'voted' => $voted,
      );
    }
    foreach ($reordered as $key => $candidates) {
      $titles = array();
      foreach ($candidates as $candidate) {
        $candidate['voted'] ? $class = 'voted' : ($class = 'not-voted');
        $titles[] = '<span class="' . $class . '">' . $candidate['title'] . '</span>';
      }
      $bars .= theme('advpoll_borda_bar', array(
        'title' => implode(', ', $titles),
        'percentage' => $key,
        'votes' => $candidate['votes'],
      ));
    }
    $output .= theme('advpoll_results', array(
      'bars' => $bars,
      'total' => $results['total'],
      'voted' => $votes,
      'nid' => $nid,
      'cancel_form' => $form,
    ));
  }
  return $output;
}