| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
*
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_form_alter().
|
| 11 |
*/
|
| 12 |
function statistics_advanced_form_alter(&$form, &$form_state, $form_id) {
|
| 13 |
if ($form_id == 'statistics_access_logging_settings') {
|
| 14 |
|
| 15 |
// Add advanced settings
|
| 16 |
$form['content']['statistics_advanced_ignore_repeat_views'] = array(
|
| 17 |
'#type' => 'checkbox',
|
| 18 |
'#title' => t('Only allow unique content views to increase a content\'s view count.'),
|
| 19 |
'#default_value' => variable_get('statistics_advanced_ignore_repeat_views', 1),
|
| 20 |
'#description' => t('Requires "count content views" enabled above. Checking for repeat anonymous visits requires the "enable access log" above.'),
|
| 21 |
);
|
| 22 |
$form['access']['statistics_advanced']['statistics_advanced_ignore_user_roles'] = array(
|
| 23 |
'#type' => 'checkboxes',
|
| 24 |
'#title' => t('Do not record entries in the access log for users with the selected permissions'),
|
| 25 |
'#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.'),
|
| 26 |
'#options' => array_slice(user_roles(TRUE), 1, NULL, TRUE),
|
| 27 |
'#default_value' => variable_get('statistics_advanced_ignore_user_roles', array()),
|
| 28 |
);
|
| 29 |
|
| 30 |
//array_splice($form, array_search('buttons', array_keys($form)), 0, $elements);
|
| 31 |
$form['array_filter'] = array('#type' => 'value', '#value' => TRUE);
|
| 32 |
$form['#submit'][] = 'statistics_advanced_form_submit';
|
| 33 |
}
|
| 34 |
}
|
| 35 |
|
| 36 |
function statistics_advanced_form_submit($form, &$form_state) {
|
| 37 |
$users = array_diff($form_state['values']['statistics_advanced_ignore_user_roles'], array(0));
|
| 38 |
if (!empty($users)) {
|
| 39 |
$query = db_query("SELECT DISTINCT uid FROM {users_roles} WHERE rid IN (" . db_placeholders($users, 'int') . ")", $users);
|
| 40 |
while ($user = (int) db_result($query)) {
|
| 41 |
$users[] = $user;
|
| 42 |
//$result = db_query("DELETE FROM {accesslog} WHERE uid = %d", $user);
|
| 43 |
}
|
| 44 |
|
| 45 |
if (!empty($users) && db_query("DELETE FROM {accesslog} WHERE uid IN (" . db_placeholders($users, 'int') . ")", $users)) {
|
| 46 |
if (db_affected_rows()) {
|
| 47 |
drupal_set_message(format_plural(db_affected_rows(), 'Pruned 1 record from the access log.', 'Pruned @count records from the access log.'));
|
| 48 |
}
|
| 49 |
}
|
| 50 |
}
|
| 51 |
}
|
| 52 |
|
| 53 |
/**
|
| 54 |
* Implementation of hook_boot().
|
| 55 |
*/
|
| 56 |
function statistics_advanced_boot() {
|
| 57 |
drupal_bootstrap(DRUPAL_BOOTSTRAP_PATH);
|
| 58 |
|
| 59 |
if ((arg(0) == 'node') && is_numeric(arg(1)) && arg(2) == '' && variable_get('statistics_count_content_views', 0)) {
|
| 60 |
$user_id = $GLOBALS['user']->uid;
|
| 61 |
$GLOBALS['_statistics_advanced_ignore_visit'] = FALSE;
|
| 62 |
|
| 63 |
if ($user_id == 0 && variable_get('statistics_enable_access_log', 0)) {
|
| 64 |
// Check access log table for recent hits with current session id
|
| 65 |
$result = db_result(db_query("SELECT uid FROM {accesslog} WHERE sid = '%s' AND (uid > 0 OR path = '%s') LIMIT 1", session_id(), $_GET['q']));
|
| 66 |
|
| 67 |
if ($result > 0) {
|
| 68 |
// Repeat visit from user that just logged out, check history table
|
| 69 |
$user_id = $result;
|
| 70 |
}
|
| 71 |
if ($result === '0') {
|
| 72 |
// Repeat anonymous user visit (same PHP session id and node id in accesslog table)
|
| 73 |
$GLOBALS['_statistics_advanced_ignore_visit'] = TRUE;
|
| 74 |
}
|
| 75 |
}
|
| 76 |
|
| 77 |
if ($user_id > 0 && db_result(db_query("SELECT TRUE FROM {history} WHERE uid = %d AND nid = %d", $user_id, arg(1)))) {
|
| 78 |
// Repeat user visit (same user id and node id in history table)
|
| 79 |
$GLOBALS['_statistics_advanced_ignore_visit'] = TRUE;
|
| 80 |
}
|
| 81 |
}
|
| 82 |
}
|
| 83 |
|
| 84 |
/**
|
| 85 |
* Implementation of hook_exit().
|
| 86 |
*/
|
| 87 |
function statistics_advanced_exit() {
|
| 88 |
// If this is a repeated visit by same user to the same node, subtract 1
|
| 89 |
// visit from the node's view counter
|
| 90 |
if ($GLOBALS['_statistics_advanced_ignore_visit'] == TRUE) {
|
| 91 |
db_query('UPDATE {node_counter} SET daycount = daycount - 1, totalcount = totalcount - 1 WHERE nid = %d', arg(1));
|
| 92 |
}
|
| 93 |
|
| 94 |
// Remove access logs for current user
|
| 95 |
$ignored_roles = variable_get('statistics_advanced_ignore_user_roles', array());
|
| 96 |
if ($GLOBALS['user']->uid != 0 && !empty($ignored_roles) && variable_get('statistics_enable_access_log', 0) && (bool) array_intersect($ignored_roles, array_keys($user->roles))) {
|
| 97 |
db_query('DELETE FROM {accesslog} WHERE uid = %d', $user->uid);
|
| 98 |
}
|
| 99 |
}
|