Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / question / format / xml / format.php
blobd0832fa720ff1e94fa540180e35d5f375799b702
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 return addslashes(trim( $data ));
85 /**
86 * return the value of a node, given a path to the node
87 * if it doesn't exist return the default value
88 * @param array xml data to read
89 * @param array path path to node expressed as array
90 * @param mixed default
91 * @param bool istext process as text
92 * @param string error if set value must exist, return false and issue message if not
93 * @return mixed value
95 function getpath( $xml, $path, $default, $istext=false, $error='' ) {
96 foreach ($path as $index) {
97 if (!isset($xml[$index])) {
98 if (!empty($error)) {
99 $this->error( $error );
100 return false;
101 } else {
102 return $default;
105 else $xml = $xml[$index];
107 if ($istext) {
108 if (!is_string($xml)) {
109 $this->error( 'Invalid xml file - string expected (use CDATA?)' );
111 $xml = addslashes( trim( $xml ) );
114 return $xml;
119 * import parts of question common to all types
120 * @param $question array question question array from xml tree
121 * @return object question object
123 function import_headers( $question ) {
124 // get some error strings
125 $error_noname = get_string( 'xmlimportnoname','quiz' );
126 $error_noquestion = get_string( 'xmlimportnoquestion','quiz' );
128 // this routine initialises the question object
129 $qo = $this->defaultquestion();
131 // question name
132 $qo->name = $this->getpath( $question, array('#','name',0,'#','text',0,'#'), '', true, $error_noname );
133 $qo->questiontext = $this->getpath( $question, array('#','questiontext',0,'#','text',0,'#'), '', true );
134 $qo->questiontextformat = $this->getpath( $question, array('#','questiontext',0,'@','format'), '' );
135 $qo->image = $this->getpath( $question, array('#','image',0,'#'), $qo->image );
136 $image_base64 = $this->getpath( $question, array('#','image_base64','0','#'),'' );
137 if (!empty($image_base64)) {
138 $qo->image = $this->importimagefile( $qo->image, stripslashes($image_base64) );
140 $qo->generalfeedback = $this->getpath( $question, array('#','generalfeedback',0,'#','text',0,'#'), $qo->generalfeedback, true );
141 $qo->defaultgrade = $this->getpath( $question, array('#','defaultgrade',0,'#'), $qo->defaultgrade );
142 $qo->penalty = $this->getpath( $question, array('#','penalty',0,'#'), $qo->penalty );
144 return $qo;
148 * import the common parts of a single answer
149 * @param array answer xml tree for single answer
150 * @return object answer object
152 function import_answer( $answer ) {
153 $fraction = $this->getpath( $answer, array('@','fraction'),0 );
154 $text = $this->getpath( $answer, array('#','text',0,'#'), '', true );
155 $feedback = $this->getpath( $answer, array('#','feedback',0,'#','text',0,'#'), '', true );
157 $ans = null;
158 $ans->answer = $text;
159 $ans->fraction = $fraction / 100;
160 $ans->feedback = $feedback;
161 return $ans;
165 * import multiple choice question
166 * @param array question question array from xml tree
167 * @return object question object
169 function import_multichoice( $question ) {
170 // get common parts
171 $qo = $this->import_headers( $question );
173 // 'header' parts particular to multichoice
174 $qo->qtype = MULTICHOICE;
175 $single = $this->getpath( $question, array('#','single',0,'#'), 'true' );
176 $qo->single = $this->trans_single( $single );
177 $shuffleanswers = $this->getpath( $question, array('#','shuffleanswers',0,'#'), 'false' );
178 $qo->answernumbering = $this->getpath( $question, array('#','answernumbering',0,'#'), 'abc' );
179 $qo->shuffleanswers = $this->trans_single($shuffleanswers);
180 $qo->correctfeedback = $this->getpath( $question, array('#','correctfeedback',0,'#','text',0,'#'), '', true );
181 $qo->partiallycorrectfeedback = $this->getpath( $question, array('#','partiallycorrectfeedback',0,'#','text',0,'#'), '', true );
182 $qo->incorrectfeedback = $this->getpath( $question, array('#','incorrectfeedback',0,'#','text',0,'#'), '', true );
184 // There was a time on the 1.8 branch when it could output an empty answernumbering tag, so fix up any found.
185 if (empty($qo->answernumbering)) {
186 $qo->answernumbering = 'abc';
189 // run through the answers
190 $answers = $question['#']['answer'];
191 $a_count = 0;
192 foreach ($answers as $answer) {
193 $ans = $this->import_answer( $answer );
194 $qo->answer[$a_count] = $ans->answer;
195 $qo->fraction[$a_count] = $ans->fraction;
196 $qo->feedback[$a_count] = $ans->feedback;
197 ++$a_count;
199 return $qo;
203 * import cloze type question
204 * @param array question question array from xml tree
205 * @return object question object
207 function import_multianswer( $questions ) {
208 $questiontext = $questions['#']['questiontext'][0]['#']['text'];
209 $qo = qtype_multianswer_extract_question($this->import_text($questiontext));
211 // 'header' parts particular to multianswer
212 $qo->qtype = MULTIANSWER;
213 $qo->course = $this->course;
214 $qo->generalfeedback = $this->getpath( $questions, array('#','generalfeedback',0,'#','text',0,'#'), '', true );
216 if (!empty($questions)) {
217 $qo->name = $this->import_text( $questions['#']['name'][0]['#']['text'] );
220 return $qo;
224 * import true/false type question
225 * @param array question question array from xml tree
226 * @return object question object
228 function import_truefalse( $question ) {
229 // get common parts
230 $qo = $this->import_headers( $question );
232 // 'header' parts particular to true/false
233 $qo->qtype = TRUEFALSE;
235 // get answer info
237 // In the past, it used to be assumed that the two answers were in the file
238 // true first, then false. Howevever that was not always true. Now, we
239 // try to match on the answer text, but in old exports, this will be a localised
240 // string, so if we don't find true or false, we fall back to the old system.
241 $first = true;
242 $warning = false;
243 foreach ($question['#']['answer'] as $answer) {
244 $answertext = $this->getpath( $answer, array('#','text',0,'#'), '', true );
245 $feedback = $this->getpath($answer, array('#','feedback',0,'#','text',0,'#'), '', true );
246 if ($answertext != 'true' && $answertext != 'false') {
247 $warning = true;
248 $answertext = $first ? 'true' : 'false'; // Old style file, assume order is true/false.
250 if ($answertext == 'true') {
251 $qo->answer = ($answer['@']['fraction'] == 100);
252 $qo->correctanswer = $qo->answer;
253 $qo->feedbacktrue = $feedback;
254 } else {
255 $qo->answer = ($answer['@']['fraction'] != 100);
256 $qo->correctanswer = $qo->answer;
257 $qo->feedbackfalse = $feedback;
259 $first = false;
262 if ($warning) {
263 $a = new stdClass;
264 $a->questiontext = $qo->questiontext;
265 $a->answer = get_string($qo->answer ? 'true' : 'false', 'quiz');
266 notify(get_string('truefalseimporterror', 'quiz', $a));
268 return $qo;
272 * import short answer type question
273 * @param array question question array from xml tree
274 * @return object question object
276 function import_shortanswer( $question ) {
277 // get common parts
278 $qo = $this->import_headers( $question );
280 // header parts particular to shortanswer
281 $qo->qtype = SHORTANSWER;
283 // get usecase
284 $qo->usecase = $this->getpath($question, array('#','usecase',0,'#'), $qo->usecase );
286 // run through the answers
287 $answers = $question['#']['answer'];
288 $a_count = 0;
289 foreach ($answers as $answer) {
290 $ans = $this->import_answer( $answer );
291 $qo->answer[$a_count] = $ans->answer;
292 $qo->fraction[$a_count] = $ans->fraction;
293 $qo->feedback[$a_count] = $ans->feedback;
294 ++$a_count;
297 return $qo;
301 * import description type question
302 * @param array question question array from xml tree
303 * @return object question object
305 function import_description( $question ) {
306 // get common parts
307 $qo = $this->import_headers( $question );
308 // header parts particular to shortanswer
309 $qo->qtype = DESCRIPTION;
310 $qo->defaultgrade = 0;
311 $qo->length = 0;
312 return $qo;
316 * import numerical type question
317 * @param array question question array from xml tree
318 * @return object question object
320 function import_numerical( $question ) {
321 // get common parts
322 $qo = $this->import_headers( $question );
324 // header parts particular to numerical
325 $qo->qtype = NUMERICAL;
327 // get answers array
328 $answers = $question['#']['answer'];
329 $qo->answer = array();
330 $qo->feedback = array();
331 $qo->fraction = array();
332 $qo->tolerance = array();
333 foreach ($answers as $answer) {
334 // answer outside of <text> is deprecated
335 $answertext = trim( $this->getpath( $answer, array('#',0), '' ) );
336 $qo->answer[] = $this->getpath( $answer, array('#','text',0,'#'), $answertext, true );
337 if (empty($qo->answer)) {
338 $qo->answer = '*';
340 $qo->feedback[] = $this->getpath( $answer, array('#','feedback',0,'#','text',0,'#'), '', true );
341 $qo->tolerance[] = $this->getpath( $answer, array('#','tolerance',0,'#'), 0 );
343 // fraction as a tag is deprecated
344 $fraction = $this->getpath( $answer, array('@','fraction'), 0 ) / 100;
345 $qo->fraction[] = $this->getpath( $answer, array('#','fraction',0,'#'), $fraction ); // deprecated
348 // get units array
349 $qo->unit = array();
350 $units = $this->getpath( $question, array('#','units',0,'#','unit'), array() );
351 if (!empty($units)) {
352 $qo->multiplier = array();
353 foreach ($units as $unit) {
354 $qo->multiplier[] = $this->getpath( $unit, array('#','multiplier',0,'#'), 1 );
355 $qo->unit[] = $this->getpath( $unit, array('#','unit_name',0,'#'), '', true );
358 return $qo;
362 * import matching type question
363 * @param array question question array from xml tree
364 * @return object question object
366 function import_matching( $question ) {
367 // get common parts
368 $qo = $this->import_headers( $question );
370 // header parts particular to matching
371 $qo->qtype = MATCH;
372 $qo->shuffleanswers = $this->getpath( $question, array( '#','shuffleanswers',0,'#' ), 1 );
374 // get subquestions
375 $subquestions = $question['#']['subquestion'];
376 $qo->subquestions = array();
377 $qo->subanswers = array();
379 // run through subquestions
380 foreach ($subquestions as $subquestion) {
381 $qo->subquestions[] = $this->getpath( $subquestion, array('#','text',0,'#'), '', true );
382 $qo->subanswers[] = $this->getpath( $subquestion, array('#','answer',0,'#','text',0,'#'), '', true);
384 return $qo;
388 * import essay type question
389 * @param array question question array from xml tree
390 * @return object question object
392 function import_essay( $question ) {
393 // get common parts
394 $qo = $this->import_headers( $question );
396 // header parts particular to essay
397 $qo->qtype = ESSAY;
399 // get feedback
400 $qo->feedback = $this->import_text( $question['#']['answer'][0]['#']['feedback'][0]['#']['text'] );
402 // handle answer
403 $answer = $question['#']['answer'][0];
405 // get fraction - <fraction> tag is deprecated
406 $qo->fraction = $this->getpath( $question, array('@','fraction'), 0 ) / 100;
407 $q0->fraction = $this->getpath( $question, array('#','fraction',0,'#'), $qo->fraction );
409 return $qo;
412 function import_calculated( $question ) {
413 // import numerical question
415 // get common parts
416 $qo = $this->import_headers( $question );
418 // header parts particular to numerical
419 $qo->qtype = CALCULATED ;//CALCULATED;
421 // get answers array
422 // echo "<pre> question";print_r($question);echo "</pre>";
423 $answers = $question['#']['answer'];
424 $qo->answers = array();
425 $qo->feedback = array();
426 $qo->fraction = array();
427 $qo->tolerance = array();
428 $qo->tolerancetype = array();
429 $qo->correctanswerformat = array();
430 $qo->correctanswerlength = array();
431 $qo->feedback = array();
432 foreach ($answers as $answer) {
433 // answer outside of <text> is deprecated
434 if (!empty( $answer['#']['text'] )) {
435 $answertext = $this->import_text( $answer['#']['text'] );
437 else {
438 $answertext = trim($answer['#'][0]);
440 if ($answertext == '') {
441 $qo->answers[] = '*';
442 } else {
443 $qo->answers[] = $answertext;
445 $qo->feedback[] = $this->import_text( $answer['#']['feedback'][0]['#']['text'] );
446 $qo->tolerance[] = $answer['#']['tolerance'][0]['#'];
447 // fraction as a tag is deprecated
448 if (!empty($answer['#']['fraction'][0]['#'])) {
449 $qo->fraction[] = $answer['#']['fraction'][0]['#'];
451 else {
452 $qo->fraction[] = $answer['@']['fraction'] / 100;
454 $qo->tolerancetype[] = $answer['#']['tolerancetype'][0]['#'];
455 $qo->correctanswerformat[] = $answer['#']['correctanswerformat'][0]['#'];
456 $qo->correctanswerlength[] = $answer['#']['correctanswerlength'][0]['#'];
458 // get units array
459 $qo->unit = array();
460 if (isset($question['#']['units'][0]['#']['unit'])) {
461 $units = $question['#']['units'][0]['#']['unit'];
462 $qo->multiplier = array();
463 foreach ($units as $unit) {
464 $qo->multiplier[] = $unit['#']['multiplier'][0]['#'];
465 $qo->unit[] = $unit['#']['unit_name'][0]['#'];
468 $datasets = $question['#']['dataset_definitions'][0]['#']['dataset_definition'];
469 $qo->dataset = array();
470 $qo->datasetindex= 0 ;
471 foreach ($datasets as $dataset) {
472 $qo->datasetindex++;
473 $qo->dataset[$qo->datasetindex] = new stdClass();
474 $qo->dataset[$qo->datasetindex]->status = $this->import_text( $dataset['#']['status'][0]['#']['text']);
475 $qo->dataset[$qo->datasetindex]->name = $this->import_text( $dataset['#']['name'][0]['#']['text']);
476 $qo->dataset[$qo->datasetindex]->type = $dataset['#']['type'][0]['#'];
477 $qo->dataset[$qo->datasetindex]->distribution = $this->import_text( $dataset['#']['distribution'][0]['#']['text']);
478 $qo->dataset[$qo->datasetindex]->max = $this->import_text( $dataset['#']['maximum'][0]['#']['text']);
479 $qo->dataset[$qo->datasetindex]->min = $this->import_text( $dataset['#']['minimum'][0]['#']['text']);
480 $qo->dataset[$qo->datasetindex]->length = $this->import_text( $dataset['#']['decimals'][0]['#']['text']);
481 $qo->dataset[$qo->datasetindex]->distribution = $this->import_text( $dataset['#']['distribution'][0]['#']['text']);
482 $qo->dataset[$qo->datasetindex]->itemcount = $dataset['#']['itemcount'][0]['#'];
483 $qo->dataset[$qo->datasetindex]->datasetitem = array();
484 $qo->dataset[$qo->datasetindex]->itemindex = 0;
485 $qo->dataset[$qo->datasetindex]->number_of_items=$dataset['#']['number_of_items'][0]['#'];
486 $datasetitems = $dataset['#']['dataset_items'][0]['#']['dataset_item'];
487 foreach ($datasetitems as $datasetitem) {
488 $qo->dataset[$qo->datasetindex]->itemindex++;
489 $qo->dataset[$qo->datasetindex]->datasetitem[$qo->dataset[$qo->datasetindex]->itemindex] = new stdClass();
490 $qo->dataset[$qo->datasetindex]->datasetitem[$qo->dataset[$qo->datasetindex]->itemindex]->itemnumber = $datasetitem['#']['number'][0]['#']; //[0]['#']['number'][0]['#'] ; // [0]['numberitems'] ;//['#']['number'][0]['#'];// $datasetitems['#']['number'][0]['#'];
491 $qo->dataset[$qo->datasetindex]->datasetitem[$qo->dataset[$qo->datasetindex]->itemindex]->value = $datasetitem['#']['value'][0]['#'] ;//$datasetitem['#']['value'][0]['#'];
495 // echo "<pre>loaded qo";print_r($qo);echo "</pre>";
497 return $qo;
501 * this is not a real question type. It's a dummy type used
502 * to specify the import category
503 * format is:
504 * <question type="category">
505 * <category>tom/dick/harry</category>
506 * </question>
508 function import_category( $question ) {
509 $qo = new stdClass;
510 $qo->qtype = 'category';
511 $qo->category = $this->import_text($question['#']['category'][0]['#']['text']);
512 return $qo;
516 * parse the array of lines into an array of questions
517 * this *could* burn memory - but it won't happen that much
518 * so fingers crossed!
519 * @param array lines array of lines from the input file
520 * @return array (of objects) question objects
522 function readquestions($lines) {
523 // we just need it as one big string
524 $text = implode($lines, " ");
525 unset( $lines );
527 // this converts xml to big nasty data structure
528 // the 0 means keep white space as it is (important for markdown format)
529 // print_r it if you want to see what it looks like!
530 $xml = xmlize( $text, 0 );
532 // set up array to hold all our questions
533 $questions = array();
535 // iterate through questions
536 foreach ($xml['quiz']['#']['question'] as $question) {
537 $question_type = $question['@']['type'];
538 $questiontype = get_string( 'questiontype','quiz',$question_type );
540 if ($question_type=='multichoice') {
541 $qo = $this->import_multichoice( $question );
543 elseif ($question_type=='truefalse') {
544 $qo = $this->import_truefalse( $question );
546 elseif ($question_type=='shortanswer') {
547 $qo = $this->import_shortanswer( $question );
549 elseif ($question_type=='numerical') {
550 $qo = $this->import_numerical( $question );
552 elseif ($question_type=='description') {
553 $qo = $this->import_description( $question );
555 elseif ($question_type=='matching') {
556 $qo = $this->import_matching( $question );
558 elseif ($question_type=='cloze') {
559 $qo = $this->import_multianswer( $question );
561 elseif ($question_type=='essay') {
562 $qo = $this->import_essay( $question );
564 elseif ($question_type=='calculated') {
565 $qo = $this->import_calculated( $question );
567 elseif ($question_type=='category') {
568 $qo = $this->import_category( $question );
570 else {
571 // try for plugin support
572 // no default question, as the plugin can call
573 // import_headers() itself if it wants to
574 if (!$qo=$this->try_importing_using_qtypes( $question )) {
575 $notsupported = get_string( 'xmltypeunsupported','quiz',$question_type );
576 $this->error( $notsupported );
577 $qo = null;
581 // stick the result in the $questions array
582 if ($qo) {
583 $questions[] = $qo;
586 return $questions;
589 // EXPORT FUNCTIONS START HERE
591 function export_file_extension() {
592 // override default type so extension is .xml
594 return ".xml";
599 * Turn the internal question code into a human readable form
600 * (The code used to be numeric, but this remains as some of
601 * the names don't match the new internal format)
602 * @param mixed type_id Internal code
603 * @return string question type string
605 function get_qtype( $type_id ) {
606 switch( $type_id ) {
607 case TRUEFALSE:
608 $name = 'truefalse';
609 break;
610 case MULTICHOICE:
611 $name = 'multichoice';
612 break;
613 case SHORTANSWER:
614 $name = 'shortanswer';
615 break;
616 case NUMERICAL:
617 $name = 'numerical';
618 break;
619 case MATCH:
620 $name = 'matching';
621 break;
622 case DESCRIPTION:
623 $name = 'description';
624 break;
625 case MULTIANSWER:
626 $name = 'cloze';
627 break;
628 case ESSAY:
629 $name = 'essay';
630 break;
631 case CALCULATED:
632 $name = 'calculated';
633 break;
634 default:
635 $name = false;
637 return $name;
641 * Convert internal Moodle text format code into
642 * human readable form
643 * @param int id internal code
644 * @return string format text
646 function get_format( $id ) {
647 switch( $id ) {
648 case 0:
649 $name = "moodle_auto_format";
650 break;
651 case 1:
652 $name = "html";
653 break;
654 case 2:
655 $name = "plain_text";
656 break;
657 case 3:
658 $name = "wiki_like";
659 break;
660 case 4:
661 $name = "markdown";
662 break;
663 default:
664 $name = "unknown";
666 return $name;
670 * Convert internal single question code into
671 * human readable form
672 * @param int id single question code
673 * @return string single question string
675 function get_single( $id ) {
676 switch( $id ) {
677 case 0:
678 $name = "false";
679 break;
680 case 1:
681 $name = "true";
682 break;
683 default:
684 $name = "unknown";
686 return $name;
690 * generates <text></text> tags, processing raw text therein
691 * @param int ilev the current indent level
692 * @param boolean short stick it on one line
693 * @return string formatted text
695 function writetext( $raw, $ilev=0, $short=true) {
696 $indent = str_repeat( " ",$ilev );
698 // encode the text to 'disguise' HTML content
699 $raw = htmlspecialchars( $raw );
701 if ($short) {
702 $xml = "$indent<text>$raw</text>\n";
704 else {
705 $xml = "$indent<text>\n$raw\n$indent</text>\n";
708 return $xml;
711 function xmltidy( $content ) {
712 // can only do this if tidy is installed
713 if (extension_loaded('tidy')) {
714 $config = array( 'input-xml'=>true, 'output-xml'=>true, 'indent'=>true, 'wrap'=>0 );
715 $tidy = new tidy;
716 $tidy->parseString($content, $config, 'utf8');
717 $tidy->cleanRepair();
718 return $tidy->value;
720 else {
721 return $content;
726 function presave_process( $content ) {
727 // override method to allow us to add xml headers and footers
729 // add the xml headers and footers
730 $content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" .
731 "<quiz>\n" .
732 $content . "\n" .
733 "</quiz>";
735 // make the xml look nice
736 $content = $this->xmltidy( $content );
738 return $content;
742 * Include an image encoded in base 64
743 * @param string imagepath The location of the image file
744 * @return string xml code segment
746 function writeimage( $imagepath ) {
747 global $CFG;
749 if (empty($imagepath)) {
750 return '';
753 $courseid = $this->course->id;
754 if (!$binary = file_get_contents( "{$CFG->dataroot}/$courseid/$imagepath" )) {
755 return '';
758 $content = " <image_base64>\n".addslashes(base64_encode( $binary ))."\n".
759 "\n </image_base64>\n";
760 return $content;
764 * Turns question into an xml segment
765 * @param array question question array
766 * @return string xml segment
768 function writequestion( $question ) {
769 global $CFG,$QTYPES;
770 // initial string;
771 $expout = "";
773 // add comment
774 $expout .= "\n\n<!-- question: $question->id -->\n";
776 // check question type
777 if (!$question_type = $this->get_qtype( $question->qtype )) {
778 // must be a plugin then, so just accept the name supplied
779 $question_type = $question->qtype;
782 // add opening tag
783 // generates specific header for Cloze and category type question
784 if ($question->qtype == 'category') {
785 $categorypath = $this->writetext( $question->category );
786 $expout .= " <question type=\"category\">\n";
787 $expout .= " <category>\n";
788 $expout .= " $categorypath\n";
789 $expout .= " </category>\n";
790 $expout .= " </question>\n";
791 return $expout;
793 elseif ($question->qtype != MULTIANSWER) {
794 // for all question types except Close
795 $name_text = $this->writetext( $question->name );
796 $qtformat = $this->get_format($question->questiontextformat);
797 $question_text = $this->writetext( $question->questiontext );
798 $generalfeedback = $this->writetext( $question->generalfeedback );
799 $expout .= " <question type=\"$question_type\">\n";
800 $expout .= " <name>$name_text</name>\n";
801 $expout .= " <questiontext format=\"$qtformat\">\n";
802 $expout .= $question_text;
803 $expout .= " </questiontext>\n";
804 $expout .= " <image>{$question->image}</image>\n";
805 $expout .= $this->writeimage($question->image);
806 $expout .= " <generalfeedback>\n";
807 $expout .= $generalfeedback;
808 $expout .= " </generalfeedback>\n";
809 $expout .= " <defaultgrade>{$question->defaultgrade}</defaultgrade>\n";
810 $expout .= " <penalty>{$question->penalty}</penalty>\n";
811 $expout .= " <hidden>{$question->hidden}</hidden>\n";
813 else {
814 // for Cloze type only
815 $name_text = $this->writetext( $question->name );
816 $question_text = $this->writetext( $question->questiontext );
817 $generalfeedback = $this->writetext( $question->generalfeedback );
818 $expout .= " <question type=\"$question_type\">\n";
819 $expout .= " <name>$name_text</name>\n";
820 $expout .= " <questiontext>\n";
821 $expout .= $question_text;
822 $expout .= " </questiontext>\n";
823 $expout .= " <generalfeedback>\n";
824 $expout .= $generalfeedback;
825 $expout .= " </generalfeedback>\n";
828 if (!empty($question->options->shuffleanswers)) {
829 $expout .= " <shuffleanswers>{$question->options->shuffleanswers}</shuffleanswers>\n";
831 else {
832 $expout .= " <shuffleanswers>0</shuffleanswers>\n";
835 // output depends on question type
836 switch($question->qtype) {
837 case 'category':
838 // not a qtype really - dummy used for category switching
839 break;
840 case TRUEFALSE:
841 foreach ($question->options->answers as $answer) {
842 $fraction_pc = round( $answer->fraction * 100 );
843 if ($answer->id == $question->options->trueanswer) {
844 $answertext = 'true';
845 } else {
846 $answertext = 'false';
848 $expout .= " <answer fraction=\"$fraction_pc\">\n";
849 $expout .= $this->writetext($answertext, 3) . "\n";
850 $expout .= " <feedback>\n";
851 $expout .= $this->writetext( $answer->feedback,4,false );
852 $expout .= " </feedback>\n";
853 $expout .= " </answer>\n";
855 break;
856 case MULTICHOICE:
857 $expout .= " <single>".$this->get_single($question->options->single)."</single>\n";
858 $expout .= " <shuffleanswers>".$this->get_single($question->options->shuffleanswers)."</shuffleanswers>\n";
859 $expout .= " <correctfeedback>".$this->writetext($question->options->correctfeedback, 3)."</correctfeedback>\n";
860 $expout .= " <partiallycorrectfeedback>".$this->writetext($question->options->partiallycorrectfeedback, 3)."</partiallycorrectfeedback>\n";
861 $expout .= " <incorrectfeedback>".$this->writetext($question->options->incorrectfeedback, 3)."</incorrectfeedback>\n";
862 $expout .= " <answernumbering>{$question->options->answernumbering}</answernumbering>\n";
863 foreach($question->options->answers as $answer) {
864 $percent = $answer->fraction * 100;
865 $expout .= " <answer fraction=\"$percent\">\n";
866 $expout .= $this->writetext( $answer->answer,4,false );
867 $expout .= " <feedback>\n";
868 $expout .= $this->writetext( $answer->feedback,5,false );
869 $expout .= " </feedback>\n";
870 $expout .= " </answer>\n";
872 break;
873 case SHORTANSWER:
874 $expout .= " <usecase>{$question->options->usecase}</usecase>\n ";
875 foreach($question->options->answers as $answer) {
876 $percent = 100 * $answer->fraction;
877 $expout .= " <answer fraction=\"$percent\">\n";
878 $expout .= $this->writetext( $answer->answer,3,false );
879 $expout .= " <feedback>\n";
880 $expout .= $this->writetext( $answer->feedback,4,false );
881 $expout .= " </feedback>\n";
882 $expout .= " </answer>\n";
884 break;
885 case NUMERICAL:
886 foreach ($question->options->answers as $answer) {
887 $tolerance = $answer->tolerance;
888 $percent = 100 * $answer->fraction;
889 $expout .= "<answer fraction=\"$percent\">\n";
890 // <text> tags are an added feature, old filed won't have them
891 $expout .= " <text>{$answer->answer}</text>\n";
892 $expout .= " <tolerance>$tolerance</tolerance>\n";
893 $expout .= " <feedback>".$this->writetext( $answer->feedback )."</feedback>\n";
894 // fraction tag is deprecated
895 // $expout .= " <fraction>{$answer->fraction}</fraction>\n";
896 $expout .= "</answer>\n";
899 $units = $question->options->units;
900 if (count($units)) {
901 $expout .= "<units>\n";
902 foreach ($units as $unit) {
903 $expout .= " <unit>\n";
904 $expout .= " <multiplier>{$unit->multiplier}</multiplier>\n";
905 $expout .= " <unit_name>{$unit->unit}</unit_name>\n";
906 $expout .= " </unit>\n";
908 $expout .= "</units>\n";
910 break;
911 case MATCH:
912 foreach($question->options->subquestions as $subquestion) {
913 $expout .= "<subquestion>\n";
914 $expout .= $this->writetext( $subquestion->questiontext );
915 $expout .= "<answer>".$this->writetext( $subquestion->answertext )."</answer>\n";
916 $expout .= "</subquestion>\n";
918 break;
919 case DESCRIPTION:
920 // nothing more to do for this type
921 break;
922 case MULTIANSWER:
923 $a_count=1;
924 foreach($question->options->questions as $question) {
925 $thispattern = addslashes("{#".$a_count."}");
926 $thisreplace = $question->questiontext;
927 $expout=ereg_replace($thispattern, $thisreplace, $expout );
928 $a_count++;
930 break;
931 case ESSAY:
932 foreach ($question->options->answers as $answer) {
933 $percent = 100 * $answer->fraction;
934 $expout .= "<answer fraction=\"$percent\">\n";
935 $expout .= " <feedback>".$this->writetext( $answer->feedback )."</feedback>\n";
936 // fraction tag is deprecated
937 // $expout .= " <fraction>{$answer->fraction}</fraction>\n";
938 $expout .= "</answer>\n";
941 break;
942 case CALCULATED:
943 foreach ($question->options->answers as $answer) {
944 $tolerance = $answer->tolerance;
945 $tolerancetype = $answer->tolerancetype;
946 $correctanswerlength= $answer->correctanswerlength ;
947 $correctanswerformat= $answer->correctanswerformat;
948 $percent = 100 * $answer->fraction;
949 $expout .= "<answer fraction=\"$percent\">\n";
950 // "<text/>" tags are an added feature, old files won't have them
951 $expout .= " <text>{$answer->answer}</text>\n";
952 $expout .= " <tolerance>$tolerance</tolerance>\n";
953 $expout .= " <tolerancetype>$tolerancetype</tolerancetype>\n";
954 $expout .= " <correctanswerformat>$correctanswerformat</correctanswerformat>\n";
955 $expout .= " <correctanswerlength>$correctanswerformat</correctanswerlength>\n";
956 $expout .= " <feedback>".$this->writetext( $answer->feedback )."</feedback>\n";
957 $expout .= "</answer>\n";
959 $units = $question->options->units;
960 if (count($units)) {
961 $expout .= "<units>\n";
962 foreach ($units as $unit) {
963 $expout .= " <unit>\n";
964 $expout .= " <multiplier>{$unit->multiplier}</multiplier>\n";
965 $expout .= " <unit_name>{$unit->unit}</unit_name>\n";
966 $expout .= " </unit>\n";
968 $expout .= "</units>\n";
970 //echo "<pre> question calc";print_r($question);echo "</pre>";
971 //First, we a new function to get all the data itmes in the database
972 // $question_datasetdefs =$QTYPES['calculated']->get_datasets_for_export ($question);
973 // echo "<pre> question defs";print_r($question_datasetdefs);echo "</pre>";
974 //If there are question_datasets
975 if( isset($question->options->datasets)&&count($question->options->datasets)){// there should be
976 $expout .= "<dataset_definitions>\n";
977 foreach ($question->options->datasets as $def) {
978 $expout .= "<dataset_definition>\n";
979 $expout .= " <status>".$this->writetext($def->status)."</status>\n";
980 $expout .= " <name>".$this->writetext($def->name)."</name>\n";
981 $expout .= " <type>calculated</type>\n";
982 $expout .= " <distribution>".$this->writetext($def->distribution)."</distribution>\n";
983 $expout .= " <minimum>".$this->writetext($def->minimum)."</minimum>\n";
984 $expout .= " <maximum>".$this->writetext($def->maximum)."</maximum>\n";
985 $expout .= " <decimals>".$this->writetext($def->decimals)."</decimals>\n";
986 $expout .= " <itemcount>$def->itemcount</itemcount>\n";
987 if ($def->itemcount > 0 ) {
988 $expout .= " <dataset_items>\n";
989 foreach ($def->items as $item ){
990 $expout .= " <dataset_item>\n";
991 $expout .= " <number>".$item->itemnumber."</number>\n";
992 $expout .= " <value>".$item->value."</value>\n";
993 $expout .= " </dataset_item>\n";
995 $expout .= " </dataset_items>\n";
996 $expout .= " <number_of_items>".$def-> number_of_items."</number_of_items>\n";
998 $expout .= "</dataset_definition>\n";
1000 $expout .= "</dataset_definitions>\n";
1002 break;
1003 default:
1004 // try support by optional plugin
1005 if (!$data = $this->try_exporting_using_qtypes( $question->qtype, $question )) {
1006 error( "Unsupported question type $question->qtype" );
1008 $expout .= $data;
1011 // close the question tag
1012 $expout .= "</question>\n";
1014 return $expout;