You are here

protected function LearningPathAchievementController::build_lp_steps in Opigno Learning path 8

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

Returns LP steps.

Parameters

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

Return value

array LP steps.

1 call to LearningPathAchievementController::build_lp_steps()
LearningPathAchievementController::training_steps_ajax in src/Controller/LearningPathAchievementController.php
Loads steps for a training with a AJAX.

File

src/Controller/LearningPathAchievementController.php, line 640

Class

LearningPathAchievementController
Class LearningPathAchievementController.

Namespace

Drupal\opigno_learning_path\Controller

Code

protected function build_lp_steps(GroupInterface $group) {
  $user = $this
    ->currentUser();
  $uid = $user
    ->id();

  // Get training latest certification timestamp.
  $latest_cert_date = LPStatus::getTrainingStartDate($group, $uid);

  // 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(), $uid, NULL, $latest_cert_date);
  }
  else {

    // Get guided steps.
    $steps = opigno_learning_path_get_steps($group
      ->id(), $uid, 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_map(function ($step) use ($uid, $latest_cert_date) {
    $step['status'] = opigno_learning_path_get_step_status($step, $uid, TRUE, $latest_cert_date);
    $step['attempted'] = opigno_learning_path_is_attempted($step, $uid);
    $step['progress'] = opigno_learning_path_get_step_progress($step, $uid, FALSE, $latest_cert_date);
    return $step;
  }, $steps);

  /** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
  $date_formatter = \Drupal::service('date.formatter');
  $status = [
    'pending' => [
      'class' => 'lp_summary_step_state_in_progress',
      'title' => t('Pending'),
    ],
    'failed' => [
      'class' => 'lp_summary_step_state_failed',
      'title' => t('Failed'),
    ],
    'passed' => [
      'class' => 'lp_summary_step_state_passed',
      'title' => t('Passed'),
    ],
  ];
  return [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'training_steps_' . $group
        ->id(),
      'class' => [
        'lp_details',
      ],
    ],
    array_map(function ($step) use ($group, $uid, $date_formatter, $status, $user, $latest_cert_date) {
      $is_module = $step['typology'] === 'Module';
      $is_course = $step['typology'] === 'Course';
      $time_spent = $step['attempted'] && $step['time spent'] > 0 ? $date_formatter
        ->formatInterval($step['time spent']) : 0;
      $completed = $step['attempted'] && $step['completed on'] > 0 ? $date_formatter
        ->format($step['completed on'], 'custom', 'F d Y') : '';
      if ($is_module) {
        $module = OpignoModule::load($step['id']);
        $moduleHandler = \Drupal::service('module_handler');
        if ($moduleHandler
          ->moduleExists('opigno_skills_system') && $module
          ->getSkillsActive()) {
          $attempts = $module
            ->getModuleAttempts($user, NULL, $latest_cert_date);
          $attempt = $this
            ->getTargetAttempt($attempts, $module);
          $account = \Drupal\user\Entity\user::load($attempt
            ->getOwnerId());
          $answers = $module
            ->userAnswers($account, $attempt);
          $count_answers_from_step = count($answers);
          $approved = $count_answers_from_step . '/' . $count_answers_from_step;
          $approved_percent = 100;
          $step['progress'] = 1;
        }
        else {
          $approved_activities = $this
            ->module_approved_activities($group
            ->id(), $step['id'], $latest_cert_date);
          $approved = $approved_activities . '/' . $step['activities'];
          $approved_percent = $approved_activities / $step['activities'] * 100;
        }
      }
      if ($is_course) {
        $course_steps = $this
          ->course_steps_passed($group, Group::load($step['id']), $latest_cert_date);
        $passed = $course_steps['passed'] . '/' . $course_steps['total'];
        $passed_percent = $course_steps['passed'] / $course_steps['total'] * 100;
        $score = $step['best score'];
        $score_percent = $score;
      }

      // Get existing badge count.
      $badges = 0;
      if (\Drupal::moduleHandler()
        ->moduleExists('opigno_module') && ($is_course || $is_module)) {
        $result = OpignoModuleBadges::opignoModuleGetBadges($uid, $group
          ->id(), $step['typology'], $step['id']);
        if ($result) {
          $badges = $result;
        }
      }
      $content = [
        '#theme' => 'opigno_learning_path_training_step',
        '#step' => $step,
        '#is_module' => $is_module,
        $is_module ? [
          '#theme' => 'opigno_learning_path_training_module',
          '#status' => isset($status[$step['status']]) ? $status[$step['status']] : $status['pending'],
          '#group_id' => $group
            ->id(),
          '#step' => $step,
          '#time_spent' => $time_spent,
          '#completed' => $completed,
          '#badges' => $badges,
          '#approved' => [
            'value' => $approved,
            'percent' => $approved_percent,
          ],
        ] : [],
        $is_course ? [
          '#type' => 'container',
          '#attributes' => [
            'class' => [
              'lp_step_summary_wrapper',
            ],
          ],
          [
            '#theme' => 'opigno_learning_path_training_course',
            '#passed' => [
              'value' => $passed,
              'percent' => $passed_percent,
            ],
            '#score' => $score,
            '#step' => $step,
            '#completed' => $completed,
            '#badges' => $badges,
            '#time_spent' => $time_spent,
          ],
          $this
            ->build_course_steps($group, Group::load($step['id'])),
        ] : [],
      ];
      return $content;
    }, $steps),
  ];
}