MDL-5482 - Backup and restore problems for match, random and truefalse question types.
[moodle.git] / question / format / xml / format.php
blobaed43b275972761cf6f317e7ac66d6682cb85497
1 <?php // $Id$
2 //
3 ///////////////////////////////////////////////////////////////
4 // XML import/export
5 //
6 //////////////////////////////////////////////////////////////////////////
7 // Based on default.php, included by ../import.php
8 /**
9 * @package questionbank
10 * @subpackage importexport
12 require_once( "$CFG->libdir/xmlize.php" );
14 class qformat_xml extends qformat_default {
16 function provide_import() {
17 return true;
20 function provide_export() {
21 return true;
24 // IMPORT FUNCTIONS START HERE
26 /**
27 * Translate human readable format name
28 * into internal Moodle code number
29 * @param string name format name from xml file
30 * @return int Moodle format code
32 function trans_format( $name ) {
33 $name = trim($name);
35 if ($name=='moodle_auto_format') {
36 $id = 0;
38 elseif ($name=='html') {
39 $id = 1;
41 elseif ($name=='plain_text') {
42 $id = 2;
44 elseif ($name=='wiki_like') {
45 $id = 3;
47 elseif ($name=='markdown') {
48 $id = 4;
50 else {
51 $id = 0; // or maybe warning required
53 return $id;
56 /**
57 * Translate human readable single answer option
58 * to internal code number
59 * @param string name true/false
60 * @return int internal code number
62 function trans_single( $name ) {
63 $name = trim($name);
64 if ($name == "false" || !$name) {
65 return 0;
66 } else {
67 return 1;
71 /**
72 * process text string from xml file
73 * @param array $text bit of xml tree after ['text']
74 * @return string processed text
76 function import_text( $text ) {
77 // quick sanity check
78 if (empty($text)) {
79 return '';
81 $data = $text[0]['#'];
82 $data = html_entity_decode( $data );
83 return addslashes(trim( $data ));
86 /**
87 * Process text from an element in the XML that may or not be there.
88 * @param string $subelement the name of the element which is either present or missing.
89 * @param array $question a bit of xml tree, this method looks for $question['#'][$subelement][0]['#']['text'].
90 * @return string If $subelement is present, return the content of the text tag inside it.
91 * Otherwise returns an empty string.
93 function import_optional_text($subelement, $question) {
94 if (array_key_exists($subelement, $question['#'])) {
95 return $this->import_text($question['#'][$subelement][0]['#']['text']);
96 } else {
97 return '';
102 * return the value of a node, given a path to the node
103 * if it doesn't exist return the default value
104 * @param array xml data to read
105 * @param array path path to node expressed as array
106 * @param mixed default
107 * @param bool istext process as text
108 * @param string error if set value must exist, return false and issue message if not
109 * @return mixed value
111 function getpath( $xml, $path, $default, $istext=false, $error='' ) {
112 foreach ($path as $index) {
113 if (empty($xml[$index])) {
114 if (!empty($error)) {
115 $this->error( $error );
116 return false;
117 } else {
118 return $default;
121 else $xml = $xml[$index];
123 if ($istext) {
124 $xml = addslashes( trim( $xml ) );
127 return $xml;
132 * import parts of question common to all types
133 * @param array question question array from xml tree
134 * @return object question object
136 function import_headers( $question ) {
137 // get some error strings
138 $error_noname = get_string( 'xmlimportnoname','quiz' );
139 $error_noquestion = get_string( 'xmlimportnoquestion','quiz' );
141 // this routine initialises the question object
142 $qo = $this->defaultquestion();
144 // question name
145 $qo->name = $this->getpath( $question, array('#','name',0,'#','text',0,'#'), '', true, $error_noname );
146 $qo->questiontext = $this->getpath( $question, array('#','questiontext',0,'#','text',0,'#'), '', true, $error_noquestion );
147 $qo->questiontextformat = $this->getpath( $question, array('#','questiontext',0,'@','format'), '' );
148 $image = $this->getpath( $question, array('#','image',0,'#'), $qo->image );
149 $image_base64 = $this->getpath( $question, array('#','image_base64','0','#'),'' );
150 if (!empty($image_base64)) {
151 $qo->image = $this->importimagefile( $image, stripslashes(image_base64) );
153 $qo->generalfeedback = $this->getpath( $question, array('#','generalfeedback',0,'#','text',0,'#'), $qo->generalfeedback, true );
154 $qo->defaultgrade = $this->getpath( $question, array('#','defaultgrade',0,'#'), $qo->defaultgrade );
155 $qo->penalty = $this->getpath( $question, array('#','penalty',0,'#'), $qo->penalty );
157 return $qo;
161 * import the common parts of a single answer
162 * @param array answer xml tree for single answer
163 * @return object answer object
165 function import_answer( $answer ) {
166 $fraction = $answer['@']['fraction'];
167 $text = $this->import_text( $answer['#']['text']);
168 $feedback = $this->import_text( $answer['#']['feedback'][0]['#']['text'] );
170 $ans = null;
171 $ans->answer = $text;
172 $ans->fraction = $fraction / 100;
173 $ans->feedback = $feedback;
175 return $ans;
179 * import multiple choice question
180 * @param array question question array from xml tree
181 * @return object question object
183 function import_multichoice( $question ) {
184 // get common parts
185 $qo = $this->import_headers( $question );
187 // 'header' parts particular to multichoice
188 $qo->qtype = MULTICHOICE;
189 $single = $question['#']['single'][0]['#'];
190 $qo->single = $this->trans_single( $single );
191 if (array_key_exists('shuffleanswers', $question['#'])) {
192 $shuffleanswers = $question['#']['shuffleanswers'][0]['#'];
193 } else {
194 $shuffleanswers = 'false';
196 $qo->shuffleanswers = $this->trans_single($shuffleanswers);
197 $qo->correctfeedback = $this->import_optional_text('correctfeedback', $question);
198 $qo->partiallycorrectfeedback = $this->import_optional_text('partiallycorrectfeedback', $question);
199 $qo->incorrectfeedback = $this->import_optional_text('incorrectfeedback', $question);
201 // run through the answers
202 $answers = $question['#']['answer'];
203 $a_count = 0;
204 foreach ($answers as $answer) {
205 $ans = $this->import_answer( $answer );
206 $qo->answer[$a_count] = $ans->answer;
207 $qo->fraction[$a_count] = $ans->fraction;
208 $qo->feedback[$a_count] = $ans->feedback;
209 ++$a_count;
212 return $qo;
216 * import cloze type question
217 * @param array question question array from xml tree
218 * @return object question object
220 function import_multianswer( $questions ) {
221 $questiontext = $questions['#']['questiontext'][0]['#']['text'];
222 $qo = qtype_multianswer_extract_question($this->import_text($questiontext));
224 // 'header' parts particular to multianswer
225 $qo->qtype = MULTIANSWER;
226 $qo->course = $this->course;
227 $qo->generalfeedback = $this->getpath( $questions, array('#','generalfeedback',0,'#','text',0,'#'), '', true );
229 if (!empty($questions)) {
230 $qo->name = $this->import_text( $questions['#']['name'][0]['#']['text'] );
233 return $qo;
237 * import true/false type question
238 * @param array question question array from xml tree
239 * @return object question object
241 function import_truefalse( $question ) {
242 // get common parts
243 $qo = $this->import_headers( $question );
245 // 'header' parts particular to true/false
246 $qo->qtype = TRUEFALSE;
248 // get answer info
250 // In the past, it used to be assumed that the two answers were in the file
251 // true first, then false. Howevever that was not always true. Now, we
252 // try to match on the answer text, but in old exports, this will be a localised
253 // string, so if we don't find true or false, we fall back to the old system.
254 $first = true;
255 $warning = false;
256 foreach ($question['#']['answer'] as $answer) {
257 $answertext = $this->import_text($answer['#']['text']);
258 $feedback = $this->import_text($answer['#']['feedback'][0]['#']['text']);
259 if ($answertext != 'true' && $answertext != 'false') {
260 $warning = true;
261 $answertext = $first ? 'true' : 'false'; // Old style file, assume order is true/false.
263 if ($answertext == 'true') {
264 $qo->answer = ($answer['@']['fraction'] == 100);
265 $qo->correctanswer = $qo->answer;
266 $qo->feedbacktrue = $feedback;
267 } else {
268 $qo->answer = ($answer['@']['fraction'] != 100);
269 $qo->correctanswer = $qo->answer;
270 $qo->feedbackfalse = $feedback;
272 $first = false;
275 if ($warning) {
276 $a = new stdClass;
277 $a->questiontext = $qo->questiontext;
278 $a->answer = get_string($qo->answer ? 'true' : 'false', 'quiz');
279 notify(get_string('truefalseimporterror', 'quiz', $a));
282 return $qo;
286 * import short answer type question
287 * @param array question question array from xml tree
288 * @return object question object
290 function import_shortanswer( $question ) {
291 // get common parts
292 $qo = $this->import_headers( $question );
294 // header parts particular to shortanswer
295 $qo->qtype = SHORTANSWER;
297 // get usecase
298 $qo->usecase = $this->getpath($question, array('#','usecase',0,'#'), $qo->usecase );
300 // run through the answers
301 $answers = $question['#']['answer'];
302 $a_count = 0;
303 foreach ($answers as $answer) {
304 $ans = $this->import_answer( $answer );
305 $qo->answer[$a_count] = $ans->answer;
306 $qo->fraction[$a_count] = $ans->fraction;
307 $qo->feedback[$a_count] = $ans->feedback;
308 ++$a_count;
311 return $qo;
315 * import regexp type question
316 * @param array question question array from xml tree
317 * @return object question object
319 function import_regexp( $question ) {
320 // get common parts
321 $qo = $this->import_headers( $question );
323 // header parts particular to shortanswer
324 $qo->qtype = regexp;
326 // get usecase
327 $qo->usecase = $question['#']['usecase'][0]['#'];
329 // run through the answers
330 $answers = $question['#']['answer'];
331 $a_count = 0;
332 foreach ($answers as $answer) {
333 $ans = $this->import_answer( $answer );
334 $qo->answer[$a_count] = $ans->answer;
335 $qo->fraction[$a_count] = $ans->fraction;
336 $qo->feedback[$a_count] = $ans->feedback;
337 ++$a_count;
340 return $qo;
344 * import description type question
345 * @param array question question array from xml tree
346 * @return object question object
348 function import_description( $question ) {
349 // get common parts
350 $qo = $this->import_headers( $question );
351 // header parts particular to shortanswer
352 $qo->qtype = DESCRIPTION;
353 $qo->defaultgrade = 0;
354 $qo->length = 0;
355 return $qo;
359 * import numerical type question
360 * @param array question question array from xml tree
361 * @return object question object
363 function import_numerical( $question ) {
364 // get common parts
365 $qo = $this->import_headers( $question );
367 // header parts particular to numerical
368 $qo->qtype = NUMERICAL;
370 // get answers array
371 $answers = $question['#']['answer'];
372 $qo->answer = array();
373 $qo->feedback = array();
374 $qo->fraction = array();
375 $qo->tolerance = array();
376 foreach ($answers as $answer) {
377 // answer outside of <text> is deprecated
378 if (!empty( $answer['#']['text'] )) {
379 $answertext = $this->import_text( $answer['#']['text'] );
381 else {
382 $answertext = trim($answer['#'][0]);
384 if ($answertext == '') {
385 $qo->answer[] = '*';
386 } else {
387 $qo->answer[] = $answertext;
389 $qo->feedback[] = $this->import_text( $answer['#']['feedback'][0]['#']['text'] );
390 $qo->tolerance[] = $answer['#']['tolerance'][0]['#'];
392 // fraction as a tag is deprecated
393 if (!empty($answer['#']['fraction'][0]['#'])) {
394 $qo->fraction[] = $answer['#']['fraction'][0]['#'];
396 else {
397 $qo->fraction[] = $answer['@']['fraction'] / 100;
401 // get units array
402 $qo->unit = array();
403 if (isset($question['#']['units'][0]['#']['unit'])) {
404 $units = $question['#']['units'][0]['#']['unit'];
405 $qo->multiplier = array();
406 foreach ($units as $unit) {
407 $qo->multiplier[] = $unit['#']['multiplier'][0]['#'];
408 $qo->unit[] = $unit['#']['unit_name'][0]['#'];
411 return $qo;
415 * import matching type question
416 * @param array question question array from xml tree
417 * @return object question object
419 function import_matching( $question ) {
420 // get common parts
421 $qo = $this->import_headers( $question );
423 // header parts particular to matching
424 $qo->qtype = MATCH;
425 if (!empty($question['#']['shuffleanswers'])) {
426 $qo->shuffleanswers = $question['#']['shuffleanswers'][0]['#'];
427 } else {
428 $qo->shuffleanswers = false;
431 // get subquestions
432 $subquestions = $question['#']['subquestion'];
433 $qo->subquestions = array();
434 $qo->subanswers = array();
436 // run through subquestions
437 foreach ($subquestions as $subquestion) {
438 $qtext = $this->import_text( $subquestion['#']['text'] );
439 $atext = $this->import_text( $subquestion['#']['answer'][0]['#']['text'] );
440 $qo->subquestions[] = $qtext;
441 $qo->subanswers[] = $atext;
443 return $qo;
447 * import essay type question
448 * @param array question question array from xml tree
449 * @return object question object
451 function import_essay( $question ) {
452 // get common parts
453 $qo = $this->import_headers( $question );
455 // header parts particular to essay
456 $qo->qtype = ESSAY;
458 // get feedback
459 $qo->feedback = $this->import_text( $question['#']['answer'][0]['#']['feedback'][0]['#']['text'] );
461 // handle answer
462 $answer = $question['#']['answer'][0];
464 // get fraction - <fraction> tag is deprecated
465 if (!empty($answer['#']['fraction'][0]['#'])) {
466 $qo->fraction = $answer['#']['fraction'][0]['#'];
468 else {
469 $qo->fraction = $answer['@']['fraction'] / 100;
472 return $qo;
475 function import_calculated( $question ) {
476 // import numerical question
478 // get common parts
479 $qo = $this->import_headers( $question );
481 // header parts particular to numerical
482 $qo->qtype = CALCULATED ;//CALCULATED;
484 // get answers array
485 // echo "<pre> question";print_r($question);echo "</pre>";
486 $answers = $question['#']['answer'];
487 $qo->answers = array();
488 $qo->feedback = array();
489 $qo->fraction = array();
490 $qo->tolerance = array();
491 $qo->tolerancetype = array();
492 $qo->correctanswerformat = array();
493 $qo->correctanswerlength = array();
494 $qo->feedback = array();
495 foreach ($answers as $answer) {
496 // answer outside of <text> is deprecated
497 if (!empty( $answer['#']['text'] )) {
498 $answertext = $this->import_text( $answer['#']['text'] );
500 else {
501 $answertext = trim($answer['#'][0]);
503 if ($answertext == '') {
504 $qo->answers[] = '*';
505 } else {
506 $qo->answers[] = $answertext;
508 $qo->feedback[] = $this->import_text( $answer['#']['feedback'][0]['#']['text'] );
509 $qo->tolerance[] = $answer['#']['tolerance'][0]['#'];
510 // fraction as a tag is deprecated
511 if (!empty($answer['#']['fraction'][0]['#'])) {
512 $qo->fraction[] = $answer['#']['fraction'][0]['#'];
514 else {
515 $qo->fraction[] = $answer['@']['fraction'] / 100;
517 $qo->tolerancetype[] = $answer['#']['tolerancetype'][0]['#'];
518 $qo->correctanswerformat[] = $answer['#']['correctanswerformat'][0]['#'];
519 $qo->correctanswerlength[] = $answer['#']['correctanswerlength'][0]['#'];
521 // get units array
522 $qo->unit = array();
523 if (isset($question['#']['units'][0]['#']['unit'])) {
524 $units = $question['#']['units'][0]['#']['unit'];
525 $qo->multiplier = array();
526 foreach ($units as $unit) {
527 $qo->multiplier[] = $unit['#']['multiplier'][0]['#'];
528 $qo->unit[] = $unit['#']['unit_name'][0]['#'];
531 $datasets = $question['#']['dataset_definitions'][0]['#']['dataset_definition'];
532 $qo->dataset = array();
533 $qo->datasetindex= 0 ;
534 foreach ($datasets as $dataset) {
535 $qo->datasetindex++;
536 $qo->dataset[$qo->datasetindex] = new stdClass();
537 $qo->dataset[$qo->datasetindex]->status = $this->import_text( $dataset['#']['status'][0]['#']['text']);
538 $qo->dataset[$qo->datasetindex]->name = $this->import_text( $dataset['#']['name'][0]['#']['text']);
539 $qo->dataset[$qo->datasetindex]->type = $dataset['#']['type'][0]['#'];
540 $qo->dataset[$qo->datasetindex]->distribution = $this->import_text( $dataset['#']['distribution'][0]['#']['text']);
541 $qo->dataset[$qo->datasetindex]->max = $this->import_text( $dataset['#']['maximum'][0]['#']['text']);
542 $qo->dataset[$qo->datasetindex]->min = $this->import_text( $dataset['#']['minimum'][0]['#']['text']);
543 $qo->dataset[$qo->datasetindex]->length = $this->import_text( $dataset['#']['decimals'][0]['#']['text']);
544 $qo->dataset[$qo->datasetindex]->distribution = $this->import_text( $dataset['#']['distribution'][0]['#']['text']);
545 $qo->dataset[$qo->datasetindex]->itemcount = $dataset['#']['itemcount'][0]['#'];
546 $qo->dataset[$qo->datasetindex]->datasetitem = array();
547 $qo->dataset[$qo->datasetindex]->itemindex = 0;
548 $qo->dataset[$qo->datasetindex]->number_of_items=$dataset['#']['number_of_items'][0]['#'];
549 $datasetitems = $dataset['#']['dataset_items'][0]['#']['dataset_item'];
550 foreach ($datasetitems as $datasetitem) {
551 $qo->dataset[$qo->datasetindex]->itemindex++;
552 $qo->dataset[$qo->datasetindex]->datasetitem[$qo->dataset[$qo->datasetindex]->itemindex] = new stdClass();
553 $qo->dataset[$qo->datasetindex]->datasetitem[$qo->dataset[$qo->datasetindex]->itemindex]->itemnumber = $datasetitem['#']['number'][0]['#']; //[0]['#']['number'][0]['#'] ; // [0]['numberitems'] ;//['#']['number'][0]['#'];// $datasetitems['#']['number'][0]['#'];
554 $qo->dataset[$qo->datasetindex]->datasetitem[$qo->dataset[$qo->datasetindex]->itemindex]->value = $datasetitem['#']['value'][0]['#'] ;//$datasetitem['#']['value'][0]['#'];
558 // echo "<pre>loaded qo";print_r($qo);echo "</pre>";
560 return $qo;
564 * this is not a real question type. It's a dummy type used
565 * to specify the import category
566 * format is:
567 * <question type="category">
568 * <category>tom/dick/harry</category>
569 * </question>
571 function import_category( $question ) {
572 $qo = new stdClass;
573 $qo->qtype = 'category';
574 $qo->category = $question['#']['category'][0]['#'];
575 return $qo;
579 * parse the array of lines into an array of questions
580 * this *could* burn memory - but it won't happen that much
581 * so fingers crossed!
582 * @param array lines array of lines from the input file
583 * @return array (of objects) question objects
585 function readquestions($lines) {
586 // we just need it as one big string
587 $text = implode($lines, " ");
588 unset( $lines );
590 // this converts xml to big nasty data structure
591 // the 0 means keep white space as it is (important for markdown format)
592 // print_r it if you want to see what it looks like!
593 $xml = xmlize( $text, 0 );
595 // set up array to hold all our questions
596 $questions = array();
598 // iterate through questions
599 foreach ($xml['quiz']['#']['question'] as $question) {
600 $question_type = $question['@']['type'];
601 $questiontype = get_string( 'questiontype','quiz',$question_type );
603 if ($question_type=='multichoice') {
604 $qo = $this->import_multichoice( $question );
606 elseif ($question_type=='truefalse') {
607 $qo = $this->import_truefalse( $question );
609 elseif ($question_type=='shortanswer') {
610 $qo = $this->import_shortanswer( $question );
612 //elseif ($question_type=='regexp') {
613 // $qo = $this->import_regexp( $question );
615 elseif ($question_type=='numerical') {
616 $qo = $this->import_numerical( $question );
618 elseif ($question_type=='description') {
619 $qo = $this->import_description( $question );
621 elseif ($question_type=='matching') {
622 $qo = $this->import_matching( $question );
624 elseif ($question_type=='cloze') {
625 $qo = $this->import_multianswer( $question );
627 elseif ($question_type=='essay') {
628 $qo = $this->import_essay( $question );
630 elseif ($question_type=='calculated') {
631 $qo = $this->import_calculated( $question );
633 elseif ($question_type=='category') {
634 $qo = $this->import_category( $question );
636 elseif ($question_type=='unknown') {
637 $qo = $this->import_headers( $question );
638 if(isset($qo->questiontext)) {
639 echo "<p>question :$qo->questiontext</p>";
641 $notsupported = get_string( 'xmltypeunsupported','quiz',$question_type );
642 echo "<p>$notsupported</p>";
643 $qo = null;
645 else {
646 $notsupported = get_string( 'xmltypeunsupported','quiz',$question_type );
647 echo "<p>$notsupported</p>";
648 $qo = null;
651 // stick the result in the $questions array
652 if ($qo) {
653 $questions[] = $qo;
656 return $questions;
659 // EXPORT FUNCTIONS START HERE
661 function export_file_extension() {
662 // override default type so extension is .xml
664 return ".xml";
669 * Turn the internal question code into a human readable form
670 * (The code used to be numeric, but this remains as some of
671 * the names don't match the new internal format)
672 * @param mixed type_id Internal code
673 * @return string question type string
675 function get_qtype( $type_id ) {
676 switch( $type_id ) {
677 case TRUEFALSE:
678 $name = 'truefalse';
679 break;
680 case MULTICHOICE:
681 $name = 'multichoice';
682 break;
683 case SHORTANSWER:
684 $name = 'shortanswer';
685 break;
686 //case regexp:
687 // $name = 'regexp';
688 // break;
689 case NUMERICAL:
690 $name = 'numerical';
691 break;
692 case MATCH:
693 $name = 'matching';
694 break;
695 case DESCRIPTION:
696 $name = 'description';
697 break;
698 case MULTIANSWER:
699 $name = 'cloze';
700 break;
701 case ESSAY:
702 $name = 'essay';
703 break;
704 case CALCULATED:
705 $name = 'calculated';
706 break;
707 default:
708 $name = 'unknown';
710 return $name;
714 * Convert internal Moodle text format code into
715 * human readable form
716 * @param int id internal code
717 * @return string format text
719 function get_format( $id ) {
720 switch( $id ) {
721 case 0:
722 $name = "moodle_auto_format";
723 break;
724 case 1:
725 $name = "html";
726 break;
727 case 2:
728 $name = "plain_text";
729 break;
730 case 3:
731 $name = "wiki_like";
732 break;
733 case 4:
734 $name = "markdown";
735 break;
736 default:
737 $name = "unknown";
739 return $name;
743 * Convert internal single question code into
744 * human readable form
745 * @param int id single question code
746 * @return string single question string
748 function get_single( $id ) {
749 switch( $id ) {
750 case 0:
751 $name = "false";
752 break;
753 case 1:
754 $name = "true";
755 break;
756 default:
757 $name = "unknown";
759 return $name;
763 * generates <text></text> tags, processing raw text therein
764 * @param int ilev the current indent level
765 * @param boolean short stick it on one line
766 * @return string formatted text
768 function writetext( $raw, $ilev=0, $short=true) {
769 $indent = str_repeat( " ",$ilev );
771 // encode the text to 'disguise' HTML content
772 $raw = htmlspecialchars( $raw );
774 if ($short) {
775 $xml = "$indent<text>$raw</text>\n";
777 else {
778 $xml = "$indent<text>\n$raw\n$indent</text>\n";
781 return $xml;
784 function presave_process( $content ) {
785 // override method to allow us to add xml headers and footers
787 $content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" .
788 "<quiz>\n" .
789 $content . "\n" .
790 "</quiz>";
792 return $content;
796 * Include an image encoded in base 64
797 * @param string imagepath The location of the image file
798 * @return string xml code segment
800 function writeimage( $imagepath ) {
801 global $CFG;
803 if (empty($imagepath)) {
804 return '';
807 $courseid = $this->course->id;
808 if (!$binary = file_get_contents( "{$CFG->dataroot}/$courseid/$imagepath" )) {
809 return '';
812 $content = " <image_base64>\n".addslashes(base64_encode( $binary ))."\n".
813 "\n </image_base64>\n";
814 return $content;
818 * Turns question into an xml segment
819 * @param array question question array
820 * @return string xml segment
822 function writequestion( $question ) {
823 global $CFG,$QTYPES;
824 // initial string;
825 $expout = "";
827 // add comment
828 $expout .= "\n\n<!-- question: $question->id -->\n";
830 // check question type - make sure valid
831 $question_type = $this->get_qtype( $question->qtype );
832 if ($question_type=='unknown') {
833 $expout .= "<!-- question: $question->name is not a supported type -->\n\n";
836 // add opening tag
837 // generates specific header for Cloze and category type question
838 if ($question->qtype == 'category') {
839 $expout .= " <question type=\"category\">\n";
840 $expout .= " <category>\n";
841 $expout .= " $question->category\n";
842 $expout .= " </category>\n";
843 $expout .= " </question>\n";
844 return $expout;
846 elseif ($question->qtype != MULTIANSWER) {
847 // for all question types except Close
848 $name_text = $this->writetext( $question->name );
849 $qtformat = $this->get_format($question->questiontextformat);
850 $question_text = $this->writetext( $question->questiontext );
851 $generalfeedback = $this->writetext( $question->generalfeedback );
852 $expout .= " <question type=\"$question_type\">\n";
853 $expout .= " <name>$name_text</name>\n";
854 $expout .= " <questiontext format=\"$qtformat\">\n";
855 $expout .= $question_text;
856 $expout .= " </questiontext>\n";
857 $expout .= " <image>{$question->image}</image>\n";
858 $expout .= $this->writeimage($question->image);
859 $expout .= " <generalfeedback>\n";
860 $expout .= $generalfeedback;
861 $expout .= " </generalfeedback>\n";
862 $expout .= " <defaultgrade>{$question->defaultgrade}</defaultgrade>\n";
863 $expout .= " <penalty>{$question->penalty}</penalty>\n";
864 $expout .= " <hidden>{$question->hidden}</hidden>\n";
866 else {
867 // for Cloze type only
868 $name_text = $this->writetext( $question->name );
869 $question_text = $this->writetext( $question->questiontext );
870 $generalfeedback = $this->writetext( $question->generalfeedback );
871 $expout .= " <question type=\"$question_type\">\n";
872 $expout .= " <name>$name_text</name>\n";
873 $expout .= " <questiontext>\n";
874 $expout .= $question_text;
875 $expout .= " </questiontext>\n";
876 $expout .= " <generalfeedback>\n";
877 $expout .= $generalfeedback;
878 $expout .= " </generalfeedback>\n";
881 if (!empty($question->options->shuffleanswers)) {
882 $expout .= " <shuffleanswers>{$question->options->shuffleanswers}</shuffleanswers>\n";
884 else {
885 $expout .= " <shuffleanswers>0</shuffleanswers>\n";
888 // output depends on question type
889 switch($question->qtype) {
890 case 'category':
891 // not a qtype really - dummy used for category switching
892 break;
893 case TRUEFALSE:
894 foreach ($question->options->answers as $answer) {
895 $fraction_pc = round( $answer->fraction * 100 );
896 if ($answer->id == $question->options->trueanswer) {
897 $answertext = 'true';
898 } else {
899 $answertext = 'false';
901 $expout .= " <answer fraction=\"$fraction_pc\">\n";
902 $expout .= $this->writetext($answertext, 3) . "\n";
903 $expout .= " <feedback>\n";
904 $expout .= $this->writetext( $answer->feedback,4,false );
905 $expout .= " </feedback>\n";
906 $expout .= " </answer>\n";
908 break;
909 case MULTICHOICE:
910 $expout .= " <single>".$this->get_single($question->options->single)."</single>\n";
911 $expout .= " <shuffleanswers>".$this->get_single($question->options->shuffleanswers)."</shuffleanswers>\n";
912 $expout .= " <correctfeedback>".$this->writetext($question->options->correctfeedback, 3)."</correctfeedback>\n";
913 $expout .= " <partiallycorrectfeedback>".$this->writetext($question->options->partiallycorrectfeedback, 3)."</partiallycorrectfeedback>\n";
914 $expout .= " <incorrectfeedback>".$this->writetext($question->options->incorrectfeedback, 3)."</incorrectfeedback>\n";
915 foreach($question->options->answers as $answer) {
916 $percent = $answer->fraction * 100;
917 $expout .= " <answer fraction=\"$percent\">\n";
918 $expout .= $this->writetext( $answer->answer,4,false );
919 $expout .= " <feedback>\n";
920 $expout .= $this->writetext( $answer->feedback,5,false );
921 $expout .= " </feedback>\n";
922 $expout .= " </answer>\n";
924 break;
925 case SHORTANSWER:
926 $expout .= " <usecase>{$question->options->usecase}</usecase>\n ";
927 foreach($question->options->answers as $answer) {
928 $percent = 100 * $answer->fraction;
929 $expout .= " <answer fraction=\"$percent\">\n";
930 $expout .= $this->writetext( $answer->answer,3,false );
931 $expout .= " <feedback>\n";
932 $expout .= $this->writetext( $answer->feedback,4,false );
933 $expout .= " </feedback>\n";
934 $expout .= " </answer>\n";
936 break;
937 //case regexp:
938 //$expout .= " <usecase>{$question->options->usecase}</usecase>\n ";
939 // foreach($question->options->answers as $answer) {
940 // $percent = 100 * $answer->fraction;
941 // $expout .= " <answer fraction=\"$percent\">\n";
942 // $expout .= $this->writetext( $answer->answer,3,false );
943 // $expout .= " <feedback>\n";
944 // $expout .= $this->writetext( $answer->feedback,4,false );
945 // $expout .= " </feedback>\n";
946 // $expout .= " </answer>\n";
947 // }
948 // break;
949 case NUMERICAL:
950 foreach ($question->options->answers as $answer) {
951 $tolerance = $answer->tolerance;
952 $percent = 100 * $answer->fraction;
953 $expout .= "<answer fraction=\"$percent\">\n";
954 // <text> tags are an added feature, old filed won't have them
955 $expout .= " <text>{$answer->answer}</text>\n";
956 $expout .= " <tolerance>$tolerance</tolerance>\n";
957 $expout .= " <feedback>".$this->writetext( $answer->feedback )."</feedback>\n";
958 // fraction tag is deprecated
959 // $expout .= " <fraction>{$answer->fraction}</fraction>\n";
960 $expout .= "</answer>\n";
963 $units = $question->options->units;
964 if (count($units)) {
965 $expout .= "<units>\n";
966 foreach ($units as $unit) {
967 $expout .= " <unit>\n";
968 $expout .= " <multiplier>{$unit->multiplier}</multiplier>\n";
969 $expout .= " <unit_name>{$unit->unit}</unit_name>\n";
970 $expout .= " </unit>\n";
972 $expout .= "</units>\n";
974 break;
975 case MATCH:
976 foreach($question->options->subquestions as $subquestion) {
977 $expout .= "<subquestion>\n";
978 $expout .= $this->writetext( $subquestion->questiontext );
979 $expout .= "<answer>".$this->writetext( $subquestion->answertext )."</answer>\n";
980 $expout .= "</subquestion>\n";
982 break;
983 case DESCRIPTION:
984 // nothing more to do for this type
985 break;
986 case MULTIANSWER:
987 $a_count=1;
988 foreach($question->options->questions as $question) {
989 $thispattern = addslashes("{#".$a_count."}");
990 $thisreplace = $question->questiontext;
991 $expout=ereg_replace($thispattern, $thisreplace, $expout );
992 $a_count++;
994 break;
995 case ESSAY:
996 foreach ($question->options->answers as $answer) {
997 $percent = 100 * $answer->fraction;
998 $expout .= "<answer fraction=\"$percent\">\n";
999 $expout .= " <feedback>".$this->writetext( $answer->feedback )."</feedback>\n";
1000 // fraction tag is deprecated
1001 // $expout .= " <fraction>{$answer->fraction}</fraction>\n";
1002 $expout .= "</answer>\n";
1005 break;
1006 case CALCULATED:
1007 foreach ($question->options->answers as $answer) {
1008 $tolerance = $answer->tolerance;
1009 $tolerancetype = $answer->tolerancetype;
1010 $correctanswerlength= $answer->correctanswerlength ;
1011 $correctanswerformat= $answer->correctanswerformat;
1012 $percent = 100 * $answer->fraction;
1013 $expout .= "<answer fraction=\"$percent\">\n";
1014 // "<text/>" tags are an added feature, old files won't have them
1015 $expout .= " <text>{$answer->answer}</text>\n";
1016 $expout .= " <tolerance>$tolerance</tolerance>\n";
1017 $expout .= " <tolerancetype>$tolerancetype</tolerancetype>\n";
1018 $expout .= " <correctanswerformat>$correctanswerformat</correctanswerformat>\n";
1019 $expout .= " <correctanswerlength>$correctanswerformat</correctanswerlength>\n";
1020 $expout .= " <feedback>".$this->writetext( $answer->feedback )."</feedback>\n";
1021 $expout .= "</answer>\n";
1023 $units = $question->options->units;
1024 if (count($units)) {
1025 $expout .= "<units>\n";
1026 foreach ($units as $unit) {
1027 $expout .= " <unit>\n";
1028 $expout .= " <multiplier>{$unit->multiplier}</multiplier>\n";
1029 $expout .= " <unit_name>{$unit->unit}</unit_name>\n";
1030 $expout .= " </unit>\n";
1032 $expout .= "</units>\n";
1034 //echo "<pre> question calc";print_r($question);echo "</pre>";
1035 //First, we a new function to get all the data itmes in the database
1036 // $question_datasetdefs =$QTYPES['calculated']->get_datasets_for_export ($question);
1037 // echo "<pre> question defs";print_r($question_datasetdefs);echo "</pre>";
1038 //If there are question_datasets
1039 if( isset($question->options->datasets)&&count($question->options->datasets)){// there should be
1040 $expout .= "<dataset_definitions>\n";
1041 foreach ($question->options->datasets as $def) {
1042 $expout .= "<dataset_definition>\n";
1043 $expout .= " <status>".$this->writetext($def->status)."</status>\n";
1044 $expout .= " <name>".$this->writetext($def->name)."</name>\n";
1045 $expout .= " <type>calculated</type>\n";
1046 $expout .= " <distribution>".$this->writetext($def->distribution)."</distribution>\n";
1047 $expout .= " <minimum>".$this->writetext($def->minimum)."</minimum>\n";
1048 $expout .= " <maximum>".$this->writetext($def->maximum)."</maximum>\n";
1049 $expout .= " <decimals>".$this->writetext($def->decimals)."</decimals>\n";
1050 $expout .= " <itemcount>$def->itemcount</itemcount>\n";
1051 if ($def->itemcount > 0 ) {
1052 $expout .= " <dataset_items>\n";
1053 foreach ($def->items as $item ){
1054 $expout .= " <dataset_item>\n";
1055 $expout .= " <number>".$item->itemnumber."</number>\n";
1056 $expout .= " <value>".$item->value."</value>\n";
1057 $expout .= " </dataset_item>\n";
1059 $expout .= " </dataset_items>\n";
1060 $expout .= " <number_of_items>".$def-> number_of_items."</number_of_items>\n";
1062 $expout .= "</dataset_definition>\n";
1064 $expout .= "</dataset_definitions>\n";
1066 break;
1067 default:
1068 // should not get here
1069 error( 'Unsupported question type detected in strange circumstances!' );
1072 // close the question tag
1073 $expout .= "</question>\n";
1075 return $expout;