summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDries Buytaert <dries@buytaert.net>2007-08-02 10:46:53 +0000
committerDries Buytaert <dries@buytaert.net>2007-08-02 10:46:53 +0000
commitd31f65a93c78ef1269991af293e835a3bbd0870d (patch)
tree1d8febcbf83fd05bfffd00658a67d22e35b510a4
parent79d8390f9041a4ac992924a5d0b99315f2366dc8 (diff)
downloadbrdo-d31f65a93c78ef1269991af293e835a3bbd0870d.tar.gz
brdo-d31f65a93c78ef1269991af293e835a3bbd0870d.tar.bz2
- Patch #162871 by merlinofchaos: poll module cleanup + tplified the module.
-rw-r--r--modules/poll/poll-bar-block.tpl.php25
-rw-r--r--modules/poll/poll-bar.tpl.php25
-rw-r--r--modules/poll/poll-results-block.tpl.php29
-rw-r--r--modules/poll/poll-results.tpl.php28
-rw-r--r--modules/poll/poll-vote.tpl.php29
-rw-r--r--modules/poll/poll.css4
-rw-r--r--modules/poll/poll.module653
7 files changed, 467 insertions, 326 deletions
diff --git a/modules/poll/poll-bar-block.tpl.php b/modules/poll/poll-bar-block.tpl.php
new file mode 100644
index 000000000..9f303eccd
--- /dev/null
+++ b/modules/poll/poll-bar-block.tpl.php
@@ -0,0 +1,25 @@
+<?php
+// $Id$
+/**
+ * @file poll-bar-block.tpl.php
+ * Display the bar for a single choice in a poll
+ *
+ * Variables available:
+ * - $title: The title of the poll.
+ * - $votes: The number of votes for this choice
+ * - $total_votes: The number of votes for this choice
+ * - $percentage: The percentage of votes for this choice.
+ * - $vote: The choice number of the current user's vote.
+ * - $voted: Set to TRUE if the user voted for this choice.
+ *
+ * @see template_preprocess_poll_bar()
+ */
+?>
+
+<div class="text"><?php print $title; ?></div>
+<div class="bar">
+ <div style="width: <?php print $percentage; ?>%;" class="foreground"></div>
+</div>
+<div class="percent">
+ <?php print $percentage; ?>%
+</div>
diff --git a/modules/poll/poll-bar.tpl.php b/modules/poll/poll-bar.tpl.php
new file mode 100644
index 000000000..550592be7
--- /dev/null
+++ b/modules/poll/poll-bar.tpl.php
@@ -0,0 +1,25 @@
+<?php
+// $Id$
+/**
+ * @file poll-bar.tpl.php
+ * Display the bar for a single choice in a poll
+ *
+ * Variables available:
+ * - $title: The title of the poll.
+ * - $votes: The number of votes for this choice
+ * - $total_votes: The number of votes for this choice
+ * - $percentage: The percentage of votes for this choice.
+ * - $vote: The choice number of the current user's vote.
+ * - $voted: Set to TRUE if the user voted for this choice.
+ *
+ * @see template_preprocess_poll_bar()
+ */
+?>
+
+<div class="text"><?php print $title; ?></div>
+<div class="bar">
+ <div style="width: <?php print $percentage; ?>%;" class="foreground"></div>
+</div>
+<div class="percent">
+ <?php print $percentage; ?>% (<?php print format_plural($votes, '1 vote', '@count votes'); ?>)
+</div>
diff --git a/modules/poll/poll-results-block.tpl.php b/modules/poll/poll-results-block.tpl.php
new file mode 100644
index 000000000..d648e58d1
--- /dev/null
+++ b/modules/poll/poll-results-block.tpl.php
@@ -0,0 +1,29 @@
+<?php
+// $Id$
+/**
+ * @file poll-results-block.tpl.php
+ * Display the poll results in a block.
+ *
+ * Variables available:
+ * - $title: The title of the poll.
+ * - $results: The results of the poll.
+ * - $votes: The total results in the poll.
+ * - $links: Links in the poll.
+ * - $nid: The nid of the poll
+ * - $cancel_form: A form to cancel the user's vote, if allowed.
+ * - $raw_links: The raw array of links. Should be run through theme('links')
+ * if used.
+ * - $vote: The choice number of the current user's vote.
+ *
+ * @see template_preprocess_poll_results()
+ */
+?>
+
+<div class="poll">
+ <div class="title"><?php print $title ?></div>
+ <?php print $results ?>
+ <div class="total">
+ <?php print t('Total votes: @votes', array('@votes' => $votes)); ?>
+ </div>
+</div>
+<div class="links"><?php print $links; ?></div>
diff --git a/modules/poll/poll-results.tpl.php b/modules/poll/poll-results.tpl.php
new file mode 100644
index 000000000..690d3ead1
--- /dev/null
+++ b/modules/poll/poll-results.tpl.php
@@ -0,0 +1,28 @@
+<?php
+// $Id$
+/**
+ * @file poll-results-block.tpl.php
+ * Display the poll results in a block.
+ *
+ * Variables available:
+ * - $title: The title of the poll.
+ * - $results: The results of the poll.
+ * - $votes: The total results in the poll.
+ * - $links: Links in the poll.
+ * - $nid: The nid of the poll
+ * - $cancel_form: A form to cancel the user's vote, if allowed.
+ * - $raw_links: The raw array of links.
+ * - $vote: The choice number of the current user's vote.
+ *
+ * @see template_preprocess_poll_results()
+ */
+?>
+<div class="poll">
+ <?php print $results; ?>
+ <div class="total">
+ <?php print t('Total votes: @votes', array('@votes' => $votes)); ?>
+ </div>
+ <?php if (!empty($cancel_form)): ?>
+ <?php print $cancel_form; ?>
+ <?php endif; ?>
+</div>
diff --git a/modules/poll/poll-vote.tpl.php b/modules/poll/poll-vote.tpl.php
new file mode 100644
index 000000000..f3b4a407b
--- /dev/null
+++ b/modules/poll/poll-vote.tpl.php
@@ -0,0 +1,29 @@
+<?php
+// $Id$
+/**
+ * @file poll-vote.tpl.php
+ * Voting form for a poll.
+ *
+ * - $choice: The radio buttons for the choices in the poll.
+ * - $title: The title of the poll.
+ * - $block: True if this is being displayed as a block.
+ * - $vote: The vote button
+ * - $rest: Anything else in the form that may have been added via
+ * form_alter hooks.
+ *
+ * @see template_preprocess_poll_vote()
+ */
+?>
+<div class="poll">
+ <div class="vote-form">
+ <div class="choices">
+ <?php if ($block): ?>
+ <div class="title"><?php print $title; ?>:</div>
+ <?php endif; ?>
+ <?php print $choice; ?>
+ </div>
+ <?php print $vote; ?>
+ </div>
+ <?php // This is the 'rest' of the form, in case items have been added. ?>
+ <?php print $rest ?>
+</div>
diff --git a/modules/poll/poll.css b/modules/poll/poll.css
index d5eeb3ffa..67695b054 100644
--- a/modules/poll/poll.css
+++ b/modules/poll/poll.css
@@ -27,6 +27,10 @@
margin: 0 auto;
display: table;
}
+.poll .vote-form .choices .title {
+ font-weight: bold;
+}
+
.node-form .poll-form fieldset {
display: block;
}
diff --git a/modules/poll/poll.module b/modules/poll/poll.module
index 15585d90a..e6952e06a 100644
--- a/modules/poll/poll.module
+++ b/modules/poll/poll.module
@@ -21,23 +21,40 @@ function poll_help($path, $arg) {
}
/**
+ * Implementation of hook_init().
+ */
+function poll_init() {
+ drupal_add_css(drupal_get_path('module', 'poll') .'/poll.css');
+}
+
+/**
* Implementation of hook_theme()
*/
function poll_theme() {
return array(
- 'poll_view_voting' => array(
+ 'poll_vote' => array(
+ 'file' => 'poll-vote',
'arguments' => array('form' => NULL),
),
'poll_results' => array(
- 'arguments' => array('title' => NULL, 'results' => NULL, 'votes' => NULL, 'links' => NULL, 'block' => NULL, 'nid' => NULL, 'vote' => NULL),
+ 'file' => 'poll-results',
+ 'arguments' => array('raw_title' => NULL, 'results' => NULL, 'votes' => NULL, 'raw_links' => NULL, 'block' => NULL, 'nid' => NULL, 'vote' => NULL),
),
'poll_bar' => array(
- 'arguments' => array('title' => NULL, 'percentage' => NULL, 'votes' => NULL, 'block' => NULL),
+ 'file' => 'poll-bar',
+ 'arguments' => array('title' => NULL, 'votes' => NULL, 'total_votes' => NULL, 'vote' => NULL, 'block' => NULL),
),
);
}
/**
+ * Implementation of hook_perm().
+ */
+function poll_perm() {
+ return array('create polls', 'vote on polls', 'cancel own vote', 'inspect all votes');
+}
+
+/**
* Implementation of hook_access().
*/
function poll_access($op, $node) {
@@ -47,6 +64,46 @@ function poll_access($op, $node) {
}
/**
+ * Implementation of hook_menu().
+ */
+function poll_menu() {
+ $items['poll'] = array(
+ 'title' => 'Polls',
+ 'page callback' => 'poll_page',
+ 'access arguments' => array('access content'),
+ 'type' => MENU_SUGGESTED_ITEM,
+ );
+
+ $items['node/%node/votes'] = array(
+ 'title' => 'Votes',
+ 'page callback' => 'poll_votes',
+ 'page arguments' => array(1),
+ 'access callback' => '_poll_menu_access',
+ 'access arguments' => array(1, 'inspect all votes', FALSE),
+ 'weight' => 3,
+ 'type' => MENU_LOCAL_TASK,
+ );
+
+ $items['node/%node/results'] = array(
+ 'title' => 'Results',
+ 'page callback' => 'poll_results',
+ 'page arguments' => array(1),
+ 'access callback' => '_poll_menu_access',
+ 'access arguments' => array(1, 'access content', TRUE),
+ 'weight' => 3,
+ 'type' => MENU_LOCAL_TASK,
+ );
+ return $items;
+}
+
+/**
+ * Callback function to see if a node is acceptable for poll menu items.
+ */
+function _poll_menu_access($node, $perm, $inspect_allowvotes) {
+ return user_access($perm) && ($node->type == 'poll') && ($node->allowvotes || !$inspect_allowvotes);
+}
+
+/**
* Implementation of hook_block().
*
* Generates a block containing the latest poll.
@@ -88,49 +145,18 @@ function poll_cron() {
}
/**
- * Implementation of hook_delete().
- */
-function poll_delete($node) {
- db_query("DELETE FROM {poll} WHERE nid = %d", $node->nid);
- db_query("DELETE FROM {poll_choices} WHERE nid = %d", $node->nid);
- db_query("DELETE FROM {poll_votes} WHERE nid = %d", $node->nid);
-}
-
-/**
- * Implementation of hook_submit().
- */
-function poll_node_form_submit(&$form, &$form_state) {
- // Renumber fields
- $form_state['values']['choice'] = array_values($form_state['values']['choice']);
- $form_state['values']['teaser'] = poll_teaser((object)$form_state['values']);
- $form_state['choices'] = $form_state['values']['choices'];
- if ($form_state['values']['morechoices']) {
- $form_state['choices'] *= 2;
- }
-}
-
-/**
- * Implementation of hook_validate().
+ * Implementation of hook_node_info().
*/
-function poll_validate($node) {
- if (isset($node->title)) {
- // Check for at least two options and validate amount of votes:
- $realchoices = 0;
- // Renumber fields
- $node->choice = array_values($node->choice);
- foreach ($node->choice as $i => $choice) {
- if ($choice['chtext'] != '') {
- $realchoices++;
- }
- if ($choice['chvotes'] < 0) {
- form_set_error("choice][$i][chvotes", t('Negative values are not allowed.'));
- }
- }
-
- if ($realchoices < 2) {
- form_set_error("choice][$realchoices][chtext", t('You must fill in at least two choices.'));
- }
- }
+function poll_node_info() {
+ return array(
+ 'poll' => array(
+ 'name' => t('Poll'),
+ 'module' => 'poll',
+ 'description' => t("A poll is a multiple-choice question which visitors can vote on."),
+ 'title_label' => t('Question'),
+ 'has_body' => FALSE,
+ )
+ );
}
/**
@@ -223,67 +249,41 @@ function poll_form(&$node, $form_state) {
return $form;
}
-function poll_insert($node) {
- if (!user_access('administer nodes')) {
- // Make sure all votes are 0 initially
- foreach ($node->choice as $i => $choice) {
- $node->choice[$i]['chvotes'] = 0;
- }
- $node->active = 1;
- }
-
- db_query("INSERT INTO {poll} (nid, runtime, active) VALUES (%d, %d, %d)", $node->nid, $node->runtime, $node->active);
-
- foreach ($node->choice as $choice) {
- if ($choice['chtext'] != '') {
- db_query("INSERT INTO {poll_choices} (nid, chtext, chvotes, chorder) VALUES (%d, '%s', %d, %d)", $node->nid, $choice['chtext'], $choice['chvotes'], $i++);
- }
+/**
+ * Implementation of hook_submit().
+ */
+function poll_node_form_submit(&$form, &$form_state) {
+ // Renumber fields
+ $form_state['values']['choice'] = array_values($form_state['values']['choice']);
+ $form_state['values']['teaser'] = poll_teaser((object)$form_state['values']);
+ $form_state['choices'] = $form_state['values']['choices'];
+ if ($form_state['values']['morechoices']) {
+ $form_state['choices'] *= 2;
}
}
-function _poll_menu_access($node, $perm, $inspect_allowvotes) {
- return user_access($perm) && ($node->type == 'poll') && ($node->allowvotes || !$inspect_allowvotes);
-}
-
/**
- * Implementation of hook_menu().
+ * Implementation of hook_validate().
*/
-function poll_menu() {
- $items['poll'] = array(
- 'title' => 'Polls',
- 'page callback' => 'poll_page',
- 'access arguments' => array('access content'),
- 'type' => MENU_SUGGESTED_ITEM,
- );
-
- $items['poll/cancel/%node'] = array(
- 'title' => 'Cancel',
- 'page callback' => 'poll_cancel',
- 'page arguments' => array(2),
- 'access arguments' => array('cancel own vote'),
- 'type' => MENU_CALLBACK,
- );
- $items['node/%node/votes'] = array(
- 'title' => 'Votes',
- 'page callback' => 'poll_votes',
- 'access callback' => '_poll_menu_access',
- 'access arguments' => array(1, 'inspect all votes', FALSE),
- 'weight' => 3,
- 'type' => MENU_LOCAL_TASK,
- );
- $items['node/%node/results'] = array(
- 'title' => 'Results',
- 'page callback' => 'poll_results',
- 'access callback' => '_poll_menu_access',
- 'access arguments' => array(1, 'access content', TRUE),
- 'weight' => 3,
- 'type' => MENU_LOCAL_TASK,
- );
- return $items;
-}
+function poll_validate($node) {
+ if (isset($node->title)) {
+ // Check for at least two options and validate amount of votes:
+ $realchoices = 0;
+ // Renumber fields
+ $node->choice = array_values($node->choice);
+ foreach ($node->choice as $i => $choice) {
+ if ($choice['chtext'] != '') {
+ $realchoices++;
+ }
+ if ($choice['chvotes'] < 0) {
+ form_set_error("choice][$i][chvotes", t('Negative values are not allowed.'));
+ }
+ }
-function poll_init() {
- drupal_add_css(drupal_get_path('module', 'poll') .'/poll.css');
+ if ($realchoices < 2) {
+ form_set_error("choice][$realchoices][chtext", t('You must fill in at least two choices.'));
+ }
+ }
}
/**
@@ -321,20 +321,97 @@ function poll_load($node) {
}
/**
- * Implementation of hook_node_info().
+ * Implementation of hook_insert().
*/
-function poll_node_info() {
- return array(
- 'poll' => array(
- 'name' => t('Poll'),
- 'module' => 'poll',
- 'description' => t("A poll is a multiple-choice question which visitors can vote on."),
- 'title_label' => t('Question'),
- 'has_body' => FALSE,
- )
- );
+function poll_insert($node) {
+ if (!user_access('administer nodes')) {
+ // Make sure all votes are 0 initially
+ foreach ($node->choice as $i => $choice) {
+ $node->choice[$i]['chvotes'] = 0;
+ }
+ $node->active = 1;
+ }
+
+ db_query("INSERT INTO {poll} (nid, runtime, active) VALUES (%d, %d, %d)", $node->nid, $node->runtime, $node->active);
+
+ $i = 0;
+ foreach ($node->choice as $choice) {
+ if ($choice['chtext'] != '') {
+ db_query("INSERT INTO {poll_choices} (nid, chtext, chvotes, chorder) VALUES (%d, '%s', %d, %d)", $node->nid, $choice['chtext'], $choice['chvotes'], $i++);
+ }
+ }
+}
+
+/**
+ * Implementation of hook_update().
+ */
+function poll_update($node) {
+ db_query('UPDATE {poll} SET runtime = %d, active = %d WHERE nid = %d', $node->runtime, $node->active, $node->nid);
+
+ db_query('DELETE FROM {poll_choices} WHERE nid = %d', $node->nid);
+ db_query('DELETE FROM {poll_votes} WHERE nid = %d', $node->nid);
+
+ $i = 0;
+ foreach ($node->choice as $choice) {
+ $chvotes = (int)$choice['chvotes'];
+ $chtext = $choice['chtext'];
+
+ if ($chtext != '') {
+ db_query("INSERT INTO {poll_choices} (nid, chtext, chvotes, chorder) VALUES (%d, '%s', %d, %d)", $node->nid, $chtext, $chvotes, $i++);
+ }
+ }
+}
+
+/**
+ * Implementation of hook_delete().
+ */
+function poll_delete($node) {
+ db_query("DELETE FROM {poll} WHERE nid = %d", $node->nid);
+ db_query("DELETE FROM {poll_choices} WHERE nid = %d", $node->nid);
+ db_query("DELETE FROM {poll_votes} WHERE nid = %d", $node->nid);
+}
+
+/**
+ * Implementation of hook_view().
+ *
+ * @param $block
+ * An extra parameter that adapts the hook to display a block-ready
+ * rendering of the poll.
+ */
+function poll_view($node, $teaser = FALSE, $page = FALSE, $block = FALSE) {
+ global $user;
+ $output = '';
+
+ // Special display for side-block
+ if ($block) {
+ // No 'read more' link
+ $node->readmore = FALSE;
+
+ $links = module_invoke_all('link', 'node', $node, 1);
+ $links[] = array('title' => t('Older polls'), 'href' => 'poll', 'attributes' => array('title' => t('View the list of polls on this site.')));
+ if ($node->allowvotes && $block) {
+ $links[] = array('title' => t('Results'), 'href' => 'node/'. $node->nid .'/results', 'attributes' => array('title' => t('View the current poll results.')));
+ }
+
+ $node->links = $links;
+ }
+
+ if (!empty($node->allowvotes) && ($block || empty($node->show_results))) {
+ $node->content['body'] = array(
+ '#value' => drupal_get_form('poll_view_voting', $node, $block),
+ );
+ }
+ else {
+ $node->content['body'] = array(
+ '#value' => poll_view_results($node, $teaser, $page, $block),
+ );
+ }
+ return $node;
}
+/**
+ * Menu callback to provide a simple list of all polls available.
+ */
function poll_page() {
// List all polls
$sql = "SELECT n.nid, n.title, p.active, n.created, SUM(c.chvotes) AS votes FROM {node} n INNER JOIN {poll} p ON n.nid = p.nid INNER JOIN {poll_choices} c ON n.nid = c.nid WHERE n.status = 1 GROUP BY n.nid, n.title, p.active, n.created ORDER BY n.created DESC";
@@ -350,14 +427,9 @@ function poll_page() {
}
/**
- * Implementation of hook_perm().
- */
-function poll_perm() {
- return array('create polls', 'vote on polls', 'cancel own vote', 'inspect all votes');
-}
-
-/**
- * Creates a simple teaser that lists all the choices.
+ * Creates a simple teaser that lists all the choices.
+ *
+ * This is primarily used for RSS.
*/
function poll_teaser($node) {
$teaser = NULL;
@@ -371,8 +443,12 @@ function poll_teaser($node) {
/**
* Generates the voting form for a poll.
+ *
+ * @ingroup forms
+ * @see poll_vote()
+ * @see phptemplate_preprocess_poll_vote()
*/
-function poll_view_voting($node, $block) {
+function poll_view_voting(&$form_state, $node, $block) {
if ($node->choice) {
$list = array();
foreach ($node->choice as $i => $choice) {
@@ -380,32 +456,66 @@ function poll_view_voting($node, $block) {
}
$form['choice'] = array(
'#type' => 'radios',
- '#title' => $block ? check_plain($node->title) : '',
'#default_value' => -1,
'#options' => $list,
);
}
- $form['nid'] = array('#type' => 'hidden', '#value' => $node->nid);
- $form['vote'] = array('#type' => 'submit', '#value' => t('Vote'));
- $form['#action'] = url('node/'. $node->nid);
+
+ $form['vote'] = array(
+ '#type' => 'submit',
+ '#value' => t('Vote'),
+ '#submit' => array('poll_vote'),
+ );
+
+ // Store the node so we can get to it in submit functions.
+ $form['#node'] = $node;
+ $form['#block'] = $block;
+
+ // Set form caching because we could have multiple of these forms on
+ // the same page, and we want to ensure the right one gets picked.
+ $form['#cache'] = TRUE;
+
+ // Provide a more cleanly named voting form theme.
+ $form['#theme'] = 'poll_vote';
return $form;
}
/**
+ * Submit handler for processing a vote
+ */
+function poll_vote($form, &$form_state) {
+ $node = $form['#node'];
+ $choice = $form_state['values']['choice'];
+
+ global $user;
+ if ($user->uid) {
+ db_query('INSERT INTO {poll_votes} (nid, chorder, uid) VALUES (%d, %d, %d)', $node->nid, $choice, $user->uid);
+ }
+ else {
+ db_query("INSERT INTO {poll_votes} (nid, chorder, hostname) VALUES (%d, %d, '%s')", $node->nid, $choice, ip_address());
+ }
+
+ // Add one to the votes.
+ db_query("UPDATE {poll_choices} SET chvotes = chvotes + 1 WHERE nid = %d AND chorder = %d", $node->nid, $choice);
+
+ cache_clear_all();
+ drupal_set_message(t('Your vote was recorded.'));
+
+ // Return the user to whatever page they voted from.
+}
+
+/**
* Themes the voting form for a poll.
+ *
+ * Inputs: $form
*/
-function theme_poll_view_voting($form) {
- $output .= '<div class="poll">';
- $output .= ' <div class="vote-form">';
- $output .= ' <div class="choices">';
- $output .= drupal_render($form['choice']);
- $output .= ' </div>';
- $output .= drupal_render($form['nid']);
- $output .= drupal_render($form['vote']);
- $output .= ' </div>';
- $output .= drupal_render($form);
- $output .= '</div>';
- return $output;
+function template_preprocess_poll_vote(&$variables) {
+ $form = $variables['form'];
+ $variables['choice'] = drupal_render($form['choice']);
+ $variables['title'] = check_plain($form['#node']->title);
+ $variables['vote'] = drupal_render($form['vote']);
+ $variables['rest'] = drupal_render($form);
+ $variables['block'] = $form['#block'];
}
/**
@@ -423,233 +533,124 @@ function poll_view_results(&$node, $teaser, $page, $block) {
$poll_results = '';
foreach ($node->choice as $i => $choice) {
if ($choice['chtext'] != '') {
- $poll_results .= theme('poll_bar', check_plain($choice['chtext']), round($choice['chvotes'] * 100 / max($total_votes, 1)), format_plural($choice['chvotes'], '1 vote', '@count votes'), $block);
- }
- }
-
- return theme('poll_results', check_plain($node->title), $poll_results, $total_votes, isset($node->links) ? $node->links : array(), $block, $node->nid, isset($node->vote) ? $node->vote : NULL);
-}
-
-function theme_poll_results($title, $results, $votes, $links, $block, $nid, $vote) {
- $output = '';
- if ($block) {
- $output .= '<div class="poll">';
- $output .= '<div class="title">'. $title .'</div>';
- $output .= $results;
- $output .= '<div class="total">'. t('Total votes: %votes', array('%votes' => $votes)) .'</div>';
- $output .= '</div>';
- $output .= '<div class="links">'. theme('links', $links) .'</div>';
- }
- else {
- $output .= '<div class="poll">';
- $output .= $results;
- $output .= '<div class="total">'. t('Total votes: %votes', array('%votes' => $votes)) .'</div>';
- if (isset($vote) && $vote > -1 && user_access('cancel own vote')) {
- $output .= drupal_get_form('poll_cancel_form', $nid);
+ $poll_results .= theme('poll_bar', $choice['chtext'], $choice['chvotes'], $total_votes, isset($node->vote) && $node->vote == $i, $block);
}
- $output .= '</div>';
- }
-
- return $output;
-}
-
-function poll_cancel_form(&$form_state, $nid) {
- $form['#action'] = url("poll/cancel/$nid");
- $form['submit'] = array('#type' => 'submit', '#value' => t('Cancel your vote'));
- return $form;
-}
-
-function theme_poll_bar($title, $percentage, $votes, $block) {
- if ($block) {
- $output = '<div class="text">'. $title .'</div>';
- $output .= '<div class="bar"><div style="width: '. $percentage .'%;" class="foreground"></div></div>';
- $output .= '<div class="percent">'. $percentage .'%</div>';
- }
- else {
- $output = '<div class="text">'. $title .'</div>';
- $output .= '<div class="bar"><div style="width: '. $percentage .'%;" class="foreground"></div></div>';
- $output .= '<div class="percent">'. $percentage .'% ('. $votes .')</div>';
}
- return $output;
+ return theme('poll_results', $node->title, $poll_results, $total_votes, isset($node->links) ? $node->links : array(), $block, $node->nid, isset($node->vote) ? $node->vote : NULL);
}
/**
- * Callback for the 'results' tab for polls you can vote on
+ * Preprocess the poll_results theme hook.
+ *
+ * Inputs: $raw_title, $results, $votes, $raw_links, $block, $nid, $vote. The
+ * $raw_* inputs to this are naturally unsafe; often safe versions are
+ * made to simply overwrite the raw version, but in this case it seems likely
+ * that the title and the links may be overridden by the theme layer, so they
+ * are left in with a different name for that purpose.
+ *
+ * @see poll-results.tpl.php
+ * @see poll-results-block.tpl.php
+ * @see theme_poll_results()
*/
-function poll_results() {
- if ($node = node_load(arg(1))) {
- drupal_set_title(check_plain($node->title));
- return node_show($node, 0);
+function template_preprocess_poll_results(&$variables) {
+ $variables['links'] = theme('links', $variables['raw_links']);
+ if (isset($variables['vote']) && $variables['vote'] > -1 && user_access('cancel own vote')) {
+ $variables['cancel_form'] = drupal_get_form('poll_cancel_form', $variables['nid']);
}
- else {
- drupal_not_found();
+ $variables['title'] = check_plain($variables['raw_title']);
+
+ // If this is a block, allow a different tpl.php to be used.
+ if ($variables['block']) {
+ $variables['template_files'][] = 'poll-results-block';
}
}
/**
- * Callback for the 'votes' tab for polls you can see other votes on
+ * Preprocess the poll_bar theme hook.
+ *
+ * Inputs: $title, $votes, $total_votes, $voted, $block
+ *
+ * @see poll-bar.tpl.php
+ * @see poll-bar-block.tpl.php
+ * @see theme_poll_bar()
*/
-function poll_votes() {
- if ($node = node_load(arg(1))) {
- 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'), 'field' => 'pv.chorder');
-
- $result = pager_query("SELECT pv.chorder, pv.uid, pv.hostname, u.name FROM {poll_votes} pv LEFT JOIN {users} u ON pv.uid = u.uid WHERE pv.nid = %d" . tablesort_sql($header), 20, 0, NULL, $node->nid);
- $rows = array();
- while ($vote = db_fetch_object($result)) {
- $rows[] = array(
- $vote->name ? theme('username', $vote) : check_plain($vote->hostname),
- check_plain($node->choice[$vote->chorder]['chtext']));
- }
- $output .= theme('table', $header, $rows);
- $output .= theme('pager', NULL, 20, 0);
- print theme('page', $output);
- }
- else {
- drupal_not_found();
+function template_preprocess_poll_bar(&$variables) {
+ if ($variables['block']) {
+ $variables['template_files'][] = 'poll-bar-block';
}
+ $variables['title'] = check_plain($variables['title']);
+ $variables['percentage'] = round($variables['votes'] * 100 / max($variables['total_votes'], 1));
}
/**
- * Callback for processing a vote
+ * Builds the cancel form for a poll.
+ *
+ * @ingroup forms
+ * @see poll_cancel()
*/
-function poll_vote(&$node) {
- global $user;
- $nid = arg(1);
-
- if ($node = node_load($nid)) {
- $edit = $_POST;
- $choice = $edit['choice'];
- $vote = $_POST['vote'];
-
- if (isset($choice) && isset($node->choice[$choice])) {
- if ($node->allowvotes) {
- // Record the vote by this user or host.
- if ($user->uid) {
- db_query('INSERT INTO {poll_votes} (nid, chorder, uid) VALUES (%d, %d, %d)', $node->nid, $choice, $user->uid);
- }
- else {
- db_query("INSERT INTO {poll_votes} (nid, chorder, hostname) VALUES (%d, %d, '%s')", $node->nid, $choice, ip_address());
- }
+function poll_cancel_form(&$form_state, $nid) {
+ // Store the nid so we can get to it in submit functions.
+ $form['#nid'] = $nid;
- // Add one to the votes.
- db_query("UPDATE {poll_choices} SET chvotes = chvotes + 1 WHERE nid = %d AND chorder = %d", $node->nid, $choice);
+ $form['submit'] = array(
+ '#type' => 'submit',
+ '#value' => t('Cancel your vote'),
+ '#submit' => array('poll_cancel')
+ );
- $node->allowvotes = FALSE;
- $node->choice[$choice]['chvotes']++;
- cache_clear_all();
- drupal_set_message(t('Your vote was recorded.'));
- }
- else {
- drupal_set_message(t("You are not allowed to vote on this poll."), 'error');
- }
- }
- else {
- drupal_set_message(t("You did not specify a valid poll choice."), 'error');
- }
- drupal_goto('node/'. $nid);
- }
- else {
- drupal_not_found();
- }
-}
+ $form['#cache'] = TRUE;
+ return $form;
+}
/**
- * Callback for canceling a vote
+ * Submit callback for poll_cancel_form
*/
-function poll_cancel(&$node) {
+function poll_cancel($form, &$form_state) {
+ $node = node_load($form['#nid']);
global $user;
- $nid = arg(2);
- if ($node = node_load($nid)) {
- if ($node->type == 'poll' && $node->allowvotes == FALSE) {
- if ($user->uid) {
- db_query('DELETE FROM {poll_votes} WHERE nid = %d and uid = %d', $node->nid, $user->uid);
- }
- else {
- db_query("DELETE FROM {poll_votes} WHERE nid = %d and hostname = '%s'", $node->nid, ip_address());
- }
-
- // Subtract from the votes.
- db_query("UPDATE {poll_choices} SET chvotes = chvotes - 1 WHERE nid = %d AND chorder = %d", $node->nid, $node->vote);
- $node->allowvotes = TRUE;
- $node->choice[$node->vote]['chvotes']--;
- drupal_set_message(t('Your vote was canceled.'));
- }
- else {
- drupal_set_message(t("You are not allowed to cancel an invalid poll choice."), 'error');
- }
- drupal_goto('node/'. $nid);
+ if ($user->uid) {
+ db_query('DELETE FROM {poll_votes} WHERE nid = %d and uid = %d', $node->nid, $user->uid);
}
else {
- drupal_not_found();
+ db_query("DELETE FROM {poll_votes} WHERE nid = %d and hostname = '%s'", $node->nid, ip_address());
}
+
+ // Subtract from the votes.
+ db_query("UPDATE {poll_choices} SET chvotes = chvotes - 1 WHERE nid = %d AND chorder = %d", $node->nid, $node->vote);
}
/**
- * Implementation of hook_view().
- *
- * @param $block
- * An extra parameter that adapts the hook to display a block-ready
- * rendering of the poll.
+ * Callback for the 'results' tab for polls you can vote on
*/
-function poll_view($node, $teaser = FALSE, $page = FALSE, $block = FALSE) {
- global $user;
- $output = '';
-
- // Special display for side-block
- if ($block) {
- // No 'read more' link
- $node->readmore = FALSE;
-
- $links = module_invoke_all('link', 'node', $node, 1);
- $links[] = array('title' => t('Older polls'), 'href' => 'poll', 'attributes' => array('title' => t('View the list of polls on this site.')));
- if ($node->allowvotes && $block) {
- $links[] = array('title' => t('Results'), 'href' => 'node/'. $node->nid .'/results', 'attributes' => array('title' => t('View the current poll results.')));
- }
-
- $node->links = $links;
- }
-
- if (!empty($node->allowvotes) && ($block || arg(2) != 'results')) {
- if ($_POST['op'] == t('Vote')) {
- poll_vote($node);
- }
- $node->content['body'] = array(
- '#value' => drupal_get_form('poll_view_voting', $node, $block),
- );
- }
- else {
- $node->content['body'] = array(
- '#value' => poll_view_results($node, $teaser, $page, $block),
- );
- }
- return $node;
+function poll_results($node) {
+ drupal_set_title(check_plain($node->title));
+ $node->show_results = TRUE;
+ return node_show($node, 0);
}
/**
- * Implementation of hook_update().
+ * Callback for the 'votes' tab for polls you can see other votes on
*/
-function poll_update($node) {
- db_query('UPDATE {poll} SET runtime = %d, active = %d WHERE nid = %d', $node->runtime, $node->active, $node->nid);
-
- db_query('DELETE FROM {poll_choices} WHERE nid = %d', $node->nid);
- db_query('DELETE FROM {poll_votes} WHERE nid = %d', $node->nid);
-
- $i = 0;
- foreach ($node->choice as $choice) {
- $chvotes = (int)$choice['chvotes'];
- $chtext = $choice['chtext'];
-
- if ($chtext != '') {
- db_query("INSERT INTO {poll_choices} (nid, chtext, chvotes, chorder) VALUES (%d, '%s', %d, %d)", $node->nid, $chtext, $chvotes, $i++);
- }
- }
+function poll_votes($node) {
+ 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'), 'field' => 'pv.chorder');
+
+ $result = pager_query("SELECT pv.chorder, pv.uid, pv.hostname, u.name FROM {poll_votes} pv LEFT JOIN {users} u ON pv.uid = u.uid WHERE pv.nid = %d" . tablesort_sql($header), 20, 0, NULL, $node->nid);
+ $rows = array();
+ while ($vote = db_fetch_object($result)) {
+ $rows[] = array(
+ $vote->name ? theme('username', $vote) : check_plain($vote->hostname),
+ check_plain($node->choice[$vote->chorder]['chtext']));
+ }
+ $output .= theme('table', $header, $rows);
+ $output .= theme('pager', NULL, 20, 0);
+ return $output;
}
/**