You are here

function opigno_learning_path_get_steps in Opigno Learning path 3.x

Same name and namespace in other branches
  1. 8 opigno_learning_path.module \opigno_learning_path_get_steps()

Builds up a list of steps in a group for a user.

Parameters

int $group_id: Group ID.

int $uid: User ID.

int $latest_cert_date: Latest certification date.

Return value

array Info about each step in a group for a user.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

14 calls to opigno_learning_path_get_steps()
LearningPathAchievementController::build_course_steps in src/Controller/LearningPathAchievementController.php
Returns course steps renderable array.
LearningPathAchievementController::build_lp_steps in src/Controller/LearningPathAchievementController.php
Returns LP steps.
LearningPathAchievementController::build_training_timeline in src/Controller/LearningPathAchievementController.php
Returns training timeline.
LearningPathAchievementController::course_steps_passed in src/Controller/LearningPathAchievementController.php
Returns course passed steps.
LearningPathContent::getAllStepsOnlyModules in src/LearningPathContent.php
Function for getting group steps without courses, only modules.

... See full list

File

./opigno_learning_path.module, line 2578
Contains opigno_learning_path.module.

Code

function opigno_learning_path_get_steps($group_id, $uid, $only_modules_and_courses = NULL, $latest_cert_date = NULL) {
  $key = "{$group_id}_{$uid}_{$only_modules_and_courses}_{$latest_cert_date}";
  $results =& drupal_static(__FUNCTION__);
  if (!isset($results[$key])) {
    $steps = [];
    $attempts_raw = [];
    $module = [];
    $user = User::load($uid);
    $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, $latest_cert_date);

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

            /** @var \Drupal\opigno_module\Entity\UserModuleStatus[] $attempts */
            $attempts = $module
              ->getModuleAttempts($user, NULL, $latest_cert_date);
            if (!empty($attempts)) {
              $attempts_raw[$id] = $attempts;
            }
            $best_attempt = NULL;
            if (!empty($attempts)) {
              $best_attempt = opigno_learning_path_best_attempt($attempts);
            }
            if ($best_attempt) {
              $step_info['best_attempt'] = $best_attempt
                ->id();
            }
          }
          $steps[] = $step_info;
          break;
        case 'ContentTypeCourse':
          $module = Group::load($id);
          $step_info = opigno_learning_path_get_course_step($group_id, $uid, $module, $latest_cert_date);
          $course_steps = OpignoGroupManagedContent::loadByGroupId($id);
          if (!empty($course_steps)) {

            // Check if each course module has at least one activity.
            foreach ($course_steps as $course_step) {
              $id = $course_step
                ->getEntityId();
              $opigno_module = OpignoModule::load($id);
              $attempts = $opigno_module
                ->getModuleAttempts($user, NULL, $latest_cert_date);
              if (!empty($attempts)) {
                $attempts_raw[$id] = $attempts;
              }
              $best_attempt = NULL;
              if (!empty($attempts)) {
                $best_attempt = opigno_learning_path_best_attempt($attempts);
              }
              if ($best_attempt) {
                $step_info['best_attempt'] = $best_attempt
                  ->id();
                $step_info['best_attempts'][$id] = $best_attempt
                  ->id();
              }
            }
          }
          $steps[] = $step_info;
          break;
        case 'ContentTypeMeeting':

          /** @var \Drupal\opigno_moxtra\MeetingInterface $meeting */
          if ($only_modules_and_courses) {
            break;
          }
          $meeting = $entity_type_manager
            ->getStorage('opigno_moxtra_meeting')
            ->load($id);
          $step_info = opigno_learning_path_get_meeting_step($group_id, $uid, $meeting);
          $steps[] = $step_info;
          break;
        case 'ContentTypeILT':

          /** @var \Drupal\opigno_ilt\ILTInterface $ilt */
          if ($only_modules_and_courses) {
            break;
          }
          $ilt = $entity_type_manager
            ->getStorage('opigno_ilt')
            ->load($id);
          $step_info = opigno_learning_path_get_ilt_step($group_id, $uid, $ilt);
          $steps[] = $step_info;
          break;
      }

      // Get training guided navigation option.
      $guidedNavigation = TRUE;
      if ($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];
}