public function OpignoModuleController::moduleGetPrevious in Opigno module 3.x
Same name and namespace in other branches
- 8 src/Controller/OpignoModuleController.php \Drupal\opigno_module\Controller\OpignoModuleController::moduleGetPrevious()
This method get parent module for current module if exist.
1 string reference to 'OpignoModuleController::moduleGetPrevious'
File
- src/
Controller/ OpignoModuleController.php, line 1275
Class
- OpignoModuleController
- Class OpignoModuleController.
Namespace
Drupal\opigno_module\ControllerCode
public function moduleGetPrevious(OpignoModuleInterface $opigno_module) {
$uid = $this
->currentUser()
->id();
$cid = OpignoGroupContext::getCurrentGroupContentId();
$gid = OpignoGroupContext::getCurrentGroupId();
// Load group steps.
$steps = LearningPathContent::getAllStepsOnlyModules($gid, $uid);
// Find current & next step.
$count = count($steps);
$previous_step = NULL;
for ($i = 0; $i < $count; ++$i) {
if ($steps[$i]['cid'] == $cid) {
$previous_step = $steps[$i - 1];
break;
}
}
// Get module.
$previous_module = OpignoModule::load($previous_step['id']);
// Get all user module attempts.
$user_attempts = $previous_module
->getModuleAttempts($this
->currentUser());
// Get last active attempt.
$active_attempt = $previous_module
->getModuleActiveAttempt($this
->currentUser());
// Check Module attempts.
$allowed_attempts = $previous_module
->get('takes')->value;
if ($allowed_attempts > 0) {
// It means, that module attempts are limited.
// Need to check User attempts.
if (count($user_attempts) >= $allowed_attempts) {
// User has more attempts then allowed.
// Check for not finished attempt.
if ($active_attempt == NULL) {
// There is no not finished attempt.
\Drupal::messenger()
->addWarning($this
->t('Maximum attempts for this module reached.'));
return $this
->redirect('opigno_module.take_module', [
'group' => $gid,
'opigno_module' => $opigno_module
->id(),
]);
}
}
}
// Take into account randomization options.
$randomization = $previous_module
->getRandomization();
if ($randomization > 0) {
// @todo: notify user that he will lost his previous module results. Try do this with ajax.
\Drupal::messenger()
->addWarning($this
->t("You can't navigate back to the module with random activities order."));
return $this
->redirect('opigno_module.take_module', [
'group' => $gid,
'opigno_module' => $opigno_module
->id(),
]);
}
// Check if user allowed to resume.
$allow_resume = $previous_module
->getAllowResume();
// Continue param will exist only after previous answer form submit.
if (!$allow_resume) {
// Module can't be resumed.
\Drupal::messenger()
->addWarning($this
->t('Module resume is not allowed.'));
// After finish existing attempt we will redirect again
// to take page to start new attempt.
return $this
->redirect('opigno_module.take_module', [
'group' => $gid,
'opigno_module' => $opigno_module
->id(),
]);
}
// Get activities from the Module.
$activities = $previous_module
->getModuleActivities();
if (count($activities) > 0) {
if ($user_attempts == NULL) {
// User has not previous module attempts.
\Drupal::messenger()
->addWarning($this
->t("You can't navigate back to the module that you don't attempt."));
return $this
->redirect('opigno_module.take_module', [
'group' => $gid,
'opigno_module' => $opigno_module
->id(),
]);
}
if ($active_attempt == NULL) {
// Previous module is finished.
// Get last finished attempt and make unfinished.
/* @var \Drupal\opigno_module\Entity\UserModuleStatus $last_attempt */
$last_attempt = end($user_attempts);
// Set current activity.
$current_activity = $last_attempt
->getLastActivity();
$last_attempt
->setCurrentActivity($current_activity);
// Set last activity.
$last_activity_info = array_slice($activities, -2, 1, TRUE);
$last_activity = OpignoActivity::load(key($last_activity_info));
$last_attempt
->setLastActivity($last_activity);
$last_attempt
->setFinished(0);
$last_attempt
->save();
}
// Update module status for current module.
$current_module_attempt = $opigno_module
->getModuleActiveAttempt($this
->currentUser());
$current_module_attempt->last_activity->target_id = NULL;
$current_module_attempt
->save();
// Update content id.
OpignoGroupContext::setCurrentContentId($previous_step['cid']);
// Redirect to the previous module.
return $this
->redirect('opigno_module.take_module', [
'group' => $gid,
'opigno_module' => $previous_module
->id(),
], [
'query' => [
'previous' => TRUE,
],
]);
}
// Module can't be navigated.
\Drupal::messenger()
->addWarning($this
->t('Can not navigate to previous module.'));
return $this
->redirect('opigno_module.take_module', [
'group' => $gid,
'opigno_module' => $opigno_module
->id(),
]);
}