adding some strings
[moodle-linuxchix.git] / question / format / xml / format.php
blob6adbfecffa751971bdc9204abe573670f16b41cc
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 (empty($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;
207 if (!empty($questions)) {
208 $qo->name = $this->import_text( $questions['#']['name'][0]['#']['text'] );
211 return $qo;
215 * import true/false type question
216 * @param array question question array from xml tree
217 * @return object question object
219 function import_truefalse( $question ) {
220 // get common parts
221 $qo = $this->import_headers( $question );
223 // 'header' parts particular to true/false
224 $qo->qtype = TRUEFALSE;
226 // get answer info
228 // In the past, it used to be assumed that the two answers were in the file
229 // true first, then false. Howevever that was not always true. Now, we
230 // try to match on the answer text, but in old exports, this will be a localised
231 // string, so if we don't find true or false, we fall back to the old system.
232 $first = true;
233 $warning = false;
234 foreach ($question['#']['answer'] as $answer) {
235 $answertext = $this->getpath( $answer, array('#','text',0,'#'), '', true );
236 $feedback = $this->getpath($answer, array('#','feedback',0,'#','text',0,'#'), '', true );
237 if ($answertext != 'true' && $answertext != 'false') {
238 $warning = true;
239 $answertext = $first ? 'true' : 'false'; // Old style file, assume order is true/false.
241 if ($answertext == 'true') {
242 $qo->answer = ($answer['@']['fraction'] == 100);
243 $qo->correctanswer = $qo->answer;
244 $qo->feedbacktrue = $feedback;
245 } else {
246 $qo->answer = ($answer['@']['fraction'] != 100);
247 $qo->correctanswer = $qo->answer;
248 $qo->feedbackfalse = $feedback;
250 $first = false;
253 if ($warning) {
254 $a = new stdClass;
255 $a->questiontext = $qo->questiontext;
256 $a->answer = get_string($qo->answer ? 'true' : 'false', 'quiz');
257 notify(get_string('truefalseimporterror', 'quiz', $a));
259 return $qo;
263 * import short answer type question
264 * @param array question question array from xml tree
265 * @return object question object
267 function import_shortanswer( $question ) {
268 // get common parts
269 $qo = $this->import_headers( $question );
271 // header parts particular to shortanswer
272 $qo->qtype = SHORTANSWER;
274 // get usecase
275 $qo->usecase = $this->getpath($question, array('#','usecase',0,'#'), $qo->usecase );
277 // run through the answers
278 $answers = $question['#']['answer'];
279 $a_count = 0;
280 foreach ($answers as $answer) {
281 $ans = $this->import_answer( $answer );
282 $qo->answer[$a_count] = $ans->answer;
283 $qo->fraction[$a_count] = $ans->fraction;
284 $qo->feedback[$a_count] = $ans->feedback;
285 ++$a_count;
288 return $qo;
292 * import regexp type question
293 * @param array question question array from xml tree
294 * @return object question object
296 function import_regexp( $question ) {
297 // get common parts
298 $qo = $this->import_headers( $question );
300 // header parts particular to shortanswer
301 $qo->qtype = regexp;
303 // get usecase
304 $qo->usecase = $question['#']['usecase'][0]['#'];
306 // run through the answers
307 $answers = $question['#']['answer'];
308 $a_count = 0;
309 foreach ($answers as $answer) {
310 $ans = $this->import_answer( $answer );
311 $qo->answer[$a_count] = $ans->answer;
312 $qo->fraction[$a_count] = $ans->fraction;
313 $qo->feedback[$a_count] = $ans->feedback;
314 ++$a_count;
317 return $qo;
321 * import description type question
322 * @param array question question array from xml tree
323 * @return object question object
325 function import_description( $question ) {
326 // get common parts
327 $qo = $this->import_headers( $question );
328 // header parts particular to shortanswer
329 $qo->qtype = DESCRIPTION;
330 return $qo;
334 * import numerical type question
335 * @param array question question array from xml tree
336 * @return object question object
338 function import_numerical( $question ) {
339 // get common parts
340 $qo = $this->import_headers( $question );
342 // header parts particular to numerical
343 $qo->qtype = NUMERICAL;
345 // get answers array
346 $answers = $question['#']['answer'];
347 $qo->answer = array();
348 $qo->feedback = array();
349 $qo->fraction = array();
350 $qo->tolerance = array();
351 foreach ($answers as $answer) {
352 // answer outside of <text> is deprecated
353 $answertext = trim( $this->getpath( $answer, array('#',0), '' ) );
354 $qo->answer[] = $this->getpath( $answer, array('#','text',0,'#'), $answertext, true );
355 if (empty($qo->answer)) {
356 $qo->answer = '*';
358 $qo->feedback[] = $this->getpath( $answer, array('#','feedback',0,'#','text',0,'#'), '', true );
359 $qo->tolerance[] = $this->getpath( $answer, array('#','tolerance',0,'#'), 0 );
361 // fraction as a tag is deprecated
362 $fraction = $this->getpath( $answer, array('@','fraction'), 0 ) / 100;
363 $qo->fraction[] = $this->getpath( $answer, array('#','fraction',0,'#'), $fraction ); // deprecated
366 // get units array
367 $qo->unit = array();
368 $units = $this->getpath( $question, array('#','units',0,'#','unit'), array() );
369 if (!empty($units)) {
370 $qo->multiplier = array();
371 foreach ($units as $unit) {
372 $qo->multiplier[] = $this->getpath( $unit, array('#','multiplier',0,'#'), 1 );
373 $qo->unit[] = $this->getpath( $unit, array('#','unit_name',0,'#'), '', true );
376 return $qo;
380 * import matching type question
381 * @param array question question array from xml tree
382 * @return object question object
384 function import_matching( $question ) {
385 // get common parts
386 $qo = $this->import_headers( $question );
388 // header parts particular to matching
389 $qo->qtype = MATCH;
390 $qo->shuffleanswers = $this->getpath( $question, array( '#','shuffleanswers',0,'#' ), 1 );
392 // get subquestions
393 $subquestions = $question['#']['subquestion'];
394 $qo->subquestions = array();
395 $qo->subanswers = array();
397 // run through subquestions
398 foreach ($subquestions as $subquestion) {
399 $qo->subquestions[] = $this->getpath( $subquestion, array('#','text',0,'#'), '', true );
400 $qo->subanswers[] = $this->getpath( $subquestion, array('#','answer',0,'#','text',0,'#'), '', true);
402 return $qo;
406 * import essay type question
407 * @param array question question array from xml tree
408 * @return object question object
410 function import_essay( $question ) {
411 // get common parts
412 $qo = $this->import_headers( $question );
414 // header parts particular to essay
415 $qo->qtype = ESSAY;
417 // get feedback
418 $qo->feedback = $this->import_text( $question['#']['answer'][0]['#']['feedback'][0]['#']['text'] );
420 // handle answer
421 $answer = $question['#']['answer'][0];
423 // get fraction - <fraction> tag is deprecated
424 $qo->fraction = $this->getpath( $question, array('@','fraction'), 0 ) / 100;
425 $q0->fraction = $this->getpath( $question, array('#','fraction',0,'#'), $qo->fraction );
427 return $qo;
430 function import_calculated( $question ) {
431 // import numerical question
433 // get common parts
434 $qo = $this->import_headers( $question );
436 // header parts particular to numerical
437 $qo->qtype = CALCULATED ;//CALCULATED;
439 // get answers array
440 // echo "<pre> question";print_r($question);echo "</pre>";
441 $answers = $question['#']['answer'];
442 $qo->answers = array();
443 $qo->feedback = array();
444 $qo->fraction = array();
445 $qo->tolerance = array();
446 $qo->tolerancetype = array();
447 $qo->correctanswerformat = array();
448 $qo->correctanswerlength = array();
449 $qo->feedback = array();
450 foreach ($answers as $answer) {
451 // answer outside of <text> is deprecated
452 if (!empty( $answer['#']['text'] )) {
453 $answertext = $this->import_text( $answer['#']['text'] );
455 else {
456 $answertext = trim($answer['#'][0]);
458 if ($answertext == '') {
459 $qo->answers[] = '*';
460 } else {
461 $qo->answers[] = $answertext;
463 $qo->feedback[] = $this->import_text( $answer['#']['feedback'][0]['#']['text'] );
464 $qo->tolerance[] = $answer['#']['tolerance'][0]['#'];
465 // fraction as a tag is deprecated
466 if (!empty($answer['#']['fraction'][0]['#'])) {
467 $qo->fraction[] = $answer['#']['fraction'][0]['#'];
469 else {
470 $qo->fraction[] = $answer['@']['fraction'] / 100;
472 $qo->tolerancetype[] = $answer['#']['tolerancetype'][0]['#'];
473 $qo->correctanswerformat[] = $answer['#']['correctanswerformat'][0]['#'];
474 $qo->correctanswerlength[] = $answer['#']['correctanswerlength'][0]['#'];
476 // get units array
477 $qo->unit = array();
478 if (isset($question['#']['units'][0]['#']['unit'])) {
479 $units = $question['#']['units'][0]['#']['unit'];
480 $qo->multiplier = array();
481 foreach ($units as $unit) {
482 $qo->multiplier[] = $unit['#']['multiplier'][0]['#'];
483 $qo->unit[] = $unit['#']['unit_name'][0]['#'];
486 $datasets = $question['#']['dataset_definitions'][0]['#']['dataset_definition'];
487 $qo->dataset = array();
488 $qo->datasetindex= 0 ;
489 foreach ($datasets as $dataset) {
490 $qo->datasetindex++;
491 $qo->dataset[$qo->datasetindex] = new stdClass();
492 $qo->dataset[$qo->datasetindex]->status = $this->import_text( $dataset['#']['status'][0]['#']['text']);
493 $qo->dataset[$qo->datasetindex]->name = $this->import_text( $dataset['#']['name'][0]['#']['text']);
494 $qo->dataset[$qo->datasetindex]->type = $dataset['#']['type'][0]['#'];
495 $qo->dataset[$qo->datasetindex]->distribution = $this->import_text( $dataset['#']['distribution'][0]['#']['text']);
496 $qo->dataset[$qo->datasetindex]->max = $this->import_text( $dataset['#']['maximum'][0]['#']['text']);
497 $qo->dataset[$qo->datasetindex]->min = $this->import_text( $dataset['#']['minimum'][0]['#']['text']);
498 $qo->dataset[$qo->datasetindex]->length = $this->import_text( $dataset['#']['decimals'][0]['#']['text']);
499 $qo->dataset[$qo->datasetindex]->distribution = $this->import_text( $dataset['#']['distribution'][0]['#']['text']);
500 $qo->dataset[$qo->datasetindex]->itemcount = $dataset['#']['itemcount'][0]['#'];
501 $qo->dataset[$qo->datasetindex]->datasetitem = array();
502 $qo->dataset[$qo->datasetindex]->itemindex = 0;
503 $qo->dataset[$qo->datasetindex]->number_of_items=$dataset['#']['number_of_items'][0]['#'];
504 $datasetitems = $dataset['#']['dataset_items'][0]['#']['dataset_item'];
505 foreach ($datasetitems as $datasetitem) {
506 $qo->dataset[$qo->datasetindex]->itemindex++;
507 $qo->dataset[$qo->datasetindex]->datasetitem[$qo->dataset[$qo->datasetindex]->itemindex] = new stdClass();
508 $qo->dataset[$qo->datasetindex]->datasetitem[$qo->dataset[$qo->datasetindex]->itemindex]->itemnumber = $datasetitem['#']['number'][0]['#']; //[0]['#']['number'][0]['#'] ; // [0]['numberitems'] ;//['#']['number'][0]['#'];// $datasetitems['#']['number'][0]['#'];
509 $qo->dataset[$qo->datasetindex]->datasetitem[$qo->dataset[$qo->datasetindex]->itemindex]->value = $datasetitem['#']['value'][0]['#'] ;//$datasetitem['#']['value'][0]['#'];
513 // echo "<pre>loaded qo";print_r($qo);echo "</pre>";
515 return $qo;
519 * this is not a real question type. It's a dummy type used
520 * to specify the import category
521 * format is:
522 * <question type="category">
523 * <category>tom/dick/harry</category>
524 * </question>
526 function import_category( $question ) {
527 $qo = new stdClass;
528 $qo->qtype = 'category';
529 $qo->category = $question['#']['category'][0]['#'];
530 return $qo;
534 * parse the array of lines into an array of questions
535 * this *could* burn memory - but it won't happen that much
536 * so fingers crossed!
537 * @param array lines array of lines from the input file
538 * @return array (of objects) question objects
540 function readquestions($lines) {
541 // we just need it as one big string
542 $text = implode($lines, " ");
543 unset( $lines );
545 // this converts xml to big nasty data structure
546 // the 0 means keep white space as it is (important for markdown format)
547 // print_r it if you want to see what it looks like!
548 $xml = xmlize( $text, 0 );
550 // set up array to hold all our questions
551 $questions = array();
553 // iterate through questions
554 foreach ($xml['quiz']['#']['question'] as $question) {
555 $question_type = $question['@']['type'];
556 $questiontype = get_string( 'questiontype','quiz',$question_type );
558 if ($question_type=='multichoice') {
559 $qo = $this->import_multichoice( $question );
561 elseif ($question_type=='truefalse') {
562 $qo = $this->import_truefalse( $question );
564 elseif ($question_type=='shortanswer') {
565 $qo = $this->import_shortanswer( $question );
567 //elseif ($question_type=='regexp') {
568 // $qo = $this->import_regexp( $question );
570 elseif ($question_type=='numerical') {
571 $qo = $this->import_numerical( $question );
573 elseif ($question_type=='description') {
574 $qo = $this->import_description( $question );
576 elseif ($question_type=='matching') {
577 $qo = $this->import_matching( $question );
579 elseif ($question_type=='cloze') {
580 $qo = $this->import_multianswer( $question );
582 elseif ($question_type=='essay') {
583 $qo = $this->import_essay( $question );
585 elseif ($question_type=='calculated') {
586 $qo = $this->import_calculated( $question );
588 elseif ($question_type=='category') {
589 $qo = $this->import_category( $question );
591 else {
592 $notsupported = get_string( 'xmltypeunsupported','quiz',$question_type );
593 $this->error( $notsupported );
594 $qo = null;
597 // stick the result in the $questions array
598 if ($qo) {
599 $questions[] = $qo;
602 return $questions;
605 // EXPORT FUNCTIONS START HERE
607 function export_file_extension() {
608 // override default type so extension is .xml
610 return ".xml";
615 * Turn the internal question code into a human readable form
616 * (The code used to be numeric, but this remains as some of
617 * the names don't match the new internal format)
618 * @param mixed type_id Internal code
619 * @return string question type string
621 function get_qtype( $type_id ) {
622 switch( $type_id ) {
623 case TRUEFALSE:
624 $name = 'truefalse';
625 break;
626 case MULTICHOICE:
627 $name = 'multichoice';
628 break;
629 case SHORTANSWER:
630 $name = 'shortanswer';
631 break;
632 //case regexp:
633 // $name = 'regexp';
634 // break;
635 case NUMERICAL:
636 $name = 'numerical';
637 break;
638 case MATCH:
639 $name = 'matching';
640 break;
641 case DESCRIPTION:
642 $name = 'description';
643 break;
644 case MULTIANSWER:
645 $name = 'cloze';
646 break;
647 case ESSAY:
648 $name = 'essay';
649 break;
650 case CALCULATED:
651 $name = 'calculated';
652 break;
653 default:
654 $name = 'unknown';
656 return $name;
660 * Convert internal Moodle text format code into
661 * human readable form
662 * @param int id internal code
663 * @return string format text
665 function get_format( $id ) {
666 switch( $id ) {
667 case 0:
668 $name = "moodle_auto_format";
669 break;
670 case 1:
671 $name = "html";
672 break;
673 case 2:
674 $name = "plain_text";
675 break;
676 case 3:
677 $name = "wiki_like";
678 break;
679 case 4:
680 $name = "markdown";
681 break;
682 default:
683 $name = "unknown";
685 return $name;
689 * Convert internal single question code into
690 * human readable form
691 * @param int id single question code
692 * @return string single question string
694 function get_single( $id ) {
695 switch( $id ) {
696 case 0:
697 $name = "false";
698 break;
699 case 1:
700 $name = "true";
701 break;
702 default:
703 $name = "unknown";
705 return $name;
709 * generates <text></text> tags, processing raw text therein
710 * @param int ilev the current indent level
711 * @param boolean short stick it on one line
712 * @return string formatted text
714 function writetext( $raw, $ilev=0, $short=true) {
715 $indent = str_repeat( " ",$ilev );
717 // encode the text to 'disguise' HTML content
718 $raw = htmlspecialchars( $raw );
720 if ($short) {
721 $xml = "$indent<text>$raw</text>\n";
723 else {
724 $xml = "$indent<text>\n$raw\n$indent</text>\n";
727 return $xml;
730 function xmltidy( $content ) {
731 // can only do this if tidy is installed
732 if (extension_loaded('tidy')) {
733 $config = array( 'input-xml'=>true, 'output-xml'=>true, 'indent'=>true, 'wrap'=>0 );
734 $tidy = new tidy;
735 $tidy->parseString($content, $config, 'utf8');
736 $tidy->cleanRepair();
737 return $tidy->value;
739 else {
740 return $content;
745 function presave_process( $content ) {
746 // override method to allow us to add xml headers and footers
748 // add the xml headers and footers
749 $content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" .
750 "<quiz>\n" .
751 $content . "\n" .
752 "</quiz>";
754 // make the xml look nice
755 $content = $this->xmltidy( $content );
757 return $content;
761 * Include an image encoded in base 64
762 * @param string imagepath The location of the image file
763 * @return string xml code segment
765 function writeimage( $imagepath ) {
766 global $CFG;
768 if (empty($imagepath)) {
769 return '';
772 $courseid = $this->course->id;
773 if (!$binary = file_get_contents( "{$CFG->dataroot}/$courseid/$imagepath" )) {
774 return '';
777 $content = " <image_base64>\n".addslashes(base64_encode( $binary ))."\n".
778 "\n </image_base64>\n";
779 return $content;
783 * Turns question into an xml segment
784 * @param array question question array
785 * @return string xml segment
787 function writequestion( $question ) {
788 global $CFG,$QTYPES;
789 // initial string;
790 $expout = "";
792 // add comment
793 $expout .= "\n\n<!-- question: $question->id -->\n";
795 // check question type - make sure valid
796 $question_type = $this->get_qtype( $question->qtype );
797 if ($question_type=='unknown') {
798 $expout .= "<!-- question: $question->name is not a supported type -->\n\n";
801 // add opening tag
802 // generates specific header for Cloze and category type question
803 if ($question->qtype == 'category') {
804 $expout .= " <question type=\"category\">\n";
805 $expout .= " <category>\n";
806 $expout .= " $question->category\n";
807 $expout .= " </category>\n";
808 $expout .= " </question>\n";
809 return $expout;
811 elseif ($question->qtype != MULTIANSWER) {
812 // for all question types except Close
813 $name_text = $this->writetext( $question->name );
814 $qtformat = $this->get_format($question->questiontextformat);
815 $question_text = $this->writetext( $question->questiontext );
816 $generalfeedback = $this->writetext( $question->generalfeedback );
817 $expout .= " <question type=\"$question_type\">\n";
818 $expout .= " <name>$name_text</name>\n";
819 $expout .= " <questiontext format=\"$qtformat\">\n";
820 $expout .= $question_text;
821 $expout .= " </questiontext>\n";
822 $expout .= " <image>{$question->image}</image>\n";
823 $expout .= $this->writeimage($question->image);
824 $expout .= " <generalfeedback>\n";
825 $expout .= $generalfeedback;
826 $expout .= " </generalfeedback>\n";
827 $expout .= " <defaultgrade>{$question->defaultgrade}</defaultgrade>\n";
828 $expout .= " <penalty>{$question->penalty}</penalty>\n";
829 $expout .= " <hidden>{$question->hidden}</hidden>\n";
831 else {
832 // for Cloze type only
833 $name_text = $this->writetext( $question->name );
834 $question_text = $this->writetext( $question->questiontext );
835 $expout .= " <question type=\"$question_type\">\n";
836 $expout .= " <name>$name_text</name>\n";
837 $expout .= " <questiontext>\n";
838 $expout .= $question_text;
839 $expout .= " </questiontext>\n";
842 if (!empty($question->options->shuffleanswers)) {
843 $expout .= " <shuffleanswers>{$question->options->shuffleanswers}</shuffleanswers>\n";
845 else {
846 $expout .= " <shuffleanswers>0</shuffleanswers>\n";
849 // output depends on question type
850 switch($question->qtype) {
851 case 'category':
852 // not a qtype really - dummy used for category switching
853 break;
854 case TRUEFALSE:
855 foreach ($question->options->answers as $answer) {
856 $fraction_pc = round( $answer->fraction * 100 );
857 if ($answer->id == $question->options->trueanswer) {
858 $answertext = 'true';
859 } else {
860 $answertext = 'false';
862 $expout .= " <answer fraction=\"$fraction_pc\">\n";
863 $expout .= $this->writetext($answertext, 3) . "\n";
864 $expout .= " <feedback>\n";
865 $expout .= $this->writetext( $answer->feedback,4,false );
866 $expout .= " </feedback>\n";
867 $expout .= " </answer>\n";
869 break;
870 case MULTICHOICE:
871 $expout .= " <single>".$this->get_single($question->options->single)."</single>\n";
872 $expout .= " <shuffleanswers>".$this->get_single($question->options->shuffleanswers)."</shuffleanswers>\n";
873 $expout .= " <correctfeedback>".$this->writetext($question->options->correctfeedback, 3)."</correctfeedback>\n";
874 $expout .= " <partiallycorrectfeedback>".$this->writetext($question->options->partiallycorrectfeedback, 3)."</partiallycorrectfeedback>\n";
875 $expout .= " <incorrectfeedback>".$this->writetext($question->options->incorrectfeedback, 3)."</incorrectfeedback>\n";
876 $expout .= " <answernumbering>{$question->options->answernumbering}</answernumbering>\n";
877 foreach($question->options->answers as $answer) {
878 $percent = $answer->fraction * 100;
879 $expout .= " <answer fraction=\"$percent\">\n";
880 $expout .= $this->writetext( $answer->answer,4,false );
881 $expout .= " <feedback>\n";
882 $expout .= $this->writetext( $answer->feedback,5,false );
883 $expout .= " </feedback>\n";
884 $expout .= " </answer>\n";
886 break;
887 case SHORTANSWER:
888 $expout .= " <usecase>{$question->options->usecase}</usecase>\n ";
889 foreach($question->options->answers as $answer) {
890 $percent = 100 * $answer->fraction;
891 $expout .= " <answer fraction=\"$percent\">\n";
892 $expout .= $this->writetext( $answer->answer,3,false );
893 $expout .= " <feedback>\n";
894 $expout .= $this->writetext( $answer->feedback,4,false );
895 $expout .= " </feedback>\n";
896 $expout .= " </answer>\n";
898 break;
899 //case regexp:
900 //$expout .= " <usecase>{$question->options->usecase}</usecase>\n ";
901 // foreach($question->options->answers as $answer) {
902 // $percent = 100 * $answer->fraction;
903 // $expout .= " <answer fraction=\"$percent\">\n";
904 // $expout .= $this->writetext( $answer->answer,3,false );
905 // $expout .= " <feedback>\n";
906 // $expout .= $this->writetext( $answer->feedback,4,false );
907 // $expout .= " </feedback>\n";
908 // $expout .= " </answer>\n";
909 // }
910 // break;
911 case NUMERICAL:
912 foreach ($question->options->answers as $answer) {
913 $tolerance = $answer->tolerance;
914 $percent = 100 * $answer->fraction;
915 $expout .= "<answer fraction=\"$percent\">\n";
916 // <text> tags are an added feature, old filed won't have them
917 $expout .= " <text>{$answer->answer}</text>\n";
918 $expout .= " <tolerance>$tolerance</tolerance>\n";
919 $expout .= " <feedback>".$this->writetext( $answer->feedback )."</feedback>\n";
920 // fraction tag is deprecated
921 // $expout .= " <fraction>{$answer->fraction}</fraction>\n";
922 $expout .= "</answer>\n";
925 $units = $question->options->units;
926 if (count($units)) {
927 $expout .= "<units>\n";
928 foreach ($units as $unit) {
929 $expout .= " <unit>\n";
930 $expout .= " <multiplier>{$unit->multiplier}</multiplier>\n";
931 $expout .= " <unit_name>{$unit->unit}</unit_name>\n";
932 $expout .= " </unit>\n";
934 $expout .= "</units>\n";
936 break;
937 case MATCH:
938 foreach($question->options->subquestions as $subquestion) {
939 $expout .= "<subquestion>\n";
940 $expout .= $this->writetext( $subquestion->questiontext );
941 $expout .= "<answer>".$this->writetext( $subquestion->answertext )."</answer>\n";
942 $expout .= "</subquestion>\n";
944 break;
945 case DESCRIPTION:
946 // nothing more to do for this type
947 break;
948 case MULTIANSWER:
949 $a_count=1;
950 foreach($question->options->questions as $question) {
951 $thispattern = addslashes("{#".$a_count."}");
952 $thisreplace = $question->questiontext;
953 $expout=ereg_replace($thispattern, $thisreplace, $expout );
954 $a_count++;
956 break;
957 case ESSAY:
958 foreach ($question->options->answers as $answer) {
959 $percent = 100 * $answer->fraction;
960 $expout .= "<answer fraction=\"$percent\">\n";
961 $expout .= " <feedback>".$this->writetext( $answer->feedback )."</feedback>\n";
962 // fraction tag is deprecated
963 // $expout .= " <fraction>{$answer->fraction}</fraction>\n";
964 $expout .= "</answer>\n";
967 break;
968 case CALCULATED:
969 foreach ($question->options->answers as $answer) {
970 $tolerance = $answer->tolerance;
971 $tolerancetype = $answer->tolerancetype;
972 $correctanswerlength= $answer->correctanswerlength ;
973 $correctanswerformat= $answer->correctanswerformat;
974 $percent = 100 * $answer->fraction;
975 $expout .= "<answer fraction=\"$percent\">\n";
976 // "<text/>" tags are an added feature, old files won't have them
977 $expout .= " <text>{$answer->answer}</text>\n";
978 $expout .= " <tolerance>$tolerance</tolerance>\n";
979 $expout .= " <tolerancetype>$tolerancetype</tolerancetype>\n";
980 $expout .= " <correctanswerformat>$correctanswerformat</correctanswerformat>\n";
981 $expout .= " <correctanswerlength>$correctanswerformat</correctanswerlength>\n";
982 $expout .= " <feedback>".$this->writetext( $answer->feedback )."</feedback>\n";
983 $expout .= "</answer>\n";
985 $units = $question->options->units;
986 if (count($units)) {
987 $expout .= "<units>\n";
988 foreach ($units as $unit) {
989 $expout .= " <unit>\n";
990 $expout .= " <multiplier>{$unit->multiplier}</multiplier>\n";
991 $expout .= " <unit_name>{$unit->unit}</unit_name>\n";
992 $expout .= " </unit>\n";
994 $expout .= "</units>\n";
996 //echo "<pre> question calc";print_r($question);echo "</pre>";
997 //First, we a new function to get all the data itmes in the database
998 // $question_datasetdefs =$QTYPES['calculated']->get_datasets_for_export ($question);
999 // echo "<pre> question defs";print_r($question_datasetdefs);echo "</pre>";
1000 //If there are question_datasets
1001 if( isset($question->options->datasets)&&count($question->options->datasets)){// there should be
1002 $expout .= "<dataset_definitions>\n";
1003 foreach ($question->options->datasets as $def) {
1004 $expout .= "<dataset_definition>\n";
1005 $expout .= " <status>".$this->writetext($def->status)."</status>\n";
1006 $expout .= " <name>".$this->writetext($def->name)."</name>\n";
1007 $expout .= " <type>calculated</type>\n";
1008 $expout .= " <distribution>".$this->writetext($def->distribution)."</distribution>\n";
1009 $expout .= " <minimum>".$this->writetext($def->minimum)."</minimum>\n";
1010 $expout .= " <maximum>".$this->writetext($def->maximum)."</maximum>\n";
1011 $expout .= " <decimals>".$this->writetext($def->decimals)."</decimals>\n";
1012 $expout .= " <itemcount>$def->itemcount</itemcount>\n";
1013 if ($def->itemcount > 0 ) {
1014 $expout .= " <dataset_items>\n";
1015 foreach ($def->items as $item ){
1016 $expout .= " <dataset_item>\n";
1017 $expout .= " <number>".$item->itemnumber."</number>\n";
1018 $expout .= " <value>".$item->value."</value>\n";
1019 $expout .= " </dataset_item>\n";
1021 $expout .= " </dataset_items>\n";
1022 $expout .= " <number_of_items>".$def-> number_of_items."</number_of_items>\n";
1024 $expout .= "</dataset_definition>\n";
1026 $expout .= "</dataset_definitions>\n";
1028 break;
1029 default:
1030 // should not get here
1031 error( 'Unsupported question type detected in strange circumstances!' );
1034 // close the question tag
1035 $expout .= "</question>\n";
1037 return $expout;