You are here

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

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

Builds training progress.

Parameters

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

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

mixed $expired_uids: Users uids with the training expired certification.

Return value

array Render array.

Throws

\Exception

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

File

src/Form/TrainingForm.php, line 100

Class

TrainingForm
Implements the training statistics page.

Namespace

Drupal\opigno_statistics\Form

Code

protected function buildTrainingsProgress(GroupInterface $group, $datetime, $expired_uids = NULL) {
  if ($datetime) {
    $time_str = $datetime
      ->format(DrupalDateTime::FORMAT);
    $group_bundle = $group
      ->bundle();

    // Get number of users with expired certificates.
    $expired_users_number = !empty($expired_uids) ? count($expired_uids) : 0;
    $query = $this->database
      ->select('opigno_learning_path_achievements', 'a');
    $query
      ->addExpression('SUM(a.progress) / (COUNT(a.progress) + :expired_users_number) / 100', 'progress', [
      ':expired_users_number' => $expired_users_number,
    ]);
    $query
      ->addExpression('COUNT(a.completed) / (COUNT(a.registered) + :expired_users_number)', 'completion', [
      ':expired_users_number' => $expired_users_number,
    ]);
    $query
      ->condition('a.uid', 0, '<>');
    if (!empty($expired_uids)) {

      // Exclude users with the training expired certification.
      $query
        ->condition('a.uid', $expired_uids, 'NOT IN');
    }
    $or_group = $query
      ->orConditionGroup();
    $or_group
      ->condition('a.completed', $time_str, '<');
    $or_group
      ->isNull('a.completed');
    if ($group_bundle == 'learning_path') {
      $query
        ->condition('a.gid', $group
        ->id())
        ->condition('a.registered', $time_str, '<');
    }
    elseif ($group_bundle == 'opigno_class') {
      $query_class = $this->database
        ->select('group_content_field_data', 'g_c_f_d')
        ->fields('g_c_f_d', [
        'gid',
      ])
        ->condition('entity_id', $group
        ->id())
        ->condition('type', 'group_content_type_27efa0097d858')
        ->execute()
        ->fetchAll();
      $lp_ids = [];
      foreach ($query_class as $result_ids) {
        $lp_ids[] = $result_ids->gid;
      }
      if (empty($lp_ids)) {
        $lp_ids[] = 0;
      }
      $query
        ->condition('a.gid', $lp_ids, 'IN');
    }

    // Add only class members.
    $members = $group
      ->getMembers();
    foreach ($members as $member) {
      $user = $member
        ->getUser();
      if ($user) {
        $members_ids[$user
          ->id()] = $member
          ->getUser()
          ->id();
      }
    }
    if (empty($members_ids)) {
      $members_ids[] = 0;
    }
    $query
      ->condition('a.uid', $members_ids, 'IN');
    $data = $query
      ->execute()
      ->fetchAssoc();
  }
  else {
    $data = [
      'progress' => 0,
      'completion' => 0,
    ];
  }
  return [
    'drupalSettings' => [
      'opignoCharts' => [
        'trainingProgress' => $this
          ->buildDonutChart((double) $data['progress'], '#training-progress-chart'),
        'trainingCompletion' => $this
          ->buildDonutChart((double) $data['completion'], '#training-completion-chart'),
      ],
    ],
  ];
}