View source
<?php
namespace Drupal\visitors\Controller\Report;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Datetime\Date;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class UserActivity extends ControllerBase {
protected $date;
protected $formBuilder;
public static function create(ContainerInterface $container) {
return new static($container
->get('date.formatter'), $container
->get('form_builder'));
}
public function __construct(DateFormatterInterface $date_formatter, FormBuilderInterface $form_builder) {
$this->date = $date_formatter;
$this->formBuilder = $form_builder;
}
public function display() {
$form = $this->formBuilder
->getForm('Drupal\\visitors\\Form\\DateFilter');
$header = $this
->_getHeader();
return array(
'visitors_date_filter_form' => $form,
'visitors_table' => array(
'#type' => 'table',
'#header' => $header,
'#rows' => $this
->_getData($header),
),
'visitors_pager' => array(
'#type' => 'pager',
),
);
}
protected function _getHeader() {
$headers = array(
'#' => array(
'data' => t('#'),
),
'u.name' => array(
'data' => t('User'),
'field' => 'u.name',
'specifier' => 'u.name',
'class' => array(
RESPONSIVE_PRIORITY_LOW,
),
),
'hits' => array(
'data' => t('Hits'),
'field' => 'hits',
'specifier' => 'hits',
'class' => array(
RESPONSIVE_PRIORITY_LOW,
),
'sort' => 'desc',
),
'nodes' => array(
'data' => t('Nodes'),
'field' => 'nodes',
'specifier' => 'nodes',
'class' => array(
RESPONSIVE_PRIORITY_LOW,
),
),
);
if (\Drupal::moduleHandler()
->moduleExists('comment')) {
$headers['comments'] = array(
'data' => t('Comments'),
'field' => 'comments',
'specifier' => 'comments',
'class' => array(
RESPONSIVE_PRIORITY_LOW,
),
);
}
return $headers;
}
protected function _getData($header) {
$is_comment_module_exist = \Drupal::moduleHandler()
->moduleExists('comment');
$items_per_page = \Drupal::config('visitors.config')
->get('items_per_page');
$query = \Drupal::database()
->select('users_field_data', 'u')
->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender')
->extend('Drupal\\Core\\Database\\Query\\TableSortExtender');
$query
->leftJoin('visitors', 'v', 'u.uid=v.visitors_uid');
$query
->leftJoin('node_field_data', 'nfd', 'nfd.uid=v.visitors_uid');
$query
->leftJoin('node', 'n', 'nfd.nid=n.nid');
if ($is_comment_module_exist) {
$query
->leftJoin('comment_field_data', 'c', 'u.uid=c.uid');
}
$query
->fields('u', array(
'name',
'uid',
));
$query
->addExpression('COUNT(DISTINCT v.visitors_id)', 'hits');
$query
->addExpression('COUNT(DISTINCT n.nid)', 'nodes');
if ($is_comment_module_exist) {
$query
->addExpression('COUNT(DISTINCT c.cid)', 'comments');
}
visitors_date_filter_sql_condition($query);
$query
->groupBy('u.name');
$query
->groupBy('u.uid');
$query
->groupBy('v.visitors_uid');
$query
->groupBy('nfd.uid');
if ($is_comment_module_exist) {
$query
->groupBy('c.uid');
}
$query
->orderByHeader($header);
$query
->limit($items_per_page);
$count_query = \Drupal::database()
->select('users_field_data', 'u');
$count_query
->leftJoin('visitors', 'v', 'u.uid=v.visitors_uid');
$count_query
->addExpression('COUNT(DISTINCT u.uid)');
visitors_date_filter_sql_condition($count_query);
$query
->setCountQuery($count_query);
$results = $query
->execute();
$rows = array();
$page = isset($_GET['page']) ? $_GET['page'] : 0;
$i = 0 + $page * $items_per_page;
foreach ($results as $data) {
$user = \Drupal::entityTypeManager()
->getStorage('user')
->load($data->uid);
if ($is_comment_module_exist) {
$rows[] = array(
++$i,
$user
->id() == 0 ? 'Anonymous User' : $user
->getAccountName(),
$data->hits,
$data->nodes,
$data->comments,
);
}
else {
$rows[] = array(
++$i,
$user
->id() == 0 ? 'Anonymous User' : $user
->getAccountName(),
$data->hits,
$data->nodes,
);
}
}
return $rows;
}
}