]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-7975 - Make import and export of truefalse questions more reliable. Merged from...
authortjhunt <tjhunt>
Mon, 18 Dec 2006 18:07:49 +0000 (18:07 +0000)
committertjhunt <tjhunt>
Mon, 18 Dec 2006 18:07:49 +0000 (18:07 +0000)
lang/en_utf8/quiz.php
question/format/xml/format.php

index dcd9f0c842d38f9bd0a46bdc057f521edcea6ab8..305f3b8c3264d0c797459772cadbd7b0fef9e5eb 100644 (file)
@@ -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'] = '<b>Warning</b>: 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';
index e3ec77e680e44907d0d1fc022b22376fa184968c..a5af6a12ad21e0b79022684ccd9af781ab2076e4 100755 (executable)
@@ -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 .= "    <answer fraction=\"$fraction_pc\">\n";
-                $expout .= $this->writetext(strtolower($answer->answer),3)."\n";
+                $expout .= $this->writetext($answertext, 3) . "\n";
                 $expout .= "      <feedback>\n";
                 $expout .= $this->writetext( $answer->feedback,4,false );
                 $expout .= "      </feedback>\n";