MDL-58668 mod_lesson: Fix how multi answers are processed

The module was choosing as incorrect the first possible incorrect
answer instead the first student incorrect answer.
In the patch I also refactored the foreach loop to avoid code
duplication.
This commit is contained in:
Juan Leyva 2017-04-21 11:26:43 +02:00
parent 6d14355ce8
commit d5eefdb6a0

View File

@ -178,71 +178,49 @@ class lesson_page_type_multichoice extends lesson_page {
}
$correctpageid = null;
$wrongpageid = null;
// this is for custom scores. If score on answer is positive, it is correct
if ($this->lesson->custom) {
$ncorrect = 0;
$nhits = 0;
foreach ($answers as $answer) {
if ($answer->score > 0) {
$ncorrect++;
foreach ($studentanswers as $answerid) {
if ($answerid == $answer->id) {
$nhits++;
// Iterate over all the possible answers.
foreach ($answers as $answer) {
if ($this->lesson->custom) {
$iscorrectanswer = $answer->score > 0;
} else {
$iscorrectanswer = $this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto);
}
// Iterate over all the student answers to check if he selected the current possible answer.
foreach ($studentanswers as $answerid) {
if ($answerid == $answer->id) {
if ($iscorrectanswer) {
$nhits++;
} else {
// Always jump to the page related to the student's first wrong answer.
if (!isset($wrongpageid)) {
// Leave in its "raw" state - will be converted into a proper page id later.
$wrongpageid = $answer->jumpto;
}
// Save the answer id for scoring.
if ($wronganswerid == 0) {
$wronganswerid = $answer->id;
}
}
// save the first jumpto page id, may be needed!...
if (!isset($correctpageid)) {
// leave in its "raw" state - will converted into a proper page id later
$correctpageid = $answer->jumpto;
}
// save the answer id for scoring
if ($correctanswerid == 0) {
$correctanswerid = $answer->id;
}
} else {
// save the first jumpto page id, may be needed!...
if (!isset($wrongpageid)) {
// leave in its "raw" state - will converted into a proper page id later
$wrongpageid = $answer->jumpto;
}
// save the answer id for scoring
if ($wronganswerid == 0) {
$wronganswerid = $answer->id;
}
}
}
} else {
foreach ($answers as $answer) {
if ($this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) {
$ncorrect++;
foreach ($studentanswers as $answerid) {
if ($answerid == $answer->id) {
$nhits++;
}
}
// save the first jumpto page id, may be needed!...
if (!isset($correctpageid)) {
// leave in its "raw" state - will converted into a proper page id later
$correctpageid = $answer->jumpto;
}
// save the answer id for scoring
if ($correctanswerid == 0) {
$correctanswerid = $answer->id;
}
} else {
// save the first jumpto page id, may be needed!...
if (!isset($wrongpageid)) {
// leave in its "raw" state - will converted into a proper page id later
$wrongpageid = $answer->jumpto;
}
// save the answer id for scoring
if ($wronganswerid == 0) {
$wronganswerid = $answer->id;
}
if ($iscorrectanswer) {
$ncorrect++;
// Save the first jumpto page id, may be needed!
if (!isset($correctpageid)) {
// Leave in its "raw" state - will be converted into a proper page id later.
$correctpageid = $answer->jumpto;
}
// Save the answer id for scoring.
if ($correctanswerid == 0) {
$correctanswerid = $answer->id;
}
}
}
if ((count($studentanswers) == $ncorrect) and ($nhits == $ncorrect)) {
$result->correctanswer = true;
$result->response = implode('<br />', $responses);