/[drupal]/contributions/modules/user_display/user_display_replacements.module
ViewVC logotype

Contents of /contributions/modules/user_display/user_display_replacements.module

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


Revision 1.2 - (show annotations) (download) (as text)
Thu Aug 14 13:04:21 2008 UTC (15 months, 1 week ago) by smk
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +10 -13 lines
File MIME type: text/x-php
by smk-ka: Fixed user count query.
1 <?php
2 // $Id: user_display_replacements.module,v 1.1 2008/07/19 23:12:33 sun Exp $
3
4 /**
5 * @file
6 * Alternative block implementations making use of User Display.
7 */
8
9 /**
10 * @ingroup user_display_api
11 * @{
12 */
13
14 /**
15 * Implementation of hook_user_display().
16 */
17 function user_display_replacements_user_display($op) {
18 switch ($op) {
19 case 'info':
20 return array(
21 'user_block_new' => array(
22 'name' => t("Who's new block"),
23 'description' => t("These settings apply to user pictures displayed in the replacement block <em>Who's new (User Display)</em>.")
24 ),
25 'user_block_online' => array(
26 'name' => t("Who's online block"),
27 'description' => t("These settings apply to user pictures displayed in the replacement block <em>Who's online (User Display)</em>.")
28 ),
29 );
30 break;
31 }
32 }
33
34 /**
35 * @} End of "ingroup user_display_api".
36 */
37
38 /**
39 * Implementation of hook_block().
40 *
41 * Replacments for the core user_block() implementations. Note the added
42 * display class in the calls to theme('user_list'), which has a corresponding
43 * entry in the user_display_replacements_user_display() hook info array.
44 */
45 function user_display_replacements_block($op = 'list', $delta = 0, $edit = array()) {
46 global $user;
47
48 if ($op == 'list') {
49 $blocks['user_display_new']['info'] = t("Who's new (User Display)");
50 $blocks['user_display_online']['info'] = t("Who's online (User Display)");
51 return $blocks;
52 }
53 else if ($op == 'view') {
54 $block = array();
55 switch ($delta) {
56 case 'user_display_new':
57 if (user_access('access content')) {
58 // Retrieve a list of new users who have subsequently accessed the site successfully.
59 $items = array();
60 $result = db_query_range('SELECT uid, name, picture FROM {users} WHERE status != 0 AND access != 0 ORDER BY created DESC', 0, variable_get('user_block_whois_new_count', 5));
61 while ($account = db_fetch_object($result)) {
62 $items[] = $account;
63 }
64 if ($items) {
65 $block['subject'] = t('Who\'s new');
66 $block['content'] = '<div class="block-user-new">'. theme('user_list', $items, NULL, 'user_block_new') .'</div>';
67 }
68 }
69 break;
70
71 case 'user_display_online':
72 if (user_access('access content')) {
73 // Count users active within the defined period.
74 $interval = time() - variable_get('user_block_seconds_online', 900);
75
76 // Perform database queries to gather online user lists. We use s.timestamp
77 // rather than u.access because it is much faster.
78 $anonymous_count = sess_count($interval);
79 $authenticated_count = db_result(db_query('SELECT COUNT(DISTINCT uid) FROM {sessions} WHERE timestamp >= %d AND uid > 0', $interval));
80
81
82 // Format the output with proper grammar.
83 if ($anonymous_count == 1 && $authenticated_count == 1) {
84 $output = t('There is currently %members and %visitors online.', array('%members' => format_plural($authenticated_count, '1 user', '@count users'), '%visitors' => format_plural($anonymous_count, '1 guest', '@count guests')));
85 }
86 else {
87 $output = t('There are currently %members and %visitors online.', array('%members' => format_plural($authenticated_count, '1 user', '@count users'), '%visitors' => format_plural($anonymous_count, '1 guest', '@count guests')));
88 }
89
90 // Display a list of currently online users.
91 if ($authenticated_count) {
92 if (variable_get('user_display_block_pictures_only', 0)) {
93 $query = " AND u.picture <> ''";
94 }
95 // Use GROUP BY to prevent users from appearing more than once.
96 $result = db_query_range('SELECT DISTINCT u.uid, u.name, u.picture FROM {users} u INNER JOIN {sessions} s ON s.uid = u.uid WHERE s.timestamp >= %d AND s.uid > 0'. $query .' ORDER BY s.timestamp DESC', $interval, 0, variable_get('user_block_max_list_count', 10));
97
98 $items = array();
99 while ($account = db_fetch_object($result)) {
100 $items[] = $account;
101 }
102
103 // Configurable sub-heading.
104 $title = (variable_get('user_display_block_online_subheading', 0) ? t('Online users') : '');
105 $output .= theme('user_list', $items, $title, 'user_block_online');
106 }
107
108 $block['subject'] = t('Who\'s online');
109 $block['content'] = '<div class="block-user-online">'. $output .'</div>';
110 }
111 break;
112 }
113
114 return $block;
115 }
116 else if ($op == 'configure') {
117 switch ($delta) {
118 case 'user_display_new':
119 $form['user_block_whois_new_count'] = array(
120 '#type' => 'select',
121 '#title' => t('Number of users to display'),
122 '#default_value' => variable_get('user_block_whois_new_count', 5),
123 '#options' => drupal_map_assoc(range(1, 10)),
124 );
125 break;
126
127 case 'user_display_online':
128 $period = drupal_map_assoc(array(30, 60, 120, 180, 300, 600, 900, 1800, 2700, 3600, 5400, 7200, 10800, 21600, 43200, 86400), 'format_interval');
129 $form['user_block_seconds_online'] = array('#type' => 'select', '#title' => t('User activity'), '#default_value' => variable_get('user_block_seconds_online', 900), '#options' => $period, '#description' => t('A user is considered online for this long after they have last viewed a page.'));
130 $form['user_block_max_list_count'] = array('#type' => 'select', '#title' => t('User list length'), '#default_value' => variable_get('user_block_max_list_count', 10), '#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 40, 50, 75, 100)), '#description' => t('Maximum number of currently online users to display.'));
131 $form['user_display_block_pictures_only'] = array(
132 '#type' => 'checkbox',
133 '#title' => t('Only with picture'),
134 '#return_value' => 1,
135 '#default_value' => variable_get('user_display_block_pictures_only', 0),
136 '#description' => t('Show only users that have uploaded a picture.'),
137 );
138 $form['user_display_block_online_subheading'] = array(
139 '#type' => 'checkbox',
140 '#title' => t('Display subheading'),
141 '#return_value' => 1,
142 '#default_value' => variable_get('user_display_block_online_subheading', 0),
143 '#description' => t('Whether to display the <em>Users online</em> sub-heading.'),
144 );
145 break;
146 }
147 return $form;
148 }
149 else if ($op == 'save') {
150 switch ($delta) {
151 case 'user_display_new':
152 variable_set('user_block_whois_new_count', $edit['user_block_whois_new_count']);
153 break;
154
155 case 'user_display_online':
156 variable_set('user_block_seconds_online', $edit['user_block_seconds_online']);
157 variable_set('user_block_max_list_count', $edit['user_block_max_list_count']);
158 variable_set('user_display_block_pictures_only', $edit['user_display_block_pictures_only']);
159 variable_set('user_display_block_online_subheading', $edit['user_display_block_online_subheading']);
160 break;
161 }
162 }
163 }
164

  ViewVC Help
Powered by ViewVC 1.1.2