MDL-34306 gift question format: allow import of general feedback
[moodle.git] / question / format / gift / format.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * GIFT format question importer/exporter.
19  *
20  * @package    qformat
21  * @subpackage gift
22  * @copyright  2003 Paul Tsuchido Shew
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
27 defined('MOODLE_INTERNAL') || die();
30 /**
31  * The GIFT import filter was designed as an easy to use method
32  * for teachers writing questions as a text file. It supports most
33  * question types and the missing word format.
34  *
35  * Multiple Choice / Missing Word
36  *     Who's buried in Grant's tomb?{~Grant ~Jefferson =no one}
37  *     Grant is {~buried =entombed ~living} in Grant's tomb.
38  * True-False:
39  *     Grant is buried in Grant's tomb.{FALSE}
40  * Short-Answer.
41  *     Who's buried in Grant's tomb?{=no one =nobody}
42  * Numerical
43  *     When was Ulysses S. Grant born?{#1822:5}
44  * Matching
45  *     Match the following countries with their corresponding
46  *     capitals.{=Canada->Ottawa =Italy->Rome =Japan->Tokyo}
47  *
48  * Comment lines start with a double backslash (//).
49  * Optional question names are enclosed in double colon(::).
50  * Answer feedback is indicated with hash mark (#).
51  * Percentage answer weights immediately follow the tilde (for
52  * multiple choice) or equal sign (for short answer and numerical),
53  * and are enclosed in percent signs (% %). See docs and examples.txt for more.
54  *
55  * This filter was written through the collaboration of numerous
56  * members of the Moodle community. It was originally based on
57  * the missingword format, which included code from Thomas Robb
58  * and others. Paul Tsuchido Shew wrote this filter in December 2003.
59  *
60  * @copyright  2003 Paul Tsuchido Shew
61  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
62  */
63 class qformat_gift extends qformat_default {
65     public function provide_import() {
66         return true;
67     }
69     public function provide_export() {
70         return true;
71     }
73     public function export_file_extension() {
74         return '.txt';
75     }
77     protected function answerweightparser(&$answer) {
78         $answer = substr($answer, 1);                        // removes initial %
79         $end_position  = strpos($answer, "%");
80         $answer_weight = substr($answer, 0, $end_position);  // gets weight as integer
81         $answer_weight = $answer_weight/100;                 // converts to percent
82         $answer = substr($answer, $end_position+1);          // removes comment from answer
83         return $answer_weight;
84     }
86     protected function commentparser($answer, $defaultformat) {
87         $bits = explode('#', $answer, 2);
88         $ans = $this->parse_text_with_format(trim($bits[0]), $defaultformat);
89         if (count($bits) > 1) {
90             $feedback = $this->parse_text_with_format(trim($bits[1]), $defaultformat);
91         } else {
92             $feedback = array('text' => '', 'format' => $defaultformat, 'files' => array());
93         }
94         return array($ans, $feedback);
95     }
97     protected function split_truefalse_comment($answer, $defaultformat) {
98         $bits = explode('#', $answer, 3);
99         $ans = $this->parse_text_with_format(trim($bits[0]), $defaultformat);
100         if (count($bits) > 1) {
101             $wrongfeedback = $this->parse_text_with_format(trim($bits[1]), $defaultformat);
102         } else {
103             $wrongfeedback = array('text' => '', 'format' => $defaultformat, 'files' => array());
104         }
105         if (count($bits) > 2) {
106             $rightfeedback = $this->parse_text_with_format(trim($bits[2]), $defaultformat);
107         } else {
108             $rightfeedback = array('text' => '', 'format' => $defaultformat, 'files' => array());
109         }
110         return array($ans, $wrongfeedback, $rightfeedback);
111     }
113     protected function escapedchar_pre($string) {
114         //Replaces escaped control characters with a placeholder BEFORE processing
116         $escapedcharacters = array("\\:",    "\\#",    "\\=",    "\\{",    "\\}",    "\\~",    "\\n"  );  //dlnsk
117         $placeholders      = array("&&058;", "&&035;", "&&061;", "&&123;", "&&125;", "&&126;", "&&010");  //dlnsk
119         $string = str_replace("\\\\", "&&092;", $string);
120         $string = str_replace($escapedcharacters, $placeholders, $string);
121         $string = str_replace("&&092;", "\\", $string);
122         return $string;
123     }
125     protected function escapedchar_post($string) {
126         //Replaces placeholders with corresponding character AFTER processing is done
127         $placeholders = array("&&058;", "&&035;", "&&061;", "&&123;", "&&125;", "&&126;", "&&010"); //dlnsk
128         $characters   = array(":",     "#",      "=",      "{",      "}",      "~",      "\n"  ); //dlnsk
129         $string = str_replace($placeholders, $characters, $string);
130         return $string;
131     }
133     protected function check_answer_count($min, $answers, $text) {
134         $countanswers = count($answers);
135         if ($countanswers < $min) {
136             $this->error(get_string('importminerror', 'qformat_gift'), $text);
137             return false;
138         }
140         return true;
141     }
143     protected function parse_text_with_format($text, $defaultformat = FORMAT_MOODLE) {
144         $result = array(
145             'text' => $text,
146             'format' => $defaultformat,
147             'files' => array(),
148         );
149         if (strpos($text, '[') === 0) {
150             $formatend = strpos($text, ']');
151             $result['format'] = $this->format_name_to_const(substr($text, 1, $formatend - 1));
152             if ($result['format'] == -1) {
153                 $result['format'] = $defaultformat;
154             } else {
155                 $result['text'] = substr($text, $formatend + 1);
156             }
157         }
158         $result['text'] = trim($this->escapedchar_post($result['text']));
159         return $result;
160     }
162     public function readquestion($lines) {
163     // Given an array of lines known to define a question in this format, this function
164     // converts it into a question object suitable for processing and insertion into Moodle.
166         $question = $this->defaultquestion();
167         $comment = NULL;
168         // define replaced by simple assignment, stop redefine notices
169         $gift_answerweight_regex = '/^%\-*([0-9]{1,2})\.?([0-9]*)%/';
171         // REMOVED COMMENTED LINES and IMPLODE
172         foreach ($lines as $key => $line) {
173             $line = trim($line);
174             if (substr($line, 0, 2) == '//') {
175                 $lines[$key] = ' ';
176             }
177         }
179         $text = trim(implode(' ', $lines));
181         if ($text == '') {
182             return false;
183         }
185         // Substitute escaped control characters with placeholders
186         $text = $this->escapedchar_pre($text);
188         // Look for category modifier
189         if (preg_match('~^\$CATEGORY:~', $text)) {
190             // $newcategory = $matches[1];
191             $newcategory = trim(substr($text, 10));
193             // build fake question to contain category
194             $question->qtype = 'category';
195             $question->category = $newcategory;
196             return $question;
197         }
199         // QUESTION NAME parser
200         if (substr($text, 0, 2) == '::') {
201             $text = substr($text, 2);
203             $namefinish = strpos($text, '::');
204             if ($namefinish === false) {
205                 $question->name = false;
206                 // name will be assigned after processing question text below
207             } else {
208                 $questionname = substr($text, 0, $namefinish);
209                 $question->name = trim($this->escapedchar_post($questionname));
210                 $text = trim(substr($text, $namefinish+2)); // Remove name from text
211             }
212         } else {
213             $question->name = false;
214         }
216         // Find the answer section.
217         $answerstart = strpos($text, '{');
218         $answerfinish = strpos($text, '}');
220         $description = false;
221         if ($answerstart === false && $answerfinish === false) {
222             // No answer means it's a description.
223             $description = true;
224             $answertext = '';
225             $answerlength = 0;
227         } else if ($answerstart === false || $answerfinish === false) {
228             $this->error(get_string('braceerror', 'qformat_gift'), $text);
229             return false;
231         } else {
232             $answerlength = $answerfinish - $answerstart;
233             $answertext = trim(substr($text, $answerstart + 1, $answerlength - 1));
234         }
236         // Format the question text, without answer, inserting "_____" as necessary.
237         if ($description) {
238             $questiontext = $text;
239         } else if (substr($text, -1) == "}") {
240             // No blank line if answers follow question, outside of closing punctuation.
241             $questiontext = substr_replace($text, "", $answerstart, $answerlength + 1);
242         } else {
243             // Inserts blank line for missing word format.
244             $questiontext = substr_replace($text, "_____", $answerstart, $answerlength + 1);
245         }
247         // Look to see if there is any general feedback.
248         $gfseparator = strrpos($answertext, '####');
249         if ($gfseparator === false) {
250             $generalfeedback = '';
251         } else {
252             $generalfeedback = substr($answertext, $gfseparator + 4);
253             $answertext = trim(substr($answertext, 0, $gfseparator));
254         }
256         // Get questiontext format from questiontext.
257         $text = $this->parse_text_with_format($questiontext);
258         $question->questiontextformat = $text['format'];
259         $question->questiontext = $text['text'];
261         // Get generalfeedback format from questiontext.
262         $text = $this->parse_text_with_format($generalfeedback, $question->questiontextformat);
263         $question->generalfeedback = $text['text'];
264         $question->generalfeedbackformat = $text['format'];
266         // set question name if not already set
267         if ($question->name === false) {
268             $question->name = $question->questiontext;
269         }
271         // ensure name is not longer than 250 characters
272         $question->name = shorten_text($question->name, 200);
273         $question->name = strip_tags(substr($question->name, 0, 250));
275         // determine QUESTION TYPE
276         $question->qtype = NULL;
278         // give plugins first try
279         // plugins must promise not to intercept standard qtypes
280         // MDL-12346, this could be called from lesson mod which has its own base class =(
281         if (method_exists($this, 'try_importing_using_qtypes') && ($try_question = $this->try_importing_using_qtypes($lines, $question, $answertext))) {
282             return $try_question;
283         }
285         if ($description) {
286             $question->qtype = DESCRIPTION;
288         } else if ($answertext == '') {
289             $question->qtype = ESSAY;
291         } else if ($answertext{0} == '#') {
292             $question->qtype = NUMERICAL;
294         } else if (strpos($answertext, '~') !== false)  {
295             // only Multiplechoice questions contain tilde ~
296             $question->qtype = MULTICHOICE;
298         } else if (strpos($answertext, '=')  !== false
299                 && strpos($answertext, '->') !== false) {
300             // only Matching contains both = and ->
301             $question->qtype = MATCH;
303         } else { // either TRUEFALSE or SHORTANSWER
305             // TRUEFALSE question check
306             $truefalse_check = $answertext;
307             if (strpos($answertext, '#') > 0) {
308                 // strip comments to check for TrueFalse question
309                 $truefalse_check = trim(substr($answertext, 0, strpos($answertext,"#")));
310             }
312             $valid_tf_answers = array('T', 'TRUE', 'F', 'FALSE');
313             if (in_array($truefalse_check, $valid_tf_answers)) {
314                 $question->qtype = TRUEFALSE;
316             } else { // Must be SHORTANSWER
317                 $question->qtype = SHORTANSWER;
318             }
319         }
321         if (!isset($question->qtype)) {
322             $giftqtypenotset = get_string('giftqtypenotset', 'qformat_gift');
323             $this->error($giftqtypenotset, $text);
324             return false;
325         }
327         switch ($question->qtype) {
328             case DESCRIPTION:
329                 $question->defaultmark = 0;
330                 $question->length = 0;
331                 return $question;
333             case ESSAY:
334                 $question->responseformat = 'editor';
335                 $question->responsefieldlines = 15;
336                 $question->attachments = 0;
337                 $question->graderinfo = array(
338                         'text' => '', 'format' => FORMAT_HTML, 'files' => array());
339                 return $question;
341             case MULTICHOICE:
342                 if (strpos($answertext,"=") === false) {
343                     $question->single = 0; // multiple answers are enabled if no single answer is 100% correct
344                 } else {
345                     $question->single = 1; // only one answer allowed (the default)
346                 }
347                 $question = $this->add_blank_combined_feedback($question);
349                 $answertext = str_replace("=", "~=", $answertext);
350                 $answers = explode("~", $answertext);
351                 if (isset($answers[0])) {
352                     $answers[0] = trim($answers[0]);
353                 }
354                 if (empty($answers[0])) {
355                     array_shift($answers);
356                 }
358                 $countanswers = count($answers);
360                 if (!$this->check_answer_count(2, $answers, $text)) {
361                     return false;
362                 }
364                 foreach ($answers as $key => $answer) {
365                     $answer = trim($answer);
367                     // determine answer weight
368                     if ($answer[0] == '=') {
369                         $answer_weight = 1;
370                         $answer = substr($answer, 1);
372                     } else if (preg_match($gift_answerweight_regex, $answer)) {    // check for properly formatted answer weight
373                         $answer_weight = $this->answerweightparser($answer);
375                     } else {     //default, i.e., wrong anwer
376                         $answer_weight = 0;
377                     }
378                     list($question->answer[$key], $question->feedback[$key]) =
379                             $this->commentparser($answer, $question->questiontextformat);
380                     $question->fraction[$key] = $answer_weight;
381                 }  // end foreach answer
383                 return $question;
385             case MATCH:
386                 $question = $this->add_blank_combined_feedback($question);
388                 $answers = explode('=', $answertext);
389                 if (isset($answers[0])) {
390                     $answers[0] = trim($answers[0]);
391                 }
392                 if (empty($answers[0])) {
393                     array_shift($answers);
394                 }
396                 if (!$this->check_answer_count(2,$answers,$text)) {
397                     return false;
398                 }
400                 foreach ($answers as $key => $answer) {
401                     $answer = trim($answer);
402                     if (strpos($answer, "->") === false) {
403                         $this->error(get_string('giftmatchingformat','qformat_gift'), $answer);
404                         return false;
405                     }
407                     $marker = strpos($answer, '->');
408                     $question->subquestions[$key] = $this->parse_text_with_format(
409                             substr($answer, 0, $marker), $question->questiontextformat);
410                     $question->subanswers[$key] = trim($this->escapedchar_post(
411                             substr($answer, $marker + 2)));
412                 }
414                 return $question;
416             case TRUEFALSE:
417                 list($answer, $wrongfeedback, $rightfeedback) =
418                         $this->split_truefalse_comment($answertext, $question->questiontextformat);
420                 if ($answer['text'] == "T" OR $answer['text'] == "TRUE") {
421                     $question->correctanswer = 1;
422                     $question->feedbacktrue = $rightfeedback;
423                     $question->feedbackfalse = $wrongfeedback;
424                 } else {
425                     $question->correctanswer = 0;
426                     $question->feedbacktrue = $wrongfeedback;
427                     $question->feedbackfalse = $rightfeedback;
428                 }
430                 $question->penalty = 1;
432                 return $question;
434             case SHORTANSWER:
435                 // SHORTANSWER Question
436                 $answers = explode("=", $answertext);
437                 if (isset($answers[0])) {
438                     $answers[0] = trim($answers[0]);
439                 }
440                 if (empty($answers[0])) {
441                     array_shift($answers);
442                 }
444                 if (!$this->check_answer_count(1, $answers, $text)) {
445                     return false;
446                 }
448                 foreach ($answers as $key => $answer) {
449                     $answer = trim($answer);
451                     // Answer weight
452                     if (preg_match($gift_answerweight_regex, $answer)) {    // check for properly formatted answer weight
453                         $answer_weight = $this->answerweightparser($answer);
454                     } else {     //default, i.e., full-credit anwer
455                         $answer_weight = 1;
456                     }
458                     list($answer, $question->feedback[$key]) = $this->commentparser(
459                             $answer, $question->questiontextformat);
461                     $question->answer[$key] = $answer['text'];
462                     $question->fraction[$key] = $answer_weight;
463                 }
465                 return $question;
467             case NUMERICAL:
468                 // Note similarities to ShortAnswer
469                 $answertext = substr($answertext, 1); // remove leading "#"
471                 // If there is feedback for a wrong answer, store it for now.
472                 if (($pos = strpos($answertext, '~')) !== false) {
473                     $wrongfeedback = substr($answertext, $pos);
474                     $answertext = substr($answertext, 0, $pos);
475                 } else {
476                     $wrongfeedback = '';
477                 }
479                 $answers = explode("=", $answertext);
480                 if (isset($answers[0])) {
481                     $answers[0] = trim($answers[0]);
482                 }
483                 if (empty($answers[0])) {
484                     array_shift($answers);
485                 }
487                 if (count($answers) == 0) {
488                     // invalid question
489                     $giftnonumericalanswers = get_string('giftnonumericalanswers','qformat_gift');
490                     $this->error($giftnonumericalanswers, $text);
491                     return false;
492                 }
494                 foreach ($answers as $key => $answer) {
495                     $answer = trim($answer);
497                     // Answer weight
498                     if (preg_match($gift_answerweight_regex, $answer)) {    // check for properly formatted answer weight
499                         $answer_weight = $this->answerweightparser($answer);
500                     } else {     //default, i.e., full-credit anwer
501                         $answer_weight = 1;
502                     }
504                     list($answer, $question->feedback[$key]) = $this->commentparser(
505                             $answer, $question->questiontextformat);
506                     $question->fraction[$key] = $answer_weight;
507                     $answer = $answer['text'];
509                     //Calculate Answer and Min/Max values
510                     if (strpos($answer,"..") > 0) { // optional [min]..[max] format
511                         $marker = strpos($answer,"..");
512                         $max = trim(substr($answer, $marker+2));
513                         $min = trim(substr($answer, 0, $marker));
514                         $ans = ($max + $min)/2;
515                         $tol = $max - $ans;
516                     } else if (strpos($answer, ':') > 0) { // standard [answer]:[errormargin] format
517                         $marker = strpos($answer, ':');
518                         $tol = trim(substr($answer, $marker+1));
519                         $ans = trim(substr($answer, 0, $marker));
520                     } else { // only one valid answer (zero errormargin)
521                         $tol = 0;
522                         $ans = trim($answer);
523                     }
525                     if (!(is_numeric($ans) || $ans = '*') || !is_numeric($tol)) {
526                             $errornotnumbers = get_string('errornotnumbers');
527                             $this->error($errornotnumbers, $text);
528                         return false;
529                     }
531                     // store results
532                     $question->answer[$key] = $ans;
533                     $question->tolerance[$key] = $tol;
534                 }
536                 if ($wrongfeedback) {
537                     $key += 1;
538                     $question->fraction[$key] = 0;
539                     list($notused, $question->feedback[$key]) = $this->commentparser(
540                             $wrongfeedback, $question->questiontextformat);
541                     $question->answer[$key] = '*';
542                     $question->tolerance[$key] = '';
543                 }
545                 return $question;
547             default:
548                 $this->error(get_string('giftnovalidquestion', 'qformat_gift'), $text);
549                 return false;
551         }
552     }
554     protected function add_blank_combined_feedback($question) {
555         $question->correctfeedback['text'] = '';
556         $question->correctfeedback['format'] = $question->questiontextformat;
557         $question->correctfeedback['files'] = array();
558         $question->partiallycorrectfeedback['text'] = '';
559         $question->partiallycorrectfeedback['format'] = $question->questiontextformat;
560         $question->partiallycorrectfeedback['files'] = array();
561         $question->incorrectfeedback['text'] = '';
562         $question->incorrectfeedback['format'] = $question->questiontextformat;
563         $question->incorrectfeedback['files'] = array();
564         return $question;
565     }
567     protected function repchar($text, $notused = 0) {
568         // Escapes 'reserved' characters # = ~ {) :
569         // Removes new lines
570         $reserved = array(  '\\',  '#', '=', '~', '{', '}', ':', "\n", "\r");
571         $escaped =  array('\\\\', '\#','\=','\~','\{','\}','\:', '\n', '' );
573         $newtext = str_replace($reserved, $escaped, $text);
574         return $newtext;
575     }
577     /**
578      * @param int $format one of the FORMAT_ constants.
579      * @return string the corresponding name.
580      */
581     protected function format_const_to_name($format) {
582         if ($format == FORMAT_MOODLE) {
583             return 'moodle';
584         } else if ($format == FORMAT_HTML) {
585             return 'html';
586         } else if ($format == FORMAT_PLAIN) {
587             return 'plain';
588         } else if ($format == FORMAT_MARKDOWN) {
589             return 'markdown';
590         } else {
591             return 'moodle';
592         }
593     }
595     /**
596      * @param int $format one of the FORMAT_ constants.
597      * @return string the corresponding name.
598      */
599     protected function format_name_to_const($format) {
600         if ($format == 'moodle') {
601             return FORMAT_MOODLE;
602         } else if ($format == 'html') {
603             return FORMAT_HTML;
604         } else if ($format == 'plain') {
605             return FORMAT_PLAIN;
606         } else if ($format == 'markdown') {
607             return FORMAT_MARKDOWN;
608         } else {
609             return -1;
610         }
611     }
613     public function write_name($name) {
614         return '::' . $this->repchar($name) . '::';
615     }
617     public function write_questiontext($text, $format, $defaultformat = FORMAT_MOODLE) {
618         $output = '';
619         if ($text != '' && $format != $defaultformat) {
620             $output .= '[' . $this->format_const_to_name($format) . ']';
621         }
622         $output .= $this->repchar($text, $format);
623         return $output;
624     }
626     /**
627      * Outputs the general feedback for the question, if any. This needs to be the
628      * last thing before the }.
629      * @param object $question the question data.
630      * @param string $indent to put before the general feedback. Defaults to a tab.
631      *      If this is not blank, a newline is added after the line.
632      */
633     public function write_general_feedback($question, $indent = "\t") {
634         $generalfeedback = $this->write_questiontext($question->generalfeedback,
635                 $question->generalfeedbackformat, $question->questiontextformat);
637         if ($generalfeedback) {
638             $generalfeedback = '####' . $generalfeedback;
639             if ($indent) {
640                 $generalfeedback = $indent . $generalfeedback . "\n";
641             }
642         }
644         return $generalfeedback;
645     }
647     public function writequestion($question) {
648         global $OUTPUT;
650         // Start with a comment
651         $expout = "// question: $question->id  name: $question->name\n";
653         // output depends on question type
654         switch($question->qtype) {
656         case 'category':
657             // not a real question, used to insert category switch
658             $expout .= "\$CATEGORY: $question->category\n";
659             break;
661         case DESCRIPTION:
662             $expout .= $this->write_name($question->name);
663             $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
664             break;
666         case ESSAY:
667             $expout .= $this->write_name($question->name);
668             $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
669             $expout .= "{";
670             $expout .= $this->write_general_feedback($question, '');
671             $expout .= "}\n";
672             break;
674         case TRUEFALSE:
675             $trueanswer = $question->options->answers[$question->options->trueanswer];
676             $falseanswer = $question->options->answers[$question->options->falseanswer];
677             if ($trueanswer->fraction == 1) {
678                 $answertext = 'TRUE';
679                 $rightfeedback = $this->write_questiontext($trueanswer->feedback,
680                         $trueanswer->feedbackformat, $question->questiontextformat);
681                 $wrongfeedback = $this->write_questiontext($falseanswer->feedback,
682                         $falseanswer->feedbackformat, $question->questiontextformat);
683             } else {
684                 $answertext = 'FALSE';
685                 $rightfeedback = $this->write_questiontext($falseanswer->feedback,
686                         $falseanswer->feedbackformat, $question->questiontextformat);
687                 $wrongfeedback = $this->write_questiontext($trueanswer->feedback,
688                         $trueanswer->feedbackformat, $question->questiontextformat);
689             }
691             $expout .= $this->write_name($question->name);
692             $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
693             $expout .= '{' . $this->repchar($answertext);
694             if ($wrongfeedback) {
695                 $expout .= '#' . $wrongfeedback;
696             } else if ($rightfeedback) {
697                 $expout .= '#';
698             }
699             if ($rightfeedback) {
700                 $expout .= '#' . $rightfeedback;
701             }
702             $expout .= $this->write_general_feedback($question, '');
703             $expout .= "}\n";
704             break;
706         case MULTICHOICE:
707             $expout .= $this->write_name($question->name);
708             $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
709             $expout .= "{\n";
710             foreach($question->options->answers as $answer) {
711                 if ($answer->fraction == 1) {
712                     $answertext = '=';
713                 } else if ($answer->fraction == 0) {
714                     $answertext = '~';
715                 } else {
716                     $weight = $answer->fraction * 100;
717                     $answertext = '~%' . $weight . '%';
718                 }
719                 $expout .= "\t" . $answertext . $this->write_questiontext($answer->answer,
720                             $answer->answerformat, $question->questiontextformat);
721                 if ($answer->feedback != '') {
722                     $expout .= '#' . $this->write_questiontext($answer->feedback,
723                             $answer->feedbackformat, $question->questiontextformat);
724                 }
725                 $expout .= "\n";
726             }
727             $expout .= $this->write_general_feedback($question);
728             $expout .= "}\n";
729             break;
731         case SHORTANSWER:
732             $expout .= $this->write_name($question->name);
733             $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
734             $expout .= "{\n";
735             foreach($question->options->answers as $answer) {
736                 $weight = 100 * $answer->fraction;
737                 $expout .= "\t=%" . $weight . '%' . $this->repchar($answer->answer) .
738                         '#' . $this->write_questiontext($answer->feedback,
739                             $answer->feedbackformat, $question->questiontextformat) . "\n";
740             }
741             $expout .= $this->write_general_feedback($question);
742             $expout .= "}\n";
743             break;
745         case NUMERICAL:
746             $expout .= $this->write_name($question->name);
747             $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
748             $expout .= "{#\n";
749             foreach ($question->options->answers as $answer) {
750                 if ($answer->answer != '' && $answer->answer != '*') {
751                     $weight = 100 * $answer->fraction;
752                     $expout .= "\t=%" . $weight . '%' . $answer->answer . ':' .
753                             (float)$answer->tolerance . '#' . $this->write_questiontext($answer->feedback,
754                             $answer->feedbackformat, $question->questiontextformat) . "\n";
755                 } else {
756                     $expout .= "\t~#" . $this->write_questiontext($answer->feedback,
757                             $answer->feedbackformat, $question->questiontextformat) . "\n";
758                 }
759             }
760             $expout .= $this->write_general_feedback($question);
761             $expout .= "}\n";
762             break;
764         case MATCH:
765             $expout .= $this->write_name($question->name);
766             $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
767             $expout .= "{\n";
768             foreach($question->options->subquestions as $subquestion) {
769                 $expout .= "\t=" . $this->write_questiontext($subquestion->questiontext,
770                         $subquestion->questiontextformat, $question->questiontextformat) .
771                         ' -> ' . $this->repchar($subquestion->answertext) . "\n";
772             }
773             $expout .= $this->write_general_feedback($question);
774             $expout .= "}\n";
775             break;
777         default:
778             // Check for plugins
779             if ($out = $this->try_exporting_using_qtypes($question->qtype, $question)) {
780                 $expout .= $out;
781             } else {
782                 $expout .= "Question type $question->qtype is not supported\n";
783                 echo $OUTPUT->notification(get_string('nohandler', 'qformat_gift',
784                         question_bank::get_qtype_name($question->qtype)));
785             }
786         }
788         // Add empty line to delimit questions
789         $expout .= "\n";
790         return $expout;
791     }