Updated the HEAD build version to 20101106
[moodle.git] / question / format / xml / format.php
CommitLineData
aeb15530 1<?php
688d8753
TH
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
84769fd8 9//
688d8753
TH
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
84769fd8 14//
688d8753
TH
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Moodle XML question importer.
20 *
21 * @package qformat
22 * @subpackage qformat_xml
23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27
41a89a07 28/**
688d8753
TH
29 * Importer for Moodle XML question format.
30 *
31 * See http://docs.moodle.org/en/Moodle_XML_format for a description of the format.
32 *
33 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41a89a07 35 */
688d8753 36require_once($CFG->libdir . '/xmlize.php');
84769fd8 37
f5565b69 38class qformat_xml extends qformat_default {
84769fd8 39
40 function provide_import() {
41 return true;
42 }
43
44 function provide_export() {
45 return true;
46 }
47
48 // IMPORT FUNCTIONS START HERE
49
88bc20c3 50 /**
c81415c7 51 * Translate human readable format name
52 * into internal Moodle code number
6e557c08 53 * @param string name format name from xml file
54 * @return int Moodle format code
c81415c7 55 */
84769fd8 56 function trans_format( $name ) {
88bc20c3 57 $name = trim($name);
58
84769fd8 59 if ($name=='moodle_auto_format') {
60 $id = 0;
61 }
62 elseif ($name=='html') {
63 $id = 1;
64 }
65 elseif ($name=='plain_text') {
66 $id = 2;
67 }
68 elseif ($name=='wiki_like') {
69 $id = 3;
70 }
71 elseif ($name=='markdown') {
72 $id = 4;
73 }
74 else {
75 $id = 0; // or maybe warning required
76 }
77 return $id;
78 }
79
6e557c08 80 /**
c81415c7 81 * Translate human readable single answer option
82 * to internal code number
6e557c08 83 * @param string name true/false
84 * @return int internal code number
c81415c7 85 */
84769fd8 86 function trans_single( $name ) {
2da44816 87 $name = trim($name);
88 if ($name == "false" || !$name) {
89 return 0;
90 } else {
91 return 1;
92 }
84769fd8 93 }
94
6e557c08 95 /**
c81415c7 96 * process text string from xml file
6e557c08 97 * @param array $text bit of xml tree after ['text']
98 * @return string processed text
c81415c7 99 */
84769fd8 100 function import_text( $text ) {
17102269 101 // quick sanity check
102 if (empty($text)) {
103 return '';
104 }
84769fd8 105 $data = $text[0]['#'];
294ce987 106 return trim($data);
84769fd8 107 }
108
46013523 109 /**
110 * return the value of a node, given a path to the node
111 * if it doesn't exist return the default value
112 * @param array xml data to read
88bc20c3 113 * @param array path path to node expressed as array
114 * @param mixed default
46013523 115 * @param bool istext process as text
116 * @param string error if set value must exist, return false and issue message if not
117 * @return mixed value
118 */
cde2709a 119 function getpath($xml, $path, $default, $istext=false, $error='') {
46013523 120 foreach ($path as $index) {
228b6f6b 121 if (!isset($xml[$index])) {
46013523 122 if (!empty($error)) {
123 $this->error( $error );
124 return false;
125 } else {
126 return $default;
127 }
128 }
129 else $xml = $xml[$index];
130 }
131 if ($istext) {
fc22da99 132 if (!is_string($xml)) {
c95defa9 133 $this->error( get_string('invalidxml','qformat_xml') );
fc22da99 134 }
cde2709a 135 $xml = trim($xml);
46013523 136 }
137
138 return $xml;
139 }
140
141
6e557c08 142 /**
c81415c7 143 * import parts of question common to all types
e7ef42f5 144 * @param $question array question question array from xml tree
6e557c08 145 * @return object question object
c81415c7 146 */
cde2709a 147 function import_headers($question) {
46013523 148 // get some error strings
cde2709a
DC
149 $error_noname = get_string('xmlimportnoname','quiz');
150 $error_noquestion = get_string('xmlimportnoquestion','quiz');
46013523 151
84769fd8 152 // this routine initialises the question object
5bed54e1 153 $qo = $this->defaultquestion();
84769fd8 154
46013523 155 // question name
156 $qo->name = $this->getpath( $question, array('#','name',0,'#','text',0,'#'), '', true, $error_noname );
cde2709a
DC
157 $qo->questiontext = $this->getpath($question, array('#','questiontext',0,'#','text',0,'#'), '', true );
158 $qo->questiontextformat = $this->trans_format($this->getpath($question, array('#','questiontext',0,'@','format'), ''));
159 $qo->image = $this->getpath($question, array('#','image',0,'#'), $qo->image );
46013523 160 $image_base64 = $this->getpath( $question, array('#','image_base64','0','#'),'' );
161 if (!empty($image_base64)) {
cde2709a 162 $qo->image = $this->importimagefile($qo->image, $image_base64);
46013523 163 }
cde2709a
DC
164
165 $qo->questiontextfiles = array();
166
167 // restore files in questiontext
168 $files = $this->getpath($question, array('#', 'questiontext', 0, '#','file'), array(), false);
169 foreach ($files as $file) {
170 $data = new stdclass;
171 $data->content = $file['#'];
172 $data->encoding = $file['@']['encoding'];
173 $data->name = $file['@']['name'];
174 $qo->questiontextfiles[] = $data;
175 }
176
177 // restore files in generalfeedback
178 $qo->generalfeedback = $this->getpath($question, array('#','generalfeedback',0,'#','text',0,'#'), $qo->generalfeedback, true);
179 $qo->generalfeedbackfiles = array();
180 $qo->generalfeedbackformat = $this->trans_format($this->getpath($question, array('#', 'generalfeedback', 0, '@', 'format'), ''));
181 $files = $this->getpath($question, array('#', 'generalfeedback', 0, '#', 'file'), array(), false);
182 foreach ($files as $file) {
183 $data = new stdclass;
184 $data->content = $file['#'];
185 $data->encoding = $file['@']['encoding'];
186 $data->name = $file['@']['name'];
187 $qo->generalfeedbackfiles[] = $data;
188 }
189
46013523 190 $qo->defaultgrade = $this->getpath( $question, array('#','defaultgrade',0,'#'), $qo->defaultgrade );
191 $qo->penalty = $this->getpath( $question, array('#','penalty',0,'#'), $qo->penalty );
84769fd8 192
193 return $qo;
194 }
195
6e557c08 196 /**
c81415c7 197 * import the common parts of a single answer
6e557c08 198 * @param array answer xml tree for single answer
199 * @return object answer object
88bc20c3 200 */
cde2709a
DC
201 function import_answer($answer) {
202 $fraction = $this->getpath($answer, array('@', 'fraction'), 0);
203 $answertext = $this->getpath($answer, array('#', 'text', 0, '#'), '', true);
204 $answerformat = $this->trans_format($this->getpath($answer, array('#', 'text', 0, '#'), ''));
205 $answerfiles = array();
206 $files = $this->getpath($answer, array('#', 'answer', 0, '#', 'file'), array());
207 foreach ($files as $file) {
208 $data = new stdclass;
209 $data->content = $file['#'];
210 $data->name = $file['@']['name'];
211 $data->encoding = $file['@']['encoding'];
212 $answerfiles[] = $data;
213 }
214
215 $feedbacktext = $this->getpath($answer, array('#', 'feedback', 0, '#', 'text', 0, '#'), '', true);
216 $feedbackformat = $this->trans_format($this->getpath($answer, array('#', 'feedback', 0, '@', 'format'), ''));
217 $feedbackfiles = array();
218 $files = $this->getpath($answer, array('#', 'feedback', 0, '#', 'file'), array());
219 foreach ($files as $file) {
220 $data = new stdclass;
221 $data->content = $file['#'];
222 $data->name = $file['@']['name'];
223 $data->encoding = $file['@']['encoding'];
224 $feedbackfiles[] = $data;
225 }
226
227 $ans = new stdclass;
228
229 $ans->answer = array();
230 $ans->answer['text'] = $answertext;
231 $ans->answer['format'] = $answerformat;
232 $ans->answer['files'] = $answerfiles;
233
234 $ans->feedback = array();
235 $ans->feedback['text'] = $feedbacktext;
236 $ans->feedback['format'] = $feedbackformat;
237 $ans->feedback['files'] = $feedbackfiles;
84769fd8 238
84769fd8 239 $ans->fraction = $fraction / 100;
84769fd8 240 return $ans;
241 }
242
6e557c08 243 /**
88bc20c3 244 * import multiple choice question
6e557c08 245 * @param array question question array from xml tree
246 * @return object question object
c81415c7 247 */
cde2709a 248 function import_multichoice($question) {
84769fd8 249 // get common parts
cde2709a 250 $qo = $this->import_headers($question);
84769fd8 251
252 // 'header' parts particular to multichoice
253 $qo->qtype = MULTICHOICE;
81a7a02b 254 $single = $this->getpath( $question, array('#','single',0,'#'), 'true' );
84769fd8 255 $qo->single = $this->trans_single( $single );
81a7a02b 256 $shuffleanswers = $this->getpath( $question, array('#','shuffleanswers',0,'#'), 'false' );
2377f8dc 257 $qo->answernumbering = $this->getpath( $question, array('#','answernumbering',0,'#'), 'abc' );
2da44816 258 $qo->shuffleanswers = $this->trans_single($shuffleanswers);
cde2709a
DC
259
260 $qo->correctfeedback = array();
261 $qo->correctfeedback['text'] = $this->getpath($question, array('#', 'correctfeedback', 0, '#', 'text', 0, '#'), '', true);
262 $qo->correctfeedback['format'] = $this->trans_format($this->getpath($question, array('#', 'correctfeedback', 0, '@', 'format'), ''));
263 $qo->correctfeedback['files'] = array();
264 // restore files in correctfeedback
265 $files = $this->getpath($question, array('#', 'correctfeedback', 0, '#','file'), array(), false);
266 foreach ($files as $file) {
267 $data = new stdclass;
268 $data->content = $file['#'];
269 $data->encoding = $file['@']['encoding'];
270 $data->name = $file['@']['name'];
271 $qo->correctfeedback['files'][] = $data;
272 }
273
274 $qo->partiallycorrectfeedback = array();
275 $qo->partiallycorrectfeedback['text'] = $this->getpath( $question, array('#','partiallycorrectfeedback',0,'#','text',0,'#'), '', true );
276 $qo->partiallycorrectfeedback['format'] = $this->trans_format($this->getpath($question, array('#', 'partiallycorrectfeedback', 0, '@', 'format'), ''));
277 $qo->partiallycorrectfeedback['files'] = array();
278 // restore files in partiallycorrectfeedback
279 $files = $this->getpath($question, array('#', 'partiallycorrectfeedback', 0, '#','file'), array(), false);
280 foreach ($files as $file) {
281 $data = new stdclass;
282 $data->content = $file['#'];
283 $data->encoding = $file['@']['encoding'];
284 $data->name = $file['@']['name'];
285 $qo->partiallycorrectfeedback['files'][] = $data;
286 }
287
288 $qo->incorrectfeedback = array();
289 $qo->incorrectfeedback['text'] = $this->getpath( $question, array('#','incorrectfeedback',0,'#','text',0,'#'), '', true );
290 $qo->incorrectfeedback['format'] = $this->trans_format($this->getpath($question, array('#', 'incorrectfeedback', 0, '@', 'format'), ''));
291 $qo->incorrectfeedback['files'] = array();
292 // restore files in incorrectfeedback
293 $files = $this->getpath($question, array('#', 'incorrectfeedback', 0, '#','file'), array(), false);
294 foreach ($files as $file) {
295 $data = new stdclass;
296 $data->content = $file['#'];
297 $data->encoding = $file['@']['encoding'];
298 $data->name = $file['@']['name'];
299 $qo->incorrectfeedback['files'][] = $data;
300 }
c0ffeb39 301
302 // There was a time on the 1.8 branch when it could output an empty answernumbering tag, so fix up any found.
303 if (empty($qo->answernumbering)) {
304 $qo->answernumbering = 'abc';
305 }
306
84769fd8 307 // run through the answers
88bc20c3 308 $answers = $question['#']['answer'];
84769fd8 309 $a_count = 0;
310 foreach ($answers as $answer) {
cde2709a 311 $ans = $this->import_answer($answer);
84769fd8 312 $qo->answer[$a_count] = $ans->answer;
313 $qo->fraction[$a_count] = $ans->fraction;
314 $qo->feedback[$a_count] = $ans->feedback;
315 ++$a_count;
316 }
cde2709a 317
84769fd8 318 return $qo;
319 }
320
6e557c08 321 /**
c81415c7 322 * import cloze type question
6e557c08 323 * @param array question question array from xml tree
324 * @return object question object
c81415c7 325 */
7b8bc256 326 function import_multianswer( $questions ) {
0c6b4d2e 327 $questiontext = $questions['#']['questiontext'][0]['#']['text'];
328 $qo = qtype_multianswer_extract_question($this->import_text($questiontext));
7b8bc256 329
330 // 'header' parts particular to multianswer
331 $qo->qtype = MULTIANSWER;
332 $qo->course = $this->course;
4ab4dcb2 333 $qo->generalfeedback = $this->getpath( $questions, array('#','generalfeedback',0,'#','text',0,'#'), '', true );
7b8bc256 334
71ffbac2 335 if (!empty($questions)) {
7b8bc256 336 $qo->name = $this->import_text( $questions['#']['name'][0]['#']['text'] );
337 }
338
339 return $qo;
340 }
341
6e557c08 342 /**
c81415c7 343 * import true/false type question
6e557c08 344 * @param array question question array from xml tree
345 * @return object question object
c81415c7 346 */
84769fd8 347 function import_truefalse( $question ) {
84769fd8 348 // get common parts
fef8f84e 349 global $OUTPUT;
84769fd8 350 $qo = $this->import_headers( $question );
351
352 // 'header' parts particular to true/false
353 $qo->qtype = TRUEFALSE;
354
355 // get answer info
3246ed33 356 //
357 // In the past, it used to be assumed that the two answers were in the file
358 // true first, then false. Howevever that was not always true. Now, we
359 // try to match on the answer text, but in old exports, this will be a localised
360 // string, so if we don't find true or false, we fall back to the old system.
361 $first = true;
362 $warning = false;
363 foreach ($question['#']['answer'] as $answer) {
81a7a02b 364 $answertext = $this->getpath( $answer, array('#','text',0,'#'), '', true );
cde2709a
DC
365 $feedback = $this->getpath($answer, array('#','feedback',0,'#','text',0,'#'), '', true);
366 $feedbackformat = $this->getpath($answer, array('#','feedback',0, '@', 'format'), '', true);
367 $feedbackfiles = $this->getpath($answer, array('#', 'feedback', 0, '#', 'file'), array(), false);
368 $files = array();
369 foreach ($feedbackfiles as $file) {
370 $data = new stdclass;
371 $data->content = $file['#'];
372 $data->encoding = $file['@']['encoding'];
373 $data->name = $file['@']['name'];
374 $files[] = $data;
375 }
3246ed33 376 if ($answertext != 'true' && $answertext != 'false') {
377 $warning = true;
378 $answertext = $first ? 'true' : 'false'; // Old style file, assume order is true/false.
88bc20c3 379 }
3246ed33 380 if ($answertext == 'true') {
381 $qo->answer = ($answer['@']['fraction'] == 100);
7939a4a0 382 $qo->correctanswer = $qo->answer;
cde2709a
DC
383 $qo->feedbacktrue = array();
384 $qo->feedbacktrue['text'] = $feedback;
385 $qo->feedbacktrue['format'] = $this->trans_format($feedbackformat);
386 $qo->feedbacktrue['itemid'] = null;
387 $qo->feedbacktruefiles = $files;
3246ed33 388 } else {
389 $qo->answer = ($answer['@']['fraction'] != 100);
7939a4a0 390 $qo->correctanswer = $qo->answer;
cde2709a
DC
391 $qo->feedbackfalse = array();
392 $qo->feedbackfalse['text'] = $feedback;
393 $qo->feedbackfalse['format'] = $this->trans_format($feedbackformat);
394 $qo->feedbackfalse['itemid'] = null;
395 $qo->feedbackfalsefiles = $files;
3246ed33 396 }
397 $first = false;
84769fd8 398 }
3246ed33 399
400 if ($warning) {
401 $a = new stdClass;
55c54868 402 $a->questiontext = $qo->questiontext;
3246ed33 403 $a->answer = get_string($qo->answer ? 'true' : 'false', 'quiz');
fef8f84e 404 echo $OUTPUT->notification(get_string('truefalseimporterror', 'quiz', $a));
84769fd8 405 }
84769fd8 406 return $qo;
407 }
408
6e557c08 409 /**
c81415c7 410 * import short answer type question
6e557c08 411 * @param array question question array from xml tree
412 * @return object question object
c81415c7 413 */
84769fd8 414 function import_shortanswer( $question ) {
84769fd8 415 // get common parts
416 $qo = $this->import_headers( $question );
417
418 // header parts particular to shortanswer
419 $qo->qtype = SHORTANSWER;
420
421 // get usecase
46013523 422 $qo->usecase = $this->getpath($question, array('#','usecase',0,'#'), $qo->usecase );
84769fd8 423
424 // run through the answers
88bc20c3 425 $answers = $question['#']['answer'];
84769fd8 426 $a_count = 0;
427 foreach ($answers as $answer) {
428 $ans = $this->import_answer( $answer );
429 $qo->answer[$a_count] = $ans->answer;
430 $qo->fraction[$a_count] = $ans->fraction;
431 $qo->feedback[$a_count] = $ans->feedback;
432 ++$a_count;
433 }
434
435 return $qo;
436 }
88bc20c3 437
6e557c08 438 /**
c81415c7 439 * import description type question
6e557c08 440 * @param array question question array from xml tree
441 * @return object question object
c81415c7 442 */
7b8bc256 443 function import_description( $question ) {
444 // get common parts
445 $qo = $this->import_headers( $question );
446 // header parts particular to shortanswer
447 $qo->qtype = DESCRIPTION;
3f5633df 448 $qo->defaultgrade = 0;
449 $qo->length = 0;
7b8bc256 450 return $qo;
451 }
84769fd8 452
6e557c08 453 /**
c81415c7 454 * import numerical type question
6e557c08 455 * @param array question question array from xml tree
456 * @return object question object
c81415c7 457 */
458 function import_numerical( $question ) {
84769fd8 459 // get common parts
460 $qo = $this->import_headers( $question );
461
462 // header parts particular to numerical
463 $qo->qtype = NUMERICAL;
464
465 // get answers array
466 $answers = $question['#']['answer'];
467 $qo->answer = array();
468 $qo->feedback = array();
469 $qo->fraction = array();
470 $qo->tolerance = array();
471 foreach ($answers as $answer) {
55c54868 472 // answer outside of <text> is deprecated
cde2709a
DC
473 $obj = $this->import_answer($answer);
474 $qo->answer[] = $obj->answer;
81a7a02b 475 if (empty($qo->answer)) {
476 $qo->answer = '*';
55c54868 477 }
cde2709a 478 $qo->feedback[] = $obj->feedback;
81a7a02b 479 $qo->tolerance[] = $this->getpath( $answer, array('#','tolerance',0,'#'), 0 );
55c54868 480
481 // fraction as a tag is deprecated
81a7a02b 482 $fraction = $this->getpath( $answer, array('@','fraction'), 0 ) / 100;
483 $qo->fraction[] = $this->getpath( $answer, array('#','fraction',0,'#'), $fraction ); // deprecated
84769fd8 484 }
485
486 // get units array
84769fd8 487 $qo->unit = array();
81a7a02b 488 $units = $this->getpath( $question, array('#','units',0,'#','unit'), array() );
489 if (!empty($units)) {
a0d187bf 490 $qo->multiplier = array();
491 foreach ($units as $unit) {
81a7a02b 492 $qo->multiplier[] = $this->getpath( $unit, array('#','multiplier',0,'#'), 1 );
493 $qo->unit[] = $this->getpath( $unit, array('#','unit_name',0,'#'), '', true );
a0d187bf 494 }
84769fd8 495 }
cde2709a
DC
496 $instructions = $this->getpath($question, array('#', 'instructions'), array());
497 if (!empty($instructions)) {
498 $qo->instructions = array();
499 $qo->instructions['text'] = $instructions[0]['#']['text'][0]['#'];
500 $qo->instructions['format'] = $this->trans_format($instructions[0]['@']['format']);
501 $files = $instructions[0]['#']['file'];
502 $qo->instructionsfiles = array();
503 foreach ($files as $file) {
504 $data = new stdclass;
505 $data->content = $file['#'];
506 $data->encoding = $file['@']['encoding'];
507 $data->name = $file['@']['name'];
508 $qo->instructionsfiles[] = $data;
509 }
510 }
84769fd8 511 return $qo;
512 }
513
6e557c08 514 /**
c81415c7 515 * import matching type question
6e557c08 516 * @param array question question array from xml tree
517 * @return object question object
c81415c7 518 */
51bcdf28 519 function import_matching( $question ) {
51bcdf28 520 // get common parts
521 $qo = $this->import_headers( $question );
522
523 // header parts particular to matching
524 $qo->qtype = MATCH;
81a7a02b 525 $qo->shuffleanswers = $this->getpath( $question, array( '#','shuffleanswers',0,'#' ), 1 );
51bcdf28 526
527 // get subquestions
528 $subquestions = $question['#']['subquestion'];
529 $qo->subquestions = array();
530 $qo->subanswers = array();
531
532 // run through subquestions
533 foreach ($subquestions as $subquestion) {
cde2709a
DC
534 $question = array();
535 $question['text'] = $this->getpath($subquestion, array('#', 'text', 0, '#'), '', true);
536 $question['format'] = $this->trans_format($this->getpath($subquestion, array('@', 'format'), '', true));
537 $question['files'] = array();
538
539 $files = $this->getpath($subquestion, array('#', 'file'), array());
540 foreach ($files as $file) {
541 $data = new stdclass();
542 $data->content = $file['#'];
543 $data->encoding = $file['@']['encoding'];
544 $data->name = $file['@']['name'];
545 $question['files'][] = $data;
546 }
547 $qo->subquestions[] = $question;
548 $answers = $this->getpath($subquestion, array('#', 'answer'), array());
549 $qo->subanswers[] = $this->getpath($subquestion, array('#','answer',0,'#','text',0,'#'), '', true);
51bcdf28 550 }
51bcdf28 551 return $qo;
552 }
553
6e557c08 554 /**
c81415c7 555 * import essay type question
6e557c08 556 * @param array question question array from xml tree
557 * @return object question object
c81415c7 558 */
559 function import_essay( $question ) {
560 // get common parts
561 $qo = $this->import_headers( $question );
562
563 // header parts particular to essay
564 $qo->qtype = ESSAY;
565
cde2709a
DC
566 $answers = $question['#']['answer'];
567 $answer = array_pop($answers);
568 $answer = $this->import_answer($answer);
c81415c7 569 // get feedback
cde2709a 570 $qo->feedback = $answer->feedback;
55c54868 571
572 // get fraction - <fraction> tag is deprecated
6af98025 573 $qo->fraction = $this->getpath( $question, array('@','fraction'), 0 ) / 100;
574 $q0->fraction = $this->getpath( $question, array('#','fraction',0,'#'), $qo->fraction );
c81415c7 575
576 return $qo;
577 }
84769fd8 578
cde2709a 579 function import_calculated($question) {
04d8268d 580 // import calculated question
725ba2a0 581
582 // get common parts
583 $qo = $this->import_headers( $question );
584
04d8268d 585 // header parts particular to calculated
725ba2a0 586 $qo->qtype = CALCULATED ;//CALCULATED;
505f7342 587 $qo->synchronize = $this->getpath( $question, array( '#','synchronize',0,'#' ), 0 );
04d8268d
PP
588 $single = $this->getpath( $question, array('#','single',0,'#'), 'true' );
589 $qo->single = $this->trans_single( $single );
590 $shuffleanswers = $this->getpath( $question, array('#','shuffleanswers',0,'#'), 'false' );
591 $qo->answernumbering = $this->getpath( $question, array('#','answernumbering',0,'#'), 'abc' );
592 $qo->shuffleanswers = $this->trans_single($shuffleanswers);
cde2709a
DC
593
594 $qo->correctfeedback = array();
595 $qo->correctfeedback['text'] = $this->getpath( $question, array('#','correctfeedback',0,'#','text',0,'#'), '', true );
596 $qo->correctfeedback['format'] = $this->trans_format($this->getpath( $question, array('#', 'correctfeedback', 0, '@', 'formath'), '', true ));
597 $qo->correctfeedback['files'] = array();
598
599 $files = $this->getpath($question, array('#', 'correctfeedback', '0', '#', 'file'), array());
600 foreach ($files as $file) {
601 $data = new stdclass();
602 $data->content = $file['#'];
603 $data->name = $file['@']['name'];
604 $data->encoding = $file['@']['encoding'];
605 $qo->correctfeedback['files'][] = $data;
606 }
607
608 $qo->partiallycorrectfeedback = array();
609 $qo->partiallycorrectfeedback['text'] = $this->getpath( $question, array('#','partiallycorrectfeedback',0,'#','text',0,'#'), '', true );
610 $qo->partiallycorrectfeedback['format'] = $this->trans_format($this->getpath($question, array('#','partiallycorrectfeedback', 0, '@','format'), '', true ));
611 $qo->partiallycorrectfeedback['files'] = array();
612
613 $files = $this->getpath($question, array('#', 'partiallycorrectfeedback', '0', '#', 'file'), array());
614 foreach ($files as $file) {
615 $data = new stdclass();
616 $data->content = $file['#'];
617 $data->name = $file['@']['name'];
618 $data->encoding = $file['@']['encoding'];
619 $qo->partiallycorrectfeedback['files'][] = $data;
620 }
621
622 $qo->incorrectfeedback = array();
623 $qo->incorrectfeedback['text'] = $this->getpath( $question, array('#','incorrectfeedback',0,'#','text',0,'#'), '', true );
624 $qo->incorrectfeedback['format'] = $this->trans_format($this->getpath($question, array('#','incorrectfeedback', 0, '@','format'), '', true ));
625 $qo->incorrectfeedback['files'] = array();
626
627 $files = $this->getpath($question, array('#', 'incorrectfeedback', '0', '#', 'file'), array());
628 foreach ($files as $file) {
629 $data = new stdclass();
630 $data->content = $file['#'];
631 $data->name = $file['@']['name'];
632 $data->encoding = $file['@']['encoding'];
633 $qo->incorrectfeedback['files'][] = $data;
634 }
635
04d8268d
PP
636 $qo->unitgradingtype = $this->getpath( $question, array('#','unitgradingtype',0,'#'), 0 );
637 $qo->unitpenalty = $this->getpath( $question, array('#','unitpenalty',0,'#'), 0 );
638 $qo->showunits = $this->getpath( $question, array('#','showunits',0,'#'), 0 );
639 $qo->unitsleft = $this->getpath( $question, array('#','unitsleft',0,'#'), 0 );
640 $qo->instructions = $this->getpath( $question, array('#','instructions',0,'#','text',0,'#'), '', true );
cde2709a
DC
641
642 $files = $this->getpath($question, array('#', 'instructions', 0, '#', 'file', 0, '@'), '', false);
643
725ba2a0 644 // get answers array
cde2709a 645 // echo "<pre> question";print_r($question);echo "</pre>";
725ba2a0 646 $answers = $question['#']['answer'];
647 $qo->answers = array();
648 $qo->feedback = array();
649 $qo->fraction = array();
650 $qo->tolerance = array();
651 $qo->tolerancetype = array();
652 $qo->correctanswerformat = array();
653 $qo->correctanswerlength = array();
654 $qo->feedback = array();
655 foreach ($answers as $answer) {
cde2709a 656 $ans = $this->import_answer($answer);
725ba2a0 657 // answer outside of <text> is deprecated
cde2709a
DC
658 if (empty($ans->answer['text'])) {
659 $ans->answer['text'] = '*';
725ba2a0 660 }
cde2709a
DC
661 $qo->answers[] = $ans->answer;
662 $qo->feedback[] = $ans->feedback;
725ba2a0 663 $qo->tolerance[] = $answer['#']['tolerance'][0]['#'];
664 // fraction as a tag is deprecated
665 if (!empty($answer['#']['fraction'][0]['#'])) {
666 $qo->fraction[] = $answer['#']['fraction'][0]['#'];
cde2709a 667 } else {
725ba2a0 668 $qo->fraction[] = $answer['@']['fraction'] / 100;
669 }
670 $qo->tolerancetype[] = $answer['#']['tolerancetype'][0]['#'];
671 $qo->correctanswerformat[] = $answer['#']['correctanswerformat'][0]['#'];
672 $qo->correctanswerlength[] = $answer['#']['correctanswerlength'][0]['#'];
673 }
674 // get units array
675 $qo->unit = array();
676 if (isset($question['#']['units'][0]['#']['unit'])) {
677 $units = $question['#']['units'][0]['#']['unit'];
678 $qo->multiplier = array();
679 foreach ($units as $unit) {
680 $qo->multiplier[] = $unit['#']['multiplier'][0]['#'];
681 $qo->unit[] = $unit['#']['unit_name'][0]['#'];
682 }
683 }
cde2709a
DC
684 $instructions = $this->getpath($question, array('#', 'instructions'), array());
685 if (!empty($instructions)) {
686 $qo->instructions = array();
687 $qo->instructions['text'] = $instructions[0]['#']['text'][0]['#'];
688 $qo->instructions['format'] = $this->trans_format($instructions[0]['@']['format']);
689 $qo->instructionsfiles = array();
690 if (isset($instructions[0]['#']['file'])) {
691 $files = $instructions[0]['#']['file'];
692 foreach ($files as $file) {
693 $data = new stdclass;
694 $data->content = $file['#'];
695 $data->encoding = $file['@']['encoding'];
696 $data->name = $file['@']['name'];
697 $qo->instructionsfiles[] = $data;
698 }
699 }
700 }
701 $datasets = $question['#']['dataset_definitions'][0]['#']['dataset_definition'];
702 $qo->dataset = array();
703 $qo->datasetindex= 0 ;
725ba2a0 704 foreach ($datasets as $dataset) {
705 $qo->datasetindex++;
706 $qo->dataset[$qo->datasetindex] = new stdClass();
707 $qo->dataset[$qo->datasetindex]->status = $this->import_text( $dataset['#']['status'][0]['#']['text']);
708 $qo->dataset[$qo->datasetindex]->name = $this->import_text( $dataset['#']['name'][0]['#']['text']);
709 $qo->dataset[$qo->datasetindex]->type = $dataset['#']['type'][0]['#'];
710 $qo->dataset[$qo->datasetindex]->distribution = $this->import_text( $dataset['#']['distribution'][0]['#']['text']);
711 $qo->dataset[$qo->datasetindex]->max = $this->import_text( $dataset['#']['maximum'][0]['#']['text']);
712 $qo->dataset[$qo->datasetindex]->min = $this->import_text( $dataset['#']['minimum'][0]['#']['text']);
713 $qo->dataset[$qo->datasetindex]->length = $this->import_text( $dataset['#']['decimals'][0]['#']['text']);
714 $qo->dataset[$qo->datasetindex]->distribution = $this->import_text( $dataset['#']['distribution'][0]['#']['text']);
715 $qo->dataset[$qo->datasetindex]->itemcount = $dataset['#']['itemcount'][0]['#'];
716 $qo->dataset[$qo->datasetindex]->datasetitem = array();
717 $qo->dataset[$qo->datasetindex]->itemindex = 0;
718 $qo->dataset[$qo->datasetindex]->number_of_items=$dataset['#']['number_of_items'][0]['#'];
719 $datasetitems = $dataset['#']['dataset_items'][0]['#']['dataset_item'];
720 foreach ($datasetitems as $datasetitem) {
721 $qo->dataset[$qo->datasetindex]->itemindex++;
cde2709a
DC
722 $qo->dataset[$qo->datasetindex]->datasetitem[$qo->dataset[$qo->datasetindex]->itemindex] = new stdClass();
723 $qo->dataset[$qo->datasetindex]->datasetitem[$qo->dataset[$qo->datasetindex]->itemindex]->itemnumber = $datasetitem['#']['number'][0]['#']; //[0]['#']['number'][0]['#'] ; // [0]['numberitems'] ;//['#']['number'][0]['#'];// $datasetitems['#']['number'][0]['#'];
724 $qo->dataset[$qo->datasetindex]->datasetitem[$qo->dataset[$qo->datasetindex]->itemindex]->value = $datasetitem['#']['value'][0]['#'] ;//$datasetitem['#']['value'][0]['#'];
725 }
725ba2a0 726 }
88bc20c3 727
cde2709a 728 // echo "<pre>loaded qo";print_r($qo);echo "</pre>";
725ba2a0 729 return $qo;
730 }
731
ee259d0c 732 /**
733 * this is not a real question type. It's a dummy type used
734 * to specify the import category
735 * format is:
736 * <question type="category">
737 * <category>tom/dick/harry</category>
738 * </question>
739 */
740 function import_category( $question ) {
0c6b4d2e 741 $qo = new stdClass;
ee259d0c 742 $qo->qtype = 'category';
86b68520 743 $qo->category = $this->import_text($question['#']['category'][0]['#']['text']);
ee259d0c 744 return $qo;
745 }
746
c81415c7 747 /**
748 * parse the array of lines into an array of questions
749 * this *could* burn memory - but it won't happen that much
750 * so fingers crossed!
6e557c08 751 * @param array lines array of lines from the input file
752 * @return array (of objects) question objects
c81415c7 753 */
754 function readquestions($lines) {
84769fd8 755 // we just need it as one big string
756 $text = implode($lines, " ");
cde2709a 757 unset($lines);
84769fd8 758
759 // this converts xml to big nasty data structure
760 // the 0 means keep white space as it is (important for markdown format)
761 // print_r it if you want to see what it looks like!
cde2709a 762 $xml = xmlize($text, 0);
84769fd8 763
764 // set up array to hold all our questions
765 $questions = array();
766
767 // iterate through questions
768 foreach ($xml['quiz']['#']['question'] as $question) {
769 $question_type = $question['@']['type'];
770 $questiontype = get_string( 'questiontype','quiz',$question_type );
84769fd8 771
772 if ($question_type=='multichoice') {
773 $qo = $this->import_multichoice( $question );
88bc20c3 774 }
84769fd8 775 elseif ($question_type=='truefalse') {
776 $qo = $this->import_truefalse( $question );
777 }
778 elseif ($question_type=='shortanswer') {
779 $qo = $this->import_shortanswer( $question );
780 }
781 elseif ($question_type=='numerical') {
782 $qo = $this->import_numerical( $question );
783 }
7b8bc256 784 elseif ($question_type=='description') {
785 $qo = $this->import_description( $question );
786 }
51bcdf28 787 elseif ($question_type=='matching') {
788 $qo = $this->import_matching( $question );
789 }
7b8bc256 790 elseif ($question_type=='cloze') {
c81415c7 791 $qo = $this->import_multianswer( $question );
792 }
793 elseif ($question_type=='essay') {
794 $qo = $this->import_essay( $question );
7b8bc256 795 }
725ba2a0 796 elseif ($question_type=='calculated') {
797 $qo = $this->import_calculated( $question );
798 }
9c1c6c7f 799 elseif ($question_type=='calculatedsimple') {
800 $qo = $this->import_calculated( $question );
801 $qo->qtype = CALCULATEDSIMPLE ;
802 }
04d8268d
PP
803 elseif ($question_type=='calculatedmulti') {
804 $qo = $this->import_calculated( $question );
805 $qo->qtype = CALCULATEDMULTI ;
806 }
ee259d0c 807 elseif ($question_type=='category') {
808 $qo = $this->import_category( $question );
809 }
84769fd8 810 else {
a41e3287 811 // try for plugin support
88bc20c3 812 // no default question, as the plugin can call
a41e3287 813 // import_headers() itself if it wants to
88bc20c3 814 if (!$qo = $this->try_importing_using_qtypes( $question, null, null, $question_type)) {
a41e3287 815 $notsupported = get_string( 'xmltypeunsupported','quiz',$question_type );
816 $this->error( $notsupported );
817 $qo = null;
818 }
84769fd8 819 }
820
821 // stick the result in the $questions array
822 if ($qo) {
823 $questions[] = $qo;
824 }
825 }
84769fd8 826 return $questions;
827 }
828
829 // EXPORT FUNCTIONS START HERE
830
84769fd8 831 function export_file_extension() {
832 // override default type so extension is .xml
88bc20c3 833
84769fd8 834 return ".xml";
835 }
836
c81415c7 837
838 /**
839 * Turn the internal question code into a human readable form
840 * (The code used to be numeric, but this remains as some of
841 * the names don't match the new internal format)
6e557c08 842 * @param mixed type_id Internal code
843 * @return string question type string
c81415c7 844 */
84769fd8 845 function get_qtype( $type_id ) {
84769fd8 846 switch( $type_id ) {
847 case TRUEFALSE:
848 $name = 'truefalse';
849 break;
850 case MULTICHOICE:
851 $name = 'multichoice';
852 break;
853 case SHORTANSWER:
854 $name = 'shortanswer';
855 break;
856 case NUMERICAL:
857 $name = 'numerical';
858 break;
859 case MATCH:
860 $name = 'matching';
861 break;
862 case DESCRIPTION:
863 $name = 'description';
864 break;
865 case MULTIANSWER:
866 $name = 'cloze';
867 break;
c81415c7 868 case ESSAY:
869 $name = 'essay';
870 break;
725ba2a0 871 case CALCULATED:
872 $name = 'calculated';
873 break;
9c1c6c7f 874 case CALCULATEDSIMPLE:
875 $name = 'calculatedsimple';
876 break;
04d8268d
PP
877 case CALCULATEDMULTI:
878 $name = 'calculatedmulti';
879 break;
84769fd8 880 default:
a41e3287 881 $name = false;
84769fd8 882 }
883 return $name;
884 }
885
6e557c08 886 /**
c81415c7 887 * Convert internal Moodle text format code into
888 * human readable form
6e557c08 889 * @param int id internal code
890 * @return string format text
c81415c7 891 */
84769fd8 892 function get_format( $id ) {
84769fd8 893 switch( $id ) {
894 case 0:
895 $name = "moodle_auto_format";
896 break;
897 case 1:
898 $name = "html";
899 break;
900 case 2:
901 $name = "plain_text";
902 break;
903 case 3:
904 $name = "wiki_like";
905 break;
906 case 4:
907 $name = "markdown";
908 break;
909 default:
910 $name = "unknown";
911 }
912 return $name;
913 }
914
6e557c08 915 /**
88bc20c3 916 * Convert internal single question code into
c81415c7 917 * human readable form
6e557c08 918 * @param int id single question code
919 * @return string single question string
c81415c7 920 */
84769fd8 921 function get_single( $id ) {
84769fd8 922 switch( $id ) {
923 case 0:
924 $name = "false";
925 break;
926 case 1:
927 $name = "true";
928 break;
929 default:
930 $name = "unknown";
931 }
932 return $name;
933 }
934
6e557c08 935 /**
88bc20c3 936 * generates <text></text> tags, processing raw text therein
6e557c08 937 * @param int ilev the current indent level
938 * @param boolean short stick it on one line
939 * @return string formatted text
c81415c7 940 */
cde2709a 941 function writetext($raw, $ilev=0, $short=true) {
84769fd8 942 $indent = str_repeat( " ",$ilev );
943
3695cfa9 944 // if required add CDATA tags
945 if (!empty($raw) and (htmlspecialchars($raw)!=$raw)) {
946 $raw = "<![CDATA[$raw]]>";
947 }
84769fd8 948
949 if ($short) {
950 $xml = "$indent<text>$raw</text>\n";
951 }
952 else {
953 $xml = "$indent<text>\n$raw\n$indent</text>\n";
954 }
955
956 return $xml;
957 }
88bc20c3 958
81a7a02b 959 function xmltidy( $content ) {
960 // can only do this if tidy is installed
961 if (extension_loaded('tidy')) {
962 $config = array( 'input-xml'=>true, 'output-xml'=>true, 'indent'=>true, 'wrap'=>0 );
963 $tidy = new tidy;
964 $tidy->parseString($content, $config, 'utf8');
88bc20c3 965 $tidy->cleanRepair();
81a7a02b 966 return $tidy->value;
967 }
968 else {
969 return $content;
970 }
971 }
972
84769fd8 973
974 function presave_process( $content ) {
975 // override method to allow us to add xml headers and footers
976
81a7a02b 977 // add the xml headers and footers
46013523 978 $content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" .
84769fd8 979 "<quiz>\n" .
980 $content . "\n" .
981 "</quiz>";
982
81a7a02b 983 // make the xml look nice
984 $content = $this->xmltidy( $content );
985
84769fd8 986 return $content;
987 }
988
6e557c08 989 /**
c81415c7 990 * Turns question into an xml segment
cde2709a
DC
991 * @param object question object
992 * @param int context id
6e557c08 993 * @return string xml segment
c81415c7 994 */
cde2709a
DC
995 function writequestion($question) {
996 global $CFG, $QTYPES, $OUTPUT;
997
998 $fs = get_file_storage();
999 $contextid = $question->contextid;
84769fd8 1000 // initial string;
1001 $expout = "";
1002
1003 // add comment
1004 $expout .= "\n\n<!-- question: $question->id -->\n";
1005
a41e3287 1006 // check question type
1007 if (!$question_type = $this->get_qtype( $question->qtype )) {
1008 // must be a plugin then, so just accept the name supplied
1009 $question_type = $question->qtype;
46013523 1010 }
1011
84769fd8 1012 // add opening tag
f1abd39f 1013 // generates specific header for Cloze and category type question
1014 if ($question->qtype == 'category') {
6f8481ec 1015 $categorypath = $this->writetext( $question->category );
f1abd39f 1016 $expout .= " <question type=\"category\">\n";
1017 $expout .= " <category>\n";
6f8481ec 1018 $expout .= " $categorypath\n";
f1abd39f 1019 $expout .= " </category>\n";
1020 $expout .= " </question>\n";
1021 return $expout;
cde2709a 1022 } elseif ($question->qtype != MULTIANSWER) {
7b8bc256 1023 // for all question types except Close
cde2709a 1024 $name_text = $this->writetext($question->name);
7b8bc256 1025 $qtformat = $this->get_format($question->questiontextformat);
cde2709a
DC
1026 $generalfeedbackformat = $this->get_format($question->generalfeedbackformat);
1027
1028 $question_text = $this->writetext($question->questiontext);
1029 $question_text_files = $this->writefiles($question->questiontextfiles);
1030
1031 $generalfeedback = $this->writetext($question->generalfeedback);
1032 $generalfeedback_files = $this->writefiles($question->generalfeedbackfiles);
1033
88bc20c3 1034 $expout .= " <question type=\"$question_type\">\n";
7b8bc256 1035 $expout .= " <name>$name_text</name>\n";
1036 $expout .= " <questiontext format=\"$qtformat\">\n";
1037 $expout .= $question_text;
cde2709a 1038 $expout .= $question_text_files;
88bc20c3 1039 $expout .= " </questiontext>\n";
cde2709a 1040 $expout .= " <generalfeedback format=\"$generalfeedbackformat\">\n";
a4514d91 1041 $expout .= $generalfeedback;
cde2709a 1042 $expout .= $generalfeedback_files;
a4514d91 1043 $expout .= " </generalfeedback>\n";
c81415c7 1044 $expout .= " <defaultgrade>{$question->defaultgrade}</defaultgrade>\n";
7b8bc256 1045 $expout .= " <penalty>{$question->penalty}</penalty>\n";
1046 $expout .= " <hidden>{$question->hidden}</hidden>\n";
cde2709a 1047 } else {
7b8bc256 1048 // for Cloze type only
7b8bc256 1049 $name_text = $this->writetext( $question->name );
1050 $question_text = $this->writetext( $question->questiontext );
4ab4dcb2 1051 $generalfeedback = $this->writetext( $question->generalfeedback );
7b8bc256 1052 $expout .= " <question type=\"$question_type\">\n";
1053 $expout .= " <name>$name_text</name>\n";
1054 $expout .= " <questiontext>\n";
1055 $expout .= $question_text;
1056 $expout .= " </questiontext>\n";
4ab4dcb2 1057 $expout .= " <generalfeedback>\n";
1058 $expout .= $generalfeedback;
1059 $expout .= " </generalfeedback>\n";
7b8bc256 1060 }
1061
d08e16b2 1062 if (!empty($question->options->shuffleanswers)) {
1063 $expout .= " <shuffleanswers>{$question->options->shuffleanswers}</shuffleanswers>\n";
1064 }
1065 else {
1066 $expout .= " <shuffleanswers>0</shuffleanswers>\n";
1067 }
84769fd8 1068
1069 // output depends on question type
1070 switch($question->qtype) {
f1abd39f 1071 case 'category':
1072 // not a qtype really - dummy used for category switching
88bc20c3 1073 break;
84769fd8 1074 case TRUEFALSE:
36e2232e 1075 foreach ($question->options->answers as $answer) {
1076 $fraction_pc = round( $answer->fraction * 100 );
3246ed33 1077 if ($answer->id == $question->options->trueanswer) {
1078 $answertext = 'true';
1079 } else {
1080 $answertext = 'false';
88bc20c3 1081 }
36e2232e 1082 $expout .= " <answer fraction=\"$fraction_pc\">\n";
3246ed33 1083 $expout .= $this->writetext($answertext, 3) . "\n";
cde2709a
DC
1084 $feedbackformat = $this->get_format($answer->feedbackformat);
1085 $expout .= " <feedback format=\"$feedbackformat\">\n";
1086 $expout .= $this->writetext($answer->feedback,4,false);
1087 $expout .= $this->writefiles($answer->feedbackfiles);
36e2232e 1088 $expout .= " </feedback>\n";
1089 $expout .= " </answer>\n";
1090 }
84769fd8 1091 break;
1092 case MULTICHOICE:
1093 $expout .= " <single>".$this->get_single($question->options->single)."</single>\n";
307f045f 1094 $expout .= " <shuffleanswers>".$this->get_single($question->options->shuffleanswers)."</shuffleanswers>\n";
cde2709a
DC
1095
1096 $textformat = $this->get_format($question->options->correctfeedbackformat);
1097 $files = $fs->get_area_files($contextid, 'qtype_multichoice', 'correctfeedback', $question->id);
1098 $expout .= " <correctfeedback format=\"$textformat\">\n";
1099 $expout .= $this->writetext($question->options->correctfeedback, 3);
1100 $expout .= $this->writefiles($files);
1101 $expout .= " </correctfeedback>\n";
1102
1103 $textformat = $this->get_format($question->options->partiallycorrectfeedbackformat);
1104 $files = $fs->get_area_files($contextid, 'qtype_multichoice', 'partiallycorrectfeedback', $question->id);
1105 $expout .= " <partiallycorrectfeedback format=\"$textformat\">\n";
1106 $expout .= $this->writetext($question->options->partiallycorrectfeedback, 3);
1107 $expout .= $this->writefiles($files);
1108 $expout .= " </partiallycorrectfeedback>\n";
1109
1110 $textformat = $this->get_format($question->options->incorrectfeedbackformat);
1111 $files = $fs->get_area_files($contextid, 'qtype_multichoice', 'incorrectfeedback', $question->id);
1112 $expout .= " <incorrectfeedback format=\"$textformat\">\n";
1113 $expout .= $this->writetext($question->options->incorrectfeedback, 3);
1114 $expout .= $this->writefiles($files);
1115 $expout .= " </incorrectfeedback>\n";
1116
04d8268d 1117 $expout .= " <answernumbering>".$this->writetext($question->options->answernumbering, 3)."</answernumbering>\n";
84769fd8 1118 foreach($question->options->answers as $answer) {
1119 $percent = $answer->fraction * 100;
1120 $expout .= " <answer fraction=\"$percent\">\n";
cde2709a
DC
1121 $expout .= $this->writetext($answer->answer,4,false);
1122 $feedbackformat = $this->get_format($answer->feedbackformat);
1123 $expout .= " <feedback format=\"$feedbackformat\">\n";
1124 $expout .= $this->writetext($answer->feedback,5,false);
1125 $expout .= $this->writefiles($answer->feedbackfiles);
84769fd8 1126 $expout .= " </feedback>\n";
1127 $expout .= " </answer>\n";
1128 }
1129 break;
1130 case SHORTANSWER:
cde2709a 1131 $expout .= " <usecase>{$question->options->usecase}</usecase>\n ";
84769fd8 1132 foreach($question->options->answers as $answer) {
1133 $percent = 100 * $answer->fraction;
1134 $expout .= " <answer fraction=\"$percent\">\n";
1135 $expout .= $this->writetext( $answer->answer,3,false );
cde2709a
DC
1136 $feedbackformat = $this->get_format($answer->feedbackformat);
1137 $expout .= " <feedback format=\"$feedbackformat\">\n";
1138 $expout .= $this->writetext($answer->feedback);
1139 $expout .= $this->writefiles($answer->feedbackfiles);
84769fd8 1140 $expout .= " </feedback>\n";
1141 $expout .= " </answer>\n";
1142 }
1143 break;
1144 case NUMERICAL:
1145 foreach ($question->options->answers as $answer) {
1146 $tolerance = $answer->tolerance;
55c54868 1147 $percent = 100 * $answer->fraction;
1148 $expout .= "<answer fraction=\"$percent\">\n";
1149 // <text> tags are an added feature, old filed won't have them
1150 $expout .= " <text>{$answer->answer}</text>\n";
84769fd8 1151 $expout .= " <tolerance>$tolerance</tolerance>\n";
cde2709a
DC
1152 $feedbackformat = $this->get_format($answer->feedbackformat);
1153 $expout .= " <feedback format=\"$feedbackformat\">\n";
1154 $expout .= $this->writetext($answer->feedback);
1155 $expout .= $this->writefiles($answer->feedbackfiles);
1156 $expout .= " </feedback>\n";
55c54868 1157 // fraction tag is deprecated
1158 // $expout .= " <fraction>{$answer->fraction}</fraction>\n";
84769fd8 1159 $expout .= "</answer>\n";
1160 }
1161
1162 $units = $question->options->units;
1163 if (count($units)) {
1164 $expout .= "<units>\n";
1165 foreach ($units as $unit) {
1166 $expout .= " <unit>\n";
1167 $expout .= " <multiplier>{$unit->multiplier}</multiplier>\n";
1168 $expout .= " <unit_name>{$unit->unit}</unit_name>\n";
1169 $expout .= " </unit>\n";
1170 }
1171 $expout .= "</units>\n";
1172 }
cde2709a
DC
1173 if (!empty($question->options->instructionsformat)) {
1174 $textformat = $this->get_format($question->options->instructionsformat);
1175 $files = $fs->get_area_files($contextid, 'qtype_numerical', 'instruction', $question->id);
1176 $expout .= " <instructions format=\"$textformat\">\n";
1177 $expout .= $this->writetext($question->options->instructions, 3);
1178 $expout .= $this->writefiles($files);
1179 $expout .= " </instructions>\n";
1180 }
84769fd8 1181 break;
1182 case MATCH:
1183 foreach($question->options->subquestions as $subquestion) {
cde2709a
DC
1184 $files = $fs->get_area_files($contextid, 'qtype_match', 'subquestion', $subquestion->id);
1185 $textformat = $this->get_format($subquestion->questiontextformat);
1186 $expout .= "<subquestion format=\"$textformat\">\n";
1187 $expout .= $this->writetext($subquestion->questiontext);
1188 $expout .= $this->writefiles($files);
1189 $expout .= "<answer>";
1190 $expout .= $this->writetext($subquestion->answertext);
1191 $expout .= "</answer>\n";
84769fd8 1192 $expout .= "</subquestion>\n";
1193 }
1194 break;
1195 case DESCRIPTION:
c81415c7 1196 // nothing more to do for this type
84769fd8 1197 break;
1198 case MULTIANSWER:
7b8bc256 1199 $a_count=1;
1200 foreach($question->options->questions as $question) {
9cc41dfd 1201 $thispattern = preg_quote("{#".$a_count."}"); //TODO: is this really necessary?
7b8bc256 1202 $thisreplace = $question->questiontext;
6dbcacee 1203 $expout=preg_replace("~$thispattern~", $thisreplace, $expout );
7b8bc256 1204 $a_count++;
1205 }
1206 break;
c81415c7 1207 case ESSAY:
33ed59a2 1208 if (!empty($question->options->answers)) {
1209 foreach ($question->options->answers as $answer) {
1210 $percent = 100 * $answer->fraction;
1211 $expout .= "<answer fraction=\"$percent\">\n";
cde2709a
DC
1212 $feedbackformat = $this->get_format($answer->feedbackformat);
1213 $expout .= " <feedback format=\"$feedbackformat\">\n";
1214 $expout .= $this->writetext($answer->feedback);
1215 $expout .= $this->writefiles($answer->feedbackfiles);
1216 $expout .= " </feedback>\n";
33ed59a2 1217 // fraction tag is deprecated
1218 // $expout .= " <fraction>{$answer->fraction}</fraction>\n";
1219 $expout .= "</answer>\n";
1220 }
c81415c7 1221 }
725ba2a0 1222 break;
1223 case CALCULATED:
9c1c6c7f 1224 case CALCULATEDSIMPLE:
04d8268d
PP
1225 case CALCULATEDMULTI:
1226 $expout .= " <synchronize>{$question->options->synchronize}</synchronize>\n";
1227 $expout .= " <single>{$question->options->single}</single>\n";
1228 $expout .= " <answernumbering>{$question->options->answernumbering}</answernumbering>\n";
1229 $expout .= " <shuffleanswers>".$this->writetext($question->options->shuffleanswers, 3)."</shuffleanswers>\n";
cde2709a
DC
1230
1231 $component = 'qtype_' . $question->qtype;
1232 $files = $fs->get_area_files($contextid, $component, 'correctfeedback', $question->id);
1233 $expout .= " <correctfeedback>\n";
1234 $expout .= $this->writetext($question->options->correctfeedback, 3);
1235 $expout .= $this->writefiles($files);
1236 $expout .= " </correctfeedback>\n";
1237
1238 $files = $fs->get_area_files($contextid, $component, 'partiallycorrectfeedback', $question->id);
1239 $expout .= " <partiallycorrectfeedback>\n";
1240 $expout .= $this->writetext($question->options->partiallycorrectfeedback, 3);
1241 $expout .= $this->writefiles($files);
1242 $expout .= " </partiallycorrectfeedback>\n";
1243
1244 $files = $fs->get_area_files($contextid, $component, 'incorrectfeedback', $question->id);
1245 $expout .= " <incorrectfeedback>\n";
1246 $expout .= $this->writetext($question->options->incorrectfeedback, 3);
1247 $expout .= $this->writefiles($files);
1248 $expout .= " </incorrectfeedback>\n";
1249
725ba2a0 1250 foreach ($question->options->answers as $answer) {
1251 $tolerance = $answer->tolerance;
1252 $tolerancetype = $answer->tolerancetype;
1253 $correctanswerlength= $answer->correctanswerlength ;
1254 $correctanswerformat= $answer->correctanswerformat;
1255 $percent = 100 * $answer->fraction;
1256 $expout .= "<answer fraction=\"$percent\">\n";
1257 // "<text/>" tags are an added feature, old files won't have them
1258 $expout .= " <text>{$answer->answer}</text>\n";
1259 $expout .= " <tolerance>$tolerance</tolerance>\n";
1260 $expout .= " <tolerancetype>$tolerancetype</tolerancetype>\n";
1261 $expout .= " <correctanswerformat>$correctanswerformat</correctanswerformat>\n";
a800639b 1262 $expout .= " <correctanswerlength>$correctanswerlength</correctanswerlength>\n";
cde2709a
DC
1263 $feedbackformat = $this->get_format($answer->feedbackformat);
1264 $expout .= " <feedback format=\"$feedbackformat\">\n";
1265 $expout .= $this->writetext($answer->feedback);
1266 $expout .= $this->writefiles($answer->feedbackfiles);
1267 $expout .= " </feedback>\n";
725ba2a0 1268 $expout .= "</answer>\n";
1269 }
cde2709a
DC
1270 if (!empty($question->options->unitgradingtype)) {
1271 $expout .= " <unitgradingtype>{$question->options->unitgradingtype}</unitgradingtype>\n";
1272 }
1273 if (!empty($question->options->unitpenalty)) {
1274 $expout .= " <unitpenalty>{$question->options->unitpenalty}</unitpenalty>\n";
1275 }
1276 if (!empty($question->options->showunits)) {
1277 $expout .= " <showunits>{$question->options->showunits}</showunits>\n";
1278 }
1279 if (!empty($question->options->unitsleft)) {
1280 $expout .= " <unitsleft>{$question->options->unitsleft}</unitsleft>\n";
1281 }
1282
1283 if (!empty($question->options->instructionsformat)) {
1284 $textformat = $this->get_format($question->options->instructionsformat);
1285 $files = $fs->get_area_files($contextid, $component, 'instruction', $question->id);
1286 $expout .= " <instructions format=\"$textformat\">\n";
1287 $expout .= $this->writetext($question->options->instructions, 3);
1288 $expout .= $this->writefiles($files);
1289 $expout .= " </instructions>\n";
1290 }
1291
1292 if (isset($question->options->units)) {
1293 $units = $question->options->units;
1294 if (count($units)) {
1295 $expout .= "<units>\n";
1296 foreach ($units as $unit) {
1297 $expout .= " <unit>\n";
1298 $expout .= " <multiplier>{$unit->multiplier}</multiplier>\n";
1299 $expout .= " <unit_name>{$unit->unit}</unit_name>\n";
1300 $expout .= " </unit>\n";
1301 }
1302 $expout .= "</units>\n";
725ba2a0 1303 }
cde2709a 1304 }
9c1c6c7f 1305 //The tag $question->export_process has been set so we get all the data items in the database
1306 // from the function $QTYPES['calculated']->get_question_options(&$question);
1307 // calculatedsimple defaults to calculated
c9e4ba36 1308 if( isset($question->options->datasets)&&count($question->options->datasets)){// there should be
725ba2a0 1309 $expout .= "<dataset_definitions>\n";
c9e4ba36 1310 foreach ($question->options->datasets as $def) {
725ba2a0 1311 $expout .= "<dataset_definition>\n";
1312 $expout .= " <status>".$this->writetext($def->status)."</status>\n";
1313 $expout .= " <name>".$this->writetext($def->name)."</name>\n";
9c1c6c7f 1314 if ( $question->qtype == CALCULATED){
1315 $expout .= " <type>calculated</type>\n";
1316 }else {
1317 $expout .= " <type>calculatedsimple</type>\n";
1318 }
725ba2a0 1319 $expout .= " <distribution>".$this->writetext($def->distribution)."</distribution>\n";
1320 $expout .= " <minimum>".$this->writetext($def->minimum)."</minimum>\n";
1321 $expout .= " <maximum>".$this->writetext($def->maximum)."</maximum>\n";
88bc20c3 1322 $expout .= " <decimals>".$this->writetext($def->decimals)."</decimals>\n";
725ba2a0 1323 $expout .= " <itemcount>$def->itemcount</itemcount>\n";
1324 if ($def->itemcount > 0 ) {
1325 $expout .= " <dataset_items>\n";
1326 foreach ($def->items as $item ){
1327 $expout .= " <dataset_item>\n";
1328 $expout .= " <number>".$item->itemnumber."</number>\n";
1329 $expout .= " <value>".$item->value."</value>\n";
1330 $expout .= " </dataset_item>\n";
88bc20c3 1331 }
725ba2a0 1332 $expout .= " </dataset_items>\n";
1333 $expout .= " <number_of_items>".$def-> number_of_items."</number_of_items>\n";
1334 }
1335 $expout .= "</dataset_definition>\n";
88bc20c3 1336 }
1337 $expout .= "</dataset_definitions>\n";
1338 }
c81415c7 1339 break;
84769fd8 1340 default:
a41e3287 1341 // try support by optional plugin
88bc20c3 1342 if (!$data = $this->try_exporting_using_qtypes( $question->qtype, $question )) {
fef8f84e 1343 echo $OUTPUT->notification( get_string( 'unsupportedexport','qformat_xml',$QTYPES[$question->qtype]->local_name() ) );
a41e3287 1344 }
392238d6 1345 $expout .= $data;
84769fd8 1346 }
1347
1348 // close the question tag
1349 $expout .= "</question>\n";
1350
cde2709a
DC
1351 // XXX: debuging
1352 //echo '<textarea cols=100 rows=20>';
1353 //echo $expout;
1354 //echo '</textarea>';
1355
84769fd8 1356 return $expout;
1357 }
1358}