diff --git a/modules/poll/poll-bar-block.tpl.php b/modules/poll/poll-bar-block.tpl.php
new file mode 100644
index 0000000000000000000000000000000000000000..9f303eccd123a1e8b7d601a84347be4eb61be7df
--- /dev/null
+++ b/modules/poll/poll-bar-block.tpl.php
@@ -0,0 +1,25 @@
+
+
+
+
+
+ %
+
diff --git a/modules/poll/poll-bar.tpl.php b/modules/poll/poll-bar.tpl.php
new file mode 100644
index 0000000000000000000000000000000000000000..550592be7c778b19008d1773629ce8f9dba52bd6
--- /dev/null
+++ b/modules/poll/poll-bar.tpl.php
@@ -0,0 +1,25 @@
+
+
+
+
+
+ % ()
+
diff --git a/modules/poll/poll-results-block.tpl.php b/modules/poll/poll-results-block.tpl.php
new file mode 100644
index 0000000000000000000000000000000000000000..d648e58d14a637fec3db7b12c5df6eb832f3de2d
--- /dev/null
+++ b/modules/poll/poll-results-block.tpl.php
@@ -0,0 +1,29 @@
+
+
+
+
diff --git a/modules/poll/poll-results.tpl.php b/modules/poll/poll-results.tpl.php
new file mode 100644
index 0000000000000000000000000000000000000000..690d3ead136e297f1868c37a7e6fd748a0f4e7ca
--- /dev/null
+++ b/modules/poll/poll-results.tpl.php
@@ -0,0 +1,28 @@
+
+
+
+
+ $votes)); ?>
+
+
+
+
+
diff --git a/modules/poll/poll-vote.tpl.php b/modules/poll/poll-vote.tpl.php
new file mode 100644
index 0000000000000000000000000000000000000000..f3b4a407b793534d1b81c0a4f1bc9e20470b9d43
--- /dev/null
+++ b/modules/poll/poll-vote.tpl.php
@@ -0,0 +1,29 @@
+
+
diff --git a/modules/poll/poll.css b/modules/poll/poll.css
index d5eeb3ffa41c319f63f9d227708b59c83bcbed5b..67695b054903b9fb0317c18e6d9ee56e73cd0d07 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 15585d90abb1a88f6317309edc63965320418ff8..e6952e06a4d55c21e1b7d507ca1a7db159d8cbd4 100644
--- a/modules/poll/poll.module
+++ b/modules/poll/poll.module
@@ -20,23 +20,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().
*/
@@ -46,6 +63,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().
*
@@ -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 .= '';
- $output .= '
';
- $output .= drupal_render($form);
- $output .= '
';
- 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 .= '';
- $output .= '
'. $title .'
';
- $output .= $results;
- $output .= '
'. t('Total votes: %votes', array('%votes' => $votes)) .'
';
- $output .= '
';
- $output .= ''. theme('links', $links) .'
';
- }
- else {
- $output .= '';
- $output .= $results;
- $output .= '
'. t('Total votes: %votes', array('%votes' => $votes)) .'
';
- 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 .= '
';
- }
-
- 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 = ''. $title .'
';
- $output .= '';
- $output .= ''. $percentage .'%
';
- }
- else {
- $output = ''. $title .'
';
- $output .= '';
- $output .= ''. $percentage .'% ('. $votes .')
';
}
- 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;
}
/**