? _project.inc_ ? _project.module_ ? release/history ? usage/test.patch Index: usage/project_usage.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/project/usage/project_usage.module,v retrieving revision 1.4 diff -u -r1.4 project_usage.module --- usage/project_usage.module 14 Aug 2007 00:51:53 -0000 1.4 +++ usage/project_usage.module 14 Aug 2007 22:09:45 -0000 @@ -29,6 +29,10 @@ // Number of seconds in a year. define('PROJECT_USAGE_YEAR', PROJECT_USAGE_DAY * 365); +// Date format for month and day. +define('PROJECT_USAGE_DATE_SHORT', 'M j'); +define('PROJECT_USAGE_DATE_LONG', 'F jS'); + /** * Implementation of hook_menu(). */ @@ -43,11 +47,195 @@ 'description' => t('Configure how long usage data is retained.'), 'weight' => 1, ); + $items[] = array( + 'path' => 'project_usage', + 'title' => t('Project usage'), + 'callback' => 'project_usage_overview', + 'access' => user_access('view project usage'), + ); + $items[] = array( + 'path' => 'project_usage/project', + 'title' => t('Project usage'), + 'callback' => 'project_usage_project_page', + ); + $items[] = array( + 'path' => 'project_usage/release', + 'title' => t('Relase usage'), + 'callback' => 'project_usage_release_page', + ); } return $items; } /** + * Implementation of hook_help(). + */ +function project_usage_help($section) { + switch ($section) { + case 'project_usage': + return t('The following is a summary of the the usage information for the projects on this site. The count is the total number of sites using any version of the project. Only sites that have opted to allow usage information to be tracked are included.'); + + case 'project_usage/project': + return t('The following is the usage counts for each API version of the project. Only sites that have opted to allow usage information to be tracked are included.'); + + case 'project_usage/release': + return t('The following is the usage counts for each project release. Only sites that have opted to allow usage information to be tracked are included.'); + + case 'admin/modules#description': + return t('Collects and processes usage information about projects and releases.'); + } +} + +/** + * Implementation of hook_perm(). + */ +function project_usage_perm() { + $perms = array( + 'view project usage', + ); + return $perms; +} + +/** + * Display and overview of usage for all modules. + */ +function project_usage_overview() { + drupal_set_title(t('Project usage overview')); + + $week_count = 6; + $weeks = project_usage_get_weeks_since(time() - ($week_count * PROJECT_USAGE_WEEK)); + + // Ignore the current week, since we won't have usage data for that and + // reverse the order so it's newest to oldest. + array_pop($weeks); + $weeks = array_reverse($weeks); + + // Query builder join a copy of {project_usage_week_project} for each week + // to the {node} table. + $header = array(array('data' => t('Project'), 'field' => 'title', 'sort' => 'asc')); + $fields = array('n.nid', 'n.title'); + $joins = array(); + foreach ($weeks as $i => $week) { + $header[] = array('data' => format_date($week, 'custom', PROJECT_USAGE_DATE_SHORT), 'field' => "week{$i}"); + $fields[] = "p{$i}.count AS week{$i}"; + $joins[] = "LEFT JOIN {project_usage_week_project} p{$i} ON n.nid = p{$i}.nid AND p{$i}.timestamp = $week"; + } + $result = pager_query('SELECT '. implode(', ', $fields) .' FROM {node} n ' + . implode(' ', $joins) .' WHERE n.nid IN (SELECT nid FROM {project_usage_week_project})' + . tablesort_sql($header), 30, 0); + + // Take the rows and cast any NULL week counts to zeros. + while ($line = db_fetch_array($result)) { + $row = array(l($line['title'], 'project_usage/project/'. $line['nid'])); + for ($i = 0; $i < $week_count; $i++) { + $row[] = (int) $line["week{$i}"]; + } + $rows[] = $row; + } + + return theme('table', $header, $rows) . theme('pager', NULL, 30, 0); +} + +/** + * Display the usage history of a project node. + */ +function project_usage_project_page($nid = NULL) { + $node = node_load((int) $nid); + if (!$node->nid) { + drupal_not_found(); + return; + } + + drupal_set_title(check_plain($node->title)); + + // Build a table of the weekly usage data with a column for each API version. + $output .= '