[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/project/usage/ -> project_usage.module (source)

   1  <?php
   2  // $Id: project_usage.module,v 1.33 2009/04/11 22:42:34 dww Exp $
   3  
   4  /**
   5   * @file
   6   *
   7   * This module provides logging of the requests sent by the
   8   * update_status.module (contrib in 5.x) and update.module (core in 6.x) to the
   9   * project_release.module on updates.drupal.org. The
  10   * release/project-release-serve-history.php script inserts data into the
  11   * {project_usage_raw} table created by this module.
  12   *
  13   * On a daily basis the usage data is matched to project and release nodes
  14   * and moved into the {project_usage_day} table. On a weekly basis the daily
  15   * usage data is tallied and stored in the {project_usage_week} table.
  16   *
  17   * This data is then used to compute live usage statistics about all projects
  18   * hosted on drupal.org. In theory, another site could setup
  19   * update_status.module-style checking to their own project.module-based
  20   * server, in which case, they might want to enable this module. Otherwise,
  21   * sites should just leave this disabled.
  22   */
  23  
  24  // Number of seconds in a day.
  25  define('PROJECT_USAGE_DAY', 60 * 60 * 24);
  26  // Number of seconds in a week.
  27  define('PROJECT_USAGE_WEEK', PROJECT_USAGE_DAY * 7);
  28  // Number of seconds in a year.
  29  define('PROJECT_USAGE_YEAR', PROJECT_USAGE_DAY * 365);
  30  
  31  /**
  32   * Date formats for month and day. We define our own rather than using core's
  33   * 'date_format_short' and 'date_format_long' variables because our timestamps
  34   * don't have hour or minute resolution so displaying that would be confusing
  35   * and take up extra space.
  36   */
  37  define('PROJECT_USAGE_DATE_LONG', 'F jS');
  38  define('PROJECT_USAGE_DATE_SHORT','M j');
  39  
  40  // How many weeks should be shown in the usage pages?
  41  define('PROJECT_USAGE_SHOW_WEEKS', 6);
  42  
  43  /**
  44   * Implementation of hook_menu().
  45   */
  46  function project_usage_menu() {
  47    $items['admin/project/project-usage-settings'] = array(
  48      'title' => 'Project usage settings',
  49      'page callback' => 'drupal_get_form',
  50      'page arguments' => array('project_usage_settings_form'),
  51      'access arguments' => array('administer projects'),
  52      'description' => 'Configure how long usage data is retained.',
  53      'weight' => 1,
  54      'file' => 'includes/admin.inc',
  55    );
  56    $items['project/usage'] = array(
  57      'title' => 'Project usage overview',
  58      'page callback' => 'project_usage_overview',
  59      'access arguments' => array('view project usage'),
  60      'file' => 'includes/pages.inc',
  61    );
  62    $items['project/usage/%'] = array(
  63      'title' => 'Project usage',
  64      'page callback' => 'project_usage_dispatch',
  65      'page arguments' => array(2),
  66      'access arguments' => array('view project usage'),
  67      'file' => 'includes/pages.inc',
  68    );
  69    return $items;
  70  }
  71  
  72  /**
  73   * Implementation of hook_theme().
  74   */
  75  function project_usage_theme() {
  76    $path = drupal_get_path('module', 'project_usage') .'/includes';
  77    return array(
  78      'project_usage_chart' => array(
  79        'arguments' => array('args' => NULL),
  80        'file' => 'pages.inc',
  81        'path' => $path,
  82      ),
  83      'project_usage_chart_by_release' => array(
  84        'arguments' => array(
  85          'title' => NULL,
  86          'header' => NULL,
  87          'rows' => NULL,
  88        ),
  89        'file' => 'pages.inc',
  90        'path' => $path,
  91      ),
  92      'project_usage_header_links' => array(
  93        'arguments' => array(
  94          'project' => NULL,
  95          'release' => NULL,
  96        ),
  97        'file' => 'pages.inc',
  98        'path' => $path,
  99      ),
 100      'project_usage_project_page' => array(
 101        'arguments' => array(
 102          'project' => NULL,
 103          'release_header' => NULL,
 104          'release_rows' => NULL,
 105          'project_header' => NULL,
 106          'project_rows' => NULL,
 107        ),
 108        'file' => 'pages.inc',
 109        'path' => $path,
 110      ),
 111      'project_usage_release_page' => array(
 112        'arguments' => array(
 113          'project' => NULL,
 114          'release' => NULL,
 115          'header' => NULL,
 116          'rows' => NULL,
 117        ),
 118        'file' => 'pages.inc',
 119        'path' => $path,
 120      ),
 121    );
 122  }
 123  
 124  /**
 125   * Implementation of hook_help().
 126   */
 127  function project_usage_help($path, $arg) {
 128    switch ($path) {
 129      case 'project/usage':
 130      case 'project/usage/%':
 131        module_load_include('inc', 'project_usage', 'includes/pages');
 132        return _project_usage_help($arg[2]);
 133    }
 134  }
 135  
 136  /**
 137   * Implementation of hook_perm().
 138   */
 139  function project_usage_perm() {
 140    return array(
 141      'view project usage',
 142    );
 143  }
 144  
 145  /**
 146   * Implementation of hook_simpletest().
 147   */
 148  function project_usage_simpletest() {
 149    $dir = drupal_get_path('module', 'project_usage'). '/tests';
 150    $tests = file_scan_directory($dir, '\.test$');
 151    return array_keys($tests);
 152  }
 153  
 154  /**
 155   * Implementation of hook_devel_caches().
 156   *
 157   * Lets the devel module know about our cache table so it can clear it.
 158   */
 159  function project_usage_flush_caches() {
 160    return array('cache_project_usage');
 161  }
 162  
 163  /**
 164   * Implementation of hook_project_page_link_alter().
 165   */
 166  function project_usage_project_page_link_alter(&$links, $node) {
 167    if (user_access('view project usage') && $node->project_release['releases']) {
 168      $links['resources']['links']['project_usage'] = l(t('View usage statistics'), 'project/usage/'. $node->project['uri']);
 169    }
 170  }
 171  
 172  /**
 173   * Return the total usage data for a given project across all versions.
 174   *
 175   * @param $nid
 176   *   The project node ID.
 177   *
 178   * @return
 179   *   The total reported usage for all versions of the given project.
 180   */
 181  function project_usage_get_project_total_usage($nid) {
 182    $active_tids = project_release_compatibility_list();
 183    static $total = array();
 184    if (empty($total[$nid])) {
 185      $total[$nid] = 0;
 186      foreach ($active_tids as $tid => $term_name) {
 187        $total[$nid] += project_usage_get_project_usage($nid, $tid);
 188      }
 189    }
 190    return $total[$nid];
 191  }
 192  
 193  /**
 194   * Return usage data for a given API version of a project.
 195   *
 196   * @param $nid
 197   *   The project node ID.
 198   * @param $tid
 199   *   The API compatibility taxonomy term ID to get usage for.
 200   *
 201   * @return
 202   *   The total reported usage for the given version of the given project.
 203   */
 204  function project_usage_get_project_usage($nid, $tid) {
 205    static $usage = array();
 206    module_load_include('inc', 'project_usage', 'includes/date_api');
 207    if (empty($usage[$nid][$tid])) {
 208      $usage[$nid][$tid] = db_result(db_query("SELECT count FROM {project_usage_week_project} WHERE nid = %d AND tid = %d AND timestamp = %d", $nid, $tid, project_usage_get_current_active_week()));
 209    }
 210    return $usage[$nid][$tid];
 211  }
 212  
 213  /**
 214   * Return usage data for a given release.
 215   *
 216   * @param $nid
 217   *   The release node ID.
 218   *
 219   * @return
 220   *   The total reported usage for the given release.
 221   */
 222  function project_usage_get_release_usage($nid) {
 223    static $usage = array();
 224    module_load_include('inc', 'project_usage', 'includes/date_api');
 225    if (empty($usage[$nid])) {
 226      $usage[$nid] = db_result(db_query("SELECT count FROM {project_usage_week_release} WHERE nid = %d AND timestamp = %d", $nid, project_usage_get_current_active_week()));
 227    }
 228    return $usage[$nid];
 229  }
 230  


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7