You are here

statistics_advanced.module in Statistics Advanced 5

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

File

statistics_advanced.module
View source
<?php

/**
 * @file
 *
 */

/**
 * Implementation of hook_form_alter().
 */
function statistics_advanced_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'statistics_access_logging_settings') {

    // Add advanced settings
    $form['content']['statistics_advanced_ignore_repeat_views'] = array(
      '#type' => 'checkbox',
      '#title' => t('Only allow unique content views to increase a content\'s view count.'),
      '#default_value' => variable_get('statistics_advanced_ignore_repeat_views', 1),
      '#description' => t('Requires "count content views" enabled above. Checking for repeat anonymous visits requires the "enable access log" above.'),
    );
    $form['access']['statistics_advanced']['statistics_advanced_ignore_user_roles'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Do not record entries in the access log for users with the selected permissions'),
      '#description' => t('Requires "enable access log" enabled above. If you have certain users that perform extensive work on your site, this will help keep your accesslog table a little more clean and perform better. If you only want to exclude user #1, create a new role with no permissions and assign it to user #1. When enabled, this setting will also delete any records in the access log from users with the roles selected.'),
      '#options' => array_slice(user_roles(TRUE), 1, NULL, TRUE),
      '#default_value' => variable_get('statistics_advanced_ignore_user_roles', array()),
    );

    //array_splice($form, array_search('buttons', array_keys($form)), 0, $elements);
    $form['array_filter'] = array(
      '#type' => 'value',
      '#value' => TRUE,
    );
    $form['#submit'][] = 'statistics_advanced_form_submit';
  }
}
function statistics_advanced_form_submit($form, &$form_state) {
  $users = array_diff($form_state['values']['statistics_advanced_ignore_user_roles'], array(
    0,
  ));
  if (!empty($users)) {
    $query = db_query("SELECT DISTINCT uid FROM {users_roles} WHERE rid IN (" . db_placeholders($users, 'int') . ")", $users);
    while ($user = (int) db_result($query)) {
      $users[] = $user;

      //$result = db_query("DELETE FROM {accesslog} WHERE uid = %d", $user);
    }
    if (!empty($users) && db_query("DELETE FROM {accesslog} WHERE uid IN (" . db_placeholders($users, 'int') . ")", $users)) {
      if (db_affected_rows()) {
        drupal_set_message(format_plural(db_affected_rows(), 'Pruned 1 record from the access log.', 'Pruned @count records from the access log.'));
      }
    }
  }
}

/**
 * Implementation of hook_boot().
 */
function statistics_advanced_boot() {
  drupal_bootstrap(DRUPAL_BOOTSTRAP_PATH);
  if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == '' && variable_get('statistics_count_content_views', 0)) {
    $user_id = $GLOBALS['user']->uid;
    $GLOBALS['_statistics_advanced_ignore_visit'] = FALSE;
    if ($user_id == 0 && variable_get('statistics_enable_access_log', 0)) {

      // Check access log table for recent hits with current session id
      $result = db_result(db_query("SELECT uid FROM {accesslog} WHERE sid = '%s' AND (uid > 0 OR path = '%s') LIMIT 1", session_id(), $_GET['q']));
      if ($result > 0) {

        // Repeat visit from user that just logged out, check history table
        $user_id = $result;
      }
      if ($result === '0') {

        // Repeat anonymous user visit (same PHP session id and node id in accesslog table)
        $GLOBALS['_statistics_advanced_ignore_visit'] = TRUE;
      }
    }
    if ($user_id > 0 && db_result(db_query("SELECT TRUE FROM {history} WHERE uid = %d AND nid = %d", $user_id, arg(1)))) {

      // Repeat user visit (same user id and node id in history table)
      $GLOBALS['_statistics_advanced_ignore_visit'] = TRUE;
    }
  }
}

/**
 * Implementation of hook_exit().
 */
function statistics_advanced_exit() {

  // If this is a repeated visit by same user to the same node, subtract 1
  // visit from the node's view counter
  if ($GLOBALS['_statistics_advanced_ignore_visit'] == TRUE) {
    db_query('UPDATE {node_counter} SET daycount = daycount - 1, totalcount = totalcount - 1 WHERE nid = %d', arg(1));
  }

  // Remove access logs for current user
  $ignored_roles = variable_get('statistics_advanced_ignore_user_roles', array());
  if ($GLOBALS['user']->uid != 0 && !empty($ignored_roles) && variable_get('statistics_enable_access_log', 0) && (bool) array_intersect($ignored_roles, array_keys($user->roles))) {
    db_query('DELETE FROM {accesslog} WHERE uid = %d', $user->uid);
  }
}

Functions

Namesort descending Description
statistics_advanced_boot Implementation of hook_boot().
statistics_advanced_exit Implementation of hook_exit().
statistics_advanced_form_alter Implementation of hook_form_alter().
statistics_advanced_form_submit