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, $node->nid, $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) && isset($node->og_initial_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); } } } break; case 'load' : if (og_is_group_type($node->type)) { $node->og_statistics = og_statistics_load($node->nid); } break; } } /** * 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, ); }