You are here

function advpoll_update_choices in Advanced Poll 7.3

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

Updates vote choices.

A helper function to associate results with choices and order them properly for display.

Parameters

array $choices: An array of poll choices.

array $results: A keyed array containing voting choices and total votes.

Return value

array A keyed array of voting totals for each choice.

2 calls to advpoll_update_choices()
advpoll_display_borda_results in advpoll_ranking/advpoll_ranking.module
Determines how to theme poll results based on settings in $data.
advpoll_display_results in ./advpoll.module
Determines how to theme poll results.

File

./advpoll.module, line 544

Code

function advpoll_update_choices($choices, $results) {
  $choice_set = array();
  $write_in = array();
  foreach ($choices as $choice) {
    $choice_set[$choice['choice_id']] = $choice['choice'];
    $write_in[$choice['choice_id']] = $choice['write_in'];
  }
  $final = array();
  foreach ($results as $result) {
    $final[] = array(
      'title' => $choice_set[$result['index']],
      'percentage' => $result['percentage'],
      'votes' => $result['votes'],
      'tag' => $result['index'],
      'write_in' => $write_in[$result['index']],
    );

    // Choice has been accounted for.  Remove it.
    unset($choice_set[$result['index']]);
  }

  // If there are still items in this array, it means they have no votes
  // associated with them but they still need to be rendered.
  if (count($choice_set) > 0) {
    foreach ($choice_set as $key => $choice) {
      $final[] = array(
        'title' => $choice,
        'percentage' => 0,
        'votes' => 0,
        'tag' => $key,
        'write_in' => $write_in[$key],
      );
    }
  }
  return $final;
}