You are here

block_tracker.module in Util 6.3

Same filename and directory in other branches
  1. 7 contribs/block_tracker/block_tracker.module

Track block usage.

File

contribs/block_tracker/block_tracker.module
View source
<?php

/**
 * @file
 * Track block usage.
 */

/**
 * Implements hook_menu().
 */
function block_tracker_menu() {
  $items = array();
  $items['admin/reports/block_tracker'] = array(
    'title' => 'Block Tracker',
    'description' => 'Block usage report',
    'page callback' => 'block_tracker_report',
    'access arguments' => array(
      'administer blocks',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function block_tracker_theme() {
  return array(
    'block_tracker_item' => array(
      'arguments' => array(
        'item' => NULL,
      ),
    ),
  );
}

/**
 * Implements hook_schema_alter().
 * Add a last used timestamp to the blocks table.
 */
function block_tracker_schema_alter(&$schema) {
  if (isset($schema['blocks'])) {
    $schema['blocks']['fields']['lastused'] = array(
      'description' => 'The Unix timestamp when the node was last used.',
      'type' => 'int',
      'not null' => TRUE,
      'default' => 0,
    );
  }
}

/**
 * Implements hook_preprocess_HOOK().
 * Update the last used timestamp in the blocks table.
 */
function block_tracker_preprocess_block($variables) {

  // Update the lastused timestamp in the block.
  db_query("UPDATE {blocks} SET lastused = %d WHERE bid = %d", time(), $variables['block']->bid);
}

/**
 * Menu callback.
 * Report on block usage.
 */
function block_tracker_report() {
  $output = '<div id="block-tracker-report">';
  $query = "SELECT bid, module, delta, theme, status FROM {blocks} WHERE lastused = 0 ORDER BY module, delta";
  $result = db_query($query);
  $rows = array();
  while ($row = db_fetch_object($result)) {
    $rows[] = theme('block_tracker_item', $row);
  }
  $header = array(
    t('Bid'),
    t('Module'),
    t('Delta'),
    t('Theme'),
    t('Status'),
    t('Name'),
  );
  $output .= '<h3>' . t('Unused blocks') . '</h3>';
  $output .= theme('table', $header, $rows, array(
    'style' => 'width: auto;',
  ));
  $output .= '<p>' . t('Note: this report only lists those that have been unused since this tracker was installed.
    Site usage usually varies, so sufficient time should be allowed before actng on this report.') . '</p>';

  // Unused in a week.
  $query = "SELECT bid, module, delta, theme, status FROM {blocks} WHERE lastused < %d AND lastused <> 0 ORDER BY module, delta";
  $result = db_query($query, time() - 604800);
  $rows = array();
  while ($row = db_fetch_object($result)) {
    $rows[] = theme('block_tracker_item', $row);
  }
  if ($rows) {
    $output .= '<h3>' . t('Blocks not used in the last week') . '</h3>';
    $output .= theme('table', $header, $rows, array(
      'style' => 'width: auto;',
    ));
  }
  return $output . '</div>';
}

/**
 * Theme function for report table rows.
 */
function theme_block_tracker_item($item) {
  static $titles = array();
  if (!isset($titles["{$item->module}-{$item->delta}"])) {
    $blocks = module_invoke($item->module, 'block');
    if ($title = $blocks[$item->delta]) {
      $titles["{$item->module}-{$item->delta}"] = $blocks[$item->delta]['info'];
    }
  }
  $row = array(
    l($item->bid, "admin/build/block/configure/{$item->module}/{$item->delta}"),
    $item->module,
    $item->delta,
    $item->theme,
    $item->status ? t('Enabled') : t('Disabled'),
    isset($titles["{$item->module}-{$item->delta}"]) ? $titles["{$item->module}-{$item->delta}"] : NULL,
  );
  return $row;
}

/**
 * Implements hook_requirements().
 * Add note that we are tracking blocks.
 */
function block_tracker_requirements($phase) {
  $requirements = array();
  switch ($phase) {
    case 'runtime':
      $requirements['block_tracker_status'] = array(
        'title' => 'Block Tracker',
        'value' => t('Block usage is being tracked on this site.'),
        'severity' => REQUIREMENT_OK,
      );
  }
  return $requirements;
}

Functions

Namesort descending Description
block_tracker_menu Implements hook_menu().
block_tracker_preprocess_block Implements hook_preprocess_HOOK(). Update the last used timestamp in the blocks table.
block_tracker_report Menu callback. Report on block usage.
block_tracker_requirements Implements hook_requirements(). Add note that we are tracking blocks.
block_tracker_schema_alter Implements hook_schema_alter(). Add a last used timestamp to the blocks table.
block_tracker_theme Implements hook_theme().
theme_block_tracker_item Theme function for report table rows.