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