Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @file
- * Tracks visited pages.
- */
- /**
- * Implements hook_init().
- */
- function tracks_init() {
- // Grab the tracking history from a variable.
- $track = variable_get('tracks_history', array());
- // Add current page to track.
- $track[] = array(
- 'title' => strip_tags(drupal_get_title()),
- 'path' => $_GET['q'],
- 'timestamp' => REQUEST_TIME,
- );
- // Save track as a variable.
- variable_set('tracks_history', $track);
- }
- /**
- * Implements hook_permission().
- */
- function tracks_permission() {
- return array(
- 'administer tracks' => array(
- 'title' => t('Administer Tracks module'),
- 'description' => t('Perform admin tasks for Tracks module.'),
- ),
- 'access tracks blocks' => array(
- 'title' => t('Access Tracks blocks'),
- 'description' => t('View blocks generated by Tracks module.'),
- ),
- );
- }
- /**
- * Implements hook_menu().
- */
- function tracks_menu() {
- // Module settings.
- $items['admin/config/tracks'] = array(
- 'title' => 'Tracks',
- 'description' => 'tracks configuration',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('tracks_admin_settings'),
- 'access arguments' => array('administer tracks'),
- 'file' => 'tracks.admin.inc',
- 'file path' => drupal_get_path('module', 'tracks'),
- );
- return $items;
- }
- /**
- * Implements hook_cron().
- */
- function tracks_cron() {
- // Get track array.
- $track = variable_get('tracks_history', array());
- // Get offset for array_slice, save last five items.
- $count_minus_5 = count($track) - 5;
- // Eliminate everything except the last five.
- $short_track = array_slice($track, $count_minus_5);
- // Save the shorter list.
- variable_set('tracks_history', $short_track);
- }
- /**
- * Implements hook_block_info().
- */
- function tracks_block_info() {
- $blocks['history'] = array(
- 'info' => t('History'),
- 'cache' => DRUPAL_NO_CACHE,
- );
- return $blocks;
- }
- /**
- * Implements hook_block_configure().
- */
- function tracks_block_configure($delta = '') {
- // Get max allowed from config.
- $max_to_display = variable_get('tracks_block_max', 50);
- // Add a select box of numbers form 1 to $max_to_display.
- $form['tracks_block_num'] = array(
- '#type' => 'select',
- '#title' => t('Number of items to show'),
- '#default_value' => variable_get('tracks_block_num', '5'),
- '#options' => drupal_map_assoc(range(1, $max_to_display)),
- );
- return $form;
- }
- /**
- * Implements hook_block_save().
- */
- function tracks_block_save($delta = '', $edit = array()) {
- variable_set('tracks_block_num', $edit['tracks_block_num']);
- }
- /**
- * Implements hook_block_view().
- */
- function tracks_block_view($delta = '') {
- if (user_access('access tracks blocks')) {
- $block = array();
- $type = $delta;
- // list($type, $id) = explode('-', $delta); // Unnecessary with one block.
- switch ($type) {
- case 'history':
- // Create list of previous paths.
- // Get history from variable.
- $track = variable_get('tracks_history', array());
- // Flip saved array to show newest pages first.
- $reverse_track = array_reverse($track);
- // Get number of items to display.
- $num_items = variable_get('tracks_block_num', '5');
- // Output latest items as list.
- // Initialize variable.
- $output = '';
- for ($i = 0; $i < $num_items; $i++) {
- if ($item = $reverse_track[$i]) {
- $output .= '<li>'
- . l($item['title'], $item['path'])
- . ' - '
- . format_interval(REQUEST_TIME - $item['timestamp'])
- . ' '
- . t('ago')
- . '</li>';
- }
- }
- if (isset($output)) {
- $output = '
- <p>' . t('Last @num pages you visited.', array('@num' => $num_items)) . '</p>
- <ul>' . $output . '</ul>
- ';
- }
- // Return the $block variable with subject (title) and content (output).
- $block['subject'] = 'History';
- $block['content'] = $output;
- break;
- }
- return $block;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement