3 ///////////////////////////////////////////////////////////////
6 //////////////////////////////////////////////////////////////////////////
7 // Based on default.php, included by ../import.php
9 * @package questionbank
10 * @subpackage importexport
12 require_once( "$CFG->libdir/xmlize.php" );
14 class qformat_xml
extends qformat_default
{
16 function provide_import() {
20 function provide_export() {
24 // IMPORT FUNCTIONS START HERE
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 ) {
35 if ($name=='moodle_auto_format') {
38 elseif ($name=='html') {
41 elseif ($name=='plain_text') {
44 elseif ($name=='wiki_like') {
47 elseif ($name=='markdown') {
51 $id = 0; // or maybe warning required
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 ) {
64 if ($name == "false" ||
!$name) {
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 ) {
81 $data = $text[0]['#'];
82 return addslashes(trim( $data ));
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']);
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'] );
119 $generalfeedback = '';
121 if (!empty($question['#']['defaultgrade'][0]['#'])) {
122 $qo->defaultgrade
= $question['#']['defaultgrade'][0]['#'];
125 $penalty = $question['#']['penalty'][0]['#'];
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;
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'] );
148 $ans->answer
= $text;
149 $ans->fraction
= $fraction / 100;
150 $ans->feedback
= $feedback;
156 * import multiple choice question
157 * @param array question question array from xml tree
158 * @return object question object
160 function import_multichoice( $question ) {
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]['#'];
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'];
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
;
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'] );
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 ) {
219 $qo = $this->import_headers( $question );
221 // 'header' parts particular to true/false
222 $qo->qtype
= TRUEFALSE
;
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.
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') {
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;
244 $qo->answer
= ($answer['@']['fraction'] != 100);
245 $qo->correctanswer
= $qo->answer
;
246 $qo->feedbackfalse
= $feedback;
253 $a->questiontext
= $qo->questiontext
;
254 $a->answer
= get_string($qo->answer ?
'true' : 'false', 'quiz');
255 notify(get_string('truefalseimporterror', 'quiz', $a));
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 ) {
268 $qo = $this->import_headers( $question );
270 // header parts particular to shortanswer
271 $qo->qtype
= SHORTANSWER
;
274 $qo->usecase
= $question['#']['usecase'][0]['#'];
276 // run through the answers
277 $answers = $question['#']['answer'];
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
;
291 * import regexp type question
292 * @param array question question array from xml tree
293 * @return object question object
295 function import_regexp( $question ) {
297 $qo = $this->import_headers( $question );
299 // header parts particular to shortanswer
303 $qo->usecase
= $question['#']['usecase'][0]['#'];
305 // run through the answers
306 $answers = $question['#']['answer'];
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
;
320 * import description type question
321 * @param array question question array from xml tree
322 * @return object question object
324 function import_description( $question ) {
326 $qo = $this->import_headers( $question );
327 // header parts particular to shortanswer
328 $qo->qtype
= DESCRIPTION
;
333 * import numerical type question
334 * @param array question question array from xml tree
335 * @return object question object
337 function import_numerical( $question ) {
339 $qo = $this->import_headers( $question );
341 // header parts particular to numerical
342 $qo->qtype
= NUMERICAL
;
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'] );
356 $answertext = trim($answer['#'][0]);
358 if ($answertext == '') {
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]['#'];
371 $qo->fraction
[] = $answer['@']['fraction'] / 100;
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]['#'];
389 * import matching type question
390 * @param array question question array from xml tree
391 * @return object question object
393 function import_matching( $question ) {
395 $qo = $this->import_headers( $question );
397 // header parts particular to matching
399 if (!empty($question['#']['shuffleanswers'])) {
400 $qo->shuffleanswers
= $question['#']['shuffleanswers'][0]['#'];
402 $qo->shuffleanswers
= false;
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;
421 * import essay type question
422 * @param array question question array from xml tree
423 * @return object question object
425 function import_essay( $question ) {
427 $qo = $this->import_headers( $question );
429 // header parts particular to essay
433 $qo->feedback
= $this->import_text( $question['#']['answer'][0]['#']['feedback'][0]['#']['text'] );
436 $answer = $question['#']['answer'][0];
438 // get fraction - <fraction> tag is deprecated
439 if (!empty($answer['#']['fraction'][0]['#'])) {
440 $qo->fraction
= $answer['#']['fraction'][0]['#'];
443 $qo->fraction
= $answer['@']['fraction'] / 100;
449 function import_calculated( $question ) {
450 // import numerical question
453 $qo = $this->import_headers( $question );
455 // header parts particular to numerical
456 $qo->qtype
= CALCULATED
;//CALCULATED;
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'] );
475 $answertext = trim($answer['#'][0]);
477 if ($answertext == '') {
478 $qo->answers
[] = '*';
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]['#'];
489 $qo->fraction
[] = $answer['@']['fraction'] / 100;
491 $qo->tolerancetype
[] = $answer['#']['tolerancetype'][0]['#'];
492 $qo->correctanswerformat
[] = $answer['#']['correctanswerformat'][0]['#'];
493 $qo->correctanswerlength
[] = $answer['#']['correctanswerlength'][0]['#'];
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) {
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>";
538 * this is not a real question type. It's a dummy type used
539 * to specify the import category
541 * <question type="category">
542 * <category>tom/dick/harry</category>
545 function import_category( $question ) {
547 $qo->qtype
= 'category';
548 $qo->category
= $question['#']['category'][0]['#'];
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, " ");
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 );
611 $notsupported = get_string( 'xmltypeunsupported','quiz',$question_type );
612 $this->error( $notsupportted );
616 // stick the result in the $questions array
618 $qo->generalfeedback
= '';
625 // EXPORT FUNCTIONS START HERE
627 function export_file_extension() {
628 // override default type so extension is .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 ) {
647 $name = 'multichoice';
650 $name = 'shortanswer';
662 $name = 'description';
671 $name = 'calculated';
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 ) {
688 $name = "moodle_auto_format";
694 $name = "plain_text";
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 ) {
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 );
741 $xml = "$indent<text>$raw</text>\n";
744 $xml = "$indent<text>\n$raw\n$indent</text>\n";
750 function presave_process( $content ) {
751 // override method to allow us to add xml headers and footers
753 $content = "<?xml version=\"1.0\"?>\n" .
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 ) {
769 if (empty($imagepath)) {
773 $courseid = $this->course
->id
;
774 if (!$binary = file_get_contents( "{$CFG->dataroot}/$courseid/$imagepath" )) {
778 $content = " <image_base64>\n".addslashes(base64_encode( $binary ))."\n".
779 "\n </image_base64>\n";
784 * Turns question into an xml segment
785 * @param array question question array
786 * @return string xml segment
788 function writequestion( $question ) {
794 $expout .= "\n\n<!-- question: $question->id -->\n";
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";
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";
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";
843 $expout .= " <shuffleanswers>0</shuffleanswers>\n";
846 // output depends on question type
847 switch($question->qtype
) {
849 // not a qtype really - dummy used for category switching
852 foreach ($question->options
->answers
as $answer) {
853 $fraction_pc = round( $answer->fraction
* 100 );
854 if ($answer->id
== $question->options
->trueanswer
) {
855 $answertext = 'true';
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";
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";
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";
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";
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
;
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";
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";
942 // nothing more to do for this type
946 foreach($question->options
->questions
as $question) {
947 $thispattern = addslashes("{#".$a_count."}");
948 $thisreplace = $question->questiontext
;
949 $expout=ereg_replace($thispattern, $thisreplace, $expout );
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";
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
;
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";
1026 $expout .= "<!-- Question type is unknown or not supported (Type=$question->qtype) -->\n";
1029 // close the question tag
1030 $expout .= "</question>\n";