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