'admin/project/project-usage-settings', 'title' => t('Project usage settings'), 'callback' => 'drupal_get_form', 'callback arguments' => array('project_usage_settings_form'), 'description' => t('Configure how long usage data is retained.'), 'weight' => 1, ); $items[] = array( 'path' => 'project/usage', 'title' => t('Project usage'), 'callback' => 'project_usage_dispatch', 'access' => user_access('view project usage'), ); } return $items; } /** * Implementation of hook_help(). */ function project_usage_help($section) { switch ($section) { case 'project/usage': return '
'. t('This page summarizes the usage of all projects on @site_name. For each week beginning on the given date the figures show the number of sites that reported they are using (any version of) the project. Detailed usage information for each release of a project is available by clicking the project name.', array('@site_name' => variable_get('site_name', t('this site')))) .'
'; } } /** * Implementation of hook_perm(). */ function project_usage_perm() { return array( 'view project usage', ); } /** * Menu handler for project URLs. * * @param $key * Optional node id or project uri. NULL gets the overview page, project * nids and uris get the project usage page, release nids get the release * usage page, and everything else gets a not found. In addition, if a user * does not have permission to view the project or release node they've * requested, they get an access denied page. */ function project_usage_dispatch($key = NULL) { if (!isset($key)) { return project_usage_overview(); } // Load the node the user has requested. We want to only use // project_project_retrieve() if the $key parameter is not numeric because // project_project_retrieve() will only return a project_project node, and // we want to allow $node to also be a project_release node. if (is_numeric($key)) { $node = node_load($key); } else { $node = project_project_retrieve($key); } if (!empty($node->nid)) { // Make sure that the user has the permission to view this project/ // project_release node. if (node_access('view', $node)) { if ($node->type == 'project_project') { return project_usage_project_page($node); } if ($node->type == 'project_release') { return project_usage_release_page($node); } } else { return drupal_access_denied(); } } return drupal_not_found(); } /** * Display an overview of usage for all modules. */ function project_usage_overview() { drupal_add_css(drupal_get_path('module', 'project_usage') .'/project_usage.css'); drupal_set_title(t('Project usage overview')); // Grab an array of active week timestamps. $weeks = project_usage_get_active_weeks(); // In order to get the project usage data into a sortable table, we've gotta // write a pretty evil query: // // - We need to create a separate column for each week to allow sorting by // usage in any week (the tablesort_sql() requires that anything you can // sort on has a distinct field in the underlying query). However, some // weeks may not have any usage data, forcing us to use a LEFT JOIN, // rather than the more efficient INNER JOIN. // - The LEFT JOINs mean we have to limit the entries in {node} so that // we're not including things like forum posts, hence the WHERE IN below. // - Each project may have multiple records in {project_usage_week_project} // to track usage for API version. We need to SUM() them to get a total // count forcing us to GROUP BY. Sadly, I can't explain why we need // SUM(DISTINCT)... it just works(TM). $sql_elements = project_empty_query(); // Ignore the order_bys generated by project_empty_query(), and use // the tablesort instead. unset($sql_elements['order_bys']); $where_args = array(); $sql_elements['fields']['pieces'] = array('n.nid', 'n.title', 'pp.uri'); $sql_elements['from']['pieces'][] = '{node} n '; $sql_elements['joins']['pieces'][] = "INNER JOIN {project_projects} pp ON n.nid = pp.nid"; $sql_elements['wheres']['pieces'] = array('n.nid IN (SELECT nid FROM {project_usage_week_project}) AND n.status = %d'); $where_args[] = 1; // n.status = 1 $sql_elements['group_bys']['pieces'] = array('n.nid', 'n.title'); $headers = array(array('field' => 'n.title', 'data' => t('Project'))); $joins_args = array(); foreach ($weeks as $i => $week) { // Note that "{$i}" in these query snippets are used to add a week integer // to the table and field aliases so we can uniquely identify each column // for sorting purposes. These are not literals in the query, we need // these aliases to be unique via this PHP string operation before we even // build the query. $sql_elements['fields']['pieces'][] = "SUM(DISTINCT p{$i}.count) AS week{$i}"; $sql_elements['joins']['pieces'][] = "LEFT JOIN {project_usage_week_project} p{$i} ON n.nid = p{$i}.nid AND p{$i}.timestamp = %d"; $joins_args[] = $week; $header = array( 'field' => "week{$i}", 'data' => format_date($week, 'custom', variable_get('project_usage_date_short', PROJECT_USAGE_DATE_SHORT), 0), 'class' => 'project-usage-numbers' ); if ($i == 0) { $header['sort'] = 'desc'; } $headers[] = $header; } // Check for a cached page. The cache id needs to take into account the sort // column and order. $sort = tablesort_init($headers); $cid = 'overview:'. $sort['sql'] .':'. $sort['sort']; if (project_can_cache() && $cached = cache_get($cid, 'cache_project_usage')) { return $cached->data; } $args = array_merge($joins_args, $where_args); $result = db_query(project_build_query($sql_elements) . tablesort_sql($headers), $args); while ($line = db_fetch_array($result)) { $row = array(array('data' => l($line['title'], 'project/usage/'. $line['uri']))); foreach ($weeks as $i => $week) { $row[] = array('data' => number_format($line["week{$i}"]), 'class' => 'project-usage-numbers'); } $rows[] = $row; } $output = theme('table', $headers, $rows, array('id' => 'project-usage-all-projects')); // Cache the completed page. if (project_can_cache()) { cache_set($cid, 'cache_project_usage', $output, project_usage_cache_time()); } return $output; } /** * Display the usage history of a project node. */ function project_usage_project_page($node) { drupal_add_css(drupal_get_path('module', 'project_usage') .'/project_usage.css'); $breadcrumb = array( l(t('Usage'), 'project/usage'), ); project_project_set_breadcrumb(NULL, $breadcrumb); drupal_set_title(t('Usage statistics for %project', array('%project' => $node->title))); // In order to keep the database load down we need to cache these pages. // Because the release usage table is sortable, the cache id needs to take // into account the sort parameters. The easiest way to ensure we have valid // sorting parameters is to build the table headers and let the tablesort // functions do it. This means we end up doing most of the work to build the // page's second table early on. We might as well finish the job, then build // the other table and output them in the correct order. // Grab an array of active week timestamps. $weeks = project_usage_get_active_weeks(); $releases = project_release_get_releases($node, FALSE); // If there are no releases for this project, we can skip the rest // of this function. if (empty($releases)) { return theme('project_usage_project_page', $node); } // Build a table showing this week's usage for each release. In order to get // the release usage data into a sortable table, we need another evil query: // - We need to create a separate column for each week to allow sorting by // usage in any week (the tablesort_sql() requires that anything you can // sort on has a distinct field in the underlying query). However, some // weeks may not have any usage data, forcing us to use a LEFT JOIN, // rather than the more efficient INNER JOIN. // - We need to create a column for each week but some weeks may not have any // usage data, forcing us to use a LEFT JOIN, rather than the more // efficient INNER JOIN. // - The LEFT JOINs mean we have to limit the entries in {node} so that we're // not including things like forum posts, hence the WHERE IN below. $sql_elements = project_empty_query(); // Ignore the order_bys generated by project_empty_query(), and use // the tablesort instead. unset($sql_elements['order_bys']); $sql_elements['fields']['pieces'] = array('n.nid'); $sql_elements['from']['pieces'][] = '{node} n '; $sql_elements['wheres']['pieces'] = array('n.nid IN ('. implode(', ', array_fill(0, count($releases), '%d')) .') AND n.status = %d'); $where_args = array_keys($releases); $where_args[] = 1; // n.status = 1 $release_header = array(array('field' => 'n.title', 'data' => t('Release'), 'sort' => 'desc')); $joins_args = array(); foreach ($weeks as $i => $week) { // Note that "{$i}" in these query snippets are used to add a week integer // to the table and field aliases so we can uniquely identify each column // for sorting purposes. These are not literals in the query, we need // these aliases to be unique via this PHP string operation before we even // build the query. $sql_elements['fields']['pieces'][] = "p{$i}.count AS week{$i}"; $sql_elements['joins']['pieces'][] = "LEFT JOIN {project_usage_week_release} p{$i} ON n.nid = p{$i}.nid AND p{$i}.timestamp = %d"; $joins_args[] = $week; $release_header[] = array( 'field' => "week{$i}", 'data' => format_date($week, 'custom', variable_get('project_usage_date_short', PROJECT_USAGE_DATE_SHORT), 0), 'class' => 'project-usage-numbers', ); } // Check for a cached page. The cache id needs to take into account the sort // column and order. $sort = tablesort_init($release_header); $cid = 'project:'. $node->nid .':'. $sort['sql'] .':'. $sort['sort']; if ($cached = cache_get($cid, 'cache_project_usage')) { return $cached->data; } $args = array_merge($joins_args, $where_args); $result = db_query(project_build_query($sql_elements) . tablesort_sql($release_header), $args); $release_rows = array(); while ($line = db_fetch_array($result)) { $sum = 0; $row = array(array('data' => l($releases[$line['nid']], 'project/usage/'. $line['nid']))); foreach ($weeks as $i => $week) { $sum += $line["week{$i}"]; $row[] = array('data' => number_format($line["week{$i}"]), 'class' => 'project-usage-numbers'); } // Skip any release with no usage. if ($sum) { $release_rows[] = $row; } } // Build a table of the weekly usage data with a column for each API version. // Get an array of the weeks going back as far as we have data... $oldest = db_result(db_query("SELECT MIN(puwp.timestamp) FROM {project_usage_week_project} puwp WHERE puwp.nid = %d", $node->nid)); if ($oldest === NULL) { $weeks = array(); } else { $weeks = project_usage_get_weeks_since($oldest); // ...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); } // The number of columns varies depending on how many different API versions // are in use. Set up the header and a blank, template row, based on the // number of distinct terms in use. This *could* be done with LEFT JOINs, // but it ends up being a more expensive query and harder to read. $project_header = array(0 => array('data' => t('Week'))); $blank_row = array(0 => array('data' => '')); $result = db_query("SELECT DISTINCT td.tid, td.name FROM {project_usage_week_project} p INNER JOIN {term_data} td ON p.tid = td.tid WHERE p.nid = %d ORDER BY td.weight, td.name", $node->nid); while ($row = db_fetch_object($result)) { $project_header[$row->tid] = array('data' => check_plain($row->name), 'class' => 'project-usage-numbers'); $blank_row[$row->tid] = array('data' => 0, 'class' => 'project-usage-numbers'); } // Now create a blank table with a row for each week and formatted date in // the first column... $project_rows = array(); foreach ($weeks as $week) { $project_rows[$week] = $blank_row; $project_rows[$week][0]['data'] = format_date($week, 'custom', variable_get('project_usage_date_long', PROJECT_USAGE_DATE_LONG), 0); } // ...then fill it in with our data. $result = db_query("SELECT timestamp, tid, count FROM {project_usage_week_project} WHERE nid = %d", $node->nid); while ($row = db_fetch_object($result)) { $project_rows[$row->timestamp][$row->tid]['data'] = number_format($row->count); } $output = theme('project_usage_project_page', $node, $release_header, $release_rows, $project_header, $project_rows); // Cache the completed page. if (project_can_cache()) { cache_set($cid, 'cache_project_usage', $output, project_usage_cache_time()); } return $output; } /** * Display the usage history of a release node. */ function project_usage_release_page($node) { drupal_add_css(drupal_get_path('module', 'project_usage') .'/project_usage.css'); $project = node_load($node->pid); $breadcrumb = array( l(t('Usage'), 'project/usage'), l($project->title, 'project/usage/'. $project->nid), ); project_project_set_breadcrumb(NULL, $breadcrumb); drupal_set_title(t('Usage statistics for %release', array('%release' => $node->title))); // Check for a cached page. $cid = "release:{$node->nid}"; if (project_can_cache() && $cached = cache_get($cid, 'cache_project_usage')) { return $cached->data; } // Table displaying the usage back through time. $header = array( array('data' => t('Week starting')), array('data' => t('Count'), 'class' => 'project-usage-numbers'), ); $rows = array(); $query = db_query("SELECT timestamp, count FROM {project_usage_week_release} WHERE nid = %d ORDER BY timestamp DESC", $node->nid); while ($row = db_fetch_object($query)) { $rows[] = array( array('data' => format_date($row->timestamp, 'custom', variable_get('project_usage_date_long', PROJECT_USAGE_DATE_LONG), 0)), array('data' => number_format($row->count), 'class' => 'project-usage-numbers'), ); } $output = theme('project_usage_release_page', $project, $node, $header, $rows); // Cache the completed page. if (project_can_cache()) { cache_set($cid, 'cache_project_usage', $output, project_usage_cache_time()); } return $output; } /** * Module settings form. */ function project_usage_settings_form() { $times = array( 3 * PROJECT_USAGE_YEAR, 2 * PROJECT_USAGE_YEAR, 1 * PROJECT_USAGE_YEAR, 26 * PROJECT_USAGE_WEEK, 12 * PROJECT_USAGE_WEEK, 8 * PROJECT_USAGE_WEEK, 4 * PROJECT_USAGE_WEEK, ); $age_options = drupal_map_assoc($times, 'format_interval'); $form['project_usage_life_daily'] = array( '#type' => 'select', '#title' => t('Daily usage data lifespan'), '#default_value' => variable_get('project_usage_life_daily', 4 * PROJECT_USAGE_WEEK), '#options' => $age_options, '#description' => t('Discard the daily usage data after this amount of time has passed.'), ); $form['project_usage_life_weekly_project'] = array( '#type' => 'select', '#title' => t('Weekly project data lifespan'), '#default_value' => variable_get('project_usage_life_weekly_project', PROJECT_USAGE_YEAR), '#options' => $age_options, '#description' => t('Discard the weekly usage for project nodes after this amount of time has passed.'), ); $form['project_usage_life_weekly_release'] = array( '#type' => 'select', '#title' => t('Weekly release data lifespan'), '#default_value' => variable_get('project_usage_life_weekly_release', 26 * PROJECT_USAGE_WEEK), '#options' => $age_options, '#description' => t('Discard the weekly usage for project nodes after this amount of time has passed.'), ); return system_settings_form($form); } /** * getdate() with timezone adjustment. * * PHP's getdate() is affected by the server's timezone. We need to cancel it * out so everything is GMT. * * @param $timestamp * An optional, integer UNIX timestamp. * @return * An array with results identical to PHP's getdate(). */ function project_usage_gmgetdate($timestamp = NULL) { $timestamp = isset($timestamp) ? $timestamp : time(); $gmt_offset = (int) date('Z', $timestamp); return getdate($timestamp - $gmt_offset); } /** * Compute a timestamp for the beginning of a day N days ago. * * @param $time * Mixed, either a GMT timestamp or an array returned by * project_usage_gmgetdate(). * @param $days_ago * An integer specifying a number of days previous. A value of 0 indicates * the current day. * * @return * GMT UNIX timestamp. */ function project_usage_daily_timestamp($time = NULL, $days_ago = 0) { $time_parts = is_array($time) ? $time : project_usage_gmgetdate($time); $day = $time_parts['mday'] - $days_ago; return gmmktime(0, 0, 0, $time_parts['mon'], $day, $time_parts['year']); } /** * Compute a timestamp for the beginning of a week N weeks ago. * * @param $time * Mixed. Integer timestamp or an array returned by project_usage_gmgetdate(). * @param $weeks_ago * An integer specifying a number of weeks previous. A value of 0 indicates * the current week. * * @return * GMT UNIX timestamp. */ function project_usage_weekly_timestamp($time = NULL, $weeks_ago = 0) { $time_parts = is_array($time) ? $time : project_usage_gmgetdate($time); $day = $time_parts['mday'] - $time_parts['wday'] + (7 * $weeks_ago); return gmmktime(0, 0, 0, $time_parts['mon'], $day, $time_parts['year']); } /** * Build an array of timestamps for the beginning (midnight Sunday) for each * week since a given timestamp. * * @param $timestamp * UNIX timestamp. The first returned timestamp will be the beginning of the * week with this time in it. * @return * An array of GMT timestamps sorted in ascending order. The first value is * is the week containing $timestamp. Each subsequent value is the timestamp * for the next week. The final value is the beginning of the current week. */ function project_usage_get_weeks_since($timestamp) { $times = array(); // First, compute the start of the current week so we know when to stop... $this_week = project_usage_weekly_timestamp(); // ...then compute all the weeks up to that. $parts = project_usage_gmgetdate($timestamp); $i = 0; do { $times[$i] = project_usage_weekly_timestamp($parts, $i); } while ($times[$i++] < $this_week); return $times; } /** * Return an array of the most recent weeks for which we have data. * * @return * An array of UNIX timestamps sorted newest to oldest. Will not include * the current week. */ function project_usage_get_active_weeks($reset = FALSE) { $weeks = variable_get('project_usage_active_weeks', array()); if ($reset || empty($weeks)) { $count = variable_get('project_usage_show_weeks', PROJECT_USAGE_SHOW_WEEKS); $query = db_query_range("SELECT DISTINCT(timestamp) FROM {project_usage_week_project} ORDER BY timestamp DESC", array(), 0, $count); $weeks = array(); while ($week = db_fetch_object($query)) { $weeks[] = $week->timestamp; } variable_set('project_usage_active_weeks', $weeks); } return $weeks; } /** * Implementation of hook_simpletest(). */ function project_usage_simpletest() { $dir = drupal_get_path('module', 'project_usage'). '/tests'; $tests = file_scan_directory($dir, '\.test$'); return array_keys($tests); } /** * Implementation of hook_devel_caches(). * * Lets the devel module know about our cache table so it can clear it. */ function project_usage_devel_caches() { return array('cache_project_usage'); } /** * Sets the expiry timestamp for cached project usage pages. * * Default is 24 hours. * * @return The UNIX timestamp to expire the page at. */ function project_usage_cache_time() { return time() + variable_get('project_usage_cache_length', 86400); } /** * Theme the output of project/usage/'. t('There is no usage information for any release of this project.') .'
'; return $output; } $output .= '' . t('There is no usage information for this release.') . '
'; return $output; } $output .= theme('project_usage_chart_by_release', t('Weekly @release usage', array('@release' => $release->title)), $header, $rows); $output .= theme('table', $header, $rows, array('id' => 'project-usage-release')); return $output; } /** * Create list of links at the top of a usage statistics page. * * @param $project * A fully loaded $node object for a project. * @param $release * If the current statistics page is for a release, the fully loaded $node * object for that release. * * @return * Themed HTML of a list of links for the top of a statistics page. */ function theme_project_usage_header_links($project, $release = NULL) { $links = array(); $links[] = l(t('%project_name project page', array('%project_name' => $project->title)), 'node/'. $project->nid, array(), NULL, NULL, FALSE, TRUE); if (!empty($release)) { $links[] = l(t('%release_name release page', array('%release_name' => $release->title)), 'node/' . $release->nid, array(), NULL, NULL, FALSE, TRUE); $links[] = l(t('All %project_name usage statistics', array('%project_name' => $project->title)), 'project/usage/' . $project->uri, array(), NULL, NULL, FALSE, TRUE); } $links[] = l(t('Usage statistics for all projects'), 'project/usage'); return theme('item_list', $links); } /** * Implementation of hook_project_page_link_alter(). */ function project_usage_project_page_link_alter($node, &$links) { if (user_access('view project usage') && $node->releases) { $links['resources']['links']['project_usage'] = l(t('View usage statistics'), 'project/usage/' . $node->uri); } } /** * Convert the usage table data into a Google Chart image. * * First column should be the weeks, each subsequent column should be a data * series. * * @param $header * A table header for the weekly usage table. * @param $rows * Table rows for the weekly usage table. * @return * An HTML IMG element, or empty string if there is an error such as * insufficent data. */ function theme_project_usage_chart_by_release($title, $header, $rows) { // Make sure we have enough data to make a useful chart. if (count($rows) < 2 || count($header) < 2) { return ''; } // Reverse the order of the rows so it's oldest to newest. $rows = array_reverse($rows); // Pull the API versions from the table header for use as a legend. Since the // table is keyed strangely, make note of which tid is in which order so we // can efficiently iterate over the columns. $legend = array(); $mapping = array(); foreach ($header as $tid => $cell) { $legend[] = $cell['data']; $mapping[] = $tid; } // Drop the date column from the legend and mapping since it's the other axis. unset($legend[0]); unset($mapping[0]); // Rotate the table so each series is in a row in the array and grab the // dates for use as axis labels. $series = array(); $date_axis = array(); foreach (array_values($rows) as $i => $row) { $date_axis[$i] = $row[0]['data']; foreach ($mapping as $j => $tid) { // FIXME: The table values have commas in them from number_format(). We // need to remove them because we'll use commas to separate the values // in the URL string. It might be better to pass in clean number values // and format them here rather than have to uncook them. $series[$j][$i] = (int) str_replace(',', '', $row[$tid]['data']); } } // Now convert the series into strings with the data. Along the way figure // out the range of data. $min = $max = 0; $data = array(); foreach ($series as $s) { $data[] = implode(',', $s); $max = max($max, max($s)); } // Round the max up to the next decimal place (3->10, 19->20, 8703->9000) so // that the labels have round numbers and the entire range is visible. We're // forced to build the number as a string because PHP's round() function // can round down (3->0, not 3->10) and it returns floats that loose // precision (causing 90,000 to display as 89,999.99). We pull off the first // digit, add 1 to that, and then pad the rest with zeros. $zeros = strlen($max) - 1; $max = ($zeros > 0) ? (substr($max, 0, 1) + 1 . str_repeat('0', $zeros)) : 10; $value_axis = range($min, $max, '1'. str_repeat('0', $zeros)); // Might need more colors than this. $colors = array('EDAA00', '0062A0', 'A17300', 'ED8200', '38B4BA', '215D6E'); // The key values in this array are dictated by the Google Charts API: // http://code.google.com/apis/chart/ $args = array( // Chart title. 'chtt' => check_plain($title), // Dimensions as width x height in pixels. 'chs' => '600x200', // Chart type 'cht' => 'lc', // Pick some colors. 'chco' => implode(',', array_slice($colors, 0, count($series))), 'chd' => 't:'. implode('|', $data), // Set the range of the chart 'chds' => implode(',', array_fill(0, count($series), $min .','. $max)), // Legend is the header titles after excluding the date. 'chdl' => implode('|', $legend), ); project_usage_chart_axis_labels($args, array( 'x' => project_usage_chart_label_subset($date_axis, 5), 'y' => array_map('number_format', project_usage_chart_label_subset($value_axis, 5)), )); return theme('project_usage_chart', $args); } /** * Extract a subset of labels at regular intervals from a set. * * @param $labels * Array of values to choose from. * @param $n * Number of segments in the set. This number should divide $labels with no * remander. */ function project_usage_chart_label_subset($labels, $n) { $subset = array(); $count = count($labels) - 1; // We can't give them back more labels that we're given. $n = min($count, $n - 1); for ($i = 0; $i <= $n; $i++) { $subset[] = $labels[(int) ($count * ($i / $n))]; } return $subset; } /** * Add axis labels to the chart arguments. * * @param $args * The array where the chart is being built. * @param $labels * Array keyed by axis (x, t, y, r) the value is an array of labels for that * axis. * @see http://code.google.com/apis/chart/labels.html#multiple_axes_labels */ function project_usage_chart_axis_labels(&$args, $labels) { $keys = array_keys($labels); $args['chxt'] = implode(',', $keys); $l = array(); foreach ($keys as $i => $key) { $l[$i] = $i .':|' . implode('|', $labels[$key]); } $args['chxl'] = implode('|', $l); } /** * Convert the array of Google Chart paramters into an image URL. * * @param $args * Array of key, value pairs to turn into a Google Charts image. * @return * HTML image element. */ function theme_project_usage_chart($args) { $params = array(); foreach ($args as $key => $value) { $params[] = $key .'='. $value; } // If the chart has a title use that for the image's alt and title values. $title = empty($args['chtt']) ? '' : $args['chtt']; return theme('image', 'http://chart.apis.google.com/chart?'. implode('&', $params), $title, $title, NULL, FALSE); }