You are here

protected function DashboardForm::buildUserMetrics in Opigno statistics 8

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

Builds user metrics.

Return value

array Render array.

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

File

src/Form/DashboardForm.php, line 238

Class

DashboardForm
Implements the statistics dashboard.

Namespace

Drupal\opigno_statistics\Form

Code

protected function buildUserMetrics($lp_ids = NULL) {
  $connection = Database::getConnection();
  $query = $connection
    ->select('users', 'u');
  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.type', 'learning_path-group_membership');
    $query
      ->condition('g_c_f_d.gid', $lp_ids, 'IN');
  }
  $query
    ->condition('u.uid', 0, '<>');
  $query
    ->groupBy('u.uid');
  $users = $query
    ->countQuery()
    ->execute()
    ->fetchField();
  $now = $this->time
    ->getRequestTime();

  // Last 7 days.
  $period = 60 * 60 * 24 * 7;
  $query = $connection
    ->select('users_field_data', 'u');
  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.type', 'learning_path-group_membership');
    $query
      ->condition('g_c_f_d.gid', $lp_ids, 'IN');
  }
  $query
    ->condition('u.uid', 0, '<>');
  $query
    ->condition('u.created', $now - $period, '>');
  $query
    ->groupBy('u.uid');
  $new_users = $query
    ->countQuery()
    ->execute()
    ->fetchField();
  $query = $connection
    ->select('users_field_data', 'u');
  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.type', 'learning_path-group_membership');
    $query
      ->condition('g_c_f_d.gid', $lp_ids, 'IN');
  }
  $query
    ->condition('u.uid', 0, '<>');
  $query
    ->condition('u.access', $now - $period, '>');
  $query
    ->groupBy('u.uid');
  $active_users = $query
    ->countQuery()
    ->execute()
    ->fetchField();
  return [
    '#theme' => 'opigno_statistics_user_metrics',
    '#help_text' => t('The data below is related to your global Opigno platform (for all trainings).'),
    'users' => $this
      ->buildUserMetric($this
      ->t('Users'), $users, t('This is the total number of users on your Opigno instance')),
    'new_users' => $this
      ->buildUserMetric($this
      ->t('New users'), $new_users, t('This is the number of new users who registered to your Opigno instance during the last 7 days.')),
    'active_users' => $this
      ->buildUserMetric($this
      ->t('Recently active users'), $active_users, t('This is the number of users who logged in to your Opigno instance during the last 7 days.')),
  ];
}