webmaster@1
|
1 <?php |
franck@19
|
2 // $Id: poll.module,v 1.263.2.3 2008/12/18 15:46:20 dries Exp $ |
webmaster@1
|
3 |
webmaster@1
|
4 /** |
webmaster@1
|
5 * @file |
webmaster@1
|
6 * Enables your site to capture votes on different topics in the form of multiple |
webmaster@1
|
7 * choice questions. |
webmaster@1
|
8 */ |
webmaster@1
|
9 |
webmaster@1
|
10 /** |
webmaster@1
|
11 * Implementation of hook_help(). |
webmaster@1
|
12 */ |
webmaster@1
|
13 function poll_help($path, $arg) { |
webmaster@1
|
14 switch ($path) { |
webmaster@1
|
15 case 'admin/help#poll': |
webmaster@1
|
16 $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 receive feedback from community members.') .'</p>'; |
webmaster@1
|
17 $output .= '<p>'. t('When creating a poll, enter the question being posed, as well as the potential choices (and beginning vote counts for each choice). The status and duration (length of time the poll remains active for new votes) can also be specified. Use the <a href="@poll">poll</a> menu item to view all current polls. To vote in or view the results of a specific poll, click on the poll itself.', array('@poll' => url('poll'))) .'</p>'; |
webmaster@1
|
18 $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@poll">Poll module</a>.', array('@poll' => 'http://drupal.org/handbook/modules/poll/')) .'</p>'; |
webmaster@1
|
19 return $output; |
webmaster@1
|
20 } |
webmaster@1
|
21 } |
webmaster@1
|
22 |
webmaster@1
|
23 /** |
webmaster@1
|
24 * Implementation of hook_init(). |
webmaster@1
|
25 */ |
webmaster@1
|
26 function poll_init() { |
webmaster@1
|
27 drupal_add_css(drupal_get_path('module', 'poll') .'/poll.css'); |
webmaster@1
|
28 } |
webmaster@1
|
29 |
webmaster@1
|
30 /** |
webmaster@1
|
31 * Implementation of hook_theme() |
webmaster@1
|
32 */ |
webmaster@1
|
33 function poll_theme() { |
webmaster@1
|
34 return array( |
webmaster@1
|
35 'poll_vote' => array( |
webmaster@1
|
36 'template' => 'poll-vote', |
webmaster@1
|
37 'arguments' => array('form' => NULL), |
webmaster@1
|
38 ), |
webmaster@1
|
39 'poll_choices' => array( |
webmaster@1
|
40 'arguments' => array('form' => NULL), |
webmaster@1
|
41 ), |
webmaster@1
|
42 'poll_results' => array( |
webmaster@1
|
43 'template' => 'poll-results', |
webmaster@1
|
44 'arguments' => array('raw_title' => NULL, 'results' => NULL, 'votes' => NULL, 'raw_links' => NULL, 'block' => NULL, 'nid' => NULL, 'vote' => NULL), |
webmaster@1
|
45 ), |
webmaster@1
|
46 'poll_bar' => array( |
webmaster@1
|
47 'template' => 'poll-bar', |
webmaster@1
|
48 'arguments' => array('title' => NULL, 'votes' => NULL, 'total_votes' => NULL, 'vote' => NULL, 'block' => NULL), |
webmaster@1
|
49 ), |
webmaster@1
|
50 ); |
webmaster@1
|
51 } |
webmaster@1
|
52 |
webmaster@1
|
53 /** |
webmaster@1
|
54 * Implementation of hook_perm(). |
webmaster@1
|
55 */ |
webmaster@1
|
56 function poll_perm() { |
webmaster@1
|
57 return array('create poll content', 'delete own poll content', 'delete any poll content', 'edit any poll content', 'edit own poll content', 'vote on polls', 'cancel own vote', 'inspect all votes'); |
webmaster@1
|
58 } |
webmaster@1
|
59 |
webmaster@1
|
60 /** |
webmaster@1
|
61 * Implementation of hook_access(). |
webmaster@1
|
62 */ |
webmaster@1
|
63 function poll_access($op, $node, $account) { |
webmaster@1
|
64 switch ($op) { |
webmaster@1
|
65 case 'create': |
webmaster@7
|
66 return user_access('create poll content', $account) ? TRUE : NULL; |
webmaster@1
|
67 case 'update': |
webmaster@7
|
68 return user_access('edit any poll content', $account) || (user_access('edit own poll content', $account) && ($node->uid == $account->uid)) ? TRUE : NULL; |
webmaster@1
|
69 case 'delete': |
webmaster@7
|
70 return user_access('delete any poll content', $account) || (user_access('delete own poll content', $account) && ($node->uid == $account->uid)) ? TRUE : NULL; |
webmaster@1
|
71 } |
webmaster@1
|
72 } |
webmaster@1
|
73 |
webmaster@1
|
74 /** |
webmaster@1
|
75 * Implementation of hook_menu(). |
webmaster@1
|
76 */ |
webmaster@1
|
77 function poll_menu() { |
webmaster@1
|
78 $items['poll'] = array( |
webmaster@1
|
79 'title' => 'Polls', |
webmaster@1
|
80 'page callback' => 'poll_page', |
webmaster@1
|
81 'access arguments' => array('access content'), |
webmaster@1
|
82 'type' => MENU_SUGGESTED_ITEM, |
webmaster@1
|
83 'file' => 'poll.pages.inc', |
webmaster@1
|
84 ); |
webmaster@1
|
85 |
webmaster@1
|
86 $items['node/%node/votes'] = array( |
webmaster@1
|
87 'title' => 'Votes', |
webmaster@1
|
88 'page callback' => 'poll_votes', |
webmaster@1
|
89 'page arguments' => array(1), |
webmaster@1
|
90 'access callback' => '_poll_menu_access', |
webmaster@1
|
91 'access arguments' => array(1, 'inspect all votes', FALSE), |
webmaster@1
|
92 'weight' => 3, |
webmaster@1
|
93 'type' => MENU_LOCAL_TASK, |
webmaster@1
|
94 'file' => 'poll.pages.inc', |
webmaster@1
|
95 ); |
webmaster@1
|
96 |
webmaster@1
|
97 $items['node/%node/results'] = array( |
webmaster@1
|
98 'title' => 'Results', |
webmaster@1
|
99 'page callback' => 'poll_results', |
webmaster@1
|
100 'page arguments' => array(1), |
webmaster@1
|
101 'access callback' => '_poll_menu_access', |
webmaster@1
|
102 'access arguments' => array(1, 'access content', TRUE), |
webmaster@1
|
103 'weight' => 3, |
webmaster@1
|
104 'type' => MENU_LOCAL_TASK, |
webmaster@1
|
105 'file' => 'poll.pages.inc', |
webmaster@1
|
106 ); |
webmaster@1
|
107 |
webmaster@1
|
108 $items['poll/js'] = array( |
webmaster@1
|
109 'title' => 'Javascript Choice Form', |
webmaster@1
|
110 'page callback' => 'poll_choice_js', |
webmaster@1
|
111 'access arguments' => array('access content'), |
webmaster@1
|
112 'type' => MENU_CALLBACK, |
webmaster@1
|
113 ); |
webmaster@1
|
114 |
webmaster@1
|
115 return $items; |
webmaster@1
|
116 } |
webmaster@1
|
117 |
webmaster@1
|
118 /** |
webmaster@1
|
119 * Callback function to see if a node is acceptable for poll menu items. |
webmaster@1
|
120 */ |
webmaster@1
|
121 function _poll_menu_access($node, $perm, $inspect_allowvotes) { |
webmaster@1
|
122 return user_access($perm) && ($node->type == 'poll') && ($node->allowvotes || !$inspect_allowvotes); |
webmaster@1
|
123 } |
webmaster@1
|
124 |
webmaster@1
|
125 /** |
webmaster@1
|
126 * Implementation of hook_block(). |
webmaster@1
|
127 * |
webmaster@1
|
128 * Generates a block containing the latest poll. |
webmaster@1
|
129 */ |
webmaster@1
|
130 function poll_block($op = 'list', $delta = 0) { |
webmaster@1
|
131 if (user_access('access content')) { |
webmaster@1
|
132 if ($op == 'list') { |
webmaster@1
|
133 $blocks[0]['info'] = t('Most recent poll'); |
webmaster@1
|
134 return $blocks; |
webmaster@1
|
135 } |
webmaster@1
|
136 else if ($op == 'view') { |
webmaster@1
|
137 // Retrieve the latest poll. |
webmaster@1
|
138 $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"); |
webmaster@1
|
139 $timestamp = db_result(db_query($sql)); |
webmaster@1
|
140 if ($timestamp) { |
webmaster@1
|
141 $poll = node_load(array('type' => 'poll', 'created' => $timestamp, 'status' => 1)); |
webmaster@1
|
142 |
webmaster@1
|
143 if ($poll->nid) { |
webmaster@1
|
144 $poll = poll_view($poll, TRUE, FALSE, TRUE); |
webmaster@1
|
145 } |
webmaster@1
|
146 } |
webmaster@1
|
147 $block['subject'] = t('Poll'); |
webmaster@1
|
148 $block['content'] = drupal_render($poll->content); |
webmaster@1
|
149 return $block; |
webmaster@1
|
150 } |
webmaster@1
|
151 } |
webmaster@1
|
152 } |
webmaster@1
|
153 |
webmaster@1
|
154 /** |
webmaster@1
|
155 * Implementation of hook_cron(). |
webmaster@1
|
156 * |
webmaster@1
|
157 * Closes polls that have exceeded their allowed runtime. |
webmaster@1
|
158 */ |
webmaster@1
|
159 function poll_cron() { |
webmaster@1
|
160 $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'); |
webmaster@1
|
161 while ($poll = db_fetch_object($result)) { |
webmaster@1
|
162 db_query("UPDATE {poll} SET active = 0 WHERE nid = %d", $poll->nid); |
webmaster@1
|
163 } |
webmaster@1
|
164 } |
webmaster@1
|
165 |
webmaster@1
|
166 /** |
webmaster@1
|
167 * Implementation of hook_node_info(). |
webmaster@1
|
168 */ |
webmaster@1
|
169 function poll_node_info() { |
webmaster@1
|
170 return array( |
webmaster@1
|
171 'poll' => array( |
webmaster@1
|
172 'name' => t('Poll'), |
webmaster@1
|
173 'module' => 'poll', |
webmaster@1
|
174 '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.'), |
webmaster@1
|
175 'title_label' => t('Question'), |
webmaster@1
|
176 'has_body' => FALSE, |
webmaster@1
|
177 ) |
webmaster@1
|
178 ); |
webmaster@1
|
179 } |
webmaster@1
|
180 |
webmaster@1
|
181 /** |
webmaster@1
|
182 * Implementation of hook_form(). |
webmaster@1
|
183 */ |
webmaster@1
|
184 function poll_form(&$node, $form_state) { |
webmaster@1
|
185 global $user; |
webmaster@1
|
186 |
webmaster@1
|
187 $admin = user_access('administer nodes') || user_access('edit any poll content') || (user_access('edit own poll content') && $user->uid == $node->uid); |
webmaster@1
|
188 |
webmaster@1
|
189 $type = node_get_types('type', $node); |
webmaster@1
|
190 |
webmaster@1
|
191 $form = array( |
webmaster@1
|
192 '#cache' => TRUE, |
webmaster@1
|
193 ); |
webmaster@1
|
194 |
webmaster@1
|
195 $form['title'] = array( |
webmaster@1
|
196 '#type' => 'textfield', |
webmaster@1
|
197 '#title' => check_plain($type->title_label), |
webmaster@1
|
198 '#required' => TRUE, |
webmaster@1
|
199 '#default_value' => $node->title, |
webmaster@1
|
200 '#weight' => -5, |
webmaster@1
|
201 ); |
webmaster@1
|
202 |
webmaster@1
|
203 if (isset($form_state['choice_count'])) { |
webmaster@1
|
204 $choice_count = $form_state['choice_count']; |
webmaster@1
|
205 } |
webmaster@1
|
206 else { |
webmaster@1
|
207 $choice_count = max(2, empty($node->choice) ? 2 : count($node->choice)); |
webmaster@1
|
208 } |
webmaster@1
|
209 |
webmaster@1
|
210 // Add a wrapper for the choices and more button. |
webmaster@1
|
211 $form['choice_wrapper'] = array( |
webmaster@1
|
212 '#tree' => FALSE, |
webmaster@1
|
213 '#weight' => -4, |
webmaster@1
|
214 '#prefix' => '<div class="clear-block" id="poll-choice-wrapper">', |
webmaster@1
|
215 '#suffix' => '</div>', |
webmaster@1
|
216 ); |
webmaster@1
|
217 |
webmaster@1
|
218 // Container for just the poll choices. |
webmaster@1
|
219 $form['choice_wrapper']['choice'] = array( |
webmaster@1
|
220 '#prefix' => '<div id="poll-choices">', |
webmaster@1
|
221 '#suffix' => '</div>', |
webmaster@1
|
222 '#theme' => 'poll_choices', |
webmaster@1
|
223 ); |
webmaster@1
|
224 |
webmaster@1
|
225 // Add the current choices to the form. |
webmaster@1
|
226 for ($delta = 0; $delta < $choice_count; $delta++) { |
webmaster@1
|
227 $text = isset($node->choice[$delta]['chtext']) ? $node->choice[$delta]['chtext'] : ''; |
webmaster@1
|
228 $votes = isset($node->choice[$delta]['chvotes']) ? $node->choice[$delta]['chvotes'] : 0; |
webmaster@1
|
229 |
webmaster@1
|
230 $form['choice_wrapper']['choice'][$delta] = _poll_choice_form($delta, $text, $votes); |
webmaster@1
|
231 } |
webmaster@1
|
232 |
webmaster@1
|
233 // We name our button 'poll_more' to avoid conflicts with other modules using |
webmaster@1
|
234 // AHAH-enabled buttons with the id 'more'. |
webmaster@1
|
235 $form['choice_wrapper']['poll_more'] = array( |
webmaster@1
|
236 '#type' => 'submit', |
webmaster@1
|
237 '#value' => t('More choices'), |
webmaster@1
|
238 '#description' => t("If the amount of boxes above isn't enough, click here to add more choices."), |
webmaster@1
|
239 '#weight' => 1, |
webmaster@1
|
240 '#submit' => array('poll_more_choices_submit'), // If no javascript action. |
webmaster@1
|
241 '#ahah' => array( |
webmaster@1
|
242 'path' => 'poll/js', |
webmaster@1
|
243 'wrapper' => 'poll-choices', |
webmaster@1
|
244 'method' => 'replace', |
webmaster@1
|
245 'effect' => 'fade', |
webmaster@1
|
246 ), |
webmaster@1
|
247 ); |
webmaster@1
|
248 |
webmaster@1
|
249 // Poll attributes |
webmaster@1
|
250 $_duration = array(0 => t('Unlimited')) + drupal_map_assoc(array(86400, 172800, 345600, 604800, 1209600, 2419200, 4838400, 9676800, 31536000), "format_interval"); |
webmaster@1
|
251 $_active = array(0 => t('Closed'), 1 => t('Active')); |
webmaster@1
|
252 |
webmaster@1
|
253 if ($admin) { |
webmaster@1
|
254 $form['settings'] = array( |
webmaster@1
|
255 '#type' => 'fieldset', |
webmaster@1
|
256 '#collapsible' => TRUE, |
webmaster@1
|
257 '#title' => t('Poll settings'), |
webmaster@1
|
258 '#weight' => -3, |
webmaster@1
|
259 ); |
webmaster@1
|
260 |
webmaster@1
|
261 $form['settings']['active'] = array( |
webmaster@1
|
262 '#type' => 'radios', |
webmaster@1
|
263 '#title' => t('Poll status'), |
webmaster@1
|
264 '#default_value' => isset($node->active) ? $node->active : 1, |
webmaster@1
|
265 '#options' => $_active, |
webmaster@1
|
266 '#description' => t('When a poll is closed, visitors can no longer vote for it.') |
webmaster@1
|
267 ); |
webmaster@1
|
268 } |
webmaster@1
|
269 $form['settings']['runtime'] = array( |
webmaster@1
|
270 '#type' => 'select', |
webmaster@1
|
271 '#title' => t('Poll duration'), |
webmaster@1
|
272 '#default_value' => isset($node->runtime) ? $node->runtime : 0, |
webmaster@1
|
273 '#options' => $_duration, |
webmaster@1
|
274 '#description' => t('After this period, the poll will be closed automatically.'), |
webmaster@1
|
275 ); |
webmaster@1
|
276 |
webmaster@1
|
277 return $form; |
webmaster@1
|
278 } |
webmaster@1
|
279 |
webmaster@1
|
280 /** |
webmaster@1
|
281 * Submit handler to add more choices to a poll form. This handler is used when |
webmaster@1
|
282 * javascript is not available. It makes changes to the form state and the |
webmaster@1
|
283 * entire form is rebuilt during the page reload. |
webmaster@1
|
284 */ |
webmaster@1
|
285 function poll_more_choices_submit($form, &$form_state) { |
webmaster@1
|
286 // Set the form to rebuild and run submit handlers. |
webmaster@1
|
287 node_form_submit_build_node($form, $form_state); |
webmaster@1
|
288 |
webmaster@1
|
289 // Make the changes we want to the form state. |
webmaster@1
|
290 if ($form_state['values']['poll_more']) { |
franck@19
|
291 $n = $_GET['q'] == 'poll/js' ? 1 : 5; |
franck@19
|
292 $form_state['choice_count'] = count($form_state['values']['choice']) + $n; |
webmaster@1
|
293 } |
webmaster@1
|
294 } |
webmaster@1
|
295 |
webmaster@1
|
296 function _poll_choice_form($delta, $value = '', $votes = 0) { |
webmaster@1
|
297 $admin = user_access('administer nodes'); |
webmaster@1
|
298 |
webmaster@1
|
299 $form = array( |
webmaster@1
|
300 '#tree' => TRUE, |
webmaster@1
|
301 ); |
webmaster@1
|
302 |
webmaster@1
|
303 // We'll manually set the #parents property of these fields so that |
webmaster@1
|
304 // their values appear in the $form_state['values']['choice'] array. |
webmaster@1
|
305 $form['chtext'] = array( |
webmaster@1
|
306 '#type' => 'textfield', |
webmaster@1
|
307 '#title' => t('Choice @n', array('@n' => ($delta + 1))), |
webmaster@1
|
308 '#default_value' => $value, |
webmaster@1
|
309 '#parents' => array('choice', $delta, 'chtext'), |
webmaster@1
|
310 ); |
webmaster@1
|
311 |
webmaster@1
|
312 if ($admin) { |
webmaster@1
|
313 $form['chvotes'] = array( |
webmaster@1
|
314 '#type' => 'textfield', |
webmaster@1
|
315 '#title' => t('Votes for choice @n', array('@n' => ($delta + 1))), |
webmaster@1
|
316 '#default_value' => $votes, |
webmaster@1
|
317 '#size' => 5, |
webmaster@1
|
318 '#maxlength' => 7, |
webmaster@1
|
319 '#parents' => array('choice', $delta, 'chvotes'), |
webmaster@1
|
320 ); |
webmaster@1
|
321 } |
webmaster@1
|
322 |
webmaster@1
|
323 return $form; |
webmaster@1
|
324 } |
webmaster@1
|
325 |
webmaster@1
|
326 /** |
webmaster@1
|
327 * Menu callback for AHAH additions. |
webmaster@1
|
328 */ |
webmaster@1
|
329 function poll_choice_js() { |
franck@19
|
330 include_once 'modules/node/node.pages.inc'; |
franck@19
|
331 $form_state = array('storage' => NULL, 'submitted' => FALSE); |
webmaster@1
|
332 $form_build_id = $_POST['form_build_id']; |
franck@19
|
333 // Get the form from the cache. |
franck@19
|
334 $form = form_get_cache($form_build_id, $form_state); |
franck@19
|
335 $args = $form['#parameters']; |
franck@19
|
336 $form_id = array_shift($args); |
franck@19
|
337 // We will run some of the submit handlers so we need to disable redirecting. |
franck@19
|
338 $form['#redirect'] = FALSE; |
franck@19
|
339 // We need to process the form, prepare for that by setting a few internals |
franck@19
|
340 // variables. |
franck@19
|
341 $form['#post'] = $_POST; |
franck@19
|
342 $form['#programmed'] = FALSE; |
franck@19
|
343 $form_state['post'] = $_POST; |
franck@19
|
344 // Build, validate and if possible, submit the form. |
franck@19
|
345 drupal_process_form($form_id, $form, $form_state); |
franck@19
|
346 // This call recreates the form relying solely on the form_state that the |
franck@19
|
347 // drupal_process_form set up. |
franck@19
|
348 $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id); |
webmaster@1
|
349 // Render the new output. |
webmaster@1
|
350 $choice_form = $form['choice_wrapper']['choice']; |
webmaster@1
|
351 unset($choice_form['#prefix'], $choice_form['#suffix']); // Prevent duplicate wrappers. |
webmaster@1
|
352 $output = theme('status_messages') . drupal_render($choice_form); |
webmaster@1
|
353 |
webmaster@1
|
354 drupal_json(array('status' => TRUE, 'data' => $output)); |
webmaster@1
|
355 } |
webmaster@1
|
356 |
webmaster@1
|
357 /** |
webmaster@1
|
358 * Implementation of hook_submit(). |
webmaster@1
|
359 */ |
webmaster@1
|
360 function poll_node_form_submit(&$form, &$form_state) { |
webmaster@1
|
361 // Renumber fields |
webmaster@1
|
362 $form_state['values']['choice'] = array_values($form_state['values']['choice']); |
webmaster@1
|
363 $form_state['values']['teaser'] = poll_teaser((object)$form_state['values']); |
webmaster@1
|
364 } |
webmaster@1
|
365 |
webmaster@1
|
366 /** |
webmaster@1
|
367 * Implementation of hook_validate(). |
webmaster@1
|
368 */ |
webmaster@1
|
369 function poll_validate($node) { |
webmaster@1
|
370 if (isset($node->title)) { |
webmaster@1
|
371 // Check for at least two options and validate amount of votes: |
webmaster@1
|
372 $realchoices = 0; |
webmaster@1
|
373 // Renumber fields |
webmaster@1
|
374 $node->choice = array_values($node->choice); |
webmaster@1
|
375 foreach ($node->choice as $i => $choice) { |
webmaster@1
|
376 if ($choice['chtext'] != '') { |
webmaster@1
|
377 $realchoices++; |
webmaster@1
|
378 } |
webmaster@1
|
379 if (isset($choice['chvotes']) && $choice['chvotes'] < 0) { |
webmaster@1
|
380 form_set_error("choice][$i][chvotes", t('Negative values are not allowed.')); |
webmaster@1
|
381 } |
webmaster@1
|
382 } |
webmaster@1
|
383 |
webmaster@1
|
384 if ($realchoices < 2) { |
webmaster@1
|
385 form_set_error("choice][$realchoices][chtext", t('You must fill in at least two choices.')); |
webmaster@1
|
386 } |
webmaster@1
|
387 } |
webmaster@1
|
388 } |
webmaster@1
|
389 |
webmaster@1
|
390 /** |
webmaster@1
|
391 * Implementation of hook_load(). |
webmaster@1
|
392 */ |
webmaster@1
|
393 function poll_load($node) { |
webmaster@1
|
394 global $user; |
webmaster@1
|
395 |
webmaster@1
|
396 $poll = db_fetch_object(db_query("SELECT runtime, active FROM {poll} WHERE nid = %d", $node->nid)); |
webmaster@1
|
397 |
webmaster@1
|
398 // Load the appropriate choices into the $poll object. |
webmaster@1
|
399 $result = db_query("SELECT chtext, chvotes, chorder FROM {poll_choices} WHERE nid = %d ORDER BY chorder", $node->nid); |
webmaster@1
|
400 while ($choice = db_fetch_array($result)) { |
webmaster@1
|
401 $poll->choice[$choice['chorder']] = $choice; |
webmaster@1
|
402 } |
webmaster@1
|
403 |
webmaster@1
|
404 // Determine whether or not this user is allowed to vote. |
webmaster@1
|
405 $poll->allowvotes = FALSE; |
webmaster@1
|
406 if (user_access('vote on polls') && $poll->active) { |
webmaster@1
|
407 if ($user->uid) { |
webmaster@1
|
408 $result = db_fetch_object(db_query('SELECT chorder FROM {poll_votes} WHERE nid = %d AND uid = %d', $node->nid, $user->uid)); |
webmaster@1
|
409 } |
webmaster@1
|
410 else { |
webmaster@1
|
411 $result = db_fetch_object(db_query("SELECT chorder FROM {poll_votes} WHERE nid = %d AND hostname = '%s'", $node->nid, ip_address())); |
webmaster@1
|
412 } |
webmaster@1
|
413 if (isset($result->chorder)) { |
webmaster@1
|
414 $poll->vote = $result->chorder; |
webmaster@1
|
415 } |
webmaster@1
|
416 else { |
webmaster@1
|
417 $poll->vote = -1; |
webmaster@1
|
418 $poll->allowvotes = TRUE; |
webmaster@1
|
419 } |
webmaster@1
|
420 } |
webmaster@1
|
421 return $poll; |
webmaster@1
|
422 } |
webmaster@1
|
423 |
webmaster@1
|
424 /** |
webmaster@1
|
425 * Implementation of hook_insert(). |
webmaster@1
|
426 */ |
webmaster@1
|
427 function poll_insert($node) { |
webmaster@1
|
428 if (!user_access('administer nodes')) { |
webmaster@1
|
429 // Make sure all votes are 0 initially |
webmaster@1
|
430 foreach ($node->choice as $i => $choice) { |
webmaster@1
|
431 $node->choice[$i]['chvotes'] = 0; |
webmaster@1
|
432 } |
webmaster@1
|
433 $node->active = 1; |
webmaster@1
|
434 } |
webmaster@1
|
435 |
webmaster@1
|
436 db_query("INSERT INTO {poll} (nid, runtime, active) VALUES (%d, %d, %d)", $node->nid, $node->runtime, $node->active); |
webmaster@1
|
437 |
webmaster@1
|
438 $i = 0; |
webmaster@1
|
439 foreach ($node->choice as $choice) { |
webmaster@1
|
440 if ($choice['chtext'] != '') { |
webmaster@1
|
441 db_query("INSERT INTO {poll_choices} (nid, chtext, chvotes, chorder) VALUES (%d, '%s', %d, %d)", $node->nid, $choice['chtext'], $choice['chvotes'], $i++); |
webmaster@1
|
442 } |
webmaster@1
|
443 } |
webmaster@1
|
444 } |
webmaster@1
|
445 |
webmaster@1
|
446 /** |
webmaster@1
|
447 * Implementation of hook_update(). |
webmaster@1
|
448 */ |
webmaster@1
|
449 function poll_update($node) { |
webmaster@1
|
450 // Update poll settings. |
webmaster@1
|
451 db_query('UPDATE {poll} SET runtime = %d, active = %d WHERE nid = %d', $node->runtime, $node->active, $node->nid); |
webmaster@1
|
452 |
webmaster@1
|
453 // Clean poll choices. |
webmaster@1
|
454 db_query('DELETE FROM {poll_choices} WHERE nid = %d', $node->nid); |
webmaster@1
|
455 |
webmaster@1
|
456 // Poll choices come in the same order with the same numbers as they are in |
webmaster@1
|
457 // the database, but some might have an empty title, which signifies that |
webmaster@1
|
458 // they should be removed. We remove all votes to the removed options, so |
webmaster@1
|
459 // people who voted on them can vote again. |
webmaster@1
|
460 $new_chorder = 0; |
webmaster@1
|
461 foreach ($node->choice as $old_chorder => $choice) { |
webmaster@1
|
462 $chvotes = isset($choice['chvotes']) ? (int)$choice['chvotes'] : 0; |
webmaster@1
|
463 $chtext = $choice['chtext']; |
webmaster@1
|
464 |
webmaster@1
|
465 if (!empty($chtext)) { |
webmaster@1
|
466 db_query("INSERT INTO {poll_choices} (nid, chtext, chvotes, chorder) VALUES (%d, '%s', %d, %d)", $node->nid, $chtext, $chvotes, $new_chorder); |
webmaster@1
|
467 if ($new_chorder != $old_chorder) { |
webmaster@1
|
468 // We can only remove items in the middle, not add, so |
webmaster@1
|
469 // new_chorder is always <= old_chorder, making this safe. |
webmaster@1
|
470 db_query("UPDATE {poll_votes} SET chorder = %d WHERE nid = %d AND chorder = %d", $new_chorder, $node->nid, $old_chorder); |
webmaster@1
|
471 } |
webmaster@1
|
472 $new_chorder++; |
webmaster@1
|
473 } |
webmaster@1
|
474 else { |
webmaster@1
|
475 db_query("DELETE FROM {poll_votes} WHERE nid = %d AND chorder = %d", $node->nid, $old_chorder); |
webmaster@1
|
476 } |
webmaster@1
|
477 } |
webmaster@1
|
478 } |
webmaster@1
|
479 |
webmaster@1
|
480 /** |
webmaster@1
|
481 * Implementation of hook_delete(). |
webmaster@1
|
482 */ |
webmaster@1
|
483 function poll_delete($node) { |
webmaster@1
|
484 db_query("DELETE FROM {poll} WHERE nid = %d", $node->nid); |
webmaster@1
|
485 db_query("DELETE FROM {poll_choices} WHERE nid = %d", $node->nid); |
webmaster@1
|
486 db_query("DELETE FROM {poll_votes} WHERE nid = %d", $node->nid); |
webmaster@1
|
487 } |
webmaster@1
|
488 |
webmaster@1
|
489 /** |
webmaster@1
|
490 * Implementation of hook_view(). |
webmaster@1
|
491 * |
webmaster@1
|
492 * @param $block |
webmaster@1
|
493 * An extra parameter that adapts the hook to display a block-ready |
webmaster@1
|
494 * rendering of the poll. |
webmaster@1
|
495 */ |
webmaster@1
|
496 function poll_view($node, $teaser = FALSE, $page = FALSE, $block = FALSE) { |
webmaster@1
|
497 global $user; |
webmaster@1
|
498 $output = ''; |
webmaster@1
|
499 |
webmaster@1
|
500 // Special display for side-block |
webmaster@1
|
501 if ($block) { |
webmaster@1
|
502 // No 'read more' link |
webmaster@1
|
503 $node->readmore = FALSE; |
webmaster@1
|
504 |
webmaster@1
|
505 $links = module_invoke_all('link', 'node', $node, 1); |
webmaster@1
|
506 $links[] = array('title' => t('Older polls'), 'href' => 'poll', 'attributes' => array('title' => t('View the list of polls on this site.'))); |
webmaster@1
|
507 if ($node->allowvotes && $block) { |
webmaster@1
|
508 $links[] = array('title' => t('Results'), 'href' => 'node/'. $node->nid .'/results', 'attributes' => array('title' => t('View the current poll results.'))); |
webmaster@1
|
509 } |
webmaster@1
|
510 |
webmaster@1
|
511 $node->links = $links; |
webmaster@1
|
512 } |
webmaster@1
|
513 |
webmaster@1
|
514 if (!empty($node->allowvotes) && ($block || empty($node->show_results))) { |
webmaster@1
|
515 $node->content['body'] = array( |
webmaster@1
|
516 '#value' => drupal_get_form('poll_view_voting', $node, $block), |
webmaster@1
|
517 ); |
webmaster@1
|
518 } |
webmaster@1
|
519 else { |
webmaster@1
|
520 $node->content['body'] = array( |
webmaster@1
|
521 '#value' => poll_view_results($node, $teaser, $page, $block), |
webmaster@1
|
522 ); |
webmaster@1
|
523 } |
webmaster@1
|
524 return $node; |
webmaster@1
|
525 } |
webmaster@1
|
526 |
webmaster@1
|
527 /** |
webmaster@1
|
528 * Creates a simple teaser that lists all the choices. |
webmaster@1
|
529 * |
webmaster@1
|
530 * This is primarily used for RSS. |
webmaster@1
|
531 */ |
webmaster@1
|
532 function poll_teaser($node) { |
webmaster@1
|
533 $teaser = NULL; |
webmaster@1
|
534 if (is_array($node->choice)) { |
webmaster@1
|
535 foreach ($node->choice as $k => $choice) { |
webmaster@1
|
536 if ($choice['chtext'] != '') { |
webmaster@1
|
537 $teaser .= '* '. check_plain($choice['chtext']) ."\n"; |
webmaster@1
|
538 } |
webmaster@1
|
539 } |
webmaster@1
|
540 } |
webmaster@1
|
541 return $teaser; |
webmaster@1
|
542 } |
webmaster@1
|
543 |
webmaster@1
|
544 /** |
webmaster@1
|
545 * Generates the voting form for a poll. |
webmaster@1
|
546 * |
webmaster@1
|
547 * @ingroup forms |
webmaster@1
|
548 * @see poll_vote() |
webmaster@1
|
549 * @see phptemplate_preprocess_poll_vote() |
webmaster@1
|
550 */ |
webmaster@1
|
551 function poll_view_voting(&$form_state, $node, $block) { |
webmaster@1
|
552 if ($node->choice) { |
webmaster@1
|
553 $list = array(); |
webmaster@1
|
554 foreach ($node->choice as $i => $choice) { |
webmaster@1
|
555 $list[$i] = check_plain($choice['chtext']); |
webmaster@1
|
556 } |
webmaster@1
|
557 $form['choice'] = array( |
webmaster@1
|
558 '#type' => 'radios', |
webmaster@1
|
559 '#default_value' => -1, |
webmaster@1
|
560 '#options' => $list, |
webmaster@1
|
561 ); |
webmaster@1
|
562 } |
webmaster@1
|
563 |
webmaster@1
|
564 $form['vote'] = array( |
webmaster@1
|
565 '#type' => 'submit', |
webmaster@1
|
566 '#value' => t('Vote'), |
webmaster@1
|
567 '#submit' => array('poll_vote'), |
webmaster@1
|
568 ); |
webmaster@1
|
569 |
webmaster@1
|
570 // Store the node so we can get to it in submit functions. |
webmaster@1
|
571 $form['#node'] = $node; |
webmaster@1
|
572 $form['#block'] = $block; |
webmaster@1
|
573 |
webmaster@1
|
574 // Set form caching because we could have multiple of these forms on |
webmaster@1
|
575 // the same page, and we want to ensure the right one gets picked. |
webmaster@1
|
576 $form['#cache'] = TRUE; |
webmaster@1
|
577 |
webmaster@1
|
578 // Provide a more cleanly named voting form theme. |
webmaster@1
|
579 $form['#theme'] = 'poll_vote'; |
webmaster@1
|
580 return $form; |
webmaster@1
|
581 } |
webmaster@1
|
582 |
webmaster@1
|
583 /** |
webmaster@1
|
584 * Validation function for processing votes |
webmaster@1
|
585 */ |
webmaster@1
|
586 function poll_view_voting_validate($form, &$form_state) { |
webmaster@1
|
587 if ($form_state['values']['choice'] == -1) { |
webmaster@1
|
588 form_set_error( 'choice', t('Your vote could not be recorded because you did not select any of the choices.')); |
webmaster@1
|
589 } |
webmaster@1
|
590 } |
webmaster@1
|
591 |
webmaster@1
|
592 /** |
webmaster@1
|
593 * Submit handler for processing a vote |
webmaster@1
|
594 */ |
webmaster@1
|
595 function poll_vote($form, &$form_state) { |
webmaster@1
|
596 $node = $form['#node']; |
webmaster@1
|
597 $choice = $form_state['values']['choice']; |
webmaster@1
|
598 |
webmaster@1
|
599 global $user; |
webmaster@1
|
600 if ($user->uid) { |
webmaster@1
|
601 db_query('INSERT INTO {poll_votes} (nid, chorder, uid) VALUES (%d, %d, %d)', $node->nid, $choice, $user->uid); |
webmaster@1
|
602 } |
webmaster@1
|
603 else { |
webmaster@1
|
604 db_query("INSERT INTO {poll_votes} (nid, chorder, hostname) VALUES (%d, %d, '%s')", $node->nid, $choice, ip_address()); |
webmaster@1
|
605 } |
webmaster@1
|
606 |
webmaster@1
|
607 // Add one to the votes. |
webmaster@1
|
608 db_query("UPDATE {poll_choices} SET chvotes = chvotes + 1 WHERE nid = %d AND chorder = %d", $node->nid, $choice); |
webmaster@1
|
609 |
webmaster@1
|
610 cache_clear_all(); |
webmaster@1
|
611 drupal_set_message(t('Your vote was recorded.')); |
webmaster@1
|
612 |
webmaster@1
|
613 // Return the user to whatever page they voted from. |
webmaster@1
|
614 } |
webmaster@1
|
615 |
webmaster@1
|
616 /** |
webmaster@1
|
617 * Themes the voting form for a poll. |
webmaster@1
|
618 * |
webmaster@1
|
619 * Inputs: $form |
webmaster@1
|
620 */ |
webmaster@1
|
621 function template_preprocess_poll_vote(&$variables) { |
webmaster@1
|
622 $form = $variables['form']; |
webmaster@1
|
623 $variables['choice'] = drupal_render($form['choice']); |
webmaster@1
|
624 $variables['title'] = check_plain($form['#node']->title); |
webmaster@1
|
625 $variables['vote'] = drupal_render($form['vote']); |
webmaster@1
|
626 $variables['rest'] = drupal_render($form); |
webmaster@1
|
627 $variables['block'] = $form['#block']; |
webmaster@1
|
628 // If this is a block, allow a different tpl.php to be used. |
webmaster@1
|
629 if ($variables['block']) { |
webmaster@1
|
630 $variables['template_files'][] = 'poll-vote-block'; |
webmaster@1
|
631 } |
webmaster@1
|
632 } |
webmaster@1
|
633 |
webmaster@1
|
634 /** |
webmaster@1
|
635 * Generates a graphical representation of the results of a poll. |
webmaster@1
|
636 */ |
webmaster@1
|
637 function poll_view_results(&$node, $teaser, $page, $block) { |
webmaster@1
|
638 // Count the votes and find the maximum |
webmaster@1
|
639 $total_votes = 0; |
webmaster@1
|
640 $max_votes = 0; |
webmaster@1
|
641 foreach ($node->choice as $choice) { |
webmaster@1
|
642 if (isset($choice['chvotes'])) { |
webmaster@1
|
643 $total_votes += $choice['chvotes']; |
webmaster@1
|
644 $max_votes = max($max_votes, $choice['chvotes']); |
webmaster@1
|
645 } |
webmaster@1
|
646 } |
webmaster@1
|
647 |
webmaster@1
|
648 $poll_results = ''; |
webmaster@1
|
649 foreach ($node->choice as $i => $choice) { |
webmaster@1
|
650 if (!empty($choice['chtext'])) { |
webmaster@1
|
651 $chvotes = isset($choice['chvotes']) ? $choice['chvotes'] : NULL; |
webmaster@1
|
652 $poll_results .= theme('poll_bar', $choice['chtext'], $chvotes, $total_votes, isset($node->vote) && $node->vote == $i, $block); |
webmaster@1
|
653 } |
webmaster@1
|
654 } |
webmaster@1
|
655 |
webmaster@1
|
656 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); |
webmaster@1
|
657 } |
webmaster@1
|
658 |
webmaster@1
|
659 |
webmaster@1
|
660 /** |
webmaster@1
|
661 * Theme the admin poll form for choices. |
webmaster@1
|
662 * |
webmaster@1
|
663 * @ingroup themeable |
webmaster@1
|
664 */ |
webmaster@1
|
665 function theme_poll_choices($form) { |
webmaster@1
|
666 // Change the button title to reflect the behavior when using JavaScript. |
webmaster@1
|
667 drupal_add_js('if (Drupal.jsEnabled) { $(document).ready(function() { $("#edit-poll-more").val("'. t('Add another choice') .'"); }); }', 'inline'); |
webmaster@1
|
668 |
webmaster@1
|
669 $rows = array(); |
webmaster@1
|
670 $headers = array( |
webmaster@1
|
671 t('Choice'), |
webmaster@1
|
672 t('Vote count'), |
webmaster@1
|
673 ); |
webmaster@1
|
674 |
webmaster@1
|
675 foreach (element_children($form) as $key) { |
webmaster@1
|
676 // No need to print the field title every time. |
webmaster@1
|
677 unset($form[$key]['chtext']['#title'], $form[$key]['chvotes']['#title']); |
webmaster@1
|
678 |
webmaster@1
|
679 // Build the table row. |
webmaster@1
|
680 $row = array( |
webmaster@1
|
681 'data' => array( |
webmaster@1
|
682 array('data' => drupal_render($form[$key]['chtext']), 'class' => 'poll-chtext'), |
webmaster@1
|
683 array('data' => drupal_render($form[$key]['chvotes']), 'class' => 'poll-chvotes'), |
webmaster@1
|
684 ), |
webmaster@1
|
685 ); |
webmaster@1
|
686 |
webmaster@1
|
687 // Add additional attributes to the row, such as a class for this row. |
webmaster@1
|
688 if (isset($form[$key]['#attributes'])) { |
webmaster@1
|
689 $row = array_merge($row, $form[$key]['#attributes']); |
webmaster@1
|
690 } |
webmaster@1
|
691 $rows[] = $row; |
webmaster@1
|
692 } |
webmaster@1
|
693 |
webmaster@1
|
694 $output = theme('table', $headers, $rows); |
webmaster@1
|
695 $output .= drupal_render($form); |
webmaster@1
|
696 return $output; |
webmaster@1
|
697 } |
webmaster@1
|
698 |
webmaster@1
|
699 /** |
webmaster@1
|
700 * Preprocess the poll_results theme hook. |
webmaster@1
|
701 * |
webmaster@1
|
702 * Inputs: $raw_title, $results, $votes, $raw_links, $block, $nid, $vote. The |
webmaster@1
|
703 * $raw_* inputs to this are naturally unsafe; often safe versions are |
webmaster@1
|
704 * made to simply overwrite the raw version, but in this case it seems likely |
webmaster@1
|
705 * that the title and the links may be overridden by the theme layer, so they |
webmaster@1
|
706 * are left in with a different name for that purpose. |
webmaster@1
|
707 * |
webmaster@1
|
708 * @see poll-results.tpl.php |
webmaster@1
|
709 * @see poll-results-block.tpl.php |
webmaster@1
|
710 * @see theme_poll_results() |
webmaster@1
|
711 */ |
webmaster@1
|
712 function template_preprocess_poll_results(&$variables) { |
webmaster@1
|
713 $variables['links'] = theme('links', $variables['raw_links']); |
webmaster@1
|
714 if (isset($variables['vote']) && $variables['vote'] > -1 && user_access('cancel own vote')) { |
webmaster@1
|
715 $variables['cancel_form'] = drupal_get_form('poll_cancel_form', $variables['nid']); |
webmaster@1
|
716 } |
webmaster@1
|
717 $variables['title'] = check_plain($variables['raw_title']); |
webmaster@1
|
718 |
webmaster@1
|
719 // If this is a block, allow a different tpl.php to be used. |
webmaster@1
|
720 if ($variables['block']) { |
webmaster@1
|
721 $variables['template_files'][] = 'poll-results-block'; |
webmaster@1
|
722 } |
webmaster@1
|
723 } |
webmaster@1
|
724 |
webmaster@1
|
725 /** |
webmaster@1
|
726 * Preprocess the poll_bar theme hook. |
webmaster@1
|
727 * |
webmaster@1
|
728 * Inputs: $title, $votes, $total_votes, $voted, $block |
webmaster@1
|
729 * |
webmaster@1
|
730 * @see poll-bar.tpl.php |
webmaster@1
|
731 * @see poll-bar-block.tpl.php |
webmaster@1
|
732 * @see theme_poll_bar() |
webmaster@1
|
733 */ |
webmaster@1
|
734 function template_preprocess_poll_bar(&$variables) { |
webmaster@1
|
735 if ($variables['block']) { |
webmaster@1
|
736 $variables['template_files'][] = 'poll-bar-block'; |
webmaster@1
|
737 } |
webmaster@1
|
738 $variables['title'] = check_plain($variables['title']); |
webmaster@1
|
739 $variables['percentage'] = round($variables['votes'] * 100 / max($variables['total_votes'], 1)); |
webmaster@1
|
740 } |
webmaster@1
|
741 |
webmaster@1
|
742 /** |
webmaster@1
|
743 * Builds the cancel form for a poll. |
webmaster@1
|
744 * |
webmaster@1
|
745 * @ingroup forms |
webmaster@1
|
746 * @see poll_cancel() |
webmaster@1
|
747 */ |
webmaster@1
|
748 function poll_cancel_form(&$form_state, $nid) { |
webmaster@1
|
749 // Store the nid so we can get to it in submit functions. |
webmaster@1
|
750 $form['#nid'] = $nid; |
webmaster@1
|
751 |
webmaster@1
|
752 $form['submit'] = array( |
webmaster@1
|
753 '#type' => 'submit', |
webmaster@1
|
754 '#value' => t('Cancel your vote'), |
webmaster@1
|
755 '#submit' => array('poll_cancel') |
webmaster@1
|
756 ); |
webmaster@1
|
757 |
webmaster@1
|
758 $form['#cache'] = TRUE; |
webmaster@1
|
759 |
webmaster@1
|
760 return $form; |
webmaster@1
|
761 } |
webmaster@1
|
762 |
webmaster@1
|
763 /** |
webmaster@1
|
764 * Submit callback for poll_cancel_form |
webmaster@1
|
765 */ |
webmaster@1
|
766 function poll_cancel($form, &$form_state) { |
webmaster@1
|
767 $node = node_load($form['#nid']); |
webmaster@1
|
768 global $user; |
webmaster@1
|
769 |
webmaster@1
|
770 if ($user->uid) { |
webmaster@1
|
771 db_query('DELETE FROM {poll_votes} WHERE nid = %d and uid = %d', $node->nid, $user->uid); |
webmaster@1
|
772 } |
webmaster@1
|
773 else { |
webmaster@1
|
774 db_query("DELETE FROM {poll_votes} WHERE nid = %d and hostname = '%s'", $node->nid, ip_address()); |
webmaster@1
|
775 } |
webmaster@1
|
776 |
webmaster@1
|
777 // Subtract from the votes. |
webmaster@1
|
778 db_query("UPDATE {poll_choices} SET chvotes = chvotes - 1 WHERE nid = %d AND chorder = %d", $node->nid, $node->vote); |
webmaster@1
|
779 } |
webmaster@1
|
780 |
webmaster@1
|
781 /** |
webmaster@1
|
782 * Implementation of hook_user(). |
webmaster@1
|
783 */ |
webmaster@1
|
784 function poll_user($op, &$edit, &$user) { |
webmaster@1
|
785 if ($op == 'delete') { |
webmaster@1
|
786 db_query('UPDATE {poll_votes} SET uid = 0 WHERE uid = %d', $user->uid); |
webmaster@1
|
787 } |
webmaster@1
|
788 } |