MDL-39283 GIFT and XML question export broken for unsupported qtypes
[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);
5df0a6b4 209 $question->name = $this->clean_question_name($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) {
5df0a6b4 254 $question->name = $this->create_default_question_name($question->questiontext, get_string('questionname', 'question'));
13bb604e 255 }
84769fd8 256
09db6da2 257 // determine QUESTION TYPE
84769fd8 258 $question->qtype = NULL;
259
4433fff5 260 // give plugins first try
261 // plugins must promise not to intercept standard qtypes
3ea69b8d 262 // MDL-12346, this could be called from lesson mod which has its own base class =(
13bb604e 263 if (method_exists($this, 'try_importing_using_qtypes') && ($try_question = $this->try_importing_using_qtypes($lines, $question, $answertext))) {
d510ec64 264 return $try_question;
4433fff5 265 }
266
e8825e72 267 if ($description) {
268 $question->qtype = DESCRIPTION;
13bb604e
TH
269
270 } else if ($answertext == '') {
e8825e72 271 $question->qtype = ESSAY;
13bb604e
TH
272
273 } else if ($answertext{0} == '#') {
84769fd8 274 $question->qtype = NUMERICAL;
275
13bb604e 276 } else if (strpos($answertext, '~') !== false) {
84769fd8 277 // only Multiplechoice questions contain tilde ~
278 $question->qtype = MULTICHOICE;
aeb15530 279
13bb604e
TH
280 } else if (strpos($answertext, '=') !== false
281 && strpos($answertext, '->') !== false) {
09db6da2 282 // only Matching contains both = and ->
84769fd8 283 $question->qtype = MATCH;
284
285 } else { // either TRUEFALSE or SHORTANSWER
aeb15530 286
84769fd8 287 // TRUEFALSE question check
288 $truefalse_check = $answertext;
13bb604e 289 if (strpos($answertext, '#') > 0) {
84769fd8 290 // strip comments to check for TrueFalse question
291 $truefalse_check = trim(substr($answertext, 0, strpos($answertext,"#")));
292 }
293
13bb604e 294 $valid_tf_answers = array('T', 'TRUE', 'F', 'FALSE');
84769fd8 295 if (in_array($truefalse_check, $valid_tf_answers)) {
296 $question->qtype = TRUEFALSE;
297
298 } else { // Must be SHORTANSWER
13bb604e 299 $question->qtype = SHORTANSWER;
84769fd8 300 }
301 }
302
303 if (!isset($question->qtype)) {
5e8a85aa 304 $giftqtypenotset = get_string('giftqtypenotset', 'qformat_gift');
13bb604e 305 $this->error($giftqtypenotset, $text);
84769fd8 306 return false;
307 }
308
309 switch ($question->qtype) {
e8825e72 310 case DESCRIPTION:
49e2bba7 311 $question->defaultmark = 0;
3f5633df 312 $question->length = 0;
e8825e72 313 return $question;
3c781845 314
e8825e72 315 case ESSAY:
0519420f
TH
316 $question->responseformat = 'editor';
317 $question->responsefieldlines = 15;
318 $question->attachments = 0;
319 $question->graderinfo = array(
320 'text' => '', 'format' => FORMAT_HTML, 'files' => array());
e8825e72 321 return $question;
3c781845 322
84769fd8 323 case MULTICHOICE:
324 if (strpos($answertext,"=") === false) {
13bb604e 325 $question->single = 0; // multiple answers are enabled if no single answer is 100% correct
84769fd8 326 } else {
13bb604e 327 $question->single = 1; // only one answer allowed (the default)
84769fd8 328 }
3c781845 329 $question = $this->add_blank_combined_feedback($question);
84769fd8 330
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 }
aeb15530 339
84769fd8 340 $countanswers = count($answers);
aeb15530 341
13bb604e 342 if (!$this->check_answer_count(2, $answers, $text)) {
2befe778 343 return false;
84769fd8 344 }
345
346 foreach ($answers as $key => $answer) {
347 $answer = trim($answer);
348
349 // determine answer weight
13bb604e 350 if ($answer[0] == '=') {
84769fd8 351 $answer_weight = 1;
352 $answer = substr($answer, 1);
aeb15530 353
13bb604e 354 } else if (preg_match($gift_answerweight_regex, $answer)) { // check for properly formatted answer weight
84769fd8 355 $answer_weight = $this->answerweightparser($answer);
aeb15530 356
84769fd8 357 } else { //default, i.e., wrong anwer
358 $answer_weight = 0;
359 }
5b744492
TH
360 list($question->answer[$key], $question->feedback[$key]) =
361 $this->commentparser($answer, $question->questiontextformat);
84769fd8 362 $question->fraction[$key] = $answer_weight;
84769fd8 363 } // end foreach answer
aeb15530 364
84769fd8 365 return $question;
84769fd8 366
367 case MATCH:
3c781845
TH
368 $question = $this->add_blank_combined_feedback($question);
369
13bb604e 370 $answers = explode('=', $answertext);
84769fd8 371 if (isset($answers[0])) {
372 $answers[0] = trim($answers[0]);
373 }
374 if (empty($answers[0])) {
375 array_shift($answers);
376 }
aeb15530 377
13bb604e 378 if (!$this->check_answer_count(2,$answers,$text)) {
2befe778 379 return false;
84769fd8 380 }
aeb15530 381
84769fd8 382 foreach ($answers as $key => $answer) {
383 $answer = trim($answer);
87ee4968 384 if (strpos($answer, "->") === false) {
3c781845 385 $this->error(get_string('giftmatchingformat','qformat_gift'), $answer);
84769fd8 386 return false;
84769fd8 387 }
388
13bb604e 389 $marker = strpos($answer, '->');
5b744492
TH
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 }
aeb15530 395
84769fd8 396 return $question;
aeb15530 397
84769fd8 398 case TRUEFALSE:
5b744492
TH
399 list($answer, $wrongfeedback, $rightfeedback) =
400 $this->split_truefalse_comment($answertext, $question->questiontextformat);
84769fd8 401
2c00fa16 402 if ($answer['text'] == "T" OR $answer['text'] == "TRUE") {
5b744492
TH
403 $question->correctanswer = 1;
404 $question->feedbacktrue = $rightfeedback;
405 $question->feedbackfalse = $wrongfeedback;
84769fd8 406 } else {
5b744492
TH
407 $question->correctanswer = 0;
408 $question->feedbacktrue = $wrongfeedback;
409 $question->feedbackfalse = $rightfeedback;
84769fd8 410 }
411
0d4c63d7 412 $question->penalty = 1;
7939a4a0 413
84769fd8 414 return $question;
aeb15530 415
84769fd8 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 }
aeb15530 425
5b744492 426 if (!$this->check_answer_count(1, $answers, $text)) {
2befe778 427 return false;
84769fd8 428 }
429
430 foreach ($answers as $key => $answer) {
431 $answer = trim($answer);
432
5b744492 433 // Answer weight
6dbcacee 434 if (preg_match($gift_answerweight_regex, $answer)) { // check for properly formatted answer weight
84769fd8 435 $answer_weight = $this->answerweightparser($answer);
436 } else { //default, i.e., full-credit anwer
437 $answer_weight = 1;
438 }
5b744492
TH
439
440 list($answer, $question->feedback[$key]) = $this->commentparser(
441 $answer, $question->questiontextformat);
442
443 $question->answer[$key] = $answer['text'];
13bb604e 444 $question->fraction[$key] = $answer_weight;
5b744492 445 }
84769fd8 446
84769fd8 447 return $question;
84769fd8 448
449 case NUMERICAL:
450 // Note similarities to ShortAnswer
451 $answertext = substr($answertext, 1); // remove leading "#"
452
1fe641f7 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 }
460
84769fd8 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 }
aeb15530 468
84769fd8 469 if (count($answers) == 0) {
470 // invalid question
5e8a85aa 471 $giftnonumericalanswers = get_string('giftnonumericalanswers','qformat_gift');
13bb604e 472 $this->error($giftnonumericalanswers, $text);
84769fd8 473 return false;
84769fd8 474 }
475
476 foreach ($answers as $key => $answer) {
477 $answer = trim($answer);
478
479 // Answer weight
6dbcacee 480 if (preg_match($gift_answerweight_regex, $answer)) { // check for properly formatted answer weight
84769fd8 481 $answer_weight = $this->answerweightparser($answer);
482 } else { //default, i.e., full-credit anwer
483 $answer_weight = 1;
484 }
5b744492
TH
485
486 list($answer, $question->feedback[$key]) = $this->commentparser(
487 $answer, $question->questiontextformat);
84769fd8 488 $question->fraction[$key] = $answer_weight;
5b744492 489 $answer = $answer['text'];
84769fd8 490
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;
13bb604e
TH
498 } else if (strpos($answer, ':') > 0) { // standard [answer]:[errormargin] format
499 $marker = strpos($answer, ':');
84769fd8 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 }
aeb15530 506
09db6da2 507 if (!(is_numeric($ans) || $ans = '*') || !is_numeric($tol)) {
13bb604e
TH
508 $errornotnumbers = get_string('errornotnumbers');
509 $this->error($errornotnumbers, $text);
84769fd8 510 return false;
84769fd8 511 }
aeb15530 512
84769fd8 513 // store results
514 $question->answer[$key] = $ans;
515 $question->tolerance[$key] = $tol;
5b744492 516 }
84769fd8 517
1fe641f7 518 if ($wrongfeedback) {
519 $key += 1;
520 $question->fraction[$key] = 0;
5b744492
TH
521 list($notused, $question->feedback[$key]) = $this->commentparser(
522 $wrongfeedback, $question->questiontextformat);
13bb604e 523 $question->answer[$key] = '*';
1fe641f7 524 $question->tolerance[$key] = '';
525 }
526
84769fd8 527 return $question;
84769fd8 528
3c781845
TH
529 default:
530 $this->error(get_string('giftnovalidquestion', 'qformat_gift'), $text);
531 return false;
aeb15530 532
13bb604e 533 }
3c781845 534 }
84769fd8 535
c7df5006 536 protected function repchar($text, $notused = 0) {
13bb604e
TH
537 // Escapes 'reserved' characters # = ~ {) :
538 // Removes new lines
69a600a0
TH
539 $reserved = array( '\\', '#', '=', '~', '{', '}', ':', "\n", "\r");
540 $escaped = array('\\\\', '\#','\=','\~','\{','\}','\:', '\n', '' );
84769fd8 541
13bb604e
TH
542 $newtext = str_replace($reserved, $escaped, $text);
543 return $newtext;
84769fd8 544 }
545
13bb604e 546 /**
f7970e3c 547 * @param int $format one of the FORMAT_ constants.
13bb604e
TH
548 * @return string the corresponding name.
549 */
c7df5006 550 protected function format_const_to_name($format) {
13bb604e
TH
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';
09db6da2 559 } else {
13bb604e 560 return 'moodle';
84769fd8 561 }
13bb604e 562 }
36e2232e 563
13bb604e 564 /**
f7970e3c 565 * @param int $format one of the FORMAT_ constants.
13bb604e
TH
566 * @return string the corresponding name.
567 */
c7df5006 568 protected function format_name_to_const($format) {
13bb604e
TH
569 if ($format == 'moodle') {
570 return FORMAT_MOODLE;
571 } else if ($format == 'html') {
5b744492 572 return FORMAT_HTML;
13bb604e
TH
573 } else if ($format == 'plain') {
574 return FORMAT_PLAIN;
575 } else if ($format == 'markdown') {
576 return FORMAT_MARKDOWN;
577 } else {
578 return -1;
84769fd8 579 }
13bb604e
TH
580 }
581
582 public function write_name($name) {
583 return '::' . $this->repchar($name) . '::';
584 }
585
5b744492 586 public function write_questiontext($text, $format, $defaultformat = FORMAT_MOODLE) {
13bb604e 587 $output = '';
5b744492 588 if ($text != '' && $format != $defaultformat) {
13bb604e 589 $output .= '[' . $this->format_const_to_name($format) . ']';
84769fd8 590 }
13bb604e
TH
591 $output .= $this->repchar($text, $format);
592 return $output;
593 }
594
c7df5006 595 public function writequestion($question) {
d649fb02 596 global $OUTPUT;
13bb604e
TH
597
598 // Start with a comment
5b744492 599 $expout = "// question: $question->id name: $question->name\n";
13bb604e
TH
600
601 // output depends on question type
602 switch($question->qtype) {
603
604 case 'category':
605 // not a real question, used to insert category switch
606 $expout .= "\$CATEGORY: $question->category\n";
607 break;
608
609 case DESCRIPTION:
610 $expout .= $this->write_name($question->name);
611 $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
612 break;
613
614 case ESSAY:
615 $expout .= $this->write_name($question->name);
616 $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
617 $expout .= "{}\n";
618 break;
619
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';
5b744492
TH
625 $rightfeedback = $this->write_questiontext($trueanswer->feedback,
626 $trueanswer->feedbackformat, $question->questiontextformat);
627 $wrongfeedback = $this->write_questiontext($falseanswer->feedback,
628 $falseanswer->feedbackformat, $question->questiontextformat);
13bb604e
TH
629 } else {
630 $answertext = 'FALSE';
5b744492
TH
631 $rightfeedback = $this->write_questiontext($falseanswer->feedback,
632 $falseanswer->feedbackformat, $question->questiontextformat);
633 $wrongfeedback = $this->write_questiontext($trueanswer->feedback,
634 $trueanswer->feedbackformat, $question->questiontextformat);
13bb604e
TH
635 }
636
13bb604e
TH
637 $expout .= $this->write_name($question->name);
638 $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
639 $expout .= '{' . $this->repchar($answertext);
5b744492
TH
640 if ($wrongfeedback) {
641 $expout .= '#' . $wrongfeedback;
642 } else if ($rightfeedback) {
13bb604e 643 $expout .= '#';
84769fd8 644 }
5b744492
TH
645 if ($rightfeedback) {
646 $expout .= '#' . $rightfeedback;
84769fd8 647 }
13bb604e
TH
648 $expout .= "}\n";
649 break;
650
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 }
5b744492
TH
664 $expout .= "\t" . $answertext . $this->write_questiontext($answer->answer,
665 $answer->answerformat, $question->questiontextformat);
13bb604e 666 if ($answer->feedback != '') {
5b744492
TH
667 $expout .= '#' . $this->write_questiontext($answer->feedback,
668 $answer->feedbackformat, $question->questiontextformat);
13bb604e
TH
669 }
670 $expout .= "\n";
84769fd8 671 }
13bb604e
TH
672 $expout .= "}\n";
673 break;
674
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) .
5b744492
TH
682 '#' . $this->write_questiontext($answer->feedback,
683 $answer->feedbackformat, $question->questiontextformat) . "\n";
84769fd8 684 }
13bb604e
TH
685 $expout .= "}\n";
686 break;
687
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 . ':' .
5b744492
TH
696 (float)$answer->tolerance . '#' . $this->write_questiontext($answer->feedback,
697 $answer->feedbackformat, $question->questiontextformat) . "\n";
13bb604e 698 } else {
5b744492
TH
699 $expout .= "\t~#" . $this->write_questiontext($answer->feedback,
700 $answer->feedbackformat, $question->questiontextformat) . "\n";
13e88db4 701 }
13bb604e
TH
702 }
703 $expout .= "}\n";
704 break;
705
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) {
3c781845
TH
711 $expout .= "\t=" . $this->write_questiontext($subquestion->questiontext,
712 $subquestion->questiontextformat, $question->questiontextformat) .
13bb604e
TH
713 ' -> ' . $this->repchar($subquestion->answertext) . "\n";
714 }
715 $expout .= "}\n";
716 break;
717
718 default:
719 // Check for plugins
720 if ($out = $this->try_exporting_using_qtypes($question->qtype, $question)) {
721 $expout .= $out;
1fe641f7 722 }
723 }
13bb604e
TH
724
725 // Add empty line to delimit questions
726 $expout .= "\n";
727 return $expout;
84769fd8 728 }
84769fd8 729}