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'));
154 * import parts of question common to all types
155 * @param $question array question question array from xml tree
156 * @return object question object
158 public function import_headers($question) {
161 // get some error strings
162 $error_noname = get_string('xmlimportnoname', 'qformat_xml');
163 $error_noquestion = get_string('xmlimportnoquestion', 'qformat_xml');
165 // this routine initialises the question object
166 $qo = $this->defaultquestion();
169 $qo->name = $this->clean_question_name($this->getpath($question,
170 array('#', 'name', 0, '#', 'text', 0, '#'), '', true,
171 get_string('xmlimportnoname', 'qformat_xml')));
172 $qo->questiontext = $this->getpath($question,
173 array('#', 'questiontext', 0, '#', 'text', 0, '#'), '', true);
174 $qo->questiontextformat = $this->trans_format($this->getpath(
175 $question, array('#', 'questiontext', 0, '@', 'format'), 'html'));
177 $qo->questiontextfiles = $this->import_files($this->getpath($question,
178 array('#', 'questiontext', 0, '#', 'file'), array(), false));
180 // Backwards compatibility, deal with the old image tag.
181 $filedata = $this->getpath($question, array('#', 'image_base64', '0', '#'), null, false);
182 $filename = $this->getpath($question, array('#', 'image', '0', '#'), null, false);
183 if ($filedata && $filename) {
184 $data = new stdClass();
185 $data->content = $filedata;
186 $data->encoding = 'base64';
187 // Question file areas don't support subdirs, so convert path to filename if necessary.
188 $data->name = clean_param(str_replace('/', '_', $filename), PARAM_FILE);
189 $qo->questiontextfiles[] = $data;
190 $qo->questiontext .= ' <img src="@@PLUGINFILE@@/' . $data->name . '" />';
193 // restore files in generalfeedback
194 $qo->generalfeedback = $this->getpath($question,
195 array('#', 'generalfeedback', 0, '#', 'text', 0, '#'), $qo->generalfeedback, true);
196 $qo->generalfeedbackfiles = array();
197 $qo->generalfeedbackformat = $this->trans_format($this->getpath($question,
198 array('#', 'generalfeedback', 0, '@', 'format'), $this->get_format($qo->questiontextformat)));
199 $qo->generalfeedbackfiles = $this->import_files($this->getpath($question,
200 array('#', 'generalfeedback', 0, '#', 'file'), array(), false));
202 $qo->defaultmark = $this->getpath($question,
203 array('#', 'defaultgrade', 0, '#'), $qo->defaultmark);
204 $qo->penalty = $this->getpath($question,
205 array('#', 'penalty', 0, '#'), $qo->penalty);
207 // Fix problematic rounding from old files:
208 if (abs($qo->penalty - 0.3333333) < 0.005) {
209 $qo->penalty = 0.3333333;
212 // Read the question tags.
213 if (!empty($CFG->usetags) && array_key_exists('tags', $question['#'])
214 && !empty($question['#']['tags'][0]['#']['tag'])) {
215 require_once($CFG->dirroot.'/tag/lib.php');
217 foreach ($question['#']['tags'][0]['#']['tag'] as $tagdata) {
218 $qo->tags[] = $this->getpath($tagdata, array('#', 'text', 0, '#'), '', true);
226 * Import the common parts of a single answer
227 * @param array answer xml tree for single answer
228 * @param bool $withanswerfiles if true, the answers are HTML (or $defaultformat)
229 * and so may contain files, otherwise the answers are plain text.
230 * @param array Default text format for the feedback, and the answers if $withanswerfiles
232 * @return object answer object
234 public function import_answer($answer, $withanswerfiles = false, $defaultformat = 'html') {
235 $ans = new stdClass();
237 $ans->answer = array();
238 $ans->answer['text'] = $this->getpath($answer, array('#', 'text', 0, '#'), '', true);
239 $ans->answer['format'] = $this->trans_format($this->getpath($answer,
240 array('@', 'format'), $defaultformat));
241 if ($withanswerfiles) {
242 $ans->answer['files'] = $this->import_files($this->getpath($answer,
243 array('#', 'file'), array()));
245 $ans->answer['format'] = FORMAT_PLAIN;
248 $ans->feedback = array();
249 $ans->feedback['text'] = $this->getpath($answer,
250 array('#', 'feedback', 0, '#', 'text', 0, '#'), '', true);
251 $ans->feedback['format'] = $this->trans_format($this->getpath($answer,
252 array('#', 'feedback', 0, '@', 'format'), $defaultformat));
253 $ans->feedback['files'] = $this->import_files($this->getpath($answer,
254 array('#', 'feedback', 0, '#', 'file'), array()));
256 $ans->fraction = $this->getpath($answer, array('@', 'fraction'), 0) / 100;
262 * Import the common overall feedback fields.
263 * @param object $question the part of the XML relating to this question.
264 * @param object $qo the question data to add the fields to.
265 * @param bool $withshownumpartscorrect include the shownumcorrect field.
267 public function import_combined_feedback($qo, $questionxml, $withshownumpartscorrect = false) {
268 $fields = array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback');
269 foreach ($fields as $field) {
271 $text['text'] = $this->getpath($questionxml,
272 array('#', $field, 0, '#', 'text', 0, '#'), '', true);
273 $text['format'] = $this->trans_format($this->getpath($questionxml,
274 array('#', $field, 0, '@', 'format'), $this->get_format($qo->questiontextformat)));
275 $text['files'] = $this->import_files($this->getpath($questionxml,
276 array('#', $field, 0, '#', 'file'), array(), false));
281 if ($withshownumpartscorrect) {
282 $qo->shownumcorrect = array_key_exists('shownumcorrect', $questionxml['#']);
284 // Backwards compatibility:
285 if (array_key_exists('correctresponsesfeedback', $questionxml['#'])) {
286 $qo->shownumcorrect = $this->trans_single($this->getpath($questionxml,
287 array('#', 'correctresponsesfeedback', 0, '#'), 1));
293 * Import a question hint
294 * @param array $hintxml hint xml fragment.
295 * @param string $defaultformat the text format to assume for hints that do not specify.
296 * @return object hint for storing in the database.
298 public function import_hint($hintxml, $defaultformat) {
299 if (array_key_exists('hintcontent', $hintxml['#'])) {
300 // Backwards compatibility:
302 $hint = new stdClass();
303 $hint->hint = array('format' => FORMAT_HTML, 'files' => array());
304 $hint->hint['text'] = $this->getpath($hintxml,
305 array('#', 'hintcontent', 0, '#', 'text', 0, '#'), '', true);
306 $hint->hint['format'] = $this->trans_format($defaultformat);
307 $hint->hint['files'] = array();
308 $hint->shownumcorrect = $this->getpath($hintxml,
309 array('#', 'statenumberofcorrectresponses', 0, '#'), 0);
310 $hint->clearwrong = $this->getpath($hintxml,
311 array('#', 'clearincorrectresponses', 0, '#'), 0);
312 $hint->options = $this->getpath($hintxml,
313 array('#', 'showfeedbacktoresponses', 0, '#'), 0);
318 $hint = new stdClass();
319 $hint->hint['text'] = $this->getpath($hintxml,
320 array('#', 'text', 0, '#'), '', true);
321 $hint->hint['format'] = $this->trans_format($this->getpath($hintxml,
322 array('@', 'format'), $defaultformat));
323 $hint->hint['files'] = $this->import_files($this->getpath($hintxml,
324 array('#', 'file'), array(), false));
325 $hint->shownumcorrect = array_key_exists('shownumcorrect', $hintxml['#']);
326 $hint->clearwrong = array_key_exists('clearwrong', $hintxml['#']);
327 $hint->options = $this->getpath($hintxml, array('#', 'options', 0, '#'), '', true);
333 * Import all the question hints
335 * @param object $qo the question data that is being constructed.
336 * @param array $questionxml The xml representing the question.
337 * @param bool $withparts whether the extra fields relating to parts should be imported.
338 * @param bool $withoptions whether the extra options field should be imported.
339 * @param string $defaultformat the text format to assume for hints that do not specify.
340 * @return array of objects representing the hints in the file.
342 public function import_hints($qo, $questionxml, $withparts = false,
343 $withoptions = false, $defaultformat = 'html') {
344 if (!isset($questionxml['#']['hint'])) {
348 foreach ($questionxml['#']['hint'] as $hintxml) {
349 $hint = $this->import_hint($hintxml, $defaultformat);
350 $qo->hint[] = $hint->hint;
353 $qo->hintshownumcorrect[] = $hint->shownumcorrect;
354 $qo->hintclearwrong[] = $hint->clearwrong;
358 $qo->hintoptions[] = $hint->options;
364 * Import files from a node in the XML.
365 * @param array $xml an array of <file> nodes from the the parsed XML.
366 * @return array of things representing files - in the form that save_question expects.
368 public function import_files($xml) {
370 foreach ($xml as $file) {
371 $data = new stdClass();
372 $data->content = $file['#'];
373 $data->encoding = $file['@']['encoding'];
374 $data->name = $file['@']['name'];
381 * import multiple choice question
382 * @param array question question array from xml tree
383 * @return object question object
385 public function import_multichoice($question) {
387 $qo = $this->import_headers($question);
389 // 'header' parts particular to multichoice
390 $qo->qtype = MULTICHOICE;
391 $single = $this->getpath($question, array('#', 'single', 0, '#'), 'true');
392 $qo->single = $this->trans_single($single);
393 $shuffleanswers = $this->getpath($question,
394 array('#', 'shuffleanswers', 0, '#'), 'false');
395 $qo->answernumbering = $this->getpath($question,
396 array('#', 'answernumbering', 0, '#'), 'abc');
397 $qo->shuffleanswers = $this->trans_single($shuffleanswers);
399 // There was a time on the 1.8 branch when it could output an empty
400 // answernumbering tag, so fix up any found.
401 if (empty($qo->answernumbering)) {
402 $qo->answernumbering = 'abc';
405 // Run through the answers
406 $answers = $question['#']['answer'];
408 foreach ($answers as $answer) {
409 $ans = $this->import_answer($answer, true, $this->get_format($qo->questiontextformat));
410 $qo->answer[$acount] = $ans->answer;
411 $qo->fraction[$acount] = $ans->fraction;
412 $qo->feedback[$acount] = $ans->feedback;
416 $this->import_combined_feedback($qo, $question, true);
417 $this->import_hints($qo, $question, true, false, $this->get_format($qo->questiontextformat));
423 * Import cloze type question
424 * @param array question question array from xml tree
425 * @return object question object
427 public function import_multianswer($question) {
428 question_bank::get_qtype('multianswer');
430 $questiontext['text'] = $this->import_text($question['#']['questiontext'][0]['#']['text']);
431 $questiontext['format'] = FORMAT_HTML;
432 $questiontext['itemid'] = '';
433 $qo = qtype_multianswer_extract_question($questiontext);
435 // 'header' parts particular to multianswer
436 $qo->qtype = 'multianswer';
437 $qo->course = $this->course;
438 $qo->generalfeedback = '';
440 $qo->name = $this->clean_question_name($this->import_text($question['#']['name'][0]['#']['text']));
441 $qo->questiontextformat = $questiontext['format'];
442 $qo->questiontext = $qo->questiontext['text'];
443 $qo->questiontextfiles = array();
445 // restore files in generalfeedback
446 $qo->generalfeedback = $this->getpath($question,
447 array('#', 'generalfeedback', 0, '#', 'text', 0, '#'), $qo->generalfeedback, true);
448 $qo->generalfeedbackformat = $this->trans_format($this->getpath($question,
449 array('#', 'generalfeedback', 0, '@', 'format'), $this->get_format($qo->questiontextformat)));
450 $qo->generalfeedbackfiles = $this->import_files($this->getpath($question,
451 array('#', 'generalfeedback', 0, '#', 'file'), array(), false));
453 $qo->penalty = $this->getpath($question,
454 array('#', 'penalty', 0, '#'), $this->defaultquestion()->penalty);
455 // Fix problematic rounding from old files:
456 if (abs($qo->penalty - 0.3333333) < 0.005) {
457 $qo->penalty = 0.3333333;
460 $this->import_hints($qo, $question, false, false, $this->get_format($qo->questiontextformat));
466 * Import true/false type question
467 * @param array question question array from xml tree
468 * @return object question object
470 public function import_truefalse($question) {
473 $qo = $this->import_headers($question);
475 // 'header' parts particular to true/false
476 $qo->qtype = TRUEFALSE;
478 // In the past, it used to be assumed that the two answers were in the file
479 // true first, then false. Howevever that was not always true. Now, we
480 // try to match on the answer text, but in old exports, this will be a localised
481 // string, so if we don't find true or false, we fall back to the old system.
484 foreach ($question['#']['answer'] as $answer) {
485 $answertext = $this->getpath($answer,
486 array('#', 'text', 0, '#'), '', true);
487 $feedback = $this->getpath($answer,
488 array('#', 'feedback', 0, '#', 'text', 0, '#'), '', true);
489 $feedbackformat = $this->getpath($answer,
490 array('#', 'feedback', 0, '@', 'format'), $this->get_format($qo->questiontextformat));
491 $feedbackfiles = $this->getpath($answer,
492 array('#', 'feedback', 0, '#', 'file'), array());
494 foreach ($feedbackfiles as $file) {
495 $data = new stdClass();
496 $data->content = $file['#'];
497 $data->encoding = $file['@']['encoding'];
498 $data->name = $file['@']['name'];
501 if ($answertext != 'true' && $answertext != 'false') {
502 // Old style file, assume order is true/false.
505 $answertext = 'true';
507 $answertext = 'false';
511 if ($answertext == 'true') {
512 $qo->answer = ($answer['@']['fraction'] == 100);
513 $qo->correctanswer = $qo->answer;
514 $qo->feedbacktrue = array();
515 $qo->feedbacktrue['text'] = $feedback;
516 $qo->feedbacktrue['format'] = $this->trans_format($feedbackformat);
517 $qo->feedbacktrue['files'] = $files;
519 $qo->answer = ($answer['@']['fraction'] != 100);
520 $qo->correctanswer = $qo->answer;
521 $qo->feedbackfalse = array();
522 $qo->feedbackfalse['text'] = $feedback;
523 $qo->feedbackfalse['format'] = $this->trans_format($feedbackformat);
524 $qo->feedbackfalse['files'] = $files;
531 $a->questiontext = $qo->questiontext;
532 $a->answer = get_string($qo->correctanswer ? 'true' : 'false', 'qtype_truefalse');
533 echo $OUTPUT->notification(get_string('truefalseimporterror', 'qformat_xml', $a));
536 $this->import_hints($qo, $question, false, false, $this->get_format($qo->questiontextformat));
542 * Import short answer type question
543 * @param array question question array from xml tree
544 * @return object question object
546 public function import_shortanswer($question) {
548 $qo = $this->import_headers($question);
550 // header parts particular to shortanswer
551 $qo->qtype = SHORTANSWER;
554 $qo->usecase = $this->getpath($question, array('#', 'usecase', 0, '#'), $qo->usecase);
556 // Run through the answers
557 $answers = $question['#']['answer'];
559 foreach ($answers as $answer) {
560 $ans = $this->import_answer($answer, false, $this->get_format($qo->questiontextformat));
561 $qo->answer[$acount] = $ans->answer['text'];
562 $qo->fraction[$acount] = $ans->fraction;
563 $qo->feedback[$acount] = $ans->feedback;
567 $this->import_hints($qo, $question, false, false, $this->get_format($qo->questiontextformat));
573 * Import description type question
574 * @param array question question array from xml tree
575 * @return object question object
577 public function import_description($question) {
579 $qo = $this->import_headers($question);
580 // header parts particular to shortanswer
581 $qo->qtype = DESCRIPTION;
582 $qo->defaultmark = 0;
588 * Import numerical type question
589 * @param array question question array from xml tree
590 * @return object question object
592 public function import_numerical($question) {
594 $qo = $this->import_headers($question);
596 // header parts particular to numerical
597 $qo->qtype = NUMERICAL;
600 $answers = $question['#']['answer'];
601 $qo->answer = array();
602 $qo->feedback = array();
603 $qo->fraction = array();
604 $qo->tolerance = array();
605 foreach ($answers as $answer) {
606 // answer outside of <text> is deprecated
607 $obj = $this->import_answer($answer, false, $this->get_format($qo->questiontextformat));
608 $qo->answer[] = $obj->answer['text'];
609 if (empty($qo->answer)) {
612 $qo->feedback[] = $obj->feedback;
613 $qo->tolerance[] = $this->getpath($answer, array('#', 'tolerance', 0, '#'), 0);
615 // fraction as a tag is deprecated
616 $fraction = $this->getpath($answer, array('@', 'fraction'), 0) / 100;
617 $qo->fraction[] = $this->getpath($answer,
618 array('#', 'fraction', 0, '#'), $fraction); // deprecated
621 // Get the units array
623 $units = $this->getpath($question, array('#', 'units', 0, '#', 'unit'), array());
624 if (!empty($units)) {
625 $qo->multiplier = array();
626 foreach ($units as $unit) {
627 $qo->multiplier[] = $this->getpath($unit, array('#', 'multiplier', 0, '#'), 1);
628 $qo->unit[] = $this->getpath($unit, array('#', 'unit_name', 0, '#'), '', true);
631 $qo->unitgradingtype = $this->getpath($question, array('#', 'unitgradingtype', 0, '#'), 0);
632 $qo->unitpenalty = $this->getpath($question, array('#', 'unitpenalty', 0, '#'), 0.1);
633 $qo->showunits = $this->getpath($question, array('#', 'showunits', 0, '#'), null);
634 $qo->unitsleft = $this->getpath($question, array('#', 'unitsleft', 0, '#'), 0);
635 $qo->instructions['text'] = '';
636 $qo->instructions['format'] = FORMAT_HTML;
637 $instructions = $this->getpath($question, array('#', 'instructions'), array());
638 if (!empty($instructions)) {
639 $qo->instructions = array();
640 $qo->instructions['text'] = $this->getpath($instructions,
641 array('0', '#', 'text', '0', '#'), '', true);
642 $qo->instructions['format'] = $this->trans_format($this->getpath($instructions,
643 array('0', '@', 'format'), $this->get_format($qo->questiontextformat)));
644 $qo->instructions['files'] = $this->import_files($this->getpath(
645 $instructions, array('0', '#', 'file'), array()));
648 if (is_null($qo->showunits)) {
649 // Set a good default, depending on whether there are any units defined.
650 if (empty($qo->unit)) {
651 $qo->showunits = 3; // qtype_numerical::UNITNONE;
653 $qo->showunits = 0; // qtype_numerical::UNITOPTIONAL;
657 $this->import_hints($qo, $question, false, false, $this->get_format($qo->questiontextformat));
663 * Import matching type question
664 * @param array question question array from xml tree
665 * @return object question object
667 public function import_match($question) {
669 $qo = $this->import_headers($question);
671 // header parts particular to matching
672 $qo->qtype = 'match';
673 $qo->shuffleanswers = $this->trans_single($this->getpath($question,
674 array('#', 'shuffleanswers', 0, '#'), 1));
676 // run through subquestions
677 $qo->subquestions = array();
678 $qo->subanswers = array();
679 foreach ($question['#']['subquestion'] as $subqxml) {
680 $subquestion = array();
681 $subquestion['text'] = $this->getpath($subqxml, array('#', 'text', 0, '#'), '', true);
682 $subquestion['format'] = $this->trans_format($this->getpath($subqxml,
683 array('@', 'format'), $this->get_format($qo->questiontextformat)));
684 $subquestion['files'] = $this->import_files($this->getpath($subqxml,
685 array('#', 'file'), array()));
687 $qo->subquestions[] = $subquestion;
688 $answers = $this->getpath($subqxml, array('#', 'answer'), array());
689 $qo->subanswers[] = $this->getpath($subqxml,
690 array('#', 'answer', 0, '#', 'text', 0, '#'), '', true);
693 $this->import_combined_feedback($qo, $question, true);
694 $this->import_hints($qo, $question, true, false, $this->get_format($qo->questiontextformat));
700 * Import essay type question
701 * @param array question question array from xml tree
702 * @return object question object
704 public function import_essay($question) {
706 $qo = $this->import_headers($question);
708 // header parts particular to essay
711 $qo->responseformat = $this->getpath($question,
712 array('#', 'responseformat', 0, '#'), 'editor');
713 $qo->responsefieldlines = $this->getpath($question,
714 array('#', 'responsefieldlines', 0, '#'), 15);
715 $qo->attachments = $this->getpath($question,
716 array('#', 'attachments', 0, '#'), 0);
717 $qo->graderinfo['text'] = $this->getpath($question,
718 array('#', 'graderinfo', 0, '#', 'text', 0, '#'), '', true);
719 $qo->graderinfo['format'] = $this->trans_format($this->getpath($question,
720 array('#', 'graderinfo', 0, '@', 'format'), $this->get_format($qo->questiontextformat)));
721 $qo->graderinfo['files'] = $this->import_files($this->getpath($question,
722 array('#', 'graderinfo', '0', '#', 'file'), array()));
728 * Import a calculated question
729 * @param object $question the imported XML data.
731 public function import_calculated($question) {
734 $qo = $this->import_headers($question);
736 // header parts particular to calculated
737 $qo->qtype = CALCULATED;
738 $qo->synchronize = $this->getpath($question, array('#', 'synchronize', 0, '#'), 0);
739 $single = $this->getpath($question, array('#', 'single', 0, '#'), 'true');
740 $qo->single = $this->trans_single($single);
741 $shuffleanswers = $this->getpath($question, array('#', 'shuffleanswers', 0, '#'), 'false');
742 $qo->answernumbering = $this->getpath($question,
743 array('#', 'answernumbering', 0, '#'), 'abc');
744 $qo->shuffleanswers = $this->trans_single($shuffleanswers);
746 $this->import_combined_feedback($qo, $question);
748 $qo->unitgradingtype = $this->getpath($question,
749 array('#', 'unitgradingtype', 0, '#'), 0);
750 $qo->unitpenalty = $this->getpath($question, array('#', 'unitpenalty', 0, '#'), null);
751 $qo->showunits = $this->getpath($question, array('#', 'showunits', 0, '#'), 0);
752 $qo->unitsleft = $this->getpath($question, array('#', 'unitsleft', 0, '#'), 0);
753 $qo->instructions = $this->getpath($question,
754 array('#', 'instructions', 0, '#', 'text', 0, '#'), '', true);
755 if (!empty($instructions)) {
756 $qo->instructions = array();
757 $qo->instructions['text'] = $this->getpath($instructions,
758 array('0', '#', 'text', '0', '#'), '', true);
759 $qo->instructions['format'] = $this->trans_format($this->getpath($instructions,
760 array('0', '@', 'format'), $this->get_format($qo->questiontextformat)));
761 $qo->instructions['files'] = $this->import_files($this->getpath($instructions,
762 array('0', '#', 'file'), array()));
766 $answers = $question['#']['answer'];
767 $qo->answers = array();
768 $qo->feedback = array();
769 $qo->fraction = array();
770 $qo->tolerance = array();
771 $qo->tolerancetype = array();
772 $qo->correctanswerformat = array();
773 $qo->correctanswerlength = array();
774 $qo->feedback = array();
775 foreach ($answers as $answer) {
776 $ans = $this->import_answer($answer, true, $this->get_format($qo->questiontextformat));
777 // answer outside of <text> is deprecated
778 if (empty($ans->answer['text'])) {
779 $ans->answer['text'] = '*';
781 $qo->answers[] = $ans->answer;
782 $qo->feedback[] = $ans->feedback;
783 $qo->tolerance[] = $answer['#']['tolerance'][0]['#'];
784 // fraction as a tag is deprecated
785 if (!empty($answer['#']['fraction'][0]['#'])) {
786 $qo->fraction[] = $answer['#']['fraction'][0]['#'];
788 $qo->fraction[] = $answer['@']['fraction'] / 100;
790 $qo->tolerancetype[] = $answer['#']['tolerancetype'][0]['#'];
791 $qo->correctanswerformat[] = $answer['#']['correctanswerformat'][0]['#'];
792 $qo->correctanswerlength[] = $answer['#']['correctanswerlength'][0]['#'];
796 if (isset($question['#']['units'][0]['#']['unit'])) {
797 $units = $question['#']['units'][0]['#']['unit'];
798 $qo->multiplier = array();
799 foreach ($units as $unit) {
800 $qo->multiplier[] = $unit['#']['multiplier'][0]['#'];
801 $qo->unit[] = $unit['#']['unit_name'][0]['#'];
804 $instructions = $this->getpath($question, array('#', 'instructions'), array());
805 if (!empty($instructions)) {
806 $qo->instructions = array();
807 $qo->instructions['text'] = $this->getpath($instructions,
808 array('0', '#', 'text', '0', '#'), '', true);
809 $qo->instructions['format'] = $this->trans_format($this->getpath($instructions,
810 array('0', '@', 'format'), $this->get_format($qo->questiontextformat)));
811 $qo->instructions['files'] = $this->import_files($this->getpath($instructions,
812 array('0', '#', 'file'), array()));
815 if (is_null($qo->unitpenalty)) {
816 // Set a good default, depending on whether there are any units defined.
817 if (empty($qo->unit)) {
818 $qo->showunits = 3; // qtype_numerical::UNITNONE;
820 $qo->showunits = 0; // qtype_numerical::UNITOPTIONAL;
824 $datasets = $question['#']['dataset_definitions'][0]['#']['dataset_definition'];
825 $qo->dataset = array();
826 $qo->datasetindex= 0;
827 foreach ($datasets as $dataset) {
829 $qo->dataset[$qo->datasetindex] = new stdClass();
830 $qo->dataset[$qo->datasetindex]->status =
831 $this->import_text($dataset['#']['status'][0]['#']['text']);
832 $qo->dataset[$qo->datasetindex]->name =
833 $this->import_text($dataset['#']['name'][0]['#']['text']);
834 $qo->dataset[$qo->datasetindex]->type =
835 $dataset['#']['type'][0]['#'];
836 $qo->dataset[$qo->datasetindex]->distribution =
837 $this->import_text($dataset['#']['distribution'][0]['#']['text']);
838 $qo->dataset[$qo->datasetindex]->max =
839 $this->import_text($dataset['#']['maximum'][0]['#']['text']);
840 $qo->dataset[$qo->datasetindex]->min =
841 $this->import_text($dataset['#']['minimum'][0]['#']['text']);
842 $qo->dataset[$qo->datasetindex]->length =
843 $this->import_text($dataset['#']['decimals'][0]['#']['text']);
844 $qo->dataset[$qo->datasetindex]->distribution =
845 $this->import_text($dataset['#']['distribution'][0]['#']['text']);
846 $qo->dataset[$qo->datasetindex]->itemcount = $dataset['#']['itemcount'][0]['#'];
847 $qo->dataset[$qo->datasetindex]->datasetitem = array();
848 $qo->dataset[$qo->datasetindex]->itemindex = 0;
849 $qo->dataset[$qo->datasetindex]->number_of_items =
850 $dataset['#']['number_of_items'][0]['#'];
851 $datasetitems = $dataset['#']['dataset_items'][0]['#']['dataset_item'];
852 foreach ($datasetitems as $datasetitem) {
853 $qo->dataset[$qo->datasetindex]->itemindex++;
854 $qo->dataset[$qo->datasetindex]->datasetitem[
855 $qo->dataset[$qo->datasetindex]->itemindex] = new stdClass();
856 $qo->dataset[$qo->datasetindex]->datasetitem[
857 $qo->dataset[$qo->datasetindex]->itemindex]->itemnumber =
858 $datasetitem['#']['number'][0]['#'];
859 $qo->dataset[$qo->datasetindex]->datasetitem[
860 $qo->dataset[$qo->datasetindex]->itemindex]->value =
861 $datasetitem['#']['value'][0]['#'];
865 $this->import_hints($qo, $question, false, false, $this->get_format($qo->questiontextformat));
871 * This is not a real question type. It's a dummy type used to specify the
872 * import category. The format is:
873 * <question type="category">
874 * <category>tom/dick/harry</category>
877 protected function import_category($question) {
878 $qo = new stdClass();
879 $qo->qtype = 'category';
880 $qo->category = $this->import_text($question['#']['category'][0]['#']['text']);
885 * Parse the array of lines into an array of questions
886 * this *could* burn memory - but it won't happen that much
887 * so fingers crossed!
888 * @param array of lines from the input file.
889 * @param stdClass $context
890 * @return array (of objects) question objects.
892 protected function readquestions($lines) {
893 // We just need it as one big string
894 $text = implode($lines, ' ');
897 // This converts xml to big nasty data structure
898 // the 0 means keep white space as it is (important for markdown format)
900 $xml = xmlize($text, 0, 'UTF-8', true);
901 } catch (xml_format_exception $e) {
902 $this->error($e->getMessage(), '');
905 // Set up array to hold all our questions
906 $questions = array();
908 // Iterate through questions
909 foreach ($xml['quiz']['#']['question'] as $question) {
910 $questiontype = $question['@']['type'];
912 if ($questiontype == 'multichoice') {
913 $qo = $this->import_multichoice($question);
914 } else if ($questiontype == 'truefalse') {
915 $qo = $this->import_truefalse($question);
916 } else if ($questiontype == 'shortanswer') {
917 $qo = $this->import_shortanswer($question);
918 } else if ($questiontype == 'numerical') {
919 $qo = $this->import_numerical($question);
920 } else if ($questiontype == 'description') {
921 $qo = $this->import_description($question);
922 } else if ($questiontype == 'matching' || $questiontype == 'match') {
923 $qo = $this->import_match($question);
924 } else if ($questiontype == 'cloze' || $questiontype == 'multianswer') {
925 $qo = $this->import_multianswer($question);
926 } else if ($questiontype == 'essay') {
927 $qo = $this->import_essay($question);
928 } else if ($questiontype == 'calculated') {
929 $qo = $this->import_calculated($question);
930 } else if ($questiontype == 'calculatedsimple') {
931 $qo = $this->import_calculated($question);
932 $qo->qtype = 'calculatedsimple';
933 } else if ($questiontype == 'calculatedmulti') {
934 $qo = $this->import_calculated($question);
935 $qo->qtype = 'calculatedmulti';
936 } else if ($questiontype == 'category') {
937 $qo = $this->import_category($question);
940 // Not a type we handle ourselves. See if the question type wants
942 if (!$qo = $this->try_importing_using_qtypes(
943 $question, null, null, $questiontype)) {
944 $this->error(get_string('xmltypeunsupported', 'qformat_xml', $questiontype));
949 // Stick the result in the $questions array
957 // EXPORT FUNCTIONS START HERE
959 public function export_file_extension() {
964 * Turn the internal question type name into a human readable form.
965 * (In the past, the code used to use integers internally. Now, it uses
966 * strings, so there is less need for this, but to maintain
967 * backwards-compatibility we change two of the type names.)
968 * @param string $qtype question type plugin name.
969 * @return string $qtype string to use in the file.
971 protected function get_qtype($qtype) {
983 * Convert internal Moodle text format code into
984 * human readable form
985 * @param int id internal code
986 * @return string format text
988 public function get_format($id) {
991 return 'moodle_auto_format';
998 case FORMAT_MARKDOWN:
1006 * Convert internal single question code into
1007 * human readable form
1008 * @param int id single question code
1009 * @return string single question string
1011 public function get_single($id) {
1023 * Take a string, and wrap it in a CDATA secion, if that is required to make
1024 * the output XML valid.
1025 * @param string $string a string
1026 * @return string the string, wrapped in CDATA if necessary.
1028 public function xml_escape($string) {
1029 if (!empty($string) && htmlspecialchars($string) != $string) {
1030 return "<![CDATA[{$string}]]>";
1037 * Generates <text></text> tags, processing raw text therein
1038 * @param string $raw the content to output.
1039 * @param int $indent the current indent level.
1040 * @param bool $short stick it on one line.
1041 * @return string formatted text.
1043 public function writetext($raw, $indent = 0, $short = true) {
1044 $indent = str_repeat(' ', $indent);
1045 $raw = $this->xml_escape($raw);
1048 $xml = "$indent<text>$raw</text>\n";
1050 $xml = "$indent<text>\n$raw\n$indent</text>\n";
1057 * Generte the XML to represent some files.
1058 * @param array of store array of stored_file objects.
1059 * @return string $string the XML.
1061 public function write_files($files) {
1062 if (empty($files)) {
1066 foreach ($files as $file) {
1067 if ($file->is_directory()) {
1070 $string .= '<file name="' . $file->get_filename() . '" encoding="base64">';
1071 $string .= base64_encode($file->get_content());
1072 $string .= '</file>';
1077 protected function presave_process($content) {
1078 // Override to allow us to add xml headers and footers
1079 return '<?xml version="1.0" encoding="UTF-8"?>
1081 ' . $content . '</quiz>';
1085 * Turns question into an xml segment
1086 * @param object $question the question data.
1087 * @return string xml segment
1089 public function writequestion($question) {
1090 global $CFG, $OUTPUT;
1092 $fs = get_file_storage();
1093 $contextid = $question->contextid;
1094 // Get files used by the questiontext.
1095 $question->questiontextfiles = $fs->get_area_files(
1096 $contextid, 'question', 'questiontext', $question->id);
1097 // Get files used by the generalfeedback.
1098 $question->generalfeedbackfiles = $fs->get_area_files(
1099 $contextid, 'question', 'generalfeedback', $question->id);
1100 if (!empty($question->options->answers)) {
1101 foreach ($question->options->answers as $answer) {
1102 $answer->answerfiles = $fs->get_area_files(
1103 $contextid, 'question', 'answer', $answer->id);
1104 $answer->feedbackfiles = $fs->get_area_files(
1105 $contextid, 'question', 'answerfeedback', $answer->id);
1111 // Add a comment linking this to the original question id.
1112 $expout .= "<!-- question: $question->id -->\n";
1114 // Check question type
1115 $questiontype = $this->get_qtype($question->qtype);
1117 // Categories are a special case.
1118 if ($question->qtype == 'category') {
1119 $categorypath = $this->writetext($question->category);
1120 $expout .= " <question type=\"category\">\n";
1121 $expout .= " <category>\n";
1122 $expout .= " $categorypath\n";
1123 $expout .= " </category>\n";
1124 $expout .= " </question>\n";
1128 // Now we know we are are handing a real question.
1129 // Output the generic information.
1130 $expout .= " <question type=\"$questiontype\">\n";
1131 $expout .= " <name>\n";
1132 $expout .= $this->writetext($question->name, 3);
1133 $expout .= " </name>\n";
1134 $expout .= " <questiontext {$this->format($question->questiontextformat)}>\n";
1135 $expout .= $this->writetext($question->questiontext, 3);
1136 $expout .= $this->write_files($question->questiontextfiles);
1137 $expout .= " </questiontext>\n";
1138 $expout .= " <generalfeedback {$this->format($question->generalfeedbackformat)}>\n";
1139 $expout .= $this->writetext($question->generalfeedback, 3);
1140 $expout .= $this->write_files($question->generalfeedbackfiles);
1141 $expout .= " </generalfeedback>\n";
1142 if ($question->qtype != 'multianswer') {
1143 $expout .= " <defaultgrade>{$question->defaultmark}</defaultgrade>\n";
1145 $expout .= " <penalty>{$question->penalty}</penalty>\n";
1146 $expout .= " <hidden>{$question->hidden}</hidden>\n";
1148 // The rest of the output depends on question type.
1149 switch($question->qtype) {
1151 // not a qtype really - dummy used for category switching
1155 $trueanswer = $question->options->answers[$question->options->trueanswer];
1156 $trueanswer->answer = 'true';
1157 $expout .= $this->write_answer($trueanswer);
1159 $falseanswer = $question->options->answers[$question->options->falseanswer];
1160 $falseanswer->answer = 'false';
1161 $expout .= $this->write_answer($falseanswer);
1165 $expout .= " <single>" . $this->get_single($question->options->single) .
1167 $expout .= " <shuffleanswers>" .
1168 $this->get_single($question->options->shuffleanswers) .
1169 "</shuffleanswers>\n";
1170 $expout .= " <answernumbering>" . $question->options->answernumbering .
1171 "</answernumbering>\n";
1172 $expout .= $this->write_combined_feedback($question->options, $question->id, $question->contextid);
1173 $expout .= $this->write_answers($question->options->answers);
1177 $expout .= " <usecase>{$question->options->usecase}</usecase>\n";
1178 $expout .= $this->write_answers($question->options->answers);
1182 foreach ($question->options->answers as $answer) {
1183 $expout .= $this->write_answer($answer,
1184 " <tolerance>$answer->tolerance</tolerance>\n");
1187 $units = $question->options->units;
1188 if (count($units)) {
1189 $expout .= "<units>\n";
1190 foreach ($units as $unit) {
1191 $expout .= " <unit>\n";
1192 $expout .= " <multiplier>{$unit->multiplier}</multiplier>\n";
1193 $expout .= " <unit_name>{$unit->unit}</unit_name>\n";
1194 $expout .= " </unit>\n";
1196 $expout .= "</units>\n";
1198 if (isset($question->options->unitgradingtype)) {
1199 $expout .= " <unitgradingtype>" . $question->options->unitgradingtype .
1200 "</unitgradingtype>\n";
1202 if (isset($question->options->unitpenalty)) {
1203 $expout .= " <unitpenalty>{$question->options->unitpenalty}</unitpenalty>\n";
1205 if (isset($question->options->showunits)) {
1206 $expout .= " <showunits>{$question->options->showunits}</showunits>\n";
1208 if (isset($question->options->unitsleft)) {
1209 $expout .= " <unitsleft>{$question->options->unitsleft}</unitsleft>\n";
1211 if (!empty($question->options->instructionsformat)) {
1212 $files = $fs->get_area_files($contextid, 'qtype_numerical',
1213 'instruction', $question->id);
1214 $expout .= " <instructions " .
1215 $this->format($question->options->instructionsformat) . ">\n";
1216 $expout .= $this->writetext($question->options->instructions, 3);
1217 $expout .= $this->write_files($files);
1218 $expout .= " </instructions>\n";
1223 $expout .= " <shuffleanswers>" .
1224 $this->get_single($question->options->shuffleanswers) .
1225 "</shuffleanswers>\n";
1226 $expout .= $this->write_combined_feedback($question->options, $question->id, $question->contextid);
1227 foreach ($question->options->subquestions as $subquestion) {
1228 $files = $fs->get_area_files($contextid, 'qtype_match',
1229 'subquestion', $subquestion->id);
1230 $expout .= " <subquestion " .
1231 $this->format($subquestion->questiontextformat) . ">\n";
1232 $expout .= $this->writetext($subquestion->questiontext, 3);
1233 $expout .= $this->write_files($files);
1234 $expout .= " <answer>\n";
1235 $expout .= $this->writetext($subquestion->answertext, 4);
1236 $expout .= " </answer>\n";
1237 $expout .= " </subquestion>\n";
1242 // Nothing else to do.
1246 foreach ($question->options->questions as $index => $subq) {
1247 $expout = preg_replace('~{#' . $index . '}~', $subq->questiontext, $expout);
1252 $expout .= " <responseformat>" . $question->options->responseformat .
1253 "</responseformat>\n";
1254 $expout .= " <responsefieldlines>" . $question->options->responsefieldlines .
1255 "</responsefieldlines>\n";
1256 $expout .= " <attachments>" . $question->options->attachments .
1258 $expout .= " <graderinfo " .
1259 $this->format($question->options->graderinfoformat) . ">\n";
1260 $expout .= $this->writetext($question->options->graderinfo, 3);
1261 $expout .= $this->write_files($fs->get_area_files($contextid, 'qtype_essay',
1262 'graderinfo', $question->id));
1263 $expout .= " </graderinfo>\n";
1267 case 'calculatedsimple':
1268 case 'calculatedmulti':
1269 $expout .= " <synchronize>{$question->options->synchronize}</synchronize>\n";
1270 $expout .= " <single>{$question->options->single}</single>\n";
1271 $expout .= " <answernumbering>" . $question->options->answernumbering .
1272 "</answernumbering>\n";
1273 $expout .= " <shuffleanswers>" . $question->options->shuffleanswers .
1274 "</shuffleanswers>\n";
1276 $component = 'qtype_' . $question->qtype;
1277 $files = $fs->get_area_files($contextid, $component,
1278 'correctfeedback', $question->id);
1279 $expout .= " <correctfeedback>\n";
1280 $expout .= $this->writetext($question->options->correctfeedback, 3);
1281 $expout .= $this->write_files($files);
1282 $expout .= " </correctfeedback>\n";
1284 $files = $fs->get_area_files($contextid, $component,
1285 'partiallycorrectfeedback', $question->id);
1286 $expout .= " <partiallycorrectfeedback>\n";
1287 $expout .= $this->writetext($question->options->partiallycorrectfeedback, 3);
1288 $expout .= $this->write_files($files);
1289 $expout .= " </partiallycorrectfeedback>\n";
1291 $files = $fs->get_area_files($contextid, $component,
1292 'incorrectfeedback', $question->id);
1293 $expout .= " <incorrectfeedback>\n";
1294 $expout .= $this->writetext($question->options->incorrectfeedback, 3);
1295 $expout .= $this->write_files($files);
1296 $expout .= " </incorrectfeedback>\n";
1298 foreach ($question->options->answers as $answer) {
1299 $percent = 100 * $answer->fraction;
1300 $expout .= "<answer fraction=\"$percent\">\n";
1301 // "<text/>" tags are an added feature, old files won't have them
1302 $expout .= " <text>{$answer->answer}</text>\n";
1303 $expout .= " <tolerance>{$answer->tolerance}</tolerance>\n";
1304 $expout .= " <tolerancetype>{$answer->tolerancetype}</tolerancetype>\n";
1305 $expout .= " <correctanswerformat>" .
1306 $answer->correctanswerformat . "</correctanswerformat>\n";
1307 $expout .= " <correctanswerlength>" .
1308 $answer->correctanswerlength . "</correctanswerlength>\n";
1309 $expout .= " <feedback {$this->format($answer->feedbackformat)}>\n";
1310 $files = $fs->get_area_files($contextid, $component,
1311 'instruction', $question->id);
1312 $expout .= $this->writetext($answer->feedback);
1313 $expout .= $this->write_files($answer->feedbackfiles);
1314 $expout .= " </feedback>\n";
1315 $expout .= "</answer>\n";
1317 if (isset($question->options->unitgradingtype)) {
1318 $expout .= " <unitgradingtype>" .
1319 $question->options->unitgradingtype . "</unitgradingtype>\n";
1321 if (isset($question->options->unitpenalty)) {
1322 $expout .= " <unitpenalty>" .
1323 $question->options->unitpenalty . "</unitpenalty>\n";
1325 if (isset($question->options->showunits)) {
1326 $expout .= " <showunits>{$question->options->showunits}</showunits>\n";
1328 if (isset($question->options->unitsleft)) {
1329 $expout .= " <unitsleft>{$question->options->unitsleft}</unitsleft>\n";
1332 if (isset($question->options->instructionsformat)) {
1333 $files = $fs->get_area_files($contextid, $component,
1334 'instruction', $question->id);
1335 $expout .= " <instructions " .
1336 $this->format($question->options->instructionsformat) . ">\n";
1337 $expout .= $this->writetext($question->options->instructions, 3);
1338 $expout .= $this->write_files($files);
1339 $expout .= " </instructions>\n";
1342 if (isset($question->options->units)) {
1343 $units = $question->options->units;
1344 if (count($units)) {
1345 $expout .= "<units>\n";
1346 foreach ($units as $unit) {
1347 $expout .= " <unit>\n";
1348 $expout .= " <multiplier>{$unit->multiplier}</multiplier>\n";
1349 $expout .= " <unit_name>{$unit->unit}</unit_name>\n";
1350 $expout .= " </unit>\n";
1352 $expout .= "</units>\n";
1356 // The tag $question->export_process has been set so we get all the
1357 // data items in the database from the function
1358 // qtype_calculated::get_question_options calculatedsimple defaults
1360 if (isset($question->options->datasets) && count($question->options->datasets)) {
1361 $expout .= "<dataset_definitions>\n";
1362 foreach ($question->options->datasets as $def) {
1363 $expout .= "<dataset_definition>\n";
1364 $expout .= " <status>".$this->writetext($def->status)."</status>\n";
1365 $expout .= " <name>".$this->writetext($def->name)."</name>\n";
1366 if ($question->qtype == CALCULATED) {
1367 $expout .= " <type>calculated</type>\n";
1369 $expout .= " <type>calculatedsimple</type>\n";
1371 $expout .= " <distribution>" . $this->writetext($def->distribution) .
1372 "</distribution>\n";
1373 $expout .= " <minimum>" . $this->writetext($def->minimum) .
1375 $expout .= " <maximum>" . $this->writetext($def->maximum) .
1377 $expout .= " <decimals>" . $this->writetext($def->decimals) .
1379 $expout .= " <itemcount>$def->itemcount</itemcount>\n";
1380 if ($def->itemcount > 0) {
1381 $expout .= " <dataset_items>\n";
1382 foreach ($def->items as $item) {
1383 $expout .= " <dataset_item>\n";
1384 $expout .= " <number>".$item->itemnumber."</number>\n";
1385 $expout .= " <value>".$item->value."</value>\n";
1386 $expout .= " </dataset_item>\n";
1388 $expout .= " </dataset_items>\n";
1389 $expout .= " <number_of_items>" . $def->number_of_items .
1390 "</number_of_items>\n";
1392 $expout .= "</dataset_definition>\n";
1394 $expout .= "</dataset_definitions>\n";
1399 // try support by optional plugin
1400 if (!$data = $this->try_exporting_using_qtypes($question->qtype, $question)) {
1401 notify(get_string('unsupportedexport', 'qformat_xml', $question->qtype));
1406 // Output any hints.
1407 $expout .= $this->write_hints($question);
1409 // Write the question tags.
1410 if (!empty($CFG->usetags)) {
1411 require_once($CFG->dirroot.'/tag/lib.php');
1412 $tags = tag_get_tags_array('question', $question->id);
1413 if (!empty($tags)) {
1414 $expout .= " <tags>\n";
1415 foreach ($tags as $tag) {
1416 $expout .= " <tag>" . $this->writetext($tag, 0, true) . "</tag>\n";
1418 $expout .= " </tags>\n";
1422 // close the question tag
1423 $expout .= " </question>\n";
1428 public function write_answers($answers) {
1429 if (empty($answers)) {
1433 foreach ($answers as $answer) {
1434 $output .= $this->write_answer($answer);
1439 public function write_answer($answer, $extra = '') {
1440 $percent = $answer->fraction * 100;
1442 $output .= " <answer fraction=\"$percent\" {$this->format($answer->answerformat)}>\n";
1443 $output .= $this->writetext($answer->answer, 3);
1444 $output .= $this->write_files($answer->answerfiles);
1445 $output .= " <feedback {$this->format($answer->feedbackformat)}>\n";
1446 $output .= $this->writetext($answer->feedback, 4);
1447 $output .= $this->write_files($answer->feedbackfiles);
1448 $output .= " </feedback>\n";
1450 $output .= " </answer>\n";
1455 * Write out the hints.
1456 * @param object $question the question definition data.
1457 * @return string XML to output.
1459 public function write_hints($question) {
1460 if (empty($question->hints)) {
1465 foreach ($question->hints as $hint) {
1466 $output .= $this->write_hint($hint, $question->contextid);
1472 * @param int $format a FORMAT_... constant.
1473 * @return string the attribute to add to an XML tag.
1475 public function format($format) {
1476 return 'format="' . $this->get_format($format) . '"';
1479 public function write_hint($hint, $contextid) {
1480 $fs = get_file_storage();
1481 $files = $fs->get_area_files($contextid, 'question', 'hint', $hint->id);
1484 $output .= " <hint {$this->format($hint->hintformat)}>\n";
1485 $output .= ' ' . $this->writetext($hint->hint);
1487 if (!empty($hint->shownumcorrect)) {
1488 $output .= " <shownumcorrect/>\n";
1490 if (!empty($hint->clearwrong)) {
1491 $output .= " <clearwrong/>\n";
1494 if (!empty($hint->options)) {
1495 $output .= ' <options>' . $this->xml_escape($hint->options) . "</options>\n";
1497 $output .= $this->write_files($files);
1498 $output .= " </hint>\n";
1503 * Output the combined feedback fields.
1504 * @param object $questionoptions the question definition data.
1505 * @param int $questionid the question id.
1506 * @param int $contextid the question context id.
1507 * @return string XML to output.
1509 public function write_combined_feedback($questionoptions, $questionid, $contextid) {
1510 $fs = get_file_storage();
1513 $fields = array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback');
1514 foreach ($fields as $field) {
1515 $formatfield = $field . 'format';
1516 $files = $fs->get_area_files($contextid, 'question', $field, $questionid);
1518 $output .= " <{$field} {$this->format($questionoptions->$formatfield)}>\n";
1519 $output .= ' ' . $this->writetext($questionoptions->$field);
1520 $output .= $this->write_files($files);
1521 $output .= " </{$field}>\n";
1524 if (!empty($questionoptions->shownumcorrect)) {
1525 $output .= " <shownumcorrect/>\n";