You are here

function statistics_top_visitors in Drupal 6

Same name and namespace in other branches
  1. 4 modules/statistics.module \statistics_top_visitors()
  2. 5 modules/statistics/statistics.module \statistics_top_visitors()
  3. 7 modules/statistics/statistics.admin.inc \statistics_top_visitors()

Menu callback; presents the "top visitors" page.

1 string reference to 'statistics_top_visitors'
statistics_menu in modules/statistics/statistics.module
Implementation of hook_menu().

File

modules/statistics/statistics.admin.inc, line 75
Admin page callbacks for the statistics module.

Code

function statistics_top_visitors() {
  $header = array(
    array(
      'data' => t('Hits'),
      'field' => 'hits',
      'sort' => 'desc',
    ),
    array(
      'data' => t('Visitor'),
      'field' => 'u.name',
    ),
    array(
      'data' => t('Total page generation time'),
      'field' => 'total',
    ),
    array(
      'data' => t('Operations'),
    ),
  );
  $sql = "SELECT COUNT(a.uid) AS hits, a.uid, u.name, a.hostname, SUM(a.timer) AS total, ac.aid FROM {accesslog} a LEFT JOIN {access} ac ON ac.type = 'host' AND LOWER(a.hostname) LIKE (ac.mask) LEFT JOIN {users} u ON a.uid = u.uid GROUP BY a.hostname, a.uid, u.name, ac.aid" . tablesort_sql($header);
  $sql_cnt = "SELECT COUNT(*) FROM (SELECT DISTINCT uid, hostname FROM {accesslog}) AS unique_visits";
  $result = pager_query($sql, 30, 0, $sql_cnt);
  $rows = array();
  while ($account = db_fetch_object($result)) {
    $qs = drupal_get_destination();
    $ban_link = $account->aid ? l(t('unban'), "admin/user/rules/delete/{$account->aid}", array(
      'query' => $qs,
    )) : l(t('ban'), "admin/user/rules/add/{$account->hostname}/host", array(
      'query' => $qs,
    ));
    $rows[] = array(
      $account->hits,
      $account->uid ? theme('username', $account) : $account->hostname,
      format_interval(round($account->total / 1000)),
      $ban_link,
    );
  }
  if (empty($rows)) {
    $rows[] = array(
      array(
        'data' => t('No statistics available.'),
        'colspan' => 4,
      ),
    );
  }
  drupal_set_title(t('Top visitors in the past %interval', array(
    '%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)),
  )));
  $output = theme('table', $header, $rows);
  $output .= theme('pager', NULL, 30, 0);
  return $output;
}