You are here

public function OpignoModuleController::moduleQuestionAnswerForm in Opigno module 3.x

Same name and namespace in other branches
  1. 8 src/Controller/OpignoModuleController.php \Drupal\opigno_module\Controller\OpignoModuleController::moduleQuestionAnswerForm()

Returns module question answer form.

1 string reference to 'OpignoModuleController::moduleQuestionAnswerForm'
opigno_module.routing.yml in ./opigno_module.routing.yml
opigno_module.routing.yml

File

src/Controller/OpignoModuleController.php, line 553

Class

OpignoModuleController
Class OpignoModuleController.

Namespace

Drupal\opigno_module\Controller

Code

public function moduleQuestionAnswerForm(OpignoModuleInterface $opigno_module, OpignoActivityInterface $opigno_activity) {
  $build = [
    '#theme' => 'opigno_module_activity_from',
  ];

  // Get activity link type.
  // If it is "flow", means activity answer displayed in Opigno common flow.
  // Otherwise activity answer link entered manually.
  $activity_link_type = OpignoGroupContext::getActivityLinkType();
  $is_flow = $activity_link_type == 'flow';
  OpignoGroupContext::removeActivityLinkType();
  $user = $this
    ->currentUser();
  $uid = $user
    ->id();
  $attempt = $opigno_module
    ->getModuleActiveAttempt($user);

  // Check if user have access on this step of LP.
  $gid = OpignoGroupContext::getCurrentGroupId();
  if ($gid && ($group = Group::load($gid))) {
    if (!$is_flow) {

      // Direct activity link.
      if ($attempt == NULL) {

        // No existing attempt, create new one.
        $attempt = UserModuleStatus::create([]);
        $attempt
          ->setModule($opigno_module);
        $attempt
          ->setFinished(0);
        $attempt
          ->save();
      }
    }

    // Get training latest certification timestamp.
    $latest_cert_date = LPStatus::getTrainingStartDate($group, $uid);

    // Load group courses substeps.
    $steps = LearningPathContent::getAllStepsOnlyModules($gid, $uid, FALSE, NULL, $latest_cert_date);
    if ($is_flow) {

      // Activity answer page displayed in Opigno common flow.
      $current_cid = OpignoGroupContext::getCurrentGroupContentId();
    }
    else {

      // Activity answer page link entered manually.
      foreach ($steps as $step) {
        if ($step['id'] == $opigno_module
          ->id()) {
          $current_cid = $step['cid'];
          break;
        }
      }
    }
  }
  else {
    return [];
  }

  // If not and group have we GuidedNavigation should redirect
  // his current point of group.
  if ($attempt === NULL && $is_flow) {
    if (OpignoGroupManagerController::getGuidedNavigation($group)) {
      $messenger = $this
        ->messenger();
      $this
        ->messenger()
        ->addMessage($this
        ->t("You can't access this step, first you need to finish required steps before."), $messenger::TYPE_WARNING);
      return $this
        ->redirect('opigno_learning_path.steps.start', [
        'group' => $gid,
      ]);
    }
    else {
      return $this
        ->redirect('opigno_module.take_module', [
        'group' => $gid,
        'opigno_module' => $opigno_module
          ->id(),
      ]);
    }
  }

  // Check if user try to load activity from another module.
  $module_activities = opigno_learning_path_get_module_activities($opigno_module
    ->id(), $uid, FALSE, $latest_cert_date);
  $activities_ids = array_keys($module_activities);

  // Check if module in skills system.
  $skills_activate = $opigno_module
    ->getSkillsActive();
  $moduleHandler = \Drupal::service('module_handler');
  if (!in_array($opigno_activity
    ->id(), $activities_ids) && (!$skills_activate || !$moduleHandler
    ->moduleExists('opigno_skills_system'))) {
    $query = \Drupal::database()
      ->select('user_module_status', 'u_m_s');
    $query
      ->fields('u_m_s', [
      'current_activity',
    ]);
    $query
      ->condition('u_m_s.user_id', $uid);
    $query
      ->condition('u_m_s.module', $opigno_module
      ->id());
    $query
      ->condition('u_m_s.learning_path', $gid);
    $query
      ->orderBy('u_m_s.id', 'DESC');
    $current_activity = $query
      ->execute()
      ->fetchField();

    // Set first activity as current if we can't get it from module status.
    if (is_null($current_activity)) {
      $current_activity = $module_activities[$activities_ids[0]]['activity_id'];
    }
    return $this
      ->redirect('opigno_module.group.answer_form', [
      'group' => $gid,
      'opigno_module' => $opigno_module
        ->id(),
      'opigno_activity' => $current_activity,
    ]);
  }
  if (!empty($steps)) {
    $group = Group::load($gid);

    // Get training guided navigation option.
    $freeNavigation = !OpignoGroupManagerController::getGuidedNavigation($group);
    foreach ($steps as $key => $step) {

      // Check if user manually entered the url.
      if ($current_cid == $step['cid']) {
        if ($opigno_module
          ->id() != $step['id']) {
          return $this
            ->redirect('opigno_learning_path.steps.next', [
            'group' => $gid,
            'parent_content' => $steps[$key]['cid'],
          ]);
        }
        break;
      }

      // Check if user is trying to skip mandatory activity.
      if (!$freeNavigation && ($step['mandatory'] == 1 && $step['required score'] > $step['best score']) || OpignoGroupManagerController::mustBeVisitedMeeting($step, $group)) {
        return $this
          ->redirect('opigno_learning_path.steps.next', [
          'group' => $gid,
          'parent_content' => $steps[$key]['cid'],
        ]);
      }
    }
  }
  if (!is_null($attempt)) {
    $existing_answer = $opigno_activity
      ->getUserAnswer($opigno_module, $attempt, $user);
    if (!is_null($existing_answer)) {
      $answer = $existing_answer;
    }
  }
  if (!isset($answer)) {
    $answer = static::entityTypeManager()
      ->getStorage('opigno_answer')
      ->create([
      'type' => $opigno_activity
        ->getType(),
      'activity' => $opigno_activity
        ->id(),
      'module' => $opigno_module
        ->id(),
    ]);
  }

  // Output rendered activity of the specified type.
  $build['activity_title'] = [
    '#markup' => $opigno_module
      ->label(),
  ];
  $build['activity_build'] = \Drupal::entityTypeManager()
    ->getViewBuilder('opigno_activity')
    ->view($opigno_activity, 'activity');

  // Output answer form of the same activity type.
  $build['form'] = $this
    ->entityFormBuilder()
    ->getForm($answer);
  return $build;
}