quiz MDL-25297 fixing quiz messaging issues
[moodle.git] / question / format / gift / format.php
CommitLineData
aeb15530 1<?php
f1abd39f 2//
84769fd8 3///////////////////////////////////////////////////////////////
aeb15530 4// The GIFT import filter was designed as an easy to use method
84769fd8 5// for teachers writing questions as a text file. It supports most
6// question types and the missing word format.
7//
8// Multiple Choice / Missing Word
9// Who's buried in Grant's tomb?{~Grant ~Jefferson =no one}
10// Grant is {~buried =entombed ~living} in Grant's tomb.
11// True-False:
12// Grant is buried in Grant's tomb.{FALSE}
13// Short-Answer.
14// Who's buried in Grant's tomb?{=no one =nobody}
15// Numerical
16// When was Ulysses S. Grant born?{#1822:5}
17// Matching
18// Match the following countries with their corresponding
19// capitals.{=Canada->Ottawa =Italy->Rome =Japan->Tokyo}
20//
aeb15530
PS
21// Comment lines start with a double backslash (//).
22// Optional question names are enclosed in double colon(::).
84769fd8 23// Answer feedback is indicated with hash mark (#).
24// Percentage answer weights immediately follow the tilde (for
25// multiple choice) or equal sign (for short answer and numerical),
26// and are enclosed in percent signs (% %). See docs and examples.txt for more.
aeb15530
PS
27//
28// This filter was written through the collaboration of numerous
29// members of the Moodle community. It was originally based on
84769fd8 30// the missingword format, which included code from Thomas Robb
31// and others. Paul Tsuchido Shew wrote this filter in December 2003.
32//////////////////////////////////////////////////////////////////////////
33// Based on default.php, included by ../import.php
41a89a07 34/**
35 * @package questionbank
36 * @subpackage importexport
37 */
f5565b69 38class qformat_gift extends qformat_default {
84769fd8 39
40 function provide_import() {
09db6da2 41 return true;
84769fd8 42 }
43
44 function provide_export() {
09db6da2 45 return true;
84769fd8 46 }
47
48 function answerweightparser(&$answer) {
49 $answer = substr($answer, 1); // removes initial %
50 $end_position = strpos($answer, "%");
51 $answer_weight = substr($answer, 0, $end_position); // gets weight as integer
52 $answer_weight = $answer_weight/100; // converts to percent
53 $answer = substr($answer, $end_position+1); // removes comment from answer
54 return $answer_weight;
55 }
56
84769fd8 57 function commentparser(&$answer) {
13bb604e 58 if (strpos($answer,"#") > 0) {
84769fd8 59 $hashpos = strpos($answer,"#");
60 $comment = substr($answer, $hashpos+1);
294ce987 61 $comment = trim($this->escapedchar_post($comment));
84769fd8 62 $answer = substr($answer, 0, $hashpos);
63 } else {
64 $comment = " ";
65 }
66 return $comment;
67 }
68
13bb604e 69 function split_truefalse_comment($comment) {
09db6da2 70 // splits up comment around # marks
71 // returns an array of true/false feedback
72 $bits = explode('#',$comment);
73 $feedback = array('wrong' => $bits[0]);
74 if (count($bits) >= 2) {
75 $feedback['right'] = $bits[1];
76 } else {
77 $feedback['right'] = '';
78 }
79 return $feedback;
84769fd8 80 }
aeb15530 81
84769fd8 82 function escapedchar_pre($string) {
83 //Replaces escaped control characters with a placeholder BEFORE processing
aeb15530 84
13bb604e
TH
85 $escapedcharacters = array("\\:", "\\#", "\\=", "\\{", "\\}", "\\~", "\\n" ); //dlnsk
86 $placeholders = array("&&058;", "&&035;", "&&061;", "&&123;", "&&125;", "&&126;", "&&010"); //dlnsk
84769fd8 87
88 $string = str_replace("\\\\", "&&092;", $string);
89 $string = str_replace($escapedcharacters, $placeholders, $string);
90 $string = str_replace("&&092;", "\\", $string);
91 return $string;
92 }
93
94 function escapedchar_post($string) {
95 //Replaces placeholders with corresponding character AFTER processing is done
dfdce7fb 96 $placeholders = array("&&058;", "&&035;", "&&061;", "&&123;", "&&125;", "&&126;", "&&010"); //dlnsk
13bb604e 97 $characters = array(":", "#", "=", "{", "}", "~", "\n" ); //dlnsk
84769fd8 98 $string = str_replace($placeholders, $characters, $string);
99 return $string;
100 }
101
13bb604e 102 function check_answer_count($min, $answers, $text) {
09db6da2 103 $countanswers = count($answers);
104 if ($countanswers < $min) {
13bb604e
TH
105 $importminerror = get_string('importminerror', 'quiz');
106 $this->error($importminerror, $text);
09db6da2 107 return false;
108 }
109
110 return true;
84769fd8 111 }
112
113
114 function readquestion($lines) {
115 // Given an array of lines known to define a question in this format, this function
116 // converts it into a question object suitable for processing and insertion into Moodle.
117
118 $question = $this->defaultquestion();
119 $comment = NULL;
120 // define replaced by simple assignment, stop redefine notices
13bb604e 121 $gift_answerweight_regex = '/^%\-*([0-9]{1,2})\.?([0-9]*)%/';
84769fd8 122
123 // REMOVED COMMENTED LINES and IMPLODE
124 foreach ($lines as $key => $line) {
09db6da2 125 $line = trim($line);
13bb604e
TH
126 if (substr($line, 0, 2) == '//') {
127 $lines[$key] = ' ';
09db6da2 128 }
84769fd8 129 }
130
13bb604e 131 $text = trim(implode(' ', $lines));
84769fd8 132
13bb604e 133 if ($text == '') {
84769fd8 134 return false;
135 }
136
137 // Substitute escaped control characters with placeholders
138 $text = $this->escapedchar_pre($text);
139
b39c7aad 140 // Look for category modifier
13bb604e 141 if (preg_match('~^\$CATEGORY:~', $text)) {
5363b555 142 // $newcategory = $matches[1];
13bb604e 143 $newcategory = trim(substr($text, 10));
b39c7aad 144
145 // build fake question to contain category
146 $question->qtype = 'category';
147 $question->category = $newcategory;
148 return $question;
149 }
aeb15530 150
84769fd8 151 // QUESTION NAME parser
13bb604e 152 if (substr($text, 0, 2) == '::') {
84769fd8 153 $text = substr($text, 2);
154
13bb604e 155 $namefinish = strpos($text, '::');
84769fd8 156 if ($namefinish === false) {
157 $question->name = false;
158 // name will be assigned after processing question text below
09db6da2 159 } else {
84769fd8 160 $questionname = substr($text, 0, $namefinish);
294ce987 161 $question->name = trim($this->escapedchar_post($questionname));
84769fd8 162 $text = trim(substr($text, $namefinish+2)); // Remove name from text
163 }
164 } else {
165 $question->name = false;
166 }
167
168
169 // FIND ANSWER section
e8825e72 170 // no answer means its a description
13bb604e
TH
171 $answerstart = strpos($text, '{');
172 $answerfinish = strpos($text, '}');
e8825e72 173
174 $description = false;
175 if (($answerstart === false) and ($answerfinish === false)) {
176 $description = true;
177 $answertext = '';
178 $answerlength = 0;
13bb604e
TH
179 } else if (!(($answerstart !== false) and ($answerfinish !== false))) {
180 $this->error(get_string('braceerror', 'quiz'), $text);
84769fd8 181 return false;
13bb604e 182 } else {
e8825e72 183 $answerlength = $answerfinish - $answerstart;
184 $answertext = trim(substr($text, $answerstart + 1, $answerlength - 1));
185 }
84769fd8 186
187 // Format QUESTION TEXT without answer, inserting "_____" as necessary
e8825e72 188 if ($description) {
189 $questiontext = $text;
13bb604e 190 } else if (substr($text, -1) == "}") {
84769fd8 191 // no blank line if answers follow question, outside of closing punctuation
192 $questiontext = substr_replace($text, "", $answerstart, $answerlength+1);
193 } else {
194 // inserts blank line for missing word format
195 $questiontext = substr_replace($text, "_____", $answerstart, $answerlength+1);
196 }
197
13bb604e
TH
198 // Get questiontext format from questiontext
199 $questiontextformat = FORMAT_MOODLE;
200 if (strpos($questiontext, '[') === 0) {
201 $formatend = strpos($questiontext, ']');
202 $questiontextformat = $this->format_name_to_const(substr($questiontext, 1, $formatend));
203 if ($questiontextformat == -1) {
204 $questiontextformat = FORMAT_MOODLE;
205 } else {
206 $questiontext = substr($questiontext, $formatend + 1);
aeb15530 207 }
84769fd8 208 }
209 $question->questiontextformat = $questiontextformat;
294ce987 210 $question->questiontext = trim($this->escapedchar_post($questiontext));
84769fd8 211
212 // set question name if not already set
213 if ($question->name === false) {
214 $question->name = $question->questiontext;
13bb604e 215 }
84769fd8 216
8daaaafc 217 // ensure name is not longer than 250 characters
13bb604e
TH
218 $question->name = shorten_text($question->name, 200);
219 $question->name = strip_tags(substr($question->name, 0, 250));
84769fd8 220
09db6da2 221 // determine QUESTION TYPE
84769fd8 222 $question->qtype = NULL;
223
4433fff5 224 // give plugins first try
225 // plugins must promise not to intercept standard qtypes
3ea69b8d 226 // MDL-12346, this could be called from lesson mod which has its own base class =(
13bb604e 227 if (method_exists($this, 'try_importing_using_qtypes') && ($try_question = $this->try_importing_using_qtypes($lines, $question, $answertext))) {
d510ec64 228 return $try_question;
4433fff5 229 }
230
e8825e72 231 if ($description) {
232 $question->qtype = DESCRIPTION;
13bb604e
TH
233
234 } else if ($answertext == '') {
e8825e72 235 $question->qtype = ESSAY;
13bb604e
TH
236
237 } else if ($answertext{0} == '#') {
84769fd8 238 $question->qtype = NUMERICAL;
239
13bb604e 240 } else if (strpos($answertext, '~') !== false) {
84769fd8 241 // only Multiplechoice questions contain tilde ~
242 $question->qtype = MULTICHOICE;
aeb15530 243
13bb604e
TH
244 } else if (strpos($answertext, '=') !== false
245 && strpos($answertext, '->') !== false) {
09db6da2 246 // only Matching contains both = and ->
84769fd8 247 $question->qtype = MATCH;
248
249 } else { // either TRUEFALSE or SHORTANSWER
aeb15530 250
84769fd8 251 // TRUEFALSE question check
252 $truefalse_check = $answertext;
13bb604e 253 if (strpos($answertext, '#') > 0) {
84769fd8 254 // strip comments to check for TrueFalse question
255 $truefalse_check = trim(substr($answertext, 0, strpos($answertext,"#")));
256 }
257
13bb604e 258 $valid_tf_answers = array('T', 'TRUE', 'F', 'FALSE');
84769fd8 259 if (in_array($truefalse_check, $valid_tf_answers)) {
260 $question->qtype = TRUEFALSE;
261
262 } else { // Must be SHORTANSWER
13bb604e 263 $question->qtype = SHORTANSWER;
84769fd8 264 }
265 }
266
267 if (!isset($question->qtype)) {
13bb604e
TH
268 $giftqtypenotset = get_string('giftqtypenotset', 'quiz');
269 $this->error($giftqtypenotset, $text);
84769fd8 270 return false;
271 }
272
273 switch ($question->qtype) {
e8825e72 274 case DESCRIPTION:
3f5633df 275 $question->defaultgrade = 0;
276 $question->length = 0;
e8825e72 277 return $question;
278 break;
279 case ESSAY:
e8825e72 280 $question->fraction = 0;
13bb604e
TH
281 $question->feedback['text'] = '';
282 $question->feedback['format'] = $questiontextformat;
283 $question->feedback['files'] = array();
e8825e72 284 return $question;
285 break;
84769fd8 286 case MULTICHOICE:
287 if (strpos($answertext,"=") === false) {
13bb604e 288 $question->single = 0; // multiple answers are enabled if no single answer is 100% correct
84769fd8 289 } else {
13bb604e 290 $question->single = 1; // only one answer allowed (the default)
84769fd8 291 }
13bb604e
TH
292 $question->correctfeedback['text'] = '';
293 $question->correctfeedback['format'] = $questiontextformat;
294 $question->correctfeedback['files'] = array();
295 $question->partiallycorrectfeedback['text'] = '';
296 $question->partiallycorrectfeedback['format'] = $questiontextformat;
297 $question->partiallycorrectfeedback['files'] = array();
298 $question->incorrectfeedback['text'] = '';
299 $question->incorrectfeedback['format'] = $questiontextformat;
300 $question->incorrectfeedback['files'] = array();
84769fd8 301
302 $answertext = str_replace("=", "~=", $answertext);
303 $answers = explode("~", $answertext);
304 if (isset($answers[0])) {
305 $answers[0] = trim($answers[0]);
306 }
307 if (empty($answers[0])) {
308 array_shift($answers);
309 }
aeb15530 310
84769fd8 311 $countanswers = count($answers);
aeb15530 312
13bb604e 313 if (!$this->check_answer_count(2, $answers, $text)) {
2befe778 314 return false;
315 break;
84769fd8 316 }
317
318 foreach ($answers as $key => $answer) {
319 $answer = trim($answer);
320
321 // determine answer weight
13bb604e 322 if ($answer[0] == '=') {
84769fd8 323 $answer_weight = 1;
324 $answer = substr($answer, 1);
aeb15530 325
13bb604e 326 } else if (preg_match($gift_answerweight_regex, $answer)) { // check for properly formatted answer weight
84769fd8 327 $answer_weight = $this->answerweightparser($answer);
aeb15530 328
84769fd8 329 } else { //default, i.e., wrong anwer
330 $answer_weight = 0;
331 }
13bb604e
TH
332 $question->answer[$key]['text'] =
333 $this->escapedchar_post($answer);
334 $question->answer[$key]['format'] = $questiontextformat;
335 $question->answer[$key]['files'] = array();
84769fd8 336 $question->fraction[$key] = $answer_weight;
13bb604e
TH
337 $question->feedback[$key]['text'] = $this->commentparser($answer); // commentparser also removes comment from $answer
338 $question->feedback[$key]['format'] = $questiontextformat;
339 $question->feedback[$key]['files'] = array();
84769fd8 340 } // end foreach answer
aeb15530 341
84769fd8 342 //$question->defaultgrade = 1;
343 //$question->image = ""; // No images with this format
344 return $question;
345 break;
346
347 case MATCH:
13bb604e 348 $answers = explode('=', $answertext);
84769fd8 349 if (isset($answers[0])) {
350 $answers[0] = trim($answers[0]);
351 }
352 if (empty($answers[0])) {
353 array_shift($answers);
354 }
aeb15530 355
13bb604e 356 if (!$this->check_answer_count(2,$answers,$text)) {
2befe778 357 return false;
358 break;
84769fd8 359 }
aeb15530 360
84769fd8 361 foreach ($answers as $key => $answer) {
362 $answer = trim($answer);
87ee4968 363 if (strpos($answer, "->") === false) {
532344b2 364 $giftmatchingformat = get_string('giftmatchingformat','quiz');
13bb604e 365 $this->error($giftmatchingformat, $answer);
84769fd8 366 return false;
367 break 2;
368 }
369
13bb604e
TH
370 $marker = strpos($answer, '->');
371 $question->subquestions[$key]['text'] =
372 trim($this->escapedchar_post(substr($answer, 0, $marker)));
373 $question->subquestions[$key]['format'] = $questiontextformat;
374 $question->subquestions[$key]['files'] = array();
375 $question->subanswers[$key] =
376 trim($this->escapedchar_post(substr($answer, $marker + 2)));
84769fd8 377 } // end foreach answer
aeb15530 378
84769fd8 379 return $question;
380 break;
aeb15530 381
84769fd8 382 case TRUEFALSE:
383 $answer = $answertext;
384 $comment = $this->commentparser($answer); // commentparser also removes comment from $answer
09db6da2 385 $feedback = $this->split_truefalse_comment($comment);
84769fd8 386
387 if ($answer == "T" OR $answer == "TRUE") {
388 $question->answer = 1;
13bb604e
TH
389 $question->feedbacktrue['text'] = $feedback['right'];
390 $question->feedbackfalse['text'] = $feedback['wrong'];
84769fd8 391 } else {
392 $question->answer = 0;
13bb604e
TH
393 $question->feedbackfalse['text'] = $feedback['right'];
394 $question->feedbacktrue['text'] = $feedback['wrong'];
84769fd8 395 }
13bb604e
TH
396 $question->feedbackfalse['format'] = $questiontextformat;
397 $question->feedbacktrue['format'] = $questiontextformat;
398 $question->feedbackfalse['files'] = array();
399 $question->feedbacktrue['files'] = array();
84769fd8 400
0d4c63d7 401 $question->penalty = 1;
7939a4a0 402 $question->correctanswer = $question->answer;
403
84769fd8 404 return $question;
405 break;
aeb15530 406
84769fd8 407 case SHORTANSWER:
408 // SHORTANSWER Question
409 $answers = explode("=", $answertext);
410 if (isset($answers[0])) {
411 $answers[0] = trim($answers[0]);
412 }
413 if (empty($answers[0])) {
414 array_shift($answers);
415 }
aeb15530 416
13bb604e 417 if (!$this->check_answer_count(1,$answers,$text)) {
2befe778 418 return false;
419 break;
84769fd8 420 }
421
422 foreach ($answers as $key => $answer) {
423 $answer = trim($answer);
424
425 // Answer Weight
6dbcacee 426 if (preg_match($gift_answerweight_regex, $answer)) { // check for properly formatted answer weight
84769fd8 427 $answer_weight = $this->answerweightparser($answer);
428 } else { //default, i.e., full-credit anwer
429 $answer_weight = 1;
430 }
294ce987 431 $question->answer[$key] = $this->escapedchar_post($answer);
13bb604e
TH
432 $question->fraction[$key] = $answer_weight;
433 $question->feedback[$key]['text'] = $this->commentparser($answer); //commentparser also removes comment from $answer
434 $question->feedback[$key]['format'] = $questiontextformat;
435 $question->feedback[$key]['files'] = array();
84769fd8 436 } // end foreach
437
438 //$question->usecase = 0; // Ignore case
439 //$question->defaultgrade = 1;
440 //$question->image = ""; // No images with this format
441 return $question;
442 break;
443
444 case NUMERICAL:
445 // Note similarities to ShortAnswer
446 $answertext = substr($answertext, 1); // remove leading "#"
447
1fe641f7 448 // If there is feedback for a wrong answer, store it for now.
449 if (($pos = strpos($answertext, '~')) !== false) {
450 $wrongfeedback = substr($answertext, $pos);
451 $answertext = substr($answertext, 0, $pos);
452 } else {
453 $wrongfeedback = '';
454 }
455
84769fd8 456 $answers = explode("=", $answertext);
457 if (isset($answers[0])) {
458 $answers[0] = trim($answers[0]);
459 }
460 if (empty($answers[0])) {
461 array_shift($answers);
462 }
aeb15530 463
84769fd8 464 if (count($answers) == 0) {
465 // invalid question
532344b2 466 $giftnonumericalanswers = get_string('giftnonumericalanswers','quiz');
13bb604e 467 $this->error($giftnonumericalanswers, $text);
84769fd8 468 return false;
469 break;
470 }
471
472 foreach ($answers as $key => $answer) {
473 $answer = trim($answer);
474
475 // Answer weight
6dbcacee 476 if (preg_match($gift_answerweight_regex, $answer)) { // check for properly formatted answer weight
84769fd8 477 $answer_weight = $this->answerweightparser($answer);
478 } else { //default, i.e., full-credit anwer
479 $answer_weight = 1;
480 }
481 $question->fraction[$key] = $answer_weight;
13bb604e
TH
482 $question->feedback[$key]['text'] = $this->commentparser($answer); //commentparser also removes comment from $answer
483 $question->feedback[$key]['format'] = $questiontextformat;
484 $question->feedback[$key]['files'] = array();
84769fd8 485
486 //Calculate Answer and Min/Max values
487 if (strpos($answer,"..") > 0) { // optional [min]..[max] format
488 $marker = strpos($answer,"..");
489 $max = trim(substr($answer, $marker+2));
490 $min = trim(substr($answer, 0, $marker));
491 $ans = ($max + $min)/2;
492 $tol = $max - $ans;
13bb604e
TH
493 } else if (strpos($answer, ':') > 0) { // standard [answer]:[errormargin] format
494 $marker = strpos($answer, ':');
84769fd8 495 $tol = trim(substr($answer, $marker+1));
496 $ans = trim(substr($answer, 0, $marker));
497 } else { // only one valid answer (zero errormargin)
498 $tol = 0;
499 $ans = trim($answer);
500 }
aeb15530 501
09db6da2 502 if (!(is_numeric($ans) || $ans = '*') || !is_numeric($tol)) {
13bb604e
TH
503 $errornotnumbers = get_string('errornotnumbers');
504 $this->error($errornotnumbers, $text);
84769fd8 505 return false;
506 break;
507 }
aeb15530 508
84769fd8 509 // store results
510 $question->answer[$key] = $ans;
511 $question->tolerance[$key] = $tol;
512 } // end foreach
513
1fe641f7 514 if ($wrongfeedback) {
515 $key += 1;
516 $question->fraction[$key] = 0;
13bb604e
TH
517 $question->feedback[$key]['text'] = $this->commentparser($wrongfeedback);
518 $question->feedback[$key]['format'] = $questiontextformat;
519 $question->feedback[$key]['files'] = array();
520 $question->answer[$key] = '*';
1fe641f7 521 $question->tolerance[$key] = '';
522 }
523
84769fd8 524 return $question;
525 break;
526
527 default:
13bb604e 528 $this->error(get_string('giftnovalidquestion', 'quiz'), $text);
84769fd8 529 return false;
aeb15530
PS
530 break;
531
13bb604e 532 }
84769fd8 533
84769fd8 534 }
535
13bb604e
TH
536 function repchar($text, $notused = 0) {
537 // Escapes 'reserved' characters # = ~ {) :
538 // Removes new lines
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
TH
546 /**
547 * @param integer $format one of the FORMAT_ constants.
548 * @return string the corresponding name.
549 */
550 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';
09db6da2 559 } else {
13bb604e 560 return 'moodle';
84769fd8 561 }
13bb604e 562 }
36e2232e 563
13bb604e
TH
564 /**
565 * @param integer $format one of the FORMAT_ constants.
566 * @return string the corresponding name.
567 */
568 function format_name_to_const($format) {
569 if ($format == 'moodle') {
570 return FORMAT_MOODLE;
571 } else if ($format == 'html') {
572 return FORMAT_PLAIN;
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
586 public function write_questiontext($text, $format) {
587 $output = '';
588 if ($format != FORMAT_MOODLE) {
589 $output .= '[' . $this->format_const_to_name($format) . ']';
84769fd8 590 }
13bb604e
TH
591 $output .= $this->repchar($text, $format);
592 return $output;
593 }
594
595 function writequestion($question) {
596 global $QTYPES, $OUTPUT;
597
598 // Start with a comment
599 $expout = "// question: $question->id name: $question->name \n";
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';
625 $right_feedback = $trueanswer->feedback;
626 $wrong_feedback = $falseanswer->feedback;
627 } else {
628 $answertext = 'FALSE';
629 $right_feedback = $falseanswer->feedback;
630 $wrong_feedback = $trueanswer->feedback;
631 }
632
633 $wrong_feedback = $this->repchar($wrong_feedback);
634 $right_feedback = $this->repchar($right_feedback);
635
636 $expout .= $this->write_name($question->name);
637 $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
638 $expout .= '{' . $this->repchar($answertext);
639 if ($wrong_feedback) {
640 $expout .= '#' . $wrong_feedback;
641 } else if ($right_feedback) {
642 $expout .= '#';
84769fd8 643 }
13bb604e
TH
644 if ($right_feedback) {
645 $expout .= '#' . $right_feedback;
84769fd8 646 }
13bb604e
TH
647 $expout .= "}\n";
648 break;
649
650 case MULTICHOICE:
651 $expout .= $this->write_name($question->name);
652 $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
653 $expout .= "{\n";
654 foreach($question->options->answers as $answer) {
655 if ($answer->fraction == 1) {
656 $answertext = '=';
657 } else if ($answer->fraction == 0) {
658 $answertext = '~';
659 } else {
660 $weight = $answer->fraction * 100;
661 $answertext = '~%' . $weight . '%';
662 }
663 $expout .= "\t" . $answertext . $this->repchar($answer->answer);
664 if ($answer->feedback != '') {
665 $expout .= '#' . $this->repchar($answer->feedback);
666 }
667 $expout .= "\n";
84769fd8 668 }
13bb604e
TH
669 $expout .= "}\n";
670 break;
671
672 case SHORTANSWER:
673 $expout .= $this->write_name($question->name);
674 $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
675 $expout .= "{\n";
676 foreach($question->options->answers as $answer) {
677 $weight = 100 * $answer->fraction;
678 $expout .= "\t=%" . $weight . '%' . $this->repchar($answer->answer) .
679 '#' . $this->repchar($answer->feedback) . "\n";
84769fd8 680 }
13bb604e
TH
681 $expout .= "}\n";
682 break;
683
684 case NUMERICAL:
685 $expout .= $this->write_name($question->name);
686 $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
687 $expout .= "{#\n";
688 foreach ($question->options->answers as $answer) {
689 if ($answer->answer != '' && $answer->answer != '*') {
690 $weight = 100 * $answer->fraction;
691 $expout .= "\t=%" . $weight . '%' . $answer->answer . ':' .
692 (float)$answer->tolerance . '#' . $this->repchar($answer->feedback) . "\n";
693 } else {
694 $expout .= "\t~#" . $this->repchar($answer->feedback) . "\n";
13e88db4 695 }
13bb604e
TH
696 }
697 $expout .= "}\n";
698 break;
699
700 case MATCH:
701 $expout .= $this->write_name($question->name);
702 $expout .= $this->write_questiontext($question->questiontext, $question->questiontextformat);
703 $expout .= "{\n";
704 foreach($question->options->subquestions as $subquestion) {
705 $expout .= "\t=" . $this->repchar($subquestion->questiontext) .
706 ' -> ' . $this->repchar($subquestion->answertext) . "\n";
707 }
708 $expout .= "}\n";
709 break;
710
711 default:
712 // Check for plugins
713 if ($out = $this->try_exporting_using_qtypes($question->qtype, $question)) {
714 $expout .= $out;
1fe641f7 715 } else {
13bb604e
TH
716 $expout .= "Question type $question->qtype is not supported\n";
717 echo $OUTPUT->notification(get_string('nohandler', 'qformat_gift',
718 $QTYPES[$question->qtype]->local_name()));
1fe641f7 719 }
720 }
13bb604e
TH
721
722 // Add empty line to delimit questions
723 $expout .= "\n";
724 return $expout;
84769fd8 725 }
84769fd8 726}
aeb15530 727