protected function DashboardForm::buildUsersPerDay in Opigno statistics 3.x
Same name and namespace in other branches
- 8 src/Form/DashboardForm.php \Drupal\opigno_statistics\Form\DashboardForm::buildUsersPerDay()
Builds active users per day graph.
Parameters
\Drupal\Core\Datetime\DrupalDateTime $datetime: Date.
array $lp_ids: LP IDs.
Return value
array Render array.
1 call to DashboardForm::buildUsersPerDay()
- DashboardForm::buildTrainingsProgress in src/
Form/ DashboardForm.php - Builds trainings progress.
File
- src/
Form/ DashboardForm.php, line 132
Class
- DashboardForm
- Implements the statistics dashboard.
Namespace
Drupal\opigno_statistics\FormCode
protected function buildUsersPerDay(DrupalDateTime $datetime, array $lp_ids = []) : array {
$max_time = $datetime
->format(DrupalDateTime::FORMAT);
// Last month.
$min_datetime = $datetime
->sub(new \DateInterval('P1M'));
$min_time = $min_datetime
->format(DrupalDateTime::FORMAT);
$query = $this->database
->select('opigno_statistics_user_login', 'u');
$query
->addExpression('DAY(u.date)', 'hour');
$query
->addExpression('COUNT(DISTINCT u.uid)', 'count');
if ($lp_ids) {
$query
->leftJoin('group_content_field_data', 'g_c_f_d', 'u.uid = g_c_f_d.entity_id');
$query
->condition('g_c_f_d.gid', $lp_ids, 'IN');
$query
->condition('g_c_f_d.type', 'learning_path-group_membership');
}
$query
->condition('u.uid', 0, '<>');
$data = $query
->condition('u.date', [
$min_time,
$max_time,
], 'BETWEEN')
->groupBy('hour')
->execute()
->fetchAllAssoc('hour');
// Get the number of days in the month.
$days = $min_datetime
->format('t');
for ($i = 1; $i <= $days; ++$i) {
if (isset($data[$i])) {
$data[$i] = $data[$i]->count;
}
else {
$data[$i] = 0;
}
}
// Sort the array.
ksort($data);
// Get color palette.
$theme = \Drupal::theme()
->getActiveTheme()
->getName();
$color_palette = color_get_palette($theme);
$color = $color_palette['desktop_link'] ?? '#4AD3B0';
// Prepare the data for the Line chart to display active users.
return [
'id' => '#active-users-chart',
'type' => 'line',
'labels' => array_keys($data),
'datasets' => [
[
'data' => array_values($data),
'fill' => FALSE,
'borderColor' => $color,
'tension' => 0,
'pointRadius' => 0,
],
],
'options' => [
'maintainAspectRatio' => FALSE,
'scales' => [
'xAxes' => [
'ticks' => [
'font' => [
'size' => 8,
],
'color' => '#A3A3A3',
'max' => count($data),
],
],
'yAxes' => [
'ticks' => [
'beginAtZero' => TRUE,
'font' => [
'size' => 8,
],
'color' => '#A3A3A3',
],
],
],
],
];
}