Exercise: Fix error in calculated score with "Global multiple answer" - refs BT#20518

pull/4573/head
Christian 3 years ago
parent c5faf6994e
commit 39ab49fb2b
  1. 21
      main/exercise/exercise.class.php

@ -3900,6 +3900,16 @@ class Exercise
$answerMatching[$real_answer['iid']] = $real_answer['answer']; $answerMatching[$real_answer['iid']] = $real_answer['answer'];
} }
// Get correct answers for multiple answers.
$sql = "SELECT iid
FROM $table_ans
WHERE question_id = $questionId AND correct = 1";
$resMAnswer = Database::query($sql);
$correctMultipleAnswers = [];
while ($rowMAanswer = Database::fetch_array($resMAnswer)) {
$correctMultipleAnswers[] = $rowMAanswer['iid'];
}
// Get first answer needed for global question, no matter the answer shuffle option; // Get first answer needed for global question, no matter the answer shuffle option;
$firstAnswer = []; $firstAnswer = [];
if ($answerType == MULTIPLE_ANSWER_COMBINATION || if ($answerType == MULTIPLE_ANSWER_COMBINATION ||
@ -4155,6 +4165,7 @@ class Exercise
$totalScore += $answerWeighting; $totalScore += $answerWeighting;
break; break;
case GLOBAL_MULTIPLE_ANSWER: case GLOBAL_MULTIPLE_ANSWER:
$validAnswer = false;
if ($from_database) { if ($from_database) {
$choice = []; $choice = [];
$sql = "SELECT answer FROM $TBL_TRACK_ATTEMPT $sql = "SELECT answer FROM $TBL_TRACK_ATTEMPT
@ -4163,14 +4174,20 @@ class Exercise
while ($row = Database::fetch_array($resultans)) { while ($row = Database::fetch_array($resultans)) {
$choice[$row['answer']] = 1; $choice[$row['answer']] = 1;
} }
if (!empty($choice) && count($choice) == count($correctMultipleAnswers)) {
$validAnswer = (0 == count(array_diff(array_keys($choice), $correctMultipleAnswers)));
}
$studentChoice = isset($choice[$answerAutoId]) ? $choice[$answerAutoId] : null; $studentChoice = isset($choice[$answerAutoId]) ? $choice[$answerAutoId] : null;
$real_answers[$answerId] = (bool) $studentChoice; $real_answers[$answerId] = (bool) $studentChoice;
if ($studentChoice) { if ($studentChoice && $validAnswer) {
$questionScore += $answerWeighting; $questionScore += $answerWeighting;
} }
} else { } else {
if (!empty($choice) && count($choice) == count($correctMultipleAnswers)) {
$validAnswer = (0 == count(array_diff(array_keys($choice), $correctMultipleAnswers)));
}
$studentChoice = isset($choice[$answerAutoId]) ? $choice[$answerAutoId] : null; $studentChoice = isset($choice[$answerAutoId]) ? $choice[$answerAutoId] : null;
if (isset($studentChoice)) { if (isset($studentChoice) && $validAnswer) {
$questionScore += $answerWeighting; $questionScore += $answerWeighting;
} }
$real_answers[$answerId] = (bool) $studentChoice; $real_answers[$answerId] = (bool) $studentChoice;

Loading…
Cancel
Save