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 if (!is_string($xml)) {
109 $this->error( 'Invalid xml file - string expected (use CDATA?)' );
111 $xml = addslashes( trim( $xml ) );
119 * import parts of question common to all types
120 * @param array question question array from xml tree
121 * @return object question object
123 function import_headers( $question ) {
124 // get some error strings
125 $error_noname = get_string( 'xmlimportnoname','quiz' );
126 $error_noquestion = get_string( 'xmlimportnoquestion','quiz' );
128 // this routine initialises the question object
129 $qo = $this->defaultquestion();
132 $qo->name
= $this->getpath( $question, array('#','name',0,'#','text',0,'#'), '', true, $error_noname );
133 $qo->questiontext
= $this->getpath( $question, array('#','questiontext',0,'#','text',0,'#'), '', true );
134 $qo->questiontextformat
= $this->getpath( $question, array('#','questiontext',0,'@','format'), '' );
135 $image = $this->getpath( $question, array('#','image',0,'#'), $qo->image
);
136 $image_base64 = $this->getpath( $question, array('#','image_base64','0','#'),'' );
137 if (!empty($image_base64)) {
138 $qo->image
= $this->importimagefile( $image, stripslashes($image_base64) );
140 $qo->generalfeedback
= $this->getpath( $question, array('#','generalfeedback',0,'#','text',0,'#'), $qo->generalfeedback
, true );
141 $qo->defaultgrade
= $this->getpath( $question, array('#','defaultgrade',0,'#'), $qo->defaultgrade
);
142 $qo->penalty
= $this->getpath( $question, array('#','penalty',0,'#'), $qo->penalty
);
148 * import the common parts of a single answer
149 * @param array answer xml tree for single answer
150 * @return object answer object
152 function import_answer( $answer ) {
153 $fraction = $this->getpath( $answer, array('@','fraction'),0 );
154 $text = $this->getpath( $answer, array('#','text',0,'#'), '', true );
155 $feedback = $this->getpath( $answer, array('#','feedback',0,'#','text',0,'#'), '', true );
158 $ans->answer
= $text;
159 $ans->fraction
= $fraction / 100;
160 $ans->feedback
= $feedback;
165 * import multiple choice question
166 * @param array question question array from xml tree
167 * @return object question object
169 function import_multichoice( $question ) {
171 $qo = $this->import_headers( $question );
173 // 'header' parts particular to multichoice
174 $qo->qtype
= MULTICHOICE
;
175 $single = $this->getpath( $question, array('#','single',0,'#'), 'true' );
176 $qo->single
= $this->trans_single( $single );
177 $shuffleanswers = $this->getpath( $question, array('#','shuffleanswers',0,'#'), 'false' );
178 $qo->answernumbering
= $this->getpath( $question, array('#','answernumbering',0,'#'), 'abc' );
179 $qo->shuffleanswers
= $this->trans_single($shuffleanswers);
180 $qo->correctfeedback
= $this->getpath( $question, array('#','correctfeedback',0,'#','text',0,'#'), '', true );
181 $qo->partiallycorrectfeedback
= $this->getpath( $question, array('#','partiallycorrectfeedback',0,'#','text',0,'#'), '', true );
182 $qo->incorrectfeedback
= $this->getpath( $question, array('#','incorrectfeedback',0,'#','text',0,'#'), '', true );
184 // run through the answers
185 $answers = $question['#']['answer'];
187 foreach ($answers as $answer) {
188 $ans = $this->import_answer( $answer );
189 $qo->answer
[$a_count] = $ans->answer
;
190 $qo->fraction
[$a_count] = $ans->fraction
;
191 $qo->feedback
[$a_count] = $ans->feedback
;
198 * import cloze type question
199 * @param array question question array from xml tree
200 * @return object question object
202 function import_multianswer( $questions ) {
203 $questiontext = $questions['#']['questiontext'][0]['#']['text'];
204 $qo = qtype_multianswer_extract_question($this->import_text($questiontext));
206 // 'header' parts particular to multianswer
207 $qo->qtype
= MULTIANSWER
;
208 $qo->course
= $this->course
;
209 $qo->generalfeedback
= $this->getpath( $questions, array('#','generalfeedback',0,'#','text',0,'#'), '', true );
211 if (!empty($questions)) {
212 $qo->name
= $this->import_text( $questions['#']['name'][0]['#']['text'] );
219 * import true/false type question
220 * @param array question question array from xml tree
221 * @return object question object
223 function import_truefalse( $question ) {
225 $qo = $this->import_headers( $question );
227 // 'header' parts particular to true/false
228 $qo->qtype
= TRUEFALSE
;
232 // In the past, it used to be assumed that the two answers were in the file
233 // true first, then false. Howevever that was not always true. Now, we
234 // try to match on the answer text, but in old exports, this will be a localised
235 // string, so if we don't find true or false, we fall back to the old system.
238 foreach ($question['#']['answer'] as $answer) {
239 $answertext = $this->getpath( $answer, array('#','text',0,'#'), '', true );
240 $feedback = $this->getpath($answer, array('#','feedback',0,'#','text',0,'#'), '', true );
241 if ($answertext != 'true' && $answertext != 'false') {
243 $answertext = $first ?
'true' : 'false'; // Old style file, assume order is true/false.
245 if ($answertext == 'true') {
246 $qo->answer
= ($answer['@']['fraction'] == 100);
247 $qo->correctanswer
= $qo->answer
;
248 $qo->feedbacktrue
= $feedback;
250 $qo->answer
= ($answer['@']['fraction'] != 100);
251 $qo->correctanswer
= $qo->answer
;
252 $qo->feedbackfalse
= $feedback;
259 $a->questiontext
= $qo->questiontext
;
260 $a->answer
= get_string($qo->answer ?
'true' : 'false', 'quiz');
261 notify(get_string('truefalseimporterror', 'quiz', $a));
267 * import short answer type question
268 * @param array question question array from xml tree
269 * @return object question object
271 function import_shortanswer( $question ) {
273 $qo = $this->import_headers( $question );
275 // header parts particular to shortanswer
276 $qo->qtype
= SHORTANSWER
;
279 $qo->usecase
= $this->getpath($question, array('#','usecase',0,'#'), $qo->usecase
);
281 // run through the answers
282 $answers = $question['#']['answer'];
284 foreach ($answers as $answer) {
285 $ans = $this->import_answer( $answer );
286 $qo->answer
[$a_count] = $ans->answer
;
287 $qo->fraction
[$a_count] = $ans->fraction
;
288 $qo->feedback
[$a_count] = $ans->feedback
;
296 * import description type question
297 * @param array question question array from xml tree
298 * @return object question object
300 function import_description( $question ) {
302 $qo = $this->import_headers( $question );
303 // header parts particular to shortanswer
304 $qo->qtype
= DESCRIPTION
;
305 $qo->defaultgrade
= 0;
311 * import numerical type question
312 * @param array question question array from xml tree
313 * @return object question object
315 function import_numerical( $question ) {
317 $qo = $this->import_headers( $question );
319 // header parts particular to numerical
320 $qo->qtype
= NUMERICAL
;
323 $answers = $question['#']['answer'];
324 $qo->answer
= array();
325 $qo->feedback
= array();
326 $qo->fraction
= array();
327 $qo->tolerance
= array();
328 foreach ($answers as $answer) {
329 // answer outside of <text> is deprecated
330 $answertext = trim( $this->getpath( $answer, array('#',0), '' ) );
331 $qo->answer
[] = $this->getpath( $answer, array('#','text',0,'#'), $answertext, true );
332 if (empty($qo->answer
)) {
335 $qo->feedback
[] = $this->getpath( $answer, array('#','feedback',0,'#','text',0,'#'), '', true );
336 $qo->tolerance
[] = $this->getpath( $answer, array('#','tolerance',0,'#'), 0 );
338 // fraction as a tag is deprecated
339 $fraction = $this->getpath( $answer, array('@','fraction'), 0 ) / 100;
340 $qo->fraction
[] = $this->getpath( $answer, array('#','fraction',0,'#'), $fraction ); // deprecated
345 $units = $this->getpath( $question, array('#','units',0,'#','unit'), array() );
346 if (!empty($units)) {
347 $qo->multiplier
= array();
348 foreach ($units as $unit) {
349 $qo->multiplier
[] = $this->getpath( $unit, array('#','multiplier',0,'#'), 1 );
350 $qo->unit
[] = $this->getpath( $unit, array('#','unit_name',0,'#'), '', true );
357 * import matching type question
358 * @param array question question array from xml tree
359 * @return object question object
361 function import_matching( $question ) {
363 $qo = $this->import_headers( $question );
365 // header parts particular to matching
367 $qo->shuffleanswers
= $this->getpath( $question, array( '#','shuffleanswers',0,'#' ), 1 );
370 $subquestions = $question['#']['subquestion'];
371 $qo->subquestions
= array();
372 $qo->subanswers
= array();
374 // run through subquestions
375 foreach ($subquestions as $subquestion) {
376 $qo->subquestions
[] = $this->getpath( $subquestion, array('#','text',0,'#'), '', true );
377 $qo->subanswers
[] = $this->getpath( $subquestion, array('#','answer',0,'#','text',0,'#'), '', true);
383 * import essay type question
384 * @param array question question array from xml tree
385 * @return object question object
387 function import_essay( $question ) {
389 $qo = $this->import_headers( $question );
391 // header parts particular to essay
395 $qo->feedback
= $this->import_text( $question['#']['answer'][0]['#']['feedback'][0]['#']['text'] );
398 $answer = $question['#']['answer'][0];
400 // get fraction - <fraction> tag is deprecated
401 $qo->fraction
= $this->getpath( $question, array('@','fraction'), 0 ) / 100;
402 $q0->fraction
= $this->getpath( $question, array('#','fraction',0,'#'), $qo->fraction
);
407 function import_calculated( $question ) {
408 // import numerical question
411 $qo = $this->import_headers( $question );
413 // header parts particular to numerical
414 $qo->qtype
= CALCULATED
;//CALCULATED;
417 // echo "<pre> question";print_r($question);echo "</pre>";
418 $answers = $question['#']['answer'];
419 $qo->answers
= array();
420 $qo->feedback
= array();
421 $qo->fraction
= array();
422 $qo->tolerance
= array();
423 $qo->tolerancetype
= array();
424 $qo->correctanswerformat
= array();
425 $qo->correctanswerlength
= array();
426 $qo->feedback
= array();
427 foreach ($answers as $answer) {
428 // answer outside of <text> is deprecated
429 if (!empty( $answer['#']['text'] )) {
430 $answertext = $this->import_text( $answer['#']['text'] );
433 $answertext = trim($answer['#'][0]);
435 if ($answertext == '') {
436 $qo->answers
[] = '*';
438 $qo->answers
[] = $answertext;
440 $qo->feedback
[] = $this->import_text( $answer['#']['feedback'][0]['#']['text'] );
441 $qo->tolerance
[] = $answer['#']['tolerance'][0]['#'];
442 // fraction as a tag is deprecated
443 if (!empty($answer['#']['fraction'][0]['#'])) {
444 $qo->fraction
[] = $answer['#']['fraction'][0]['#'];
447 $qo->fraction
[] = $answer['@']['fraction'] / 100;
449 $qo->tolerancetype
[] = $answer['#']['tolerancetype'][0]['#'];
450 $qo->correctanswerformat
[] = $answer['#']['correctanswerformat'][0]['#'];
451 $qo->correctanswerlength
[] = $answer['#']['correctanswerlength'][0]['#'];
455 if (isset($question['#']['units'][0]['#']['unit'])) {
456 $units = $question['#']['units'][0]['#']['unit'];
457 $qo->multiplier
= array();
458 foreach ($units as $unit) {
459 $qo->multiplier
[] = $unit['#']['multiplier'][0]['#'];
460 $qo->unit
[] = $unit['#']['unit_name'][0]['#'];
463 $datasets = $question['#']['dataset_definitions'][0]['#']['dataset_definition'];
464 $qo->dataset
= array();
465 $qo->datasetindex
= 0 ;
466 foreach ($datasets as $dataset) {
468 $qo->dataset
[$qo->datasetindex
] = new stdClass();
469 $qo->dataset
[$qo->datasetindex
]->status
= $this->import_text( $dataset['#']['status'][0]['#']['text']);
470 $qo->dataset
[$qo->datasetindex
]->name
= $this->import_text( $dataset['#']['name'][0]['#']['text']);
471 $qo->dataset
[$qo->datasetindex
]->type
= $dataset['#']['type'][0]['#'];
472 $qo->dataset
[$qo->datasetindex
]->distribution
= $this->import_text( $dataset['#']['distribution'][0]['#']['text']);
473 $qo->dataset
[$qo->datasetindex
]->max
= $this->import_text( $dataset['#']['maximum'][0]['#']['text']);
474 $qo->dataset
[$qo->datasetindex
]->min
= $this->import_text( $dataset['#']['minimum'][0]['#']['text']);
475 $qo->dataset
[$qo->datasetindex
]->length
= $this->import_text( $dataset['#']['decimals'][0]['#']['text']);
476 $qo->dataset
[$qo->datasetindex
]->distribution
= $this->import_text( $dataset['#']['distribution'][0]['#']['text']);
477 $qo->dataset
[$qo->datasetindex
]->itemcount
= $dataset['#']['itemcount'][0]['#'];
478 $qo->dataset
[$qo->datasetindex
]->datasetitem
= array();
479 $qo->dataset
[$qo->datasetindex
]->itemindex
= 0;
480 $qo->dataset
[$qo->datasetindex
]->number_of_items
=$dataset['#']['number_of_items'][0]['#'];
481 $datasetitems = $dataset['#']['dataset_items'][0]['#']['dataset_item'];
482 foreach ($datasetitems as $datasetitem) {
483 $qo->dataset
[$qo->datasetindex
]->itemindex++
;
484 $qo->dataset
[$qo->datasetindex
]->datasetitem
[$qo->dataset
[$qo->datasetindex
]->itemindex
] = new stdClass();
485 $qo->dataset
[$qo->datasetindex
]->datasetitem
[$qo->dataset
[$qo->datasetindex
]->itemindex
]->itemnumber
= $datasetitem['#']['number'][0]['#']; //[0]['#']['number'][0]['#'] ; // [0]['numberitems'] ;//['#']['number'][0]['#'];// $datasetitems['#']['number'][0]['#'];
486 $qo->dataset
[$qo->datasetindex
]->datasetitem
[$qo->dataset
[$qo->datasetindex
]->itemindex
]->value
= $datasetitem['#']['value'][0]['#'] ;//$datasetitem['#']['value'][0]['#'];
490 // echo "<pre>loaded qo";print_r($qo);echo "</pre>";
496 * this is not a real question type. It's a dummy type used
497 * to specify the import category
499 * <question type="category">
500 * <category>tom/dick/harry</category>
503 function import_category( $question ) {
505 $qo->qtype
= 'category';
506 $qo->category
= $this->import_text($question['#']['category'][0]['#']['text']);
511 * parse the array of lines into an array of questions
512 * this *could* burn memory - but it won't happen that much
513 * so fingers crossed!
514 * @param array lines array of lines from the input file
515 * @return array (of objects) question objects
517 function readquestions($lines) {
518 // we just need it as one big string
519 $text = implode($lines, " ");
522 // this converts xml to big nasty data structure
523 // the 0 means keep white space as it is (important for markdown format)
524 // print_r it if you want to see what it looks like!
525 $xml = xmlize( $text, 0 );
527 // set up array to hold all our questions
528 $questions = array();
530 // iterate through questions
531 foreach ($xml['quiz']['#']['question'] as $question) {
532 $question_type = $question['@']['type'];
533 $questiontype = get_string( 'questiontype','quiz',$question_type );
535 if ($question_type=='multichoice') {
536 $qo = $this->import_multichoice( $question );
538 elseif ($question_type=='truefalse') {
539 $qo = $this->import_truefalse( $question );
541 elseif ($question_type=='shortanswer') {
542 $qo = $this->import_shortanswer( $question );
544 elseif ($question_type=='numerical') {
545 $qo = $this->import_numerical( $question );
547 elseif ($question_type=='description') {
548 $qo = $this->import_description( $question );
550 elseif ($question_type=='matching') {
551 $qo = $this->import_matching( $question );
553 elseif ($question_type=='cloze') {
554 $qo = $this->import_multianswer( $question );
556 elseif ($question_type=='essay') {
557 $qo = $this->import_essay( $question );
559 elseif ($question_type=='calculated') {
560 $qo = $this->import_calculated( $question );
562 elseif ($question_type=='category') {
563 $qo = $this->import_category( $question );
566 // try for plugin support
567 // no default question, as the plugin can call
568 // import_headers() itself if it wants to
569 if (!$qo=$this->try_importing_using_qtypes( $question )) {
570 $notsupported = get_string( 'xmltypeunsupported','quiz',$question_type );
571 $this->error( $notsupported );
576 // stick the result in the $questions array
584 // EXPORT FUNCTIONS START HERE
586 function export_file_extension() {
587 // override default type so extension is .xml
594 * Turn the internal question code into a human readable form
595 * (The code used to be numeric, but this remains as some of
596 * the names don't match the new internal format)
597 * @param mixed type_id Internal code
598 * @return string question type string
600 function get_qtype( $type_id ) {
606 $name = 'multichoice';
609 $name = 'shortanswer';
618 $name = 'description';
627 $name = 'calculated';
636 * Convert internal Moodle text format code into
637 * human readable form
638 * @param int id internal code
639 * @return string format text
641 function get_format( $id ) {
644 $name = "moodle_auto_format";
650 $name = "plain_text";
665 * Convert internal single question code into
666 * human readable form
667 * @param int id single question code
668 * @return string single question string
670 function get_single( $id ) {
685 * generates <text></text> tags, processing raw text therein
686 * @param int ilev the current indent level
687 * @param boolean short stick it on one line
688 * @return string formatted text
690 function writetext( $raw, $ilev=0, $short=true) {
691 $indent = str_repeat( " ",$ilev );
693 // encode the text to 'disguise' HTML content
694 $raw = htmlspecialchars( $raw );
697 $xml = "$indent<text>$raw</text>\n";
700 $xml = "$indent<text>\n$raw\n$indent</text>\n";
706 function xmltidy( $content ) {
707 // can only do this if tidy is installed
708 if (extension_loaded('tidy')) {
709 $config = array( 'input-xml'=>true, 'output-xml'=>true, 'indent'=>true, 'wrap'=>0 );
711 $tidy->parseString($content, $config, 'utf8');
712 $tidy->cleanRepair();
721 function presave_process( $content ) {
722 // override method to allow us to add xml headers and footers
724 // add the xml headers and footers
725 $content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" .
730 // make the xml look nice
731 $content = $this->xmltidy( $content );
737 * Include an image encoded in base 64
738 * @param string imagepath The location of the image file
739 * @return string xml code segment
741 function writeimage( $imagepath ) {
744 if (empty($imagepath)) {
748 $courseid = $this->course
->id
;
749 if (!$binary = file_get_contents( "{$CFG->dataroot}/$courseid/$imagepath" )) {
753 $content = " <image_base64>\n".addslashes(base64_encode( $binary ))."\n".
754 "\n </image_base64>\n";
759 * Turns question into an xml segment
760 * @param array question question array
761 * @return string xml segment
763 function writequestion( $question ) {
769 $expout .= "\n\n<!-- question: $question->id -->\n";
771 // check question type
772 if (!$question_type = $this->get_qtype( $question->qtype
)) {
773 // must be a plugin then, so just accept the name supplied
774 $question_type = $question->qtype
;
778 // generates specific header for Cloze and category type question
779 if ($question->qtype
== 'category') {
780 $categorypath = $this->writetext( $question->category
);
781 $expout .= " <question type=\"category\">\n";
782 $expout .= " <category>\n";
783 $expout .= " $categorypath\n";
784 $expout .= " </category>\n";
785 $expout .= " </question>\n";
788 elseif ($question->qtype
!= MULTIANSWER
) {
789 // for all question types except Close
790 $name_text = $this->writetext( $question->name
);
791 $qtformat = $this->get_format($question->questiontextformat
);
792 $question_text = $this->writetext( $question->questiontext
);
793 $generalfeedback = $this->writetext( $question->generalfeedback
);
794 $expout .= " <question type=\"$question_type\">\n";
795 $expout .= " <name>$name_text</name>\n";
796 $expout .= " <questiontext format=\"$qtformat\">\n";
797 $expout .= $question_text;
798 $expout .= " </questiontext>\n";
799 $expout .= " <image>{$question->image}</image>\n";
800 $expout .= $this->writeimage($question->image
);
801 $expout .= " <generalfeedback>\n";
802 $expout .= $generalfeedback;
803 $expout .= " </generalfeedback>\n";
804 $expout .= " <defaultgrade>{$question->defaultgrade}</defaultgrade>\n";
805 $expout .= " <penalty>{$question->penalty}</penalty>\n";
806 $expout .= " <hidden>{$question->hidden}</hidden>\n";
809 // for Cloze type only
810 $name_text = $this->writetext( $question->name
);
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>\n";
816 $expout .= $question_text;
817 $expout .= " </questiontext>\n";
818 $expout .= " <generalfeedback>\n";
819 $expout .= $generalfeedback;
820 $expout .= " </generalfeedback>\n";
823 if (!empty($question->options
->shuffleanswers
)) {
824 $expout .= " <shuffleanswers>{$question->options->shuffleanswers}</shuffleanswers>\n";
827 $expout .= " <shuffleanswers>0</shuffleanswers>\n";
830 // output depends on question type
831 switch($question->qtype
) {
833 // not a qtype really - dummy used for category switching
836 foreach ($question->options
->answers
as $answer) {
837 $fraction_pc = round( $answer->fraction
* 100 );
838 if ($answer->id
== $question->options
->trueanswer
) {
839 $answertext = 'true';
841 $answertext = 'false';
843 $expout .= " <answer fraction=\"$fraction_pc\">\n";
844 $expout .= $this->writetext($answertext, 3) . "\n";
845 $expout .= " <feedback>\n";
846 $expout .= $this->writetext( $answer->feedback
,4,false );
847 $expout .= " </feedback>\n";
848 $expout .= " </answer>\n";
852 $expout .= " <single>".$this->get_single($question->options
->single
)."</single>\n";
853 $expout .= " <shuffleanswers>".$this->get_single($question->options
->shuffleanswers
)."</shuffleanswers>\n";
854 $expout .= " <correctfeedback>".$this->writetext($question->options
->correctfeedback
, 3)."</correctfeedback>\n";
855 $expout .= " <partiallycorrectfeedback>".$this->writetext($question->options
->partiallycorrectfeedback
, 3)."</partiallycorrectfeedback>\n";
856 $expout .= " <incorrectfeedback>".$this->writetext($question->options
->incorrectfeedback
, 3)."</incorrectfeedback>\n";
857 $expout .= " <answernumbering>{$question->options->answernumbering}</answernumbering>\n";
858 foreach($question->options
->answers
as $answer) {
859 $percent = $answer->fraction
* 100;
860 $expout .= " <answer fraction=\"$percent\">\n";
861 $expout .= $this->writetext( $answer->answer
,4,false );
862 $expout .= " <feedback>\n";
863 $expout .= $this->writetext( $answer->feedback
,5,false );
864 $expout .= " </feedback>\n";
865 $expout .= " </answer>\n";
869 $expout .= " <usecase>{$question->options->usecase}</usecase>\n ";
870 foreach($question->options
->answers
as $answer) {
871 $percent = 100 * $answer->fraction
;
872 $expout .= " <answer fraction=\"$percent\">\n";
873 $expout .= $this->writetext( $answer->answer
,3,false );
874 $expout .= " <feedback>\n";
875 $expout .= $this->writetext( $answer->feedback
,4,false );
876 $expout .= " </feedback>\n";
877 $expout .= " </answer>\n";
881 foreach ($question->options
->answers
as $answer) {
882 $tolerance = $answer->tolerance
;
883 $percent = 100 * $answer->fraction
;
884 $expout .= "<answer fraction=\"$percent\">\n";
885 // <text> tags are an added feature, old filed won't have them
886 $expout .= " <text>{$answer->answer}</text>\n";
887 $expout .= " <tolerance>$tolerance</tolerance>\n";
888 $expout .= " <feedback>".$this->writetext( $answer->feedback
)."</feedback>\n";
889 // fraction tag is deprecated
890 // $expout .= " <fraction>{$answer->fraction}</fraction>\n";
891 $expout .= "</answer>\n";
894 $units = $question->options
->units
;
896 $expout .= "<units>\n";
897 foreach ($units as $unit) {
898 $expout .= " <unit>\n";
899 $expout .= " <multiplier>{$unit->multiplier}</multiplier>\n";
900 $expout .= " <unit_name>{$unit->unit}</unit_name>\n";
901 $expout .= " </unit>\n";
903 $expout .= "</units>\n";
907 foreach($question->options
->subquestions
as $subquestion) {
908 $expout .= "<subquestion>\n";
909 $expout .= $this->writetext( $subquestion->questiontext
);
910 $expout .= "<answer>".$this->writetext( $subquestion->answertext
)."</answer>\n";
911 $expout .= "</subquestion>\n";
915 // nothing more to do for this type
919 foreach($question->options
->questions
as $question) {
920 $thispattern = addslashes("{#".$a_count."}");
921 $thisreplace = $question->questiontext
;
922 $expout=ereg_replace($thispattern, $thisreplace, $expout );
927 foreach ($question->options
->answers
as $answer) {
928 $percent = 100 * $answer->fraction
;
929 $expout .= "<answer fraction=\"$percent\">\n";
930 $expout .= " <feedback>".$this->writetext( $answer->feedback
)."</feedback>\n";
931 // fraction tag is deprecated
932 // $expout .= " <fraction>{$answer->fraction}</fraction>\n";
933 $expout .= "</answer>\n";
938 foreach ($question->options
->answers
as $answer) {
939 $tolerance = $answer->tolerance
;
940 $tolerancetype = $answer->tolerancetype
;
941 $correctanswerlength= $answer->correctanswerlength
;
942 $correctanswerformat= $answer->correctanswerformat
;
943 $percent = 100 * $answer->fraction
;
944 $expout .= "<answer fraction=\"$percent\">\n";
945 // "<text/>" tags are an added feature, old files won't have them
946 $expout .= " <text>{$answer->answer}</text>\n";
947 $expout .= " <tolerance>$tolerance</tolerance>\n";
948 $expout .= " <tolerancetype>$tolerancetype</tolerancetype>\n";
949 $expout .= " <correctanswerformat>$correctanswerformat</correctanswerformat>\n";
950 $expout .= " <correctanswerlength>$correctanswerformat</correctanswerlength>\n";
951 $expout .= " <feedback>".$this->writetext( $answer->feedback
)."</feedback>\n";
952 $expout .= "</answer>\n";
954 $units = $question->options
->units
;
956 $expout .= "<units>\n";
957 foreach ($units as $unit) {
958 $expout .= " <unit>\n";
959 $expout .= " <multiplier>{$unit->multiplier}</multiplier>\n";
960 $expout .= " <unit_name>{$unit->unit}</unit_name>\n";
961 $expout .= " </unit>\n";
963 $expout .= "</units>\n";
965 //echo "<pre> question calc";print_r($question);echo "</pre>";
966 //First, we a new function to get all the data itmes in the database
967 // $question_datasetdefs =$QTYPES['calculated']->get_datasets_for_export ($question);
968 // echo "<pre> question defs";print_r($question_datasetdefs);echo "</pre>";
969 //If there are question_datasets
970 if( isset($question->options
->datasets
)&&count($question->options
->datasets
)){// there should be
971 $expout .= "<dataset_definitions>\n";
972 foreach ($question->options
->datasets
as $def) {
973 $expout .= "<dataset_definition>\n";
974 $expout .= " <status>".$this->writetext($def->status
)."</status>\n";
975 $expout .= " <name>".$this->writetext($def->name
)."</name>\n";
976 $expout .= " <type>calculated</type>\n";
977 $expout .= " <distribution>".$this->writetext($def->distribution
)."</distribution>\n";
978 $expout .= " <minimum>".$this->writetext($def->minimum
)."</minimum>\n";
979 $expout .= " <maximum>".$this->writetext($def->maximum
)."</maximum>\n";
980 $expout .= " <decimals>".$this->writetext($def->decimals
)."</decimals>\n";
981 $expout .= " <itemcount>$def->itemcount</itemcount>\n";
982 if ($def->itemcount
> 0 ) {
983 $expout .= " <dataset_items>\n";
984 foreach ($def->items
as $item ){
985 $expout .= " <dataset_item>\n";
986 $expout .= " <number>".$item->itemnumber
."</number>\n";
987 $expout .= " <value>".$item->value
."</value>\n";
988 $expout .= " </dataset_item>\n";
990 $expout .= " </dataset_items>\n";
991 $expout .= " <number_of_items>".$def-> number_of_items
."</number_of_items>\n";
993 $expout .= "</dataset_definition>\n";
995 $expout .= "</dataset_definitions>\n";
999 // try support by optional plugin
1000 if (!$data = $this->try_exporting_using_qtypes( $question->qtype
, $question )) {
1001 error( "Unsupported question type $question->qtype" );
1006 // close the question tag
1007 $expout .= "</question>\n";