You are here

protected function TrainingForm::buildUserMetrics in Opigno statistics 8

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

Builds user metrics.

Parameters

\Drupal\group\Entity\GroupInterface $group: Group.

Return value

array Render array.

1 call to TrainingForm::buildUserMetrics()
TrainingForm::buildForm in src/Form/TrainingForm.php
Form constructor.

File

src/Form/TrainingForm.php, line 239

Class

TrainingForm
Implements the training statistics page.

Namespace

Drupal\opigno_statistics\Form

Code

protected function buildUserMetrics(GroupInterface $group) {
  if ($group
    ->bundle() == 'opigno_class') {
    $condition = 'AND gc.type IN (\'opigno_class-group_membership\')';
  }
  else {
    $condition = 'AND gc.type IN (\'learning_path-group_membership\', \'opigno_course-group_membership\')';
  }
  $query = $this->database
    ->select('users', 'u');
  $query
    ->innerJoin('group_content_field_data', 'gc', "gc.entity_id = u.uid\n" . $condition . "\nAND gc.gid = :gid", [
    ':gid' => $group
      ->id(),
  ]);
  $users = $query
    ->condition('u.uid', 0, '<>')
    ->countQuery()
    ->execute()
    ->fetchField();
  $now = $this->time
    ->getRequestTime();

  // Last 7 days.
  $period = 60 * 60 * 24 * 7;
  $query = $this->database
    ->select('users_field_data', 'u');
  $query
    ->innerJoin('group_content_field_data', 'gc', "gc.entity_id = u.uid\n" . $condition . "\nAND gc.gid = :gid", [
    ':gid' => $group
      ->id(),
  ]);
  $new_users = $query
    ->condition('u.uid', 0, '<>')
    ->condition('u.created', $now - $period, '>')
    ->countQuery()
    ->execute()
    ->fetchField();
  $query = $this->database
    ->select('users_field_data', 'u');
  $query
    ->innerJoin('group_content_field_data', 'gc', "gc.entity_id = u.uid\n" . $condition . "\nAND gc.gid = :gid", [
    ':gid' => $group
      ->id(),
  ]);
  $active_users = $query
    ->condition('u.uid', 0, '<>')
    ->condition('u.login', $now - $period, '>')
    ->countQuery()
    ->execute()
    ->fetchField();
  return [
    '#theme' => 'opigno_statistics_user_metrics',
    '#help_text' => t('The metrics below are related to this training'),
    'users' => $this
      ->buildUserMetric($this
      ->t('Users'), $users, t('This is the number of users registered to that training.')),
    'new_users' => $this
      ->buildUserMetric($this
      ->t('New users'), $new_users, t('This is the number of users who registered during the last 7 days')),
    'active_users' => $this
      ->buildUserMetric($this
      ->t('Recently active users'), $active_users, t('This is the number of users who where active in that training within the last 7 days.')),
  ];
}