MDL-12016:
[moodle-linuxchix.git] / question / format / xml / format.php
blobe8e4edbe8cf825f39a978cdc99f7130e32d9134c
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 $xml = addslashes( trim( $xml ) );
111 return $xml;
116 * import parts of question common to all types
117 * @param array question question array from xml tree
118 * @return object question object
120 function import_headers( $question ) {
121 // get some error strings
122 $error_noname = get_string( 'xmlimportnoname','quiz' );
123 $error_noquestion = get_string( 'xmlimportnoquestion','quiz' );
125 // this routine initialises the question object
126 $qo = $this->defaultquestion();
128 // question name
129 $qo->name = $this->getpath( $question, array('#','name',0,'#','text',0,'#'), '', true, $error_noname );
130 $qo->questiontext = $this->getpath( $question, array('#','questiontext',0,'#','text',0,'#'), '', true );
131 $qo->questiontextformat = $this->getpath( $question, array('#','questiontext',0,'@','format'), '' );
132 $image = $this->getpath( $question, array('#','image',0,'#'), $qo->image );
133 $image_base64 = $this->getpath( $question, array('#','image_base64','0','#'),'' );
134 if (!empty($image_base64)) {
135 $qo->image = $this->importimagefile( $image, stripslashes($image_base64) );
137 $qo->generalfeedback = $this->getpath( $question, array('#','generalfeedback',0,'#','text',0,'#'), $qo->generalfeedback, true );
138 $qo->defaultgrade = $this->getpath( $question, array('#','defaultgrade',0,'#'), $qo->defaultgrade );
139 $qo->penalty = $this->getpath( $question, array('#','penalty',0,'#'), $qo->penalty );
141 return $qo;
145 * import the common parts of a single answer
146 * @param array answer xml tree for single answer
147 * @return object answer object
149 function import_answer( $answer ) {
150 $fraction = $this->getpath( $answer, array('@','fraction'),0 );
151 $text = $this->getpath( $answer, array('#','text',0,'#'), '', true );
152 $feedback = $this->getpath( $answer, array('#','feedback',0,'#','text',0,'#'), '', true );
154 $ans = null;
155 $ans->answer = $text;
156 $ans->fraction = $fraction / 100;
157 $ans->feedback = $feedback;
158 return $ans;
162 * import multiple choice question
163 * @param array question question array from xml tree
164 * @return object question object
166 function import_multichoice( $question ) {
167 // get common parts
168 $qo = $this->import_headers( $question );
170 // 'header' parts particular to multichoice
171 $qo->qtype = MULTICHOICE;
172 $single = $this->getpath( $question, array('#','single',0,'#'), 'true' );
173 $qo->single = $this->trans_single( $single );
174 $shuffleanswers = $this->getpath( $question, array('#','shuffleanswers',0,'#'), 'false' );
175 $qo->answernumbering = $this->getpath( $question, array('#','answernumbering',0,'#'), 'abc' );
176 $qo->shuffleanswers = $this->trans_single($shuffleanswers);
177 $qo->correctfeedback = $this->getpath( $question, array('#','correctfeedback',0,'#','text',0,'#'), '', true );
178 $qo->partiallycorrectfeedback = $this->getpath( $question, array('#','partiallycorrectfeedback',0,'#','text',0,'#'), '', true );
179 $qo->incorrectfeedback = $this->getpath( $question, array('#','incorrectfeedback',0,'#','text',0,'#'), '', true );
181 // run through the answers
182 $answers = $question['#']['answer'];
183 $a_count = 0;
184 foreach ($answers as $answer) {
185 $ans = $this->import_answer( $answer );
186 $qo->answer[$a_count] = $ans->answer;
187 $qo->fraction[$a_count] = $ans->fraction;
188 $qo->feedback[$a_count] = $ans->feedback;
189 ++$a_count;
191 return $qo;
195 * import cloze type question
196 * @param array question question array from xml tree
197 * @return object question object
199 function import_multianswer( $questions ) {
200 $questiontext = $questions['#']['questiontext'][0]['#']['text'];
201 $qo = qtype_multianswer_extract_question($this->import_text($questiontext));
203 // 'header' parts particular to multianswer
204 $qo->qtype = MULTIANSWER;
205 $qo->course = $this->course;
206 $qo->generalfeedback = $this->getpath( $questions, array('#','generalfeedback',0,'#','text',0,'#'), '', true );
208 if (!empty($questions)) {
209 $qo->name = $this->import_text( $questions['#']['name'][0]['#']['text'] );
212 return $qo;
216 * import true/false type question
217 * @param array question question array from xml tree
218 * @return object question object
220 function import_truefalse( $question ) {
221 // get common parts
222 $qo = $this->import_headers( $question );
224 // 'header' parts particular to true/false
225 $qo->qtype = TRUEFALSE;
227 // get answer info
229 // In the past, it used to be assumed that the two answers were in the file
230 // true first, then false. Howevever that was not always true. Now, we
231 // try to match on the answer text, but in old exports, this will be a localised
232 // string, so if we don't find true or false, we fall back to the old system.
233 $first = true;
234 $warning = false;
235 foreach ($question['#']['answer'] as $answer) {
236 $answertext = $this->getpath( $answer, array('#','text',0,'#'), '', true );
237 $feedback = $this->getpath($answer, array('#','feedback',0,'#','text',0,'#'), '', true );
238 if ($answertext != 'true' && $answertext != 'false') {
239 $warning = true;
240 $answertext = $first ? 'true' : 'false'; // Old style file, assume order is true/false.
242 if ($answertext == 'true') {
243 $qo->answer = ($answer['@']['fraction'] == 100);
244 $qo->correctanswer = $qo->answer;
245 $qo->feedbacktrue = $feedback;
246 } else {
247 $qo->answer = ($answer['@']['fraction'] != 100);
248 $qo->correctanswer = $qo->answer;
249 $qo->feedbackfalse = $feedback;
251 $first = false;
254 if ($warning) {
255 $a = new stdClass;
256 $a->questiontext = $qo->questiontext;
257 $a->answer = get_string($qo->answer ? 'true' : 'false', 'quiz');
258 notify(get_string('truefalseimporterror', 'quiz', $a));
260 return $qo;
264 * import short answer type question
265 * @param array question question array from xml tree
266 * @return object question object
268 function import_shortanswer( $question ) {
269 // get common parts
270 $qo = $this->import_headers( $question );
272 // header parts particular to shortanswer
273 $qo->qtype = SHORTANSWER;
275 // get usecase
276 $qo->usecase = $this->getpath($question, array('#','usecase',0,'#'), $qo->usecase );
278 // run through the answers
279 $answers = $question['#']['answer'];
280 $a_count = 0;
281 foreach ($answers as $answer) {
282 $ans = $this->import_answer( $answer );
283 $qo->answer[$a_count] = $ans->answer;
284 $qo->fraction[$a_count] = $ans->fraction;
285 $qo->feedback[$a_count] = $ans->feedback;
286 ++$a_count;
289 return $qo;
293 * import description type question
294 * @param array question question array from xml tree
295 * @return object question object
297 function import_description( $question ) {
298 // get common parts
299 $qo = $this->import_headers( $question );
300 // header parts particular to shortanswer
301 $qo->qtype = DESCRIPTION;
302 return $qo;
306 * import numerical type question
307 * @param array question question array from xml tree
308 * @return object question object
310 function import_numerical( $question ) {
311 // get common parts
312 $qo = $this->import_headers( $question );
314 // header parts particular to numerical
315 $qo->qtype = NUMERICAL;
317 // get answers array
318 $answers = $question['#']['answer'];
319 $qo->answer = array();
320 $qo->feedback = array();
321 $qo->fraction = array();
322 $qo->tolerance = array();
323 foreach ($answers as $answer) {
324 // answer outside of <text> is deprecated
325 $answertext = trim( $this->getpath( $answer, array('#',0), '' ) );
326 $qo->answer[] = $this->getpath( $answer, array('#','text',0,'#'), $answertext, true );
327 if (empty($qo->answer)) {
328 $qo->answer = '*';
330 $qo->feedback[] = $this->getpath( $answer, array('#','feedback',0,'#','text',0,'#'), '', true );
331 $qo->tolerance[] = $this->getpath( $answer, array('#','tolerance',0,'#'), 0 );
333 // fraction as a tag is deprecated
334 $fraction = $this->getpath( $answer, array('@','fraction'), 0 ) / 100;
335 $qo->fraction[] = $this->getpath( $answer, array('#','fraction',0,'#'), $fraction ); // deprecated
338 // get units array
339 $qo->unit = array();
340 $units = $this->getpath( $question, array('#','units',0,'#','unit'), array() );
341 if (!empty($units)) {
342 $qo->multiplier = array();
343 foreach ($units as $unit) {
344 $qo->multiplier[] = $this->getpath( $unit, array('#','multiplier',0,'#'), 1 );
345 $qo->unit[] = $this->getpath( $unit, array('#','unit_name',0,'#'), '', true );
348 return $qo;
352 * import matching type question
353 * @param array question question array from xml tree
354 * @return object question object
356 function import_matching( $question ) {
357 // get common parts
358 $qo = $this->import_headers( $question );
360 // header parts particular to matching
361 $qo->qtype = MATCH;
362 $qo->shuffleanswers = $this->getpath( $question, array( '#','shuffleanswers',0,'#' ), 1 );
364 // get subquestions
365 $subquestions = $question['#']['subquestion'];
366 $qo->subquestions = array();
367 $qo->subanswers = array();
369 // run through subquestions
370 foreach ($subquestions as $subquestion) {
371 $qo->subquestions[] = $this->getpath( $subquestion, array('#','text',0,'#'), '', true );
372 $qo->subanswers[] = $this->getpath( $subquestion, array('#','answer',0,'#','text',0,'#'), '', true);
374 return $qo;
378 * import essay type question
379 * @param array question question array from xml tree
380 * @return object question object
382 function import_essay( $question ) {
383 // get common parts
384 $qo = $this->import_headers( $question );
386 // header parts particular to essay
387 $qo->qtype = ESSAY;
389 // get feedback
390 $qo->feedback = $this->import_text( $question['#']['answer'][0]['#']['feedback'][0]['#']['text'] );
392 // handle answer
393 $answer = $question['#']['answer'][0];
395 // get fraction - <fraction> tag is deprecated
396 $qo->fraction = $this->getpath( $question, array('@','fraction'), 0 ) / 100;
397 $q0->fraction = $this->getpath( $question, array('#','fraction',0,'#'), $qo->fraction );
399 return $qo;
402 function import_calculated( $question ) {
403 // import numerical question
405 // get common parts
406 $qo = $this->import_headers( $question );
408 // header parts particular to numerical
409 $qo->qtype = CALCULATED ;//CALCULATED;
411 // get answers array
412 // echo "<pre> question";print_r($question);echo "</pre>";
413 $answers = $question['#']['answer'];
414 $qo->answers = array();
415 $qo->feedback = array();
416 $qo->fraction = array();
417 $qo->tolerance = array();
418 $qo->tolerancetype = array();
419 $qo->correctanswerformat = array();
420 $qo->correctanswerlength = array();
421 $qo->feedback = array();
422 foreach ($answers as $answer) {
423 // answer outside of <text> is deprecated
424 if (!empty( $answer['#']['text'] )) {
425 $answertext = $this->import_text( $answer['#']['text'] );
427 else {
428 $answertext = trim($answer['#'][0]);
430 if ($answertext == '') {
431 $qo->answers[] = '*';
432 } else {
433 $qo->answers[] = $answertext;
435 $qo->feedback[] = $this->import_text( $answer['#']['feedback'][0]['#']['text'] );
436 $qo->tolerance[] = $answer['#']['tolerance'][0]['#'];
437 // fraction as a tag is deprecated
438 if (!empty($answer['#']['fraction'][0]['#'])) {
439 $qo->fraction[] = $answer['#']['fraction'][0]['#'];
441 else {
442 $qo->fraction[] = $answer['@']['fraction'] / 100;
444 $qo->tolerancetype[] = $answer['#']['tolerancetype'][0]['#'];
445 $qo->correctanswerformat[] = $answer['#']['correctanswerformat'][0]['#'];
446 $qo->correctanswerlength[] = $answer['#']['correctanswerlength'][0]['#'];
448 // get units array
449 $qo->unit = array();
450 if (isset($question['#']['units'][0]['#']['unit'])) {
451 $units = $question['#']['units'][0]['#']['unit'];
452 $qo->multiplier = array();
453 foreach ($units as $unit) {
454 $qo->multiplier[] = $unit['#']['multiplier'][0]['#'];
455 $qo->unit[] = $unit['#']['unit_name'][0]['#'];
458 $datasets = $question['#']['dataset_definitions'][0]['#']['dataset_definition'];
459 $qo->dataset = array();
460 $qo->datasetindex= 0 ;
461 foreach ($datasets as $dataset) {
462 $qo->datasetindex++;
463 $qo->dataset[$qo->datasetindex] = new stdClass();
464 $qo->dataset[$qo->datasetindex]->status = $this->import_text( $dataset['#']['status'][0]['#']['text']);
465 $qo->dataset[$qo->datasetindex]->name = $this->import_text( $dataset['#']['name'][0]['#']['text']);
466 $qo->dataset[$qo->datasetindex]->type = $dataset['#']['type'][0]['#'];
467 $qo->dataset[$qo->datasetindex]->distribution = $this->import_text( $dataset['#']['distribution'][0]['#']['text']);
468 $qo->dataset[$qo->datasetindex]->max = $this->import_text( $dataset['#']['maximum'][0]['#']['text']);
469 $qo->dataset[$qo->datasetindex]->min = $this->import_text( $dataset['#']['minimum'][0]['#']['text']);
470 $qo->dataset[$qo->datasetindex]->length = $this->import_text( $dataset['#']['decimals'][0]['#']['text']);
471 $qo->dataset[$qo->datasetindex]->distribution = $this->import_text( $dataset['#']['distribution'][0]['#']['text']);
472 $qo->dataset[$qo->datasetindex]->itemcount = $dataset['#']['itemcount'][0]['#'];
473 $qo->dataset[$qo->datasetindex]->datasetitem = array();
474 $qo->dataset[$qo->datasetindex]->itemindex = 0;
475 $qo->dataset[$qo->datasetindex]->number_of_items=$dataset['#']['number_of_items'][0]['#'];
476 $datasetitems = $dataset['#']['dataset_items'][0]['#']['dataset_item'];
477 foreach ($datasetitems as $datasetitem) {
478 $qo->dataset[$qo->datasetindex]->itemindex++;
479 $qo->dataset[$qo->datasetindex]->datasetitem[$qo->dataset[$qo->datasetindex]->itemindex] = new stdClass();
480 $qo->dataset[$qo->datasetindex]->datasetitem[$qo->dataset[$qo->datasetindex]->itemindex]->itemnumber = $datasetitem['#']['number'][0]['#']; //[0]['#']['number'][0]['#'] ; // [0]['numberitems'] ;//['#']['number'][0]['#'];// $datasetitems['#']['number'][0]['#'];
481 $qo->dataset[$qo->datasetindex]->datasetitem[$qo->dataset[$qo->datasetindex]->itemindex]->value = $datasetitem['#']['value'][0]['#'] ;//$datasetitem['#']['value'][0]['#'];
485 // echo "<pre>loaded qo";print_r($qo);echo "</pre>";
487 return $qo;
491 * this is not a real question type. It's a dummy type used
492 * to specify the import category
493 * format is:
494 * <question type="category">
495 * <category>tom/dick/harry</category>
496 * </question>
498 function import_category( $question ) {
499 $qo = new stdClass;
500 $qo->qtype = 'category';
501 $qo->category = $this->import_text($question['#']['category'][0]['#']['text']);
502 return $qo;
506 * parse the array of lines into an array of questions
507 * this *could* burn memory - but it won't happen that much
508 * so fingers crossed!
509 * @param array lines array of lines from the input file
510 * @return array (of objects) question objects
512 function readquestions($lines) {
513 // we just need it as one big string
514 $text = implode($lines, " ");
515 unset( $lines );
517 // this converts xml to big nasty data structure
518 // the 0 means keep white space as it is (important for markdown format)
519 // print_r it if you want to see what it looks like!
520 $xml = xmlize( $text, 0 );
522 // set up array to hold all our questions
523 $questions = array();
525 // iterate through questions
526 foreach ($xml['quiz']['#']['question'] as $question) {
527 $question_type = $question['@']['type'];
528 $questiontype = get_string( 'questiontype','quiz',$question_type );
530 if ($question_type=='multichoice') {
531 $qo = $this->import_multichoice( $question );
533 elseif ($question_type=='truefalse') {
534 $qo = $this->import_truefalse( $question );
536 elseif ($question_type=='shortanswer') {
537 $qo = $this->import_shortanswer( $question );
539 elseif ($question_type=='numerical') {
540 $qo = $this->import_numerical( $question );
542 elseif ($question_type=='description') {
543 $qo = $this->import_description( $question );
545 elseif ($question_type=='matching') {
546 $qo = $this->import_matching( $question );
548 elseif ($question_type=='cloze') {
549 $qo = $this->import_multianswer( $question );
551 elseif ($question_type=='essay') {
552 $qo = $this->import_essay( $question );
554 elseif ($question_type=='calculated') {
555 $qo = $this->import_calculated( $question );
557 elseif ($question_type=='category') {
558 $qo = $this->import_category( $question );
560 else {
561 // try for plugin support
562 // no default question, as the plugin can call
563 // import_headers() itself if it wants to
564 if (!$qo=$this->try_importing_using_qtypes( $question )) {
565 $notsupported = get_string( 'xmltypeunsupported','quiz',$question_type );
566 $this->error( $notsupported );
567 $qo = null;
571 // stick the result in the $questions array
572 if ($qo) {
573 $questions[] = $qo;
576 return $questions;
579 // EXPORT FUNCTIONS START HERE
581 function export_file_extension() {
582 // override default type so extension is .xml
584 return ".xml";
589 * Turn the internal question code into a human readable form
590 * (The code used to be numeric, but this remains as some of
591 * the names don't match the new internal format)
592 * @param mixed type_id Internal code
593 * @return string question type string
595 function get_qtype( $type_id ) {
596 switch( $type_id ) {
597 case TRUEFALSE:
598 $name = 'truefalse';
599 break;
600 case MULTICHOICE:
601 $name = 'multichoice';
602 break;
603 case SHORTANSWER:
604 $name = 'shortanswer';
605 break;
606 case NUMERICAL:
607 $name = 'numerical';
608 break;
609 case MATCH:
610 $name = 'matching';
611 break;
612 case DESCRIPTION:
613 $name = 'description';
614 break;
615 case MULTIANSWER:
616 $name = 'cloze';
617 break;
618 case ESSAY:
619 $name = 'essay';
620 break;
621 case CALCULATED:
622 $name = 'calculated';
623 break;
624 default:
625 $name = false;
627 return $name;
631 * Convert internal Moodle text format code into
632 * human readable form
633 * @param int id internal code
634 * @return string format text
636 function get_format( $id ) {
637 switch( $id ) {
638 case 0:
639 $name = "moodle_auto_format";
640 break;
641 case 1:
642 $name = "html";
643 break;
644 case 2:
645 $name = "plain_text";
646 break;
647 case 3:
648 $name = "wiki_like";
649 break;
650 case 4:
651 $name = "markdown";
652 break;
653 default:
654 $name = "unknown";
656 return $name;
660 * Convert internal single question code into
661 * human readable form
662 * @param int id single question code
663 * @return string single question string
665 function get_single( $id ) {
666 switch( $id ) {
667 case 0:
668 $name = "false";
669 break;
670 case 1:
671 $name = "true";
672 break;
673 default:
674 $name = "unknown";
676 return $name;
680 * generates <text></text> tags, processing raw text therein
681 * @param int ilev the current indent level
682 * @param boolean short stick it on one line
683 * @return string formatted text
685 function writetext( $raw, $ilev=0, $short=true) {
686 $indent = str_repeat( " ",$ilev );
688 // encode the text to 'disguise' HTML content
689 $raw = htmlspecialchars( $raw );
691 if ($short) {
692 $xml = "$indent<text>$raw</text>\n";
694 else {
695 $xml = "$indent<text>\n$raw\n$indent</text>\n";
698 return $xml;
701 function xmltidy( $content ) {
702 // can only do this if tidy is installed
703 if (extension_loaded('tidy')) {
704 $config = array( 'input-xml'=>true, 'output-xml'=>true, 'indent'=>true, 'wrap'=>0 );
705 $tidy = new tidy;
706 $tidy->parseString($content, $config, 'utf8');
707 $tidy->cleanRepair();
708 return $tidy->value;
710 else {
711 return $content;
716 function presave_process( $content ) {
717 // override method to allow us to add xml headers and footers
719 // add the xml headers and footers
720 $content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" .
721 "<quiz>\n" .
722 $content . "\n" .
723 "</quiz>";
725 // make the xml look nice
726 $content = $this->xmltidy( $content );
728 return $content;
732 * Include an image encoded in base 64
733 * @param string imagepath The location of the image file
734 * @return string xml code segment
736 function writeimage( $imagepath ) {
737 global $CFG;
739 if (empty($imagepath)) {
740 return '';
743 $courseid = $this->course->id;
744 if (!$binary = file_get_contents( "{$CFG->dataroot}/$courseid/$imagepath" )) {
745 return '';
748 $content = " <image_base64>\n".addslashes(base64_encode( $binary ))."\n".
749 "\n </image_base64>\n";
750 return $content;
754 * Turns question into an xml segment
755 * @param array question question array
756 * @return string xml segment
758 function writequestion( $question ) {
759 global $CFG,$QTYPES;
760 // initial string;
761 $expout = "";
763 // add comment
764 $expout .= "\n\n<!-- question: $question->id -->\n";
766 // check question type
767 if (!$question_type = $this->get_qtype( $question->qtype )) {
768 // must be a plugin then, so just accept the name supplied
769 $question_type = $question->qtype;
772 // add opening tag
773 // generates specific header for Cloze and category type question
774 if ($question->qtype == 'category') {
775 $categorypath = $this->writetext( $question->category );
776 $expout .= " <question type=\"category\">\n";
777 $expout .= " <category>\n";
778 $expout .= " $categorypath\n";
779 $expout .= " </category>\n";
780 $expout .= " </question>\n";
781 return $expout;
783 elseif ($question->qtype != MULTIANSWER) {
784 // for all question types except Close
785 $name_text = $this->writetext( $question->name );
786 $qtformat = $this->get_format($question->questiontextformat);
787 $question_text = $this->writetext( $question->questiontext );
788 $generalfeedback = $this->writetext( $question->generalfeedback );
789 $expout .= " <question type=\"$question_type\">\n";
790 $expout .= " <name>$name_text</name>\n";
791 $expout .= " <questiontext format=\"$qtformat\">\n";
792 $expout .= $question_text;
793 $expout .= " </questiontext>\n";
794 $expout .= " <image>{$question->image}</image>\n";
795 $expout .= $this->writeimage($question->image);
796 $expout .= " <generalfeedback>\n";
797 $expout .= $generalfeedback;
798 $expout .= " </generalfeedback>\n";
799 $expout .= " <defaultgrade>{$question->defaultgrade}</defaultgrade>\n";
800 $expout .= " <penalty>{$question->penalty}</penalty>\n";
801 $expout .= " <hidden>{$question->hidden}</hidden>\n";
803 else {
804 // for Cloze type only
805 $name_text = $this->writetext( $question->name );
806 $question_text = $this->writetext( $question->questiontext );
807 $generalfeedback = $this->writetext( $question->generalfeedback );
808 $expout .= " <question type=\"$question_type\">\n";
809 $expout .= " <name>$name_text</name>\n";
810 $expout .= " <questiontext>\n";
811 $expout .= $question_text;
812 $expout .= " </questiontext>\n";
813 $expout .= " <generalfeedback>\n";
814 $expout .= $generalfeedback;
815 $expout .= " </generalfeedback>\n";
818 if (!empty($question->options->shuffleanswers)) {
819 $expout .= " <shuffleanswers>{$question->options->shuffleanswers}</shuffleanswers>\n";
821 else {
822 $expout .= " <shuffleanswers>0</shuffleanswers>\n";
825 // output depends on question type
826 switch($question->qtype) {
827 case 'category':
828 // not a qtype really - dummy used for category switching
829 break;
830 case TRUEFALSE:
831 foreach ($question->options->answers as $answer) {
832 $fraction_pc = round( $answer->fraction * 100 );
833 if ($answer->id == $question->options->trueanswer) {
834 $answertext = 'true';
835 } else {
836 $answertext = 'false';
838 $expout .= " <answer fraction=\"$fraction_pc\">\n";
839 $expout .= $this->writetext($answertext, 3) . "\n";
840 $expout .= " <feedback>\n";
841 $expout .= $this->writetext( $answer->feedback,4,false );
842 $expout .= " </feedback>\n";
843 $expout .= " </answer>\n";
845 break;
846 case MULTICHOICE:
847 $expout .= " <single>".$this->get_single($question->options->single)."</single>\n";
848 $expout .= " <shuffleanswers>".$this->get_single($question->options->shuffleanswers)."</shuffleanswers>\n";
849 $expout .= " <correctfeedback>".$this->writetext($question->options->correctfeedback, 3)."</correctfeedback>\n";
850 $expout .= " <partiallycorrectfeedback>".$this->writetext($question->options->partiallycorrectfeedback, 3)."</partiallycorrectfeedback>\n";
851 $expout .= " <incorrectfeedback>".$this->writetext($question->options->incorrectfeedback, 3)."</incorrectfeedback>\n";
852 $expout .= " <answernumbering>{$question->options->answernumbering}</answernumbering>\n";
853 foreach($question->options->answers as $answer) {
854 $percent = $answer->fraction * 100;
855 $expout .= " <answer fraction=\"$percent\">\n";
856 $expout .= $this->writetext( $answer->answer,4,false );
857 $expout .= " <feedback>\n";
858 $expout .= $this->writetext( $answer->feedback,5,false );
859 $expout .= " </feedback>\n";
860 $expout .= " </answer>\n";
862 break;
863 case SHORTANSWER:
864 $expout .= " <usecase>{$question->options->usecase}</usecase>\n ";
865 foreach($question->options->answers as $answer) {
866 $percent = 100 * $answer->fraction;
867 $expout .= " <answer fraction=\"$percent\">\n";
868 $expout .= $this->writetext( $answer->answer,3,false );
869 $expout .= " <feedback>\n";
870 $expout .= $this->writetext( $answer->feedback,4,false );
871 $expout .= " </feedback>\n";
872 $expout .= " </answer>\n";
874 break;
875 case NUMERICAL:
876 foreach ($question->options->answers as $answer) {
877 $tolerance = $answer->tolerance;
878 $percent = 100 * $answer->fraction;
879 $expout .= "<answer fraction=\"$percent\">\n";
880 // <text> tags are an added feature, old filed won't have them
881 $expout .= " <text>{$answer->answer}</text>\n";
882 $expout .= " <tolerance>$tolerance</tolerance>\n";
883 $expout .= " <feedback>".$this->writetext( $answer->feedback )."</feedback>\n";
884 // fraction tag is deprecated
885 // $expout .= " <fraction>{$answer->fraction}</fraction>\n";
886 $expout .= "</answer>\n";
889 $units = $question->options->units;
890 if (count($units)) {
891 $expout .= "<units>\n";
892 foreach ($units as $unit) {
893 $expout .= " <unit>\n";
894 $expout .= " <multiplier>{$unit->multiplier}</multiplier>\n";
895 $expout .= " <unit_name>{$unit->unit}</unit_name>\n";
896 $expout .= " </unit>\n";
898 $expout .= "</units>\n";
900 break;
901 case MATCH:
902 foreach($question->options->subquestions as $subquestion) {
903 $expout .= "<subquestion>\n";
904 $expout .= $this->writetext( $subquestion->questiontext );
905 $expout .= "<answer>".$this->writetext( $subquestion->answertext )."</answer>\n";
906 $expout .= "</subquestion>\n";
908 break;
909 case DESCRIPTION:
910 // nothing more to do for this type
911 break;
912 case MULTIANSWER:
913 $a_count=1;
914 foreach($question->options->questions as $question) {
915 $thispattern = addslashes("{#".$a_count."}");
916 $thisreplace = $question->questiontext;
917 $expout=ereg_replace($thispattern, $thisreplace, $expout );
918 $a_count++;
920 break;
921 case ESSAY:
922 foreach ($question->options->answers as $answer) {
923 $percent = 100 * $answer->fraction;
924 $expout .= "<answer fraction=\"$percent\">\n";
925 $expout .= " <feedback>".$this->writetext( $answer->feedback )."</feedback>\n";
926 // fraction tag is deprecated
927 // $expout .= " <fraction>{$answer->fraction}</fraction>\n";
928 $expout .= "</answer>\n";
931 break;
932 case CALCULATED:
933 foreach ($question->options->answers as $answer) {
934 $tolerance = $answer->tolerance;
935 $tolerancetype = $answer->tolerancetype;
936 $correctanswerlength= $answer->correctanswerlength ;
937 $correctanswerformat= $answer->correctanswerformat;
938 $percent = 100 * $answer->fraction;
939 $expout .= "<answer fraction=\"$percent\">\n";
940 // "<text/>" tags are an added feature, old files won't have them
941 $expout .= " <text>{$answer->answer}</text>\n";
942 $expout .= " <tolerance>$tolerance</tolerance>\n";
943 $expout .= " <tolerancetype>$tolerancetype</tolerancetype>\n";
944 $expout .= " <correctanswerformat>$correctanswerformat</correctanswerformat>\n";
945 $expout .= " <correctanswerlength>$correctanswerformat</correctanswerlength>\n";
946 $expout .= " <feedback>".$this->writetext( $answer->feedback )."</feedback>\n";
947 $expout .= "</answer>\n";
949 $units = $question->options->units;
950 if (count($units)) {
951 $expout .= "<units>\n";
952 foreach ($units as $unit) {
953 $expout .= " <unit>\n";
954 $expout .= " <multiplier>{$unit->multiplier}</multiplier>\n";
955 $expout .= " <unit_name>{$unit->unit}</unit_name>\n";
956 $expout .= " </unit>\n";
958 $expout .= "</units>\n";
960 //echo "<pre> question calc";print_r($question);echo "</pre>";
961 //First, we a new function to get all the data itmes in the database
962 // $question_datasetdefs =$QTYPES['calculated']->get_datasets_for_export ($question);
963 // echo "<pre> question defs";print_r($question_datasetdefs);echo "</pre>";
964 //If there are question_datasets
965 if( isset($question->options->datasets)&&count($question->options->datasets)){// there should be
966 $expout .= "<dataset_definitions>\n";
967 foreach ($question->options->datasets as $def) {
968 $expout .= "<dataset_definition>\n";
969 $expout .= " <status>".$this->writetext($def->status)."</status>\n";
970 $expout .= " <name>".$this->writetext($def->name)."</name>\n";
971 $expout .= " <type>calculated</type>\n";
972 $expout .= " <distribution>".$this->writetext($def->distribution)."</distribution>\n";
973 $expout .= " <minimum>".$this->writetext($def->minimum)."</minimum>\n";
974 $expout .= " <maximum>".$this->writetext($def->maximum)."</maximum>\n";
975 $expout .= " <decimals>".$this->writetext($def->decimals)."</decimals>\n";
976 $expout .= " <itemcount>$def->itemcount</itemcount>\n";
977 if ($def->itemcount > 0 ) {
978 $expout .= " <dataset_items>\n";
979 foreach ($def->items as $item ){
980 $expout .= " <dataset_item>\n";
981 $expout .= " <number>".$item->itemnumber."</number>\n";
982 $expout .= " <value>".$item->value."</value>\n";
983 $expout .= " </dataset_item>\n";
985 $expout .= " </dataset_items>\n";
986 $expout .= " <number_of_items>".$def-> number_of_items."</number_of_items>\n";
988 $expout .= "</dataset_definition>\n";
990 $expout .= "</dataset_definitions>\n";
992 break;
993 default:
994 // try support by optional plugin
995 if (!$data = $this->try_exporting_using_qtypes( $question->qtype, $question )) {
996 error( "Unsupported question type $question->qtype" );
998 $expout .= $data;
1001 // close the question tag
1002 $expout .= "</question>\n";
1004 return $expout;