You are here

public static function LearningPathValidator::stepsValidate in Opigno Learning path 8

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

Redirect user if one of learning path steps aren't completed.

4 calls to LearningPathValidator::stepsValidate()
LearningPathContentController::coursesIndex in src/Controller/LearningPathContentController.php
Root page for angular app.
LearningPathContentController::modulesIndex in src/Controller/LearningPathContentController.php
Root page for angular app.
LearningPathMembersForm::buildForm in src/Form/LearningPathMembersForm.php
Form constructor.
LearningPathStepsController::contentSteps in src/Controller/LearningPathStepsController.php
Steps.

File

src/LearningPathValidator.php, line 114

Class

LearningPathValidator
Class LearningPathValidator.

Namespace

Drupal\opigno_learning_path

Code

public static function stepsValidate(Group $group) {
  $messenger = \Drupal::messenger();
  $current_route = \Drupal::service('current_route_match');
  $current_route_name = $current_route
    ->getRouteName();
  $current_route_step = (int) $current_route
    ->getParameter('current');

  // Validate only group type "learning_path".
  $group_type = opigno_learning_path_get_group_type();
  if ($group_type !== 'learning_path') {
    return TRUE;
  }

  // Steps 1 and 2 don't need validation.
  $current_step = (int) opigno_learning_path_get_current_step();
  if ($current_step === 1 || $current_step === 2) {
    return TRUE;
  }
  $redirect_step = NULL;

  // Get all training content.
  $contents = OpignoGroupManagedContent::loadByGroupId($group
    ->id());
  if (empty($contents)) {

    // Learning path is empty.
    $redirect_step = 2;
    $messenger
      ->addError(t('Please, add some course or module!'));
  }
  else {

    // Learning path is created and not empty.
    // Skip 4 step if learning path hasn't any courses.
    $group_courses = $group
      ->getContent('subgroup:opigno_course');
    if (empty($group_courses) && $current_route_name === 'opigno_learning_path.learning_path_courses') {
      $redirect_step = 4;
    }

    // Check if training has at least one mandatory entity.
    $has_mandatory = self::hasMandatoryItem($contents);
    if (!$has_mandatory) {
      $redirect_step = 2;
      $messenger
        ->addError(t('At least one entity must be mandatory!'));
    }
    elseif ($current_route_name != 'opigno_learning_path.learning_path_modules' && $current_route_step != 2 && $current_route_step != 3) {
      foreach ($contents as $content) {
        $type_id = $content
          ->getGroupContentTypeId();
        switch ($type_id) {
          case 'ContentTypeModule':
            $module_id = $content
              ->getEntityId();
            $module = OpignoModule::load($module_id);
            static::moduleValidate($module, $redirect_step);
            break;
          case 'ContentTypeCourse':
            $course_id = $content
              ->getEntityId();
            static::courseValidate($course_id, $redirect_step);
            break;
        }
      }
    }
  }
  if (isset($redirect_step)) {
    $redirect_route_name = array_search($redirect_step, opigno_learning_path_get_routes_steps());
    if ($redirect_route_name === $current_route_name) {

      // Prevent redirect from current route.
      return FALSE;
    }

    // Redirect to incomplete step.
    $response = new RedirectResponse(Url::fromRoute($redirect_route_name, [
      'group' => $group
        ->id(),
    ])
      ->toString());
    return $response
      ->send();
  }
  return TRUE;
}