A couple of changes to Pierre's calculated question import/export
[moodle-linuxchix.git] / question / format / xml / format.php
blobc4b4e6cda11284fd04605d330d1a129bc1e33a5e
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 * Process text from an element in the XML that may or not be there.
87 * @param string $subelement the name of the element which is either present or missing.
88 * @param array $question a bit of xml tree, this method looks for $question['#'][$subelement][0]['#']['text'].
89 * @return string If $subelement is present, return the content of the text tag inside it.
90 * Otherwise returns an empty string.
92 function import_optional_text($subelement, $question) {
93 if (array_key_exists($subelement, $question['#'])) {
94 return $this->import_text($question['#'][$subelement][0]['#']['text']);
95 } else {
96 return '';
101 * import parts of question common to all types
102 * @param array question question array from xml tree
103 * @return object question object
105 function import_headers( $question ) {
106 // this routine initialises the question object
107 $qo = $this->defaultquestion();
108 $name = $this->import_text( $question['#']['name'][0]['#']['text'] );
109 $qtext = $this->import_text( $question['#']['questiontext'][0]['#']['text'] );
110 $qformat = $question['#']['questiontext'][0]['@']['format'];
111 $image = $question['#']['image'][0]['#'];
112 if (!empty($question['#']['image_base64'][0]['#'])) {
113 $image_base64 = stripslashes( trim( $question['#']['image_base64'][0]['#'] ) );
114 $image = $this->importimagefile( $image, $image_base64 );
116 if (array_key_exists('generalfeedback', $question['#'])) {
117 $generalfeedback = $this->import_text( $question['#']['generalfeedback'][0]['#']['text'] );
118 } else {
119 $generalfeedback = '';
121 if (!empty($question['#']['defaultgrade'][0]['#'])) {
122 $qo->defaultgrade = $question['#']['defaultgrade'][0]['#'];
125 $penalty = $question['#']['penalty'][0]['#'];
127 $qo->name = $name;
128 $qo->questiontext = $qtext;
129 $qo->questiontextformat = $this->trans_format( $qformat );
130 $qo->image = ((!empty($image)) ? $image : '');
131 $qo->generalfeedback = $generalfeedback;
132 $qo->penalty = $penalty;
134 return $qo;
138 * import the common parts of a single answer
139 * @param array answer xml tree for single answer
140 * @return object answer object
142 function import_answer( $answer ) {
143 $fraction = $answer['@']['fraction'];
144 $text = $this->import_text( $answer['#']['text']);
145 $feedback = $this->import_text( $answer['#']['feedback'][0]['#']['text'] );
147 $ans = null;
148 $ans->answer = $text;
149 $ans->fraction = $fraction / 100;
150 $ans->feedback = $feedback;
152 return $ans;
156 * import multiple choice question
157 * @param array question question array from xml tree
158 * @return object question object
160 function import_multichoice( $question ) {
161 // get common parts
162 $qo = $this->import_headers( $question );
164 // 'header' parts particular to multichoice
165 $qo->qtype = MULTICHOICE;
166 $single = $question['#']['single'][0]['#'];
167 $qo->single = $this->trans_single( $single );
168 if (array_key_exists('shuffleanswers', $question['#'])) {
169 $shuffleanswers = $question['#']['shuffleanswers'][0]['#'];
170 } else {
171 $shuffleanswers = 'false';
173 $qo->shuffleanswers = $this->trans_single($shuffleanswers);
174 $qo->correctfeedback = $this->import_optional_text('correctfeedback', $question);
175 $qo->partiallycorrectfeedback = $this->import_optional_text('partiallycorrectfeedback', $question);
176 $qo->incorrectfeedback = $this->import_optional_text('incorrectfeedback', $question);
178 // run through the answers
179 $answers = $question['#']['answer'];
180 $a_count = 0;
181 foreach ($answers as $answer) {
182 $ans = $this->import_answer( $answer );
183 $qo->answer[$a_count] = $ans->answer;
184 $qo->fraction[$a_count] = $ans->fraction;
185 $qo->feedback[$a_count] = $ans->feedback;
186 ++$a_count;
189 return $qo;
193 * import cloze type question
194 * @param array question question array from xml tree
195 * @return object question object
197 function import_multianswer( $questions ) {
198 $questiontext = $questions['#']['questiontext'][0]['#']['text'];
199 $qo = qtype_multianswer_extract_question($this->import_text($questiontext));
201 // 'header' parts particular to multianswer
202 $qo->qtype = MULTIANSWER;
203 $qo->course = $this->course;
205 if (!empty($questions)) {
206 $qo->name = $this->import_text( $questions['#']['name'][0]['#']['text'] );
209 return $qo;
213 * import true/false type question
214 * @param array question question array from xml tree
215 * @return object question object
217 function import_truefalse( $question ) {
218 // get common parts
219 $qo = $this->import_headers( $question );
221 // 'header' parts particular to true/false
222 $qo->qtype = TRUEFALSE;
224 // get answer info
226 // In the past, it used to be assumed that the two answers were in the file
227 // true first, then false. Howevever that was not always true. Now, we
228 // try to match on the answer text, but in old exports, this will be a localised
229 // string, so if we don't find true or false, we fall back to the old system.
230 $first = true;
231 $warning = false;
232 foreach ($question['#']['answer'] as $answer) {
233 $answertext = $this->import_text($answer['#']['text']);
234 $feedback = $this->import_text($answer['#']['feedback'][0]['#']['text']);
235 if ($answertext != 'true' && $answertext != 'false') {
236 $warning = true;
237 $answertext = $first ? 'true' : 'false'; // Old style file, assume order is true/false.
239 if ($answertext == 'true') {
240 $qo->answer = ($answer['@']['fraction'] == 100);
241 $qo->correctanswer = $qo->answer;
242 $qo->feedbacktrue = $feedback;
243 } else {
244 $qo->answer = ($answer['@']['fraction'] != 100);
245 $qo->correctanswer = $qo->answer;
246 $qo->feedbackfalse = $feedback;
248 $first = false;
251 if ($warning) {
252 $a = new stdClass;
253 $a->questiontext = $qo->questiontext;
254 $a->answer = get_string($qo->answer ? 'true' : 'false', 'quiz');
255 notify(get_string('truefalseimporterror', 'quiz', $a));
258 return $qo;
262 * import short answer type question
263 * @param array question question array from xml tree
264 * @return object question object
266 function import_shortanswer( $question ) {
267 // get common parts
268 $qo = $this->import_headers( $question );
270 // header parts particular to shortanswer
271 $qo->qtype = SHORTANSWER;
273 // get usecase
274 $qo->usecase = $question['#']['usecase'][0]['#'];
276 // run through the answers
277 $answers = $question['#']['answer'];
278 $a_count = 0;
279 foreach ($answers as $answer) {
280 $ans = $this->import_answer( $answer );
281 $qo->answer[$a_count] = $ans->answer;
282 $qo->fraction[$a_count] = $ans->fraction;
283 $qo->feedback[$a_count] = $ans->feedback;
284 ++$a_count;
287 return $qo;
291 * import regexp type question
292 * @param array question question array from xml tree
293 * @return object question object
295 function import_regexp( $question ) {
296 // get common parts
297 $qo = $this->import_headers( $question );
299 // header parts particular to shortanswer
300 $qo->qtype = regexp;
302 // get usecase
303 $qo->usecase = $question['#']['usecase'][0]['#'];
305 // run through the answers
306 $answers = $question['#']['answer'];
307 $a_count = 0;
308 foreach ($answers as $answer) {
309 $ans = $this->import_answer( $answer );
310 $qo->answer[$a_count] = $ans->answer;
311 $qo->fraction[$a_count] = $ans->fraction;
312 $qo->feedback[$a_count] = $ans->feedback;
313 ++$a_count;
316 return $qo;
320 * import description type question
321 * @param array question question array from xml tree
322 * @return object question object
324 function import_description( $question ) {
325 // get common parts
326 $qo = $this->import_headers( $question );
327 // header parts particular to shortanswer
328 $qo->qtype = DESCRIPTION;
329 return $qo;
333 * import numerical type question
334 * @param array question question array from xml tree
335 * @return object question object
337 function import_numerical( $question ) {
338 // get common parts
339 $qo = $this->import_headers( $question );
341 // header parts particular to numerical
342 $qo->qtype = NUMERICAL;
344 // get answers array
345 $answers = $question['#']['answer'];
346 $qo->answer = array();
347 $qo->feedback = array();
348 $qo->fraction = array();
349 $qo->tolerance = array();
350 foreach ($answers as $answer) {
351 // answer outside of <text> is deprecated
352 if (!empty( $answer['#']['text'] )) {
353 $answertext = $this->import_text( $answer['#']['text'] );
355 else {
356 $answertext = trim($answer['#'][0]);
358 if ($answertext == '') {
359 $qo->answer[] = '*';
360 } else {
361 $qo->answer[] = $answertext;
363 $qo->feedback[] = $this->import_text( $answer['#']['feedback'][0]['#']['text'] );
364 $qo->tolerance[] = $answer['#']['tolerance'][0]['#'];
366 // fraction as a tag is deprecated
367 if (!empty($answer['#']['fraction'][0]['#'])) {
368 $qo->fraction[] = $answer['#']['fraction'][0]['#'];
370 else {
371 $qo->fraction[] = $answer['@']['fraction'] / 100;
375 // get units array
376 $qo->unit = array();
377 if (isset($question['#']['units'][0]['#']['unit'])) {
378 $units = $question['#']['units'][0]['#']['unit'];
379 $qo->multiplier = array();
380 foreach ($units as $unit) {
381 $qo->multiplier[] = $unit['#']['multiplier'][0]['#'];
382 $qo->unit[] = $unit['#']['unit_name'][0]['#'];
385 return $qo;
389 * import matching type question
390 * @param array question question array from xml tree
391 * @return object question object
393 function import_matching( $question ) {
394 // get common parts
395 $qo = $this->import_headers( $question );
397 // header parts particular to matching
398 $qo->qtype = MATCH;
399 if (!empty($question['#']['shuffleanswers'])) {
400 $qo->shuffleanswers = $question['#']['shuffleanswers'][0]['#'];
401 } else {
402 $qo->shuffleanswers = false;
405 // get subquestions
406 $subquestions = $question['#']['subquestion'];
407 $qo->subquestions = array();
408 $qo->subanswers = array();
410 // run through subquestions
411 foreach ($subquestions as $subquestion) {
412 $qtext = $this->import_text( $subquestion['#']['text'] );
413 $atext = $this->import_text( $subquestion['#']['answer'][0]['#']['text'] );
414 $qo->subquestions[] = $qtext;
415 $qo->subanswers[] = $atext;
417 return $qo;
421 * import essay type question
422 * @param array question question array from xml tree
423 * @return object question object
425 function import_essay( $question ) {
426 // get common parts
427 $qo = $this->import_headers( $question );
429 // header parts particular to essay
430 $qo->qtype = ESSAY;
432 // get feedback
433 $qo->feedback = $this->import_text( $question['#']['answer'][0]['#']['feedback'][0]['#']['text'] );
435 // handle answer
436 $answer = $question['#']['answer'][0];
438 // get fraction - <fraction> tag is deprecated
439 if (!empty($answer['#']['fraction'][0]['#'])) {
440 $qo->fraction = $answer['#']['fraction'][0]['#'];
442 else {
443 $qo->fraction = $answer['@']['fraction'] / 100;
446 return $qo;
449 function import_calculated( $question ) {
450 // import numerical question
452 // get common parts
453 $qo = $this->import_headers( $question );
455 // header parts particular to numerical
456 $qo->qtype = CALCULATED ;//CALCULATED;
458 // get answers array
459 // echo "<pre> question";print_r($question);echo "</pre>";
460 $answers = $question['#']['answer'];
461 $qo->answers = array();
462 $qo->feedback = array();
463 $qo->fraction = array();
464 $qo->tolerance = array();
465 $qo->tolerancetype = array();
466 $qo->correctanswerformat = array();
467 $qo->correctanswerlength = array();
468 $qo->feedback = array();
469 foreach ($answers as $answer) {
470 // answer outside of <text> is deprecated
471 if (!empty( $answer['#']['text'] )) {
472 $answertext = $this->import_text( $answer['#']['text'] );
474 else {
475 $answertext = trim($answer['#'][0]);
477 if ($answertext == '') {
478 $qo->answers[] = '*';
479 } else {
480 $qo->answers[] = $answertext;
482 $qo->feedback[] = $this->import_text( $answer['#']['feedback'][0]['#']['text'] );
483 $qo->tolerance[] = $answer['#']['tolerance'][0]['#'];
484 // fraction as a tag is deprecated
485 if (!empty($answer['#']['fraction'][0]['#'])) {
486 $qo->fraction[] = $answer['#']['fraction'][0]['#'];
488 else {
489 $qo->fraction[] = $answer['@']['fraction'] / 100;
491 $qo->tolerancetype[] = $answer['#']['tolerancetype'][0]['#'];
492 $qo->correctanswerformat[] = $answer['#']['correctanswerformat'][0]['#'];
493 $qo->correctanswerlength[] = $answer['#']['correctanswerlength'][0]['#'];
495 // get units array
496 $qo->unit = array();
497 if (isset($question['#']['units'][0]['#']['unit'])) {
498 $units = $question['#']['units'][0]['#']['unit'];
499 $qo->multiplier = array();
500 foreach ($units as $unit) {
501 $qo->multiplier[] = $unit['#']['multiplier'][0]['#'];
502 $qo->unit[] = $unit['#']['unit_name'][0]['#'];
505 $datasets = $question['#']['dataset_definitions'][0]['#']['dataset_definition'];
506 $qo->dataset = array();
507 $qo->datasetindex= 0 ;
508 foreach ($datasets as $dataset) {
509 $qo->datasetindex++;
510 $qo->dataset[$qo->datasetindex] = new stdClass();
511 $qo->dataset[$qo->datasetindex]->status = $this->import_text( $dataset['#']['status'][0]['#']['text']);
512 $qo->dataset[$qo->datasetindex]->name = $this->import_text( $dataset['#']['name'][0]['#']['text']);
513 $qo->dataset[$qo->datasetindex]->type = $dataset['#']['type'][0]['#'];
514 $qo->dataset[$qo->datasetindex]->distribution = $this->import_text( $dataset['#']['distribution'][0]['#']['text']);
515 $qo->dataset[$qo->datasetindex]->max = $this->import_text( $dataset['#']['maximum'][0]['#']['text']);
516 $qo->dataset[$qo->datasetindex]->min = $this->import_text( $dataset['#']['minimum'][0]['#']['text']);
517 $qo->dataset[$qo->datasetindex]->length = $this->import_text( $dataset['#']['decimals'][0]['#']['text']);
518 $qo->dataset[$qo->datasetindex]->distribution = $this->import_text( $dataset['#']['distribution'][0]['#']['text']);
519 $qo->dataset[$qo->datasetindex]->itemcount = $dataset['#']['itemcount'][0]['#'];
520 $qo->dataset[$qo->datasetindex]->datasetitem = array();
521 $qo->dataset[$qo->datasetindex]->itemindex = 0;
522 $qo->dataset[$qo->datasetindex]->number_of_items=$dataset['#']['number_of_items'][0]['#'];
523 $datasetitems = $dataset['#']['dataset_items'][0]['#']['dataset_item'];
524 foreach ($datasetitems as $datasetitem) {
525 $qo->dataset[$qo->datasetindex]->itemindex++;
526 $qo->dataset[$qo->datasetindex]->datasetitem[$qo->dataset[$qo->datasetindex]->itemindex] = new stdClass();
527 $qo->dataset[$qo->datasetindex]->datasetitem[$qo->dataset[$qo->datasetindex]->itemindex]->itemnumber = $datasetitem['#']['number'][0]['#']; //[0]['#']['number'][0]['#'] ; // [0]['numberitems'] ;//['#']['number'][0]['#'];// $datasetitems['#']['number'][0]['#'];
528 $qo->dataset[$qo->datasetindex]->datasetitem[$qo->dataset[$qo->datasetindex]->itemindex]->value = $datasetitem['#']['value'][0]['#'] ;//$datasetitem['#']['value'][0]['#'];
532 // echo "<pre>loaded qo";print_r($qo);echo "</pre>";
534 return $qo;
538 * this is not a real question type. It's a dummy type used
539 * to specify the import category
540 * format is:
541 * <question type="category">
542 * <category>tom/dick/harry</category>
543 * </question>
545 function import_category( $question ) {
546 $qo = new stdClass;
547 $qo->qtype = 'category';
548 $qo->category = $question['#']['category'][0]['#'];
549 return $qo;
553 * parse the array of lines into an array of questions
554 * this *could* burn memory - but it won't happen that much
555 * so fingers crossed!
556 * @param array lines array of lines from the input file
557 * @return array (of objects) question objects
559 function readquestions($lines) {
560 // we just need it as one big string
561 $text = implode($lines, " ");
562 unset( $lines );
564 // this converts xml to big nasty data structure
565 // the 0 means keep white space as it is (important for markdown format)
566 // print_r it if you want to see what it looks like!
567 $xml = xmlize( $text, 0 );
569 // set up array to hold all our questions
570 $questions = array();
572 // iterate through questions
573 foreach ($xml['quiz']['#']['question'] as $question) {
574 $question_type = $question['@']['type'];
575 $questiontype = get_string( 'questiontype','quiz',$question_type );
577 if ($question_type=='multichoice') {
578 $qo = $this->import_multichoice( $question );
580 elseif ($question_type=='truefalse') {
581 $qo = $this->import_truefalse( $question );
583 elseif ($question_type=='shortanswer') {
584 $qo = $this->import_shortanswer( $question );
586 //elseif ($question_type=='regexp') {
587 // $qo = $this->import_regexp( $question );
589 elseif ($question_type=='numerical') {
590 $qo = $this->import_numerical( $question );
592 elseif ($question_type=='description') {
593 $qo = $this->import_description( $question );
595 elseif ($question_type=='matching') {
596 $qo = $this->import_matching( $question );
598 elseif ($question_type=='cloze') {
599 $qo = $this->import_multianswer( $question );
601 elseif ($question_type=='essay') {
602 $qo = $this->import_essay( $question );
604 elseif ($question_type=='calculated') {
605 $qo = $this->import_calculated( $question );
607 elseif ($question_type=='category') {
608 $qo = $this->import_category( $question );
610 else {
611 $notsupported = get_string( 'xmltypeunsupported','quiz',$question_type );
612 $this->error( $notsupportted );
613 $qo = null;
616 // stick the result in the $questions array
617 if ($qo) {
618 $qo->generalfeedback = '';
619 $questions[] = $qo;
622 return $questions;
625 // EXPORT FUNCTIONS START HERE
627 function export_file_extension() {
628 // override default type so extension is .xml
630 return ".xml";
635 * Turn the internal question code into a human readable form
636 * (The code used to be numeric, but this remains as some of
637 * the names don't match the new internal format)
638 * @param mixed type_id Internal code
639 * @return string question type string
641 function get_qtype( $type_id ) {
642 switch( $type_id ) {
643 case TRUEFALSE:
644 $name = 'truefalse';
645 break;
646 case MULTICHOICE:
647 $name = 'multichoice';
648 break;
649 case SHORTANSWER:
650 $name = 'shortanswer';
651 break;
652 //case regexp:
653 // $name = 'regexp';
654 // break;
655 case NUMERICAL:
656 $name = 'numerical';
657 break;
658 case MATCH:
659 $name = 'matching';
660 break;
661 case DESCRIPTION:
662 $name = 'description';
663 break;
664 case MULTIANSWER:
665 $name = 'cloze';
666 break;
667 case ESSAY:
668 $name = 'essay';
669 break;
670 case CALCULATED:
671 $name = 'calculated';
672 break;
673 default:
674 $name = 'unknown';
676 return $name;
680 * Convert internal Moodle text format code into
681 * human readable form
682 * @param int id internal code
683 * @return string format text
685 function get_format( $id ) {
686 switch( $id ) {
687 case 0:
688 $name = "moodle_auto_format";
689 break;
690 case 1:
691 $name = "html";
692 break;
693 case 2:
694 $name = "plain_text";
695 break;
696 case 3:
697 $name = "wiki_like";
698 break;
699 case 4:
700 $name = "markdown";
701 break;
702 default:
703 $name = "unknown";
705 return $name;
709 * Convert internal single question code into
710 * human readable form
711 * @param int id single question code
712 * @return string single question string
714 function get_single( $id ) {
715 switch( $id ) {
716 case 0:
717 $name = "false";
718 break;
719 case 1:
720 $name = "true";
721 break;
722 default:
723 $name = "unknown";
725 return $name;
729 * generates <text></text> tags, processing raw text therein
730 * @param int ilev the current indent level
731 * @param boolean short stick it on one line
732 * @return string formatted text
734 function writetext( $raw, $ilev=0, $short=true) {
735 $indent = str_repeat( " ",$ilev );
737 // encode the text to 'disguise' HTML content
738 $raw = htmlspecialchars( $raw );
740 if ($short) {
741 $xml = "$indent<text>$raw</text>\n";
743 else {
744 $xml = "$indent<text>\n$raw\n$indent</text>\n";
747 return $xml;
750 function presave_process( $content ) {
751 // override method to allow us to add xml headers and footers
753 $content = "<?xml version=\"1.0\"?>\n" .
754 "<quiz>\n" .
755 $content . "\n" .
756 "</quiz>";
758 return $content;
762 * Include an image encoded in base 64
763 * @param string imagepath The location of the image file
764 * @return string xml code segment
766 function writeimage( $imagepath ) {
767 global $CFG;
769 if (empty($imagepath)) {
770 return '';
773 $courseid = $this->course->id;
774 if (!$binary = file_get_contents( "{$CFG->dataroot}/$courseid/$imagepath" )) {
775 return '';
778 $content = " <image_base64>\n".addslashes(base64_encode( $binary ))."\n".
779 "\n </image_base64>\n";
780 return $content;
784 * Turns question into an xml segment
785 * @param array question question array
786 * @return string xml segment
788 function writequestion( $question ) {
789 global $CFG,$QTYPES;
790 // initial string;
791 $expout = "";
793 // add comment
794 $expout .= "\n\n<!-- question: $question->id -->\n";
796 // add opening tag
797 // generates specific header for Cloze and category type question
798 if ($question->qtype == 'category') {
799 $expout .= " <question type=\"category\">\n";
800 $expout .= " <category>\n";
801 $expout .= " $question->category\n";
802 $expout .= " </category>\n";
803 $expout .= " </question>\n";
804 return $expout;
806 elseif ($question->qtype != MULTIANSWER) {
807 // for all question types except Close
808 $question_type = $this->get_qtype( $question->qtype );
809 $name_text = $this->writetext( $question->name );
810 $qtformat = $this->get_format($question->questiontextformat);
811 $question_text = $this->writetext( $question->questiontext );
812 $generalfeedback = $this->writetext( $question->generalfeedback );
813 $expout .= " <question type=\"$question_type\">\n";
814 $expout .= " <name>$name_text</name>\n";
815 $expout .= " <questiontext format=\"$qtformat\">\n";
816 $expout .= $question_text;
817 $expout .= " </questiontext>\n";
818 $expout .= " <image>{$question->image}</image>\n";
819 $expout .= $this->writeimage($question->image);
820 $expout .= " <generalfeedback>\n";
821 $expout .= $generalfeedback;
822 $expout .= " </generalfeedback>\n";
823 $expout .= " <defaultgrade>{$question->defaultgrade}</defaultgrade>\n";
824 $expout .= " <penalty>{$question->penalty}</penalty>\n";
825 $expout .= " <hidden>{$question->hidden}</hidden>\n";
827 else {
828 // for Cloze type only
829 $question_type = $this->get_qtype( $question->qtype );
830 $name_text = $this->writetext( $question->name );
831 $question_text = $this->writetext( $question->questiontext );
832 $expout .= " <question type=\"$question_type\">\n";
833 $expout .= " <name>$name_text</name>\n";
834 $expout .= " <questiontext>\n";
835 $expout .= $question_text;
836 $expout .= " </questiontext>\n";
839 if (!empty($question->options->shuffleanswers)) {
840 $expout .= " <shuffleanswers>{$question->options->shuffleanswers}</shuffleanswers>\n";
842 else {
843 $expout .= " <shuffleanswers>0</shuffleanswers>\n";
846 // output depends on question type
847 switch($question->qtype) {
848 case 'category':
849 // not a qtype really - dummy used for category switching
850 break;
851 case TRUEFALSE:
852 foreach ($question->options->answers as $answer) {
853 $fraction_pc = round( $answer->fraction * 100 );
854 if ($answer->id == $question->options->trueanswer) {
855 $answertext = 'true';
856 } else {
857 $answertext = 'false';
859 $expout .= " <answer fraction=\"$fraction_pc\">\n";
860 $expout .= $this->writetext($answertext, 3) . "\n";
861 $expout .= " <feedback>\n";
862 $expout .= $this->writetext( $answer->feedback,4,false );
863 $expout .= " </feedback>\n";
864 $expout .= " </answer>\n";
866 break;
867 case MULTICHOICE:
868 $expout .= " <single>".$this->get_single($question->options->single)."</single>\n";
869 $expout .= " <shuffleanswers>".$this->get_single($question->options->shuffleanswers)."</shuffleanswers>\n";
870 $expout .= " <correctfeedback>".$this->writetext($question->options->correctfeedback, 3)."</correctfeedback>\n";
871 $expout .= " <partiallycorrectfeedback>".$this->writetext($question->options->partiallycorrectfeedback, 3)."</partiallycorrectfeedback>\n";
872 $expout .= " <incorrectfeedback>".$this->writetext($question->options->incorrectfeedback, 3)."</incorrectfeedback>\n";
873 foreach($question->options->answers as $answer) {
874 $percent = $answer->fraction * 100;
875 $expout .= " <answer fraction=\"$percent\">\n";
876 $expout .= $this->writetext( $answer->answer,4,false );
877 $expout .= " <feedback>\n";
878 $expout .= $this->writetext( $answer->feedback,5,false );
879 $expout .= " </feedback>\n";
880 $expout .= " </answer>\n";
882 break;
883 case SHORTANSWER:
884 $expout .= " <usecase>{$question->options->usecase}</usecase>\n ";
885 foreach($question->options->answers as $answer) {
886 $percent = 100 * $answer->fraction;
887 $expout .= " <answer fraction=\"$percent\">\n";
888 $expout .= $this->writetext( $answer->answer,3,false );
889 $expout .= " <feedback>\n";
890 $expout .= $this->writetext( $answer->feedback,4,false );
891 $expout .= " </feedback>\n";
892 $expout .= " </answer>\n";
894 break;
895 //case regexp:
896 //$expout .= " <usecase>{$question->options->usecase}</usecase>\n ";
897 // foreach($question->options->answers as $answer) {
898 // $percent = 100 * $answer->fraction;
899 // $expout .= " <answer fraction=\"$percent\">\n";
900 // $expout .= $this->writetext( $answer->answer,3,false );
901 // $expout .= " <feedback>\n";
902 // $expout .= $this->writetext( $answer->feedback,4,false );
903 // $expout .= " </feedback>\n";
904 // $expout .= " </answer>\n";
905 // }
906 // break;
907 case NUMERICAL:
908 foreach ($question->options->answers as $answer) {
909 $tolerance = $answer->tolerance;
910 $percent = 100 * $answer->fraction;
911 $expout .= "<answer fraction=\"$percent\">\n";
912 // <text> tags are an added feature, old filed won't have them
913 $expout .= " <text>{$answer->answer}</text>\n";
914 $expout .= " <tolerance>$tolerance</tolerance>\n";
915 $expout .= " <feedback>".$this->writetext( $answer->feedback )."</feedback>\n";
916 // fraction tag is deprecated
917 // $expout .= " <fraction>{$answer->fraction}</fraction>\n";
918 $expout .= "</answer>\n";
921 $units = $question->options->units;
922 if (count($units)) {
923 $expout .= "<units>\n";
924 foreach ($units as $unit) {
925 $expout .= " <unit>\n";
926 $expout .= " <multiplier>{$unit->multiplier}</multiplier>\n";
927 $expout .= " <unit_name>{$unit->unit}</unit_name>\n";
928 $expout .= " </unit>\n";
930 $expout .= "</units>\n";
932 break;
933 case MATCH:
934 foreach($question->options->subquestions as $subquestion) {
935 $expout .= "<subquestion>\n";
936 $expout .= $this->writetext( $subquestion->questiontext );
937 $expout .= "<answer>".$this->writetext( $subquestion->answertext )."</answer>\n";
938 $expout .= "</subquestion>\n";
940 break;
941 case DESCRIPTION:
942 // nothing more to do for this type
943 break;
944 case MULTIANSWER:
945 $a_count=1;
946 foreach($question->options->questions as $question) {
947 $thispattern = addslashes("{#".$a_count."}");
948 $thisreplace = $question->questiontext;
949 $expout=ereg_replace($thispattern, $thisreplace, $expout );
950 $a_count++;
952 break;
953 case ESSAY:
954 foreach ($question->options->answers as $answer) {
955 $percent = 100 * $answer->fraction;
956 $expout .= "<answer fraction=\"$percent\">\n";
957 $expout .= " <feedback>".$this->writetext( $answer->feedback )."</feedback>\n";
958 // fraction tag is deprecated
959 // $expout .= " <fraction>{$answer->fraction}</fraction>\n";
960 $expout .= "</answer>\n";
963 break;
964 case CALCULATED:
965 foreach ($question->options->answers as $answer) {
966 $tolerance = $answer->tolerance;
967 $tolerancetype = $answer->tolerancetype;
968 $correctanswerlength= $answer->correctanswerlength ;
969 $correctanswerformat= $answer->correctanswerformat;
970 $percent = 100 * $answer->fraction;
971 $expout .= "<answer fraction=\"$percent\">\n";
972 // "<text/>" tags are an added feature, old files won't have them
973 $expout .= " <text>{$answer->answer}</text>\n";
974 $expout .= " <tolerance>$tolerance</tolerance>\n";
975 $expout .= " <tolerancetype>$tolerancetype</tolerancetype>\n";
976 $expout .= " <correctanswerformat>$correctanswerformat</correctanswerformat>\n";
977 $expout .= " <correctanswerlength>$correctanswerformat</correctanswerlength>\n";
978 $expout .= " <feedback>".$this->writetext( $answer->feedback )."</feedback>\n";
979 $expout .= "</answer>\n";
981 $units = $question->options->units;
982 if (count($units)) {
983 $expout .= "<units>\n";
984 foreach ($units as $unit) {
985 $expout .= " <unit>\n";
986 $expout .= " <multiplier>{$unit->multiplier}</multiplier>\n";
987 $expout .= " <unit_name>{$unit->unit}</unit_name>\n";
988 $expout .= " </unit>\n";
990 $expout .= "</units>\n";
992 //echo "<pre> question calc";print_r($question);echo "</pre>";
993 //First, we a new function to get all the data itmes in the database
994 $question_datasetdefs =$QTYPES['calculated']->get_datasets_for_export ($question->id);
995 // echo "<pre> question defs";print_r($question_datasetdefs);echo "</pre>";
996 //If there are question_datasets
997 if (count($question_datasetdefs)) {// there should be
998 $expout .= "<dataset_definitions>\n";
999 foreach ($question_datasetdefs as $def) {
1000 $expout .= "<dataset_definition>\n";
1001 $expout .= " <status>".$this->writetext($def->status)."</status>\n";
1002 $expout .= " <name>".$this->writetext($def->name)."</name>\n";
1003 $expout .= " <type>calculated</type>\n";
1004 $expout .= " <distribution>".$this->writetext($def->distribution)."</distribution>\n";
1005 $expout .= " <minimum>".$this->writetext($def->minimum)."</minimum>\n";
1006 $expout .= " <maximum>".$this->writetext($def->maximum)."</maximum>\n";
1007 $expout .= " <decimals>".$this->writetext($def->decimals)."</decimals>\n";
1008 $expout .= " <itemcount>$def->itemcount</itemcount>\n";
1009 if ($def->itemcount > 0 ) {
1010 $expout .= " <dataset_items>\n";
1011 foreach ($def->items as $item ){
1012 $expout .= " <dataset_item>\n";
1013 $expout .= " <number>".$item->itemnumber."</number>\n";
1014 $expout .= " <value>".$item->value."</value>\n";
1015 $expout .= " </dataset_item>\n";
1017 $expout .= " </dataset_items>\n";
1018 $expout .= " <number_of_items>".$def-> number_of_items."</number_of_items>\n";
1020 $expout .= "</dataset_definition>\n";
1022 $expout .= "</dataset_definitions>\n";
1024 break;
1025 default:
1026 $expout .= "<!-- Question type is unknown or not supported (Type=$question->qtype) -->\n";
1029 // close the question tag
1030 $expout .= "</question>\n";
1032 return $expout;