Merege from stable. New strings for xml import error messages.
[moodle.git] / question / format / xml / format.php
CommitLineData
84769fd8 1<?php // $Id$
2//
3///////////////////////////////////////////////////////////////
4// XML import/export
5//
6//////////////////////////////////////////////////////////////////////////
7// Based on default.php, included by ../import.php
41a89a07 8/**
9 * @package questionbank
10 * @subpackage importexport
11 */
84769fd8 12require_once( "$CFG->libdir/xmlize.php" );
13
f5565b69 14class qformat_xml extends qformat_default {
84769fd8 15
16 function provide_import() {
17 return true;
18 }
19
20 function provide_export() {
21 return true;
22 }
23
24 // IMPORT FUNCTIONS START HERE
25
6e557c08 26 /**
c81415c7 27 * Translate human readable format name
28 * into internal Moodle code number
6e557c08 29 * @param string name format name from xml file
30 * @return int Moodle format code
c81415c7 31 */
84769fd8 32 function trans_format( $name ) {
84769fd8 33 $name = trim($name);
34
35 if ($name=='moodle_auto_format') {
36 $id = 0;
37 }
38 elseif ($name=='html') {
39 $id = 1;
40 }
41 elseif ($name=='plain_text') {
42 $id = 2;
43 }
44 elseif ($name=='wiki_like') {
45 $id = 3;
46 }
47 elseif ($name=='markdown') {
48 $id = 4;
49 }
50 else {
51 $id = 0; // or maybe warning required
52 }
53 return $id;
54 }
55
6e557c08 56 /**
c81415c7 57 * Translate human readable single answer option
58 * to internal code number
6e557c08 59 * @param string name true/false
60 * @return int internal code number
c81415c7 61 */
84769fd8 62 function trans_single( $name ) {
2da44816 63 $name = trim($name);
64 if ($name == "false" || !$name) {
65 return 0;
66 } else {
67 return 1;
68 }
84769fd8 69 }
70
6e557c08 71 /**
c81415c7 72 * process text string from xml file
6e557c08 73 * @param array $text bit of xml tree after ['text']
74 * @return string processed text
c81415c7 75 */
84769fd8 76 function import_text( $text ) {
17102269 77 // quick sanity check
78 if (empty($text)) {
79 return '';
80 }
84769fd8 81 $data = $text[0]['#'];
84769fd8 82 return addslashes(trim( $data ));
83 }
84
307f045f 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.
91 */
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 '';
97 }
98 }
99
6e557c08 100 /**
c81415c7 101 * import parts of question common to all types
6e557c08 102 * @param array question question array from xml tree
103 * @return object question object
c81415c7 104 */
84769fd8 105 function import_headers( $question ) {
84769fd8 106 // this routine initialises the question object
5bed54e1 107 $qo = $this->defaultquestion();
84769fd8 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]['#'];
d08e16b2 112 if (!empty($question['#']['image_base64'][0]['#'])) {
113 $image_base64 = stripslashes( trim( $question['#']['image_base64'][0]['#'] ) );
114 $image = $this->importimagefile( $image, $image_base64 );
115 }
a4514d91 116 if (array_key_exists('generalfeedback', $question['#'])) {
117 $generalfeedback = $this->import_text( $question['#']['generalfeedback'][0]['#']['text'] );
1b8a7434 118 } else {
a4514d91 119 $generalfeedback = '';
1b8a7434 120 }
c81415c7 121 if (!empty($question['#']['defaultgrade'][0]['#'])) {
122 $qo->defaultgrade = $question['#']['defaultgrade'][0]['#'];
123 }
5bed54e1 124
84769fd8 125 $penalty = $question['#']['penalty'][0]['#'];
126
84769fd8 127 $qo->name = $name;
128 $qo->questiontext = $qtext;
129 $qo->questiontextformat = $this->trans_format( $qformat );
130 $qo->image = ((!empty($image)) ? $image : '');
a4514d91 131 $qo->generalfeedback = $generalfeedback;
84769fd8 132 $qo->penalty = $penalty;
133
134 return $qo;
135 }
136
6e557c08 137 /**
c81415c7 138 * import the common parts of a single answer
6e557c08 139 * @param array answer xml tree for single answer
140 * @return object answer object
c81415c7 141 */
84769fd8 142 function import_answer( $answer ) {
84769fd8 143 $fraction = $answer['@']['fraction'];
144 $text = $this->import_text( $answer['#']['text']);
145 $feedback = $this->import_text( $answer['#']['feedback'][0]['#']['text'] );
146
147 $ans = null;
148 $ans->answer = $text;
149 $ans->fraction = $fraction / 100;
150 $ans->feedback = $feedback;
151
152 return $ans;
153 }
154
6e557c08 155 /**
c81415c7 156 * import multiple choice question
6e557c08 157 * @param array question question array from xml tree
158 * @return object question object
c81415c7 159 */
84769fd8 160 function import_multichoice( $question ) {
84769fd8 161 // get common parts
162 $qo = $this->import_headers( $question );
163
164 // 'header' parts particular to multichoice
165 $qo->qtype = MULTICHOICE;
166 $single = $question['#']['single'][0]['#'];
167 $qo->single = $this->trans_single( $single );
307f045f 168 if (array_key_exists('shuffleanswers', $question['#'])) {
169 $shuffleanswers = $question['#']['shuffleanswers'][0]['#'];
170 } else {
171 $shuffleanswers = 'false';
172 }
2da44816 173 $qo->shuffleanswers = $this->trans_single($shuffleanswers);
307f045f 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);
177
84769fd8 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;
187 }
188
189 return $qo;
190 }
191
6e557c08 192 /**
c81415c7 193 * import cloze type question
6e557c08 194 * @param array question question array from xml tree
195 * @return object question object
c81415c7 196 */
7b8bc256 197 function import_multianswer( $questions ) {
0c6b4d2e 198 $questiontext = $questions['#']['questiontext'][0]['#']['text'];
199 $qo = qtype_multianswer_extract_question($this->import_text($questiontext));
7b8bc256 200
201 // 'header' parts particular to multianswer
202 $qo->qtype = MULTIANSWER;
203 $qo->course = $this->course;
204
71ffbac2 205 if (!empty($questions)) {
7b8bc256 206 $qo->name = $this->import_text( $questions['#']['name'][0]['#']['text'] );
207 }
208
209 return $qo;
210 }
211
6e557c08 212 /**
c81415c7 213 * import true/false type question
6e557c08 214 * @param array question question array from xml tree
215 * @return object question object
c81415c7 216 */
84769fd8 217 function import_truefalse( $question ) {
84769fd8 218 // get common parts
219 $qo = $this->import_headers( $question );
220
221 // 'header' parts particular to true/false
222 $qo->qtype = TRUEFALSE;
223
224 // get answer info
3246ed33 225 //
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.
238 }
239 if ($answertext == 'true') {
240 $qo->answer = ($answer['@']['fraction'] == 100);
7939a4a0 241 $qo->correctanswer = $qo->answer;
3246ed33 242 $qo->feedbacktrue = $feedback;
243 } else {
244 $qo->answer = ($answer['@']['fraction'] != 100);
7939a4a0 245 $qo->correctanswer = $qo->answer;
3246ed33 246 $qo->feedbackfalse = $feedback;
247 }
248 $first = false;
84769fd8 249 }
3246ed33 250
251 if ($warning) {
252 $a = new stdClass;
55c54868 253 $a->questiontext = $qo->questiontext;
3246ed33 254 $a->answer = get_string($qo->answer ? 'true' : 'false', 'quiz');
255 notify(get_string('truefalseimporterror', 'quiz', $a));
84769fd8 256 }
257
258 return $qo;
259 }
260
6e557c08 261 /**
c81415c7 262 * import short answer type question
6e557c08 263 * @param array question question array from xml tree
264 * @return object question object
c81415c7 265 */
84769fd8 266 function import_shortanswer( $question ) {
84769fd8 267 // get common parts
268 $qo = $this->import_headers( $question );
269
270 // header parts particular to shortanswer
271 $qo->qtype = SHORTANSWER;
272
273 // get usecase
274 $qo->usecase = $question['#']['usecase'][0]['#'];
275
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;
285 }
286
287 return $qo;
288 }
7b8bc256 289
6e557c08 290 /**
c81415c7 291 * import regexp type question
6e557c08 292 * @param array question question array from xml tree
293 * @return object question object
c81415c7 294 */
7b8bc256 295 function import_regexp( $question ) {
7b8bc256 296 // get common parts
297 $qo = $this->import_headers( $question );
298
299 // header parts particular to shortanswer
300 $qo->qtype = regexp;
301
302 // get usecase
303 $qo->usecase = $question['#']['usecase'][0]['#'];
304
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;
314 }
84769fd8 315
7b8bc256 316 return $qo;
317 }
318
6e557c08 319 /**
c81415c7 320 * import description type question
6e557c08 321 * @param array question question array from xml tree
322 * @return object question object
c81415c7 323 */
7b8bc256 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;
330 }
84769fd8 331
6e557c08 332 /**
c81415c7 333 * import numerical type question
6e557c08 334 * @param array question question array from xml tree
335 * @return object question object
c81415c7 336 */
337 function import_numerical( $question ) {
84769fd8 338 // get common parts
339 $qo = $this->import_headers( $question );
340
341 // header parts particular to numerical
342 $qo->qtype = NUMERICAL;
343
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) {
55c54868 351 // answer outside of <text> is deprecated
352 if (!empty( $answer['#']['text'] )) {
353 $answertext = $this->import_text( $answer['#']['text'] );
354 }
355 else {
356 $answertext = trim($answer['#'][0]);
357 }
55894a42 358 if ($answertext == '') {
359 $qo->answer[] = '*';
360 } else {
361 $qo->answer[] = $answertext;
362 }
a0d187bf 363 $qo->feedback[] = $this->import_text( $answer['#']['feedback'][0]['#']['text'] );
84769fd8 364 $qo->tolerance[] = $answer['#']['tolerance'][0]['#'];
55c54868 365
366 // fraction as a tag is deprecated
367 if (!empty($answer['#']['fraction'][0]['#'])) {
368 $qo->fraction[] = $answer['#']['fraction'][0]['#'];
369 }
370 else {
371 $qo->fraction[] = $answer['@']['fraction'] / 100;
372 }
84769fd8 373 }
374
375 // get units array
84769fd8 376 $qo->unit = array();
a0d187bf 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]['#'];
383 }
84769fd8 384 }
84769fd8 385 return $qo;
386 }
387
6e557c08 388 /**
c81415c7 389 * import matching type question
6e557c08 390 * @param array question question array from xml tree
391 * @return object question object
c81415c7 392 */
51bcdf28 393 function import_matching( $question ) {
51bcdf28 394 // get common parts
395 $qo = $this->import_headers( $question );
396
397 // header parts particular to matching
398 $qo->qtype = MATCH;
ee800653 399 if (!empty($question['#']['shuffleanswers'])) {
400 $qo->shuffleanswers = $question['#']['shuffleanswers'][0]['#'];
401 } else {
402 $qo->shuffleanswers = false;
403 }
51bcdf28 404
405 // get subquestions
406 $subquestions = $question['#']['subquestion'];
407 $qo->subquestions = array();
408 $qo->subanswers = array();
409
410 // run through subquestions
411 foreach ($subquestions as $subquestion) {
a0d187bf 412 $qtext = $this->import_text( $subquestion['#']['text'] );
413 $atext = $this->import_text( $subquestion['#']['answer'][0]['#']['text'] );
51bcdf28 414 $qo->subquestions[] = $qtext;
415 $qo->subanswers[] = $atext;
416 }
51bcdf28 417 return $qo;
418 }
419
6e557c08 420 /**
c81415c7 421 * import essay type question
6e557c08 422 * @param array question question array from xml tree
423 * @return object question object
c81415c7 424 */
425 function import_essay( $question ) {
426 // get common parts
427 $qo = $this->import_headers( $question );
428
429 // header parts particular to essay
430 $qo->qtype = ESSAY;
431
432 // get feedback
433 $qo->feedback = $this->import_text( $question['#']['answer'][0]['#']['feedback'][0]['#']['text'] );
434
55c54868 435 // handle answer
436 $answer = $question['#']['answer'][0];
437
438 // get fraction - <fraction> tag is deprecated
439 if (!empty($answer['#']['fraction'][0]['#'])) {
440 $qo->fraction = $answer['#']['fraction'][0]['#'];
441 }
442 else {
443 $qo->fraction = $answer['@']['fraction'] / 100;
444 }
c81415c7 445
446 return $qo;
447 }
84769fd8 448
725ba2a0 449 function import_calculated( $question ) {
450 // import numerical question
451
452 // get common parts
453 $qo = $this->import_headers( $question );
454
455 // header parts particular to numerical
456 $qo->qtype = CALCULATED ;//CALCULATED;
457
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'] );
473 }
474 else {
475 $answertext = trim($answer['#'][0]);
476 }
477 if ($answertext == '') {
478 $qo->answers[] = '*';
479 } else {
480 $qo->answers[] = $answertext;
481 }
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]['#'];
487 }
488 else {
489 $qo->fraction[] = $answer['@']['fraction'] / 100;
490 }
491 $qo->tolerancetype[] = $answer['#']['tolerancetype'][0]['#'];
492 $qo->correctanswerformat[] = $answer['#']['correctanswerformat'][0]['#'];
493 $qo->correctanswerlength[] = $answer['#']['correctanswerlength'][0]['#'];
494 }
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]['#'];
503 }
504 }
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]['#'];
529 }
530 }
531
5fd8f999 532 // echo "<pre>loaded qo";print_r($qo);echo "</pre>";
725ba2a0 533
534 return $qo;
535 }
536
ee259d0c 537 /**
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>
544 */
545 function import_category( $question ) {
0c6b4d2e 546 $qo = new stdClass;
ee259d0c 547 $qo->qtype = 'category';
548 $qo->category = $question['#']['category'][0]['#'];
549 return $qo;
550 }
551
c81415c7 552 /**
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!
6e557c08 556 * @param array lines array of lines from the input file
557 * @return array (of objects) question objects
c81415c7 558 */
559 function readquestions($lines) {
84769fd8 560 // we just need it as one big string
561 $text = implode($lines, " ");
562 unset( $lines );
563
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 );
568
569 // set up array to hold all our questions
570 $questions = array();
571
572 // iterate through questions
573 foreach ($xml['quiz']['#']['question'] as $question) {
574 $question_type = $question['@']['type'];
575 $questiontype = get_string( 'questiontype','quiz',$question_type );
84769fd8 576
577 if ($question_type=='multichoice') {
578 $qo = $this->import_multichoice( $question );
579 }
580 elseif ($question_type=='truefalse') {
581 $qo = $this->import_truefalse( $question );
582 }
583 elseif ($question_type=='shortanswer') {
584 $qo = $this->import_shortanswer( $question );
585 }
7b8bc256 586 //elseif ($question_type=='regexp') {
587 // $qo = $this->import_regexp( $question );
588 //}
84769fd8 589 elseif ($question_type=='numerical') {
590 $qo = $this->import_numerical( $question );
591 }
7b8bc256 592 elseif ($question_type=='description') {
593 $qo = $this->import_description( $question );
594 }
51bcdf28 595 elseif ($question_type=='matching') {
596 $qo = $this->import_matching( $question );
597 }
7b8bc256 598 elseif ($question_type=='cloze') {
c81415c7 599 $qo = $this->import_multianswer( $question );
600 }
601 elseif ($question_type=='essay') {
602 $qo = $this->import_essay( $question );
7b8bc256 603 }
725ba2a0 604 elseif ($question_type=='calculated') {
605 $qo = $this->import_calculated( $question );
606 }
ee259d0c 607 elseif ($question_type=='category') {
608 $qo = $this->import_category( $question );
609 }
84769fd8 610 else {
ee800653 611 $notsupported = get_string( 'xmltypeunsupported','quiz',$question_type );
4089729b 612 $this->error( $notsupportted );
84769fd8 613 $qo = null;
614 }
615
616 // stick the result in the $questions array
617 if ($qo) {
0c6b4d2e 618 $qo->generalfeedback = '';
84769fd8 619 $questions[] = $qo;
620 }
621 }
84769fd8 622 return $questions;
623 }
624
625 // EXPORT FUNCTIONS START HERE
626
84769fd8 627 function export_file_extension() {
628 // override default type so extension is .xml
629
630 return ".xml";
631 }
632
c81415c7 633
634 /**
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)
6e557c08 638 * @param mixed type_id Internal code
639 * @return string question type string
c81415c7 640 */
84769fd8 641 function get_qtype( $type_id ) {
84769fd8 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;
7b8bc256 652 //case regexp:
653 // $name = 'regexp';
654 // break;
84769fd8 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;
c81415c7 667 case ESSAY:
668 $name = 'essay';
669 break;
725ba2a0 670 case CALCULATED:
671 $name = 'calculated';
672 break;
84769fd8 673 default:
674 $name = 'unknown';
675 }
676 return $name;
677 }
678
6e557c08 679 /**
c81415c7 680 * Convert internal Moodle text format code into
681 * human readable form
6e557c08 682 * @param int id internal code
683 * @return string format text
c81415c7 684 */
84769fd8 685 function get_format( $id ) {
84769fd8 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";
704 }
705 return $name;
706 }
707
6e557c08 708 /**
c81415c7 709 * Convert internal single question code into
710 * human readable form
6e557c08 711 * @param int id single question code
712 * @return string single question string
c81415c7 713 */
84769fd8 714 function get_single( $id ) {
84769fd8 715 switch( $id ) {
716 case 0:
717 $name = "false";
718 break;
719 case 1:
720 $name = "true";
721 break;
722 default:
723 $name = "unknown";
724 }
725 return $name;
726 }
727
6e557c08 728 /**
c81415c7 729 * generates <text></text> tags, processing raw text therein
6e557c08 730 * @param int ilev the current indent level
731 * @param boolean short stick it on one line
732 * @return string formatted text
c81415c7 733 */
84769fd8 734 function writetext( $raw, $ilev=0, $short=true) {
84769fd8 735 $indent = str_repeat( " ",$ilev );
736
737 // encode the text to 'disguise' HTML content
738 $raw = htmlspecialchars( $raw );
739
740 if ($short) {
741 $xml = "$indent<text>$raw</text>\n";
742 }
743 else {
744 $xml = "$indent<text>\n$raw\n$indent</text>\n";
745 }
746
747 return $xml;
748 }
749
750 function presave_process( $content ) {
751 // override method to allow us to add xml headers and footers
752
753 $content = "<?xml version=\"1.0\"?>\n" .
754 "<quiz>\n" .
755 $content . "\n" .
756 "</quiz>";
757
758 return $content;
759 }
760
6e557c08 761 /**
c81415c7 762 * Include an image encoded in base 64
6e557c08 763 * @param string imagepath The location of the image file
764 * @return string xml code segment
c81415c7 765 */
d08e16b2 766 function writeimage( $imagepath ) {
d08e16b2 767 global $CFG;
768
769 if (empty($imagepath)) {
770 return '';
771 }
772
773 $courseid = $this->course->id;
774 if (!$binary = file_get_contents( "{$CFG->dataroot}/$courseid/$imagepath" )) {
775 return '';
776 }
777
778 $content = " <image_base64>\n".addslashes(base64_encode( $binary ))."\n".
779 "\n </image_base64>\n";
780 return $content;
781 }
782
6e557c08 783 /**
c81415c7 784 * Turns question into an xml segment
6e557c08 785 * @param array question question array
786 * @return string xml segment
c81415c7 787 */
84769fd8 788 function writequestion( $question ) {
725ba2a0 789 global $CFG,$QTYPES;
84769fd8 790 // initial string;
791 $expout = "";
792
793 // add comment
794 $expout .= "\n\n<!-- question: $question->id -->\n";
795
796 // add opening tag
f1abd39f 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;
805 }
806 elseif ($question->qtype != MULTIANSWER) {
7b8bc256 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 );
a4514d91 812 $generalfeedback = $this->writetext( $question->generalfeedback );
7b8bc256 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);
a4514d91 820 $expout .= " <generalfeedback>\n";
821 $expout .= $generalfeedback;
822 $expout .= " </generalfeedback>\n";
c81415c7 823 $expout .= " <defaultgrade>{$question->defaultgrade}</defaultgrade>\n";
7b8bc256 824 $expout .= " <penalty>{$question->penalty}</penalty>\n";
825 $expout .= " <hidden>{$question->hidden}</hidden>\n";
826 }
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";
837 }
838
d08e16b2 839 if (!empty($question->options->shuffleanswers)) {
840 $expout .= " <shuffleanswers>{$question->options->shuffleanswers}</shuffleanswers>\n";
841 }
842 else {
843 $expout .= " <shuffleanswers>0</shuffleanswers>\n";
844 }
84769fd8 845
846 // output depends on question type
847 switch($question->qtype) {
f1abd39f 848 case 'category':
849 // not a qtype really - dummy used for category switching
850 break;
84769fd8 851 case TRUEFALSE:
36e2232e 852 foreach ($question->options->answers as $answer) {
853 $fraction_pc = round( $answer->fraction * 100 );
3246ed33 854 if ($answer->id == $question->options->trueanswer) {
855 $answertext = 'true';
856 } else {
857 $answertext = 'false';
858 }
36e2232e 859 $expout .= " <answer fraction=\"$fraction_pc\">\n";
3246ed33 860 $expout .= $this->writetext($answertext, 3) . "\n";
36e2232e 861 $expout .= " <feedback>\n";
862 $expout .= $this->writetext( $answer->feedback,4,false );
863 $expout .= " </feedback>\n";
864 $expout .= " </answer>\n";
865 }
84769fd8 866 break;
867 case MULTICHOICE:
868 $expout .= " <single>".$this->get_single($question->options->single)."</single>\n";
307f045f 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";
84769fd8 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";
6e557c08 878 $expout .= $this->writetext( $answer->feedback,5,false );
84769fd8 879 $expout .= " </feedback>\n";
880 $expout .= " </answer>\n";
881 }
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";
893 }
894 break;
7b8bc256 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;
84769fd8 907 case NUMERICAL:
908 foreach ($question->options->answers as $answer) {
909 $tolerance = $answer->tolerance;
55c54868 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";
84769fd8 914 $expout .= " <tolerance>$tolerance</tolerance>\n";
915 $expout .= " <feedback>".$this->writetext( $answer->feedback )."</feedback>\n";
55c54868 916 // fraction tag is deprecated
917 // $expout .= " <fraction>{$answer->fraction}</fraction>\n";
84769fd8 918 $expout .= "</answer>\n";
919 }
920
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";
929 }
930 $expout .= "</units>\n";
931 }
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";
939 }
940 break;
941 case DESCRIPTION:
c81415c7 942 // nothing more to do for this type
84769fd8 943 break;
944 case MULTIANSWER:
7b8bc256 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++;
951 }
952 break;
c81415c7 953 case ESSAY:
954 foreach ($question->options->answers as $answer) {
55c54868 955 $percent = 100 * $answer->fraction;
956 $expout .= "<answer fraction=\"$percent\">\n";
c81415c7 957 $expout .= " <feedback>".$this->writetext( $answer->feedback )."</feedback>\n";
55c54868 958 // fraction tag is deprecated
959 // $expout .= " <fraction>{$answer->fraction}</fraction>\n";
c81415c7 960 $expout .= "</answer>\n";
961 }
962
725ba2a0 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";
980 }
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";
989 }
990 $expout .= "</units>\n";
991 }
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
c9e4ba36 994 // $question_datasetdefs =$QTYPES['calculated']->get_datasets_for_export ($question);
725ba2a0 995 // echo "<pre> question defs";print_r($question_datasetdefs);echo "</pre>";
996 //If there are question_datasets
c9e4ba36 997 if( isset($question->options->datasets)&&count($question->options->datasets)){// there should be
725ba2a0 998 $expout .= "<dataset_definitions>\n";
c9e4ba36 999 foreach ($question->options->datasets as $def) {
725ba2a0 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";
1016 }
1017 $expout .= " </dataset_items>\n";
1018 $expout .= " <number_of_items>".$def-> number_of_items."</number_of_items>\n";
1019 }
1020 $expout .= "</dataset_definition>\n";
1021 }
1022 $expout .= "</dataset_definitions>\n";
1023 }
c81415c7 1024 break;
84769fd8 1025 default:
1026 $expout .= "<!-- Question type is unknown or not supported (Type=$question->qtype) -->\n";
1027 }
1028
1029 // close the question tag
1030 $expout .= "</question>\n";
1031
84769fd8 1032 return $expout;
1033 }
1034}
1035
1036?>