From 2e48a424190106030938415f5d2dc36abf920ba0 Mon Sep 17 00:00:00 2001 From: Rossiani Wijaya Date: Fri, 11 Jan 2013 12:00:13 +0800 Subject: [PATCH] MDL-34050 Lesson Module: improved matching question logic for checking user response. This patch will also resolved MDL-36343. --- mod/lesson/pagetypes/matching.php | 35 ++++++++++++++++--------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/mod/lesson/pagetypes/matching.php b/mod/lesson/pagetypes/matching.php index 9cf8f3549fc..d27ceccfa34 100644 --- a/mod/lesson/pagetypes/matching.php +++ b/mod/lesson/pagetypes/matching.php @@ -72,16 +72,18 @@ class lesson_page_type_matching extends lesson_page { foreach ($answers as $answer) { // get all the response if ($answer->response != NULL) { - $responses[] = trim($answer->response); + $responses[$answer->id] = trim($answer->response); } } $responseoptions = array(''=>get_string('choosedots')); if (!empty($responses)) { - shuffle($responses); - $responses = array_unique($responses); - foreach ($responses as $response) { - $responseoptions[htmlspecialchars(trim($response))] = $response; + $shuffleresponses = $responses; + shuffle($shuffleresponses); + $shuffleresponses = array_unique($shuffleresponses); + foreach ($shuffleresponses as $response) { + $key = array_search($response, $responses); + $responseoptions[$key] = $response; } } if (isset($USER->modattempts[$this->lesson->id]) && !empty($attempt->useranswer)) { @@ -171,27 +173,26 @@ class lesson_page_type_matching extends lesson_page { $wrong = array_shift($answers); foreach ($answers as $key=>$answer) { - if ($answer->answer === '' or $answer->response === '') { - // incomplete option! - unset($answers[$key]); + if ($answer->answer !== '' or $answer->response !== '') { + $answers[$answer->id] = $answer; } + unset($answers[$key]); } // get he users exact responses for record keeping $hits = 0; $userresponse = array(); - foreach ($response as $key => $value) { - foreach($answers as $answer) { - if ($value === $answer->response) { - $userresponse[] = $answer->id; - } - if ((int)$answer->id === (int)$key) { - $result->studentanswer .= '
'.format_text($answer->answer, $answer->answerformat, $formattextdefoptions).' = '.$value; - } - if ((int)$answer->id === (int)$key and $value === $answer->response) { + foreach ($response as $id => $value) { + $userresponse[] = $value; + // Make sure the user's answer is exist in question's answer + if (array_key_exists($id, $answers)) { + $answer = $answers[$id]; + $result->studentanswer .= '
'.format_text($answer->answer, $answer->answerformat, $formattextdefoptions).' = '.$answers[$value]->response; + if ($id == $value) { $hits++; } } } + $result->userresponse = implode(",", $userresponse); if ($hits == count($answers)) { -- 2.43.0