Skip to content
Browse files

MDL-34050 Lesson Module: improved matching question logic for checkin…

…g user response. This patch will also resolved MDL-36343.
  • Loading branch information...
1 parent f7109ce commit 7f5290099fbd930db05001430f25eb4c4ef4b3c6 @rwijaya rwijaya committed Jan 11, 2013
Showing with 18 additions and 17 deletions.
  1. +18 −17 mod/lesson/pagetypes/matching.php
View
35 mod/lesson/pagetypes/matching.php
@@ -72,16 +72,18 @@ protected function make_answer_form($attempt=null) {
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 @@ public function check_answer() {
$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 .= '<br />'.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 .= '<br />'.format_text($answer->answer, $answer->answerformat, $formattextdefoptions).' = '.$answers[$value]->response;
+ if ($id == $value) {
$hits++;
}
}
}
+
$result->userresponse = implode(",", $userresponse);
if ($hits == count($answers)) {

0 comments on commit 7f52900

Please sign in to comment.
Something went wrong with that request. Please try again.