diff --git a/main/exercice/answer.class.php b/main/exercice/answer.class.php index d293049938..c64d2a93ee 100755 --- a/main/exercice/answer.class.php +++ b/main/exercice/answer.class.php @@ -832,4 +832,25 @@ class Answer "; } + /** + * Check if a answer is correct by an answer auto id + * @param $needle int The answer auto id + * @return bool + */ + public function isCorrectByAutoId($needle) + { + $key = 0; + + foreach ($this->autoId as $autoIdKey => $autoId) { + if ($autoId == $needle) { + $key = $autoIdKey; + } + } + + if (!$key) { + return false; + } + + return $this->isCorrect($key) ? true : false; + } } diff --git a/main/exercice/exercise.class.php b/main/exercice/exercise.class.php index b5face4dea..099ee49f38 100755 --- a/main/exercice/exercise.class.php +++ b/main/exercice/exercise.class.php @@ -8267,4 +8267,38 @@ class Exercise } return 1; } + + /** + * Get the correct answers in all attempts + * @param int $learnPathId + * @param int $learnPathItemId + * @return array + */ + public function getCorrectAnswersInAllAttempts($learnPathId = 0, $learnPathItemId = 0) + { + $attempts = Event::getExerciseResultsByUser( + api_get_user_id(), + $this->id, + api_get_course_int_id(), + api_get_session_id(), + $learnPathId, + $learnPathItemId, + 'asc' + ); + + $corrects = []; + + foreach ($attempts as $attempt) { + foreach ($attempt['question_list'] as $answer) { + $objAnswer = new Answer($answer['question_id']); + $isCorrect = $objAnswer->isCorrectByAutoId($answer['answer']); + + if ($isCorrect) { + $corrects[$answer['question_id']][] = $answer; + } + } + } + + return $corrects; + } } diff --git a/main/exercice/exercise_result.php b/main/exercice/exercise_result.php index 2ce42e6ee0..c3514d3648 100755 --- a/main/exercice/exercise_result.php +++ b/main/exercice/exercise_result.php @@ -144,6 +144,26 @@ if ($objExercise->selectAttempts() > 0) { Display::display_footer(); } exit; + } else { + $attempt_count++; + $remainingAttempts = $objExercise->selectAttempts() - $attempt_count; + + if ($remainingAttempts) { + $attemptButton = Display::toolbarButton( + get_lang('AnotherAttempt'), + api_get_patth(WEB_CODE_PATH) . 'exercice/overview.php?' . api_get_cidreq() . '&' . http_build_query([ + 'exerciseId' => $objExercise->id + ]), + 'pencil-square-o', + 'info' + ); + $attemptMessage = sprintf(get_lang('RemainingXAttempts'), $remainingAttempts); + + Display::display_normal_message( + sprintf("
%s
%s", $attemptMessage, $attemptButton), + false + ); + } } } diff --git a/main/exercice/exercise_submit.php b/main/exercice/exercise_submit.php index f4a779cc81..073f3e4f42 100755 --- a/main/exercice/exercise_submit.php +++ b/main/exercice/exercise_submit.php @@ -1116,7 +1116,17 @@ if (!empty($error)) { } } - $user_choice = isset($attempt_list[$questionId]) ? $attempt_list[$questionId] : null; + $user_choice = null; + + if (isset($attempt_list[$questionId])) { + $user_choice = $attempt_list[$questionId]; + } elseif ($objExercise->saveCorrectAnswers) { + $correctAnswers = $objExercise->getCorrectAnswersInAllAttempts($learnpath_id, $learnpath_item_id); + + if (isset($correctAnswers[$questionId])) { + $user_choice = $correctAnswers[$questionId]; + } + } $remind_highlight = '';