You are here

public function LearningPathController::trainingContentSteps in Opigno Learning path 3.x

Training content steps.

1 call to LearningPathController::trainingContentSteps()
LearningPathController::trainingContent in src/Controller/LearningPathController.php
Training content.

File

src/Controller/LearningPathController.php, line 183

Class

LearningPathController
Class LearningPathController.

Namespace

Drupal\opigno_learning_path\Controller

Code

public function trainingContentSteps(&$content, $group, $user, $latest_cert_date) {

  // Get training guided navigation option.
  $freeNavigation = !OpignoGroupManagerController::getGuidedNavigation($group);
  if ($freeNavigation) {

    // Get all steps for LP.
    $steps = opigno_learning_path_get_all_steps($group
      ->id(), $user
      ->id(), NULL, $latest_cert_date);
  }
  else {

    // Get guided steps.
    $steps = opigno_learning_path_get_steps($group
      ->id(), $user
      ->id(), NULL, $latest_cert_date);
  }
  $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 = [];
  $steps = array_values($steps);
  if ($steps) {
    foreach ($steps as $key => $step) {
      $sub_title = '';
      $link = NULL;
      $free_link = NULL;
      $score = $this
        ->build_step_score_label($step);
      $state = $this
        ->build_step_state_label($step);
      unset($start_date);
      unset($end_date);
      if ($step['typology'] === 'Course') {
        if ($freeNavigation) {

          // Get all steps for LP.
          $course_steps = opigno_learning_path_get_all_steps($step['id'], $user
            ->id(), NULL, $latest_cert_date);
        }
        else {

          // Get guided steps.
          $course_steps = opigno_learning_path_get_steps($step['id'], $user
            ->id(), NULL, $latest_cert_date);
        }
        foreach ($course_steps as $course_step_key => &$course_step) {
          if ($course_step_key == 0) {

            // Load first step entity.
            $first_step = OpignoGroupManagedContent::load($course_steps[$course_step_key]['cid']);

            /* @var \Drupal\opigno_group_manager\OpignoGroupContentTypesManager $content_types_manager */
            $content_types_manager = \Drupal::service('opigno_group_manager.content_types.manager');
            $content_type = $content_types_manager
              ->createInstance($first_step
              ->getGroupContentTypeId());
            $step_url = $content_type
              ->getStartContentUrl($first_step
              ->getEntityId(), $group
              ->id());
            $link = Link::createFromRoute($course_step['name'], $step_url
              ->getRouteName(), $step_url
              ->getRouteParameters())
              ->toString();
          }
          else {

            // Get link to module.
            $course_parent_content_id = $course_steps[$course_step_key - 1]['cid'];
            $link = Link::createFromRoute($course_step['name'], 'opigno_learning_path.steps.next', [
              'group' => $group
                ->id(),
              'parent_content' => $course_parent_content_id,
            ])
              ->toString();
          }

          // Add compiled parameters to step array.
          $course_step['title'] = !empty($link) ? $link : $course_step['name'];
          $course_step['summary_details_table'] = [
            '#theme' => 'opigno_learning_path_training_content_step_summary_details_table',
            '#mandatory' => $step["mandatory"],
            '#type' => $course_step["typology"],
            '#steps' => $course_step['title'],
            '#status' => $this
              ->build_step_state_label($course_step),
            '#progress' => $this
              ->build_step_score_label($course_step),
          ];
        }
        $course_steps_array = array_map(function ($value) use ($group) {
          return [
            '#theme' => 'opigno_learning_path_training_content_step',
            'step' => $value,
            '#group' => $group,
          ];
        }, $course_steps);
        $step['course_steps'] = $course_steps_array;
        $steps[$key]['course_steps'] = $course_steps_array;
      }
      elseif ($step['typology'] === 'Module') {
        $step['module'] = OpignoModule::load($step['id']);
      }
      $title = $step['name'];
      if ($step['typology'] === 'Meeting') {

        /** @var \Drupal\opigno_moxtra\MeetingInterface $meeting */
        $meeting = $this
          ->entityTypeManager()
          ->getStorage('opigno_moxtra_meeting')
          ->load($step['id']);
        $start_date = $meeting
          ->getStartDate();
        $end_date = $meeting
          ->getEndDate();
        if ($freeNavigation) {
          $free_link = Link::createFromRoute($title, 'opigno_moxtra.meeting', [
            'opigno_moxtra_meeting' => $step['id'],
          ])
            ->toString();
        }
      }
      elseif ($step['typology'] === 'ILT') {

        /** @var \Drupal\opigno_ilt\ILTInterface $ilt */
        $ilt = $this
          ->entityTypeManager()
          ->getStorage('opigno_ilt')
          ->load($step['id']);
        $start_date = $ilt
          ->getStartDate();
        $end_date = $ilt
          ->getEndDate();
        if ($freeNavigation) {
          $free_link = Link::createFromRoute($title, 'entity.opigno_ilt.canonical', [
            'opigno_ilt' => $step['id'],
          ])
            ->toString();
        }
      }
      if (isset($start_date) && isset($end_date)) {
        $start_date = DrupalDateTime::createFromFormat(DrupalDateTime::FORMAT, $start_date);
        $end_date = DrupalDateTime::createFromFormat(DrupalDateTime::FORMAT, $end_date);
        $end_date_format = $end_date
          ->format('g:i A');
        if ($start_date
          ->format('jS F Y') != $end_date
          ->format('jS F Y')) {
          $end_date_format = $end_date
            ->format('jS F Y - g:i A');
        }
        $title .= ' / ' . $this
          ->t('@start to @end', [
          '@start' => $start_date
            ->format('jS F Y - g:i A'),
          '@end' => $end_date_format,
        ]);
      }
      $keys = array_keys($steps);

      // Build link for first step.
      if ($key == $keys[0]) {
        if ($step['typology'] == 'Course') {
          $link = NULL;
        }
        else {

          // Load first step entity.
          $first_step = OpignoGroupManagedContent::load($steps[$key]['cid']);

          /* @var \Drupal\opigno_group_manager\OpignoGroupContentTypesManager $content_types_manager */
          $content_types_manager = \Drupal::service('opigno_group_manager.content_types.manager');
          $content_type = $content_types_manager
            ->createInstance($first_step
            ->getGroupContentTypeId());
          $step_url = $content_type
            ->getStartContentUrl($first_step
            ->getEntityId(), $group
            ->id());
          $link = Link::createFromRoute($title, $step_url
            ->getRouteName(), $step_url
            ->getRouteParameters())
            ->toString();
        }
      }
      else {
        if ($step['typology'] == 'Course') {
          $link = NULL;
        }
        else {

          // Get link to module.
          if (!empty($free_link)) {
            $link = $free_link;
          }
          elseif (!empty($steps[$key - 1]['cid'])) {

            // Get previous step cid.
            if ($steps[$key - 1]['typology'] == 'Course') {

              // If previous step is course get it's last step.
              if (!empty($steps[$key - 1]['course_steps'])) {
                $course_last_step = end($steps[$key - 1]['course_steps']);
                if (!empty($course_last_step['cid'])) {
                  $parent_content_id = $course_last_step['cid'];
                }
              }
            }
            else {

              // If previous step isn't a course.
              $parent_content_id = $steps[$key - 1]['cid'];
            }
            if (!empty($parent_content_id)) {
              $link = Link::createFromRoute($title, 'opigno_learning_path.steps.next', [
                'group' => $group
                  ->id(),
                'parent_content' => $parent_content_id,
              ])
                ->toString();
            }
          }
        }
      }

      // Add compiled parameters to step array.
      $step['title'] = !empty($link) ? $link : $title;
      $step['sub_title'] = $sub_title;
      $step['score'] = $score;
      $step['state'] = $state;
      $step['summary_details_table'] = [
        '#theme' => 'opigno_learning_path_training_content_step_summary_details_table',
        '#mandatory' => $step["mandatory"],
        '#type' => $step["typology"],
        '#steps' => $step['title'],
        '#substeps' => (bool) ($step['course_steps'] ?? FALSE),
        '#status' => $state,
        '#progress' => $score,
      ];
      $steps_array[] = [
        '#theme' => 'opigno_learning_path_training_content_step',
        'step' => $step,
        '#group' => $group,
      ];
    }
    if ($steps_array) {
      $steps = $steps_array;
    }
  }
  return $steps;
}