5 * Enables your site to capture votes on different topics in the form of multiple
10 * Implements hook_help().
12 function poll_help($path, $arg) {
14 case 'admin/help#poll':
16 $output .= '<h3>' . t('About') . '</h3>';
17 $output .= '<p>' . t('The Poll module can be used to create simple surveys or questionnaires that display cumulative results. A poll is a good way to receive feedback from site users and community members. For more information, see the online handbook entry for the <a href="@poll">Poll module</a>.', array('@poll' => 'http://drupal.org/documentation/modules/poll/')) . '</p>';
18 $output .= '<h3>' . t('Uses') . '</h3>';
20 $output .= '<dt>' . t('Creating a poll') . '</dt>';
21 $output .= '<dd>' . t('Users can create a poll by clicking on Poll on the <a href="@add-content">Add new content</a> page, and entering the question being posed, the answer choices, and beginning vote counts for each choice. The status (closed or active) and duration (length of time the poll remains active for new votes) can also be specified.', array('@add-content' => url('node/add'))) . '</dd>';
22 $output .= '<dt>' . t('Viewing polls') . '</dt>';
23 $output .= '<dd>' . t('You can visit the <a href="@poll">Polls</a> page to view all current polls, or alternately enable the <em>Most recent poll</em> block on the <a href="@blocks">Blocks administration page</a>. To vote in or view the results of a specific poll, you can click on the poll itself.', array('@poll' => url('poll'), '@blocks' => url('admin/structure/block'))) . '</dd>';
30 * Implements hook_theme().
32 function poll_theme() {
35 'template' => 'poll-vote',
36 'render element' => 'form',
38 'poll_choices' => array(
39 'render element' => 'form',
41 'poll_results' => array(
42 'template' => 'poll-results',
43 'variables' => array('raw_title' => NULL, 'results' => NULL, 'votes' => NULL, 'raw_links' => NULL, 'block' => NULL, 'nid' => NULL, 'vote' => NULL),
46 'template' => 'poll-bar',
47 'variables' => array('title' => NULL, 'votes' => NULL, 'total_votes' => NULL, 'vote' => NULL, 'block' => NULL),
50 // The theme system automatically discovers the theme's functions and
51 // templates that implement more targeted "suggestions" of generic theme
52 // hooks. But suggestions implemented by a module must be explicitly
54 $theme_hooks += array(
55 'poll_results__block' => array(
56 'template' => 'poll-results--block',
57 'variables' => $theme_hooks['poll_results']['variables'],
59 'poll_bar__block' => array(
60 'template' => 'poll-bar--block',
61 'variables' => $theme_hooks['poll_bar']['variables'],
68 * Implements hook_permission().
70 function poll_permission() {
72 'vote on polls' => array(
73 'title' => t('Vote on polls'),
75 'cancel own vote' => array(
76 'title' => t('Cancel and change own votes'),
78 'inspect all votes' => array(
79 'title' => t('View details for all votes'),
87 * Implements hook_menu().
89 function poll_menu() {
90 $items['poll'] = array(
92 'page callback' => 'poll_page',
93 'access arguments' => array('access content'),
94 'type' => MENU_SUGGESTED_ITEM,
95 'file' => 'poll.pages.inc',
98 $items['node/%node/votes'] = array(
100 'page callback' => 'poll_votes',
101 'page arguments' => array(1),
102 'access callback' => '_poll_menu_access',
103 'access arguments' => array(1, 'inspect all votes', FALSE),
105 'type' => MENU_LOCAL_TASK,
106 'file' => 'poll.pages.inc',
109 $items['node/%node/results'] = array(
110 'title' => 'Results',
111 'page callback' => 'poll_results',
112 'page arguments' => array(1),
113 'access callback' => '_poll_menu_access',
114 'access arguments' => array(1, 'access content', TRUE),
116 'type' => MENU_LOCAL_TASK,
117 'file' => 'poll.pages.inc',
124 * Callback function to see if a node is acceptable for poll menu items.
126 function _poll_menu_access($node, $perm, $inspect_allowvotes) {
127 return user_access($perm) && ($node->type == 'poll') && ($node->allowvotes || !$inspect_allowvotes);
131 * Implements hook_block_info().
133 function poll_block_info() {
134 $blocks['recent']['info'] = t('Most recent poll');
135 $blocks['recent']['properties']['administrative'] = TRUE;
140 * Implements hook_block_view().
142 * Generates a block containing the latest poll.
144 function poll_block_view($delta = '') {
145 if (user_access('access content')) {
146 // Retrieve the latest poll.
147 $select = db_select('node', 'n');
148 $select->join('poll', 'p', 'p.nid = n.nid');
149 $select->fields('n', array('nid'))
150 ->condition('n.status', 1)
151 ->condition('p.active', 1)
152 ->orderBy('n.created', 'DESC')
154 ->addTag('node_access');
156 $record = $select->execute()->fetchObject();
158 $poll = node_load($record->nid);
160 $poll = poll_block_latest_poll_view($poll);
161 $block['subject'] = t('Poll');
162 $block['content'] = $poll->content;
170 * Implements hook_cron().
172 * Closes polls that have exceeded their allowed runtime.
174 function poll_cron() {
175 $nids = db_query('SELECT p.nid FROM {poll} p INNER JOIN {node} n ON p.nid = n.nid WHERE (n.created + p.runtime) < :request_time AND p.active = :active AND p.runtime <> :runtime', array(':request_time' => REQUEST_TIME, ':active' => 1, ':runtime' => 0))->fetchCol();
178 ->fields(array('active' => 0))
179 ->condition('nid', $nids, 'IN')
185 * Implements hook_node_info().
187 function poll_node_info() {
192 'description' => t('A <em>poll</em> is a question with a set of possible responses. A <em>poll</em>, once created, automatically provides a simple running count of the number of votes received for each response.'),
193 'title_label' => t('Question'),
199 * Implements hook_field_extra_fields().
201 function poll_field_extra_fields() {
202 $extra['node']['poll'] = array(
204 'choice_wrapper' => array(
205 'label' => t('Poll choices'),
206 'description' => t('Poll choices'),
210 'label' => t('Poll settings'),
211 'description' => t('Poll module settings'),
216 'poll_view_voting' => array(
217 'label' => t('Poll vote'),
218 'description' => t('Poll vote'),
221 'poll_view_results' => array(
222 'label' => t('Poll results'),
223 'description' => t('Poll results'),
233 * Implements hook_form().
235 function poll_form($node, &$form_state) {
238 $admin = user_access('bypass node access') || user_access('edit any poll content') || (user_access('edit own poll content') && $user->uid == $node->uid);
240 $type = node_type_get_type($node);
242 // The submit handlers to add more poll choices require that this form is
243 // cached, regardless of whether Ajax is used.
244 $form_state['cache'] = TRUE;
246 $form['title'] = array(
247 '#type' => 'textfield',
248 '#title' => check_plain($type->title_label),
250 '#default_value' => $node->title,
255 if (isset($form_state['choice_count'])) {
256 $choice_count = $form_state['choice_count'];
259 $choice_count = max(2, empty($node->choice) ? 2 : count($node->choice));
262 // Add a wrapper for the choices and more button.
263 $form['choice_wrapper'] = array(
266 '#prefix' => '<div class="clearfix" id="poll-choice-wrapper">',
267 '#suffix' => '</div>',
270 // Container for just the poll choices.
271 $form['choice_wrapper']['choice'] = array(
272 '#prefix' => '<div id="poll-choices">',
273 '#suffix' => '</div>',
274 '#theme' => 'poll_choices',
277 // Add the current choices to the form.
280 if (isset($node->choice)) {
281 $delta = count($node->choice);
282 foreach ($node->choice as $chid => $choice) {
283 $key = 'chid:' . $chid;
284 $form['choice_wrapper']['choice'][$key] = _poll_choice_form($key, $choice['chid'], $choice['chtext'], $choice['chvotes'], $choice['weight'], $choice_count);
285 $weight = max($choice['weight'], $weight);
289 // Add initial or additional choices.
290 $existing_delta = $delta;
291 for ($delta; $delta < $choice_count; $delta++) {
292 $key = 'new:' . ($delta - $existing_delta);
293 // Increase the weight of each new choice.
295 $form['choice_wrapper']['choice'][$key] = _poll_choice_form($key, NULL, '', 0, $weight, $choice_count);
298 // We name our button 'poll_more' to avoid conflicts with other modules using
299 // Ajax-enabled buttons with the id 'more'.
300 $form['choice_wrapper']['poll_more'] = array(
302 '#value' => t('More choices'),
303 '#attributes' => array(
304 'title' => t("If the amount of boxes above isn't enough, click here to add more choices."),
307 '#limit_validation_errors' => array(array('choice')),
308 '#submit' => array('poll_more_choices_submit'),
310 'callback' => 'poll_choice_js',
311 'wrapper' => 'poll-choices',
319 86400, 2 * 86400, 3 * 86400, 4 * 86400, 5 * 86400, 6 * 86400,
320 // 1-3 weeks (7 days).
321 604800, 2 * 604800, 3 * 604800,
322 // 1-3,6,9 months (30 days).
323 2592000, 2 * 2592000, 3 * 2592000, 6 * 2592000, 9 * 2592000,
324 // 1 year (365 days).
327 $duration = array(0 => t('Unlimited')) + drupal_map_assoc($duration, 'format_interval');
328 $active = array(0 => t('Closed'), 1 => t('Active'));
330 $form['settings'] = array(
331 '#type' => 'fieldset',
332 '#collapsible' => TRUE,
333 '#title' => t('Poll settings'),
338 $form['settings']['active'] = array(
340 '#title' => t('Poll status'),
341 '#default_value' => isset($node->active) ? $node->active : 1,
342 '#options' => $active,
343 '#description' => t('When a poll is closed, visitors can no longer vote for it.'),
346 $form['settings']['runtime'] = array(
348 '#title' => t('Poll duration'),
349 '#default_value' => isset($node->runtime) ? $node->runtime : 0,
350 '#options' => $duration,
351 '#description' => t('After this period, the poll will be closed automatically.'),
358 * Submit handler to add more choices to a poll form.
360 * This handler is run regardless of whether JS is enabled or not. It makes
361 * changes to the form state. If the button was clicked with JS disabled, then
362 * the page is reloaded with the complete rebuilt form. If the button was
363 * clicked with JS enabled, then ajax_form_callback() calls poll_choice_js() to
364 * return just the changed part of the form.
366 function poll_more_choices_submit($form, &$form_state) {
367 // If this is a Ajax POST, add 1, otherwise add 5 more choices to the form.
368 if ($form_state['values']['poll_more']) {
369 $n = $_GET['q'] == 'system/ajax' ? 1 : 5;
370 $form_state['choice_count'] = count($form_state['values']['choice']) + $n;
372 // Renumber the choices. This invalidates the corresponding key/value
373 // associations in $form_state['input'], so clear that out. This requires
374 // poll_form() to rebuild the choices with the values in
375 // $form_state['node']->choice, which it does.
376 $form_state['node']->choice = array_values($form_state['values']['choice']);
377 unset($form_state['input']['choice']);
378 $form_state['rebuild'] = TRUE;
381 function _poll_choice_form($key, $chid = NULL, $value = '', $votes = 0, $weight = 0, $size = 10) {
384 '#weight' => $weight,
387 // We'll manually set the #parents property of these fields so that
388 // their values appear in the $form_state['values']['choice'] array.
389 $form['chid'] = array(
392 '#parents' => array('choice', $key, 'chid'),
395 $form['chtext'] = array(
396 '#type' => 'textfield',
397 '#title' => $value !== '' ? t('Choice label') : t('New choice label'),
398 '#title_display' => 'invisible',
399 '#default_value' => $value,
400 '#parents' => array('choice', $key, 'chtext'),
403 $form['chvotes'] = array(
404 '#type' => 'textfield',
405 '#title' => $value !== '' ? t('Vote count for choice @label', array('@label' => $value)) : t('Vote count for new choice'),
406 '#title_display' => 'invisible',
407 '#default_value' => $votes,
410 '#parents' => array('choice', $key, 'chvotes'),
411 '#access' => user_access('administer nodes'),
412 '#element_validate' => array('element_validate_integer'),
415 $form['weight'] = array(
417 '#title' => $value !== '' ? t('Weight for choice @label', array('@label' => $value)) : t('Weight for new choice'),
418 '#title_display' => 'invisible',
419 '#default_value' => $weight,
421 '#parents' => array('choice', $key, 'weight'),
428 * Ajax callback in response to new choices being added to the form.
430 * This returns the new page content to replace the page content made obsolete
431 * by the form submission.
433 * @see poll_more_choices_submit()
435 function poll_choice_js($form, $form_state) {
436 return $form['choice_wrapper']['choice'];
440 * Form submit handler for node_form().
442 * Upon preview and final submission, we need to renumber poll choices and
443 * create a teaser output.
445 function poll_node_form_submit(&$form, &$form_state) {
447 $form_state['values']['choice'] = array_values($form_state['values']['choice']);
448 $form_state['values']['teaser'] = poll_teaser((object) $form_state['values']);
452 * Implements hook_validate().
454 function poll_validate($node, $form) {
455 if (isset($node->title)) {
456 // Check for at least two options and validate amount of votes.
458 foreach ($node->choice as $i => $choice) {
459 if ($choice['chtext'] != '') {
462 if (isset($choice['chvotes']) && $choice['chvotes'] < 0) {
463 form_set_error("choice][$i][chvotes", t('Negative values are not allowed.'));
467 if ($realchoices < 2) {
468 form_set_error("choice][$realchoices][chtext", t('You must fill in at least two choices.'));
474 * Implements hook_field_attach_prepare_translation_alter().
476 function poll_field_attach_prepare_translation_alter(&$entity, $context) {
477 if ($context['entity_type'] == 'node' && $entity->type == 'poll') {
478 $entity->choice = $context['source_entity']->choice;
479 foreach ($entity->choice as $i => $options) {
480 $entity->choice[$i]['chvotes'] = 0;
486 * Implements hook_load().
488 function poll_load($nodes) {
490 foreach ($nodes as $node) {
491 $poll = db_query("SELECT runtime, active FROM {poll} WHERE nid = :nid", array(':nid' => $node->nid))->fetchObject();
494 $poll = new stdClass();
497 // Load the appropriate choices into the $poll object.
498 $poll->choice = db_select('poll_choice', 'c')
499 ->addTag('translatable')
500 ->fields('c', array('chid', 'chtext', 'chvotes', 'weight'))
501 ->condition('c.nid', $node->nid)
503 ->execute()->fetchAllAssoc('chid', PDO::FETCH_ASSOC);
505 // Determine whether or not this user is allowed to vote.
506 $poll->allowvotes = FALSE;
507 if (user_access('vote on polls') && $poll->active) {
509 // If authenticated, find existing vote based on uid.
510 $poll->vote = db_query('SELECT chid FROM {poll_vote} WHERE nid = :nid AND uid = :uid', array(':nid' => $node->nid, ':uid' => $user->uid))->fetchField();
511 if (empty($poll->vote)) {
513 $poll->allowvotes = TRUE;
516 elseif (!empty($_SESSION['poll_vote'][$node->nid])) {
517 // Otherwise the user is anonymous. Look for an existing vote in the
519 $poll->vote = $_SESSION['poll_vote'][$node->nid];
522 // Finally, query the database for an existing vote based on anonymous
524 $poll->allowvotes = !db_query("SELECT 1 FROM {poll_vote} WHERE nid = :nid AND hostname = :hostname AND uid = 0", array(':nid' => $node->nid, ':hostname' => ip_address()))->fetchField();
527 foreach ($poll as $key => $value) {
528 $nodes[$node->nid]->$key = $value;
534 * Implements hook_insert().
536 function poll_insert($node) {
537 if (!user_access('administer nodes')) {
538 // Make sure all votes are 0 initially
539 foreach ($node->choice as $i => $choice) {
540 $node->choice[$i]['chvotes'] = 0;
548 'runtime' => $node->runtime,
549 'active' => $node->active,
553 foreach ($node->choice as $choice) {
554 if ($choice['chtext'] != '') {
555 db_insert('poll_choice')
558 'chtext' => $choice['chtext'],
559 'chvotes' => $choice['chvotes'],
560 'weight' => $choice['weight'],
568 * Implements hook_update().
570 function poll_update($node) {
571 // Update poll settings.
574 'runtime' => $node->runtime,
575 'active' => $node->active,
577 ->condition('nid', $node->nid)
580 // Poll choices with empty titles signifies removal. We remove all votes to
581 // the removed options, so people who voted on them can vote again.
582 foreach ($node->choice as $key => $choice) {
583 if (!empty($choice['chtext'])) {
584 db_merge('poll_choice')
585 ->key(array('chid' => $choice['chid']))
587 'chtext' => $choice['chtext'],
588 'chvotes' => (int) $choice['chvotes'],
589 'weight' => $choice['weight'],
591 ->insertFields(array(
593 'chtext' => $choice['chtext'],
594 'chvotes' => (int) $choice['chvotes'],
595 'weight' => $choice['weight'],
600 db_delete('poll_vote')
601 ->condition('nid', $node->nid)
602 ->condition('chid', $key)
604 db_delete('poll_choice')
605 ->condition('nid', $node->nid)
606 ->condition('chid', $choice['chid'])
613 * Implements hook_delete().
615 function poll_delete($node) {
617 ->condition('nid', $node->nid)
619 db_delete('poll_choice')
620 ->condition('nid', $node->nid)
622 db_delete('poll_vote')
623 ->condition('nid', $node->nid)
628 * Return content for 'latest poll' block.
631 * The node object to load.
633 function poll_block_latest_poll_view($node) {
634 // This is necessary for shared objects because PHP doesn't copy objects, but
635 // passes them by reference. So when the objects are cached it can result in
636 // the wrong output being displayed on subsequent calls. The cloning and
637 // unsetting of $node->content prevents the block output from being the same
638 // as the node output.
640 unset($node->content);
642 // No 'read more' link.
643 $node->readmore = FALSE;
647 $links[] = array('title' => t('Older polls'), 'href' => 'poll', 'attributes' => array('title' => t('View the list of polls on this site.')));
648 if ($node->allowvotes) {
649 $links[] = array('title' => t('Results'), 'href' => 'node/' . $node->nid . '/results', 'attributes' => array('title' => t('View the current poll results.')));
652 $node->links = $links;
654 if (!empty($node->allowvotes)) {
655 $node->content['poll_view_voting'] = drupal_get_form('poll_view_voting', $node, TRUE);
656 $node->content['links'] = array(
658 '#links' => $node->links,
663 $node->content['poll_view_results'] = array('#markup' => poll_view_results($node, TRUE, TRUE));
671 * Implements hook_view().
673 function poll_view($node, $view_mode) {
674 if (!empty($node->allowvotes) && empty($node->show_results)) {
675 $node->content['poll_view_voting'] = drupal_get_form('poll_view_voting', $node);
678 $node->content['poll_view_results'] = array('#markup' => poll_view_results($node, $view_mode));
684 * Creates a simple teaser that lists all the choices.
686 * This is primarily used for RSS.
688 function poll_teaser($node) {
690 if (is_array($node->choice)) {
691 foreach ($node->choice as $choice) {
692 if ($choice['chtext'] != '') {
693 $teaser .= '* ' . check_plain($choice['chtext']) . "\n";
701 * Generates the voting form for a poll.
705 * @see phptemplate_preprocess_poll_vote()
707 function poll_view_voting($form, &$form_state, $node, $block = FALSE) {
710 foreach ($node->choice as $i => $choice) {
711 $list[$i] = check_plain($choice['chtext']);
713 $form['choice'] = array(
715 '#title' => t('Choices'),
716 '#title_display' => 'invisible',
721 $form['vote'] = array(
723 '#value' => t('Vote'),
724 '#submit' => array('poll_vote'),
727 // Store the node so we can get to it in submit functions.
728 $form['#node'] = $node;
729 $form['#block'] = $block;
731 // Set form caching because we could have multiple of these forms on
732 // the same page, and we want to ensure the right one gets picked.
733 $form_state['cache'] = TRUE;
735 // Provide a more cleanly named voting form theme.
736 $form['#theme'] = 'poll_vote';
741 * Validation function for processing votes
743 function poll_view_voting_validate($form, &$form_state) {
744 if (empty($form_state['values']['choice'])) {
745 form_set_error( 'choice', t('Your vote could not be recorded because you did not select any of the choices.'));
750 * Submit handler for processing a vote.
752 function poll_vote($form, &$form_state) {
753 $node = $form['#node'];
754 $choice = $form_state['values']['choice'];
757 db_insert('poll_vote')
762 'hostname' => ip_address(),
763 'timestamp' => REQUEST_TIME,
767 // Add one to the votes.
768 db_update('poll_choice')
769 ->expression('chvotes', 'chvotes + 1')
770 ->condition('chid', $choice)
776 // The vote is recorded so the user gets the result view instead of the
777 // voting form when viewing the poll. Saving a value in $_SESSION has the
778 // convenient side effect of preventing the user from hitting the page
779 // cache. When anonymous voting is allowed, the page cache should only
780 // contain the voting form, not the results.
781 $_SESSION['poll_vote'][$node->nid] = $choice;
784 drupal_set_message(t('Your vote was recorded.'));
786 // Return the user to whatever page they voted from.
790 * Themes the voting form for a poll.
794 function template_preprocess_poll_vote(&$variables) {
795 $form = $variables['form'];
796 $variables['choice'] = drupal_render($form['choice']);
797 $variables['title'] = check_plain($form['#node']->title);
798 $variables['vote'] = drupal_render($form['vote']);
799 $variables['rest'] = drupal_render_children($form);
800 $variables['block'] = $form['#block'];
801 if ($variables['block']) {
802 $variables['theme_hook_suggestions'][] = 'poll_vote__block';
807 * Generates a graphical representation of the results of a poll.
809 function poll_view_results($node, $view_mode, $block = FALSE) {
810 // Make sure that choices are ordered by their weight.
811 uasort($node->choice, 'drupal_sort_weight');
813 // Count the votes and find the maximum.
816 foreach ($node->choice as $choice) {
817 if (isset($choice['chvotes'])) {
818 $total_votes += $choice['chvotes'];
819 $max_votes = max($max_votes, $choice['chvotes']);
824 foreach ($node->choice as $i => $choice) {
825 if (!empty($choice['chtext'])) {
826 $chvotes = isset($choice['chvotes']) ? $choice['chvotes'] : NULL;
827 $poll_results .= theme('poll_bar', array('title' => $choice['chtext'], 'votes' => $chvotes, 'total_votes' => $total_votes, 'vote' => isset($node->vote) && $node->vote == $i, 'block' => $block));
831 return theme('poll_results', array('raw_title' => $node->title, 'results' => $poll_results, 'votes' => $total_votes, 'raw_links' => isset($node->links) ? $node->links : array(), 'block' => $block, 'nid' => $node->nid, 'vote' => isset($node->vote) ? $node->vote : NULL));
836 * Returns HTML for an admin poll form for choices.
839 * An associative array containing:
840 * - form: A render element representing the form.
844 function theme_poll_choices($variables) {
845 $form = $variables['form'];
847 drupal_add_tabledrag('poll-choice-table', 'order', 'sibling', 'poll-weight');
849 $is_admin= user_access('administer nodes');
852 $headers = array('', t('Choice'));
854 $headers[] = t('Vote count');
856 $headers[] = t('Weight');
858 foreach (element_children($form) as $key) {
860 // Set special classes for drag and drop updating.
861 $form[$key]['weight']['#attributes']['class'] = array('poll-weight');
863 // Build the table row.
866 array('class' => array('choice-flag')),
867 drupal_render($form[$key]['chtext']),
869 'class' => array('draggable'),
872 $row['data'][] = drupal_render($form[$key]['chvotes']);
874 $row['data'][] = drupal_render($form[$key]['weight']);
876 // Add any additional classes set on the row.
877 if (!empty($form[$key]['#attributes']['class'])) {
878 $row['class'] = array_merge($row['class'], $form[$key]['#attributes']['class']);
884 $output = theme('table', array('header' => $headers, 'rows' => $rows, 'attributes' => array('id' => 'poll-choice-table')));
885 $output .= drupal_render_children($form);
890 * Preprocess the poll_results theme hook.
892 * Inputs: $raw_title, $results, $votes, $raw_links, $block, $nid, $vote. The
893 * $raw_* inputs to this are naturally unsafe; often safe versions are
894 * made to simply overwrite the raw version, but in this case it seems likely
895 * that the title and the links may be overridden by the theme layer, so they
896 * are left in with a different name for that purpose.
898 * @see poll-results.tpl.php
899 * @see poll-results--block.tpl.php
901 function template_preprocess_poll_results(&$variables) {
902 $variables['links'] = theme('links__poll_results', array('links' => $variables['raw_links']));
903 if (isset($variables['vote']) && $variables['vote'] > -1 && user_access('cancel own vote')) {
904 $elements = drupal_get_form('poll_cancel_form', $variables['nid']);
905 $variables['cancel_form'] = drupal_render($elements);
907 $variables['title'] = check_plain($variables['raw_title']);
909 if ($variables['block']) {
910 $variables['theme_hook_suggestions'][] = 'poll_results__block';
915 * Preprocess the poll_bar theme hook.
917 * Inputs: $title, $votes, $total_votes, $voted, $block
919 * @see poll-bar.tpl.php
920 * @see poll-bar--block.tpl.php
922 function template_preprocess_poll_bar(&$variables) {
923 if ($variables['block']) {
924 $variables['theme_hook_suggestions'][] = 'poll_bar__block';
926 $variables['title'] = check_plain($variables['title']);
927 $variables['percentage'] = round($variables['votes'] * 100 / max($variables['total_votes'], 1));
931 * Builds the cancel form for a poll.
936 function poll_cancel_form($form, &$form_state, $nid) {
937 $form_state['cache'] = TRUE;
939 // Store the nid so we can get to it in submit functions.
940 $form['#nid'] = $nid;
942 $form['actions'] = array('#type' => 'actions');
943 $form['actions']['submit'] = array(
945 '#value' => t('Cancel your vote'),
946 '#submit' => array('poll_cancel')
953 * Submit callback for poll_cancel_form().
955 function poll_cancel($form, &$form_state) {
957 $node = node_load($form['#nid']);
959 db_delete('poll_vote')
960 ->condition('nid', $node->nid)
961 ->condition($user->uid ? 'uid' : 'hostname', $user->uid ? $user->uid : ip_address())
964 // Subtract from the votes.
965 db_update('poll_choice')
966 ->expression('chvotes', 'chvotes - 1')
967 ->condition('chid', $node->vote)
970 unset($_SESSION['poll_vote'][$node->nid]);
972 drupal_set_message(t('Your vote was cancelled.'));
976 * Implements hook_user_cancel().
978 function poll_user_cancel($edit, $account, $method) {
980 case 'user_cancel_reassign':
981 db_update('poll_vote')
982 ->fields(array('uid' => 0))
983 ->condition('uid', $account->uid)
990 * Implements hook_user_delete().
992 function poll_user_delete($account) {
993 db_delete('poll_vote')
994 ->condition('uid', $account->uid)
999 * Implements hook_rdf_mapping().
1001 function poll_rdf_mapping() {
1007 'rdftype' => array('sioc:Post', 'sioct:Poll'),