diff 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
line wrap: on
line diff
--- a/og_statistics.module	Tue Nov 24 14:25:13 2009 +0100
+++ b/og_statistics.module	Tue Nov 24 14:27:37 2009 +0100
@@ -1,5 +1,5 @@
 <?php
-// $Id: og_statistics.module,v 1.2.2.1 2009/06/05 22:38:12 dereine Exp $
+// $Id: og_statistics.module,v 1.2.2.6 2009/09/07 19:22:31 dereine Exp $
 
 /**
  * @file
@@ -17,6 +17,23 @@
  */
 
 /**
+ * Implementation of hook_menu().
+ */
+function og_statistics_menu() {
+  $items = array();
+  $items['admin/settings/og_statistics'] = array(
+    'title' => 'Og Statistics settings',
+    'description' => '',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('og_statistics_settings'),
+    'access arguments' => array('administer organic groups'),
+    'file' => 'og_statistics.admin.inc',
+    'type' => MENU_NORMAL_ITEM,
+  );
+  return $items;
+}
+
+/**
  * Central og_statistics api function.
  *
  * This function is not used yet.
@@ -58,7 +75,7 @@
           $node->og_groups = array_unique($node->og_groups);
           foreach ($node->og_groups as $gid) {
             og_statistics_add_node($gid);
-            og_statistics_update_last_node($node->created, $gid);
+            og_statistics_update_last_node($node, $gid);
           }
         }
       }
@@ -66,14 +83,14 @@
     case 'delete':
       // Remove a record for group.
       if (og_is_group_type($node->type)) {
-        og_statistics_delete_record($node);
+        og_statistics_delete_record($node->nid);
       }
       // Update statistics.
       elseif (og_is_group_post_type($node->type)) {
         if (isset($node->og_groups)) {
           $node->og_groups = array_unique($node->og_groups);
           foreach ($node->og_groups as $gid) {
-            og_statistics_remove_node($gid);
+            og_statistics_remove_node($node->nid, $gid);
           }
         }
       }
@@ -86,14 +103,14 @@
           $added_gid = array_diff($node->og_groups, $node->og_initial_groups);
           $removed_gid = array_diff($node->og_initial_groups, $node->og_groups);
           foreach ($updated_gid as $gid) {
-            og_statistics_update_last_node($node->changed, $gid);
+            og_statistics_update_last_node($node, $gid);
           }
           foreach ($added_gid as $gid) {
             og_statistics_add_node($gid);
-            og_statistics_update_last_node($node->changed, $gid);
+            og_statistics_update_last_node($node, $gid);
           }
           foreach ($removed_gid as $gid) {
-            og_statistics_remove_node($gid);
+            og_statistics_remove_node($node->nid, $gid);
           }
         }
       }
@@ -110,7 +127,7 @@
       if (og_is_group_post_type($node->type)) {
         foreach ($node->og_groups as $gid) {
           og_statistics_add_comment($gid);
-          og_statistics_update_last_comment($a1['timestamp'], $gid);
+          og_statistics_update_last_comment($a1, $gid);
         }
       }
       break;
@@ -126,7 +143,7 @@
       $node = node_load($a1['nid']);
       if (og_is_group_post_type($node->type)) {
         foreach ($node->og_groups as $gid) {
-          og_statistics_update_last_comment($a1['timestamp'], $gid);
+          og_statistics_update_last_comment($a1, $gid);
         }
       }
       break;
@@ -141,7 +158,7 @@
     case 'user insert':
       $time = time();
       og_statistics_add_user($gid);
-      og_statistics_update_last_member($time, $gid);
+      og_statistics_update_last_member($time, $uid, $gid);
       break;
     case 'user delete':
       og_statistics_remove_user($gid);
@@ -180,6 +197,12 @@
     'last_node_timestamp' => 0,
     'last_comment_timestamp' => 0,
     'last_member_timestamp' => 0,
+    'last_node_nid' => 0,
+    'last_node_uid' => 0,
+    'last_comment_cid' => 0,
+    'last_comment_nid' => 0,
+    'last_comment_uid' => 0,
+    'last_member_uid' => 0,
   );
   drupal_write_record('og_statistics', $stat);
 }
@@ -202,11 +225,11 @@
  * @param $gid
  *   The group nid.
  */
-function og_statistics_remove_node($gid) {
+function og_statistics_remove_node($nid, $gid) {
   $stat = og_statistics_load($gid);
   $stat['posts_count']--;
   // Load the count of comments and remove this amount of comments.
-  $node = node_load($gid);
+  $node = node_load($nid);
   $stat['comments_count'] -= $node->comment_count;
 
   drupal_write_record('og_statistics', $stat, 'nid');
@@ -215,12 +238,16 @@
 /**
  * Updates the last node of a group.
  *
+ * @param $node
+ *   A node object.
  * @param $gid
  *   The group nid.
  */
-function og_statistics_update_last_node($timestamp, $gid) {
+function og_statistics_update_last_node($node, $gid) {
   $stat = og_statistics_load($gid);
-  $stat['last_node_timestamp'] = $timestamp;
+  $stat['last_node_timestamp'] = $node->changed;
+  $stat['last_node_uid'] = $node->uid;
+  $stat['last_node_nid'] = $node->nid;
   drupal_write_record('og_statistics', $stat, 'nid');
 }
 
@@ -251,12 +278,17 @@
 /**
  * Updates the last comment of a group.
  *
+ * @param $comment
+ *   A comment array.
  * @param $gid
  *   The group nid.
  */
-function og_statistics_update_last_comment($timestamp, $gid) {
+function og_statistics_update_last_comment($comment, $gid) {
   $stat = og_statistics_load($gid);
-  $stat['last_comment_timestamp'] = $timestamp;
+  $stat['last_comment_timestamp'] = $comment['timestamp'];
+  $stat['last_comment_uid'] = $comment['uid'];
+  $stat['last_comment_nid'] = $comment['nid'];
+  $stat['last_comment_cid'] = $comment['cid'];
   drupal_write_record('og_statistics', $stat, 'nid');
 }
 
@@ -289,10 +321,13 @@
  *
  * @param $gid
  *   The group nid.
+ * @param $uid
+ *   The uid of the latest member.
  */
-function og_statistics_update_last_member($timestamp, $gid) {
+function og_statistics_update_last_member($timestamp, $uid, $gid) {
   $stat = og_statistics_load($gid);
   $stat['last_member_timestamp'] = $timestamp;
+  $stat['last_member_uid'] = $uid;
   drupal_write_record('og_statistics', $stat, 'nid');
 }
 
@@ -307,6 +342,63 @@
 }
 
 /**
+ * Recalcs mulitple records.
+ *
+ * @param $nids
+ *  A list of nids to recalc the records.
+ */
+function og_statistcs_recalc($nids = array()) {
+  foreach ($nids as $nid) {
+    // All statistics are set to zero.
+    $stat = array(
+      'nid' => $nid,
+      'members_count' => 0,
+      'posts_count' => 0,
+      'comments_count' => 0,
+      'last_node_timestamp' => 0,
+      'last_comment_timestamp' => 0,
+      'last_member_timestamp' => 0,
+      'last_node_nid' => 0,
+      'last_node_uid' => 0,
+      'last_comment_cid' => 0,
+      'last_comment_nid' => 0,
+      'last_comment_uid' => 0,
+      'last_member_uid' => 0,
+    );
+    $stat['members_count'] = db_result(db_query("SELECT COUNT(uid) FROM {og_uid} WHERE nid = %d", $nid));
+    $stat['posts_count'] = db_result(db_query("SELECT COUNT(nid) FROM {og_ancestry} WHERE group_nid = %d", $nid));
+    $stat['comments_count'] = db_result(db_query("SELECT COUNT(c.cid) FROM {comments} c
+      INNER JOIN {og_ancestry} oa ON oa.nid = c.nid WHERE oa.group_nid = %d", $nid));
+
+    $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
+      INNER JOIN {og_ancestry} oa ON oa.nid = n.nid WHERE oa.group_nid = %d ORDER BY n.created DESC", $nid, 0, 1));
+    $array = $array ? $array : array();
+    $stat = array_merge($stat, $array);
+    $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
+      INNER JOIN {og_ancestry} oa ON oa.nid = c.nid WHERE group_nid = %d ORDER BY c.timestamp DESC
+      ", $nid, 0, 1));
+    $array = $array ? $array : array();
+    $stat = array_merge($stat, $array);
+    $array = db_fetch_array(db_query_range("SELECT created AS last_member_timestamp, uid AS last_member_uid FROM {og_uid}
+      WHERE nid = %d ORDER BY created DESC", $nid, 0, 1));
+    $array = $array ? $array : array();
+    $stat = array_merge($stat, $array);
+
+    if (og_statistics_load($nid)) {
+      drupal_write_record('og_statistics', $stat, 'nid');
+    }
+    else {
+      drupal_write_record('og_statistics', $stat);
+    }
+  }
+}
+
+function og_statistcs_settings_finished() {
+  drupal_set_message('Statistics rebuilded successfull');
+}
+
+
+/**
  * views stuff.
  */