View source
<?php
function statistics_advanced_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'statistics_access_logging_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()),
);
$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;
}
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.'));
}
}
}
}
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)) {
$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) {
$user_id = $result;
}
if ($result === '0') {
$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)))) {
$GLOBALS['_statistics_advanced_ignore_visit'] = TRUE;
}
}
}
function statistics_advanced_exit() {
if ($GLOBALS['_statistics_advanced_ignore_visit'] == TRUE) {
db_query('UPDATE {node_counter} SET daycount = daycount - 1, totalcount = totalcount - 1 WHERE nid = %d', arg(1));
}
$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);
}
}