poll.module

  1. drupal
    1. 4.6 modules/poll.module
    2. 4.7 modules/poll.module
    3. 5 modules/poll/poll.module
    4. 6 modules/poll/poll.module
    5. 7 modules/poll/poll.module
    6. 8 core/modules/poll/poll.module

Enables your site to capture votes on different topics in the form of multiple choice questions.

Functions & methods

NameDescription
poll_accessImplementation of hook_access().
poll_blockImplementation of hook_block().
poll_cancelCallback for canceling a vote
poll_cancel_form
poll_cronImplementation of hook_cron().
poll_deleteImplementation of hook_delete().
poll_formImplementation of hook_form().
poll_helpImplementation of hook_help().
poll_insert
poll_loadImplementation of hook_load().
poll_menuImplementation of hook_menu().
poll_node_infoImplementation of hook_node_info().
poll_page
poll_permImplementation of hook_perm().
poll_resultsCallback for the 'results' tab for polls you can vote on
poll_submitImplementation of hook_submit().
poll_teaserCreates a simple teaser that lists all the choices.
poll_updateImplementation of hook_update().
poll_userImplementation of hook_user().
poll_validateImplementation of hook_validate().
poll_viewImplementation of hook_view().
poll_view_resultsGenerates a graphical representation of the results of a poll.
poll_view_votingGenerates the voting form for a poll.
poll_voteCallback for processing a vote
poll_votesCallback for the 'votes' tab for polls you can see other votes on
theme_poll_bar
theme_poll_results
theme_poll_view_votingThemes the voting form for a poll.

File

modules/poll/poll.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Enables your site to capture votes on different topics in the form of multiple
  5. * choice questions.
  6. */
  7. /**
  8. * Implementation of hook_help().
  9. */
  10. function poll_help($section) {
  11. switch ($section) {
  12. case 'admin/help#poll':
  13. $output = '<p>'. t('The poll module can be used to create simple polls for site users. A poll is a simple multiple choice questionnaire which displays the cumulative results of the answers to the poll. Having polls on the site is a good way to get instant feedback from community members.') .'</p>';
  14. $output .= '<p>'. t('Users can create a poll. The title of the poll should be the question, then enter the answers and the "base" vote counts. You can also choose the time period over which the vote will run.The <a href="@poll">poll</a> item in the navigation menu will take you to a page where you can see all the current polls, vote on them (if you haven\'t already) and view the results.', array('@poll' => url('poll'))) .'</p>';
  15. $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="@poll">Poll page</a>.', array('@poll' => 'http://drupal.org/handbook/modules/poll/')) .'</p>';
  16. return $output;
  17. }
  18. }
  19. /**
  20. * Implementation of hook_access().
  21. */
  22. function poll_access($op, $node) {
  23. if ($op == 'create') {
  24. return user_access('create polls');
  25. }
  26. }
  27. /**
  28. * Implementation of hook_block().
  29. *
  30. * Generates a block containing the latest poll.
  31. */
  32. function poll_block($op = 'list', $delta = 0) {
  33. if (user_access('access content')) {
  34. if ($op == 'list') {
  35. $blocks[0]['info'] = t('Most recent poll');
  36. return $blocks;
  37. }
  38. else if ($op == 'view') {
  39. // Retrieve the latest poll.
  40. $sql = db_rewrite_sql("SELECT MAX(n.created) FROM {node} n INNER JOIN {poll} p ON p.nid = n.nid WHERE n.status = 1 AND p.active = 1");
  41. $timestamp = db_result(db_query($sql));
  42. if ($timestamp) {
  43. $poll = node_load(array('type' => 'poll', 'created' => $timestamp, 'status' => 1));
  44. if ($poll->nid) {
  45. $poll = poll_view($poll, TRUE, FALSE, TRUE);
  46. }
  47. }
  48. $block['subject'] = t('Poll');
  49. $block['content'] = drupal_render($poll->content);
  50. return $block;
  51. }
  52. }
  53. }
  54. /**
  55. * Implementation of hook_cron().
  56. *
  57. * Closes polls that have exceeded their allowed runtime.
  58. */
  59. function poll_cron() {
  60. $result = db_query('SELECT p.nid FROM {poll} p INNER JOIN {node} n ON p.nid = n.nid WHERE (n.created + p.runtime) < '. time() .' AND p.active = 1 AND p.runtime != 0');
  61. while ($poll = db_fetch_object($result)) {
  62. db_query("UPDATE {poll} SET active = 0 WHERE nid = %d", $poll->nid);
  63. }
  64. }
  65. /**
  66. * Implementation of hook_delete().
  67. */
  68. function poll_delete($node) {
  69. db_query("DELETE FROM {poll} WHERE nid = %d", $node->nid);
  70. db_query("DELETE FROM {poll_choices} WHERE nid = %d", $node->nid);
  71. db_query("DELETE FROM {poll_votes} WHERE nid = %d", $node->nid);
  72. }
  73. /**
  74. * Implementation of hook_submit().
  75. */
  76. function poll_submit(&$node) {
  77. // Renumber fields
  78. $node->choice = array_values($node->choice);
  79. $node->teaser = poll_teaser($node);
  80. }
  81. /**
  82. * Implementation of hook_validate().
  83. */
  84. function poll_validate($node) {
  85. if (isset($node->title)) {
  86. // Check for at least two options and validate amount of votes:
  87. $realchoices = 0;
  88. // Renumber fields
  89. $node->choice = array_values($node->choice);
  90. foreach ($node->choice as $i => $choice) {
  91. if ($choice['chtext'] != '') {
  92. $realchoices++;
  93. }
  94. if ($choice['chvotes'] < 0) {
  95. form_set_error("choice][$i][chvotes", t('Negative values are not allowed.'));
  96. }
  97. }
  98. if ($realchoices < 2) {
  99. form_set_error("choice][$realchoices][chtext", t('You must fill in at least two choices.'));
  100. }
  101. }
  102. }
  103. /**
  104. * Implementation of hook_form().
  105. */
  106. function poll_form($node, $form_values = NULL) {
  107. $admin = user_access('administer nodes');
  108. $type = node_get_types('type', $node);
  109. $form['title'] = array(
  110. '#type' => 'textfield',
  111. '#title' => check_plain($type->title_label),
  112. '#required' => TRUE,
  113. '#default_value' => $node->title,
  114. '#weight' => -1
  115. );
  116. if (isset($form_values)) {
  117. $choices = $form_values['choices'];
  118. if ($form_values['morechoices']) {
  119. $choices *= 2;
  120. }
  121. }
  122. else {
  123. $choices = max(2, count($node->choice) ? count($node->choice) : 5);
  124. }
  125. $form['choices'] = array(
  126. '#type' => 'hidden',
  127. '#value' => $choices,
  128. );
  129. // Poll choices
  130. $form['choice'] = array(
  131. '#type' => 'fieldset',
  132. '#title' => t('Choices'),
  133. '#prefix' => '<div class="poll-form">',
  134. '#suffix' => '</div>',
  135. '#tree' => TRUE
  136. );
  137. // We'll manually set the #parents property of this checkbox so that
  138. // it appears in the fieldset visually, but its value won't pollute
  139. // the $form_values['choice'] array.
  140. $form['choice']['morechoices'] = array(
  141. '#type' => 'checkbox',
  142. '#parents' => array('morechoices'),
  143. '#title' => t('Need more choices'),
  144. '#value' => 0,
  145. '#description' => t("If the amount of boxes above isn't enough, check this box and click the Preview button below to add some more."),
  146. '#weight' => 1,
  147. );
  148. for ($a = 0; $a < $choices; $a++) {
  149. $form['choice'][$a]['chtext'] = array(
  150. '#type' => 'textfield',
  151. '#title' => t('Choice @n', array('@n' => ($a + 1))),
  152. '#default_value' => $node->choice[$a]['chtext'],
  153. );
  154. if ($admin) {
  155. $form['choice'][$a]['chvotes'] = array(
  156. '#type' => 'textfield',
  157. '#title' => t('Votes for choice @n', array('@n' => ($a + 1))),
  158. '#default_value' => (int)$node->choice[$a]['chvotes'],
  159. '#size' => 5, '#maxlength' => 7
  160. );
  161. }
  162. }
  163. // Poll attributes
  164. $_duration = array(0 => t('Unlimited')) + drupal_map_assoc(array(86400, 172800, 345600, 604800, 1209600, 2419200, 4838400, 9676800, 31536000), "format_interval");
  165. $_active = array(0 => t('Closed'), 1 => t('Active'));
  166. if ($admin) {
  167. $form['settings'] = array('#type' => 'fieldset', '#title' => t('Settings'));
  168. $form['settings']['active'] = array(
  169. '#type' => 'radios',
  170. '#title' => t('Poll status'),
  171. '#default_value' => isset($node->active) ? $node->active : 1,
  172. '#options' => $_active,
  173. '#description' => t('When a poll is closed, visitors can no longer vote for it.')
  174. );
  175. }
  176. $form['settings']['runtime'] = array(
  177. '#type' => 'select',
  178. '#title' => t('Poll duration'),
  179. '#default_value' => $node->runtime,
  180. '#options' => $_duration,
  181. '#description' => t('After this period, the poll will be closed automatically.'),
  182. );
  183. $form['#multistep'] = TRUE;
  184. return $form;
  185. }
  186. function poll_insert($node) {
  187. if (!user_access('administer nodes')) {
  188. // Make sure all votes are 0 initially
  189. foreach ($node->choice as $i => $choice) {
  190. $node->choice[$i]['chvotes'] = 0;
  191. }
  192. $node->active = 1;
  193. }
  194. db_query("INSERT INTO {poll} (nid, runtime, active) VALUES (%d, %d, %d)", $node->nid, $node->runtime, $node->active);
  195. $i = 0;
  196. foreach ($node->choice as $choice) {
  197. if ($choice['chtext'] != '') {
  198. db_query("INSERT INTO {poll_choices} (nid, chtext, chvotes, chorder) VALUES (%d, '%s', %d, %d)", $node->nid, $choice['chtext'], $choice['chvotes'], $i++);
  199. }
  200. }
  201. }
  202. /**
  203. * Implementation of hook_menu().
  204. */
  205. function poll_menu($may_cache) {
  206. $items = array();
  207. if ($may_cache) {
  208. $items[] = array('path' => 'poll', 'title' => t('Polls'),
  209. 'callback' => 'poll_page',
  210. 'access' => user_access('access content'),
  211. 'type' => MENU_SUGGESTED_ITEM);
  212. $items[] = array('path' => 'poll/vote',
  213. 'title' => t('Vote'),
  214. 'callback' => 'poll_vote',
  215. 'access' => user_access('vote on polls'),
  216. 'type' => MENU_CALLBACK);
  217. $items[] = array('path' => 'poll/cancel',
  218. 'title' => t('Cancel'),
  219. 'callback' => 'poll_cancel',
  220. 'access' => user_access('cancel own vote'),
  221. 'type' => MENU_CALLBACK);
  222. }
  223. else {
  224. // Add the CSS for this module
  225. // We put this in !$may_cache so it's only added once per request
  226. drupal_add_css(drupal_get_path('module', 'poll') .'/poll.css');
  227. if (arg(0) == 'node' && is_numeric(arg(1))) {
  228. $node = node_load(arg(1));
  229. if ($node->type == 'poll') {
  230. $items[] = array('path' => 'node/'. arg(1) .'/votes',
  231. 'title' => t('Votes'),
  232. 'callback' => 'poll_votes',
  233. 'access' => user_access('inspect all votes'),
  234. 'weight' => 3,
  235. 'type' => MENU_LOCAL_TASK);
  236. }
  237. if ($node->type == 'poll' && $node->allowvotes) {
  238. $items[] = array('path' => 'node/'. arg(1) .'/results',
  239. 'title' => t('Results'),
  240. 'callback' => 'poll_results',
  241. 'access' => user_access('access content'),
  242. 'weight' => 3,
  243. 'type' => MENU_LOCAL_TASK);
  244. }
  245. }
  246. }
  247. return $items;
  248. }
  249. /**
  250. * Implementation of hook_load().
  251. */
  252. function poll_load($node) {
  253. global $user;
  254. $poll = db_fetch_object(db_query("SELECT runtime, active FROM {poll} WHERE nid = %d", $node->nid));
  255. // Load the appropriate choices into the $poll object.
  256. $result = db_query("SELECT chtext, chvotes, chorder FROM {poll_choices} WHERE nid = %d ORDER BY chorder", $node->nid);
  257. while ($choice = db_fetch_array($result)) {
  258. $poll->choice[$choice['chorder']] = $choice;
  259. }
  260. // Determine whether or not this user is allowed to vote.
  261. $poll->allowvotes = FALSE;
  262. if (user_access('vote on polls') && $poll->active) {
  263. if ($user->uid) {
  264. $result = db_fetch_object(db_query('SELECT chorder FROM {poll_votes} WHERE nid = %d AND uid = %d', $node->nid, $user->uid));
  265. }
  266. else {
  267. $result = db_fetch_object(db_query("SELECT chorder FROM {poll_votes} WHERE nid = %d AND hostname = '%s'", $node->nid, $_SERVER['REMOTE_ADDR']));
  268. }
  269. if (isset($result->chorder)) {
  270. $poll->vote = $result->chorder;
  271. }
  272. else {
  273. $poll->vote = -1;
  274. $poll->allowvotes = TRUE;
  275. }
  276. }
  277. return $poll;
  278. }
  279. /**
  280. * Implementation of hook_node_info().
  281. */
  282. function poll_node_info() {
  283. return array(
  284. 'poll' => array(
  285. 'name' => t('Poll'),
  286. 'module' => 'poll',
  287. 'description' => t("A poll is a multiple-choice question which visitors can vote on."),
  288. 'title_label' => t('Question'),
  289. 'has_body' => FALSE,
  290. )
  291. );
  292. }
  293. function poll_page() {
  294. // List all polls.
  295. $sql = db_rewrite_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");
  296. // Count all polls for the pager.
  297. $count_sql = db_rewrite_sql('SELECT COUNT(*) FROM {node} n INNER JOIN {poll} p ON n.nid = p.nid WHERE n.status = 1');
  298. $result = pager_query($sql, 15, 0, $count_sql);
  299. $output = '<ul>';
  300. while ($node = db_fetch_object($result)) {
  301. $output .= '<li>'. l($node->title, "node/$node->nid") .' - '. format_plural($node->votes, '1 vote', '@count votes') .' - '. ($node->active ? t('open') : t('closed')) .'</li>';
  302. }
  303. $output .= '</ul>';
  304. $output .= theme("pager", NULL, 15);
  305. return $output;
  306. }
  307. /**
  308. * Implementation of hook_perm().
  309. */
  310. function poll_perm() {
  311. return array('create polls', 'vote on polls', 'cancel own vote', 'inspect all votes');
  312. }
  313. /**
  314. * Creates a simple teaser that lists all the choices.
  315. */
  316. function poll_teaser($node) {
  317. $teaser = NULL;
  318. if (is_array($node->choice)) {
  319. foreach ($node->choice as $k => $choice) {
  320. if ($choice['chtext'] != '') {
  321. $teaser .= '* '. check_plain($choice['chtext']) ."\n";
  322. }
  323. }
  324. }
  325. return $teaser;
  326. }
  327. /**
  328. * Generates the voting form for a poll.
  329. */
  330. function poll_view_voting($node, $block) {
  331. if ($node->choice) {
  332. $list = array();
  333. foreach ($node->choice as $i => $choice) {
  334. $list[$i] = check_plain($choice['chtext']);
  335. }
  336. $form['choice'] = array(
  337. '#type' => 'radios',
  338. '#title' => $block ? check_plain($node->title) : '',
  339. '#default_value' => -1,
  340. '#options' => $list,
  341. );
  342. }
  343. $form['nid'] = array('#type' => 'hidden', '#value' => $node->nid);
  344. $form['vote'] = array('#type' => 'submit', '#value' => t('Vote'));
  345. $form['#action'] = url('node/'. $node->nid);
  346. return $form;
  347. }
  348. /**
  349. * Themes the voting form for a poll.
  350. */
  351. function theme_poll_view_voting($form) {
  352. $output .= '<div class="poll">';
  353. $output .= ' <div class="vote-form">';
  354. $output .= ' <div class="choices">';
  355. $output .= drupal_render($form['choice']);
  356. $output .= ' </div>';
  357. $output .= drupal_render($form['nid']);
  358. $output .= drupal_render($form['vote']);
  359. $output .= ' </div>';
  360. $output .= drupal_render($form);
  361. $output .= '</div>';
  362. return $output;
  363. }
  364. /**
  365. * Generates a graphical representation of the results of a poll.
  366. */
  367. function poll_view_results(&$node, $teaser, $page, $block) {
  368. // Count the votes and find the maximum
  369. foreach ($node->choice as $choice) {
  370. $total_votes += $choice['chvotes'];
  371. $max_votes = max($max_votes, $choice['chvotes']);
  372. }
  373. foreach ($node->choice as $i => $choice) {
  374. if ($choice['chtext'] != '') {
  375. $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);
  376. }
  377. }
  378. $output .= theme('poll_results', check_plain($node->title), $poll_results, $total_votes, $node->links, $block, $node->nid, $node->vote);
  379. return $output;
  380. }
  381. function theme_poll_results($title, $results, $votes, $links, $block, $nid, $vote) {
  382. if ($block) {
  383. $output .= '<div class="poll">';
  384. $output .= '<div class="title">'. $title .'</div>';
  385. $output .= $results;
  386. $output .= '<div class="total">'. t('Total votes: %votes', array('%votes' => $votes)) .'</div>';
  387. $output .= '</div>';
  388. $output .= '<div class="links">'. theme('links', $links) .'</div>';
  389. }
  390. else {
  391. $output .= '<div class="poll">';
  392. $output .= $results;
  393. $output .= '<div class="total">'. t('Total votes: %votes', array('%votes' => $votes)) .'</div>';
  394. if (isset($vote) && $vote > -1 && user_access('cancel own vote')) {
  395. $output .= drupal_get_form('poll_cancel_form', $nid);
  396. }
  397. $output .= '</div>';
  398. }
  399. return $output;
  400. }
  401. function poll_cancel_form($nid) {
  402. $form['#action'] = url("poll/cancel/$nid");
  403. $form['submit'] = array('#type' => 'submit', '#value' => t('Cancel your vote'));
  404. return $form;
  405. }
  406. function theme_poll_bar($title, $percentage, $votes, $block) {
  407. if ($block) {
  408. $output = '<div class="text">'. $title .'</div>';
  409. $output .= '<div class="bar"><div style="width: '. $percentage .'%;" class="foreground"></div></div>';
  410. $output .= '<div class="percent">'. $percentage .'%</div>';
  411. }
  412. else {
  413. $output = '<div class="text">'. $title .'</div>';
  414. $output .= '<div class="bar"><div style="width: '. $percentage .'%;" class="foreground"></div></div>';
  415. $output .= '<div class="percent">'. $percentage .'% ('. $votes .')</div>';
  416. }
  417. return $output;
  418. }
  419. /**
  420. * Callback for the 'results' tab for polls you can vote on
  421. */
  422. function poll_results() {
  423. if ($node = node_load(arg(1))) {
  424. drupal_set_title(check_plain($node->title));
  425. return node_show($node, 0);
  426. }
  427. else {
  428. drupal_not_found();
  429. }
  430. }
  431. /**
  432. * Callback for the 'votes' tab for polls you can see other votes on
  433. */
  434. function poll_votes() {
  435. if ($node = node_load(arg(1))) {
  436. drupal_set_title(check_plain($node->title));
  437. $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.');
  438. $header[] = array('data' => t('Visitor'), 'field' => 'u.name');
  439. $header[] = array('data' => t('Vote'), 'field' => 'pv.chorder');
  440. $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);
  441. $rows = array();
  442. while ($vote = db_fetch_object($result)) {
  443. $rows[] = array(
  444. $vote->name ? theme('username', $vote) : check_plain($vote->hostname),
  445. check_plain($node->choice[$vote->chorder]['chtext']));
  446. }
  447. $output .= theme('table', $header, $rows);
  448. $output .= theme('pager', NULL, 20, 0);
  449. print theme('page', $output);
  450. }
  451. else {
  452. drupal_not_found();
  453. }
  454. }
  455. /**
  456. * Callback for processing a vote
  457. */
  458. function poll_vote(&$node) {
  459. global $user;
  460. $nid = arg(1);
  461. if ($node = node_load($nid)) {
  462. $edit = $_POST;
  463. $choice = $edit['choice'];
  464. $vote = $_POST['vote'];
  465. if (isset($choice) && isset($node->choice[$choice])) {
  466. if ($node->allowvotes) {
  467. // Record the vote by this user or host.
  468. if ($user->uid) {
  469. db_query('INSERT INTO {poll_votes} (nid, chorder, uid) VALUES (%d, %d, %d)', $node->nid, $choice, $user->uid);
  470. }
  471. else {
  472. db_query("INSERT INTO {poll_votes} (nid, chorder, hostname) VALUES (%d, %d, '%s')", $node->nid, $choice, $_SERVER['REMOTE_ADDR']);
  473. }
  474. // Add one to the votes.
  475. db_query("UPDATE {poll_choices} SET chvotes = chvotes + 1 WHERE nid = %d AND chorder = %d", $node->nid, $choice);
  476. $node->allowvotes = FALSE;
  477. $node->choice[$choice]['chvotes']++;
  478. cache_clear_all();
  479. drupal_set_message(t('Your vote was recorded.'));
  480. }
  481. else {
  482. drupal_set_message(t("You are not allowed to vote on this poll."), 'error');
  483. }
  484. }
  485. else {
  486. drupal_set_message(t("You did not specify a valid poll choice."), 'error');
  487. }
  488. drupal_goto('node/'. $nid);
  489. }
  490. else {
  491. drupal_not_found();
  492. }
  493. }
  494. /**
  495. * Callback for canceling a vote
  496. */
  497. function poll_cancel(&$node) {
  498. global $user;
  499. $nid = arg(2);
  500. if ($node = node_load($nid)) {
  501. if ($node->type == 'poll' && $node->allowvotes == FALSE) {
  502. if ($user->uid) {
  503. db_query('DELETE FROM {poll_votes} WHERE nid = %d and uid = %d', $node->nid, $user->uid);
  504. }
  505. else {
  506. db_query("DELETE FROM {poll_votes} WHERE nid = %d and hostname = '%s'", $node->nid, $_SERVER['REMOTE_ADDR']);
  507. }
  508. // Subtract from the votes.
  509. db_query("UPDATE {poll_choices} SET chvotes = chvotes - 1 WHERE nid = %d AND chorder = %d", $node->nid, $node->vote);
  510. $node->allowvotes = TRUE;
  511. $node->choice[$node->vote]['chvotes']--;
  512. drupal_set_message(t('Your vote was canceled.'));
  513. }
  514. else {
  515. drupal_set_message(t("You are not allowed to cancel an invalid poll choice."), 'error');
  516. }
  517. drupal_goto('node/'. $nid);
  518. }
  519. else {
  520. drupal_not_found();
  521. }
  522. }
  523. /**
  524. * Implementation of hook_view().
  525. *
  526. * @param $block
  527. * An extra parameter that adapts the hook to display a block-ready
  528. * rendering of the poll.
  529. */
  530. function poll_view($node, $teaser = FALSE, $page = FALSE, $block = FALSE) {
  531. global $user;
  532. $output = '';
  533. // Special display for side-block
  534. if ($block) {
  535. // No 'read more' link
  536. $node->readmore = FALSE;
  537. $links = module_invoke_all('link', 'node', $node, 1);
  538. $links[] = array('title' => t('Older polls'), 'href' => 'poll', 'attributes' => array('title' => t('View the list of polls on this site.')));
  539. if ($node->allowvotes && $block) {
  540. $links[] = array('title' => t('Results'), 'href' => 'node/'. $node->nid .'/results', 'attributes' => array('title' => t('View the current poll results.')));
  541. }
  542. $node->links = $links;
  543. }
  544. if ($node->allowvotes && ($block || arg(2) != 'results')) {
  545. if ($_POST['op'] == t('Vote')) {
  546. poll_vote($node);
  547. }
  548. $node->content['body'] = array(
  549. '#value' => drupal_get_form('poll_view_voting', $node, $block),
  550. );
  551. }
  552. else {
  553. $node->content['body'] = array(
  554. '#value' => poll_view_results($node, $teaser, $page, $block),
  555. );
  556. }
  557. return $node;
  558. }
  559. /**
  560. * Implementation of hook_update().
  561. */
  562. function poll_update($node) {
  563. // Update poll settings.
  564. db_query('UPDATE {poll} SET runtime = %d, active = %d WHERE nid = %d', $node->runtime, $node->active, $node->nid);
  565. // Clean poll choices.
  566. db_query('DELETE FROM {poll_choices} WHERE nid = %d', $node->nid);
  567. // Poll choices come in the same order with the same numbers as they are in
  568. // the database, but some might have an empty title, which signifies that
  569. // they should be removed. We remove all votes to the removed options, so
  570. // people who voted on them can vote again.
  571. $new_chorder = 0;
  572. foreach ($node->choice as $old_chorder => $choice) {
  573. $chvotes = isset($choice['chvotes']) ? (int)$choice['chvotes'] : 0;
  574. $chtext = $choice['chtext'];
  575. if (!empty($chtext)) {
  576. db_query("INSERT INTO {poll_choices} (nid, chtext, chvotes, chorder) VALUES (%d, '%s', %d, %d)", $node->nid, $chtext, $chvotes, $new_chorder);
  577. if ($new_chorder != $old_chorder) {
  578. // We can only remove items in the middle, not add, so
  579. // new_chorder is always <= old_chorder, making this safe.
  580. db_query("UPDATE {poll_votes} SET chorder = %d WHERE nid = %d AND chorder = %d", $new_chorder, $node->nid, $old_chorder);
  581. }
  582. $new_chorder++;
  583. }
  584. else {
  585. db_query("DELETE FROM {poll_votes} WHERE nid = %d AND chorder = %d", $node->nid, $old_chorder);
  586. }
  587. }
  588. }
  589. /**
  590. * Implementation of hook_user().
  591. */
  592. function poll_user($op, &$edit, &$user) {
  593. if ($op == 'delete') {
  594. db_query('UPDATE {poll_votes} SET uid = 0 WHERE uid = %d', $user->uid);
  595. }
  596. }
Login or register to post comments