comparison og_statistics.module @ 0:9ce879ecbce6

OG Stats beta 3
author Franck Deroche <franck@defr.org>
date Tue, 24 Nov 2009 14:25:13 +0100
parents
children 0aae3e2e6309 48f07e7acaca
comparison
equal deleted inserted replaced
-1:000000000000 0:9ce879ecbce6
1 <?php
2 // $Id: og_statistics.module,v 1.2.2.1 2009/06/05 22:38:12 dereine Exp $
3
4 /**
5 * @file
6 * Logs statistics of the organic group module.
7 *
8 * @todo
9 * Write more inline comments.
10 * Build a central api function.
11 * Use the abstract api function to remove all this functions,
12 * like og_stat_add_*, og_stat_update_*.
13 *
14 * Remove all og_statistics_load in update/add/remove functions, its not needed there.
15 *
16 * Make more functions use arrays instaed of single values.
17 */
18
19 /**
20 * Central og_statistics api function.
21 *
22 * This function is not used yet.
23 *
24 * @param $gid
25 * The group nid.
26 *
27 * @param $key
28 * The statistics key, for example members_count.
29 *
30 * @param $method
31 * How should the value be handled, possible values:
32 * - set: Ignore the previos value.
33 * - add: add a integer to the previos value.
34 *
35 * @param $value
36 * The new statistics value.
37 *
38 */
39 function og_statistics_update_statistic($gid, $key, $method = 'set', $value) {
40 $stat = og_statistics_load($gid);
41 $stat[$key] = $method == 'set' ? $value : $stat[$key] + $value;
42 return drupal_write_record('og_statistics', $stat, 'nid');
43 }
44
45 /**
46 * Implementation of hook_nodeapi().
47 */
48 function og_statistics_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
49 switch ($op) {
50 case 'insert':
51 // Adds a new record for the new group.
52 if (og_is_group_type($node->type)) {
53 og_statistics_write_pure_record($node->nid);
54 }
55 // Update statistics.
56 elseif (og_is_group_post_type($node->type)) {
57 if (isset($node->og_groups)) {
58 $node->og_groups = array_unique($node->og_groups);
59 foreach ($node->og_groups as $gid) {
60 og_statistics_add_node($gid);
61 og_statistics_update_last_node($node->created, $gid);
62 }
63 }
64 }
65 break;
66 case 'delete':
67 // Remove a record for group.
68 if (og_is_group_type($node->type)) {
69 og_statistics_delete_record($node);
70 }
71 // Update statistics.
72 elseif (og_is_group_post_type($node->type)) {
73 if (isset($node->og_groups)) {
74 $node->og_groups = array_unique($node->og_groups);
75 foreach ($node->og_groups as $gid) {
76 og_statistics_remove_node($gid);
77 }
78 }
79 }
80 break;
81 case 'update':
82 // Update statistics.
83 if (og_is_group_post_type($node->type)) {
84 if (isset($node->og_groups)) {
85 $updated_gid = array_intersect($node->og_groups, $node->og_initial_groups);
86 $added_gid = array_diff($node->og_groups, $node->og_initial_groups);
87 $removed_gid = array_diff($node->og_initial_groups, $node->og_groups);
88 foreach ($updated_gid as $gid) {
89 og_statistics_update_last_node($node->changed, $gid);
90 }
91 foreach ($added_gid as $gid) {
92 og_statistics_add_node($gid);
93 og_statistics_update_last_node($node->changed, $gid);
94 }
95 foreach ($removed_gid as $gid) {
96 og_statistics_remove_node($gid);
97 }
98 }
99 }
100 }
101 }
102
103 /**
104 * Implementation of hook_comment().
105 */
106 function og_statistics_comment(&$a1, $op) {
107 switch ($op) {
108 case 'insert':
109 $node = node_load($a1['nid']);
110 if (og_is_group_post_type($node->type)) {
111 foreach ($node->og_groups as $gid) {
112 og_statistics_add_comment($gid);
113 og_statistics_update_last_comment($a1['timestamp'], $gid);
114 }
115 }
116 break;
117 case 'delete':
118 $node = node_load($a1->nid);
119 if (og_is_group_post_type($node->type)) {
120 foreach ($node->og_groups as $gid) {
121 og_statistics_remove_comment($gid);
122 }
123 }
124 break;
125 case 'update':
126 $node = node_load($a1['nid']);
127 if (og_is_group_post_type($node->type)) {
128 foreach ($node->og_groups as $gid) {
129 og_statistics_update_last_comment($a1['timestamp'], $gid);
130 }
131 }
132 break;
133 }
134 }
135
136 /**
137 * Implementation of hook_og().
138 */
139 function og_statistics_og($op, $gid, $uid, $args) {
140 switch ($op) {
141 case 'user insert':
142 $time = time();
143 og_statistics_add_user($gid);
144 og_statistics_update_last_member($time, $gid);
145 break;
146 case 'user delete':
147 og_statistics_remove_user($gid);
148 break;
149 }
150 }
151
152 /**
153 * Returns a statistic for a group().
154 *
155 * @param $gid
156 * The group nid.
157 *
158 * @todo
159 * Build perhaps a static cache here.
160 *
161 */
162 function og_statistics_load($gid) {
163 $result = db_query("SELECT * FROM {og_statistics} WHERE nid = %d", $gid);
164 return db_fetch_array($result);
165 }
166
167 /**
168 * Writes a record of statistics without any content, but nid.
169 *
170 * @param $gid
171 * The group nid.
172 */
173 function og_statistics_write_pure_record($gid) {
174 // All statistics are set to zero.
175 $stat = array(
176 'nid' => $gid,
177 'members_count' => 0,
178 'posts_count' => 0,
179 'comments_count' => 0,
180 'last_node_timestamp' => 0,
181 'last_comment_timestamp' => 0,
182 'last_member_timestamp' => 0,
183 );
184 drupal_write_record('og_statistics', $stat);
185 }
186
187 /**
188 * Add 1 to posts_count of a group.
189 *
190 * @param $gid
191 * The group nid.
192 */
193 function og_statistics_add_node($gid) {
194 $stat = og_statistics_load($gid);
195 $stat['posts_count']++;
196 drupal_write_record('og_statistics', $stat, 'nid');
197 }
198
199 /**
200 * Removes 1 form posts_count of a group.
201 *
202 * @param $gid
203 * The group nid.
204 */
205 function og_statistics_remove_node($gid) {
206 $stat = og_statistics_load($gid);
207 $stat['posts_count']--;
208 // Load the count of comments and remove this amount of comments.
209 $node = node_load($gid);
210 $stat['comments_count'] -= $node->comment_count;
211
212 drupal_write_record('og_statistics', $stat, 'nid');
213 }
214
215 /**
216 * Updates the last node of a group.
217 *
218 * @param $gid
219 * The group nid.
220 */
221 function og_statistics_update_last_node($timestamp, $gid) {
222 $stat = og_statistics_load($gid);
223 $stat['last_node_timestamp'] = $timestamp;
224 drupal_write_record('og_statistics', $stat, 'nid');
225 }
226
227 /**
228 * Add 1 to comments_count of a group.
229 *
230 * @param $gid
231 * The group nid.
232 */
233 function og_statistics_add_comment($gid) {
234 $stat = og_statistics_load($gid);
235 $stat['comments_count']++;
236 drupal_write_record('og_statistics', $stat, 'nid');
237 }
238
239 /**
240 * Removes 1 from comments_count of a group.
241 *
242 * @param $gid
243 * The group nid.
244 */
245 function og_statistics_remove_comment($gid) {
246 $stat = og_statistics_load($gid);
247 $stat['comments_count']--;
248 drupal_write_record('og_statistics', $stat, 'nid');
249 }
250
251 /**
252 * Updates the last comment of a group.
253 *
254 * @param $gid
255 * The group nid.
256 */
257 function og_statistics_update_last_comment($timestamp, $gid) {
258 $stat = og_statistics_load($gid);
259 $stat['last_comment_timestamp'] = $timestamp;
260 drupal_write_record('og_statistics', $stat, 'nid');
261 }
262
263 /**
264 * Add 1 to members_count of a group.
265 *
266 * @param $gid
267 * The group nid.
268 */
269 function og_statistics_add_user($gid) {
270 $stat = og_statistics_load($gid);
271 $stat['members_count']++;
272 drupal_write_record('og_statistics', $stat, 'nid');
273 }
274
275 /**
276 * Removes 1 from members_count of a group.
277 *
278 * @param $gid
279 * The group nid.
280 */
281 function og_statistics_remove_user($gid) {
282 $stat = og_statistics_load($gid);
283 $stat['members_count']--;
284 drupal_write_record('og_statistics', $stat, 'nid');
285 }
286
287 /**
288 * Updates the last member of a group.
289 *
290 * @param $gid
291 * The group nid.
292 */
293 function og_statistics_update_last_member($timestamp, $gid) {
294 $stat = og_statistics_load($gid);
295 $stat['last_member_timestamp'] = $timestamp;
296 drupal_write_record('og_statistics', $stat, 'nid');
297 }
298
299 /**
300 * Removes a complete record.
301 *
302 * @param $gid
303 * The group nid.
304 */
305 function og_statistics_delete_record($gid) {
306 db_query("DELETE FROM {og_statistics} WHERE nid = %d", $gid);
307 }
308
309 /**
310 * views stuff.
311 */
312
313 /**
314 * Implementation of hook_views_api().
315 */
316 function og_statistics_views_api() {
317 return array(
318 'api' => 2,
319 );
320 }
321