/[drupal]/contributions/modules/dodge/dodge.module
ViewVC logotype

Contents of /contributions/modules/dodge/dodge.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.20 - (show annotations) (download) (as text)
Fri Apr 18 13:42:49 2008 UTC (19 months, 1 week ago) by davyvandenbremt
Branch: MAIN
CVS Tags: HEAD
Changes since 1.19: +79 -77 lines
File MIME type: text/x-php
- small bug fixes
- changed theme function name for blocks
- added page with all dodges of a certain user
- added links to blocks for 'users' to all all user's dodges
1 <?php
2 // $Id: dodge.module,v 1.19 2008/04/18 12:12:54 davyvandenbremt Exp $
3
4 /**
5 * @file
6 * Dodge module.
7 *
8 * The dodge module mimics the Twitter functionality in a very basic way.
9 *
10 * Users can publish what they're doing, how they feel, their state (= dodge)...
11 *
12 * For each user a for in provided in a new category where he/she can set his state. State can be one of a list of predefined stated (provided by the webmaster) or a state typed in by the user in a free text box.
13 *
14 * A history of user states is kept so you can always see what someone's states was some time ago. The size of the history can be set.
15 *
16 * A few blocks are provided:
17 * - Most popular dodges.
18 * - Most recent dodges.
19 * - On a profile page: the user's history.
20 * - On every page: your own history (if you are logged in)
21 *
22 * Before version 1.0 is released we need :
23 * - all bugs resolved in the issue queue
24 * - a block (ajax) where you can specify your current dodge
25 *
26 * Then dodge will be ported to Drupal 6.
27 *
28 * In Drupal 6 version all blocks will have an ajax alternative.
29 *
30 * @todo add indexes ... but take care, because submitting them has a problem with age updating
31 * @todo 'show all dodges' should show all popular dodges when on popular block and all history on 'recent block'. on the user's blocks it should redirect to user block page
32 * @todo make a new block : random dodge where a random dodge of 1 user is displayed (with his picture if available)
33 * @todo provide a help function
34 * @todo let admin choose if forced_dodges are necessary
35 * @todo let admin choose if 'No dodge at this moment' is possible.
36 * @todo make a paged list with all states and the ability to delete them
37 * @todo bug: dodge_dodge_delete_confirm_submit -> do different query depending on type (free/forced), pass type via delete/1/free
38 * @todo all blocks should refresh by ajax
39 * @todo ?but should all existing free dodges be destoryed then?: let admin choose if user can define free dodges
40 * @todo let user choose if he wants his status to be shown on profile page
41 * @todo make button : remove all free dodges
42 * @todo make button : converted forced to free / free to forced
43 */
44
45 require_once 'dodge.inc';
46
47 define('DODGE_DEFAULT_MAX_BLOCK_SIZE', 5);
48 define('DODGE_LIMIT_AUTOCOMPLETE', 10);
49
50 /**
51 * Implementation of hook_menu().
52 */
53 function dodge_menu($may_cache) {
54 global $user;
55 $items = array();
56
57 if ($may_cache) {
58 $items[] = array(
59 'path' => 'dodge/autocomplete',
60 'title' => t('Dodge autocomplete'),
61 'callback' => 'dodge_autocomplete',
62 'access' => TRUE,
63 'type' => MENU_CALLBACK
64 );
65 $items[] = array(
66 'path' => 'dodge/browse',
67 'title' => t('Dodge user list'),
68 'callback' => 'dodge_browse',
69 'access' => TRUE,
70 'type' => MENU_SUGGESTED_ITEM
71 );
72 $items[] = array(
73 'path' => 'dodge/all',
74 'title' => t('All dodges'),
75 'callback' => 'dodge_dodge_list',
76 'access' => TRUE,
77 'type' => MENU_SUGGESTED_ITEM
78 );
79
80 /*
81 * Options.
82 */
83 $items[] = array(
84 'path' => 'admin/settings/dodge',
85 'title' => t('Dodge'),
86 'callback' => 'drupal_get_form',
87 'callback arguments' => array('dodge_settings_form'),
88 'access' => user_access('administer dodge'),
89 'type' => MENU_NORMAL_ITEM
90 );
91 $items[] = array(
92 'path' => 'admin/settings/dodge/general',
93 'title' => t('General'),
94 'callback' => 'drupal_get_form',
95 'callback arguments' => array('dodge_settings_form'),
96 'access' => user_access('administer dodge'),
97 'weight' => 0,
98 'type' => MENU_DEFAULT_LOCAL_TASK
99 );
100 $items[] = array(
101 'path' => 'admin/settings/dodge/options',
102 'title' => t('Presets'),
103 'callback' => 'dodge_preset_list',
104 'access' => user_access('administer dodge'),
105 'weight' => 1,
106 'type' => MENU_LOCAL_TASK
107 );
108 $items[] = array(
109 'path' => 'admin/settings/dodge/options/list',
110 'title' => t('List'),
111 'callback' => 'dodge_preset_list',
112 'access' => user_access('administer dodge'),
113 'weight' => 0,
114 'type' => MENU_DEFAULT_LOCAL_TASK
115 );
116 $items[] = array(
117 'path' => 'admin/settings/dodge/options/new',
118 'title' => t('New'),
119 'callback' => 'drupal_get_form',
120 'callback arguments' => array('dodge_preset_edit', 'new'),
121 'access' => user_access('administer dodge'),
122 'weight' => 1,
123 'type' => MENU_LOCAL_TASK
124 );
125 $items[] = array(
126 'path' => 'admin/settings/dodge/options/edit',
127 'title' => t('Edit option'),
128 'callback' => 'drupal_get_form',
129 'callback arguments' => array('dodge_preset_edit'),
130 'access' => user_access('administer dodge'),
131 'type' => MENU_CALLBACK,
132 );
133 $items[] = array(
134 'path' => 'admin/settings/dodge/options/delete',
135 'title' => t('Delete option'),
136 'callback' => 'drupal_get_form',
137 'callback arguments' => array('dodge_preset_delete_confirm'),
138 'access' => user_access('administer dodge'),
139 'type' => MENU_CALLBACK,
140 );
141 $items[] = array(
142 'path' => 'admin/settings/dodge/dodges',
143 'title' => t('Dodges'),
144 'callback' => 'dodge_dodge_list',
145 'callback arguments' => array(TRUE),
146 'access' => user_access('administer dodge'),
147 'weight' => 1,
148 'type' => MENU_LOCAL_TASK
149 );
150 $items[] = array(
151 'path' => 'admin/settings/dodge/dodges/list',
152 'title' => t('List'),
153 'callback' => 'dodge_dodge_list',
154 'callback arguments' => array(TRUE),
155 'access' => user_access('administer dodge'),
156 'weight' => 0,
157 'type' => MENU_DEFAULT_LOCAL_TASK
158 );
159 $items[] = array(
160 'path' => 'admin/settings/dodge/dodges/delete',
161 'title' => t('Delete dodge'),
162 'callback' => 'drupal_get_form',
163 'callback arguments' => array('dodge_dodge_delete_confirm'),
164 'access' => user_access('administer dodge'),
165 'type' => MENU_CALLBACK,
166 );
167 }
168 else {
169 if(arg(0) == 'user' && is_numeric(arg(1)) && arg(2) == 'dodge') {
170 $display_user = user_load((array('uid' => arg(1))));
171 if($user->uid) {
172 $items[] = array(
173 'path' => 'user/'.arg(1).'/dodge',
174 'title' => t('@name\'s dodges', array('@name'=>$display_user->name)),
175 'callback' => 'dodge_user_dodge',
176 'access' => TRUE,
177 'type' => MENU_CALLBACK,
178 );
179 /*if(arg(3) == 'rss.xml') {
180 $items[] = array(
181 'path' => 'user/'.arg(1).'/dodge/rss.xml',
182 'title' => t('@name\'s dodges', array('@name'=>$user->name)),
183 'callback' => 'dodge_user_dodge_feed',
184 'access' => TRUE,
185 'type' => MENU_CALLBACK,
186 );
187 }*/
188 }
189 }
190 }
191
192 return $items;
193 }
194
195 function dodge_user_dodge() {
196 // add feed icon
197 $user = user_load(array('uid' => arg(1)));
198 /*$feed_url = url('user/'.arg(1).'/dodge/rss.xml', NULL, NULL, TRUE);
199 drupal_add_feed($feed_url, t('@name\'s dodges', array('@name'=>$user->name)) . ' | '.variable_get('site_name', 'Drupal') .' '. t('RSS'));*/
200 $dodges = _dodge_history_of_user(arg(1), variable_get('dodge_max_log_size', 20), TRUE);
201 return theme('dodge_user_dodge', $dodges);
202 }
203
204 /**
205 * Format the page of user dodges.
206 *
207 * @ingroup themeable
208 */
209 function theme_dodge_user_dodge($dodges) {
210 $items = array();
211 foreach ($dodges as $dodge) {
212 $items[] = sprintf('%s %s %s %s',
213 _dodget_format_date_time_ago($dodge->time),
214 l($dodge->name, 'user/'. $dodge->uid),
215 t('was'),
216 l($dodge->dodge, 'dodge/browse/'. $dodge->dodge)
217 );
218 }
219 return theme('item_list', $items);
220 }
221
222 /*function dodge_user_dodge_feed() {
223 $dodges = _dodge_history_of_user(arg(1), variable_get('feed_default_items', 10), TRUE);
224 dodge_feed($dodges);
225 }*/
226
227 /**
228 * A generic function for generating RSS feeds from a set of nodes.
229 *
230 * @param $nodes
231 * An object as returned by db_query() which contains the nid field.
232 * @param $channel
233 * An associative array containing title, link, description and other keys.
234 * The link should be an absolute URL.
235 */
236 /*function dodge_feed($dodges) {
237 global $base_url, $locale;
238
239 $namespaces = array('xmlns:dc="http://purl.org/dc/elements/1.1/"');
240
241 foreach($dodges as $dodge) {
242 $link = url("dodge/$dodge->did", NULL, NULL, 1);
243
244 // Allow modules to add additional item fields and/or modify $item
245 //$extra = node_invoke_nodeapi($item, 'rss item');
246 $extra = array(
247 array(
248 'key' => 'pubDate',
249 'value' => date('r', $dodge->time)
250 ),
251 array(
252 'key' => 'guid',
253 'value' => $dodge->time . 'x' . $dodge->uid .' at '. $base_url,
254 'attributes' => array('isPermaLink' => 'false')
255 )
256 );
257 foreach ($extra as $element) {
258 if ($element['namespace']) {
259 $namespaces = array_merge($namespaces, $element['namespace']);
260 }
261 }
262
263 $item_text = sprintf('%s %s %s %s',
264 format_date($dodge->time),
265 $dodge->name,
266 t('was'),
267 $dodge->dodge
268 );
269
270 $items .= format_rss_item($item_text, $link, $item_text, $extra);
271 }
272
273 $channel = array(
274 'version' => '2.0',
275 'title' => variable_get('site_name', 'Drupal') .' - '. variable_get('site_slogan', ''),
276 'link' => $base_url,
277 'description' => variable_get('site_mission', ''),
278 'language' => $locale
279 );
280
281 $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
282 $output .= "<rss version=\"". $channel["version"] ."\" xml:base=\"". $base_url ."\" ". implode(' ', $namespaces) .">\n";
283 $output .= format_rss_channel($channel['title'], $channel['link'], $channel['description'], $items, $channel['language']);
284 $output .= "</rss>\n";
285
286 drupal_set_header('Content-Type: application/rss+xml; charset=utf-8');
287 print $output;
288 }*/
289
290 /**
291 * Implementation of hook_perm().
292 */
293 function dodge_perm() {
294 return array('can dodge', 'administer dodge');
295 }
296
297 /**
298 * Drupal settings form for specifying the general settings for this module.
299 */
300 function dodge_settings_form() {
301
302 $form['dodge_max_log_size'] = array(
303 '#type' => 'textfield',
304 '#title' => t('Maximum log size'),
305 '#default_value' => variable_get('dodge_max_log_size', 20),
306 '#size' => 3,
307 '#maxlength' => 5,
308 '#description' => t('Maximum number of dodges that will be remembered. To specify that no dodges should ever be removed, use -1.'),
309 '#validate' => array("_dodge_number_validate" => array()),
310 );
311
312 $form['dodge_dodges_per_page'] = array(
313 '#type' => 'textfield',
314 '#title' => t('Dodges per page'),
315 '#default_value' => variable_get('dodge_dodges_per_page', 20),
316 '#size' => 3,
317 '#maxlength' => 5,
318 '#description' => t('Maximum number of dodges that will be displayed on a page in a listing of dodges like dodge/all.'),
319 '#validate' => array("_dodge_number_validate" => array()),
320 );
321
322 $form['dodge_allow_free_dodge'] = array(
323 '#type' => 'checkbox',
324 '#title' => t('Should a user be allowed to free dodges?'),
325 '#default_value' => variable_get('dodge_allow_free_dodge', 1),
326 '#description' => t('Uncheck to only allow users to use the dodges you define.'),
327 );
328
329 $form['dodge_expire'] = array(
330 '#type' => 'textfield',
331 '#title' => t('Number of days after which dodges will be purged from the database'),
332 '#default_value' => variable_get('dodge_expire', 0),
333 '#size' => 4,
334 '#maxlength' => 4,
335 '#description' => t("Dodges will be permanently deleted after the number of days specified. Dodges will never expire when this is set to zero."),
336 '#validate' => array("_dodge_number_validate" => array()),
337 );
338
339 return system_settings_form($form);
340 }
341
342 /**
343 * Implementation of hook_cron().
344 */
345 function dodge_cron() {
346 $expiration = variable_get('dodge_expire', 0);
347 if ($expiration > 0) {
348 db_query('DELETE FROM {dodge} WHERE time < %d', time() - 60*60*24*$expiration);
349 }
350 }
351
352 function dodge_dodge_list($administer = FALSE) {
353 $sql = 'SELECT dodge, count(uid) AS count, if (MIN(forced_dodge) = 1, \'free\', \'forced\') as type FROM {dodge} GROUP BY dodge ORDER BY dodge ASC';
354 $sql_counter = 'SELECT COUNT(DISTINCT dodge) FROM {dodge}';
355 $result = pager_query($sql, variable_get('dodge_dodges_per_page', 20), 0, $sql_counter, $filter['args']);
356 $header = array(t('Dodge'), t('Occurences'));
357 if($administer) {
358 $header[] = t('Operations');
359 }
360 $rows = array();
361
362 while ($dodge = db_fetch_object($result)) {
363 $row = array();
364 $row[] = l($dodge->dodge, 'dodge/browse/'.$dodge->dodge);
365 $row[] = $dodge->count;
366 if($administer) {
367 $row[] = l(t('delete'), 'admin/settings/dodge/dodges/delete/'. $dodge->dodge);
368 }
369
370 $rows[] = $row;
371 }
372
373 if (empty($rows)) {
374 $rows[] = array(array('data' => t('No dodges specified yet.'), 'colspan' => '3', 'class' => 'message'));
375 }
376
377 return theme('table', $header, $rows) . theme('pager', NULL, variable_get('dodge_dodges_per_page', 20), 0);
378 }
379
380 function dodge_preset_list() {
381 $options = _dodge_get_forced_dodge_custom_options();
382 $header = array(t('Dodge'), array('data' => t('Operations'), 'colspan' => '2'));
383 $rows = array();
384
385 foreach ($options as $key => $option) {
386 $row = array();
387 $row[] = $option;
388 $row[] = l(t('edit'), 'admin/settings/dodge/options/edit/'. $key);
389 $row[] = l(t('delete'), 'admin/settings/dodge/options/delete/'. $key);
390
391 $rows[] = $row;
392 }
393
394 if (empty($rows)) {
395 $rows[] = array(array('data' => t('No options defined. <a href="!url">Create a new option</a>.', array('!url' => url('admin/settings/dodge/options/new'))), 'colspan' => '3', 'class' => 'message'));
396 }
397
398 return theme('table', $header, $rows);
399 }
400
401 function dodge_dodge_delete_confirm($dodge) {
402 $form['dodge'] = array('#type' => 'value', '#value' => $dodge);
403
404 return confirm_form($form,
405 t('Are you sure you want to delete the dodge %selector?', array('%selector' => $dodge)),
406 $_GET['destination'] ? $_GET['destination'] : 'admin/settings/dodge/dodges/list',
407 t('This action cannot be undone.'),
408 t('Delete'), t('Cancel')
409 );
410 }
411
412 function dodge_dodge_delete_confirm_submit($form_id, $form_values) {
413 $options = _dodge_get_forced_dodge_options();
414 $sql = 'UPDATE {dodge} SET dodge = \'%s\', free_dodge = \'\', forced_dodge = 0 WHERE free_dodge = \'%s\'';
415 db_query($sql, $dodge->dodge, $dodge->dodge);
416
417 drupal_set_message(t('Deleted dodge %name', array('%name' => $form_values['dodge'])));
418 drupal_goto('admin/settings/dodge/dodges/list');
419 }
420
421 function dodge_preset_delete_confirm($did) {
422 $dodges = _dodge_get_forced_dodge_custom_options();
423 $dodge = $dodges[$did];
424
425 $form['did'] = array('#type' => 'value', '#value' => $did);
426 $form['dodge'] = array('#type' => 'value', '#value' => $dodge);
427
428 return confirm_form($form,
429 t('Are you sure you want to delete the option %selector?', array('%selector' => $dodge)),
430 $_GET['destination'] ? $_GET['destination'] : 'admin/settings/dodge/options/list',
431 t('This action cannot be undone.'),
432 t('Delete'), t('Cancel')
433 );
434 }
435
436 function dodge_preset_delete_confirm_submit($form_id, $form_values) {
437 _dodge_preset_delete($form_values['did']);
438
439 $sql = 'UPDATE {dodge} SET dodge = \'%s\', free_dodge = \'%s\', forced_dodge = 1 WHERE forced_dodge = %d';
440 db_query($sql, $form_values['dodge'], $form_values['dodge'], $form_values['did']);
441
442 drupal_set_message(t('Deleted option %name', array('%name' => $form_values['dodge'])));
443 drupal_goto('admin/settings/dodge/options/list');
444 }
445
446 /**
447 * FAPI definition for the dodge preset edit form.
448 *
449 * @ingroup forms
450 * @see dodge_preset_edit_validate()
451 * @see dodge_preset_edit_submit()
452 */
453 function dodge_preset_edit($did) {
454 $dodge;
455 if (is_numeric($did)) {
456 $dodges = _dodge_get_forced_dodge_custom_options();
457 $dodge = $dodges[$did];
458 }
459 else {
460 $dodge = '';
461 }
462
463 $form = array();
464
465 $form['did'] = array(
466 '#type' => 'value',
467 '#value' => $did,
468 );
469
470 $form['dodge'] = array(
471 '#type' => 'textfield',
472 '#title' => t('Dodge'),
473 '#description' => t('Specify a dodge.'),
474 '#default_value' => $dodge,
475 '#maxlength' => DODGE_MAX_DODGE_LENGTH,
476 '#required' => TRUE,
477 );
478
479 $form['submit'] = array(
480 '#type' => 'submit',
481 '#value' => t('Submit'),
482 );
483
484 return $form;
485 }
486
487 function dodge_preset_edit_validate($form_id, $form_values) {
488 $sql = 'SELECT did FROM {dodge_options} WHERE LOWER(dodge) = LOWER(\'%s\')';
489 $result = db_query($sql, $form_values['dodge']);
490 if (db_num_rows($result) > 0) {
491 form_set_error('dodge', t('This dodge already exist.'));
492 }
493 }
494
495 function dodge_preset_edit_submit($form_id, $form_values) {
496 if ($form_values['did'] == 'new') {
497 $next_id = -1;
498 $return = _dodge_preset_create($form_values['dodge'], $next_id);
499 $sql = 'UPDATE {dodge} SET dodge = \'%s\', free_dodge = \'\', forced_dodge = %d WHERE free_dodge LIKE \'%s\'';
500 db_query($sql, $form_values['dodge'], $next_id, $form_values['dodge']);
501 }
502 elseif (is_numeric($form_values['did'])) {
503 $return = _dodge_preset_update($form_values['did'], $form_values['dodge']);
504 $sql = 'UPDATE {dodge} SET dodge = \'%s\' WHERE forced_dodge = %d';
505 db_query($sql, $form_values['dodge'], $form_values['did']);
506 }
507
508 if ($return) {
509 drupal_set_message(t('Updated preset %selector', array('%selector' => $form_values['dodge'])));
510 drupal_goto('admin/settings/dodge/options/list');
511 }
512 else {
513 drupal_set_message(t('The option was unable to be updated.', 'error'));
514 }
515 }
516
517 function _dodge_preset_create($dodge, &$next_id) {
518 $next_id = db_next_id('{dodge_options}_did');
519 return db_query('INSERT INTO {dodge_options} (did, dodge) VALUES (%d, \'%s\')', $next_id, $dodge);
520 }
521
522 function _dodge_preset_update($did, $dodge) {
523 return db_query('UPDATE {dodge_options} SET dodge =\'%s\' WHERE did = %d', $dodge, $did);
524 }
525
526 function _dodge_preset_delete($did) {
527 return db_query('DELETE FROM {dodge_options} where did = %d', $did);
528 }
529
530 function dodge_autocomplete($string = '') {
531 if (user_access('can dodge')) {
532 $matches = array();
533 if ($string) {
534 global $user;
535 $result = db_query_range("SELECT DISTINCT dodge FROM (SELECT dodge, if (uid = %d, 0, 1) AS weight FROM {dodge} WHERE uid = 1 UNION SELECT dodge, 2 AS weight FROM {dodge_options}) AS all_dodges WHERE LOWER(dodge) LIKE LOWER('%s%') ORDER BY weight ASC, dodge ASC", $user->uid, $string, 0, DODGE_LIMIT_AUTOCOMPLETE);
536 while ($dodge = db_fetch_object($result)) {
537 $matches[$dodge->dodge] = check_plain($dodge->dodge);
538 }
539 }
540 print drupal_to_js($matches);
541 }
542 exit();
543
544 }
545
546 function _dodget_format_date_time_ago($time) {
547 return t('@time ago', array('@time' => format_interval(time() - $time)));
548 }
549
550 /**
551 * Format the block of recent dodges.
552 *
553 * @ingroup themeable
554 */
555 function theme_dodge_block_recent_dodges($dodges) {
556 $items = array();
557 foreach ($dodges as $dodge) {
558 $items[] = sprintf('%s %s %s %s',
559 _dodget_format_date_time_ago($dodge->time),
560 l($dodge->name, 'user/'. $dodge->uid),
561 t('was'),
562 l($dodge->dodge, 'dodge/browse/'. $dodge->dodge)
563 );
564 }
565 return theme('item_list', $items).(variable_get('dodge_block_all_dodgesrecent_dodges', 1) ? l(t('Show all dodges'), 'dodge/all') : '');
566 }
567
568 /**
569 * Format the block of popular dodges.
570 *
571 * @ingroup themeable
572 */
573 function theme_dodge_block_popular_dodges($dodges) {
574 $items = array();
575 foreach ($dodges as $dodge) {
576 $items[] = sprintf('%s %s %s %s',
577 $dodge->count,
578 t(format_plural($dodge->count, t('person'), t('people'))),
579 format_plural($dodge->count, t('is'), t('are')),
580 l($dodge->dodge, 'dodge/browse/'. $dodge->dodge)
581 );
582 }
583 return theme('item_list', $items).(variable_get('dodge_block_all_dodgespopular_dodges', 1) ? l(t('Show all dodges'), 'dodge/all') : '');
584 }
585
586 /**
587 * Format the block of recent dodges for a specific user.
588 *
589 * @ingroup themeable
590 */
591 function theme_dodge_block_history_of_user($dodges, $display_user) {
592 global $user;
593 $is_current_user = ($display_user->uid == $user->uid);
594 $items = array();
595 foreach ($dodges as $dodge) {
596 $items[] = sprintf('%s %s %s %s',
597 _dodget_format_date_time_ago($dodge->time),
598 !$is_current_user ? $display_user->name : t('You'),
599 !$is_current_user ? t('was') : t('were'),
600 l($dodge->dodge, 'dodge/browse/'. $dodge->dodge)
601 );
602 }
603 return theme('item_list', $items).(variable_get('dodge_block_all_dodgeshistory_of_user', 1) ? l(t('Show all dodges'), 'user/'.$display_user->uid.'/dodge') : '');
604 }
605
606 /**
607 * Format the block of recent dodges of the current user.
608 *
609 * @ingroup themeable
610 */
611 function theme_dodge_block_history_of_current_user($dodges) {
612 global $user;
613 if (count($dodges) == 0) {
614 return t('You have not set a dodge yet. Set your dodge !link.', array('!link' => l(t('in your user settings'), 'user/'. $user->uid .'/edit/dodge')));
615 }
616 return theme('dodge_block_history_of_user', $dodges, $user);
617 }
618
619 function theme_dodge_user_view($dodges, $user) {
620 return theme('dodge_block_history_of_user', $dodges, $user);
621 }
622
623 function dodge_block_admin_configure_validate($form_values) {
624 if (variable_get('dodge_max_log_size', 20) > 0 && $form_values['#post']['limit'] > variable_get('dodge_max_log_size', 20)) {
625 form_set_error('limit', t('Value can be maximum !dodge_max_log_size', array('!dodge_max_log_size' => variable_get('dodge_max_log_size', 20))));
626 }
627 }
628
629 /**
630 * Implementation of hook_block().
631 */
632 function dodge_block($op = 'list', $delta = 0, $edit = array()) {
633
634 $blocks = array(
635 'dodge_form' =>
636 array('info' => t('What\'s your current dodge?')),
637 'history_of_user' =>
638 array('info' => t('User\'s dodge history')),
639 'history_of_current_user' =>
640 array('info' => t('Your dodge history')),
641 'recent_dodges' =>
642 array('info' => t('Recent dodges')),
643 'popular_dodges' =>
644 array('info' => t('Popular dodges'))
645 );
646
647 switch ($op) {
648 case 'list':
649 return $blocks;
650
651 case 'view':
652
653 $block = array();
654 $block['subject'] = $blocks[$delta]['info'];
655
656 if ($delta == 'dodge_form') {
657 $block['content'] = drupal_get_form('dodge_user_form', 'block');
658 }
659
660 if ($delta == 'popular_dodges') {
661 $dodges = _dodge_popular_dodges(variable_get('dodge_block_limit_popular_dodges', DODGE_DEFAULT_MAX_BLOCK_SIZE), variable_get('dodge_block_no_dodgepopular_dodges', 1));
662 if (count($dodges) > 0) {
663 $block['content'] = theme('dodge_block_popular_dodges', $dodges);
664 }
665 }
666
667 if ($delta == 'recent_dodges') {
668 $dodges = _dodge_recent_dodges(variable_get('dodge_block_limit_recent_dodges', DODGE_DEFAULT_MAX_BLOCK_SIZE), variable_get('dodge_block_no_dodgerecent_dodges', 1));
669 if (count($dodges) > 0) {
670 $block['content'] = theme('dodge_block_recent_dodges', $dodges);
671 }
672 }
673
674 if ($delta == 'history_of_user') {
675 global $user;
676 if (strpos($_GET['q'], 'user') !== FALSE && is_numeric(arg(1)) && $user->uid != arg(1)) {
677 $page_user = user_load(array('uid' => arg(1)));
678 $dodges = _dodge_history_of_user(arg(1), variable_get('dodge_block_limit_history_of_user', DODGE_DEFAULT_MAX_BLOCK_SIZE), variable_get('dodge_block_no_dodgehistory_of_user', 1));
679 $block['title'] = t('!user\'s dodge history', array('!user' => $page_user->name));
680 if (count($dodges) > 0) {
681 $block['content'] = theme('dodge_block_history_of_user', $dodges, $page_user);
682 }
683 }
684 }
685
686 if ($delta == 'history_of_current_user') {
687 global $user;
688 if ($user->uid != 0) {
689 $dodges = _dodge_history_of_user($user->uid, variable_get('dodge_block_limit_history_of_current_user', DODGE_DEFAULT_MAX_BLOCK_SIZE), variable_get('dodge_block_no_dodgehistory_of_current_user', 1));
690 $block['content'] = theme('dodge_block_history_of_current_user', $dodges);
691 }
692 }
693 if (!isset($block['content'])) return;
694 return $block;
695
696 case 'configure':
697 if ($delta == 'recent_dodges' || $delta == 'history_of_user' || $delta == 'history_of_current_user' || $delta == 'popular_dodges') {
698 $form = array();
699 $form['#validate'] = array('dodge_block_admin_configure_validate' => array());
700 $form['limit'] = array(
701 '#type' => 'textfield',
702 '#title' => t('Number of dodges to display'),
703 '#size' => 4,
704 '#description' => t('Up to this many dodges will be displayed in the block.'),
705 '#default_value' => variable_get('dodge_block_limit_'. $delta, 5),
706 );
707 $form['show_no_dodge'] = array(
708 '#type' => 'checkbox',
709 '#title' => t('Show \'No dodge at this moment.\''),
710 '#description' => t('Should the \'No dodge at this moment.\' state be displayed in this block?'),
711 '#default_value' => variable_get('dodge_block_no_dodge'. $delta, 1),
712 );
713 $form['show_all_dodges'] = array(
714 '#type' => 'checkbox',
715 '#title' => t('Show \'Show all dodges.\''),
716 '#description' => t('Should the \'Show all dodges.\' state be displayed in this block?'),
717 '#default_value' => variable_get('dodge_block_all_dodges'. $delta, 1),
718 );
719 return $form;
720 }
721
722 case 'save':
723 if ($delta == 'recent_dodges' || $delta == 'history_of_user' || $delta == 'history_of_current_user' || $delta == 'popular_dodges') {
724 variable_set('dodge_block_limit_'. $delta, $edit['limit']);
725 variable_set('dodge_block_no_dodge'. $delta, $edit['show_no_dodge']);
726 variable_set('dodge_block_all_dodges'. $delta, $edit['show_all_dodges']);
727 }
728 break;
729
730 default:
731 break;
732 }
733 }
734
735 function _dodge_get_forced_dodge_options() {
736 $options = array();
737 $options[0] = t('No dodge at this moment.');
738 if (variable_get('dodge_allow_free_dodge', 1)) {
739 $options[1] = t('I wanna pick my own dodge.');
740 }
741 $custom = _dodge_get_forced_dodge_custom_options();
742 foreach ($custom as $key => $option) {
743 $options[$key] = $option;
744 }
745 return $options;
746 }
747
748 function _dodge_get_forced_dodge_custom_options() {
749 $options = array();
750 $query = db_query('SELECT did, dodge FROM {dodge_options}');
751 while ($option = db_fetch_object($query)) {
752 $options[$option->did] = $option->dodge;
753 }
754 return $options;
755 }
756
757 /**
758 * Get the current dodge for a specific user.
759 *
760 * @param $uid
761 * The user id of the user the dodge is requested for.
762 * @return
763 * Object with uid, dodge, forced_dodge, free_dodge if dodge is available. NULL if no dodge is available.
764 */
765 function _dodge_get_dodge($uid) {
766 $sql = 'SELECT uid, dodge, forced_dodge, free_dodge FROM {dodge} WHERE uid = %d AND age = 0';
767 $result = db_query($sql, $uid);
768 if ($result) {
769 return db_fetch_object($result, $uid);
770 }
771 return NULL;
772 }
773
774 /**
775 * Implementation of hook_user().
776 */
777 function dodge_user($op, &$edit, &$edituser, $category = null) {
778 global $user;
779
780 if($op == 'view') {
781 $dodges = _dodge_history_of_user(arg(1), variable_get('dodge_block_limit_history_of_user', DODGE_DEFAULT_MAX_BLOCK_SIZE), variable_get('dodge_block_no_dodgehistory_of_user', 1));
782 $display_user = user_load(array('uid'=>arg(1)));
783 $items['dodge'] = array(
784 'value' => theme('dodge_user_view', $dodges, $display_user),
785 'class' => 'dodge',
786 );
787
788 return array(t('Dodge history') => $items);
789 }
790 if (user_access('can dodge') && $edituser->uid == $user->uid) {
791 if ($op == 'categories') {
792 $data = array();
793 $data[] = array(
794 'name' => 'dodge',
795 'title' => t('Dodge'),
796 'weight' => '1'
797 );
798 return $data;
799 }
800
801 /* if ($category == 'account') {
802 switch($op) {
803 case 'form':
804 //$options = _dodge_get_forced_dodge_options();
805 $form = array();
806 $form['dodge'] = array('#type' => 'fieldset',
807 '#title' => t('Dodge'),
808 '#collapsible' => TRUE,
809 '#collapsed' => TRUE,
810 );
811 $form['dodge']['show_block_history_of_user'] = array('#type' => 'checkbox',
812 '#title' => t('Show my dodge history to others'),
813 '#default_value' => TRUE,//$edituser->forced_dodge,
814 '#description' => t('This block shows other users your latest dodges.'),
815 '#default_value' => $edituser->show_block_history_of_user,
816 );
817 $form['dodge']['show_block_get_log_by_currentuser'] = array('#type' => 'checkbox',
818 '#title' => t('Show me my user history on every page'),
819 '#default_value' => TRUE,//$edituser->forced_dodge,
820 '#description' => t('This block shows your latest dodge dodges.'),
821 '#default_value' => $edituser->show_block_get_log_by_currentuser,
822 );
823 return $form;
824
825 }
826 }*/
827 if ($category == 'dodge') {
828 $user_dodge = _dodge_get_dodge($edituser->uid);
829
830 switch ($op) {
831 case 'after_update':
832 unset($edituser->dodge);
833 unset($edituser->free_dodge);
834 unset($edituser->forced_dodge);
835 user_save($edituser);
836 break;
837 case 'submit':
838 _dodge_submit($user_dodge, $edit, $edituser);
839
840 break;
841
842 case 'validate':
843 _dodge_validate($edit);
844 break;
845
846 case 'form':
847 return dodge_user_form('user');
848
849 case 'delete':
850 _dodge_delete_user_history($edituser->uid, FALSE);
851 break;
852
853 }
854 }
855 }
856 }
857
858 function _dodge_submit($user_dodge, $edit, $edituser) {
859
860 if (!($user_dodge->free_dodge == $edit['free_dodge'] && $user_dodge->forced_dodge == $edit['forced_dodge'])) {
861 $options = _dodge_get_forced_dodge_options();
862 $edit['dodge'] = $options[0];
863
864 if (strtolower($options[0]) == strtolower(trim($edit['free_dodge']))) {
865 $edit['forced_dodge'] = 0;
866 $edit['free_dodge'] = '';
867 }
868 if (strtolower($options[1]) == strtolower(trim($edit['free_dodge']))) {
869 $edit['forced_dodge'] = 1;
870 }
871 foreach ($options as $key => $option) {
872 if (strtolower($option) == strtolower(trim($edit['free_dodge']))) {
873 $edit['forced_dodge'] = $key;
874 }
875 }
876
877 if (isset($edit['forced_dodge']) && $edit['forced_dodge'] != 1 && $edit['forced_dodge'] != 0) {
878
879 $edit['dodge'] = $options[$edit['forced_dodge']];
880 $edit['free_dodge'] = '';
881 }
882 else if (isset($edit['free_dodge']) && $edit['forced_dodge'] == 1 && trim($edit['free_dodge']) != '') {
883 $edit['dodge'] = $edit['free_dodge'];
884 }
885 else if ($edit['forced_dodge'] == 0) {
886 $edit['free_dodge'] = '';
887 }
888
889 _dodge_log_dodge($edituser->uid, $edit['dodge'], $edit['free_dodge'], $edit['forced_dodge']);
890 drupal_set_message(t('Your current dodge state has been succesfully set to %state.', array('%state'=>$edit['dodge'])));
891 }
892 }
893
894 function _dodge_validate($edit) {
895 if ($edit['forced_dodge'] == 1 && trim($edit['free_dodge']) == '') {
896 form_set_error('free_dodge', t('This field can not be empty. Please specify a dodge or choose one from the list.'));
897 }
898 }
899
900 function dodge_user_form_validate($form_id, $form_values) {
901 _dodge_validate($form_values);
902 }
903
904 function dodge_user_form_submit($form_id, $form_values) {
905 global $user;
906 $user_dodge = _dodge_get_dodge($user->uid);
907 _dodge_submit($user_dodge, $form_values, $user);
908 }
909
910 /**
911 * Get a drupal form for specifying a new dodge.
912 * @ingroup forms
913 *
914 * @param $type
915 * 'block' if the form is for a block. 'user' if it is a user edit form.
916 * @return
917 * Drupal form for specifying a new dodge.
918 * @see dodge_preset_edit_validate()
919 * @see dodge_preset_edit_submit()
920 */
921 function dodge_user_form($type) {
922 $options = _dodge_get_forced_dodge_options();
923 $form = array();
924 $form['forced_dodge'] = array('#type' => 'select',
925 '#title' => t('Choose a dodge'),
926 '#default_value' => $user_dodge->forced_dodge,
927 '#options' => $options,
928 '#description' => t('By specifying a dodge, you let other users know what you do, how you feel,... If your dodge is not in this list, choose !pick_my_own_dodge and fill in the field below.', array('!pick_my_own_dodge' => t('I wanna pick my own dodge'))),
929 );
930 if (variable_get('dodge_allow_free_dodge', 1)) {
931 $form['free_dodge'] = array(
932 '#prefix' => '<div id="field_free_dodge">',
933 '#suffix' => '</div>',
934 '#type' => 'textfield',
935 '#title' => t('Or type your own dodge dodge'),
936 '#maxlength' => DODGE_MAX_DODGE_LENGTH,
937 '#autocomplete_path' => 'dodge/autocomplete',
938 '#description' => t('The value of this field is only used when you select !pick_my_own_dodge as your dodge in the field !choose_a_dodge', array('!pick_my_own_dodge' => t('I wanna pick my own dodge'), '!choose_a_dodge' => t('Choose a dodge'))),
939 '#default_value' => $user_dodge->free_dodge,
940 );
941 if($type == 'block') {
942 $form['free_dodge']['#size'] = 20;
943 }
944 drupal_add_js(
945 'if (Drupal.jsEnabled) {
946 $(document).ready(function() {
947 if ($("#edit-forced-dodge").val() != 1) {
948 $("#field_free_dodge").hide();
949 }
950 //var oldvalue = $("#edit-forced-dodge").val();
951 $("#edit-forced-dodge").change(
952
953 function() {
954 if ($("#edit-forced-dodge").val() == 1) {
955 $("#field_free_dodge").show("normal");
956 }
957 else if (!$("#field_free_dodge").is(":hidden")) {
958 $("#field_free_dodge").hide("normal");
959 }
960 //oldvalue = $("#edit-forced-dodge").val();
961 }
962 );
963 });
964 }',
965 'inline'
966 );
967 }
968 if ($type == 'block') {
969 $form['submit'] = array(
970 '#type' => 'submit',
971 '#value' => t('Submit')
972 );
973
974 }
975 return $form;
976 }
977
978 /**
979 * Get a list of the most popular dodges.
980 *
981 * @param $total
982 * 'block' if the form is for a block. 'user' if it is a user edit form.
983 * @param $show_no_dodge
984 * Boolean to specify if the dodges that are 'No dodge at this moment.' should be included.
985 * @return
986 * Array of dodges. The dodge is represented as an object with the fields dodge and count.
987 */
988 function _dodge_popular_dodges($total, $show_no_dodge) {
989 if (!$show_no_dodge) {
990 $filter = 'AND d.forced_dodge <> 0 ';
991 }
992 $sql = 'SELECT d.dodge, count(d.uid) as count FROM {dodge} d INNER JOIN {users} u ON u.uid = d.uid WHERE d.age = 0 AND u.status = 1 '. $filter .' GROUP BY d.dodge ORDER BY count(d.uid), min(d.time) DESC';
993 $result = db_query_range($sql, 0, $total);
994 $dodges = array();
995 while ($row = db_fetch_object($result)) {
996 $dodges[] = $row;
997 }
998 return $dodges;
999 }
1000
1001 /**
1002 * Get a list of the most recent dodges.
1003 *
1004 * @param $total
1005 * 'block' if the form is for a block. 'user' if it is a user edit form.
1006 * @param $show_no_dodge
1007 * Boolean to specify if the dodges that are 'No dodge at this moment.' should be included.
1008 * @return
1009 * Array of dodges. The dodge is represented as an object with the fields dodge and count.
1010 */
1011 function _dodge_recent_dodges($total, $show_no_dodge) {
1012 if (!$show_no_dodge) {
1013 $filter = 'AND d.forced_dodge <> 0 ';
1014 }
1015 $sql = 'SELECT d.uid, u.name, d.dodge, d.time FROM {dodge} d INNER JOIN {users} u ON u.uid = d.uid WHERE d.age = 0 AND u.status = 1 '. $filter .'ORDER BY time DESC';
1016 $result = db_query_range($sql, 0, $total);
1017 $dodges = array();
1018 while ($row = db_fetch_object($result)) {
1019 $dodges[] = $row;
1020 }
1021 return $dodges;
1022 }
1023
1024 /**
1025 * Get a list of the most recent dodges of a specfic user.
1026 *
1027 * @param $uid
1028 * The user id of the user the dodges are requested for.
1029 * @param $total
1030 * 'block' if the form is for a block. 'user' if it is a user edit form.
1031 * @param $show_no_dodge
1032 * Boolean to specify if the dodges that are 'No dodge at this moment.' should be included.
1033 * @return
1034 * Array of dodges. The dodge is represented as an object with the fields dodge and count.
1035 */
1036 function _dodge_history_of_user($uid, $total, $show_no_dodge) {
1037 if (!$show_no_dodge) {
1038 $filter = 'AND d.forced_dodge <> 0 ';
1039 }
1040 $sql = 'SELECT d.uid, u.name, d.dodge, d.time FROM {dodge} d INNER JOIN {users} u ON u.uid = d.uid WHERE u.uid = %d '. $filter .'ORDER BY time DESC';
1041 $result = db_query_range($sql, $uid, 0, $total);
1042 $dodges = array();
1043 while ($row = db_fetch_object($result)) {
1044 $dodges[] = $row;
1045 }
1046 return $dodges;
1047 }
1048
1049 /**
1050 * Delete the dodge history for a specific user.
1051 *
1052 * @param $uid
1053 * The user id of the action is requested for.
1054 */
1055 function _dodge_delete_user_history($uid) {
1056 $sql = 'DELETE FROM {dodge} WHERE uid = %d';
1057 db_query($sql);
1058 }
1059
1060 /**
1061 * Add a new dodge for a specific user.
1062 *
1063 * @param $uid
1064 * The user id of the action is requested for.
1065 * @param $dodge
1066 * The resulting.
1067 * @param $free_dodge
1068 * The original free dodge if the current dodge is a free dodge. Empty if not.
1069 * @param $forced_dodge
1070 * The forced dodge id. 0 for no dodge, 1 for free dodge, above 1 for admin defined dodges.
1071 */
1072 function _dodge_log_dodge($uid, $dodge, $free_dodge, $forced_dodge) {
1073 $sql = 'UPDATE {dodge} SET age = age + 1 WHERE uid = %d';
1074 db_query($sql, $uid);
1075
1076 $sql = 'INSERT INTO {dodge} (uid, dodge, free_dodge, forced_dodge, age, time) values (%d, \'%s\', \'%s\', %d, %d, %d)';
1077 db_query($sql, $uid, $dodge, $free_dodge, $forced_dodge, 0, time());
1078
1079 if (variable_get('dodge_max_log_size', 20) > 0) {
1080 $sql = 'DELETE FROM {dodge} WHERE age > %d';
1081 db_query($sql, variable_get('dodge_max_log_size', 20));
1082 }
1083 }
1084
1085 function dodge_browse() {
1086 if (arg(2) == '' || !user_access('access user profiles')) {
1087 drupal_not_found();
1088 }
1089 // Extract the affected users:
1090 $result = pager_query("SELECT u.uid, u.access FROM {users} u INNER JOIN {dodge} d ON u.uid = d.uid WHERE d.dodge = '%s' AND d.age = 0 AND u.access <> 0 AND u.status <> 0 ORDER BY u.access DESC", 20, 0, NULL, arg(2));
1091
1092 $users = array();
1093 while ($account = db_fetch_object($result)) {
1094 $users[] = $account->uid;
1095 }
1096
1097 return theme('dodge_browse', $users);
1098 }
1099
1100 function theme_dodge_browse($users) {
1101 if(module_exists('profile')) {
1102 $fields = array();
1103 $result_fields = db_query('SELECT name, title, type, weight, page FROM {profile_fields} WHERE visibility = %d ORDER BY weight', 3);
1104 while ($record = db_fetch_object($result_fields)) {
1105 $fields[] = $record;
1106 }
1107
1108 $output = '<div id="profile">';
1109 foreach($users as $user) {
1110 $account = user_load(array('uid' => $user));
1111 $profile = _profile_update_user_fields($fields, $account);
1112 $output .= theme('profile_listing', $account, $profile);
1113 }
1114
1115 $output .= '</div>';
1116 } else {
1117 $items = array();
1118 foreach($users as $user) {
1119 $account = user_load(array('uid' => $user));
1120 $items[] = l($account->name, 'user/'.$account->uid);
1121 }
1122 $output .= theme('item_list', $items);
1123 }
1124
1125 $output .= theme('pager', NULL, 20);
1126
1127 $title = t('People having dodge %title', array('%title' => arg(2)));
1128 drupal_set_title($title);
1129
1130 return $output;
1131 }
1132
1133 /**
1134 * Validate a form field to make sure it's a number.
1135 *
1136 * @param $field
1137 * The field to be validated.
1138 */
1139 function _dodge_number_validate($field) {
1140 if (!empty($field['#value']) && !is_numeric($field['#value'])) {
1141 form_error($field, t('The value for %field must be a number.', array('%field' => $field['#title'])));
1142 }
1143 }

  ViewVC Help
Powered by ViewVC 1.1.2