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 * 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
95 function getpath( $xml, $path, $default, $istext=false, $error='' ) {
96 foreach ($path as $index) {
97 if (!isset($xml[$index])) {
99 $this->error( $error );
105 else $xml = $xml[$index];
108 $xml = addslashes( trim( $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();
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
);
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 );
155 $ans->answer
= $text;
156 $ans->fraction
= $fraction / 100;
157 $ans->feedback
= $feedback;
162 * import multiple choice question
163 * @param array question question array from xml tree
164 * @return object question object
166 function import_multichoice( $question ) {
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'];
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
;
195 * import cloze type question
196 * @param array question question array from xml tree
197 * @return object question object
199 function import_multianswer( $questions ) {
200 $questiontext = $questions['#']['questiontext'][0]['#']['text'];
201 $qo = qtype_multianswer_extract_question($this->import_text($questiontext));
203 // 'header' parts particular to multianswer
204 $qo->qtype
= MULTIANSWER
;
205 $qo->course
= $this->course
;
206 $qo->generalfeedback
= $this->getpath( $questions, array('#','generalfeedback',0,'#','text',0,'#'), '', true );
208 if (!empty($questions)) {
209 $qo->name
= $this->import_text( $questions['#']['name'][0]['#']['text'] );
216 * import true/false type question
217 * @param array question question array from xml tree
218 * @return object question object
220 function import_truefalse( $question ) {
222 $qo = $this->import_headers( $question );
224 // 'header' parts particular to true/false
225 $qo->qtype
= TRUEFALSE
;
229 // In the past, it used to be assumed that the two answers were in the file
230 // true first, then false. Howevever that was not always true. Now, we
231 // try to match on the answer text, but in old exports, this will be a localised
232 // string, so if we don't find true or false, we fall back to the old system.
235 foreach ($question['#']['answer'] as $answer) {
236 $answertext = $this->getpath( $answer, array('#','text',0,'#'), '', true );
237 $feedback = $this->getpath($answer, array('#','feedback',0,'#','text',0,'#'), '', true );
238 if ($answertext != 'true' && $answertext != 'false') {
240 $answertext = $first ?
'true' : 'false'; // Old style file, assume order is true/false.
242 if ($answertext == 'true') {
243 $qo->answer
= ($answer['@']['fraction'] == 100);
244 $qo->correctanswer
= $qo->answer
;
245 $qo->feedbacktrue
= $feedback;
247 $qo->answer
= ($answer['@']['fraction'] != 100);
248 $qo->correctanswer
= $qo->answer
;
249 $qo->feedbackfalse
= $feedback;
256 $a->questiontext
= $qo->questiontext
;
257 $a->answer
= get_string($qo->answer ?
'true' : 'false', 'quiz');
258 notify(get_string('truefalseimporterror', 'quiz', $a));
264 * import short answer type question
265 * @param array question question array from xml tree
266 * @return object question object
268 function import_shortanswer( $question ) {
270 $qo = $this->import_headers( $question );
272 // header parts particular to shortanswer
273 $qo->qtype
= SHORTANSWER
;
276 $qo->usecase
= $this->getpath($question, array('#','usecase',0,'#'), $qo->usecase
);
278 // run through the answers
279 $answers = $question['#']['answer'];
281 foreach ($answers as $answer) {
282 $ans = $this->import_answer( $answer );
283 $qo->answer
[$a_count] = $ans->answer
;
284 $qo->fraction
[$a_count] = $ans->fraction
;
285 $qo->feedback
[$a_count] = $ans->feedback
;
293 * import description type question
294 * @param array question question array from xml tree
295 * @return object question object
297 function import_description( $question ) {
299 $qo = $this->import_headers( $question );
300 // header parts particular to shortanswer
301 $qo->qtype
= DESCRIPTION
;
306 * import numerical type question
307 * @param array question question array from xml tree
308 * @return object question object
310 function import_numerical( $question ) {
312 $qo = $this->import_headers( $question );
314 // header parts particular to numerical
315 $qo->qtype
= NUMERICAL
;
318 $answers = $question['#']['answer'];
319 $qo->answer
= array();
320 $qo->feedback
= array();
321 $qo->fraction
= array();
322 $qo->tolerance
= array();
323 foreach ($answers as $answer) {
324 // answer outside of <text> is deprecated
325 $answertext = trim( $this->getpath( $answer, array('#',0), '' ) );
326 $qo->answer
[] = $this->getpath( $answer, array('#','text',0,'#'), $answertext, true );
327 if (empty($qo->answer
)) {
330 $qo->feedback
[] = $this->getpath( $answer, array('#','feedback',0,'#','text',0,'#'), '', true );
331 $qo->tolerance
[] = $this->getpath( $answer, array('#','tolerance',0,'#'), 0 );
333 // fraction as a tag is deprecated
334 $fraction = $this->getpath( $answer, array('@','fraction'), 0 ) / 100;
335 $qo->fraction
[] = $this->getpath( $answer, array('#','fraction',0,'#'), $fraction ); // deprecated
340 $units = $this->getpath( $question, array('#','units',0,'#','unit'), array() );
341 if (!empty($units)) {
342 $qo->multiplier
= array();
343 foreach ($units as $unit) {
344 $qo->multiplier
[] = $this->getpath( $unit, array('#','multiplier',0,'#'), 1 );
345 $qo->unit
[] = $this->getpath( $unit, array('#','unit_name',0,'#'), '', true );
352 * import matching type question
353 * @param array question question array from xml tree
354 * @return object question object
356 function import_matching( $question ) {
358 $qo = $this->import_headers( $question );
360 // header parts particular to matching
362 $qo->shuffleanswers
= $this->getpath( $question, array( '#','shuffleanswers',0,'#' ), 1 );
365 $subquestions = $question['#']['subquestion'];
366 $qo->subquestions
= array();
367 $qo->subanswers
= array();
369 // run through subquestions
370 foreach ($subquestions as $subquestion) {
371 $qo->subquestions
[] = $this->getpath( $subquestion, array('#','text',0,'#'), '', true );
372 $qo->subanswers
[] = $this->getpath( $subquestion, array('#','answer',0,'#','text',0,'#'), '', true);
378 * import essay type question
379 * @param array question question array from xml tree
380 * @return object question object
382 function import_essay( $question ) {
384 $qo = $this->import_headers( $question );
386 // header parts particular to essay
390 $qo->feedback
= $this->import_text( $question['#']['answer'][0]['#']['feedback'][0]['#']['text'] );
393 $answer = $question['#']['answer'][0];
395 // get fraction - <fraction> tag is deprecated
396 $qo->fraction
= $this->getpath( $question, array('@','fraction'), 0 ) / 100;
397 $q0->fraction
= $this->getpath( $question, array('#','fraction',0,'#'), $qo->fraction
);
402 function import_calculated( $question ) {
403 // import numerical question
406 $qo = $this->import_headers( $question );
408 // header parts particular to numerical
409 $qo->qtype
= CALCULATED
;//CALCULATED;
412 // echo "<pre> question";print_r($question);echo "</pre>";
413 $answers = $question['#']['answer'];
414 $qo->answers
= array();
415 $qo->feedback
= array();
416 $qo->fraction
= array();
417 $qo->tolerance
= array();
418 $qo->tolerancetype
= array();
419 $qo->correctanswerformat
= array();
420 $qo->correctanswerlength
= array();
421 $qo->feedback
= array();
422 foreach ($answers as $answer) {
423 // answer outside of <text> is deprecated
424 if (!empty( $answer['#']['text'] )) {
425 $answertext = $this->import_text( $answer['#']['text'] );
428 $answertext = trim($answer['#'][0]);
430 if ($answertext == '') {
431 $qo->answers
[] = '*';
433 $qo->answers
[] = $answertext;
435 $qo->feedback
[] = $this->import_text( $answer['#']['feedback'][0]['#']['text'] );
436 $qo->tolerance
[] = $answer['#']['tolerance'][0]['#'];
437 // fraction as a tag is deprecated
438 if (!empty($answer['#']['fraction'][0]['#'])) {
439 $qo->fraction
[] = $answer['#']['fraction'][0]['#'];
442 $qo->fraction
[] = $answer['@']['fraction'] / 100;
444 $qo->tolerancetype
[] = $answer['#']['tolerancetype'][0]['#'];
445 $qo->correctanswerformat
[] = $answer['#']['correctanswerformat'][0]['#'];
446 $qo->correctanswerlength
[] = $answer['#']['correctanswerlength'][0]['#'];
450 if (isset($question['#']['units'][0]['#']['unit'])) {
451 $units = $question['#']['units'][0]['#']['unit'];
452 $qo->multiplier
= array();
453 foreach ($units as $unit) {
454 $qo->multiplier
[] = $unit['#']['multiplier'][0]['#'];
455 $qo->unit
[] = $unit['#']['unit_name'][0]['#'];
458 $datasets = $question['#']['dataset_definitions'][0]['#']['dataset_definition'];
459 $qo->dataset
= array();
460 $qo->datasetindex
= 0 ;
461 foreach ($datasets as $dataset) {
463 $qo->dataset
[$qo->datasetindex
] = new stdClass();
464 $qo->dataset
[$qo->datasetindex
]->status
= $this->import_text( $dataset['#']['status'][0]['#']['text']);
465 $qo->dataset
[$qo->datasetindex
]->name
= $this->import_text( $dataset['#']['name'][0]['#']['text']);
466 $qo->dataset
[$qo->datasetindex
]->type
= $dataset['#']['type'][0]['#'];
467 $qo->dataset
[$qo->datasetindex
]->distribution
= $this->import_text( $dataset['#']['distribution'][0]['#']['text']);
468 $qo->dataset
[$qo->datasetindex
]->max
= $this->import_text( $dataset['#']['maximum'][0]['#']['text']);
469 $qo->dataset
[$qo->datasetindex
]->min
= $this->import_text( $dataset['#']['minimum'][0]['#']['text']);
470 $qo->dataset
[$qo->datasetindex
]->length
= $this->import_text( $dataset['#']['decimals'][0]['#']['text']);
471 $qo->dataset
[$qo->datasetindex
]->distribution
= $this->import_text( $dataset['#']['distribution'][0]['#']['text']);
472 $qo->dataset
[$qo->datasetindex
]->itemcount
= $dataset['#']['itemcount'][0]['#'];
473 $qo->dataset
[$qo->datasetindex
]->datasetitem
= array();
474 $qo->dataset
[$qo->datasetindex
]->itemindex
= 0;
475 $qo->dataset
[$qo->datasetindex
]->number_of_items
=$dataset['#']['number_of_items'][0]['#'];
476 $datasetitems = $dataset['#']['dataset_items'][0]['#']['dataset_item'];
477 foreach ($datasetitems as $datasetitem) {
478 $qo->dataset
[$qo->datasetindex
]->itemindex++
;
479 $qo->dataset
[$qo->datasetindex
]->datasetitem
[$qo->dataset
[$qo->datasetindex
]->itemindex
] = new stdClass();
480 $qo->dataset
[$qo->datasetindex
]->datasetitem
[$qo->dataset
[$qo->datasetindex
]->itemindex
]->itemnumber
= $datasetitem['#']['number'][0]['#']; //[0]['#']['number'][0]['#'] ; // [0]['numberitems'] ;//['#']['number'][0]['#'];// $datasetitems['#']['number'][0]['#'];
481 $qo->dataset
[$qo->datasetindex
]->datasetitem
[$qo->dataset
[$qo->datasetindex
]->itemindex
]->value
= $datasetitem['#']['value'][0]['#'] ;//$datasetitem['#']['value'][0]['#'];
485 // echo "<pre>loaded qo";print_r($qo);echo "</pre>";
491 * this is not a real question type. It's a dummy type used
492 * to specify the import category
494 * <question type="category">
495 * <category>tom/dick/harry</category>
498 function import_category( $question ) {
500 $qo->qtype
= 'category';
501 $qo->category
= $this->import_text($question['#']['category'][0]['#']['text']);
506 * parse the array of lines into an array of questions
507 * this *could* burn memory - but it won't happen that much
508 * so fingers crossed!
509 * @param array lines array of lines from the input file
510 * @return array (of objects) question objects
512 function readquestions($lines) {
513 // we just need it as one big string
514 $text = implode($lines, " ");
517 // this converts xml to big nasty data structure
518 // the 0 means keep white space as it is (important for markdown format)
519 // print_r it if you want to see what it looks like!
520 $xml = xmlize( $text, 0 );
522 // set up array to hold all our questions
523 $questions = array();
525 // iterate through questions
526 foreach ($xml['quiz']['#']['question'] as $question) {
527 $question_type = $question['@']['type'];
528 $questiontype = get_string( 'questiontype','quiz',$question_type );
530 if ($question_type=='multichoice') {
531 $qo = $this->import_multichoice( $question );
533 elseif ($question_type=='truefalse') {
534 $qo = $this->import_truefalse( $question );
536 elseif ($question_type=='shortanswer') {
537 $qo = $this->import_shortanswer( $question );
539 elseif ($question_type=='numerical') {
540 $qo = $this->import_numerical( $question );
542 elseif ($question_type=='description') {
543 $qo = $this->import_description( $question );
545 elseif ($question_type=='matching') {
546 $qo = $this->import_matching( $question );
548 elseif ($question_type=='cloze') {
549 $qo = $this->import_multianswer( $question );
551 elseif ($question_type=='essay') {
552 $qo = $this->import_essay( $question );
554 elseif ($question_type=='calculated') {
555 $qo = $this->import_calculated( $question );
557 elseif ($question_type=='category') {
558 $qo = $this->import_category( $question );
561 // try for plugin support
562 // no default question, as the plugin can call
563 // import_headers() itself if it wants to
564 if (!$qo=$this->try_importing_using_qtypes( $question )) {
565 $notsupported = get_string( 'xmltypeunsupported','quiz',$question_type );
566 $this->error( $notsupported );
571 // stick the result in the $questions array
579 // EXPORT FUNCTIONS START HERE
581 function export_file_extension() {
582 // override default type so extension is .xml
589 * Turn the internal question code into a human readable form
590 * (The code used to be numeric, but this remains as some of
591 * the names don't match the new internal format)
592 * @param mixed type_id Internal code
593 * @return string question type string
595 function get_qtype( $type_id ) {
601 $name = 'multichoice';
604 $name = 'shortanswer';
613 $name = 'description';
622 $name = 'calculated';
631 * Convert internal Moodle text format code into
632 * human readable form
633 * @param int id internal code
634 * @return string format text
636 function get_format( $id ) {
639 $name = "moodle_auto_format";
645 $name = "plain_text";
660 * Convert internal single question code into
661 * human readable form
662 * @param int id single question code
663 * @return string single question string
665 function get_single( $id ) {
680 * generates <text></text> tags, processing raw text therein
681 * @param int ilev the current indent level
682 * @param boolean short stick it on one line
683 * @return string formatted text
685 function writetext( $raw, $ilev=0, $short=true) {
686 $indent = str_repeat( " ",$ilev );
688 // encode the text to 'disguise' HTML content
689 $raw = htmlspecialchars( $raw );
692 $xml = "$indent<text>$raw</text>\n";
695 $xml = "$indent<text>\n$raw\n$indent</text>\n";
701 function xmltidy( $content ) {
702 // can only do this if tidy is installed
703 if (extension_loaded('tidy')) {
704 $config = array( 'input-xml'=>true, 'output-xml'=>true, 'indent'=>true, 'wrap'=>0 );
706 $tidy->parseString($content, $config, 'utf8');
707 $tidy->cleanRepair();
716 function presave_process( $content ) {
717 // override method to allow us to add xml headers and footers
719 // add the xml headers and footers
720 $content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" .
725 // make the xml look nice
726 $content = $this->xmltidy( $content );
732 * Include an image encoded in base 64
733 * @param string imagepath The location of the image file
734 * @return string xml code segment
736 function writeimage( $imagepath ) {
739 if (empty($imagepath)) {
743 $courseid = $this->course
->id
;
744 if (!$binary = file_get_contents( "{$CFG->dataroot}/$courseid/$imagepath" )) {
748 $content = " <image_base64>\n".addslashes(base64_encode( $binary ))."\n".
749 "\n </image_base64>\n";
754 * Turns question into an xml segment
755 * @param array question question array
756 * @return string xml segment
758 function writequestion( $question ) {
764 $expout .= "\n\n<!-- question: $question->id -->\n";
766 // check question type
767 if (!$question_type = $this->get_qtype( $question->qtype
)) {
768 // must be a plugin then, so just accept the name supplied
769 $question_type = $question->qtype
;
773 // generates specific header for Cloze and category type question
774 if ($question->qtype
== 'category') {
775 $categorypath = $this->writetext( $question->category
);
776 $expout .= " <question type=\"category\">\n";
777 $expout .= " <category>\n";
778 $expout .= " $categorypath\n";
779 $expout .= " </category>\n";
780 $expout .= " </question>\n";
783 elseif ($question->qtype
!= MULTIANSWER
) {
784 // for all question types except Close
785 $name_text = $this->writetext( $question->name
);
786 $qtformat = $this->get_format($question->questiontextformat
);
787 $question_text = $this->writetext( $question->questiontext
);
788 $generalfeedback = $this->writetext( $question->generalfeedback
);
789 $expout .= " <question type=\"$question_type\">\n";
790 $expout .= " <name>$name_text</name>\n";
791 $expout .= " <questiontext format=\"$qtformat\">\n";
792 $expout .= $question_text;
793 $expout .= " </questiontext>\n";
794 $expout .= " <image>{$question->image}</image>\n";
795 $expout .= $this->writeimage($question->image
);
796 $expout .= " <generalfeedback>\n";
797 $expout .= $generalfeedback;
798 $expout .= " </generalfeedback>\n";
799 $expout .= " <defaultgrade>{$question->defaultgrade}</defaultgrade>\n";
800 $expout .= " <penalty>{$question->penalty}</penalty>\n";
801 $expout .= " <hidden>{$question->hidden}</hidden>\n";
804 // for Cloze type only
805 $name_text = $this->writetext( $question->name
);
806 $question_text = $this->writetext( $question->questiontext
);
807 $generalfeedback = $this->writetext( $question->generalfeedback
);
808 $expout .= " <question type=\"$question_type\">\n";
809 $expout .= " <name>$name_text</name>\n";
810 $expout .= " <questiontext>\n";
811 $expout .= $question_text;
812 $expout .= " </questiontext>\n";
813 $expout .= " <generalfeedback>\n";
814 $expout .= $generalfeedback;
815 $expout .= " </generalfeedback>\n";
818 if (!empty($question->options
->shuffleanswers
)) {
819 $expout .= " <shuffleanswers>{$question->options->shuffleanswers}</shuffleanswers>\n";
822 $expout .= " <shuffleanswers>0</shuffleanswers>\n";
825 // output depends on question type
826 switch($question->qtype
) {
828 // not a qtype really - dummy used for category switching
831 foreach ($question->options
->answers
as $answer) {
832 $fraction_pc = round( $answer->fraction
* 100 );
833 if ($answer->id
== $question->options
->trueanswer
) {
834 $answertext = 'true';
836 $answertext = 'false';
838 $expout .= " <answer fraction=\"$fraction_pc\">\n";
839 $expout .= $this->writetext($answertext, 3) . "\n";
840 $expout .= " <feedback>\n";
841 $expout .= $this->writetext( $answer->feedback
,4,false );
842 $expout .= " </feedback>\n";
843 $expout .= " </answer>\n";
847 $expout .= " <single>".$this->get_single($question->options
->single
)."</single>\n";
848 $expout .= " <shuffleanswers>".$this->get_single($question->options
->shuffleanswers
)."</shuffleanswers>\n";
849 $expout .= " <correctfeedback>".$this->writetext($question->options
->correctfeedback
, 3)."</correctfeedback>\n";
850 $expout .= " <partiallycorrectfeedback>".$this->writetext($question->options
->partiallycorrectfeedback
, 3)."</partiallycorrectfeedback>\n";
851 $expout .= " <incorrectfeedback>".$this->writetext($question->options
->incorrectfeedback
, 3)."</incorrectfeedback>\n";
852 $expout .= " <answernumbering>{$question->options->answernumbering}</answernumbering>\n";
853 foreach($question->options
->answers
as $answer) {
854 $percent = $answer->fraction
* 100;
855 $expout .= " <answer fraction=\"$percent\">\n";
856 $expout .= $this->writetext( $answer->answer
,4,false );
857 $expout .= " <feedback>\n";
858 $expout .= $this->writetext( $answer->feedback
,5,false );
859 $expout .= " </feedback>\n";
860 $expout .= " </answer>\n";
864 $expout .= " <usecase>{$question->options->usecase}</usecase>\n ";
865 foreach($question->options
->answers
as $answer) {
866 $percent = 100 * $answer->fraction
;
867 $expout .= " <answer fraction=\"$percent\">\n";
868 $expout .= $this->writetext( $answer->answer
,3,false );
869 $expout .= " <feedback>\n";
870 $expout .= $this->writetext( $answer->feedback
,4,false );
871 $expout .= " </feedback>\n";
872 $expout .= " </answer>\n";
876 foreach ($question->options
->answers
as $answer) {
877 $tolerance = $answer->tolerance
;
878 $percent = 100 * $answer->fraction
;
879 $expout .= "<answer fraction=\"$percent\">\n";
880 // <text> tags are an added feature, old filed won't have them
881 $expout .= " <text>{$answer->answer}</text>\n";
882 $expout .= " <tolerance>$tolerance</tolerance>\n";
883 $expout .= " <feedback>".$this->writetext( $answer->feedback
)."</feedback>\n";
884 // fraction tag is deprecated
885 // $expout .= " <fraction>{$answer->fraction}</fraction>\n";
886 $expout .= "</answer>\n";
889 $units = $question->options
->units
;
891 $expout .= "<units>\n";
892 foreach ($units as $unit) {
893 $expout .= " <unit>\n";
894 $expout .= " <multiplier>{$unit->multiplier}</multiplier>\n";
895 $expout .= " <unit_name>{$unit->unit}</unit_name>\n";
896 $expout .= " </unit>\n";
898 $expout .= "</units>\n";
902 foreach($question->options
->subquestions
as $subquestion) {
903 $expout .= "<subquestion>\n";
904 $expout .= $this->writetext( $subquestion->questiontext
);
905 $expout .= "<answer>".$this->writetext( $subquestion->answertext
)."</answer>\n";
906 $expout .= "</subquestion>\n";
910 // nothing more to do for this type
914 foreach($question->options
->questions
as $question) {
915 $thispattern = addslashes("{#".$a_count."}");
916 $thisreplace = $question->questiontext
;
917 $expout=ereg_replace($thispattern, $thisreplace, $expout );
922 foreach ($question->options
->answers
as $answer) {
923 $percent = 100 * $answer->fraction
;
924 $expout .= "<answer fraction=\"$percent\">\n";
925 $expout .= " <feedback>".$this->writetext( $answer->feedback
)."</feedback>\n";
926 // fraction tag is deprecated
927 // $expout .= " <fraction>{$answer->fraction}</fraction>\n";
928 $expout .= "</answer>\n";
933 foreach ($question->options
->answers
as $answer) {
934 $tolerance = $answer->tolerance
;
935 $tolerancetype = $answer->tolerancetype
;
936 $correctanswerlength= $answer->correctanswerlength
;
937 $correctanswerformat= $answer->correctanswerformat
;
938 $percent = 100 * $answer->fraction
;
939 $expout .= "<answer fraction=\"$percent\">\n";
940 // "<text/>" tags are an added feature, old files won't have them
941 $expout .= " <text>{$answer->answer}</text>\n";
942 $expout .= " <tolerance>$tolerance</tolerance>\n";
943 $expout .= " <tolerancetype>$tolerancetype</tolerancetype>\n";
944 $expout .= " <correctanswerformat>$correctanswerformat</correctanswerformat>\n";
945 $expout .= " <correctanswerlength>$correctanswerformat</correctanswerlength>\n";
946 $expout .= " <feedback>".$this->writetext( $answer->feedback
)."</feedback>\n";
947 $expout .= "</answer>\n";
949 $units = $question->options
->units
;
951 $expout .= "<units>\n";
952 foreach ($units as $unit) {
953 $expout .= " <unit>\n";
954 $expout .= " <multiplier>{$unit->multiplier}</multiplier>\n";
955 $expout .= " <unit_name>{$unit->unit}</unit_name>\n";
956 $expout .= " </unit>\n";
958 $expout .= "</units>\n";
960 //echo "<pre> question calc";print_r($question);echo "</pre>";
961 //First, we a new function to get all the data itmes in the database
962 // $question_datasetdefs =$QTYPES['calculated']->get_datasets_for_export ($question);
963 // echo "<pre> question defs";print_r($question_datasetdefs);echo "</pre>";
964 //If there are question_datasets
965 if( isset($question->options
->datasets
)&&count($question->options
->datasets
)){// there should be
966 $expout .= "<dataset_definitions>\n";
967 foreach ($question->options
->datasets
as $def) {
968 $expout .= "<dataset_definition>\n";
969 $expout .= " <status>".$this->writetext($def->status
)."</status>\n";
970 $expout .= " <name>".$this->writetext($def->name
)."</name>\n";
971 $expout .= " <type>calculated</type>\n";
972 $expout .= " <distribution>".$this->writetext($def->distribution
)."</distribution>\n";
973 $expout .= " <minimum>".$this->writetext($def->minimum
)."</minimum>\n";
974 $expout .= " <maximum>".$this->writetext($def->maximum
)."</maximum>\n";
975 $expout .= " <decimals>".$this->writetext($def->decimals
)."</decimals>\n";
976 $expout .= " <itemcount>$def->itemcount</itemcount>\n";
977 if ($def->itemcount
> 0 ) {
978 $expout .= " <dataset_items>\n";
979 foreach ($def->items
as $item ){
980 $expout .= " <dataset_item>\n";
981 $expout .= " <number>".$item->itemnumber
."</number>\n";
982 $expout .= " <value>".$item->value
."</value>\n";
983 $expout .= " </dataset_item>\n";
985 $expout .= " </dataset_items>\n";
986 $expout .= " <number_of_items>".$def-> number_of_items
."</number_of_items>\n";
988 $expout .= "</dataset_definition>\n";
990 $expout .= "</dataset_definitions>\n";
994 // try support by optional plugin
995 if (!$data = $this->try_exporting_using_qtypes( $question->qtype
, $question )) {
996 error( "Unsupported question type $question->qtype" );
1001 // close the question tag
1002 $expout .= "</question>\n";