You are here

protected function TrainingForm::buildUserMetrics in Opigno statistics 3.x

Same name and namespace in other branches
  1. 8 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 235

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',
    'users' => $this
      ->buildUserMetric($this
      ->t('Users'), $users),
    'new_users' => $this
      ->buildUserMetric($this
      ->t('New users'), $new_users),
    'active_users' => $this
      ->buildUserMetric($this
      ->t('Recently active users'), $active_users),
  ];
}