function statistics_top_visitors

Page callback: Displays the "top visitors" page.

This displays the pages with the top number of visitors in a given time interval that haven't been flushed yet. The flush interval is set on the statistics settings form, but is dependent on cron running.

Return value

array A render array containing the top visitors information.

1 string reference to 'statistics_top_visitors'
statistics_menu in drupal/core/modules/statistics/statistics.module
Implements hook_menu().

File

drupal/core/modules/statistics/statistics.admin.inc, line 134
Admin page callbacks for the Statistics module.

Code

function statistics_top_visitors() {
  $ban_exists = module_exists('ban');
  $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',
    ),
    $ban_exists && user_access('Ban IP addresses') ? t('Operations') : '',
  );
  $query = db_select('accesslog', 'a', array(
    'target' => 'slave',
  ))
    ->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
    ->extend('Drupal\\Core\\Database\\Query\\TableSortExtender');
  if ($ban_exists) {
    $query
      ->leftJoin('ban_ip', 'b', 'a.hostname = b.ip');
  }
  $query
    ->leftJoin('users', 'u', 'a.uid = u.uid');
  $query
    ->addExpression('COUNT(a.uid)', 'hits');
  $query
    ->addExpression('SUM(a.timer)', 'total');
  $query
    ->fields('a', array(
    'uid',
    'hostname',
  ))
    ->fields('u', array(
    'name',
  ))
    ->groupBy('a.hostname')
    ->groupBy('a.uid')
    ->groupBy('u.name')
    ->limit(30);
  if ($ban_exists) {
    $query
      ->fields('b', array(
      'iid',
    ))
      ->groupBy('b.iid');
  }
  $query
    ->orderByHeader($header);
  $uniques_query = db_select('accesslog')
    ->distinct();
  $uniques_query
    ->fields('accesslog', array(
    'uid',
    'hostname',
  ));
  $count_query = db_select($uniques_query);
  $count_query
    ->addExpression('COUNT(*)');
  $query
    ->setCountQuery($count_query);
  $result = $query
    ->execute();
  $rows = array();
  $destination = drupal_get_destination();
  foreach ($result as $account) {
    $links = array();
    if ($ban_exists && user_access('ban IP addresses') && !$account->uid) {
      if ($account->iid) {
        $links['unban'] = array(
          'title' => t('unban IP address'),
          'href' => "admin/config/people/ban/delete/{$account->iid}",
          'query' => $destination,
        );
      }
      else {
        $links['ban'] = array(
          'title' => t('ban IP address'),
          'href' => "admin/config/people/ban/{$account->hostname}",
          'query' => $destination,
        );
      }
    }
    $row = array();
    $row[] = $account->hits;
    $row[] = $account->uid ? theme('username', array(
      'account' => $account,
    )) : $account->hostname;
    $row[] = format_interval(round($account->total / 1000));
    $row[] = array(
      'data' => array(
        '#type' => 'operations',
        '#links' => $links,
      ),
    );
    $rows[] = $row;
  }
  drupal_set_title(t('Top visitors in the past %interval', array(
    '%interval' => format_interval(config('statistics.settings')
      ->get('access_log.max_lifetime')),
  )), PASS_THROUGH);
  $build['statistics_top_visitors_table'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => t('No statistics available.'),
  );
  $build['statistics_top_visitors_pager'] = array(
    '#theme' => 'pager',
  );
  return $build;
}