You are here

quant.module in Quant 7

Same filename and directory in other branches
  1. 6 quant.module

File

quant.module
View source
<?php

/**
 * @todo
 *   QuantData sub classes?
 *   Better error-handling in classes
 */

/**
 * Implements hook_hook_info().
 */
function quant_hook_info() {
  $hooks = array();
  $hooks['quants'] = array(
    'group' => 'quant',
  );
  $hooks['quant_charts'] = array(
    'group' => 'quant',
  );
  return $hooks;
}

/**
 * Implements hook_permission().
 */
function quant_permission() {
  return array(
    'administer quant' => array(
      'title' => t('Administer quant'),
      'description' => t('Permits access to the administration page and analytics page.'),
    ),
    'view analytics page' => array(
      'title' => t('View the analytics page'),
      'description' => t('Permits access to view the analytics page.'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function quant_menu() {
  $items = array();

  // Main analytics page
  $items['analytics'] = array(
    'title' => 'Site analytics',
    'description' => 'View charts depicting action over time for many Drupal components.',
    'page callback' => 'quant_page',
    'access arguments' => array(
      'view analytics page',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'quant.pages.inc',
  );

  // Also provide an analytics link in the admin reports
  $items['admin/reports/analytics'] = $items['analytics'];

  // Settings page
  $items['admin/config/media/quant'] = array(
    'title' => 'Quant (analytics)',
    'description' => 'Configure the quantitative analytics page',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'quant_admin_settings',
    ),
    'access arguments' => array(
      'administer quant',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'quant.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function quant_theme($existing, $type, $theme, $path) {
  $items = array();
  $items['quant_page'] = array(
    'variables' => array(
      'form' => NULL,
      'charts' => NULL,
    ),
    'file' => 'quant.theme.inc',
  );
  $items['quant_time_form'] = array(
    'render element' => 'form',
    'file' => 'quant.theme.inc',
  );
  $items['quant_table'] = array(
    'arguments' => array(
      'table' => NULL,
      'title' => NULL,
      'width' => NULL,
    ),
    'path' => $path . '/theme',
    'template' => 'quant_table',
  );
  return $items;
}

/**
 * Fetch all available quants
 */
function quant_get_quants() {
  return _quant_get_data('quants');
}

/**
 * Fetch a quant by ID
 * 
 * @param $id
 *   The quant id
 * @return
 *   A quant object, or NULL, if there is no quant with this ID
 */
function quant_get_quant($id) {
  $quants = quant_get_quants();
  return isset($quants[$id]) ? $quants[$id] : NULL;
}

/**
 * Fetch all available charts
 */
function quant_get_quant_charts() {
  static $charts = NULL;

  // Act if the static cache is not yet populated
  if ($charts === NULL) {
    $charts = _quant_get_data('quant_charts');
    foreach ($charts as $key => $plugin) {

      // Include the file we need
      quant_include($plugin->plugin['file'], $plugin->plugin['path'], NULL);

      // Load the class so the functions can be accessed, if needed
      $charts[$key]->chart = new $plugin->plugin['class'](NULL, $plugin);
    }
  }
  return $charts;
}

/**
 * Fetch a quant chart by ID
 * 
 * @param $id
 *   The quant chart  id
 * @return
 *   A quant chart info, or NULL, if there is no quant with this ID
 */
function quant_get_quant_chart($id) {
  $charts = quant_get_quant_charts();
  return isset($charts[$id]) ? $charts[$id] : NULL;
}

/**
 * Fetch the active chart plugin
 */
function quant_get_chart_plugin() {
  return quant_get_quant_chart(variable_get('quant_chart', 'table'));
}

/**
 * Fetch quant data
 * 
 * @param $hook
 *   The hook used to populate the desired data. This is also used as
 *   the cache key and alter hook.
 * @param $reset
 *   TRUE if the data should be completely refetched from the hooks.
 *   Defaults to FALSE.
 * @return
 *   The requested data
 */
function _quant_get_data($hook, $reset = FALSE) {
  static $data = array();

  // Act if the static cache is not yet populated
  if (!isset($data[$hook]) || $reset) {

    // Check the cache table next
    $cache = !$reset ? cache_get($hook) : NULL;

    // See if the cache has data
    if (isset($cache->data)) {

      // Use the cached tree
      $data[$hook] = $cache->data;
    }
    else {

      // Invoke the hooks
      $data[$hook] = module_invoke_all($hook);

      // Allow modules to alter the quants
      drupal_alter($hook, $data[$hook]);

      // Cache the quants
      cache_set($hook, $data[$hook], 'cache', CACHE_TEMPORARY);
    }
  }
  return $data[$hook];
}

/**
 * Include .inc files
 * Similar to ctools_include()
 * 
 * @param $file
 *   The base file name to be included.
 * @param $dir
 *   Optional subdirectory containing the include file.
 * @param $module
 *   Optional module containing the include.
 */
function quant_include($file, $dir = 'includes', $module = 'quant') {
  static $used = array();
  if ($module) {
    $dir = '/' . ($dir ? $dir . '/' : '');
    if (!isset($used[$module][$dir][$file])) {
      require_once './' . drupal_get_path('module', $module) . "{$dir}{$file}.inc";
      $used[$module][$dir][$file] = TRUE;
    }
  }
  else {
    if (!isset($used[$dir][$file])) {
      require_once "{$dir}/{$file}";
      $user[$dir][$file] = TRUE;
    }
  }
}

Functions

Namesort descending Description
quant_get_chart_plugin Fetch the active chart plugin
quant_get_quant Fetch a quant by ID
quant_get_quants Fetch all available quants
quant_get_quant_chart Fetch a quant chart by ID
quant_get_quant_charts Fetch all available charts
quant_hook_info Implements hook_hook_info().
quant_include Include .inc files Similar to ctools_include()
quant_menu Implements hook_menu().
quant_permission Implements hook_permission().
quant_theme Implements hook_theme().
_quant_get_data Fetch quant data