You are here

function opigno_learning_path_get_steps_current_attempt in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x opigno_learning_path.module \opigno_learning_path_get_steps_current_attempt()

Builds a list of group steps for a user current attempt.

2 calls to opigno_learning_path_get_steps_current_attempt()
opigno_learning_path_get_score in ./opigno_learning_path.module
Calculates learning path score.
opigno_learning_path_is_passed in ./opigno_learning_path.module
Returns LP passed flag.

File

./opigno_learning_path.module, line 3313
Contains opigno_learning_path.module.

Code

function opigno_learning_path_get_steps_current_attempt($group_id, $uid) {
  $key = "{$group_id}_{$uid}";
  $results =& drupal_static(__FUNCTION__);
  if (!isset($results[$key])) {
    $steps = [];
    $attempts_raw = [];
    $module = [];
    $entity_type_manager = \Drupal::entityTypeManager();

    /** @var \Drupal\opigno_group_manager\OpignoGroupContentTypesManager $content_type_manager */
    $content_type_manager = \Drupal::service('opigno_group_manager.content_types.manager');

    /** @var \Drupal\opigno_group_manager\Entity\OpignoGroupManagedContent $first_content */
    $managed_content = OpignoGroupManagedContent::getFirstStep($group_id);
    while ($managed_content) {
      $id = $managed_content
        ->getEntityId();
      $type_id = $managed_content
        ->getGroupContentTypeId();
      $type = $content_type_manager
        ->createInstance($type_id);

      /** @var \Drupal\opigno_group_manager\OpignoGroupContent $content */
      $content = $type
        ->getContent($id);
      if ($content === FALSE) {

        // If can't load step content, skip it. Assume user has got 100% score.
        $managed_content = $managed_content
          ->getNextStep(100);
        continue;
      }
      switch ($type_id) {
        case 'ContentTypeModule':

          /** @var \Drupal\opigno_module\Entity\OpignoModule $module */
          $module = $entity_type_manager
            ->getStorage('opigno_module')
            ->load($id);
          $step_info = opigno_learning_path_get_module_step($group_id, $uid, $module);
          $step_info["best score"] = $step_info["current attempt score"];

          /** @var \Drupal\opigno_module\Entity\OpignoModule $module */
          if ($module = OpignoModule::load($id)) {

            /** @var \Drupal\opigno_module\Entity\UserModuleStatus[] $attempts */
            $user = \Drupal::currentUser();
            $attempts = $module
              ->getModuleAttempts($user, 'last');
            $attempts_raw[$id] = $attempts;
            if (!empty($attempts)) {
              $last_attempt = array_shift($attempts);
              $step_info['last_attempt'] = $last_attempt;
              $step_info['best_attempt'] = $last_attempt
                ->id();
            }
          }
          $steps[] = $step_info;
          break;
        case 'ContentTypeCourse':
          $course = Group::load($id);
          $step_info = opigno_learning_path_get_course_step($group_id, $uid, $course);
          $step_info["best score"] = $step_info["current attempt score"];
          $steps[] = $step_info;
          break;
        case 'ContentTypeMeeting':

          /** @var \Drupal\opigno_moxtra\MeetingInterface $meeting */
          $meeting = $entity_type_manager
            ->getStorage('opigno_moxtra_meeting')
            ->load($id);
          $step_info = opigno_learning_path_get_meeting_step($group_id, $uid, $meeting);
          $step_info["best score"] = $step_info["current attempt score"];
          $steps[] = $step_info;
          break;
        case 'ContentTypeILT':

          /** @var \Drupal\opigno_ilt\ILTInterface $ilt */
          $ilt = $entity_type_manager
            ->getStorage('opigno_ilt')
            ->load($id);
          $step_info = opigno_learning_path_get_ilt_step($group_id, $uid, $ilt);
          $step_info["best score"] = $step_info["current attempt score"];
          $steps[] = $step_info;
          break;
      }

      // Get training guided navigation option.
      $group = Group::load($group_id);
      $guidedNavigation = OpignoGroupManagerController::getGuidedNavigation($group);

      // To get a next step, if user is not attempted step,
      // assume user has got 100% score.
      $best_score = isset($step_info) && opigno_learning_path_is_attempted($step_info, $uid) ? $step_info['best score'] : 100;
      $managed_content = $managed_content
        ->getNextStep($best_score, $attempts_raw, $module, $guidedNavigation, $type_id);
    }
    $results[$key] = $steps;
  }
  return $results[$key];
}