2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Code for exporting questions as Moodle XML.
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->libdir . '/xmlize.php');
30 if (!class_exists('qformat_default')) {
31 // This is ugly, but this class is also (ab)used by mod/lesson, which defines
32 // a different base class in mod/lesson/format.php. Thefore, we can only
33 // include the proper base class conditionally like this. (We have to include
34 // the base class like this, otherwise it breaks third-party question types.)
35 // This may be reviewd, and a better fix found one day.
36 require_once($CFG->dirroot . '/question/format.php');
41 * Importer for Moodle XML question format.
43 * See http://docs.moodle.org/en/Moodle_XML_format for a description of the format.
45 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
46 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
48 class qformat_xml extends qformat_default {
50 public function provide_import() {
54 public function provide_export() {
58 public function mime_type() {
59 return 'application/xml';
62 // IMPORT FUNCTIONS START HERE
65 * Translate human readable format name
66 * into internal Moodle code number
67 * @param string name format name from xml file
68 * @return int Moodle format code
70 public function trans_format($name) {
73 if ($name == 'moodle_auto_format') {
75 } else if ($name == 'html') {
77 } else if ($name == 'plain_text') {
79 } else if ($name == 'wiki_like') {
81 } else if ($name == 'markdown') {
82 return FORMAT_MARKDOWN;
84 debugging("Unrecognised text format '{$name}' in the import file. Assuming 'html'.");
90 * Translate human readable single answer option
91 * to internal code number
92 * @param string name true/false
93 * @return int internal code number
95 public function trans_single($name) {
97 if ($name == "false" || !$name) {
105 * process text string from xml file
106 * @param array $text bit of xml tree after ['text']
107 * @return string processed text.
109 public function import_text($text) {
110 // quick sanity check
114 $data = $text[0]['#'];
119 * return the value of a node, given a path to the node
120 * if it doesn't exist return the default value
121 * @param array xml data to read
122 * @param array path path to node expressed as array
123 * @param mixed default
124 * @param bool istext process as text
125 * @param string error if set value must exist, return false and issue message if not
126 * @return mixed value
128 public function getpath($xml, $path, $default, $istext=false, $error='') {
129 foreach ($path as $index) {
130 if (!isset($xml[$index])) {
131 if (!empty($error)) {
132 $this->error($error);
143 if (!is_string($xml)) {
144 $this->error(get_string('invalidxml', 'qformat_xml'));
152 public function import_text_with_files($data, $path, $defaultvalue = '', $defaultformat = 'html') {
154 $field['text'] = $this->getpath($data,
155 array_merge($path, array('#', 'text', 0, '#')), $defaultvalue, true);
156 $field['format'] = $this->trans_format($this->getpath($data,
157 array_merge($path, array('@', 'format')), $defaultformat));
158 $itemid = $this->import_files_as_draft($this->getpath($data,
159 array_merge($path, array('#', 'file')), array(), false));
160 if (!empty($itemid)) {
161 $field['itemid'] = $itemid;
166 public function import_files_as_draft($xml) {
171 $fs = get_file_storage();
172 $itemid = file_get_unused_draft_itemid();
173 $filenames = array();
174 foreach ($xml as $file) {
175 $filename = $file['@']['name'];
176 if (in_array($filename, $filenames)) {
177 debugging('Duplicate file in XML: ' . $filename, DEBUG_DEVELOPER);
181 'contextid' => context_user::instance($USER->id)->id,
182 'component' => 'user',
183 'filearea' => 'draft',
186 'filename' => $filename,
188 $fs->create_file_from_string($filerecord, base64_decode($file['#']));
189 $filenames[] = $filename;
195 * import parts of question common to all types
196 * @param $question array question question array from xml tree
197 * @return object question object
199 public function import_headers($question) {
202 // get some error strings
203 $error_noname = get_string('xmlimportnoname', 'qformat_xml');
204 $error_noquestion = get_string('xmlimportnoquestion', 'qformat_xml');
206 // this routine initialises the question object
207 $qo = $this->defaultquestion();
210 $qo->name = $this->clean_question_name($this->getpath($question,
211 array('#', 'name', 0, '#', 'text', 0, '#'), '', true,
212 get_string('xmlimportnoname', 'qformat_xml')));
213 $questiontext = $this->import_text_with_files($question,
214 array('#', 'questiontext', 0));
215 $qo->questiontext = $questiontext['text'];
216 $qo->questiontextformat = $questiontext['format'];
217 if (!empty($questiontext['itemid'])) {
218 $qo->questiontextitemid = $questiontext['itemid'];
220 // Backwards compatibility, deal with the old image tag.
221 $filedata = $this->getpath($question, array('#', 'image_base64', '0', '#'), null, false);
222 $filename = $this->getpath($question, array('#', 'image', '0', '#'), null, false);
223 if ($filedata && $filename) {
224 $fs = get_file_storage();
225 if (empty($qo->questiontextitemid)) {
226 $qo->questiontextitemid = file_get_unused_draft_itemid();
228 $filename = clean_param(str_replace('/', '_', $filename), PARAM_FILE);
230 'contextid' => context_user::instance($USER->id)->id,
231 'component' => 'user',
232 'filearea' => 'draft',
233 'itemid' => $qo->questiontextitemid,
235 'filename' => $filename,
237 $fs->create_file_from_string($filerecord, base64_decode($filedata));
238 $qo->questiontext .= ' <img src="@@PLUGINFILE@@/' . $filename . '" />';
241 // restore files in generalfeedback
242 $generalfeedback = $this->import_text_with_files($question,
243 array('#', 'generalfeedback', 0), $qo->generalfeedback, $this->get_format($qo->questiontextformat));
244 $qo->generalfeedback = $generalfeedback['text'];
245 $qo->generalfeedbackformat = $generalfeedback['format'];
246 if (!empty($generalfeedback['itemid'])) {
247 $qo->generalfeedbackitemid = $generalfeedback['itemid'];
250 $qo->defaultmark = $this->getpath($question,
251 array('#', 'defaultgrade', 0, '#'), $qo->defaultmark);
252 $qo->penalty = $this->getpath($question,
253 array('#', 'penalty', 0, '#'), $qo->penalty);
255 // Fix problematic rounding from old files:
256 if (abs($qo->penalty - 0.3333333) < 0.005) {
257 $qo->penalty = 0.3333333;
260 // Read the question tags.
261 if (!empty($CFG->usetags) && array_key_exists('tags', $question['#'])
262 && !empty($question['#']['tags'][0]['#']['tag'])) {
263 require_once($CFG->dirroot.'/tag/lib.php');
265 foreach ($question['#']['tags'][0]['#']['tag'] as $tagdata) {
266 $qo->tags[] = $this->getpath($tagdata, array('#', 'text', 0, '#'), '', true);
274 * Import the common parts of a single answer
275 * @param array answer xml tree for single answer
276 * @param bool $withanswerfiles if true, the answers are HTML (or $defaultformat)
277 * and so may contain files, otherwise the answers are plain text.
278 * @param array Default text format for the feedback, and the answers if $withanswerfiles
280 * @return object answer object
282 public function import_answer($answer, $withanswerfiles = false, $defaultformat = 'html') {
283 $ans = new stdClass();
285 if ($withanswerfiles) {
286 $ans->answer = $this->import_text_with_files($answer, array(), '', $defaultformat);
288 $ans->answer = array();
289 $ans->answer['text'] = $this->getpath($answer, array('#', 'text', 0, '#'), '', true);
290 $ans->answer['format'] = FORMAT_PLAIN;
293 $ans->feedback = $this->import_text_with_files($answer, array('#', 'feedback', 0), '', $defaultformat);
295 $ans->fraction = $this->getpath($answer, array('@', 'fraction'), 0) / 100;
301 * Import the common overall feedback fields.
302 * @param object $question the part of the XML relating to this question.
303 * @param object $qo the question data to add the fields to.
304 * @param bool $withshownumpartscorrect include the shownumcorrect field.
306 public function import_combined_feedback($qo, $questionxml, $withshownumpartscorrect = false) {
307 $fields = array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback');
308 foreach ($fields as $field) {
309 $qo->$field = $this->import_text_with_files($questionxml,
310 array('#', $field, 0), '', $this->get_format($qo->questiontextformat));
313 if ($withshownumpartscorrect) {
314 $qo->shownumcorrect = array_key_exists('shownumcorrect', $questionxml['#']);
316 // Backwards compatibility:
317 if (array_key_exists('correctresponsesfeedback', $questionxml['#'])) {
318 $qo->shownumcorrect = $this->trans_single($this->getpath($questionxml,
319 array('#', 'correctresponsesfeedback', 0, '#'), 1));
325 * Import a question hint
326 * @param array $hintxml hint xml fragment.
327 * @param string $defaultformat the text format to assume for hints that do not specify.
328 * @return object hint for storing in the database.
330 public function import_hint($hintxml, $defaultformat) {
331 $hint = new stdClass();
332 if (array_key_exists('hintcontent', $hintxml['#'])) {
333 // Backwards compatibility:
335 $hint->hint = $this->import_text_with_files($hintxml,
336 array('#', 'hintcontent', 0), '', $defaultformat);
338 $hint->shownumcorrect = $this->getpath($hintxml,
339 array('#', 'statenumberofcorrectresponses', 0, '#'), 0);
340 $hint->clearwrong = $this->getpath($hintxml,
341 array('#', 'clearincorrectresponses', 0, '#'), 0);
342 $hint->options = $this->getpath($hintxml,
343 array('#', 'showfeedbacktoresponses', 0, '#'), 0);
347 $hint->hint = $this->import_text_with_files($hintxml, array(), '', $defaultformat);
348 $hint->shownumcorrect = array_key_exists('shownumcorrect', $hintxml['#']);
349 $hint->clearwrong = array_key_exists('clearwrong', $hintxml['#']);
350 $hint->options = $this->getpath($hintxml, array('#', 'options', 0, '#'), '', true);
356 * Import all the question hints
358 * @param object $qo the question data that is being constructed.
359 * @param array $questionxml The xml representing the question.
360 * @param bool $withparts whether the extra fields relating to parts should be imported.
361 * @param bool $withoptions whether the extra options field should be imported.
362 * @param string $defaultformat the text format to assume for hints that do not specify.
363 * @return array of objects representing the hints in the file.
365 public function import_hints($qo, $questionxml, $withparts = false,
366 $withoptions = false, $defaultformat = 'html') {
367 if (!isset($questionxml['#']['hint'])) {
371 foreach ($questionxml['#']['hint'] as $hintxml) {
372 $hint = $this->import_hint($hintxml, $defaultformat);
373 $qo->hint[] = $hint->hint;
376 $qo->hintshownumcorrect[] = $hint->shownumcorrect;
377 $qo->hintclearwrong[] = $hint->clearwrong;
381 $qo->hintoptions[] = $hint->options;
387 * Import files from a node in the XML.
388 * @param array $xml an array of <file> nodes from the the parsed XML.
389 * @return array of things representing files - in the form that save_question expects.
391 public function import_files($xml) {
393 foreach ($xml as $file) {
394 $data = new stdClass();
395 $data->content = $file['#'];
396 $data->encoding = $file['@']['encoding'];
397 $data->name = $file['@']['name'];
404 * import multiple choice question
405 * @param array question question array from xml tree
406 * @return object question object
408 public function import_multichoice($question) {
410 $qo = $this->import_headers($question);
412 // 'header' parts particular to multichoice
413 $qo->qtype = MULTICHOICE;
414 $single = $this->getpath($question, array('#', 'single', 0, '#'), 'true');
415 $qo->single = $this->trans_single($single);
416 $shuffleanswers = $this->getpath($question,
417 array('#', 'shuffleanswers', 0, '#'), 'false');
418 $qo->answernumbering = $this->getpath($question,
419 array('#', 'answernumbering', 0, '#'), 'abc');
420 $qo->shuffleanswers = $this->trans_single($shuffleanswers);
422 // There was a time on the 1.8 branch when it could output an empty
423 // answernumbering tag, so fix up any found.
424 if (empty($qo->answernumbering)) {
425 $qo->answernumbering = 'abc';
428 // Run through the answers
429 $answers = $question['#']['answer'];
431 foreach ($answers as $answer) {
432 $ans = $this->import_answer($answer, true, $this->get_format($qo->questiontextformat));
433 $qo->answer[$acount] = $ans->answer;
434 $qo->fraction[$acount] = $ans->fraction;
435 $qo->feedback[$acount] = $ans->feedback;
439 $this->import_combined_feedback($qo, $question, true);
440 $this->import_hints($qo, $question, true, false, $this->get_format($qo->questiontextformat));
446 * Import cloze type question
447 * @param array question question array from xml tree
448 * @return object question object
450 public function import_multianswer($question) {
452 question_bank::get_qtype('multianswer');
454 $questiontext = $this->import_text_with_files($question,
455 array('#', 'questiontext', 0));
456 $qo = qtype_multianswer_extract_question($questiontext);
458 // 'header' parts particular to multianswer
459 $qo->qtype = 'multianswer';
460 $qo->course = $this->course;
462 $qo->name = $this->clean_question_name($this->import_text($question['#']['name'][0]['#']['text']));
463 $qo->questiontextformat = $questiontext['format'];
464 $qo->questiontext = $qo->questiontext['text'];
465 if (!empty($questiontext['itemid'])) {
466 $qo->questiontextitemid = $questiontext['itemid'];
469 // Backwards compatibility, deal with the old image tag.
470 $filedata = $this->getpath($question, array('#', 'image_base64', '0', '#'), null, false);
471 $filename = $this->getpath($question, array('#', 'image', '0', '#'), null, false);
472 if ($filedata && $filename) {
473 $fs = get_file_storage();
474 if (empty($qo->questiontextitemid)) {
475 $qo->questiontextitemid = file_get_unused_draft_itemid();
477 $filename = clean_param(str_replace('/', '_', $filename), PARAM_FILE);
479 'contextid' => context_user::instance($USER->id)->id,
480 'component' => 'user',
481 'filearea' => 'draft',
482 'itemid' => $qo->questiontextitemid,
484 'filename' => $filename,
486 $fs->create_file_from_string($filerecord, base64_decode($filedata));
487 $qo->questiontext .= ' <img src="@@PLUGINFILE@@/' . $filename . '" />';
490 // restore files in generalfeedback
491 $generalfeedback = $this->import_text_with_files($question,
492 array('#', 'generalfeedback', 0), $qo->generalfeedback, $this->get_format($qo->questiontextformat));
493 $qo->generalfeedback = $generalfeedback['text'];
494 $qo->generalfeedbackformat = $generalfeedback['format'];
495 if (!empty($generalfeedback['itemid'])) {
496 $qo->generalfeedbackitemid = $generalfeedback['itemid'];
499 $qo->penalty = $this->getpath($question,
500 array('#', 'penalty', 0, '#'), $this->defaultquestion()->penalty);
501 // Fix problematic rounding from old files:
502 if (abs($qo->penalty - 0.3333333) < 0.005) {
503 $qo->penalty = 0.3333333;
506 $this->import_hints($qo, $question, false, false, $this->get_format($qo->questiontextformat));
512 * Import true/false type question
513 * @param array question question array from xml tree
514 * @return object question object
516 public function import_truefalse($question) {
519 $qo = $this->import_headers($question);
521 // 'header' parts particular to true/false
522 $qo->qtype = TRUEFALSE;
524 // In the past, it used to be assumed that the two answers were in the file
525 // true first, then false. Howevever that was not always true. Now, we
526 // try to match on the answer text, but in old exports, this will be a localised
527 // string, so if we don't find true or false, we fall back to the old system.
530 foreach ($question['#']['answer'] as $answer) {
531 $answertext = $this->getpath($answer,
532 array('#', 'text', 0, '#'), '', true);
533 $feedback = $this->import_text_with_files($answer,
534 array('#', 'feedback', 0), '', $this->get_format($qo->questiontextformat));
536 if ($answertext != 'true' && $answertext != 'false') {
537 // Old style file, assume order is true/false.
540 $answertext = 'true';
542 $answertext = 'false';
546 if ($answertext == 'true') {
547 $qo->answer = ($answer['@']['fraction'] == 100);
548 $qo->correctanswer = $qo->answer;
549 $qo->feedbacktrue = $feedback;
551 $qo->answer = ($answer['@']['fraction'] != 100);
552 $qo->correctanswer = $qo->answer;
553 $qo->feedbackfalse = $feedback;
560 $a->questiontext = $qo->questiontext;
561 $a->answer = get_string($qo->correctanswer ? 'true' : 'false', 'qtype_truefalse');
562 echo $OUTPUT->notification(get_string('truefalseimporterror', 'qformat_xml', $a));
565 $this->import_hints($qo, $question, false, false, $this->get_format($qo->questiontextformat));
571 * Import short answer type question
572 * @param array question question array from xml tree
573 * @return object question object
575 public function import_shortanswer($question) {
577 $qo = $this->import_headers($question);
579 // header parts particular to shortanswer
580 $qo->qtype = SHORTANSWER;
583 $qo->usecase = $this->getpath($question, array('#', 'usecase', 0, '#'), $qo->usecase);
585 // Run through the answers
586 $answers = $question['#']['answer'];
588 foreach ($answers as $answer) {
589 $ans = $this->import_answer($answer, false, $this->get_format($qo->questiontextformat));
590 $qo->answer[$acount] = $ans->answer['text'];
591 $qo->fraction[$acount] = $ans->fraction;
592 $qo->feedback[$acount] = $ans->feedback;
596 $this->import_hints($qo, $question, false, false, $this->get_format($qo->questiontextformat));
602 * Import description type question
603 * @param array question question array from xml tree
604 * @return object question object
606 public function import_description($question) {
608 $qo = $this->import_headers($question);
609 // header parts particular to shortanswer
610 $qo->qtype = DESCRIPTION;
611 $qo->defaultmark = 0;
617 * Import numerical type question
618 * @param array question question array from xml tree
619 * @return object question object
621 public function import_numerical($question) {
623 $qo = $this->import_headers($question);
625 // header parts particular to numerical
626 $qo->qtype = NUMERICAL;
629 $answers = $question['#']['answer'];
630 $qo->answer = array();
631 $qo->feedback = array();
632 $qo->fraction = array();
633 $qo->tolerance = array();
634 foreach ($answers as $answer) {
635 // answer outside of <text> is deprecated
636 $obj = $this->import_answer($answer, false, $this->get_format($qo->questiontextformat));
637 $qo->answer[] = $obj->answer['text'];
638 if (empty($qo->answer)) {
641 $qo->feedback[] = $obj->feedback;
642 $qo->tolerance[] = $this->getpath($answer, array('#', 'tolerance', 0, '#'), 0);
644 // fraction as a tag is deprecated
645 $fraction = $this->getpath($answer, array('@', 'fraction'), 0) / 100;
646 $qo->fraction[] = $this->getpath($answer,
647 array('#', 'fraction', 0, '#'), $fraction); // deprecated
650 // Get the units array
652 $units = $this->getpath($question, array('#', 'units', 0, '#', 'unit'), array());
653 if (!empty($units)) {
654 $qo->multiplier = array();
655 foreach ($units as $unit) {
656 $qo->multiplier[] = $this->getpath($unit, array('#', 'multiplier', 0, '#'), 1);
657 $qo->unit[] = $this->getpath($unit, array('#', 'unit_name', 0, '#'), '', true);
660 $qo->unitgradingtype = $this->getpath($question, array('#', 'unitgradingtype', 0, '#'), 0);
661 $qo->unitpenalty = $this->getpath($question, array('#', 'unitpenalty', 0, '#'), 0.1);
662 $qo->showunits = $this->getpath($question, array('#', 'showunits', 0, '#'), null);
663 $qo->unitsleft = $this->getpath($question, array('#', 'unitsleft', 0, '#'), 0);
664 $qo->instructions['text'] = '';
665 $qo->instructions['format'] = FORMAT_HTML;
666 $instructions = $this->getpath($question, array('#', 'instructions'), array());
667 if (!empty($instructions)) {
668 $qo->instructions = $this->import_text_with_files($instructions,
669 array('0'), '', $this->get_format($qo->questiontextformat));
672 if (is_null($qo->showunits)) {
673 // Set a good default, depending on whether there are any units defined.
674 if (empty($qo->unit)) {
675 $qo->showunits = 3; // qtype_numerical::UNITNONE;
677 $qo->showunits = 0; // qtype_numerical::UNITOPTIONAL;
681 $this->import_hints($qo, $question, false, false, $this->get_format($qo->questiontextformat));
687 * Import matching type question
688 * @param array question question array from xml tree
689 * @return object question object
691 public function import_match($question) {
693 $qo = $this->import_headers($question);
695 // header parts particular to matching
696 $qo->qtype = 'match';
697 $qo->shuffleanswers = $this->trans_single($this->getpath($question,
698 array('#', 'shuffleanswers', 0, '#'), 1));
700 // run through subquestions
701 $qo->subquestions = array();
702 $qo->subanswers = array();
703 foreach ($question['#']['subquestion'] as $subqxml) {
704 $qo->subquestions[] = $this->import_text_with_files($subqxml,
705 array(), '', $this->get_format($qo->questiontextformat));
707 $answers = $this->getpath($subqxml, array('#', 'answer'), array());
708 $qo->subanswers[] = $this->getpath($subqxml,
709 array('#', 'answer', 0, '#', 'text', 0, '#'), '', true);
712 $this->import_combined_feedback($qo, $question, true);
713 $this->import_hints($qo, $question, true, false, $this->get_format($qo->questiontextformat));
719 * Import essay type question
720 * @param array question question array from xml tree
721 * @return object question object
723 public function import_essay($question) {
725 $qo = $this->import_headers($question);
727 // header parts particular to essay
730 $qo->responseformat = $this->getpath($question,
731 array('#', 'responseformat', 0, '#'), 'editor');
732 $qo->responsefieldlines = $this->getpath($question,
733 array('#', 'responsefieldlines', 0, '#'), 15);
734 $qo->attachments = $this->getpath($question,
735 array('#', 'attachments', 0, '#'), 0);
736 $qo->graderinfo = $this->import_text_with_files($question,
737 array('#', 'graderinfo', 0), '', $this->get_format($qo->questiontextformat));
743 * Import a calculated question
744 * @param object $question the imported XML data.
746 public function import_calculated($question) {
749 $qo = $this->import_headers($question);
751 // header parts particular to calculated
752 $qo->qtype = CALCULATED;
753 $qo->synchronize = $this->getpath($question, array('#', 'synchronize', 0, '#'), 0);
754 $single = $this->getpath($question, array('#', 'single', 0, '#'), 'true');
755 $qo->single = $this->trans_single($single);
756 $shuffleanswers = $this->getpath($question, array('#', 'shuffleanswers', 0, '#'), 'false');
757 $qo->answernumbering = $this->getpath($question,
758 array('#', 'answernumbering', 0, '#'), 'abc');
759 $qo->shuffleanswers = $this->trans_single($shuffleanswers);
761 $this->import_combined_feedback($qo, $question);
763 $qo->unitgradingtype = $this->getpath($question,
764 array('#', 'unitgradingtype', 0, '#'), 0);
765 $qo->unitpenalty = $this->getpath($question, array('#', 'unitpenalty', 0, '#'), null);
766 $qo->showunits = $this->getpath($question, array('#', 'showunits', 0, '#'), 0);
767 $qo->unitsleft = $this->getpath($question, array('#', 'unitsleft', 0, '#'), 0);
768 $qo->instructions = $this->getpath($question,
769 array('#', 'instructions', 0, '#', 'text', 0, '#'), '', true);
770 if (!empty($instructions)) {
771 $qo->instructions = $this->import_text_with_files($instructions,
772 array('0'), '', $this->get_format($qo->questiontextformat));
776 $answers = $question['#']['answer'];
777 $qo->answers = array();
778 $qo->feedback = array();
779 $qo->fraction = array();
780 $qo->tolerance = array();
781 $qo->tolerancetype = array();
782 $qo->correctanswerformat = array();
783 $qo->correctanswerlength = array();
784 $qo->feedback = array();
785 foreach ($answers as $answer) {
786 $ans = $this->import_answer($answer, true, $this->get_format($qo->questiontextformat));
787 // answer outside of <text> is deprecated
788 if (empty($ans->answer['text'])) {
789 $ans->answer['text'] = '*';
791 $qo->answers[] = $ans->answer;
792 $qo->feedback[] = $ans->feedback;
793 $qo->tolerance[] = $answer['#']['tolerance'][0]['#'];
794 // fraction as a tag is deprecated
795 if (!empty($answer['#']['fraction'][0]['#'])) {
796 $qo->fraction[] = $answer['#']['fraction'][0]['#'];
798 $qo->fraction[] = $answer['@']['fraction'] / 100;
800 $qo->tolerancetype[] = $answer['#']['tolerancetype'][0]['#'];
801 $qo->correctanswerformat[] = $answer['#']['correctanswerformat'][0]['#'];
802 $qo->correctanswerlength[] = $answer['#']['correctanswerlength'][0]['#'];
806 if (isset($question['#']['units'][0]['#']['unit'])) {
807 $units = $question['#']['units'][0]['#']['unit'];
808 $qo->multiplier = array();
809 foreach ($units as $unit) {
810 $qo->multiplier[] = $unit['#']['multiplier'][0]['#'];
811 $qo->unit[] = $unit['#']['unit_name'][0]['#'];
814 $instructions = $this->getpath($question, array('#', 'instructions'), array());
815 if (!empty($instructions)) {
816 $qo->instructions = $this->import_text_with_files($instructions,
817 array('0'), '', $this->get_format($qo->questiontextformat));
820 if (is_null($qo->unitpenalty)) {
821 // Set a good default, depending on whether there are any units defined.
822 if (empty($qo->unit)) {
823 $qo->showunits = 3; // qtype_numerical::UNITNONE;
825 $qo->showunits = 0; // qtype_numerical::UNITOPTIONAL;
829 $datasets = $question['#']['dataset_definitions'][0]['#']['dataset_definition'];
830 $qo->dataset = array();
831 $qo->datasetindex= 0;
832 foreach ($datasets as $dataset) {
834 $qo->dataset[$qo->datasetindex] = new stdClass();
835 $qo->dataset[$qo->datasetindex]->status =
836 $this->import_text($dataset['#']['status'][0]['#']['text']);
837 $qo->dataset[$qo->datasetindex]->name =
838 $this->import_text($dataset['#']['name'][0]['#']['text']);
839 $qo->dataset[$qo->datasetindex]->type =
840 $dataset['#']['type'][0]['#'];
841 $qo->dataset[$qo->datasetindex]->distribution =
842 $this->import_text($dataset['#']['distribution'][0]['#']['text']);
843 $qo->dataset[$qo->datasetindex]->max =
844 $this->import_text($dataset['#']['maximum'][0]['#']['text']);
845 $qo->dataset[$qo->datasetindex]->min =
846 $this->import_text($dataset['#']['minimum'][0]['#']['text']);
847 $qo->dataset[$qo->datasetindex]->length =
848 $this->import_text($dataset['#']['decimals'][0]['#']['text']);
849 $qo->dataset[$qo->datasetindex]->distribution =
850 $this->import_text($dataset['#']['distribution'][0]['#']['text']);
851 $qo->dataset[$qo->datasetindex]->itemcount = $dataset['#']['itemcount'][0]['#'];
852 $qo->dataset[$qo->datasetindex]->datasetitem = array();
853 $qo->dataset[$qo->datasetindex]->itemindex = 0;
854 $qo->dataset[$qo->datasetindex]->number_of_items =
855 $dataset['#']['number_of_items'][0]['#'];
856 $datasetitems = $dataset['#']['dataset_items'][0]['#']['dataset_item'];
857 foreach ($datasetitems as $datasetitem) {
858 $qo->dataset[$qo->datasetindex]->itemindex++;
859 $qo->dataset[$qo->datasetindex]->datasetitem[
860 $qo->dataset[$qo->datasetindex]->itemindex] = new stdClass();
861 $qo->dataset[$qo->datasetindex]->datasetitem[
862 $qo->dataset[$qo->datasetindex]->itemindex]->itemnumber =
863 $datasetitem['#']['number'][0]['#'];
864 $qo->dataset[$qo->datasetindex]->datasetitem[
865 $qo->dataset[$qo->datasetindex]->itemindex]->value =
866 $datasetitem['#']['value'][0]['#'];
870 $this->import_hints($qo, $question, false, false, $this->get_format($qo->questiontextformat));
876 * This is not a real question type. It's a dummy type used to specify the
877 * import category. The format is:
878 * <question type="category">
879 * <category>tom/dick/harry</category>
882 protected function import_category($question) {
883 $qo = new stdClass();
884 $qo->qtype = 'category';
885 $qo->category = $this->import_text($question['#']['category'][0]['#']['text']);
890 * Parse the array of lines into an array of questions
891 * this *could* burn memory - but it won't happen that much
892 * so fingers crossed!
893 * @param array of lines from the input file.
894 * @param stdClass $context
895 * @return array (of objects) question objects.
897 protected function readquestions($lines) {
898 // We just need it as one big string
899 $lines = implode('', $lines);
901 // This converts xml to big nasty data structure
902 // the 0 means keep white space as it is (important for markdown format)
904 $xml = xmlize($lines, 0, 'UTF-8', true);
905 } catch (xml_format_exception $e) {
906 $this->error($e->getMessage(), '');
909 unset($lines); // No need to keep this in memory.
911 // Set up array to hold all our questions
912 $questions = array();
914 // Iterate through questions
915 foreach ($xml['quiz']['#']['question'] as $question) {
916 $questiontype = $question['@']['type'];
918 if ($questiontype == 'multichoice') {
919 $qo = $this->import_multichoice($question);
920 } else if ($questiontype == 'truefalse') {
921 $qo = $this->import_truefalse($question);
922 } else if ($questiontype == 'shortanswer') {
923 $qo = $this->import_shortanswer($question);
924 } else if ($questiontype == 'numerical') {
925 $qo = $this->import_numerical($question);
926 } else if ($questiontype == 'description') {
927 $qo = $this->import_description($question);
928 } else if ($questiontype == 'matching' || $questiontype == 'match') {
929 $qo = $this->import_match($question);
930 } else if ($questiontype == 'cloze' || $questiontype == 'multianswer') {
931 $qo = $this->import_multianswer($question);
932 } else if ($questiontype == 'essay') {
933 $qo = $this->import_essay($question);
934 } else if ($questiontype == 'calculated') {
935 $qo = $this->import_calculated($question);
936 } else if ($questiontype == 'calculatedsimple') {
937 $qo = $this->import_calculated($question);
938 $qo->qtype = 'calculatedsimple';
939 } else if ($questiontype == 'calculatedmulti') {
940 $qo = $this->import_calculated($question);
941 $qo->qtype = 'calculatedmulti';
942 } else if ($questiontype == 'category') {
943 $qo = $this->import_category($question);
946 // Not a type we handle ourselves. See if the question type wants
948 if (!$qo = $this->try_importing_using_qtypes(
949 $question, null, null, $questiontype)) {
950 $this->error(get_string('xmltypeunsupported', 'qformat_xml', $questiontype));
955 // Stick the result in the $questions array
963 // EXPORT FUNCTIONS START HERE
965 public function export_file_extension() {
970 * Turn the internal question type name into a human readable form.
971 * (In the past, the code used to use integers internally. Now, it uses
972 * strings, so there is less need for this, but to maintain
973 * backwards-compatibility we change two of the type names.)
974 * @param string $qtype question type plugin name.
975 * @return string $qtype string to use in the file.
977 protected function get_qtype($qtype) {
989 * Convert internal Moodle text format code into
990 * human readable form
991 * @param int id internal code
992 * @return string format text
994 public function get_format($id) {
997 return 'moodle_auto_format';
1001 return 'plain_text';
1004 case FORMAT_MARKDOWN:
1012 * Convert internal single question code into
1013 * human readable form
1014 * @param int id single question code
1015 * @return string single question string
1017 public function get_single($id) {
1029 * Take a string, and wrap it in a CDATA secion, if that is required to make
1030 * the output XML valid.
1031 * @param string $string a string
1032 * @return string the string, wrapped in CDATA if necessary.
1034 public function xml_escape($string) {
1035 if (!empty($string) && htmlspecialchars($string) != $string) {
1036 return "<![CDATA[{$string}]]>";
1043 * Generates <text></text> tags, processing raw text therein
1044 * @param string $raw the content to output.
1045 * @param int $indent the current indent level.
1046 * @param bool $short stick it on one line.
1047 * @return string formatted text.
1049 public function writetext($raw, $indent = 0, $short = true) {
1050 $indent = str_repeat(' ', $indent);
1051 $raw = $this->xml_escape($raw);
1054 $xml = "$indent<text>$raw</text>\n";
1056 $xml = "$indent<text>\n$raw\n$indent</text>\n";
1063 * Generte the XML to represent some files.
1064 * @param array of store array of stored_file objects.
1065 * @return string $string the XML.
1067 public function write_files($files) {
1068 if (empty($files)) {
1072 foreach ($files as $file) {
1073 if ($file->is_directory()) {
1076 $string .= '<file name="' . $file->get_filename() . '" encoding="base64">';
1077 $string .= base64_encode($file->get_content());
1078 $string .= '</file>';
1083 protected function presave_process($content) {
1084 // Override to allow us to add xml headers and footers
1085 return '<?xml version="1.0" encoding="UTF-8"?>
1087 ' . $content . '</quiz>';
1091 * Turns question into an xml segment
1092 * @param object $question the question data.
1093 * @return string xml segment
1095 public function writequestion($question) {
1096 global $CFG, $OUTPUT;
1098 $fs = get_file_storage();
1099 $contextid = $question->contextid;
1100 // Get files used by the questiontext.
1101 $question->questiontextfiles = $fs->get_area_files(
1102 $contextid, 'question', 'questiontext', $question->id);
1103 // Get files used by the generalfeedback.
1104 $question->generalfeedbackfiles = $fs->get_area_files(
1105 $contextid, 'question', 'generalfeedback', $question->id);
1106 if (!empty($question->options->answers)) {
1107 foreach ($question->options->answers as $answer) {
1108 $answer->answerfiles = $fs->get_area_files(
1109 $contextid, 'question', 'answer', $answer->id);
1110 $answer->feedbackfiles = $fs->get_area_files(
1111 $contextid, 'question', 'answerfeedback', $answer->id);
1117 // Add a comment linking this to the original question id.
1118 $expout .= "<!-- question: $question->id -->\n";
1120 // Check question type
1121 $questiontype = $this->get_qtype($question->qtype);
1123 // Categories are a special case.
1124 if ($question->qtype == 'category') {
1125 $categorypath = $this->writetext($question->category);
1126 $expout .= " <question type=\"category\">\n";
1127 $expout .= " <category>\n";
1128 $expout .= " $categorypath\n";
1129 $expout .= " </category>\n";
1130 $expout .= " </question>\n";
1134 // Now we know we are are handing a real question.
1135 // Output the generic information.
1136 $expout .= " <question type=\"$questiontype\">\n";
1137 $expout .= " <name>\n";
1138 $expout .= $this->writetext($question->name, 3);
1139 $expout .= " </name>\n";
1140 $expout .= " <questiontext {$this->format($question->questiontextformat)}>\n";
1141 $expout .= $this->writetext($question->questiontext, 3);
1142 $expout .= $this->write_files($question->questiontextfiles);
1143 $expout .= " </questiontext>\n";
1144 $expout .= " <generalfeedback {$this->format($question->generalfeedbackformat)}>\n";
1145 $expout .= $this->writetext($question->generalfeedback, 3);
1146 $expout .= $this->write_files($question->generalfeedbackfiles);
1147 $expout .= " </generalfeedback>\n";
1148 if ($question->qtype != 'multianswer') {
1149 $expout .= " <defaultgrade>{$question->defaultmark}</defaultgrade>\n";
1151 $expout .= " <penalty>{$question->penalty}</penalty>\n";
1152 $expout .= " <hidden>{$question->hidden}</hidden>\n";
1154 // The rest of the output depends on question type.
1155 switch($question->qtype) {
1157 // not a qtype really - dummy used for category switching
1161 $trueanswer = $question->options->answers[$question->options->trueanswer];
1162 $trueanswer->answer = 'true';
1163 $expout .= $this->write_answer($trueanswer);
1165 $falseanswer = $question->options->answers[$question->options->falseanswer];
1166 $falseanswer->answer = 'false';
1167 $expout .= $this->write_answer($falseanswer);
1171 $expout .= " <single>" . $this->get_single($question->options->single) .
1173 $expout .= " <shuffleanswers>" .
1174 $this->get_single($question->options->shuffleanswers) .
1175 "</shuffleanswers>\n";
1176 $expout .= " <answernumbering>" . $question->options->answernumbering .
1177 "</answernumbering>\n";
1178 $expout .= $this->write_combined_feedback($question->options, $question->id, $question->contextid);
1179 $expout .= $this->write_answers($question->options->answers);
1183 $expout .= " <usecase>{$question->options->usecase}</usecase>\n";
1184 $expout .= $this->write_answers($question->options->answers);
1188 foreach ($question->options->answers as $answer) {
1189 $expout .= $this->write_answer($answer,
1190 " <tolerance>$answer->tolerance</tolerance>\n");
1193 $units = $question->options->units;
1194 if (count($units)) {
1195 $expout .= "<units>\n";
1196 foreach ($units as $unit) {
1197 $expout .= " <unit>\n";
1198 $expout .= " <multiplier>{$unit->multiplier}</multiplier>\n";
1199 $expout .= " <unit_name>{$unit->unit}</unit_name>\n";
1200 $expout .= " </unit>\n";
1202 $expout .= "</units>\n";
1204 if (isset($question->options->unitgradingtype)) {
1205 $expout .= " <unitgradingtype>" . $question->options->unitgradingtype .
1206 "</unitgradingtype>\n";
1208 if (isset($question->options->unitpenalty)) {
1209 $expout .= " <unitpenalty>{$question->options->unitpenalty}</unitpenalty>\n";
1211 if (isset($question->options->showunits)) {
1212 $expout .= " <showunits>{$question->options->showunits}</showunits>\n";
1214 if (isset($question->options->unitsleft)) {
1215 $expout .= " <unitsleft>{$question->options->unitsleft}</unitsleft>\n";
1217 if (!empty($question->options->instructionsformat)) {
1218 $files = $fs->get_area_files($contextid, 'qtype_numerical',
1219 'instruction', $question->id);
1220 $expout .= " <instructions " .
1221 $this->format($question->options->instructionsformat) . ">\n";
1222 $expout .= $this->writetext($question->options->instructions, 3);
1223 $expout .= $this->write_files($files);
1224 $expout .= " </instructions>\n";
1229 $expout .= " <shuffleanswers>" .
1230 $this->get_single($question->options->shuffleanswers) .
1231 "</shuffleanswers>\n";
1232 $expout .= $this->write_combined_feedback($question->options, $question->id, $question->contextid);
1233 foreach ($question->options->subquestions as $subquestion) {
1234 $files = $fs->get_area_files($contextid, 'qtype_match',
1235 'subquestion', $subquestion->id);
1236 $expout .= " <subquestion " .
1237 $this->format($subquestion->questiontextformat) . ">\n";
1238 $expout .= $this->writetext($subquestion->questiontext, 3);
1239 $expout .= $this->write_files($files);
1240 $expout .= " <answer>\n";
1241 $expout .= $this->writetext($subquestion->answertext, 4);
1242 $expout .= " </answer>\n";
1243 $expout .= " </subquestion>\n";
1248 // Nothing else to do.
1252 foreach ($question->options->questions as $index => $subq) {
1253 $expout = preg_replace('~{#' . $index . '}~', $subq->questiontext, $expout);
1258 $expout .= " <responseformat>" . $question->options->responseformat .
1259 "</responseformat>\n";
1260 $expout .= " <responsefieldlines>" . $question->options->responsefieldlines .
1261 "</responsefieldlines>\n";
1262 $expout .= " <attachments>" . $question->options->attachments .
1264 $expout .= " <graderinfo " .
1265 $this->format($question->options->graderinfoformat) . ">\n";
1266 $expout .= $this->writetext($question->options->graderinfo, 3);
1267 $expout .= $this->write_files($fs->get_area_files($contextid, 'qtype_essay',
1268 'graderinfo', $question->id));
1269 $expout .= " </graderinfo>\n";
1273 case 'calculatedsimple':
1274 case 'calculatedmulti':
1275 $expout .= " <synchronize>{$question->options->synchronize}</synchronize>\n";
1276 $expout .= " <single>{$question->options->single}</single>\n";
1277 $expout .= " <answernumbering>" . $question->options->answernumbering .
1278 "</answernumbering>\n";
1279 $expout .= " <shuffleanswers>" . $question->options->shuffleanswers .
1280 "</shuffleanswers>\n";
1282 $component = 'qtype_' . $question->qtype;
1283 $files = $fs->get_area_files($contextid, $component,
1284 'correctfeedback', $question->id);
1285 $expout .= " <correctfeedback>\n";
1286 $expout .= $this->writetext($question->options->correctfeedback, 3);
1287 $expout .= $this->write_files($files);
1288 $expout .= " </correctfeedback>\n";
1290 $files = $fs->get_area_files($contextid, $component,
1291 'partiallycorrectfeedback', $question->id);
1292 $expout .= " <partiallycorrectfeedback>\n";
1293 $expout .= $this->writetext($question->options->partiallycorrectfeedback, 3);
1294 $expout .= $this->write_files($files);
1295 $expout .= " </partiallycorrectfeedback>\n";
1297 $files = $fs->get_area_files($contextid, $component,
1298 'incorrectfeedback', $question->id);
1299 $expout .= " <incorrectfeedback>\n";
1300 $expout .= $this->writetext($question->options->incorrectfeedback, 3);
1301 $expout .= $this->write_files($files);
1302 $expout .= " </incorrectfeedback>\n";
1304 foreach ($question->options->answers as $answer) {
1305 $percent = 100 * $answer->fraction;
1306 $expout .= "<answer fraction=\"$percent\">\n";
1307 // "<text/>" tags are an added feature, old files won't have them
1308 $expout .= " <text>{$answer->answer}</text>\n";
1309 $expout .= " <tolerance>{$answer->tolerance}</tolerance>\n";
1310 $expout .= " <tolerancetype>{$answer->tolerancetype}</tolerancetype>\n";
1311 $expout .= " <correctanswerformat>" .
1312 $answer->correctanswerformat . "</correctanswerformat>\n";
1313 $expout .= " <correctanswerlength>" .
1314 $answer->correctanswerlength . "</correctanswerlength>\n";
1315 $expout .= " <feedback {$this->format($answer->feedbackformat)}>\n";
1316 $files = $fs->get_area_files($contextid, $component,
1317 'instruction', $question->id);
1318 $expout .= $this->writetext($answer->feedback);
1319 $expout .= $this->write_files($answer->feedbackfiles);
1320 $expout .= " </feedback>\n";
1321 $expout .= "</answer>\n";
1323 if (isset($question->options->unitgradingtype)) {
1324 $expout .= " <unitgradingtype>" .
1325 $question->options->unitgradingtype . "</unitgradingtype>\n";
1327 if (isset($question->options->unitpenalty)) {
1328 $expout .= " <unitpenalty>" .
1329 $question->options->unitpenalty . "</unitpenalty>\n";
1331 if (isset($question->options->showunits)) {
1332 $expout .= " <showunits>{$question->options->showunits}</showunits>\n";
1334 if (isset($question->options->unitsleft)) {
1335 $expout .= " <unitsleft>{$question->options->unitsleft}</unitsleft>\n";
1338 if (isset($question->options->instructionsformat)) {
1339 $files = $fs->get_area_files($contextid, $component,
1340 'instruction', $question->id);
1341 $expout .= " <instructions " .
1342 $this->format($question->options->instructionsformat) . ">\n";
1343 $expout .= $this->writetext($question->options->instructions, 3);
1344 $expout .= $this->write_files($files);
1345 $expout .= " </instructions>\n";
1348 if (isset($question->options->units)) {
1349 $units = $question->options->units;
1350 if (count($units)) {
1351 $expout .= "<units>\n";
1352 foreach ($units as $unit) {
1353 $expout .= " <unit>\n";
1354 $expout .= " <multiplier>{$unit->multiplier}</multiplier>\n";
1355 $expout .= " <unit_name>{$unit->unit}</unit_name>\n";
1356 $expout .= " </unit>\n";
1358 $expout .= "</units>\n";
1362 // The tag $question->export_process has been set so we get all the
1363 // data items in the database from the function
1364 // qtype_calculated::get_question_options calculatedsimple defaults
1366 if (isset($question->options->datasets) && count($question->options->datasets)) {
1367 $expout .= "<dataset_definitions>\n";
1368 foreach ($question->options->datasets as $def) {
1369 $expout .= "<dataset_definition>\n";
1370 $expout .= " <status>".$this->writetext($def->status)."</status>\n";
1371 $expout .= " <name>".$this->writetext($def->name)."</name>\n";
1372 if ($question->qtype == CALCULATED) {
1373 $expout .= " <type>calculated</type>\n";
1375 $expout .= " <type>calculatedsimple</type>\n";
1377 $expout .= " <distribution>" . $this->writetext($def->distribution) .
1378 "</distribution>\n";
1379 $expout .= " <minimum>" . $this->writetext($def->minimum) .
1381 $expout .= " <maximum>" . $this->writetext($def->maximum) .
1383 $expout .= " <decimals>" . $this->writetext($def->decimals) .
1385 $expout .= " <itemcount>$def->itemcount</itemcount>\n";
1386 if ($def->itemcount > 0) {
1387 $expout .= " <dataset_items>\n";
1388 foreach ($def->items as $item) {
1389 $expout .= " <dataset_item>\n";
1390 $expout .= " <number>".$item->itemnumber."</number>\n";
1391 $expout .= " <value>".$item->value."</value>\n";
1392 $expout .= " </dataset_item>\n";
1394 $expout .= " </dataset_items>\n";
1395 $expout .= " <number_of_items>" . $def->number_of_items .
1396 "</number_of_items>\n";
1398 $expout .= "</dataset_definition>\n";
1400 $expout .= "</dataset_definitions>\n";
1405 // try support by optional plugin
1406 if (!$data = $this->try_exporting_using_qtypes($question->qtype, $question)) {
1407 notify(get_string('unsupportedexport', 'qformat_xml', $question->qtype));
1412 // Output any hints.
1413 $expout .= $this->write_hints($question);
1415 // Write the question tags.
1416 if (!empty($CFG->usetags)) {
1417 require_once($CFG->dirroot.'/tag/lib.php');
1418 $tags = tag_get_tags_array('question', $question->id);
1419 if (!empty($tags)) {
1420 $expout .= " <tags>\n";
1421 foreach ($tags as $tag) {
1422 $expout .= " <tag>" . $this->writetext($tag, 0, true) . "</tag>\n";
1424 $expout .= " </tags>\n";
1428 // close the question tag
1429 $expout .= " </question>\n";
1434 public function write_answers($answers) {
1435 if (empty($answers)) {
1439 foreach ($answers as $answer) {
1440 $output .= $this->write_answer($answer);
1445 public function write_answer($answer, $extra = '') {
1446 $percent = $answer->fraction * 100;
1448 $output .= " <answer fraction=\"$percent\" {$this->format($answer->answerformat)}>\n";
1449 $output .= $this->writetext($answer->answer, 3);
1450 $output .= $this->write_files($answer->answerfiles);
1451 $output .= " <feedback {$this->format($answer->feedbackformat)}>\n";
1452 $output .= $this->writetext($answer->feedback, 4);
1453 $output .= $this->write_files($answer->feedbackfiles);
1454 $output .= " </feedback>\n";
1456 $output .= " </answer>\n";
1461 * Write out the hints.
1462 * @param object $question the question definition data.
1463 * @return string XML to output.
1465 public function write_hints($question) {
1466 if (empty($question->hints)) {
1471 foreach ($question->hints as $hint) {
1472 $output .= $this->write_hint($hint, $question->contextid);
1478 * @param int $format a FORMAT_... constant.
1479 * @return string the attribute to add to an XML tag.
1481 public function format($format) {
1482 return 'format="' . $this->get_format($format) . '"';
1485 public function write_hint($hint, $contextid) {
1486 $fs = get_file_storage();
1487 $files = $fs->get_area_files($contextid, 'question', 'hint', $hint->id);
1490 $output .= " <hint {$this->format($hint->hintformat)}>\n";
1491 $output .= ' ' . $this->writetext($hint->hint);
1493 if (!empty($hint->shownumcorrect)) {
1494 $output .= " <shownumcorrect/>\n";
1496 if (!empty($hint->clearwrong)) {
1497 $output .= " <clearwrong/>\n";
1500 if (!empty($hint->options)) {
1501 $output .= ' <options>' . $this->xml_escape($hint->options) . "</options>\n";
1503 $output .= $this->write_files($files);
1504 $output .= " </hint>\n";
1509 * Output the combined feedback fields.
1510 * @param object $questionoptions the question definition data.
1511 * @param int $questionid the question id.
1512 * @param int $contextid the question context id.
1513 * @return string XML to output.
1515 public function write_combined_feedback($questionoptions, $questionid, $contextid) {
1516 $fs = get_file_storage();
1519 $fields = array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback');
1520 foreach ($fields as $field) {
1521 $formatfield = $field . 'format';
1522 $files = $fs->get_area_files($contextid, 'question', $field, $questionid);
1524 $output .= " <{$field} {$this->format($questionoptions->$formatfield)}>\n";
1525 $output .= ' ' . $this->writetext($questionoptions->$field);
1526 $output .= $this->write_files($files);
1527 $output .= " </{$field}>\n";
1530 if (!empty($questionoptions->shownumcorrect)) {
1531 $output .= " <shownumcorrect/>\n";