You are here

protected function LearningPathAchievementController::build_training_timeline in Opigno Learning path 3.x

Same name and namespace in other branches
  1. 8 src/Controller/LearningPathAchievementController.php \Drupal\opigno_learning_path\Controller\LearningPathAchievementController::build_training_timeline()

Returns training timeline.

Parameters

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

Return value

array Training timeline.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

src/Controller/LearningPathAchievementController.php, line 796

Class

LearningPathAchievementController
Class LearningPathAchievementController.

Namespace

Drupal\opigno_learning_path\Controller

Code

protected function build_training_timeline(GroupInterface $group) {

  /** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
  $date_formatter = \Drupal::service('date.formatter');
  $user = $this
    ->currentUser();
  $latest_cert_date = LPStatus::getTrainingStartDate($group, $user
    ->id());
  $result = (int) $this->database
    ->select('opigno_learning_path_achievements', 'a')
    ->fields('a')
    ->condition('uid', $user
    ->id())
    ->condition('gid', $group
    ->id())
    ->condition('status', 'completed')
    ->countQuery()
    ->execute()
    ->fetchField();
  if ($latest_cert_date || $result === 0) {

    // If training is not completed, generate steps.
    $steps = opigno_learning_path_get_steps($group
      ->id(), $user
      ->id(), NULL, $latest_cert_date);
    $steps = array_filter($steps, function ($step) {
      return $step['mandatory'];
    });
    $steps = array_filter($steps, function ($step) use ($user) {
      if ($step['typology'] === 'Meeting') {

        // If the user have not the collaborative features role.
        if (!$user
          ->hasPermission('view meeting entities')) {
          return FALSE;
        }

        // If the user is not a member of the meeting.

        /** @var \Drupal\opigno_moxtra\MeetingInterface $meeting */
        $meeting = \Drupal::entityTypeManager()
          ->getStorage('opigno_moxtra_meeting')
          ->load($step['id']);
        if (!$meeting
          ->isMember($user
          ->id())) {
          return FALSE;
        }
      }
      elseif ($step['typology'] === 'ILT') {

        // If the user is not a member of the ILT.

        /** @var \Drupal\opigno_ilt\ILTInterface $ilt */
        $ilt = \Drupal::entityTypeManager()
          ->getStorage('opigno_ilt')
          ->load($step['id']);
        if (!$ilt
          ->isMember($user
          ->id())) {
          return FALSE;
        }
      }
      return TRUE;
    });
    $steps = array_map(function ($step) use ($user, $latest_cert_date) {
      $status = opigno_learning_path_get_step_status($step, $user
        ->id(), TRUE, $latest_cert_date);
      if ($status == 'passed') {
        $step['passed'] = opigno_learning_path_is_passed($step, $user
          ->id());
      }
      return $step;
    }, $steps);
  }
  else {

    // Load steps from cache table.
    $results = $this->database
      ->select('opigno_learning_path_step_achievements', 'a')
      ->fields('a', [
      'name',
      'status',
      'completed',
      'typology',
      'entity_id',
    ])
      ->condition('uid', $user
      ->id())
      ->condition('gid', $group
      ->id())
      ->condition('mandatory', 1)
      ->execute()
      ->fetchAll();
    $steps = array_map(function ($result) {

      // Convert datetime string to timestamp.
      if (isset($result->completed)) {
        $completed = DrupalDateTime::createFromFormat(DrupalDateTime::FORMAT, $result->completed);
        $completed_timestamp = $completed
          ->getTimestamp();
      }
      else {
        $completed_timestamp = 0;
      }
      return [
        'name' => $result->name,
        'passed' => $result->status === 'passed',
        'completed on' => $completed_timestamp,
        'typology' => $result->typology,
        'id' => $result->entity_id,
      ];
    }, $results);
  }
  $items = [];
  foreach ($steps as $step) {
    $items[] = [
      'label' => $step['name'],
      'completed_on' => $step['completed on'] > 0 ? $date_formatter
        ->format($step['completed on'], 'custom', 'F d, Y') : '',
      'status' => opigno_learning_path_get_step_status($step, $user
        ->id(), TRUE, $latest_cert_date),
    ];
  }
  return [
    '#theme' => 'opigno_learning_path_training_timeline',
    '#steps' => $items,
  ];
}