Allow save correct answers for the next attempt - refs BT#11024

ofaj
Angel Fernando Quiroz Campos 10 years ago
parent 879d991f15
commit da418e6f1a
  1. 21
      main/exercice/answer.class.php
  2. 34
      main/exercice/exercise.class.php
  3. 20
      main/exercice/exercise_result.php
  4. 12
      main/exercice/exercise_submit.php

@ -832,4 +832,25 @@ class Answer
</script>";
}
/**
* 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;
}
}

@ -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;
}
}

@ -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("<p>%s</p> %s", $attemptMessage, $attemptButton),
false
);
}
}
}

@ -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 = '';

Loading…
Cancel
Save