You are here

function opigno_learning_path_get_all_steps in Opigno Learning path 3.x

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

Builds up a full list of all the steps in a group for a user.

Parameters

int $group_id: Group ID.

int $uid: User ID.

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

3 calls to opigno_learning_path_get_all_steps()
LearningPathAchievementController::build_lp_steps in src/Controller/LearningPathAchievementController.php
Returns LP steps.
LearningPathContent::getAllStepsOnlyModules in src/LearningPathContent.php
Function for getting group steps without courses, only modules.
LearningPathController::trainingContentSteps in src/Controller/LearningPathController.php
Training content steps.

File

./opigno_learning_path.module, line 2773
Contains opigno_learning_path.module.

Code

function opigno_learning_path_get_all_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]) && !empty($group_id)) {
    $steps = [];
    $contents = OpignoGroupManagedContent::loadByGroupId($group_id);
    usort($contents, function ($a, $b) {

      /** @var \Drupal\opigno_group_manager\Entity\OpignoGroupManagedContent $a */

      /** @var \Drupal\opigno_group_manager\Entity\OpignoGroupManagedContent $b */
      $b_y = $b
        ->getCoordinateY();
      $a_y = $a
        ->getCoordinateY();
      return $a_y > $b_y;
    });
    $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');
    $managed_content = reset($contents);
    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 */
            $user = \Drupal::currentUser();
            $attempts = $module
              ->getModuleAttempts($user, NULL, $latest_cert_date);
            $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':
          $course = Group::load($id);
          $step_info = opigno_learning_path_get_course_step($group_id, $uid, $course, $latest_cert_date);
          $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;
      }
      $managed_content = next($contents);
    }
    $results[$key] = $steps;
  }
  return $results[$key];
}