You are here

function opigno_learning_path_validate_group_creating_steps in Opigno Learning path 8

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

Detects if all steps of creating learning path are correctly filled.

Return array with the correctly filled steps.

1 call to opigno_learning_path_validate_group_creating_steps()
opigno_learning_path_get_step_list_top in ./opigno_learning_path.module
Returns step list for page top area.

File

./opigno_learning_path.module, line 3730
Contains opigno_learning_path.module.

Code

function opigno_learning_path_validate_group_creating_steps() {
  $is_learning_path_route = opigno_learning_path_is_lp_route();
  if ($is_learning_path_route) {

    // Load the learning path group.
    $group = \Drupal::routeMatch()
      ->getParameter('group');
    if (!$group) {

      // First step - group is not created yet.
      $steps_number = [];
      return $steps_number;
    }
    $group_type = $group
      ->getGroupType()
      ->id();
    $steps = OpignoGroupManagedContent::loadByGroupId($group
      ->id());

    // Array with steps number.
    $steps_number = array_values(opigno_learning_path_get_routes_steps());
    $steps_number = array_unique($steps_number);
    if (!$steps) {

      // Means group is empty or all steps are invalid.
      $steps_number = [
        1,
      ];
    }
    else {

      // Check if there is at least one mandatory entity.
      if ($group_type == 'learning_path') {
        $has_mandatory_item = FALSE;
        foreach ($steps as $step) {
          if ($step
            ->isMandatory()) {
            $has_mandatory_item = TRUE;
          }
        }
        if (!$has_mandatory_item) {
          $steps_number = array_diff($steps_number, [
            2,
          ]);
        }
      }
      foreach ($steps as $step) {
        $id = $step
          ->getEntityId();
        $type_id = $step
          ->getGroupContentTypeId();
        if ($group_type == 'learning_path') {

          // Check if training is published.
          $is_published = $group->field_learning_path_published->value;
          if (!$is_published) {
            $steps_number = array_diff($steps_number, [
              5,
            ]);
          }

          // Check if each module has at least one activity.
          if ($type_id === 'ContentTypeModule') {
            $module = OpignoModule::load($id);
            $activities = $module
              ->getModuleActivities();
            $moduleHandler = \Drupal::service('module_handler');
            if (!$moduleHandler
              ->moduleExists('opigno_skills_system') || !$activities && !$module
              ->getSkillsActive() && !$module
              ->getModuleSkillsGlobal()) {
              $steps_number = array_diff($steps_number, [
                4,
              ]);
            }
          }
          elseif ($type_id === 'ContentTypeCourse') {
            $course_steps = OpignoGroupManagedContent::loadByGroupId($id);
            if (!$course_steps) {

              // Means course group is empty, so 3 and 4 steps are invalid.
              $steps_number = array_diff($steps_number, [
                3,
                4,
              ]);
            }
            else {

              // Check if each course module has at least one activity.
              foreach ($course_steps as $course_step) {
                $id = $course_step
                  ->getEntityId();
                $module = OpignoModule::load($id);
                $activities = $module
                  ->getModuleActivities();
                if (!$activities) {
                  $steps_number = array_diff($steps_number, [
                    4,
                  ]);
                }
              }
            }
          }
        }
        elseif ($group_type == 'opigno_course') {
          $course_steps = OpignoGroupManagedContent::loadByGroupId($group
            ->id());
          if (!$course_steps) {

            // Means course group is empty, so 2 and 3 steps are invalid.
            $steps_number = array_diff($steps_number, [
              2,
              3,
            ]);
          }
          else {
            foreach ($course_steps as $index => $course_step) {
              $id = $course_step
                ->getEntityId();
              $module = OpignoModule::load($id);
              if ($module) {
                $activities = $module
                  ->getModuleActivities();
                if (!$activities) {
                  $steps_number = array_diff($steps_number, [
                    3,
                  ]);
                }
              }
            }
          }
        }
      }
    }
    return $steps_number;
  }
  return FALSE;
}