You are here

function advpoll_votes_page in Advanced Poll 5

Same name and namespace in other branches
  1. 6.3 advpoll.pages.inc \advpoll_votes_page()
  2. 6 advpoll.pages.inc \advpoll_votes_page()
  3. 6.2 advpoll.pages.inc \advpoll_votes_page()
  4. 7.3 includes/advpoll.pages.inc \advpoll_votes_page()
  5. 7 includes/advpoll.pages.inc \advpoll_votes_page()
  6. 7.2 includes/advpoll.pages.inc \advpoll_votes_page()

Display the votes page.

1 string reference to 'advpoll_votes_page'
advpoll_menu in ./advpoll.module
Implementation of hook_menu().

File

./advpoll.module, line 1036
Advanced Poll - a sophisticated polling module for voting, elections, and group decision-making.

Code

function advpoll_votes_page() {
  if ($node = node_load(arg(1))) {
    if (!$node->show_votes) {

      // Advanced Poll is set to not allow viewing of votes.
      drupal_not_found();
      return;
    }
    drupal_set_title(check_plain($node->title));
    $output = t('This table lists all the recorded votes for this poll. If anonymous users are allowed to vote, they will be identified by the IP address of the computer they used when they voted.');
    $header[] = array(
      'data' => t('Visitor'),
      'field' => 'u.name',
    );
    $header[] = array(
      'data' => t('Vote'),
      '',
    );
    $result = pager_query("SELECT v.uid, v.hostname, v.timestamp, u.name FROM {votingapi_vote} v LEFT JOIN {users} u ON v.uid = u.uid WHERE v.content_type = 'advpoll' AND v.content_id = %d GROUP BY v.uid, v.hostname, v.timestamp, u.name" . tablesort_sql($header), 20, 0, NULL, $node->nid);
    $uids = array();
    $hostnames = array();
    $timestamp = array();
    while ($vote = db_fetch_object($result)) {
      $uids[$vote->uid] = $vote->uid;
      $hostnames[$vote->hostname] = $vote->hostname;
      $timestamps[$vote->timestamp] = $vote->timestamp;
    }
    $rows = array();
    if (count($uids) > 0) {

      // Use db_query()'s placeholder syntax to prevent any potential SQL
      // injection attacks.
      $uid_placeholders = array_fill(0, count($uids), '%d');
      $host_placeholders = array_fill(0, count($hostnames), "'%s'");
      $time_placeholders = array_fill(0, count($timestamps), '%d');

      // Here we have to select votes based on their uid+hostname+timestamp
      // combination because there no unique id for the set of rankings that
      // corresponds to a uesr's full vote on a given poll.
      $query = "SELECT v.vote_id, v.tag, v.uid, v.hostname, v.timestamp, v.value, u.name FROM {votingapi_vote} v LEFT JOIN {users} u ON v.uid = u.uid WHERE v.content_id = %d AND v.uid IN(" . implode(', ', $uid_placeholders) . ") AND v.hostname IN(" . implode(', ', $host_placeholders) . ") AND v.timestamp IN(" . implode(', ', $time_placeholders) . ")" . tablesort_sql($header);
      $parameters = array_merge(array(
        $query,
        $node->nid,
      ), array_values($uids), array_values($hostnames), array_values($timestamps));
      $result = call_user_func_array('db_query', $parameters);
      while ($vote = db_fetch_object($result)) {
        $key = $vote->uid ? $vote->uid : $vote->hostname . '-' . $vote->timestamp;
        $rows[$key]['name'] = $vote->name ? theme('username', $vote) : check_plain($vote->hostname);
        if ($node->type == 'advpoll_ranking') {

          // Need two dimensional results (if equal rankings are allowed).
          $rows[$key]['votes'][$vote->value][] = _advpoll_choice_markup($node->choice[$vote->tag]['label'], $node->format, FALSE);
        }
        else {

          // Just need one dimensional results.
          $rows[$key]['votes'][] = _advpoll_choice_markup($node->choice[$vote->tag]['label'], $node->format, FALSE);
        }
      }
    }
    $separators = array(
      'advpoll_ranking' => ' > ',
      'advpoll_binary' => ', ',
    );

    // Create strings out of each vote.
    $results = array();
    foreach ($rows as $key => $container) {
      $ranking = $container['votes'];
      asort($ranking);
      $rankings = array();
      if ($node->type == 'advpoll_ranking') {

        // Include support for multiple choices having the same ranking.
        foreach ($ranking as $vote => $choices) {
          $rankings[$vote] = implode(' = ', $choices);
        }
      }
      else {

        // Just copy the previous array.
        $rankings = $ranking;
      }
      ksort($rankings);
      $results[$key]['name'] = $rows[$key]['name'];
      $results[$key]['vote'] = implode($separators[$node->type], $rankings);
    }
    $output .= theme('table', $header, $results);
    $output .= theme('pager', NULL, 20, 0);
    print theme('page', $output);
  }
  else {
    drupal_not_found();
  }
}