Mercurial > defr > drupal > og_statistics
changeset 3:13824d66b299 tip
Application du patch sur la 1.0-rc2
author | Franck Deroche <franck@defr.org> |
---|---|
date | Tue, 24 Nov 2009 14:30:14 +0100 |
parents | 48f07e7acaca (diff) 0aae3e2e6309 (current diff) |
children | |
files | og_statistics.module |
diffstat | 7 files changed, 374 insertions(+), 29 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/og_statistics.admin.inc Tue Nov 24 14:30:14 2009 +0100 @@ -0,0 +1,45 @@ +<?php +// $Id: og_statistics.admin.inc,v 1.1.2.1 2009/09/08 20:42:56 dereine Exp $ +/** + * @file + * Admin settings for og_statistics module. + */ + +function og_statistics_settings() { + $form['rebuild'] = array( + '#type' => 'submit', + '#value' => t('Rebuild og_statistics') + ); + + return $form; +} + +function og_statistics_settings_submit(&$form, &$form_state) { + // get all group nodes + $types = og_get_types('group'); + $placeholders = db_placeholders($types, 'varchar'); + $result = db_query("SELECT nid FROM {node} WHERE type IN($placeholders)", $types); + while ($item = db_fetch_object($result)) { + $nodes[$item->nid] = $item->nid; + } + $counter = 0; + foreach ($nodes as $nid) { + $counter ++; + $array[] = $nid; + if (($counter % 20) == 1) { + $operations[] = array('og_statistcs_recalc', array($array)); + $array = array(); + } + } + $operations[] = array('og_statistcs_recalc', array($array)); + // Execute the batch progress + $batch = array( + 'operations' => $operations, + 'title' => 'Generating statistics', + 'init_message' => 'Loading group nodes', + 'error_message' => 'An unrecoverable error has occurred. You can find the error message below. It is advised to copy it to the clipboard for reference.', + 'finished' => 'og_statistcs_settings_finished', + ); + batch_set($batch); + batch_process('admin/settings/og_statistics'); +}
--- a/og_statistics.faceted_search.inc Tue Nov 24 14:26:41 2009 +0100 +++ b/og_statistics.faceted_search.inc Tue Nov 24 14:30:14 2009 +0100 @@ -1,9 +1,12 @@ <?php -// $Id: og_statistics.faceted_search.inc,v 1.2 2009/05/29 16:46:25 dereine Exp $ +// $Id: og_statistics.faceted_search.inc,v 1.2.2.1 2009/08/12 19:56:41 dereine Exp $ /** * @file * Integration of og_statistics into faceted_search. + * + * @TODO + * Add every statistic field for the sort availible. */ /**
--- a/og_statistics.info Tue Nov 24 14:26:41 2009 +0100 +++ b/og_statistics.info Tue Nov 24 14:30:14 2009 +0100 @@ -1,4 +1,4 @@ -;$Id; +; $Id: og_statistics.info,v 1.2.2.2 2009/08/12 21:06:27 dereine Exp $ name = Organic groups statistics description = "Enable statistics for organic groups." package = "Organic groups" @@ -6,9 +6,9 @@ dependencies[] = og_views core = 6.x -; Information added by drupal.org packaging script on 2009-06-18 -version = "6.x-1.0-beta3" +; Information added by drupal.org packaging script on 2009-09-08 +version = "6.x-1.0-rc2" core = "6.x" project = "og_statistics" -datestamp = "1245344747" +datestamp = "1252442748"
--- a/og_statistics.install Tue Nov 24 14:26:41 2009 +0100 +++ b/og_statistics.install Tue Nov 24 14:30:14 2009 +0100 @@ -1,5 +1,5 @@ <?php -// $Id: og_statistics.install,v 1.2 2009/05/29 16:46:25 dereine Exp $ +// $Id: og_statistics.install,v 1.2.2.1 2009/08/12 19:53:28 dereine Exp $ /** * @file @@ -65,6 +65,48 @@ 'not null' => FALSE, 'default' => 0, ), + 'last_comment_uid' => array( + 'description' => 'Last {users}.uid to post a comment in a group.', + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => FALSE, + 'default' => 0, + ), + 'last_comment_nid' => array( + 'description' => 'Node the last comment was posted to in a group.', + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => FALSE, + 'default' => 0, + ), + 'last_comment_cid' => array( + 'description' => '{comments}.cid of the last comment in a group.', + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => FALSE, + 'default' => 0, + ), + 'last_node_nid' => array( + 'description' => 'Last {node}.nid posted to a group.', + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => FALSE, + 'default' => 0, + ), + 'last_node_uid' => array( + 'description' => '{users}.uid of the last node posted to a group.', + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => FALSE, + 'default' => 0, + ), + 'last_member_uid' => array( + 'description' => '{users}.uid of the last user to join a group.', + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => FALSE, + 'default' => 0, + ), ), 'primary key' => array('nid'), ); @@ -78,3 +120,32 @@ function og_statistics_uninstall() { drupal_uninstall_schema('og_statistics'); } + +/** + * Adds more verbose og statistics. + */ +function og_statistics_update_6001() { + $ret = array(); + $schema = drupal_get_schema_unprocessed('og_statistics'); + $table_definition = $schema['og_statistics']; + // Add detailed columns for last comment post. + foreach (array('last_comment_uid', 'last_comment_nid', 'last_comment_cid') as $column) { + if (!db_column_exists('og_statistics', $column)) { + db_add_field($ret, 'og_statistics', $column, $table_definition['fields'][$column]); + } + } + // Add detailed columns for last node post. + foreach (array('last_node_uid', 'last_node_nid') as $column) { + if (!db_column_exists('og_statistics', $column)) { + db_add_field($ret, 'og_statistics', $column, $table_definition['fields'][$column]); + } + } + // Add detailed columns for last user to join. + foreach (array('last_member_uid') as $column) { + if (!db_column_exists('og_statistics', $column)) { + db_add_field($ret, 'og_statistics', $column, $table_definition['fields'][$column]); + } + } + // Rebuild schema. + drupal_get_schema('og_statistics', TRUE); +} \ No newline at end of file
--- a/og_statistics.module Tue Nov 24 14:26:41 2009 +0100 +++ b/og_statistics.module Tue Nov 24 14:30:14 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. */
--- a/og_statistics.test Tue Nov 24 14:26:41 2009 +0100 +++ b/og_statistics.test Tue Nov 24 14:30:14 2009 +0100 @@ -1,5 +1,5 @@ <?php -// $Id: og_statistics.test,v 1.2.2.2 2009/06/05 22:38:12 dereine Exp $ +// $Id: og_statistics.test,v 1.2.2.6 2009/09/07 19:22:31 dereine Exp $ /** * Test file for og_statitics. @@ -11,7 +11,7 @@ require_once drupal_get_path('module', 'og') .'/tests/og_testcase.php'; class OgStatisticsTestCase extends OgTestCase { - function getInfo() { + public static function getInfo() { return array( 'name' => t('Organic groups statistics'), 'description' => t('tests statistics of og_statistics module'), @@ -20,7 +20,7 @@ } function setUp() { - parent::setUp('node', 'user', 'comment', 'views', 'og', 'og_views', 'og_statistics', 'og_access'); + parent::setUp('node', 'user', 'comment', 'views', 'og', 'og_views', 'og_statistics', 'og_access', 'devel'); // Create a user with admin permissions. $web_admin = $this->drupalCreateUser(array('administer nodes', 'administer content types', 'access administration pages', 'administer site configuration', 'administer organic groups')); @@ -48,7 +48,6 @@ $members = $empty = FALSE; - $this->assertFalse($empty, print_r($stat, TRUE)); foreach ($stat as $key => $item) { $empty = $empty || $item == TRUE; } @@ -82,6 +81,8 @@ $this->assertTrue($count_one_up, 'A node post in a group, counts the counter 1 up'); $this->assertEqual($node->created, $after['last_node_timestamp'], 'A node post in a group, sets the last node timestamp'); + $this->assertEqual($node->nid, $after['last_node_nid'], 'A node post in a group sets the last node nid'); + $this->assertEqual($node->uid, $after['last_node_uid'], 'A node post in a group sets the last node uid'); // remove the post node_delete($nid); @@ -92,8 +93,15 @@ $this->assertTrue($count_down, 'A node poast in a group was deleted, counts the counter 1 down'); } + function drupalCreateComment($edit) { + if ($cid = comment_save($edit)) { + return $cid; + } + } + // Tests comments hooks statistic function TestOgStatisticsComment() { + global $user; // Create a group node content type. $og_group_type = $this->drupalCreateContentType(); variable_set('og_content_type_usage_'. $og_group_type->name, 'group'); @@ -111,19 +119,49 @@ // add a test post $nids[] = $nid = $this->addOgPost($og_post_type->name, array($gid)); + $node = node_load($nid); // adds some comments $edit = array(); $edit['subject'] = $this->randomName(10); $edit['comment'] = $this->randomName(50); $edit['nid'] = $nid; - $cid = comment_save($edit); + $edit['format'] = FILTER_FORMAT_DEFAULT; + $edit['node'] = $node; + $edit['uid'] = $user->uid; + $edit['pid'] = NULL; + $edit['cid'] = NULL; + $cid = $this->drupalCreateComment($edit); $comment = _comment_load($cid); $after = og_statistics_load($gid); $count_up = ($after['comments_count'] - $before['comments_count']) == 1; $this->assertTrue($count_up, 'new comment to one group post, counts up the comment counter'); $this->assertEqual($comment->timestamp, $after['last_comment_timestamp'], 'new comment to one group post, counts up the comment counter'); + $this->assertEqual($comment->cid, $after['last_comment_cid'], 'New comment to one group post sets the last comment cid.'); + $this->assertEqual($comment->nid, $after['last_comment_nid'], 'New comment to one group post sets the last comment nid.'); + $this->assertEqual($comment->uid, $after['last_comment_uid'], 'New comment to one group post sets the last comment uid.'); + + // add another comment. + + $edit = array(); + $edit['subject'] = $this->randomName(10); + $edit['comment'] = $this->randomName(50); + $edit['nid'] = $nid; + $edit['format'] = FILTER_FORMAT_DEFAULT; + $edit['node'] = $node; + $edit['uid'] = $user->uid; + $edit['pid'] = NULL; + $edit['cid'] = NULL; + $cid = $this->drupalCreateComment($edit); + + // Remove the node and check whether comment count is updated + // reload the node, to update the stats. + $node = node_load($node, NULL, TRUE); + + node_delete($nid); + $stat = og_statistics_load($gid); + $this->assertEqual($stat['comments_count'], 0, 'all comments were deleted'); } function TestOgStatisticsOg() { @@ -153,6 +191,32 @@ $this->assertTrue($count_up, 'New subscription to a group adds the counter 1 up'); $this->assertEqual($time, $after['last_member_timestamp'], 'New subscription to a group adds the counter 1 up'); + $this->assertEqual($account->uid, $after['last_member_uid'], 'New subscription to a group sets the last member uid.'); + } + + function TestOgStatisticsDelete() { + // Create a group node content type. + $og_group_type = $this->drupalCreateContentType(); + variable_set('og_content_type_usage_'. $og_group_type->name, 'group'); + + // Create a group post content type. + $og_post_type = $this->drupalCreateContentType(); + variable_set('og_content_type_usage_'. $og_post_type->name, 'group_post_standard'); + + // Rebuild the menu so the new content types will appear in the menu. + menu_rebuild(); + + $gid = $this->addOgGroup($og_group_type->name); + // create a test group + $gid = $this->addOgGroup($og_group_type->name); + $before = og_statistics_load($gid); + + // Delete the node, the stat-record should be deleted too. + $node = node_load($gid); + node_delete($node->nid); + $after = og_statistics_load($gid); + + $this->assertTrue(empty($after), 'Og statistics record get deleted'); } }
--- a/og_statistics.views.inc Tue Nov 24 14:26:41 2009 +0100 +++ b/og_statistics.views.inc Tue Nov 24 14:30:14 2009 +0100 @@ -1,5 +1,5 @@ <?php -// $Id: og_statistics.views.inc,v 1.2.2.1 2009/06/18 17:02:45 dereine Exp $ +// $Id: og_statistics.views.inc,v 1.2.2.2 2009/08/12 19:53:28 dereine Exp $ /** * @file @@ -90,6 +90,76 @@ 'handler' => 'views_handler_sort_date', ), ); + + // Add relationship to node table for last node posted. + $data['og_statistics']['last_node_nid'] = array( + 'title' => 'Group Statistic: Last Node posted', + 'help' => 'Create a relationship to for the last node posted', + 'relationship' => array( + 'handler' => 'views_handler_relationship', + 'base' => 'node', + 'field' => 'nid', + 'label' => t('node'), + ), + ); + // Add relationship to user table for last node posted. + $data['og_statistics']['last_node_uid'] = array( + 'title' => 'Group Statistic: User that created the last node posted', + 'help' => 'Create a relationship to the user that last posted a node.', + 'relationship' => array( + 'handler' => 'views_handler_relationship', + 'base' => 'users', + 'field' => 'uid', + 'label' => t('user'), + ), + ); + + // Add relationship to node table for last comment posted. + $data['og_statistics']['last_comment_nid'] = array( + 'title' => 'Group Statistic: Node the last comment was posted to', + 'help' => 'Create a relationship to the last comment posted', + 'relationship' => array( + 'handler' => 'views_handler_relationship', + 'base' => 'node', + 'field' => 'nid', + 'label' => t('node'), + ), + ); + // Add relationship to user table for last comment posted. + $data['og_statistics']['last_comment_uid'] = array( + 'title' => 'Group Statistic: User that posted the last comment', + 'help' => 'Create a relationship to the user that last posted a comment.', + 'relationship' => array( + 'handler' => 'views_handler_relationship', + 'base' => 'users', + 'field' => 'uid', + 'label' => t('user'), + ), + ); + // Add relationship to the comment table for the last comment posted. + $data['og_statistics']['last_comment_cid'] = array( + 'title' => 'Group Statistic: Comment last posted', + 'help' => 'Create a relationship to the last comment posted', + 'relationship' => array( + 'handler' => 'views_handler_relationship', + 'base' => 'comments', + 'field' => 'cid', + 'label' => t('comment'), + ), + ); + + // Add relationship to the users table for the last member. + $data['og_statistics']['last_member_uid'] = array( + 'title' => 'Group Statistic: Last member to join', + 'help' => 'Create a relationship to the last user to join', + 'relationship' => array( + 'handler' => 'views_handler_relationship', + 'base' => 'users', + 'field' => 'uid', + 'label' => t('user'), + ), + ); + return $data; }