Mercurial > defr > drupal > og_statistics
comparison og_statistics.module @ 2:48f07e7acaca
OG Stats 1.0-rc2
author | Franck Deroche <franck@defr.org> |
---|---|
date | Tue, 24 Nov 2009 14:27:37 +0100 |
parents | 9ce879ecbce6 |
children | 13824d66b299 |
comparison
equal
deleted
inserted
replaced
0:9ce879ecbce6 | 2:48f07e7acaca |
---|---|
1 <?php | 1 <?php |
2 // $Id: og_statistics.module,v 1.2.2.1 2009/06/05 22:38:12 dereine Exp $ | 2 // $Id: og_statistics.module,v 1.2.2.6 2009/09/07 19:22:31 dereine Exp $ |
3 | 3 |
4 /** | 4 /** |
5 * @file | 5 * @file |
6 * Logs statistics of the organic group module. | 6 * Logs statistics of the organic group module. |
7 * | 7 * |
13 * | 13 * |
14 * Remove all og_statistics_load in update/add/remove functions, its not needed there. | 14 * Remove all og_statistics_load in update/add/remove functions, its not needed there. |
15 * | 15 * |
16 * Make more functions use arrays instaed of single values. | 16 * Make more functions use arrays instaed of single values. |
17 */ | 17 */ |
18 | |
19 /** | |
20 * Implementation of hook_menu(). | |
21 */ | |
22 function og_statistics_menu() { | |
23 $items = array(); | |
24 $items['admin/settings/og_statistics'] = array( | |
25 'title' => 'Og Statistics settings', | |
26 'description' => '', | |
27 'page callback' => 'drupal_get_form', | |
28 'page arguments' => array('og_statistics_settings'), | |
29 'access arguments' => array('administer organic groups'), | |
30 'file' => 'og_statistics.admin.inc', | |
31 'type' => MENU_NORMAL_ITEM, | |
32 ); | |
33 return $items; | |
34 } | |
18 | 35 |
19 /** | 36 /** |
20 * Central og_statistics api function. | 37 * Central og_statistics api function. |
21 * | 38 * |
22 * This function is not used yet. | 39 * This function is not used yet. |
56 elseif (og_is_group_post_type($node->type)) { | 73 elseif (og_is_group_post_type($node->type)) { |
57 if (isset($node->og_groups)) { | 74 if (isset($node->og_groups)) { |
58 $node->og_groups = array_unique($node->og_groups); | 75 $node->og_groups = array_unique($node->og_groups); |
59 foreach ($node->og_groups as $gid) { | 76 foreach ($node->og_groups as $gid) { |
60 og_statistics_add_node($gid); | 77 og_statistics_add_node($gid); |
61 og_statistics_update_last_node($node->created, $gid); | 78 og_statistics_update_last_node($node, $gid); |
62 } | 79 } |
63 } | 80 } |
64 } | 81 } |
65 break; | 82 break; |
66 case 'delete': | 83 case 'delete': |
67 // Remove a record for group. | 84 // Remove a record for group. |
68 if (og_is_group_type($node->type)) { | 85 if (og_is_group_type($node->type)) { |
69 og_statistics_delete_record($node); | 86 og_statistics_delete_record($node->nid); |
70 } | 87 } |
71 // Update statistics. | 88 // Update statistics. |
72 elseif (og_is_group_post_type($node->type)) { | 89 elseif (og_is_group_post_type($node->type)) { |
73 if (isset($node->og_groups)) { | 90 if (isset($node->og_groups)) { |
74 $node->og_groups = array_unique($node->og_groups); | 91 $node->og_groups = array_unique($node->og_groups); |
75 foreach ($node->og_groups as $gid) { | 92 foreach ($node->og_groups as $gid) { |
76 og_statistics_remove_node($gid); | 93 og_statistics_remove_node($node->nid, $gid); |
77 } | 94 } |
78 } | 95 } |
79 } | 96 } |
80 break; | 97 break; |
81 case 'update': | 98 case 'update': |
84 if (isset($node->og_groups)) { | 101 if (isset($node->og_groups)) { |
85 $updated_gid = array_intersect($node->og_groups, $node->og_initial_groups); | 102 $updated_gid = array_intersect($node->og_groups, $node->og_initial_groups); |
86 $added_gid = array_diff($node->og_groups, $node->og_initial_groups); | 103 $added_gid = array_diff($node->og_groups, $node->og_initial_groups); |
87 $removed_gid = array_diff($node->og_initial_groups, $node->og_groups); | 104 $removed_gid = array_diff($node->og_initial_groups, $node->og_groups); |
88 foreach ($updated_gid as $gid) { | 105 foreach ($updated_gid as $gid) { |
89 og_statistics_update_last_node($node->changed, $gid); | 106 og_statistics_update_last_node($node, $gid); |
90 } | 107 } |
91 foreach ($added_gid as $gid) { | 108 foreach ($added_gid as $gid) { |
92 og_statistics_add_node($gid); | 109 og_statistics_add_node($gid); |
93 og_statistics_update_last_node($node->changed, $gid); | 110 og_statistics_update_last_node($node, $gid); |
94 } | 111 } |
95 foreach ($removed_gid as $gid) { | 112 foreach ($removed_gid as $gid) { |
96 og_statistics_remove_node($gid); | 113 og_statistics_remove_node($node->nid, $gid); |
97 } | 114 } |
98 } | 115 } |
99 } | 116 } |
100 } | 117 } |
101 } | 118 } |
108 case 'insert': | 125 case 'insert': |
109 $node = node_load($a1['nid']); | 126 $node = node_load($a1['nid']); |
110 if (og_is_group_post_type($node->type)) { | 127 if (og_is_group_post_type($node->type)) { |
111 foreach ($node->og_groups as $gid) { | 128 foreach ($node->og_groups as $gid) { |
112 og_statistics_add_comment($gid); | 129 og_statistics_add_comment($gid); |
113 og_statistics_update_last_comment($a1['timestamp'], $gid); | 130 og_statistics_update_last_comment($a1, $gid); |
114 } | 131 } |
115 } | 132 } |
116 break; | 133 break; |
117 case 'delete': | 134 case 'delete': |
118 $node = node_load($a1->nid); | 135 $node = node_load($a1->nid); |
124 break; | 141 break; |
125 case 'update': | 142 case 'update': |
126 $node = node_load($a1['nid']); | 143 $node = node_load($a1['nid']); |
127 if (og_is_group_post_type($node->type)) { | 144 if (og_is_group_post_type($node->type)) { |
128 foreach ($node->og_groups as $gid) { | 145 foreach ($node->og_groups as $gid) { |
129 og_statistics_update_last_comment($a1['timestamp'], $gid); | 146 og_statistics_update_last_comment($a1, $gid); |
130 } | 147 } |
131 } | 148 } |
132 break; | 149 break; |
133 } | 150 } |
134 } | 151 } |
139 function og_statistics_og($op, $gid, $uid, $args) { | 156 function og_statistics_og($op, $gid, $uid, $args) { |
140 switch ($op) { | 157 switch ($op) { |
141 case 'user insert': | 158 case 'user insert': |
142 $time = time(); | 159 $time = time(); |
143 og_statistics_add_user($gid); | 160 og_statistics_add_user($gid); |
144 og_statistics_update_last_member($time, $gid); | 161 og_statistics_update_last_member($time, $uid, $gid); |
145 break; | 162 break; |
146 case 'user delete': | 163 case 'user delete': |
147 og_statistics_remove_user($gid); | 164 og_statistics_remove_user($gid); |
148 break; | 165 break; |
149 } | 166 } |
178 'posts_count' => 0, | 195 'posts_count' => 0, |
179 'comments_count' => 0, | 196 'comments_count' => 0, |
180 'last_node_timestamp' => 0, | 197 'last_node_timestamp' => 0, |
181 'last_comment_timestamp' => 0, | 198 'last_comment_timestamp' => 0, |
182 'last_member_timestamp' => 0, | 199 'last_member_timestamp' => 0, |
200 'last_node_nid' => 0, | |
201 'last_node_uid' => 0, | |
202 'last_comment_cid' => 0, | |
203 'last_comment_nid' => 0, | |
204 'last_comment_uid' => 0, | |
205 'last_member_uid' => 0, | |
183 ); | 206 ); |
184 drupal_write_record('og_statistics', $stat); | 207 drupal_write_record('og_statistics', $stat); |
185 } | 208 } |
186 | 209 |
187 /** | 210 /** |
200 * Removes 1 form posts_count of a group. | 223 * Removes 1 form posts_count of a group. |
201 * | 224 * |
202 * @param $gid | 225 * @param $gid |
203 * The group nid. | 226 * The group nid. |
204 */ | 227 */ |
205 function og_statistics_remove_node($gid) { | 228 function og_statistics_remove_node($nid, $gid) { |
206 $stat = og_statistics_load($gid); | 229 $stat = og_statistics_load($gid); |
207 $stat['posts_count']--; | 230 $stat['posts_count']--; |
208 // Load the count of comments and remove this amount of comments. | 231 // Load the count of comments and remove this amount of comments. |
209 $node = node_load($gid); | 232 $node = node_load($nid); |
210 $stat['comments_count'] -= $node->comment_count; | 233 $stat['comments_count'] -= $node->comment_count; |
211 | 234 |
212 drupal_write_record('og_statistics', $stat, 'nid'); | 235 drupal_write_record('og_statistics', $stat, 'nid'); |
213 } | 236 } |
214 | 237 |
215 /** | 238 /** |
216 * Updates the last node of a group. | 239 * Updates the last node of a group. |
217 * | 240 * |
218 * @param $gid | 241 * @param $node |
219 * The group nid. | 242 * A node object. |
220 */ | 243 * @param $gid |
221 function og_statistics_update_last_node($timestamp, $gid) { | 244 * The group nid. |
222 $stat = og_statistics_load($gid); | 245 */ |
223 $stat['last_node_timestamp'] = $timestamp; | 246 function og_statistics_update_last_node($node, $gid) { |
247 $stat = og_statistics_load($gid); | |
248 $stat['last_node_timestamp'] = $node->changed; | |
249 $stat['last_node_uid'] = $node->uid; | |
250 $stat['last_node_nid'] = $node->nid; | |
224 drupal_write_record('og_statistics', $stat, 'nid'); | 251 drupal_write_record('og_statistics', $stat, 'nid'); |
225 } | 252 } |
226 | 253 |
227 /** | 254 /** |
228 * Add 1 to comments_count of a group. | 255 * Add 1 to comments_count of a group. |
249 } | 276 } |
250 | 277 |
251 /** | 278 /** |
252 * Updates the last comment of a group. | 279 * Updates the last comment of a group. |
253 * | 280 * |
254 * @param $gid | 281 * @param $comment |
255 * The group nid. | 282 * A comment array. |
256 */ | 283 * @param $gid |
257 function og_statistics_update_last_comment($timestamp, $gid) { | 284 * The group nid. |
258 $stat = og_statistics_load($gid); | 285 */ |
259 $stat['last_comment_timestamp'] = $timestamp; | 286 function og_statistics_update_last_comment($comment, $gid) { |
287 $stat = og_statistics_load($gid); | |
288 $stat['last_comment_timestamp'] = $comment['timestamp']; | |
289 $stat['last_comment_uid'] = $comment['uid']; | |
290 $stat['last_comment_nid'] = $comment['nid']; | |
291 $stat['last_comment_cid'] = $comment['cid']; | |
260 drupal_write_record('og_statistics', $stat, 'nid'); | 292 drupal_write_record('og_statistics', $stat, 'nid'); |
261 } | 293 } |
262 | 294 |
263 /** | 295 /** |
264 * Add 1 to members_count of a group. | 296 * Add 1 to members_count of a group. |
287 /** | 319 /** |
288 * Updates the last member of a group. | 320 * Updates the last member of a group. |
289 * | 321 * |
290 * @param $gid | 322 * @param $gid |
291 * The group nid. | 323 * The group nid. |
292 */ | 324 * @param $uid |
293 function og_statistics_update_last_member($timestamp, $gid) { | 325 * The uid of the latest member. |
326 */ | |
327 function og_statistics_update_last_member($timestamp, $uid, $gid) { | |
294 $stat = og_statistics_load($gid); | 328 $stat = og_statistics_load($gid); |
295 $stat['last_member_timestamp'] = $timestamp; | 329 $stat['last_member_timestamp'] = $timestamp; |
330 $stat['last_member_uid'] = $uid; | |
296 drupal_write_record('og_statistics', $stat, 'nid'); | 331 drupal_write_record('og_statistics', $stat, 'nid'); |
297 } | 332 } |
298 | 333 |
299 /** | 334 /** |
300 * Removes a complete record. | 335 * Removes a complete record. |
303 * The group nid. | 338 * The group nid. |
304 */ | 339 */ |
305 function og_statistics_delete_record($gid) { | 340 function og_statistics_delete_record($gid) { |
306 db_query("DELETE FROM {og_statistics} WHERE nid = %d", $gid); | 341 db_query("DELETE FROM {og_statistics} WHERE nid = %d", $gid); |
307 } | 342 } |
343 | |
344 /** | |
345 * Recalcs mulitple records. | |
346 * | |
347 * @param $nids | |
348 * A list of nids to recalc the records. | |
349 */ | |
350 function og_statistcs_recalc($nids = array()) { | |
351 foreach ($nids as $nid) { | |
352 // All statistics are set to zero. | |
353 $stat = array( | |
354 'nid' => $nid, | |
355 'members_count' => 0, | |
356 'posts_count' => 0, | |
357 'comments_count' => 0, | |
358 'last_node_timestamp' => 0, | |
359 'last_comment_timestamp' => 0, | |
360 'last_member_timestamp' => 0, | |
361 'last_node_nid' => 0, | |
362 'last_node_uid' => 0, | |
363 'last_comment_cid' => 0, | |
364 'last_comment_nid' => 0, | |
365 'last_comment_uid' => 0, | |
366 'last_member_uid' => 0, | |
367 ); | |
368 $stat['members_count'] = db_result(db_query("SELECT COUNT(uid) FROM {og_uid} WHERE nid = %d", $nid)); | |
369 $stat['posts_count'] = db_result(db_query("SELECT COUNT(nid) FROM {og_ancestry} WHERE group_nid = %d", $nid)); | |
370 $stat['comments_count'] = db_result(db_query("SELECT COUNT(c.cid) FROM {comments} c | |
371 INNER JOIN {og_ancestry} oa ON oa.nid = c.nid WHERE oa.group_nid = %d", $nid)); | |
372 | |
373 $array = db_fetch_array(db_query_range("SELECT n.uid AS last_node_uid, n.nid AS last_node_nid, n.created AS last_node_timestamp FROM {node} n | |
374 INNER JOIN {og_ancestry} oa ON oa.nid = n.nid WHERE oa.group_nid = %d ORDER BY n.created DESC", $nid, 0, 1)); | |
375 $array = $array ? $array : array(); | |
376 $stat = array_merge($stat, $array); | |
377 $array = db_fetch_array(db_query_range("SELECT c.nid AS last_comment_nid, c.uid AS last_comment_uid, c.cid AS last_comment_cid, c.timestamp AS last_comment_timestamp FROM {comments} c | |
378 INNER JOIN {og_ancestry} oa ON oa.nid = c.nid WHERE group_nid = %d ORDER BY c.timestamp DESC | |
379 ", $nid, 0, 1)); | |
380 $array = $array ? $array : array(); | |
381 $stat = array_merge($stat, $array); | |
382 $array = db_fetch_array(db_query_range("SELECT created AS last_member_timestamp, uid AS last_member_uid FROM {og_uid} | |
383 WHERE nid = %d ORDER BY created DESC", $nid, 0, 1)); | |
384 $array = $array ? $array : array(); | |
385 $stat = array_merge($stat, $array); | |
386 | |
387 if (og_statistics_load($nid)) { | |
388 drupal_write_record('og_statistics', $stat, 'nid'); | |
389 } | |
390 else { | |
391 drupal_write_record('og_statistics', $stat); | |
392 } | |
393 } | |
394 } | |
395 | |
396 function og_statistcs_settings_finished() { | |
397 drupal_set_message('Statistics rebuilded successfull'); | |
398 } | |
399 | |
308 | 400 |
309 /** | 401 /** |
310 * views stuff. | 402 * views stuff. |
311 */ | 403 */ |
312 | 404 |