poll.pages.inc 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * @file
  4. * User page callbacks for the poll module.
  5. */
  6. /**
  7. * Menu callback to provide a simple list of all polls available.
  8. */
  9. function poll_page() {
  10. $polls_per_page = 15;
  11. $count_select = db_select('node', 'n');
  12. $count_select->addExpression('COUNT(*)', 'expression');
  13. $count_select->join('poll', 'p', 'p.nid = n.nid');
  14. $count_select->condition('n.status', 1);
  15. // List all polls.
  16. $select = db_select('node', 'n');
  17. $select->join('poll', 'p', 'p.nid = n.nid');
  18. $select->join('poll_choice', 'c', 'c.nid = n.nid');
  19. $select->addExpression('SUM(c.chvotes)', 'votes');
  20. $select = $select->fields('n', array('nid', 'title', 'created'))
  21. ->fields('p', array('active'))
  22. ->condition('n.status', 1)
  23. ->orderBy('n.created', 'DESC')
  24. ->groupBy('n.nid')
  25. ->groupBy('n.title')
  26. ->groupBy('p.active')
  27. ->groupBy('n.created')
  28. ->extend('PagerDefault')
  29. ->limit($polls_per_page)
  30. ->addTag('node_access');
  31. $select->setCountQuery($count_select);
  32. $queried_nodes = $select->execute()
  33. ->fetchAllAssoc('nid');
  34. $output = '<ul>';
  35. foreach ($queried_nodes as $node) {
  36. $output .= '<li>' . l($node->title, "node/$node->nid") . ' - ' . format_plural($node->votes, '1 vote', '@count votes') . ' - ' . ($node->active ? t('open') : t('closed')) . '</li>';
  37. }
  38. $output .= '</ul>';
  39. $output .= theme('pager');
  40. return $output;
  41. }
  42. /**
  43. * Callback for the 'votes' tab for polls you can see other votes on
  44. */
  45. function poll_votes($node) {
  46. $votes_per_page = 20;
  47. drupal_set_title($node->title);
  48. $header[] = array('data' => t('Visitor'), 'field' => 'u.name');
  49. $header[] = array('data' => t('Vote'), 'field' => 'pc.chtext');
  50. $header[] = array('data' => t('Timestamp'), 'field' => 'pv.timestamp', 'sort' => 'desc');
  51. $select = db_select('poll_vote', 'pv')->extend('PagerDefault')->extend('TableSort');
  52. $select->join('poll_choice', 'pc', 'pv.chid = pc.chid');
  53. $select->join('users', 'u', 'pv.uid = u.uid');
  54. $queried_votes = $select
  55. ->addTag('translatable')
  56. ->fields('pv', array('chid', 'uid', 'hostname', 'timestamp', 'nid'))
  57. ->fields('pc', array('chtext'))
  58. ->fields('u', array('name'))
  59. ->condition('pv.nid', $node->nid)
  60. ->limit($votes_per_page)
  61. ->orderByHeader($header)
  62. ->execute();
  63. $rows = array();
  64. foreach ($queried_votes as $vote) {
  65. $rows[] = array(
  66. $vote->name ? theme('username', array('account' => $vote)) : check_plain($vote->hostname),
  67. check_plain($vote->chtext),
  68. format_date($vote->timestamp),
  69. );
  70. }
  71. $build['poll_votes_table'] = array(
  72. '#theme' => 'table',
  73. '#header' => $header,
  74. '#rows' => $rows,
  75. '#prefix' => 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.'),
  76. );
  77. $build['poll_votes_pager'] = array('#theme' => 'pager');
  78. return $build;
  79. }
  80. /**
  81. * Callback for the 'results' tab for polls you can vote on
  82. */
  83. function poll_results($node) {
  84. drupal_set_title($node->title);
  85. $node->show_results = TRUE;
  86. return node_show($node);
  87. }