webmaster@1
|
1 <?php |
webmaster@7
|
2 // $Id: poll.module,v 1.263.2.1 2008/04/25 20:39:55 goba 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']) { |
webmaster@1
|
291 $form_state['choice_count'] = count($form_state['values']['choice']) + 5; |
webmaster@1
|
292 } |
webmaster@1
|
293 } |
webmaster@1
|
294 |
webmaster@1
|
295 function _poll_choice_form($delta, $value = '', $votes = 0) { |
webmaster@1
|
296 $admin = user_access('administer nodes'); |
webmaster@1
|
297 |
webmaster@1
|
298 $form = array( |
webmaster@1
|
299 '#tree' => TRUE, |
webmaster@1
|
300 ); |
webmaster@1
|
301 |
webmaster@1
|
302 // We'll manually set the #parents property of these fields so that |
webmaster@1
|
303 // their values appear in the $form_state['values']['choice'] array. |
webmaster@1
|
304 $form['chtext'] = array( |
webmaster@1
|
305 '#type' => 'textfield', |
webmaster@1
|
306 '#title' => t('Choice @n', array('@n' => ($delta + 1))), |
webmaster@1
|
307 '#default_value' => $value, |
webmaster@1
|
308 '#parents' => array('choice', $delta, 'chtext'), |
webmaster@1
|
309 ); |
webmaster@1
|
310 |
webmaster@1
|
311 if ($admin) { |
webmaster@1
|
312 $form['chvotes'] = array( |
webmaster@1
|
313 '#type' => 'textfield', |
webmaster@1
|
314 '#title' => t('Votes for choice @n', array('@n' => ($delta + 1))), |
webmaster@1
|
315 '#default_value' => $votes, |
webmaster@1
|
316 '#size' => 5, |
webmaster@1
|
317 '#maxlength' => 7, |
webmaster@1
|
318 '#parents' => array('choice', $delta, 'chvotes'), |
webmaster@1
|
319 ); |
webmaster@1
|
320 } |
webmaster@1
|
321 |
webmaster@1
|
322 return $form; |
webmaster@1
|
323 } |
webmaster@1
|
324 |
webmaster@1
|
325 /** |
webmaster@1
|
326 * Menu callback for AHAH additions. |
webmaster@1
|
327 */ |
webmaster@1
|
328 function poll_choice_js() { |
webmaster@1
|
329 $delta = count($_POST['choice']); |
webmaster@1
|
330 |
webmaster@1
|
331 // Build our new form element. |
webmaster@1
|
332 $form_element = _poll_choice_form($delta); |
webmaster@1
|
333 drupal_alter('form', $form_element, array(), 'poll_choice_js'); |
webmaster@1
|
334 |
webmaster@1
|
335 // Build the new form. |
webmaster@1
|
336 $form_state = array('submitted' => FALSE); |
webmaster@1
|
337 $form_build_id = $_POST['form_build_id']; |
webmaster@1
|
338 // Add the new element to the stored form. Without adding the element to the |
webmaster@1
|
339 // form, Drupal is not aware of this new elements existence and will not |
webmaster@1
|
340 // process it. We retreive the cached form, add the element, and resave. |
webmaster@1
|
341 $form = form_get_cache($form_build_id, $form_state); |
webmaster@1
|
342 $form['choice_wrapper']['choice'][$delta] = $form_element; |
webmaster@1
|
343 form_set_cache($form_build_id, $form, $form_state); |
webmaster@1
|
344 $form += array( |
webmaster@1
|
345 '#post' => $_POST, |
webmaster@1
|
346 '#programmed' => FALSE, |
webmaster@1
|
347 ); |
webmaster@1
|
348 |
webmaster@1
|
349 // Rebuild the form. |
webmaster@1
|
350 $form = form_builder('poll_node_form', $form, $form_state); |
webmaster@1
|
351 |
webmaster@1
|
352 // Render the new output. |
webmaster@1
|
353 $choice_form = $form['choice_wrapper']['choice']; |
webmaster@1
|
354 unset($choice_form['#prefix'], $choice_form['#suffix']); // Prevent duplicate wrappers. |
webmaster@1
|
355 $choice_form[$delta]['#attributes']['class'] = empty($choice_form[$delta]['#attributes']['class']) ? 'ahah-new-content' : $choice_form[$delta]['#attributes']['class'] .' ahah-new-content'; |
webmaster@1
|
356 $choice_form[$delta]['chvotes']['#value'] = 0; |
webmaster@1
|
357 $output = theme('status_messages') . drupal_render($choice_form); |
webmaster@1
|
358 |
webmaster@1
|
359 drupal_json(array('status' => TRUE, 'data' => $output)); |
webmaster@1
|
360 } |
webmaster@1
|
361 |
webmaster@1
|
362 /** |
webmaster@1
|
363 * Implementation of hook_submit(). |
webmaster@1
|
364 */ |
webmaster@1
|
365 function poll_node_form_submit(&$form, &$form_state) { |
webmaster@1
|
366 // Renumber fields |
webmaster@1
|
367 $form_state['values']['choice'] = array_values($form_state['values']['choice']); |
webmaster@1
|
368 $form_state['values']['teaser'] = poll_teaser((object)$form_state['values']); |
webmaster@1
|
369 } |
webmaster@1
|
370 |
webmaster@1
|
371 /** |
webmaster@1
|
372 * Implementation of hook_validate(). |
webmaster@1
|
373 */ |
webmaster@1
|
374 function poll_validate($node) { |
webmaster@1
|
375 if (isset($node->title)) { |
webmaster@1
|
376 // Check for at least two options and validate amount of votes: |
webmaster@1
|
377 $realchoices = 0; |
webmaster@1
|
378 // Renumber fields |
webmaster@1
|
379 $node->choice = array_values($node->choice); |
webmaster@1
|
380 foreach ($node->choice as $i => $choice) { |
webmaster@1
|
381 if ($choice['chtext'] != '') { |
webmaster@1
|
382 $realchoices++; |
webmaster@1
|
383 } |
webmaster@1
|
384 if (isset($choice['chvotes']) && $choice['chvotes'] < 0) { |
webmaster@1
|
385 form_set_error("choice][$i][chvotes", t('Negative values are not allowed.')); |
webmaster@1
|
386 } |
webmaster@1
|
387 } |
webmaster@1
|
388 |
webmaster@1
|
389 if ($realchoices < 2) { |
webmaster@1
|
390 form_set_error("choice][$realchoices][chtext", t('You must fill in at least two choices.')); |
webmaster@1
|
391 } |
webmaster@1
|
392 } |
webmaster@1
|
393 } |
webmaster@1
|
394 |
webmaster@1
|
395 /** |
webmaster@1
|
396 * Implementation of hook_load(). |
webmaster@1
|
397 */ |
webmaster@1
|
398 function poll_load($node) { |
webmaster@1
|
399 global $user; |
webmaster@1
|
400 |
webmaster@1
|
401 $poll = db_fetch_object(db_query("SELECT runtime, active FROM {poll} WHERE nid = %d", $node->nid)); |
webmaster@1
|
402 |
webmaster@1
|
403 // Load the appropriate choices into the $poll object. |
webmaster@1
|
404 $result = db_query("SELECT chtext, chvotes, chorder FROM {poll_choices} WHERE nid = %d ORDER BY chorder", $node->nid); |
webmaster@1
|
405 while ($choice = db_fetch_array($result)) { |
webmaster@1
|
406 $poll->choice[$choice['chorder']] = $choice; |
webmaster@1
|
407 } |
webmaster@1
|
408 |
webmaster@1
|
409 // Determine whether or not this user is allowed to vote. |
webmaster@1
|
410 $poll->allowvotes = FALSE; |
webmaster@1
|
411 if (user_access('vote on polls') && $poll->active) { |
webmaster@1
|
412 if ($user->uid) { |
webmaster@1
|
413 $result = db_fetch_object(db_query('SELECT chorder FROM {poll_votes} WHERE nid = %d AND uid = %d', $node->nid, $user->uid)); |
webmaster@1
|
414 } |
webmaster@1
|
415 else { |
webmaster@1
|
416 $result = db_fetch_object(db_query("SELECT chorder FROM {poll_votes} WHERE nid = %d AND hostname = '%s'", $node->nid, ip_address())); |
webmaster@1
|
417 } |
webmaster@1
|
418 if (isset($result->chorder)) { |
webmaster@1
|
419 $poll->vote = $result->chorder; |
webmaster@1
|
420 } |
webmaster@1
|
421 else { |
webmaster@1
|
422 $poll->vote = -1; |
webmaster@1
|
423 $poll->allowvotes = TRUE; |
webmaster@1
|
424 } |
webmaster@1
|
425 } |
webmaster@1
|
426 return $poll; |
webmaster@1
|
427 } |
webmaster@1
|
428 |
webmaster@1
|
429 /** |
webmaster@1
|
430 * Implementation of hook_insert(). |
webmaster@1
|
431 */ |
webmaster@1
|
432 function poll_insert($node) { |
webmaster@1
|
433 if (!user_access('administer nodes')) { |
webmaster@1
|
434 // Make sure all votes are 0 initially |
webmaster@1
|
435 foreach ($node->choice as $i => $choice) { |
webmaster@1
|
436 $node->choice[$i]['chvotes'] = 0; |
webmaster@1
|
437 } |
webmaster@1
|
438 $node->active = 1; |
webmaster@1
|
439 } |
webmaster@1
|
440 |
webmaster@1
|
441 db_query("INSERT INTO {poll} (nid, runtime, active) VALUES (%d, %d, %d)", $node->nid, $node->runtime, $node->active); |
webmaster@1
|
442 |
webmaster@1
|
443 $i = 0; |
webmaster@1
|
444 foreach ($node->choice as $choice) { |
webmaster@1
|
445 if ($choice['chtext'] != '') { |
webmaster@1
|
446 db_query("INSERT INTO {poll_choices} (nid, chtext, chvotes, chorder) VALUES (%d, '%s', %d, %d)", $node->nid, $choice['chtext'], $choice['chvotes'], $i++); |
webmaster@1
|
447 } |
webmaster@1
|
448 } |
webmaster@1
|
449 } |
webmaster@1
|
450 |
webmaster@1
|
451 /** |
webmaster@1
|
452 * Implementation of hook_update(). |
webmaster@1
|
453 */ |
webmaster@1
|
454 function poll_update($node) { |
webmaster@1
|
455 // Update poll settings. |
webmaster@1
|
456 db_query('UPDATE {poll} SET runtime = %d, active = %d WHERE nid = %d', $node->runtime, $node->active, $node->nid); |
webmaster@1
|
457 |
webmaster@1
|
458 // Clean poll choices. |
webmaster@1
|
459 db_query('DELETE FROM {poll_choices} WHERE nid = %d', $node->nid); |
webmaster@1
|
460 |
webmaster@1
|
461 // Poll choices come in the same order with the same numbers as they are in |
webmaster@1
|
462 // the database, but some might have an empty title, which signifies that |
webmaster@1
|
463 // they should be removed. We remove all votes to the removed options, so |
webmaster@1
|
464 // people who voted on them can vote again. |
webmaster@1
|
465 $new_chorder = 0; |
webmaster@1
|
466 foreach ($node->choice as $old_chorder => $choice) { |
webmaster@1
|
467 $chvotes = isset($choice['chvotes']) ? (int)$choice['chvotes'] : 0; |
webmaster@1
|
468 $chtext = $choice['chtext']; |
webmaster@1
|
469 |
webmaster@1
|
470 if (!empty($chtext)) { |
webmaster@1
|
471 db_query("INSERT INTO {poll_choices} (nid, chtext, chvotes, chorder) VALUES (%d, '%s', %d, %d)", $node->nid, $chtext, $chvotes, $new_chorder); |
webmaster@1
|
472 if ($new_chorder != $old_chorder) { |
webmaster@1
|
473 // We can only remove items in the middle, not add, so |
webmaster@1
|
474 // new_chorder is always <= old_chorder, making this safe. |
webmaster@1
|
475 db_query("UPDATE {poll_votes} SET chorder = %d WHERE nid = %d AND chorder = %d", $new_chorder, $node->nid, $old_chorder); |
webmaster@1
|
476 } |
webmaster@1
|
477 $new_chorder++; |
webmaster@1
|
478 } |
webmaster@1
|
479 else { |
webmaster@1
|
480 db_query("DELETE FROM {poll_votes} WHERE nid = %d AND chorder = %d", $node->nid, $old_chorder); |
webmaster@1
|
481 } |
webmaster@1
|
482 } |
webmaster@1
|
483 } |
webmaster@1
|
484 |
webmaster@1
|
485 /** |
webmaster@1
|
486 * Implementation of hook_delete(). |
webmaster@1
|
487 */ |
webmaster@1
|
488 function poll_delete($node) { |
webmaster@1
|
489 db_query("DELETE FROM {poll} WHERE nid = %d", $node->nid); |
webmaster@1
|
490 db_query("DELETE FROM {poll_choices} WHERE nid = %d", $node->nid); |
webmaster@1
|
491 db_query("DELETE FROM {poll_votes} WHERE nid = %d", $node->nid); |
webmaster@1
|
492 } |
webmaster@1
|
493 |
webmaster@1
|
494 /** |
webmaster@1
|
495 * Implementation of hook_view(). |
webmaster@1
|
496 * |
webmaster@1
|
497 * @param $block |
webmaster@1
|
498 * An extra parameter that adapts the hook to display a block-ready |
webmaster@1
|
499 * rendering of the poll. |
webmaster@1
|
500 */ |
webmaster@1
|
501 function poll_view($node, $teaser = FALSE, $page = FALSE, $block = FALSE) { |
webmaster@1
|
502 global $user; |
webmaster@1
|
503 $output = ''; |
webmaster@1
|
504 |
webmaster@1
|
505 // Special display for side-block |
webmaster@1
|
506 if ($block) { |
webmaster@1
|
507 // No 'read more' link |
webmaster@1
|
508 $node->readmore = FALSE; |
webmaster@1
|
509 |
webmaster@1
|
510 $links = module_invoke_all('link', 'node', $node, 1); |
webmaster@1
|
511 $links[] = array('title' => t('Older polls'), 'href' => 'poll', 'attributes' => array('title' => t('View the list of polls on this site.'))); |
webmaster@1
|
512 if ($node->allowvotes && $block) { |
webmaster@1
|
513 $links[] = array('title' => t('Results'), 'href' => 'node/'. $node->nid .'/results', 'attributes' => array('title' => t('View the current poll results.'))); |
webmaster@1
|
514 } |
webmaster@1
|
515 |
webmaster@1
|
516 $node->links = $links; |
webmaster@1
|
517 } |
webmaster@1
|
518 |
webmaster@1
|
519 if (!empty($node->allowvotes) && ($block || empty($node->show_results))) { |
webmaster@1
|
520 $node->content['body'] = array( |
webmaster@1
|
521 '#value' => drupal_get_form('poll_view_voting', $node, $block), |
webmaster@1
|
522 ); |
webmaster@1
|
523 } |
webmaster@1
|
524 else { |
webmaster@1
|
525 $node->content['body'] = array( |
webmaster@1
|
526 '#value' => poll_view_results($node, $teaser, $page, $block), |
webmaster@1
|
527 ); |
webmaster@1
|
528 } |
webmaster@1
|
529 return $node; |
webmaster@1
|
530 } |
webmaster@1
|
531 |
webmaster@1
|
532 /** |
webmaster@1
|
533 * Creates a simple teaser that lists all the choices. |
webmaster@1
|
534 * |
webmaster@1
|
535 * This is primarily used for RSS. |
webmaster@1
|
536 */ |
webmaster@1
|
537 function poll_teaser($node) { |
webmaster@1
|
538 $teaser = NULL; |
webmaster@1
|
539 if (is_array($node->choice)) { |
webmaster@1
|
540 foreach ($node->choice as $k => $choice) { |
webmaster@1
|
541 if ($choice['chtext'] != '') { |
webmaster@1
|
542 $teaser .= '* '. check_plain($choice['chtext']) ."\n"; |
webmaster@1
|
543 } |
webmaster@1
|
544 } |
webmaster@1
|
545 } |
webmaster@1
|
546 return $teaser; |
webmaster@1
|
547 } |
webmaster@1
|
548 |
webmaster@1
|
549 /** |
webmaster@1
|
550 * Generates the voting form for a poll. |
webmaster@1
|
551 * |
webmaster@1
|
552 * @ingroup forms |
webmaster@1
|
553 * @see poll_vote() |
webmaster@1
|
554 * @see phptemplate_preprocess_poll_vote() |
webmaster@1
|
555 */ |
webmaster@1
|
556 function poll_view_voting(&$form_state, $node, $block) { |
webmaster@1
|
557 if ($node->choice) { |
webmaster@1
|
558 $list = array(); |
webmaster@1
|
559 foreach ($node->choice as $i => $choice) { |
webmaster@1
|
560 $list[$i] = check_plain($choice['chtext']); |
webmaster@1
|
561 } |
webmaster@1
|
562 $form['choice'] = array( |
webmaster@1
|
563 '#type' => 'radios', |
webmaster@1
|
564 '#default_value' => -1, |
webmaster@1
|
565 '#options' => $list, |
webmaster@1
|
566 ); |
webmaster@1
|
567 } |
webmaster@1
|
568 |
webmaster@1
|
569 $form['vote'] = array( |
webmaster@1
|
570 '#type' => 'submit', |
webmaster@1
|
571 '#value' => t('Vote'), |
webmaster@1
|
572 '#submit' => array('poll_vote'), |
webmaster@1
|
573 ); |
webmaster@1
|
574 |
webmaster@1
|
575 // Store the node so we can get to it in submit functions. |
webmaster@1
|
576 $form['#node'] = $node; |
webmaster@1
|
577 $form['#block'] = $block; |
webmaster@1
|
578 |
webmaster@1
|
579 // Set form caching because we could have multiple of these forms on |
webmaster@1
|
580 // the same page, and we want to ensure the right one gets picked. |
webmaster@1
|
581 $form['#cache'] = TRUE; |
webmaster@1
|
582 |
webmaster@1
|
583 // Provide a more cleanly named voting form theme. |
webmaster@1
|
584 $form['#theme'] = 'poll_vote'; |
webmaster@1
|
585 return $form; |
webmaster@1
|
586 } |
webmaster@1
|
587 |
webmaster@1
|
588 /** |
webmaster@1
|
589 * Validation function for processing votes |
webmaster@1
|
590 */ |
webmaster@1
|
591 function poll_view_voting_validate($form, &$form_state) { |
webmaster@1
|
592 if ($form_state['values']['choice'] == -1) { |
webmaster@1
|
593 form_set_error( 'choice', t('Your vote could not be recorded because you did not select any of the choices.')); |
webmaster@1
|
594 } |
webmaster@1
|
595 } |
webmaster@1
|
596 |
webmaster@1
|
597 /** |
webmaster@1
|
598 * Submit handler for processing a vote |
webmaster@1
|
599 */ |
webmaster@1
|
600 function poll_vote($form, &$form_state) { |
webmaster@1
|
601 $node = $form['#node']; |
webmaster@1
|
602 $choice = $form_state['values']['choice']; |
webmaster@1
|
603 |
webmaster@1
|
604 global $user; |
webmaster@1
|
605 if ($user->uid) { |
webmaster@1
|
606 db_query('INSERT INTO {poll_votes} (nid, chorder, uid) VALUES (%d, %d, %d)', $node->nid, $choice, $user->uid); |
webmaster@1
|
607 } |
webmaster@1
|
608 else { |
webmaster@1
|
609 db_query("INSERT INTO {poll_votes} (nid, chorder, hostname) VALUES (%d, %d, '%s')", $node->nid, $choice, ip_address()); |
webmaster@1
|
610 } |
webmaster@1
|
611 |
webmaster@1
|
612 // Add one to the votes. |
webmaster@1
|
613 db_query("UPDATE {poll_choices} SET chvotes = chvotes + 1 WHERE nid = %d AND chorder = %d", $node->nid, $choice); |
webmaster@1
|
614 |
webmaster@1
|
615 cache_clear_all(); |
webmaster@1
|
616 drupal_set_message(t('Your vote was recorded.')); |
webmaster@1
|
617 |
webmaster@1
|
618 // Return the user to whatever page they voted from. |
webmaster@1
|
619 } |
webmaster@1
|
620 |
webmaster@1
|
621 /** |
webmaster@1
|
622 * Themes the voting form for a poll. |
webmaster@1
|
623 * |
webmaster@1
|
624 * Inputs: $form |
webmaster@1
|
625 */ |
webmaster@1
|
626 function template_preprocess_poll_vote(&$variables) { |
webmaster@1
|
627 $form = $variables['form']; |
webmaster@1
|
628 $variables['choice'] = drupal_render($form['choice']); |
webmaster@1
|
629 $variables['title'] = check_plain($form['#node']->title); |
webmaster@1
|
630 $variables['vote'] = drupal_render($form['vote']); |
webmaster@1
|
631 $variables['rest'] = drupal_render($form); |
webmaster@1
|
632 $variables['block'] = $form['#block']; |
webmaster@1
|
633 // If this is a block, allow a different tpl.php to be used. |
webmaster@1
|
634 if ($variables['block']) { |
webmaster@1
|
635 $variables['template_files'][] = 'poll-vote-block'; |
webmaster@1
|
636 } |
webmaster@1
|
637 } |
webmaster@1
|
638 |
webmaster@1
|
639 /** |
webmaster@1
|
640 * Generates a graphical representation of the results of a poll. |
webmaster@1
|
641 */ |
webmaster@1
|
642 function poll_view_results(&$node, $teaser, $page, $block) { |
webmaster@1
|
643 // Count the votes and find the maximum |
webmaster@1
|
644 $total_votes = 0; |
webmaster@1
|
645 $max_votes = 0; |
webmaster@1
|
646 foreach ($node->choice as $choice) { |
webmaster@1
|
647 if (isset($choice['chvotes'])) { |
webmaster@1
|
648 $total_votes += $choice['chvotes']; |
webmaster@1
|
649 $max_votes = max($max_votes, $choice['chvotes']); |
webmaster@1
|
650 } |
webmaster@1
|
651 } |
webmaster@1
|
652 |
webmaster@1
|
653 $poll_results = ''; |
webmaster@1
|
654 foreach ($node->choice as $i => $choice) { |
webmaster@1
|
655 if (!empty($choice['chtext'])) { |
webmaster@1
|
656 $chvotes = isset($choice['chvotes']) ? $choice['chvotes'] : NULL; |
webmaster@1
|
657 $poll_results .= theme('poll_bar', $choice['chtext'], $chvotes, $total_votes, isset($node->vote) && $node->vote == $i, $block); |
webmaster@1
|
658 } |
webmaster@1
|
659 } |
webmaster@1
|
660 |
webmaster@1
|
661 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
|
662 } |
webmaster@1
|
663 |
webmaster@1
|
664 |
webmaster@1
|
665 /** |
webmaster@1
|
666 * Theme the admin poll form for choices. |
webmaster@1
|
667 * |
webmaster@1
|
668 * @ingroup themeable |
webmaster@1
|
669 */ |
webmaster@1
|
670 function theme_poll_choices($form) { |
webmaster@1
|
671 // Change the button title to reflect the behavior when using JavaScript. |
webmaster@1
|
672 drupal_add_js('if (Drupal.jsEnabled) { $(document).ready(function() { $("#edit-poll-more").val("'. t('Add another choice') .'"); }); }', 'inline'); |
webmaster@1
|
673 |
webmaster@1
|
674 $rows = array(); |
webmaster@1
|
675 $headers = array( |
webmaster@1
|
676 t('Choice'), |
webmaster@1
|
677 t('Vote count'), |
webmaster@1
|
678 ); |
webmaster@1
|
679 |
webmaster@1
|
680 foreach (element_children($form) as $key) { |
webmaster@1
|
681 // No need to print the field title every time. |
webmaster@1
|
682 unset($form[$key]['chtext']['#title'], $form[$key]['chvotes']['#title']); |
webmaster@1
|
683 |
webmaster@1
|
684 // Build the table row. |
webmaster@1
|
685 $row = array( |
webmaster@1
|
686 'data' => array( |
webmaster@1
|
687 array('data' => drupal_render($form[$key]['chtext']), 'class' => 'poll-chtext'), |
webmaster@1
|
688 array('data' => drupal_render($form[$key]['chvotes']), 'class' => 'poll-chvotes'), |
webmaster@1
|
689 ), |
webmaster@1
|
690 ); |
webmaster@1
|
691 |
webmaster@1
|
692 // Add additional attributes to the row, such as a class for this row. |
webmaster@1
|
693 if (isset($form[$key]['#attributes'])) { |
webmaster@1
|
694 $row = array_merge($row, $form[$key]['#attributes']); |
webmaster@1
|
695 } |
webmaster@1
|
696 $rows[] = $row; |
webmaster@1
|
697 } |
webmaster@1
|
698 |
webmaster@1
|
699 $output = theme('table', $headers, $rows); |
webmaster@1
|
700 $output .= drupal_render($form); |
webmaster@1
|
701 return $output; |
webmaster@1
|
702 } |
webmaster@1
|
703 |
webmaster@1
|
704 /** |
webmaster@1
|
705 * Preprocess the poll_results theme hook. |
webmaster@1
|
706 * |
webmaster@1
|
707 * Inputs: $raw_title, $results, $votes, $raw_links, $block, $nid, $vote. The |
webmaster@1
|
708 * $raw_* inputs to this are naturally unsafe; often safe versions are |
webmaster@1
|
709 * made to simply overwrite the raw version, but in this case it seems likely |
webmaster@1
|
710 * that the title and the links may be overridden by the theme layer, so they |
webmaster@1
|
711 * are left in with a different name for that purpose. |
webmaster@1
|
712 * |
webmaster@1
|
713 * @see poll-results.tpl.php |
webmaster@1
|
714 * @see poll-results-block.tpl.php |
webmaster@1
|
715 * @see theme_poll_results() |
webmaster@1
|
716 */ |
webmaster@1
|
717 function template_preprocess_poll_results(&$variables) { |
webmaster@1
|
718 $variables['links'] = theme('links', $variables['raw_links']); |
webmaster@1
|
719 if (isset($variables['vote']) && $variables['vote'] > -1 && user_access('cancel own vote')) { |
webmaster@1
|
720 $variables['cancel_form'] = drupal_get_form('poll_cancel_form', $variables['nid']); |
webmaster@1
|
721 } |
webmaster@1
|
722 $variables['title'] = check_plain($variables['raw_title']); |
webmaster@1
|
723 |
webmaster@1
|
724 // If this is a block, allow a different tpl.php to be used. |
webmaster@1
|
725 if ($variables['block']) { |
webmaster@1
|
726 $variables['template_files'][] = 'poll-results-block'; |
webmaster@1
|
727 } |
webmaster@1
|
728 } |
webmaster@1
|
729 |
webmaster@1
|
730 /** |
webmaster@1
|
731 * Preprocess the poll_bar theme hook. |
webmaster@1
|
732 * |
webmaster@1
|
733 * Inputs: $title, $votes, $total_votes, $voted, $block |
webmaster@1
|
734 * |
webmaster@1
|
735 * @see poll-bar.tpl.php |
webmaster@1
|
736 * @see poll-bar-block.tpl.php |
webmaster@1
|
737 * @see theme_poll_bar() |
webmaster@1
|
738 */ |
webmaster@1
|
739 function template_preprocess_poll_bar(&$variables) { |
webmaster@1
|
740 if ($variables['block']) { |
webmaster@1
|
741 $variables['template_files'][] = 'poll-bar-block'; |
webmaster@1
|
742 } |
webmaster@1
|
743 $variables['title'] = check_plain($variables['title']); |
webmaster@1
|
744 $variables['percentage'] = round($variables['votes'] * 100 / max($variables['total_votes'], 1)); |
webmaster@1
|
745 } |
webmaster@1
|
746 |
webmaster@1
|
747 /** |
webmaster@1
|
748 * Builds the cancel form for a poll. |
webmaster@1
|
749 * |
webmaster@1
|
750 * @ingroup forms |
webmaster@1
|
751 * @see poll_cancel() |
webmaster@1
|
752 */ |
webmaster@1
|
753 function poll_cancel_form(&$form_state, $nid) { |
webmaster@1
|
754 // Store the nid so we can get to it in submit functions. |
webmaster@1
|
755 $form['#nid'] = $nid; |
webmaster@1
|
756 |
webmaster@1
|
757 $form['submit'] = array( |
webmaster@1
|
758 '#type' => 'submit', |
webmaster@1
|
759 '#value' => t('Cancel your vote'), |
webmaster@1
|
760 '#submit' => array('poll_cancel') |
webmaster@1
|
761 ); |
webmaster@1
|
762 |
webmaster@1
|
763 $form['#cache'] = TRUE; |
webmaster@1
|
764 |
webmaster@1
|
765 return $form; |
webmaster@1
|
766 } |
webmaster@1
|
767 |
webmaster@1
|
768 /** |
webmaster@1
|
769 * Submit callback for poll_cancel_form |
webmaster@1
|
770 */ |
webmaster@1
|
771 function poll_cancel($form, &$form_state) { |
webmaster@1
|
772 $node = node_load($form['#nid']); |
webmaster@1
|
773 global $user; |
webmaster@1
|
774 |
webmaster@1
|
775 if ($user->uid) { |
webmaster@1
|
776 db_query('DELETE FROM {poll_votes} WHERE nid = %d and uid = %d', $node->nid, $user->uid); |
webmaster@1
|
777 } |
webmaster@1
|
778 else { |
webmaster@1
|
779 db_query("DELETE FROM {poll_votes} WHERE nid = %d and hostname = '%s'", $node->nid, ip_address()); |
webmaster@1
|
780 } |
webmaster@1
|
781 |
webmaster@1
|
782 // Subtract from the votes. |
webmaster@1
|
783 db_query("UPDATE {poll_choices} SET chvotes = chvotes - 1 WHERE nid = %d AND chorder = %d", $node->nid, $node->vote); |
webmaster@1
|
784 } |
webmaster@1
|
785 |
webmaster@1
|
786 /** |
webmaster@1
|
787 * Implementation of hook_user(). |
webmaster@1
|
788 */ |
webmaster@1
|
789 function poll_user($op, &$edit, &$user) { |
webmaster@1
|
790 if ($op == 'delete') { |
webmaster@1
|
791 db_query('UPDATE {poll_votes} SET uid = 0 WHERE uid = %d', $user->uid); |
webmaster@1
|
792 } |
webmaster@1
|
793 } |