From 3246ed335d2a9125f43f995807103bf9e9477d8c Mon Sep 17 00:00:00 2001 From: tjhunt Date: Mon, 18 Dec 2006 18:07:49 +0000 Subject: [PATCH] MDL-7975 - Make import and export of truefalse questions more reliable. Merged from MOODLE_17_STABLE. --- lang/en_utf8/quiz.php | 1 + question/format/xml/format.php | 50 +++++++++++++++++++++++----------- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/lang/en_utf8/quiz.php b/lang/en_utf8/quiz.php index dcd9f0c842..305f3b8c32 100644 --- a/lang/en_utf8/quiz.php +++ b/lang/en_utf8/quiz.php @@ -514,6 +514,7 @@ $string['toomanyrandom'] = 'The number of random questions required is more than $string['top'] = 'Top'; $string['true'] = 'True'; $string['truefalse'] = 'True/False'; +$string['truefalseimporterror'] = 'Warning: The True/False question \'$a->questiontext\' could not be imported properly. It was not clear whether the correct answer is True of False. The question has been imported assuming that the answer is \'$a->answer\'. If this is not correct, you will need to edit the question.'; $string['type'] = 'Type'; $string['unfinished'] = 'open'; $string['ungraded'] = 'Ungraded'; diff --git a/question/format/xml/format.php b/question/format/xml/format.php index e3ec77e680..a5af6a12ad 100755 --- a/question/format/xml/format.php +++ b/question/format/xml/format.php @@ -215,22 +215,35 @@ class qformat_xml extends qformat_default { $qo->qtype = TRUEFALSE; // get answer info - $answers = $question['#']['answer']; - $fraction0 = $answers[0]['@']['fraction']; - $feedback0 = $this->import_text($answers[0]['#']['feedback'][0]['#']['text']); - $fraction1 = $answers[1]['@']['fraction']; - $feedback1 = $this->import_text($answers[1]['#']['feedback'][0]['#']['text']); - - // sort out which is true and build object accordingly - if ($fraction0==100) { // then 0 index is true - $qo->answer = 1; - $qo->feedbacktrue=$feedback0; - $qo->feedbackfalse=$feedback1; + // + // In the past, it used to be assumed that the two answers were in the file + // true first, then false. Howevever that was not always true. Now, we + // try to match on the answer text, but in old exports, this will be a localised + // string, so if we don't find true or false, we fall back to the old system. + $first = true; + $warning = false; + foreach ($question['#']['answer'] as $answer) { + $answertext = $this->import_text($answer['#']['text']); + $feedback = $this->import_text($answer['#']['feedback'][0]['#']['text']); + if ($answertext != 'true' && $answertext != 'false') { + $warning = true; + $answertext = $first ? 'true' : 'false'; // Old style file, assume order is true/false. + } + if ($answertext == 'true') { + $qo->answer = ($answer['@']['fraction'] == 100); + $qo->feedbacktrue = $feedback; + } else { + $qo->answer = ($answer['@']['fraction'] != 100); + $qo->feedbackfalse = $feedback; + } + $first = false; } - else { - $qo->answer = 0; - $qo->feedbacktrue = $feedback1; - $qo->feedbackfalse = $feedback0; + + if ($warning) { + $a = new stdClass; + $a->questiontext = stripslashes($qo->questiontext); + $a->answer = get_string($qo->answer ? 'true' : 'false', 'quiz'); + notify(get_string('truefalseimporterror', 'quiz', $a)); } return $qo; @@ -700,8 +713,13 @@ class qformat_xml extends qformat_default { case TRUEFALSE: foreach ($question->options->answers as $answer) { $fraction_pc = round( $answer->fraction * 100 ); + if ($answer->id == $question->options->trueanswer) { + $answertext = 'true'; + } else { + $answertext = 'false'; + } $expout .= " \n"; - $expout .= $this->writetext(strtolower($answer->answer),3)."\n"; + $expout .= $this->writetext($answertext, 3) . "\n"; $expout .= " \n"; $expout .= $this->writetext( $answer->feedback,4,false ); $expout .= " \n"; -- 2.39.5