ecca6e2e852194dbbf1975f9202f6120801bcb54
[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 = $this->clean_question_name($this->escapedchar_post($questionname));
210                 $text = trim(substr($text, $namefinish+2)); // Remove name from text
211             }
212         } else {
213             $question->name = false;
214         }
217         // FIND ANSWER section
218         // no answer means its a description
219         $answerstart = strpos($text, '{');
220         $answerfinish = strpos($text, '}');
222         $description = false;
223         if (($answerstart === false) and ($answerfinish === false)) {
224             $description = true;
225             $answertext = '';
226             $answerlength = 0;
227         } else if (!(($answerstart !== false) and ($answerfinish !== false))) {
228             $this->error(get_string('braceerror', 'qformat_gift'), $text);
229             return false;
230         } else {
231             $answerlength = $answerfinish - $answerstart;
232             $answertext = trim(substr($text, $answerstart + 1, $answerlength - 1));
233         }
235         // Format QUESTION TEXT without answer, inserting "_____" as necessary
236         if ($description) {
237             $questiontext = $text;
238         } else if (substr($text, -1) == "}") {
239             // no blank line if answers follow question, outside of closing punctuation
240             $questiontext = substr_replace($text, "", $answerstart, $answerlength+1);
241         } else {
242             // inserts blank line for missing word format
243             $questiontext = substr_replace($text, "_____", $answerstart, $answerlength+1);
244         }
246         // Get questiontext format from questiontext
247         $text = $this->parse_text_with_format($questiontext);
248         $question->questiontextformat = $text['format'];
249         $question->generalfeedbackformat = $text['format'];
250         $question->questiontext = $text['text'];
252         // set question name if not already set
253         if ($question->name === false) {
254             $question->name = $this->create_default_question_name($question->questiontext, get_string('questionname', 'question'));
255         }
257         // determine QUESTION TYPE
258         $question->qtype = NULL;
260         // give plugins first try
261         // plugins must promise not to intercept standard qtypes
262         // MDL-12346, this could be called from lesson mod which has its own base class =(
263         if (method_exists($this, 'try_importing_using_qtypes') && ($try_question = $this->try_importing_using_qtypes($lines, $question, $answertext))) {
264             return $try_question;
265         }
267         if ($description) {
268             $question->qtype = DESCRIPTION;
270         } else if ($answertext == '') {
271             $question->qtype = ESSAY;
273         } else if ($answertext{0} == '#') {
274             $question->qtype = NUMERICAL;
276         } else if (strpos($answertext, '~') !== false)  {
277             // only Multiplechoice questions contain tilde ~
278             $question->qtype = MULTICHOICE;
280         } else if (strpos($answertext, '=')  !== false
281                 && strpos($answertext, '->') !== false) {
282             // only Matching contains both = and ->
283             $question->qtype = MATCH;
285         } else { // either TRUEFALSE or SHORTANSWER
287             // TRUEFALSE question check
288             $truefalse_check = $answertext;
289             if (strpos($answertext, '#') > 0) {
290                 // strip comments to check for TrueFalse question
291                 $truefalse_check = trim(substr($answertext, 0, strpos($answertext,"#")));
292             }
294             $valid_tf_answers = array('T', 'TRUE', 'F', 'FALSE');
295             if (in_array($truefalse_check, $valid_tf_answers)) {
296                 $question->qtype = TRUEFALSE;
298             } else { // Must be SHORTANSWER
299                 $question->qtype = SHORTANSWER;
300             }
301         }
303         if (!isset($question->qtype)) {
304             $giftqtypenotset = get_string('giftqtypenotset', 'qformat_gift');
305             $this->error($giftqtypenotset, $text);
306             return false;
307         }
309         switch ($question->qtype) {
310             case DESCRIPTION:
311                 $question->defaultmark = 0;
312                 $question->length = 0;
313                 return $question;
315             case ESSAY:
316                 $question->responseformat = 'editor';
317                 $question->responsefieldlines = 15;
318                 $question->attachments = 0;
319                 $question->graderinfo = array(
320                         'text' => '', 'format' => FORMAT_HTML, 'files' => array());
321                 return $question;
323             case MULTICHOICE:
324                 if (strpos($answertext,"=") === false) {
325                     $question->single = 0; // multiple answers are enabled if no single answer is 100% correct
326                 } else {
327                     $question->single = 1; // only one answer allowed (the default)
328                 }
329                 $question = $this->add_blank_combined_feedback($question);
331                 $answertext = str_replace("=", "~=", $answertext);
332                 $answers = explode("~", $answertext);
333                 if (isset($answers[0])) {
334                     $answers[0] = trim($answers[0]);
335                 }
336                 if (empty($answers[0])) {
337                     array_shift($answers);
338                 }
340                 $countanswers = count($answers);
342                 if (!$this->check_answer_count(2, $answers, $text)) {
343                     return false;
344                 }
346                 foreach ($answers as $key => $answer) {
347                     $answer = trim($answer);
349                     // determine answer weight
350                     if ($answer[0] == '=') {
351                         $answer_weight = 1;
352                         $answer = substr($answer, 1);
354                     } else if (preg_match($gift_answerweight_regex, $answer)) {    // check for properly formatted answer weight
355                         $answer_weight = $this->answerweightparser($answer);
357                     } else {     //default, i.e., wrong anwer
358                         $answer_weight = 0;
359                     }
360                     list($question->answer[$key], $question->feedback[$key]) =
361                             $this->commentparser($answer, $question->questiontextformat);
362                     $question->fraction[$key] = $answer_weight;
363                 }  // end foreach answer
365                 return $question;
367             case MATCH:
368                 $question = $this->add_blank_combined_feedback($question);
370                 $answers = explode('=', $answertext);
371                 if (isset($answers[0])) {
372                     $answers[0] = trim($answers[0]);
373                 }
374                 if (empty($answers[0])) {
375                     array_shift($answers);
376                 }
378                 if (!$this->check_answer_count(2,$answers,$text)) {
379                     return false;
380                 }
382                 foreach ($answers as $key => $answer) {
383                     $answer = trim($answer);
384                     if (strpos($answer, "->") === false) {
385                         $this->error(get_string('giftmatchingformat','qformat_gift'), $answer);
386                         return false;
387                     }
389                     $marker = strpos($answer, '->');
390                     $question->subquestions[$key] = $this->parse_text_with_format(
391                             substr($answer, 0, $marker), $question->questiontextformat);
392                     $question->subanswers[$key] = trim($this->escapedchar_post(
393                             substr($answer, $marker + 2)));
394                 }
396                 return $question;
398             case TRUEFALSE:
399                 list($answer, $wrongfeedback, $rightfeedback) =
400                         $this->split_truefalse_comment($answertext, $question->questiontextformat);
402                 if ($answer['text'] == "T" OR $answer['text'] == "TRUE") {
403                     $question->correctanswer = 1;
404                     $question->feedbacktrue = $rightfeedback;
405                     $question->feedbackfalse = $wrongfeedback;
406                 } else {
407                     $question->correctanswer = 0;
408                     $question->feedbacktrue = $wrongfeedback;
409                     $question->feedbackfalse = $rightfeedback;
410                 }
412                 $question->penalty = 1;
414                 return $question;
416             case SHORTANSWER:
417                 // SHORTANSWER Question
418                 $answers = explode("=", $answertext);
419                 if (isset($answers[0])) {
420                     $answers[0] = trim($answers[0]);
421                 }
422                 if (empty($answers[0])) {
423                     array_shift($answers);
424                 }
426                 if (!$this->check_answer_count(1, $answers, $text)) {
427                     return false;
428                 }
430                 foreach ($answers as $key => $answer) {
431                     $answer = trim($answer);
433                     // Answer weight
434                     if (preg_match($gift_answerweight_regex, $answer)) {    // check for properly formatted answer weight
435                         $answer_weight = $this->answerweightparser($answer);
436                     } else {     //default, i.e., full-credit anwer
437                         $answer_weight = 1;
438                     }
440                     list($answer, $question->feedback[$key]) = $this->commentparser(
441                             $answer, $question->questiontextformat);
443                     $question->answer[$key] = $answer['text'];
444                     $question->fraction[$key] = $answer_weight;
445                 }
447                 return $question;
449             case NUMERICAL:
450                 // Note similarities to ShortAnswer
451                 $answertext = substr($answertext, 1); // remove leading "#"
453                 // If there is feedback for a wrong answer, store it for now.
454                 if (($pos = strpos($answertext, '~')) !== false) {
455                     $wrongfeedback = substr($answertext, $pos);
456                     $answertext = substr($answertext, 0, $pos);
457                 } else {
458                     $wrongfeedback = '';
459                 }
461                 $answers = explode("=", $answertext);
462                 if (isset($answers[0])) {
463                     $answers[0] = trim($answers[0]);
464                 }
465                 if (empty($answers[0])) {
466                     array_shift($answers);
467                 }
469                 if (count($answers) == 0) {
470                     // invalid question
471                     $giftnonumericalanswers = get_string('giftnonumericalanswers','qformat_gift');
472                     $this->error($giftnonumericalanswers, $text);
473                     return false;
474                 }
476                 foreach ($answers as $key => $answer) {
477                     $answer = trim($answer);
479                     // Answer weight
480                     if (preg_match($gift_answerweight_regex, $answer)) {    // check for properly formatted answer weight
481                         $answer_weight = $this->answerweightparser($answer);
482                     } else {     //default, i.e., full-credit anwer
483                         $answer_weight = 1;
484                     }
486                     list($answer, $question->feedback[$key]) = $this->commentparser(
487                             $answer, $question->questiontextformat);
488                     $question->fraction[$key] = $answer_weight;
489                     $answer = $answer['text'];
491                     //Calculate Answer and Min/Max values
492                     if (strpos($answer,"..") > 0) { // optional [min]..[max] format
493                         $marker = strpos($answer,"..");
494                         $max = trim(substr($answer, $marker+2));
495                         $min = trim(substr($answer, 0, $marker));
496                         $ans = ($max + $min)/2;
497                         $tol = $max - $ans;
498                     } else if (strpos($answer, ':') > 0) { // standard [answer]:[errormargin] format
499                         $marker = strpos($answer, ':');
500                         $tol = trim(substr($answer, $marker+1));
501                         $ans = trim(substr($answer, 0, $marker));
502                     } else { // only one valid answer (zero errormargin)
503                         $tol = 0;
504                         $ans = trim($answer);
505                     }
507                     if (!(is_numeric($ans) || $ans = '*') || !is_numeric($tol)) {
508                             $errornotnumbers = get_string('errornotnumbers');
509                             $this->error($errornotnumbers, $text);
510                         return false;
511                     }
513                     // store results
514                     $question->answer[$key] = $ans;
515                     $question->tolerance[$key] = $tol;
516                 }
518                 if ($wrongfeedback) {
519                     $key += 1;
520                     $question->fraction[$key] = 0;
521                     list($notused, $question->feedback[$key]) = $this->commentparser(
522                             $wrongfeedback, $question->questiontextformat);
523                     $question->answer[$key] = '*';
524                     $question->tolerance[$key] = '';
525                 }
527                 return $question;
529             default:
530                 $this->error(get_string('giftnovalidquestion', 'qformat_gift'), $text);
531                 return false;
533         }
534     }
536     protected function repchar($text, $notused = 0) {
537         // Escapes 'reserved' characters # = ~ {) :
538         // Removes new lines
539         $reserved = array(  '\\',  '#', '=', '~', '{', '}', ':', "\n", "\r");
540         $escaped =  array('\\\\', '\#','\=','\~','\{','\}','\:', '\n', '' );
542         $newtext = str_replace($reserved, $escaped, $text);
543         return $newtext;
544     }
546     /**
547      * @param int $format one of the FORMAT_ constants.
548      * @return string the corresponding name.
549      */
550     protected function format_const_to_name($format) {
551         if ($format == FORMAT_MOODLE) {
552             return 'moodle';
553         } else if ($format == FORMAT_HTML) {
554             return 'html';
555         } else if ($format == FORMAT_PLAIN) {
556             return 'plain';
557         } else if ($format == FORMAT_MARKDOWN) {
558             return 'markdown';
559         } else {
560             return 'moodle';
561         }
562     }
564     /**
565      * @param int $format one of the FORMAT_ constants.
566      * @return string the corresponding name.
567      */
568     protected function format_name_to_const($format) {
569         if ($format == 'moodle') {
570             return FORMAT_MOODLE;
571         } else if ($format == 'html') {
572             return FORMAT_HTML;
573         } else if ($format == 'plain') {
574             return FORMAT_PLAIN;
575         } else if ($format == 'markdown') {
576             return FORMAT_MARKDOWN;
577         } else {
578             return -1;
579         }
580     }
582     public function write_name($name) {
583         return '::' . $this->repchar($name) . '::';
584     }
586     public function write_questiontext($text, $format, $defaultformat = FORMAT_MOODLE) {
587         $output = '';
588         if ($text != '' && $format != $defaultformat) {
589             $output .= '[' . $this->format_const_to_name($format) . ']';
590         }
591         $output .= $this->repchar($text, $format);
592         return $output;
593     }
595     public function writequestion($question) {
596         global $OUTPUT;
598         // Start with a comment
599         $expout = "// question: $question->id  name: $question->name\n";
601         // output depends on question type
602         switch($question->qtype) {
604         case 'category':
605             // not a real question, used to insert category switch
606             $expout .= "\$CATEGORY: $question->category\n";
607             break;
609         case DESCRIPTION:
610             $expout .= $this->write_name($question->name);
611             $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
612             break;
614         case ESSAY:
615             $expout .= $this->write_name($question->name);
616             $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
617             $expout .= "{}\n";
618             break;
620         case TRUEFALSE:
621             $trueanswer = $question->options->answers[$question->options->trueanswer];
622             $falseanswer = $question->options->answers[$question->options->falseanswer];
623             if ($trueanswer->fraction == 1) {
624                 $answertext = 'TRUE';
625                 $rightfeedback = $this->write_questiontext($trueanswer->feedback,
626                         $trueanswer->feedbackformat, $question->questiontextformat);
627                 $wrongfeedback = $this->write_questiontext($falseanswer->feedback,
628                         $falseanswer->feedbackformat, $question->questiontextformat);
629             } else {
630                 $answertext = 'FALSE';
631                 $rightfeedback = $this->write_questiontext($falseanswer->feedback,
632                         $falseanswer->feedbackformat, $question->questiontextformat);
633                 $wrongfeedback = $this->write_questiontext($trueanswer->feedback,
634                         $trueanswer->feedbackformat, $question->questiontextformat);
635             }
637             $expout .= $this->write_name($question->name);
638             $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
639             $expout .= '{' . $this->repchar($answertext);
640             if ($wrongfeedback) {
641                 $expout .= '#' . $wrongfeedback;
642             } else if ($rightfeedback) {
643                 $expout .= '#';
644             }
645             if ($rightfeedback) {
646                 $expout .= '#' . $rightfeedback;
647             }
648             $expout .= "}\n";
649             break;
651         case MULTICHOICE:
652             $expout .= $this->write_name($question->name);
653             $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
654             $expout .= "{\n";
655             foreach($question->options->answers as $answer) {
656                 if ($answer->fraction == 1) {
657                     $answertext = '=';
658                 } else if ($answer->fraction == 0) {
659                     $answertext = '~';
660                 } else {
661                     $weight = $answer->fraction * 100;
662                     $answertext = '~%' . $weight . '%';
663                 }
664                 $expout .= "\t" . $answertext . $this->write_questiontext($answer->answer,
665                             $answer->answerformat, $question->questiontextformat);
666                 if ($answer->feedback != '') {
667                     $expout .= '#' . $this->write_questiontext($answer->feedback,
668                             $answer->feedbackformat, $question->questiontextformat);
669                 }
670                 $expout .= "\n";
671             }
672             $expout .= "}\n";
673             break;
675         case SHORTANSWER:
676             $expout .= $this->write_name($question->name);
677             $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
678             $expout .= "{\n";
679             foreach($question->options->answers as $answer) {
680                 $weight = 100 * $answer->fraction;
681                 $expout .= "\t=%" . $weight . '%' . $this->repchar($answer->answer) .
682                         '#' . $this->write_questiontext($answer->feedback,
683                             $answer->feedbackformat, $question->questiontextformat) . "\n";
684             }
685             $expout .= "}\n";
686             break;
688         case NUMERICAL:
689             $expout .= $this->write_name($question->name);
690             $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
691             $expout .= "{#\n";
692             foreach ($question->options->answers as $answer) {
693                 if ($answer->answer != '' && $answer->answer != '*') {
694                     $weight = 100 * $answer->fraction;
695                     $expout .= "\t=%" . $weight . '%' . $answer->answer . ':' .
696                             (float)$answer->tolerance . '#' . $this->write_questiontext($answer->feedback,
697                             $answer->feedbackformat, $question->questiontextformat) . "\n";
698                 } else {
699                     $expout .= "\t~#" . $this->write_questiontext($answer->feedback,
700                             $answer->feedbackformat, $question->questiontextformat) . "\n";
701                 }
702             }
703             $expout .= "}\n";
704             break;
706         case MATCH:
707             $expout .= $this->write_name($question->name);
708             $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
709             $expout .= "{\n";
710             foreach($question->options->subquestions as $subquestion) {
711                 $expout .= "\t=" . $this->write_questiontext($subquestion->questiontext,
712                         $subquestion->questiontextformat, $question->questiontextformat) .
713                         ' -> ' . $this->repchar($subquestion->answertext) . "\n";
714             }
715             $expout .= "}\n";
716             break;
718         default:
719             // Check for plugins
720             if ($out = $this->try_exporting_using_qtypes($question->qtype, $question)) {
721                 $expout .= $out;
722             } else {
723                 $expout .= "Question type $question->qtype is not supported\n";
724                 echo $OUTPUT->notification(get_string('nohandler', 'qformat_gift',
725                         question_bank::get_qtype_name($question->qtype)));
726             }
727         }
729         // Add empty line to delimit questions
730         $expout .= "\n";
731         return $expout;
732     }