diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/og_statistics.module	Tue Nov 24 14:25:13 2009 +0100
@@ -0,0 +1,321 @@
+<?php
+// $Id: og_statistics.module,v 1.2.2.1 2009/06/05 22:38:12 dereine Exp $
+
+/**
+ * @file
+ *   Logs statistics of the organic group module.
+ *
+ * @todo
+ *   Write more inline comments.
+ *   Build a central api function.
+ *   Use the abstract api function to remove all this functions,
+ *   like og_stat_add_*, og_stat_update_*.
+ *
+ *   Remove all og_statistics_load in update/add/remove functions, its not needed there.
+ *
+ *   Make more functions use arrays instaed of single values.
+ */
+
+/**
+ * Central og_statistics api function.
+ *
+ * This function is not used yet.
+ *
+ * @param $gid
+ *  The group nid.
+ *
+ * @param $key
+ *   The statistics key, for example members_count.
+ *
+ * @param $method
+ *   How should the value be handled, possible values:
+ *   - set: Ignore the previos value.
+ *   - add: add a integer to the previos value.
+ *
+ * @param $value
+ *   The new statistics value.
+ *
+ */
+function og_statistics_update_statistic($gid, $key, $method = 'set', $value) {
+  $stat = og_statistics_load($gid);
+  $stat[$key] = $method == 'set' ? $value : $stat[$key] + $value;
+  return drupal_write_record('og_statistics', $stat, 'nid');
+}
+
+/**
+ * Implementation of hook_nodeapi().
+ */
+function og_statistics_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
+  switch ($op) {
+    case 'insert':
+      // Adds a new record for the new group.
+      if (og_is_group_type($node->type)) {
+        og_statistics_write_pure_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_add_node($gid);
+            og_statistics_update_last_node($node->created, $gid);
+          }
+        }
+      }
+      break;
+    case 'delete':
+      // Remove a record for group.
+      if (og_is_group_type($node->type)) {
+        og_statistics_delete_record($node);
+      }
+      // 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);
+          }
+        }
+      }
+      break;
+    case 'update':
+      // Update statistics.
+      if (og_is_group_post_type($node->type)) {
+        if (isset($node->og_groups)) {
+          $updated_gid = array_intersect($node->og_groups, $node->og_initial_groups);
+          $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);
+          }
+          foreach ($added_gid as $gid) {
+            og_statistics_add_node($gid);
+            og_statistics_update_last_node($node->changed, $gid);
+          }
+          foreach ($removed_gid as $gid) {
+            og_statistics_remove_node($gid);
+          }
+        }
+      }
+  }
+}
+
+/**
+ * Implementation of hook_comment().
+ */
+function og_statistics_comment(&$a1, $op) {
+  switch ($op) {
+    case 'insert':
+      $node = node_load($a1['nid']);
+      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);
+        }
+      }
+      break;
+    case 'delete':
+      $node = node_load($a1->nid);
+      if (og_is_group_post_type($node->type)) {
+        foreach ($node->og_groups as $gid) {
+          og_statistics_remove_comment($gid);
+        }
+      }
+      break;
+    case 'update':
+      $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);
+        }
+      }
+      break;
+  }
+}
+
+/**
+ * Implementation of hook_og().
+ */
+function og_statistics_og($op, $gid, $uid, $args) {
+  switch ($op) {
+    case 'user insert':
+      $time = time();
+      og_statistics_add_user($gid);
+      og_statistics_update_last_member($time, $gid);
+      break;
+    case 'user delete':
+      og_statistics_remove_user($gid);
+      break;
+  }
+}
+
+/**
+ * Returns a statistic for a group().
+ *
+ * @param $gid
+ *   The group nid.
+ *
+ * @todo
+ *   Build perhaps a static cache here.
+ *
+ */
+function og_statistics_load($gid) {
+  $result = db_query("SELECT * FROM {og_statistics} WHERE nid = %d", $gid);
+  return db_fetch_array($result);
+}
+
+/**
+ * Writes a record of statistics without any content, but nid.
+ *
+ * @param $gid
+ *   The group nid.
+ */
+function og_statistics_write_pure_record($gid) {
+  // All statistics are set to zero.
+  $stat = array(
+    'nid' => $gid,
+    'members_count' => 0,
+    'posts_count' => 0,
+    'comments_count' => 0,
+    'last_node_timestamp' => 0,
+    'last_comment_timestamp' => 0,
+    'last_member_timestamp' => 0,
+  );
+  drupal_write_record('og_statistics', $stat);
+}
+
+/**
+ * Add 1 to posts_count of a group.
+ *
+ * @param $gid
+ *   The group nid.
+ */
+function og_statistics_add_node($gid) {
+  $stat = og_statistics_load($gid);
+  $stat['posts_count']++;
+  drupal_write_record('og_statistics', $stat, 'nid');
+}
+
+/**
+ * Removes 1 form posts_count of a group.
+ *
+ * @param $gid
+ *   The group nid.
+ */
+function og_statistics_remove_node($gid) {
+  $stat = og_statistics_load($gid);
+  $stat['posts_count']--;
+  // Load the count of comments and remove this amount of comments.
+  $node = node_load($gid);
+  $stat['comments_count'] -= $node->comment_count;
+
+  drupal_write_record('og_statistics', $stat, 'nid');
+}
+
+/**
+ * Updates the last node of a group.
+ *
+ * @param $gid
+ *   The group nid.
+ */
+function og_statistics_update_last_node($timestamp, $gid) {
+  $stat = og_statistics_load($gid);
+  $stat['last_node_timestamp'] = $timestamp;
+  drupal_write_record('og_statistics', $stat, 'nid');
+}
+
+/**
+ * Add 1 to comments_count of a group.
+ *
+ * @param $gid
+ *   The group nid.
+ */
+function og_statistics_add_comment($gid) {
+  $stat = og_statistics_load($gid);
+  $stat['comments_count']++;
+  drupal_write_record('og_statistics', $stat, 'nid');
+}
+
+/**
+ * Removes 1 from comments_count of a group.
+ *
+ * @param $gid
+ *   The group nid.
+ */
+function og_statistics_remove_comment($gid) {
+  $stat = og_statistics_load($gid);
+  $stat['comments_count']--;
+  drupal_write_record('og_statistics', $stat, 'nid');
+}
+
+/**
+ * Updates the last comment of a group.
+ *
+ * @param $gid
+ *   The group nid.
+ */
+function og_statistics_update_last_comment($timestamp, $gid) {
+  $stat = og_statistics_load($gid);
+  $stat['last_comment_timestamp'] = $timestamp;
+  drupal_write_record('og_statistics', $stat, 'nid');
+}
+
+/**
+ * Add 1 to members_count of a group.
+ *
+ * @param $gid
+ *   The group nid.
+ */
+function og_statistics_add_user($gid) {
+  $stat = og_statistics_load($gid);
+  $stat['members_count']++;
+  drupal_write_record('og_statistics', $stat, 'nid');
+}
+
+/**
+ * Removes 1 from members_count of a group.
+ *
+ * @param $gid
+ *   The group nid.
+ */
+function og_statistics_remove_user($gid) {
+  $stat = og_statistics_load($gid);
+  $stat['members_count']--;
+  drupal_write_record('og_statistics', $stat, 'nid');
+}
+
+/**
+ * Updates the last member of a group.
+ *
+ * @param $gid
+ *   The group nid.
+ */
+function og_statistics_update_last_member($timestamp, $gid) {
+  $stat = og_statistics_load($gid);
+  $stat['last_member_timestamp'] = $timestamp;
+  drupal_write_record('og_statistics', $stat, 'nid');
+}
+
+/**
+ * Removes a complete record.
+ *
+ * @param $gid
+ *   The group nid.
+ */
+function og_statistics_delete_record($gid) {
+  db_query("DELETE FROM {og_statistics} WHERE nid = %d", $gid);
+}
+
+/**
+ * views stuff.
+ */
+
+/**
+ * Implementation of hook_views_api().
+ */
+function og_statistics_views_api() {
+  return array(
+    'api' => 2,
+  );
+}
+