MDL-34171 qformat_gift: Fix edge case with special character escaping.
[moodle.git] / question / format / gift / format.php
CommitLineData
aeb15530 1<?php
d3603157 2// This file is part of Moodle - http://moodle.org/
f1abd39f 3//
d3603157
TH
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.
84769fd8 8//
d3603157
TH
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.
aeb15530 13//
d3603157
TH
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/>.
16
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 */
25
26
a17b297d
TH
27defined('MOODLE_INTERNAL') || die();
28
29
41a89a07 30/**
d3603157
TH
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
41a89a07 62 */
f5565b69 63class qformat_gift extends qformat_default {
84769fd8 64
c7df5006 65 public function provide_import() {
09db6da2 66 return true;
84769fd8 67 }
68
c7df5006 69 public function provide_export() {
09db6da2 70 return true;
84769fd8 71 }
72
c7df5006 73 public function export_file_extension() {
b80d424c
TH
74 return '.txt';
75 }
76
c7df5006 77 protected function answerweightparser(&$answer) {
84769fd8 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 }
85
c7df5006 86 protected function commentparser($answer, $defaultformat) {
5b744492
TH
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);
84769fd8 91 } else {
5b744492 92 $feedback = array('text' => '', 'format' => $defaultformat, 'files' => array());
84769fd8 93 }
5b744492 94 return array($ans, $feedback);
84769fd8 95 }
96
c7df5006 97 protected function split_truefalse_comment($answer, $defaultformat) {
5b744492
TH
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);
09db6da2 102 } else {
5b744492 103 $wrongfeedback = array('text' => '', 'format' => $defaultformat, 'files' => array());
09db6da2 104 }
5b744492
TH
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);
84769fd8 111 }
aeb15530 112
c7df5006 113 protected function escapedchar_pre($string) {
84769fd8 114 //Replaces escaped control characters with a placeholder BEFORE processing
aeb15530 115
13bb604e
TH
116 $escapedcharacters = array("\\:", "\\#", "\\=", "\\{", "\\}", "\\~", "\\n" ); //dlnsk
117 $placeholders = array("&&058;", "&&035;", "&&061;", "&&123;", "&&125;", "&&126;", "&&010"); //dlnsk
84769fd8 118
119 $string = str_replace("\\\\", "&&092;", $string);
120 $string = str_replace($escapedcharacters, $placeholders, $string);
121 $string = str_replace("&&092;", "\\", $string);
122 return $string;
123 }
124
c7df5006 125 protected function escapedchar_post($string) {
84769fd8 126 //Replaces placeholders with corresponding character AFTER processing is done
dfdce7fb 127 $placeholders = array("&&058;", "&&035;", "&&061;", "&&123;", "&&125;", "&&126;", "&&010"); //dlnsk
13bb604e 128 $characters = array(":", "#", "=", "{", "}", "~", "\n" ); //dlnsk
84769fd8 129 $string = str_replace($placeholders, $characters, $string);
130 return $string;
131 }
132
c7df5006 133 protected function check_answer_count($min, $answers, $text) {
09db6da2 134 $countanswers = count($answers);
135 if ($countanswers < $min) {
5e8a85aa 136 $this->error(get_string('importminerror', 'qformat_gift'), $text);
09db6da2 137 return false;
138 }
139
140 return true;
84769fd8 141 }
142
5b744492
TH
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 }
84769fd8 161
c7df5006 162 public function readquestion($lines) {
84769fd8 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.
165
166 $question = $this->defaultquestion();
167 $comment = NULL;
168 // define replaced by simple assignment, stop redefine notices
13bb604e 169 $gift_answerweight_regex = '/^%\-*([0-9]{1,2})\.?([0-9]*)%/';
84769fd8 170
171 // REMOVED COMMENTED LINES and IMPLODE
172 foreach ($lines as $key => $line) {
09db6da2 173 $line = trim($line);
13bb604e
TH
174 if (substr($line, 0, 2) == '//') {
175 $lines[$key] = ' ';
09db6da2 176 }
84769fd8 177 }
178
13bb604e 179 $text = trim(implode(' ', $lines));
84769fd8 180
13bb604e 181 if ($text == '') {
84769fd8 182 return false;
183 }
184
185 // Substitute escaped control characters with placeholders
186 $text = $this->escapedchar_pre($text);
187
b39c7aad 188 // Look for category modifier
13bb604e 189 if (preg_match('~^\$CATEGORY:~', $text)) {
5363b555 190 // $newcategory = $matches[1];
13bb604e 191 $newcategory = trim(substr($text, 10));
b39c7aad 192
193 // build fake question to contain category
194 $question->qtype = 'category';
195 $question->category = $newcategory;
196 return $question;
197 }
aeb15530 198
84769fd8 199 // QUESTION NAME parser
13bb604e 200 if (substr($text, 0, 2) == '::') {
84769fd8 201 $text = substr($text, 2);
202
13bb604e 203 $namefinish = strpos($text, '::');
84769fd8 204 if ($namefinish === false) {
205 $question->name = false;
206 // name will be assigned after processing question text below
09db6da2 207 } else {
84769fd8 208 $questionname = substr($text, 0, $namefinish);
294ce987 209 $question->name = trim($this->escapedchar_post($questionname));
84769fd8 210 $text = trim(substr($text, $namefinish+2)); // Remove name from text
211 }
212 } else {
213 $question->name = false;
214 }
215
216
217 // FIND ANSWER section
e8825e72 218 // no answer means its a description
13bb604e
TH
219 $answerstart = strpos($text, '{');
220 $answerfinish = strpos($text, '}');
e8825e72 221
222 $description = false;
223 if (($answerstart === false) and ($answerfinish === false)) {
224 $description = true;
225 $answertext = '';
226 $answerlength = 0;
13bb604e 227 } else if (!(($answerstart !== false) and ($answerfinish !== false))) {
5e8a85aa 228 $this->error(get_string('braceerror', 'qformat_gift'), $text);
84769fd8 229 return false;
13bb604e 230 } else {
e8825e72 231 $answerlength = $answerfinish - $answerstart;
232 $answertext = trim(substr($text, $answerstart + 1, $answerlength - 1));
233 }
84769fd8 234
235 // Format QUESTION TEXT without answer, inserting "_____" as necessary
e8825e72 236 if ($description) {
237 $questiontext = $text;
13bb604e 238 } else if (substr($text, -1) == "}") {
84769fd8 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 }
245
13bb604e 246 // Get questiontext format from questiontext
5b744492
TH
247 $text = $this->parse_text_with_format($questiontext);
248 $question->questiontextformat = $text['format'];
249 $question->generalfeedbackformat = $text['format'];
250 $question->questiontext = $text['text'];
84769fd8 251
252 // set question name if not already set
253 if ($question->name === false) {
254 $question->name = $question->questiontext;
13bb604e 255 }
84769fd8 256
8daaaafc 257 // ensure name is not longer than 250 characters
13bb604e
TH
258 $question->name = shorten_text($question->name, 200);
259 $question->name = strip_tags(substr($question->name, 0, 250));
84769fd8 260
09db6da2 261 // determine QUESTION TYPE
84769fd8 262 $question->qtype = NULL;
263
4433fff5 264 // give plugins first try
265 // plugins must promise not to intercept standard qtypes
3ea69b8d 266 // MDL-12346, this could be called from lesson mod which has its own base class =(
13bb604e 267 if (method_exists($this, 'try_importing_using_qtypes') && ($try_question = $this->try_importing_using_qtypes($lines, $question, $answertext))) {
d510ec64 268 return $try_question;
4433fff5 269 }
270
e8825e72 271 if ($description) {
272 $question->qtype = DESCRIPTION;
13bb604e
TH
273
274 } else if ($answertext == '') {
e8825e72 275 $question->qtype = ESSAY;
13bb604e
TH
276
277 } else if ($answertext{0} == '#') {
84769fd8 278 $question->qtype = NUMERICAL;
279
13bb604e 280 } else if (strpos($answertext, '~') !== false) {
84769fd8 281 // only Multiplechoice questions contain tilde ~
282 $question->qtype = MULTICHOICE;
aeb15530 283
13bb604e
TH
284 } else if (strpos($answertext, '=') !== false
285 && strpos($answertext, '->') !== false) {
09db6da2 286 // only Matching contains both = and ->
84769fd8 287 $question->qtype = MATCH;
288
289 } else { // either TRUEFALSE or SHORTANSWER
aeb15530 290
84769fd8 291 // TRUEFALSE question check
292 $truefalse_check = $answertext;
13bb604e 293 if (strpos($answertext, '#') > 0) {
84769fd8 294 // strip comments to check for TrueFalse question
295 $truefalse_check = trim(substr($answertext, 0, strpos($answertext,"#")));
296 }
297
13bb604e 298 $valid_tf_answers = array('T', 'TRUE', 'F', 'FALSE');
84769fd8 299 if (in_array($truefalse_check, $valid_tf_answers)) {
300 $question->qtype = TRUEFALSE;
301
302 } else { // Must be SHORTANSWER
13bb604e 303 $question->qtype = SHORTANSWER;
84769fd8 304 }
305 }
306
307 if (!isset($question->qtype)) {
5e8a85aa 308 $giftqtypenotset = get_string('giftqtypenotset', 'qformat_gift');
13bb604e 309 $this->error($giftqtypenotset, $text);
84769fd8 310 return false;
311 }
312
313 switch ($question->qtype) {
e8825e72 314 case DESCRIPTION:
49e2bba7 315 $question->defaultmark = 0;
3f5633df 316 $question->length = 0;
e8825e72 317 return $question;
3c781845 318
e8825e72 319 case ESSAY:
0519420f
TH
320 $question->responseformat = 'editor';
321 $question->responsefieldlines = 15;
322 $question->attachments = 0;
323 $question->graderinfo = array(
324 'text' => '', 'format' => FORMAT_HTML, 'files' => array());
e8825e72 325 return $question;
3c781845 326
84769fd8 327 case MULTICHOICE:
328 if (strpos($answertext,"=") === false) {
13bb604e 329 $question->single = 0; // multiple answers are enabled if no single answer is 100% correct
84769fd8 330 } else {
13bb604e 331 $question->single = 1; // only one answer allowed (the default)
84769fd8 332 }
3c781845 333 $question = $this->add_blank_combined_feedback($question);
84769fd8 334
335 $answertext = str_replace("=", "~=", $answertext);
336 $answers = explode("~", $answertext);
337 if (isset($answers[0])) {
338 $answers[0] = trim($answers[0]);
339 }
340 if (empty($answers[0])) {
341 array_shift($answers);
342 }
aeb15530 343
84769fd8 344 $countanswers = count($answers);
aeb15530 345
13bb604e 346 if (!$this->check_answer_count(2, $answers, $text)) {
2befe778 347 return false;
84769fd8 348 }
349
350 foreach ($answers as $key => $answer) {
351 $answer = trim($answer);
352
353 // determine answer weight
13bb604e 354 if ($answer[0] == '=') {
84769fd8 355 $answer_weight = 1;
356 $answer = substr($answer, 1);
aeb15530 357
13bb604e 358 } else if (preg_match($gift_answerweight_regex, $answer)) { // check for properly formatted answer weight
84769fd8 359 $answer_weight = $this->answerweightparser($answer);
aeb15530 360
84769fd8 361 } else { //default, i.e., wrong anwer
362 $answer_weight = 0;
363 }
5b744492
TH
364 list($question->answer[$key], $question->feedback[$key]) =
365 $this->commentparser($answer, $question->questiontextformat);
84769fd8 366 $question->fraction[$key] = $answer_weight;
84769fd8 367 } // end foreach answer
aeb15530 368
84769fd8 369 return $question;
84769fd8 370
371 case MATCH:
3c781845
TH
372 $question = $this->add_blank_combined_feedback($question);
373
13bb604e 374 $answers = explode('=', $answertext);
84769fd8 375 if (isset($answers[0])) {
376 $answers[0] = trim($answers[0]);
377 }
378 if (empty($answers[0])) {
379 array_shift($answers);
380 }
aeb15530 381
13bb604e 382 if (!$this->check_answer_count(2,$answers,$text)) {
2befe778 383 return false;
84769fd8 384 }
aeb15530 385
84769fd8 386 foreach ($answers as $key => $answer) {
387 $answer = trim($answer);
87ee4968 388 if (strpos($answer, "->") === false) {
3c781845 389 $this->error(get_string('giftmatchingformat','qformat_gift'), $answer);
84769fd8 390 return false;
84769fd8 391 }
392
13bb604e 393 $marker = strpos($answer, '->');
5b744492
TH
394 $question->subquestions[$key] = $this->parse_text_with_format(
395 substr($answer, 0, $marker), $question->questiontextformat);
396 $question->subanswers[$key] = trim($this->escapedchar_post(
397 substr($answer, $marker + 2)));
398 }
aeb15530 399
84769fd8 400 return $question;
aeb15530 401
84769fd8 402 case TRUEFALSE:
5b744492
TH
403 list($answer, $wrongfeedback, $rightfeedback) =
404 $this->split_truefalse_comment($answertext, $question->questiontextformat);
84769fd8 405
2c00fa16 406 if ($answer['text'] == "T" OR $answer['text'] == "TRUE") {
5b744492
TH
407 $question->correctanswer = 1;
408 $question->feedbacktrue = $rightfeedback;
409 $question->feedbackfalse = $wrongfeedback;
84769fd8 410 } else {
5b744492
TH
411 $question->correctanswer = 0;
412 $question->feedbacktrue = $wrongfeedback;
413 $question->feedbackfalse = $rightfeedback;
84769fd8 414 }
415
0d4c63d7 416 $question->penalty = 1;
7939a4a0 417
84769fd8 418 return $question;
aeb15530 419
84769fd8 420 case SHORTANSWER:
421 // SHORTANSWER Question
422 $answers = explode("=", $answertext);
423 if (isset($answers[0])) {
424 $answers[0] = trim($answers[0]);
425 }
426 if (empty($answers[0])) {
427 array_shift($answers);
428 }
aeb15530 429
5b744492 430 if (!$this->check_answer_count(1, $answers, $text)) {
2befe778 431 return false;
84769fd8 432 }
433
434 foreach ($answers as $key => $answer) {
435 $answer = trim($answer);
436
5b744492 437 // Answer weight
6dbcacee 438 if (preg_match($gift_answerweight_regex, $answer)) { // check for properly formatted answer weight
84769fd8 439 $answer_weight = $this->answerweightparser($answer);
440 } else { //default, i.e., full-credit anwer
441 $answer_weight = 1;
442 }
5b744492
TH
443
444 list($answer, $question->feedback[$key]) = $this->commentparser(
445 $answer, $question->questiontextformat);
446
447 $question->answer[$key] = $answer['text'];
13bb604e 448 $question->fraction[$key] = $answer_weight;
5b744492 449 }
84769fd8 450
84769fd8 451 return $question;
84769fd8 452
453 case NUMERICAL:
454 // Note similarities to ShortAnswer
455 $answertext = substr($answertext, 1); // remove leading "#"
456
1fe641f7 457 // If there is feedback for a wrong answer, store it for now.
458 if (($pos = strpos($answertext, '~')) !== false) {
459 $wrongfeedback = substr($answertext, $pos);
460 $answertext = substr($answertext, 0, $pos);
461 } else {
462 $wrongfeedback = '';
463 }
464
84769fd8 465 $answers = explode("=", $answertext);
466 if (isset($answers[0])) {
467 $answers[0] = trim($answers[0]);
468 }
469 if (empty($answers[0])) {
470 array_shift($answers);
471 }
aeb15530 472
84769fd8 473 if (count($answers) == 0) {
474 // invalid question
5e8a85aa 475 $giftnonumericalanswers = get_string('giftnonumericalanswers','qformat_gift');
13bb604e 476 $this->error($giftnonumericalanswers, $text);
84769fd8 477 return false;
84769fd8 478 }
479
480 foreach ($answers as $key => $answer) {
481 $answer = trim($answer);
482
483 // Answer weight
6dbcacee 484 if (preg_match($gift_answerweight_regex, $answer)) { // check for properly formatted answer weight
84769fd8 485 $answer_weight = $this->answerweightparser($answer);
486 } else { //default, i.e., full-credit anwer
487 $answer_weight = 1;
488 }
5b744492
TH
489
490 list($answer, $question->feedback[$key]) = $this->commentparser(
491 $answer, $question->questiontextformat);
84769fd8 492 $question->fraction[$key] = $answer_weight;
5b744492 493 $answer = $answer['text'];
84769fd8 494
495 //Calculate Answer and Min/Max values
496 if (strpos($answer,"..") > 0) { // optional [min]..[max] format
497 $marker = strpos($answer,"..");
498 $max = trim(substr($answer, $marker+2));
499 $min = trim(substr($answer, 0, $marker));
500 $ans = ($max + $min)/2;
501 $tol = $max - $ans;
13bb604e
TH
502 } else if (strpos($answer, ':') > 0) { // standard [answer]:[errormargin] format
503 $marker = strpos($answer, ':');
84769fd8 504 $tol = trim(substr($answer, $marker+1));
505 $ans = trim(substr($answer, 0, $marker));
506 } else { // only one valid answer (zero errormargin)
507 $tol = 0;
508 $ans = trim($answer);
509 }
aeb15530 510
09db6da2 511 if (!(is_numeric($ans) || $ans = '*') || !is_numeric($tol)) {
13bb604e
TH
512 $errornotnumbers = get_string('errornotnumbers');
513 $this->error($errornotnumbers, $text);
84769fd8 514 return false;
84769fd8 515 }
aeb15530 516
84769fd8 517 // store results
518 $question->answer[$key] = $ans;
519 $question->tolerance[$key] = $tol;
5b744492 520 }
84769fd8 521
1fe641f7 522 if ($wrongfeedback) {
523 $key += 1;
524 $question->fraction[$key] = 0;
5b744492
TH
525 list($notused, $question->feedback[$key]) = $this->commentparser(
526 $wrongfeedback, $question->questiontextformat);
13bb604e 527 $question->answer[$key] = '*';
1fe641f7 528 $question->tolerance[$key] = '';
529 }
530
84769fd8 531 return $question;
84769fd8 532
3c781845
TH
533 default:
534 $this->error(get_string('giftnovalidquestion', 'qformat_gift'), $text);
535 return false;
aeb15530 536
13bb604e 537 }
3c781845 538 }
84769fd8 539
3c781845
TH
540 protected function add_blank_combined_feedback($question) {
541 $question->correctfeedback['text'] = '';
542 $question->correctfeedback['format'] = $question->questiontextformat;
543 $question->correctfeedback['files'] = array();
544 $question->partiallycorrectfeedback['text'] = '';
545 $question->partiallycorrectfeedback['format'] = $question->questiontextformat;
546 $question->partiallycorrectfeedback['files'] = array();
547 $question->incorrectfeedback['text'] = '';
548 $question->incorrectfeedback['format'] = $question->questiontextformat;
549 $question->incorrectfeedback['files'] = array();
550 return $question;
84769fd8 551 }
552
c7df5006 553 protected function repchar($text, $notused = 0) {
13bb604e
TH
554 // Escapes 'reserved' characters # = ~ {) :
555 // Removes new lines
69a600a0
TH
556 $reserved = array( '\\', '#', '=', '~', '{', '}', ':', "\n", "\r");
557 $escaped = array('\\\\', '\#','\=','\~','\{','\}','\:', '\n', '' );
84769fd8 558
13bb604e
TH
559 $newtext = str_replace($reserved, $escaped, $text);
560 return $newtext;
84769fd8 561 }
562
13bb604e 563 /**
f7970e3c 564 * @param int $format one of the FORMAT_ constants.
13bb604e
TH
565 * @return string the corresponding name.
566 */
c7df5006 567 protected function format_const_to_name($format) {
13bb604e
TH
568 if ($format == FORMAT_MOODLE) {
569 return 'moodle';
570 } else if ($format == FORMAT_HTML) {
571 return 'html';
572 } else if ($format == FORMAT_PLAIN) {
573 return 'plain';
574 } else if ($format == FORMAT_MARKDOWN) {
575 return 'markdown';
09db6da2 576 } else {
13bb604e 577 return 'moodle';
84769fd8 578 }
13bb604e 579 }
36e2232e 580
13bb604e 581 /**
f7970e3c 582 * @param int $format one of the FORMAT_ constants.
13bb604e
TH
583 * @return string the corresponding name.
584 */
c7df5006 585 protected function format_name_to_const($format) {
13bb604e
TH
586 if ($format == 'moodle') {
587 return FORMAT_MOODLE;
588 } else if ($format == 'html') {
5b744492 589 return FORMAT_HTML;
13bb604e
TH
590 } else if ($format == 'plain') {
591 return FORMAT_PLAIN;
592 } else if ($format == 'markdown') {
593 return FORMAT_MARKDOWN;
594 } else {
595 return -1;
84769fd8 596 }
13bb604e
TH
597 }
598
599 public function write_name($name) {
600 return '::' . $this->repchar($name) . '::';
601 }
602
5b744492 603 public function write_questiontext($text, $format, $defaultformat = FORMAT_MOODLE) {
13bb604e 604 $output = '';
5b744492 605 if ($text != '' && $format != $defaultformat) {
13bb604e 606 $output .= '[' . $this->format_const_to_name($format) . ']';
84769fd8 607 }
13bb604e
TH
608 $output .= $this->repchar($text, $format);
609 return $output;
610 }
611
c7df5006 612 public function writequestion($question) {
d649fb02 613 global $OUTPUT;
13bb604e
TH
614
615 // Start with a comment
5b744492 616 $expout = "// question: $question->id name: $question->name\n";
13bb604e
TH
617
618 // output depends on question type
619 switch($question->qtype) {
620
621 case 'category':
622 // not a real question, used to insert category switch
623 $expout .= "\$CATEGORY: $question->category\n";
624 break;
625
626 case DESCRIPTION:
627 $expout .= $this->write_name($question->name);
628 $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
629 break;
630
631 case ESSAY:
632 $expout .= $this->write_name($question->name);
633 $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
634 $expout .= "{}\n";
635 break;
636
637 case TRUEFALSE:
638 $trueanswer = $question->options->answers[$question->options->trueanswer];
639 $falseanswer = $question->options->answers[$question->options->falseanswer];
640 if ($trueanswer->fraction == 1) {
641 $answertext = 'TRUE';
5b744492
TH
642 $rightfeedback = $this->write_questiontext($trueanswer->feedback,
643 $trueanswer->feedbackformat, $question->questiontextformat);
644 $wrongfeedback = $this->write_questiontext($falseanswer->feedback,
645 $falseanswer->feedbackformat, $question->questiontextformat);
13bb604e
TH
646 } else {
647 $answertext = 'FALSE';
5b744492
TH
648 $rightfeedback = $this->write_questiontext($falseanswer->feedback,
649 $falseanswer->feedbackformat, $question->questiontextformat);
650 $wrongfeedback = $this->write_questiontext($trueanswer->feedback,
651 $trueanswer->feedbackformat, $question->questiontextformat);
13bb604e
TH
652 }
653
13bb604e
TH
654 $expout .= $this->write_name($question->name);
655 $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
656 $expout .= '{' . $this->repchar($answertext);
5b744492
TH
657 if ($wrongfeedback) {
658 $expout .= '#' . $wrongfeedback;
659 } else if ($rightfeedback) {
13bb604e 660 $expout .= '#';
84769fd8 661 }
5b744492
TH
662 if ($rightfeedback) {
663 $expout .= '#' . $rightfeedback;
84769fd8 664 }
13bb604e
TH
665 $expout .= "}\n";
666 break;
667
668 case MULTICHOICE:
669 $expout .= $this->write_name($question->name);
670 $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
671 $expout .= "{\n";
672 foreach($question->options->answers as $answer) {
673 if ($answer->fraction == 1) {
674 $answertext = '=';
675 } else if ($answer->fraction == 0) {
676 $answertext = '~';
677 } else {
678 $weight = $answer->fraction * 100;
679 $answertext = '~%' . $weight . '%';
680 }
5b744492
TH
681 $expout .= "\t" . $answertext . $this->write_questiontext($answer->answer,
682 $answer->answerformat, $question->questiontextformat);
13bb604e 683 if ($answer->feedback != '') {
5b744492
TH
684 $expout .= '#' . $this->write_questiontext($answer->feedback,
685 $answer->feedbackformat, $question->questiontextformat);
13bb604e
TH
686 }
687 $expout .= "\n";
84769fd8 688 }
13bb604e
TH
689 $expout .= "}\n";
690 break;
691
692 case SHORTANSWER:
693 $expout .= $this->write_name($question->name);
694 $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
695 $expout .= "{\n";
696 foreach($question->options->answers as $answer) {
697 $weight = 100 * $answer->fraction;
698 $expout .= "\t=%" . $weight . '%' . $this->repchar($answer->answer) .
5b744492
TH
699 '#' . $this->write_questiontext($answer->feedback,
700 $answer->feedbackformat, $question->questiontextformat) . "\n";
84769fd8 701 }
13bb604e
TH
702 $expout .= "}\n";
703 break;
704
705 case NUMERICAL:
706 $expout .= $this->write_name($question->name);
707 $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
708 $expout .= "{#\n";
709 foreach ($question->options->answers as $answer) {
710 if ($answer->answer != '' && $answer->answer != '*') {
711 $weight = 100 * $answer->fraction;
712 $expout .= "\t=%" . $weight . '%' . $answer->answer . ':' .
5b744492
TH
713 (float)$answer->tolerance . '#' . $this->write_questiontext($answer->feedback,
714 $answer->feedbackformat, $question->questiontextformat) . "\n";
13bb604e 715 } else {
5b744492
TH
716 $expout .= "\t~#" . $this->write_questiontext($answer->feedback,
717 $answer->feedbackformat, $question->questiontextformat) . "\n";
13e88db4 718 }
13bb604e
TH
719 }
720 $expout .= "}\n";
721 break;
722
723 case MATCH:
724 $expout .= $this->write_name($question->name);
725 $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
726 $expout .= "{\n";
727 foreach($question->options->subquestions as $subquestion) {
3c781845
TH
728 $expout .= "\t=" . $this->write_questiontext($subquestion->questiontext,
729 $subquestion->questiontextformat, $question->questiontextformat) .
13bb604e
TH
730 ' -> ' . $this->repchar($subquestion->answertext) . "\n";
731 }
732 $expout .= "}\n";
733 break;
734
735 default:
736 // Check for plugins
737 if ($out = $this->try_exporting_using_qtypes($question->qtype, $question)) {
738 $expout .= $out;
1fe641f7 739 } else {
13bb604e
TH
740 $expout .= "Question type $question->qtype is not supported\n";
741 echo $OUTPUT->notification(get_string('nohandler', 'qformat_gift',
d649fb02 742 question_bank::get_qtype_name($question->qtype)));
1fe641f7 743 }
744 }
13bb604e
TH
745
746 // Add empty line to delimit questions
747 $expout .= "\n";
748 return $expout;
84769fd8 749 }
84769fd8 750}