You are here

protected function DashboardForm::buildUsersPerDay in Opigno statistics 8

Same name and namespace in other branches
  1. 3.x src/Form/DashboardForm.php \Drupal\opigno_statistics\Form\DashboardForm::buildUsersPerDay()

Builds active users per day graph.

Parameters

\Drupal\Core\Datetime\DrupalDateTime $datetime: Date.

mixed $lp_ids: LP ID.

Return value

array Render array.

Throws

\Exception

1 call to DashboardForm::buildUsersPerDay()
DashboardForm::buildTrainingsProgress in src/Form/DashboardForm.php
Builds trainings progress.

File

src/Form/DashboardForm.php, line 93

Class

DashboardForm
Implements the statistics dashboard.

Namespace

Drupal\opigno_statistics\Form

Code

protected function buildUsersPerDay(DrupalDateTime $datetime, $lp_ids = NULL) {
  $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 (is_array($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');
  for ($i = 1; $i <= 31; ++$i) {
    if (isset($data[$i])) {
      $data[$i] = $data[$i]->count;
    }
    else {
      $data[$i] = 0;
    }
  }
  return [
    '#theme' => 'opigno_statistics_chart__user_per_day',
    '#max_count' => max(max($data), 5),
    '#data' => $data,
  ];
}