From 8380311af6296973717911031f5063d5b81fd6c0 Mon Sep 17 00:00:00 2001 From: Francis Devine Date: Fri, 10 Nov 2023 13:27:07 +1300 Subject: [PATCH] MDL-79937 mod_lesson: Fix matching pagetype question matching The addition of text format on the output of the answer responses broke the matching later when comparing the valid answer against the sent response, as the answer response was not correspondingly formatted. I decided to use the un formatted answer response as the key, to keep it as close as identical to prior behaviour --- mod/lesson/pagetypes/matching.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/mod/lesson/pagetypes/matching.php b/mod/lesson/pagetypes/matching.php index 17c4531c917..930be9c588e 100644 --- a/mod/lesson/pagetypes/matching.php +++ b/mod/lesson/pagetypes/matching.php @@ -79,19 +79,22 @@ class lesson_page_type_matching extends lesson_page { $answers[$getanswer->id] = $getanswer; } + // Calculate the text for the dropdown, keyed by the non formatted version. $responses = array(); foreach ($answers as $answer) { - // get all the response + // Get all the response. if ($answer->response != null) { - $responses[] = format_text(trim($answer->response)); + $responses[trim($answer->response)] = format_text(trim($answer->response)); } } - $responseoptions = array(''=>get_string('choosedots')); + // Now shuffle the answers to randomise the order of the items in the dropdown. + $responseoptions = ['' => get_string('choosedots')]; if (!empty($responses)) { - shuffle($responses); - foreach ($responses as $response) { - $responseoptions[htmlspecialchars($response, ENT_COMPAT)] = $response; + $keys = array_keys($responses); + shuffle($keys); + foreach ($keys as $key) { + $responseoptions[$key] = $responses[$key]; } } if (isset($USER->modattempts[$this->lesson->id]) && !empty($attempt->useranswer)) { @@ -211,7 +214,6 @@ class lesson_page_type_matching extends lesson_page { $result->noanswer = true; return $result; } - $value = htmlspecialchars_decode($value, ENT_COMPAT); $userresponse[] = $value; // Make sure the user's answer exists in question's answer if (array_key_exists($id, $answers)) {