rss MDL-25076 made forum rss feeds that are displaying discussions instead of posts...
[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 */
119 function getpath( $xml, $path, $default, $istext=false, $error='' ) {
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 }
294ce987 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 */
84769fd8 147 function import_headers( $question ) {
46013523 148 // get some error strings
149 $error_noname = get_string( 'xmlimportnoname','quiz' );
150 $error_noquestion = get_string( 'xmlimportnoquestion','quiz' );
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 );
6af98025 157 $qo->questiontext = $this->getpath( $question, array('#','questiontext',0,'#','text',0,'#'), '', true );
688d8753
TH
158 $qo->questiontextformat = $this->trans_format($this->getpath(
159 $question, array('#','questiontext',0,'@','format'), ''));
e7ef42f5 160 $qo->image = $this->getpath( $question, array('#','image',0,'#'), $qo->image );
46013523 161 $image_base64 = $this->getpath( $question, array('#','image_base64','0','#'),'' );
162 if (!empty($image_base64)) {
cdfaa838 163 $qo->image = $this->importimagefile( $qo->image, $image_base64);
46013523 164 }
165 $qo->generalfeedback = $this->getpath( $question, array('#','generalfeedback',0,'#','text',0,'#'), $qo->generalfeedback, true );
166 $qo->defaultgrade = $this->getpath( $question, array('#','defaultgrade',0,'#'), $qo->defaultgrade );
167 $qo->penalty = $this->getpath( $question, array('#','penalty',0,'#'), $qo->penalty );
84769fd8 168
169 return $qo;
170 }
171
6e557c08 172 /**
c81415c7 173 * import the common parts of a single answer
6e557c08 174 * @param array answer xml tree for single answer
175 * @return object answer object
88bc20c3 176 */
84769fd8 177 function import_answer( $answer ) {
81a7a02b 178 $fraction = $this->getpath( $answer, array('@','fraction'),0 );
179 $text = $this->getpath( $answer, array('#','text',0,'#'), '', true );
180 $feedback = $this->getpath( $answer, array('#','feedback',0,'#','text',0,'#'), '', true );
84769fd8 181
182 $ans = null;
183 $ans->answer = $text;
184 $ans->fraction = $fraction / 100;
185 $ans->feedback = $feedback;
84769fd8 186 return $ans;
187 }
188
6e557c08 189 /**
88bc20c3 190 * import multiple choice question
6e557c08 191 * @param array question question array from xml tree
192 * @return object question object
c81415c7 193 */
84769fd8 194 function import_multichoice( $question ) {
84769fd8 195 // get common parts
196 $qo = $this->import_headers( $question );
197
198 // 'header' parts particular to multichoice
199 $qo->qtype = MULTICHOICE;
81a7a02b 200 $single = $this->getpath( $question, array('#','single',0,'#'), 'true' );
84769fd8 201 $qo->single = $this->trans_single( $single );
81a7a02b 202 $shuffleanswers = $this->getpath( $question, array('#','shuffleanswers',0,'#'), 'false' );
2377f8dc 203 $qo->answernumbering = $this->getpath( $question, array('#','answernumbering',0,'#'), 'abc' );
2da44816 204 $qo->shuffleanswers = $this->trans_single($shuffleanswers);
81a7a02b 205 $qo->correctfeedback = $this->getpath( $question, array('#','correctfeedback',0,'#','text',0,'#'), '', true );
206 $qo->partiallycorrectfeedback = $this->getpath( $question, array('#','partiallycorrectfeedback',0,'#','text',0,'#'), '', true );
207 $qo->incorrectfeedback = $this->getpath( $question, array('#','incorrectfeedback',0,'#','text',0,'#'), '', true );
c0ffeb39 208
209 // There was a time on the 1.8 branch when it could output an empty answernumbering tag, so fix up any found.
210 if (empty($qo->answernumbering)) {
211 $qo->answernumbering = 'abc';
212 }
213
84769fd8 214 // run through the answers
88bc20c3 215 $answers = $question['#']['answer'];
84769fd8 216 $a_count = 0;
217 foreach ($answers as $answer) {
218 $ans = $this->import_answer( $answer );
219 $qo->answer[$a_count] = $ans->answer;
220 $qo->fraction[$a_count] = $ans->fraction;
221 $qo->feedback[$a_count] = $ans->feedback;
222 ++$a_count;
223 }
84769fd8 224 return $qo;
225 }
226
6e557c08 227 /**
c81415c7 228 * import cloze type question
6e557c08 229 * @param array question question array from xml tree
230 * @return object question object
c81415c7 231 */
7b8bc256 232 function import_multianswer( $questions ) {
0c6b4d2e 233 $questiontext = $questions['#']['questiontext'][0]['#']['text'];
234 $qo = qtype_multianswer_extract_question($this->import_text($questiontext));
7b8bc256 235
236 // 'header' parts particular to multianswer
237 $qo->qtype = MULTIANSWER;
238 $qo->course = $this->course;
4ab4dcb2 239 $qo->generalfeedback = $this->getpath( $questions, array('#','generalfeedback',0,'#','text',0,'#'), '', true );
7b8bc256 240
71ffbac2 241 if (!empty($questions)) {
7b8bc256 242 $qo->name = $this->import_text( $questions['#']['name'][0]['#']['text'] );
243 }
244
245 return $qo;
246 }
247
6e557c08 248 /**
c81415c7 249 * import true/false type question
6e557c08 250 * @param array question question array from xml tree
251 * @return object question object
c81415c7 252 */
84769fd8 253 function import_truefalse( $question ) {
84769fd8 254 // get common parts
fef8f84e 255 global $OUTPUT;
84769fd8 256 $qo = $this->import_headers( $question );
257
258 // 'header' parts particular to true/false
259 $qo->qtype = TRUEFALSE;
260
261 // get answer info
3246ed33 262 //
263 // In the past, it used to be assumed that the two answers were in the file
264 // true first, then false. Howevever that was not always true. Now, we
265 // try to match on the answer text, but in old exports, this will be a localised
266 // string, so if we don't find true or false, we fall back to the old system.
267 $first = true;
268 $warning = false;
269 foreach ($question['#']['answer'] as $answer) {
81a7a02b 270 $answertext = $this->getpath( $answer, array('#','text',0,'#'), '', true );
271 $feedback = $this->getpath($answer, array('#','feedback',0,'#','text',0,'#'), '', true );
3246ed33 272 if ($answertext != 'true' && $answertext != 'false') {
273 $warning = true;
274 $answertext = $first ? 'true' : 'false'; // Old style file, assume order is true/false.
88bc20c3 275 }
3246ed33 276 if ($answertext == 'true') {
277 $qo->answer = ($answer['@']['fraction'] == 100);
7939a4a0 278 $qo->correctanswer = $qo->answer;
3246ed33 279 $qo->feedbacktrue = $feedback;
280 } else {
281 $qo->answer = ($answer['@']['fraction'] != 100);
7939a4a0 282 $qo->correctanswer = $qo->answer;
3246ed33 283 $qo->feedbackfalse = $feedback;
284 }
285 $first = false;
84769fd8 286 }
3246ed33 287
288 if ($warning) {
289 $a = new stdClass;
55c54868 290 $a->questiontext = $qo->questiontext;
3246ed33 291 $a->answer = get_string($qo->answer ? 'true' : 'false', 'quiz');
fef8f84e 292 echo $OUTPUT->notification(get_string('truefalseimporterror', 'quiz', $a));
84769fd8 293 }
84769fd8 294 return $qo;
295 }
296
6e557c08 297 /**
c81415c7 298 * import short answer type question
6e557c08 299 * @param array question question array from xml tree
300 * @return object question object
c81415c7 301 */
84769fd8 302 function import_shortanswer( $question ) {
84769fd8 303 // get common parts
304 $qo = $this->import_headers( $question );
305
306 // header parts particular to shortanswer
307 $qo->qtype = SHORTANSWER;
308
309 // get usecase
46013523 310 $qo->usecase = $this->getpath($question, array('#','usecase',0,'#'), $qo->usecase );
84769fd8 311
312 // run through the answers
88bc20c3 313 $answers = $question['#']['answer'];
84769fd8 314 $a_count = 0;
315 foreach ($answers as $answer) {
316 $ans = $this->import_answer( $answer );
317 $qo->answer[$a_count] = $ans->answer;
318 $qo->fraction[$a_count] = $ans->fraction;
319 $qo->feedback[$a_count] = $ans->feedback;
320 ++$a_count;
321 }
322
323 return $qo;
324 }
88bc20c3 325
6e557c08 326 /**
c81415c7 327 * import description type question
6e557c08 328 * @param array question question array from xml tree
329 * @return object question object
c81415c7 330 */
7b8bc256 331 function import_description( $question ) {
332 // get common parts
333 $qo = $this->import_headers( $question );
334 // header parts particular to shortanswer
335 $qo->qtype = DESCRIPTION;
3f5633df 336 $qo->defaultgrade = 0;
337 $qo->length = 0;
7b8bc256 338 return $qo;
339 }
84769fd8 340
6e557c08 341 /**
c81415c7 342 * import numerical type question
6e557c08 343 * @param array question question array from xml tree
344 * @return object question object
c81415c7 345 */
346 function import_numerical( $question ) {
84769fd8 347 // get common parts
348 $qo = $this->import_headers( $question );
349
350 // header parts particular to numerical
351 $qo->qtype = NUMERICAL;
352
353 // get answers array
354 $answers = $question['#']['answer'];
355 $qo->answer = array();
356 $qo->feedback = array();
357 $qo->fraction = array();
358 $qo->tolerance = array();
359 foreach ($answers as $answer) {
55c54868 360 // answer outside of <text> is deprecated
81a7a02b 361 $answertext = trim( $this->getpath( $answer, array('#',0), '' ) );
362 $qo->answer[] = $this->getpath( $answer, array('#','text',0,'#'), $answertext, true );
363 if (empty($qo->answer)) {
364 $qo->answer = '*';
55c54868 365 }
81a7a02b 366 $qo->feedback[] = $this->getpath( $answer, array('#','feedback',0,'#','text',0,'#'), '', true );
367 $qo->tolerance[] = $this->getpath( $answer, array('#','tolerance',0,'#'), 0 );
55c54868 368
369 // fraction as a tag is deprecated
81a7a02b 370 $fraction = $this->getpath( $answer, array('@','fraction'), 0 ) / 100;
371 $qo->fraction[] = $this->getpath( $answer, array('#','fraction',0,'#'), $fraction ); // deprecated
84769fd8 372 }
373
374 // get units array
84769fd8 375 $qo->unit = array();
81a7a02b 376 $units = $this->getpath( $question, array('#','units',0,'#','unit'), array() );
377 if (!empty($units)) {
a0d187bf 378 $qo->multiplier = array();
379 foreach ($units as $unit) {
81a7a02b 380 $qo->multiplier[] = $this->getpath( $unit, array('#','multiplier',0,'#'), 1 );
381 $qo->unit[] = $this->getpath( $unit, array('#','unit_name',0,'#'), '', true );
a0d187bf 382 }
84769fd8 383 }
84769fd8 384 return $qo;
385 }
386
6e557c08 387 /**
c81415c7 388 * import matching type question
6e557c08 389 * @param array question question array from xml tree
390 * @return object question object
c81415c7 391 */
51bcdf28 392 function import_matching( $question ) {
51bcdf28 393 // get common parts
394 $qo = $this->import_headers( $question );
395
396 // header parts particular to matching
397 $qo->qtype = MATCH;
81a7a02b 398 $qo->shuffleanswers = $this->getpath( $question, array( '#','shuffleanswers',0,'#' ), 1 );
51bcdf28 399
400 // get subquestions
401 $subquestions = $question['#']['subquestion'];
402 $qo->subquestions = array();
403 $qo->subanswers = array();
404
405 // run through subquestions
406 foreach ($subquestions as $subquestion) {
81a7a02b 407 $qo->subquestions[] = $this->getpath( $subquestion, array('#','text',0,'#'), '', true );
408 $qo->subanswers[] = $this->getpath( $subquestion, array('#','answer',0,'#','text',0,'#'), '', true);
51bcdf28 409 }
51bcdf28 410 return $qo;
411 }
412
6e557c08 413 /**
c81415c7 414 * import essay type question
6e557c08 415 * @param array question question array from xml tree
416 * @return object question object
c81415c7 417 */
418 function import_essay( $question ) {
419 // get common parts
420 $qo = $this->import_headers( $question );
421
422 // header parts particular to essay
423 $qo->qtype = ESSAY;
424
425 // get feedback
88bc20c3 426 $qo->feedback = $this->getpath( $question, array('#','answer',0,'#','feedback',0,'#','text',0,'#'), '', true );
55c54868 427
428 // get fraction - <fraction> tag is deprecated
6af98025 429 $qo->fraction = $this->getpath( $question, array('@','fraction'), 0 ) / 100;
430 $q0->fraction = $this->getpath( $question, array('#','fraction',0,'#'), $qo->fraction );
c81415c7 431
432 return $qo;
433 }
84769fd8 434
725ba2a0 435 function import_calculated( $question ) {
04d8268d 436 // import calculated question
725ba2a0 437
438 // get common parts
439 $qo = $this->import_headers( $question );
440
04d8268d 441 // header parts particular to calculated
725ba2a0 442 $qo->qtype = CALCULATED ;//CALCULATED;
505f7342 443 $qo->synchronize = $this->getpath( $question, array( '#','synchronize',0,'#' ), 0 );
04d8268d
PP
444 $single = $this->getpath( $question, array('#','single',0,'#'), 'true' );
445 $qo->single = $this->trans_single( $single );
446 $shuffleanswers = $this->getpath( $question, array('#','shuffleanswers',0,'#'), 'false' );
447 $qo->answernumbering = $this->getpath( $question, array('#','answernumbering',0,'#'), 'abc' );
448 $qo->shuffleanswers = $this->trans_single($shuffleanswers);
449 $qo->correctfeedback = $this->getpath( $question, array('#','correctfeedback',0,'#','text',0,'#'), '', true );
450 $qo->partiallycorrectfeedback = $this->getpath( $question, array('#','partiallycorrectfeedback',0,'#','text',0,'#'), '', true );
451 $qo->incorrectfeedback = $this->getpath( $question, array('#','incorrectfeedback',0,'#','text',0,'#'), '', true );
452 $qo->unitgradingtype = $this->getpath( $question, array('#','unitgradingtype',0,'#'), 0 );
453 $qo->unitpenalty = $this->getpath( $question, array('#','unitpenalty',0,'#'), 0 );
454 $qo->showunits = $this->getpath( $question, array('#','showunits',0,'#'), 0 );
455 $qo->unitsleft = $this->getpath( $question, array('#','unitsleft',0,'#'), 0 );
456 $qo->instructions = $this->getpath( $question, array('#','instructions',0,'#','text',0,'#'), '', true );
725ba2a0 457 // get answers array
458 // echo "<pre> question";print_r($question);echo "</pre>";
459 $answers = $question['#']['answer'];
460 $qo->answers = array();
461 $qo->feedback = array();
462 $qo->fraction = array();
463 $qo->tolerance = array();
464 $qo->tolerancetype = array();
465 $qo->correctanswerformat = array();
466 $qo->correctanswerlength = array();
467 $qo->feedback = array();
468 foreach ($answers as $answer) {
469 // answer outside of <text> is deprecated
470 if (!empty( $answer['#']['text'] )) {
471 $answertext = $this->import_text( $answer['#']['text'] );
472 }
473 else {
474 $answertext = trim($answer['#'][0]);
475 }
476 if ($answertext == '') {
477 $qo->answers[] = '*';
478 } else {
479 $qo->answers[] = $answertext;
480 }
481 $qo->feedback[] = $this->import_text( $answer['#']['feedback'][0]['#']['text'] );
482 $qo->tolerance[] = $answer['#']['tolerance'][0]['#'];
483 // fraction as a tag is deprecated
484 if (!empty($answer['#']['fraction'][0]['#'])) {
485 $qo->fraction[] = $answer['#']['fraction'][0]['#'];
486 }
487 else {
488 $qo->fraction[] = $answer['@']['fraction'] / 100;
489 }
490 $qo->tolerancetype[] = $answer['#']['tolerancetype'][0]['#'];
491 $qo->correctanswerformat[] = $answer['#']['correctanswerformat'][0]['#'];
492 $qo->correctanswerlength[] = $answer['#']['correctanswerlength'][0]['#'];
493 }
494 // get units array
495 $qo->unit = array();
496 if (isset($question['#']['units'][0]['#']['unit'])) {
497 $units = $question['#']['units'][0]['#']['unit'];
498 $qo->multiplier = array();
499 foreach ($units as $unit) {
500 $qo->multiplier[] = $unit['#']['multiplier'][0]['#'];
501 $qo->unit[] = $unit['#']['unit_name'][0]['#'];
502 }
503 }
504 $datasets = $question['#']['dataset_definitions'][0]['#']['dataset_definition'];
505 $qo->dataset = array();
506 $qo->datasetindex= 0 ;
507 foreach ($datasets as $dataset) {
508 $qo->datasetindex++;
509 $qo->dataset[$qo->datasetindex] = new stdClass();
510 $qo->dataset[$qo->datasetindex]->status = $this->import_text( $dataset['#']['status'][0]['#']['text']);
511 $qo->dataset[$qo->datasetindex]->name = $this->import_text( $dataset['#']['name'][0]['#']['text']);
512 $qo->dataset[$qo->datasetindex]->type = $dataset['#']['type'][0]['#'];
513 $qo->dataset[$qo->datasetindex]->distribution = $this->import_text( $dataset['#']['distribution'][0]['#']['text']);
514 $qo->dataset[$qo->datasetindex]->max = $this->import_text( $dataset['#']['maximum'][0]['#']['text']);
515 $qo->dataset[$qo->datasetindex]->min = $this->import_text( $dataset['#']['minimum'][0]['#']['text']);
516 $qo->dataset[$qo->datasetindex]->length = $this->import_text( $dataset['#']['decimals'][0]['#']['text']);
517 $qo->dataset[$qo->datasetindex]->distribution = $this->import_text( $dataset['#']['distribution'][0]['#']['text']);
518 $qo->dataset[$qo->datasetindex]->itemcount = $dataset['#']['itemcount'][0]['#'];
519 $qo->dataset[$qo->datasetindex]->datasetitem = array();
520 $qo->dataset[$qo->datasetindex]->itemindex = 0;
521 $qo->dataset[$qo->datasetindex]->number_of_items=$dataset['#']['number_of_items'][0]['#'];
522 $datasetitems = $dataset['#']['dataset_items'][0]['#']['dataset_item'];
523 foreach ($datasetitems as $datasetitem) {
524 $qo->dataset[$qo->datasetindex]->itemindex++;
525 $qo->dataset[$qo->datasetindex]->datasetitem[$qo->dataset[$qo->datasetindex]->itemindex] = new stdClass();
526 $qo->dataset[$qo->datasetindex]->datasetitem[$qo->dataset[$qo->datasetindex]->itemindex]->itemnumber = $datasetitem['#']['number'][0]['#']; //[0]['#']['number'][0]['#'] ; // [0]['numberitems'] ;//['#']['number'][0]['#'];// $datasetitems['#']['number'][0]['#'];
527 $qo->dataset[$qo->datasetindex]->datasetitem[$qo->dataset[$qo->datasetindex]->itemindex]->value = $datasetitem['#']['value'][0]['#'] ;//$datasetitem['#']['value'][0]['#'];
88bc20c3 528 }
725ba2a0 529 }
88bc20c3 530
5fd8f999 531 // echo "<pre>loaded qo";print_r($qo);echo "</pre>";
725ba2a0 532
533 return $qo;
534 }
535
ee259d0c 536 /**
537 * this is not a real question type. It's a dummy type used
538 * to specify the import category
539 * format is:
540 * <question type="category">
541 * <category>tom/dick/harry</category>
542 * </question>
543 */
544 function import_category( $question ) {
0c6b4d2e 545 $qo = new stdClass;
ee259d0c 546 $qo->qtype = 'category';
86b68520 547 $qo->category = $this->import_text($question['#']['category'][0]['#']['text']);
ee259d0c 548 return $qo;
549 }
550
c81415c7 551 /**
552 * parse the array of lines into an array of questions
553 * this *could* burn memory - but it won't happen that much
554 * so fingers crossed!
6e557c08 555 * @param array lines array of lines from the input file
556 * @return array (of objects) question objects
c81415c7 557 */
558 function readquestions($lines) {
84769fd8 559 // we just need it as one big string
560 $text = implode($lines, " ");
561 unset( $lines );
562
563 // this converts xml to big nasty data structure
564 // the 0 means keep white space as it is (important for markdown format)
565 // print_r it if you want to see what it looks like!
88bc20c3 566 $xml = xmlize( $text, 0 );
84769fd8 567
568 // set up array to hold all our questions
569 $questions = array();
570
571 // iterate through questions
572 foreach ($xml['quiz']['#']['question'] as $question) {
573 $question_type = $question['@']['type'];
574 $questiontype = get_string( 'questiontype','quiz',$question_type );
84769fd8 575
576 if ($question_type=='multichoice') {
577 $qo = $this->import_multichoice( $question );
88bc20c3 578 }
84769fd8 579 elseif ($question_type=='truefalse') {
580 $qo = $this->import_truefalse( $question );
581 }
582 elseif ($question_type=='shortanswer') {
583 $qo = $this->import_shortanswer( $question );
584 }
585 elseif ($question_type=='numerical') {
586 $qo = $this->import_numerical( $question );
587 }
7b8bc256 588 elseif ($question_type=='description') {
589 $qo = $this->import_description( $question );
590 }
51bcdf28 591 elseif ($question_type=='matching') {
592 $qo = $this->import_matching( $question );
593 }
7b8bc256 594 elseif ($question_type=='cloze') {
c81415c7 595 $qo = $this->import_multianswer( $question );
596 }
597 elseif ($question_type=='essay') {
598 $qo = $this->import_essay( $question );
7b8bc256 599 }
725ba2a0 600 elseif ($question_type=='calculated') {
601 $qo = $this->import_calculated( $question );
602 }
9c1c6c7f 603 elseif ($question_type=='calculatedsimple') {
604 $qo = $this->import_calculated( $question );
605 $qo->qtype = CALCULATEDSIMPLE ;
606 }
04d8268d
PP
607 elseif ($question_type=='calculatedmulti') {
608 $qo = $this->import_calculated( $question );
609 $qo->qtype = CALCULATEDMULTI ;
610 }
ee259d0c 611 elseif ($question_type=='category') {
612 $qo = $this->import_category( $question );
613 }
84769fd8 614 else {
a41e3287 615 // try for plugin support
88bc20c3 616 // no default question, as the plugin can call
a41e3287 617 // import_headers() itself if it wants to
88bc20c3 618 if (!$qo = $this->try_importing_using_qtypes( $question, null, null, $question_type)) {
a41e3287 619 $notsupported = get_string( 'xmltypeunsupported','quiz',$question_type );
620 $this->error( $notsupported );
621 $qo = null;
622 }
84769fd8 623 }
624
625 // stick the result in the $questions array
626 if ($qo) {
627 $questions[] = $qo;
628 }
629 }
84769fd8 630 return $questions;
631 }
632
633 // EXPORT FUNCTIONS START HERE
634
84769fd8 635 function export_file_extension() {
636 // override default type so extension is .xml
88bc20c3 637
84769fd8 638 return ".xml";
639 }
640
c81415c7 641
642 /**
643 * Turn the internal question code into a human readable form
644 * (The code used to be numeric, but this remains as some of
645 * the names don't match the new internal format)
6e557c08 646 * @param mixed type_id Internal code
647 * @return string question type string
c81415c7 648 */
84769fd8 649 function get_qtype( $type_id ) {
84769fd8 650 switch( $type_id ) {
651 case TRUEFALSE:
652 $name = 'truefalse';
653 break;
654 case MULTICHOICE:
655 $name = 'multichoice';
656 break;
657 case SHORTANSWER:
658 $name = 'shortanswer';
659 break;
660 case NUMERICAL:
661 $name = 'numerical';
662 break;
663 case MATCH:
664 $name = 'matching';
665 break;
666 case DESCRIPTION:
667 $name = 'description';
668 break;
669 case MULTIANSWER:
670 $name = 'cloze';
671 break;
c81415c7 672 case ESSAY:
673 $name = 'essay';
674 break;
725ba2a0 675 case CALCULATED:
676 $name = 'calculated';
677 break;
9c1c6c7f 678 case CALCULATEDSIMPLE:
679 $name = 'calculatedsimple';
680 break;
04d8268d
PP
681 case CALCULATEDMULTI:
682 $name = 'calculatedmulti';
683 break;
84769fd8 684 default:
a41e3287 685 $name = false;
84769fd8 686 }
687 return $name;
688 }
689
6e557c08 690 /**
c81415c7 691 * Convert internal Moodle text format code into
692 * human readable form
6e557c08 693 * @param int id internal code
694 * @return string format text
c81415c7 695 */
84769fd8 696 function get_format( $id ) {
84769fd8 697 switch( $id ) {
698 case 0:
699 $name = "moodle_auto_format";
700 break;
701 case 1:
702 $name = "html";
703 break;
704 case 2:
705 $name = "plain_text";
706 break;
707 case 3:
708 $name = "wiki_like";
709 break;
710 case 4:
711 $name = "markdown";
712 break;
713 default:
714 $name = "unknown";
715 }
716 return $name;
717 }
718
6e557c08 719 /**
88bc20c3 720 * Convert internal single question code into
c81415c7 721 * human readable form
6e557c08 722 * @param int id single question code
723 * @return string single question string
c81415c7 724 */
84769fd8 725 function get_single( $id ) {
84769fd8 726 switch( $id ) {
727 case 0:
728 $name = "false";
729 break;
730 case 1:
731 $name = "true";
732 break;
733 default:
734 $name = "unknown";
735 }
736 return $name;
737 }
738
6e557c08 739 /**
88bc20c3 740 * generates <text></text> tags, processing raw text therein
6e557c08 741 * @param int ilev the current indent level
742 * @param boolean short stick it on one line
743 * @return string formatted text
c81415c7 744 */
84769fd8 745 function writetext( $raw, $ilev=0, $short=true) {
84769fd8 746 $indent = str_repeat( " ",$ilev );
747
3695cfa9 748 // if required add CDATA tags
749 if (!empty($raw) and (htmlspecialchars($raw)!=$raw)) {
750 $raw = "<![CDATA[$raw]]>";
751 }
84769fd8 752
753 if ($short) {
754 $xml = "$indent<text>$raw</text>\n";
755 }
756 else {
757 $xml = "$indent<text>\n$raw\n$indent</text>\n";
758 }
759
760 return $xml;
761 }
88bc20c3 762
81a7a02b 763 function xmltidy( $content ) {
764 // can only do this if tidy is installed
765 if (extension_loaded('tidy')) {
766 $config = array( 'input-xml'=>true, 'output-xml'=>true, 'indent'=>true, 'wrap'=>0 );
767 $tidy = new tidy;
768 $tidy->parseString($content, $config, 'utf8');
88bc20c3 769 $tidy->cleanRepair();
81a7a02b 770 return $tidy->value;
771 }
772 else {
773 return $content;
774 }
775 }
776
84769fd8 777
778 function presave_process( $content ) {
779 // override method to allow us to add xml headers and footers
780
81a7a02b 781 // add the xml headers and footers
46013523 782 $content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" .
84769fd8 783 "<quiz>\n" .
784 $content . "\n" .
785 "</quiz>";
786
81a7a02b 787 // make the xml look nice
788 $content = $this->xmltidy( $content );
789
84769fd8 790 return $content;
791 }
792
6e557c08 793 /**
c81415c7 794 * Include an image encoded in base 64
6e557c08 795 * @param string imagepath The location of the image file
88bc20c3 796 * @return string xml code segment
c81415c7 797 */
d08e16b2 798 function writeimage( $imagepath ) {
d08e16b2 799 global $CFG;
88bc20c3 800
d08e16b2 801 if (empty($imagepath)) {
802 return '';
803 }
804
805 $courseid = $this->course->id;
806 if (!$binary = file_get_contents( "{$CFG->dataroot}/$courseid/$imagepath" )) {
807 return '';
808 }
809
294ce987 810 $content = " <image_base64>\n".base64_encode( $binary )."\n".
d08e16b2 811 "\n </image_base64>\n";
812 return $content;
813 }
814
6e557c08 815 /**
c81415c7 816 * Turns question into an xml segment
6e557c08 817 * @param array question question array
818 * @return string xml segment
c81415c7 819 */
84769fd8 820 function writequestion( $question ) {
fef8f84e 821 global $CFG,$QTYPES, $OUTPUT;
84769fd8 822 // initial string;
823 $expout = "";
824
825 // add comment
826 $expout .= "\n\n<!-- question: $question->id -->\n";
827
a41e3287 828 // check question type
829 if (!$question_type = $this->get_qtype( $question->qtype )) {
830 // must be a plugin then, so just accept the name supplied
831 $question_type = $question->qtype;
46013523 832 }
833
84769fd8 834 // add opening tag
f1abd39f 835 // generates specific header for Cloze and category type question
836 if ($question->qtype == 'category') {
6f8481ec 837 $categorypath = $this->writetext( $question->category );
f1abd39f 838 $expout .= " <question type=\"category\">\n";
839 $expout .= " <category>\n";
6f8481ec 840 $expout .= " $categorypath\n";
f1abd39f 841 $expout .= " </category>\n";
842 $expout .= " </question>\n";
843 return $expout;
88bc20c3 844 }
f1abd39f 845 elseif ($question->qtype != MULTIANSWER) {
7b8bc256 846 // for all question types except Close
7b8bc256 847 $name_text = $this->writetext( $question->name );
848 $qtformat = $this->get_format($question->questiontextformat);
849 $question_text = $this->writetext( $question->questiontext );
a4514d91 850 $generalfeedback = $this->writetext( $question->generalfeedback );
88bc20c3 851 $expout .= " <question type=\"$question_type\">\n";
7b8bc256 852 $expout .= " <name>$name_text</name>\n";
853 $expout .= " <questiontext format=\"$qtformat\">\n";
854 $expout .= $question_text;
88bc20c3 855 $expout .= " </questiontext>\n";
7b8bc256 856 $expout .= " <image>{$question->image}</image>\n";
857 $expout .= $this->writeimage($question->image);
a4514d91 858 $expout .= " <generalfeedback>\n";
859 $expout .= $generalfeedback;
860 $expout .= " </generalfeedback>\n";
c81415c7 861 $expout .= " <defaultgrade>{$question->defaultgrade}</defaultgrade>\n";
7b8bc256 862 $expout .= " <penalty>{$question->penalty}</penalty>\n";
863 $expout .= " <hidden>{$question->hidden}</hidden>\n";
864 }
865 else {
866 // for Cloze type only
7b8bc256 867 $name_text = $this->writetext( $question->name );
868 $question_text = $this->writetext( $question->questiontext );
4ab4dcb2 869 $generalfeedback = $this->writetext( $question->generalfeedback );
7b8bc256 870 $expout .= " <question type=\"$question_type\">\n";
871 $expout .= " <name>$name_text</name>\n";
872 $expout .= " <questiontext>\n";
873 $expout .= $question_text;
874 $expout .= " </questiontext>\n";
4ab4dcb2 875 $expout .= " <generalfeedback>\n";
876 $expout .= $generalfeedback;
877 $expout .= " </generalfeedback>\n";
7b8bc256 878 }
879
d08e16b2 880 if (!empty($question->options->shuffleanswers)) {
881 $expout .= " <shuffleanswers>{$question->options->shuffleanswers}</shuffleanswers>\n";
882 }
883 else {
884 $expout .= " <shuffleanswers>0</shuffleanswers>\n";
885 }
84769fd8 886
887 // output depends on question type
888 switch($question->qtype) {
f1abd39f 889 case 'category':
890 // not a qtype really - dummy used for category switching
88bc20c3 891 break;
84769fd8 892 case TRUEFALSE:
36e2232e 893 foreach ($question->options->answers as $answer) {
894 $fraction_pc = round( $answer->fraction * 100 );
3246ed33 895 if ($answer->id == $question->options->trueanswer) {
896 $answertext = 'true';
897 } else {
898 $answertext = 'false';
88bc20c3 899 }
36e2232e 900 $expout .= " <answer fraction=\"$fraction_pc\">\n";
3246ed33 901 $expout .= $this->writetext($answertext, 3) . "\n";
36e2232e 902 $expout .= " <feedback>\n";
903 $expout .= $this->writetext( $answer->feedback,4,false );
904 $expout .= " </feedback>\n";
905 $expout .= " </answer>\n";
906 }
84769fd8 907 break;
908 case MULTICHOICE:
909 $expout .= " <single>".$this->get_single($question->options->single)."</single>\n";
307f045f 910 $expout .= " <shuffleanswers>".$this->get_single($question->options->shuffleanswers)."</shuffleanswers>\n";
911 $expout .= " <correctfeedback>".$this->writetext($question->options->correctfeedback, 3)."</correctfeedback>\n";
912 $expout .= " <partiallycorrectfeedback>".$this->writetext($question->options->partiallycorrectfeedback, 3)."</partiallycorrectfeedback>\n";
913 $expout .= " <incorrectfeedback>".$this->writetext($question->options->incorrectfeedback, 3)."</incorrectfeedback>\n";
04d8268d 914 $expout .= " <answernumbering>".$this->writetext($question->options->answernumbering, 3)."</answernumbering>\n";
84769fd8 915 foreach($question->options->answers as $answer) {
916 $percent = $answer->fraction * 100;
917 $expout .= " <answer fraction=\"$percent\">\n";
918 $expout .= $this->writetext( $answer->answer,4,false );
919 $expout .= " <feedback>\n";
6e557c08 920 $expout .= $this->writetext( $answer->feedback,5,false );
84769fd8 921 $expout .= " </feedback>\n";
922 $expout .= " </answer>\n";
923 }
924 break;
925 case SHORTANSWER:
926 $expout .= " <usecase>{$question->options->usecase}</usecase>\n ";
927 foreach($question->options->answers as $answer) {
928 $percent = 100 * $answer->fraction;
929 $expout .= " <answer fraction=\"$percent\">\n";
930 $expout .= $this->writetext( $answer->answer,3,false );
931 $expout .= " <feedback>\n";
932 $expout .= $this->writetext( $answer->feedback,4,false );
933 $expout .= " </feedback>\n";
934 $expout .= " </answer>\n";
935 }
936 break;
937 case NUMERICAL:
938 foreach ($question->options->answers as $answer) {
939 $tolerance = $answer->tolerance;
55c54868 940 $percent = 100 * $answer->fraction;
941 $expout .= "<answer fraction=\"$percent\">\n";
942 // <text> tags are an added feature, old filed won't have them
943 $expout .= " <text>{$answer->answer}</text>\n";
84769fd8 944 $expout .= " <tolerance>$tolerance</tolerance>\n";
945 $expout .= " <feedback>".$this->writetext( $answer->feedback )."</feedback>\n";
55c54868 946 // fraction tag is deprecated
947 // $expout .= " <fraction>{$answer->fraction}</fraction>\n";
84769fd8 948 $expout .= "</answer>\n";
949 }
950
951 $units = $question->options->units;
952 if (count($units)) {
953 $expout .= "<units>\n";
954 foreach ($units as $unit) {
955 $expout .= " <unit>\n";
956 $expout .= " <multiplier>{$unit->multiplier}</multiplier>\n";
957 $expout .= " <unit_name>{$unit->unit}</unit_name>\n";
958 $expout .= " </unit>\n";
959 }
960 $expout .= "</units>\n";
961 }
962 break;
963 case MATCH:
964 foreach($question->options->subquestions as $subquestion) {
965 $expout .= "<subquestion>\n";
966 $expout .= $this->writetext( $subquestion->questiontext );
967 $expout .= "<answer>".$this->writetext( $subquestion->answertext )."</answer>\n";
968 $expout .= "</subquestion>\n";
969 }
970 break;
971 case DESCRIPTION:
c81415c7 972 // nothing more to do for this type
84769fd8 973 break;
974 case MULTIANSWER:
7b8bc256 975 $a_count=1;
976 foreach($question->options->questions as $question) {
9cc41dfd 977 $thispattern = preg_quote("{#".$a_count."}"); //TODO: is this really necessary?
7b8bc256 978 $thisreplace = $question->questiontext;
6dbcacee 979 $expout=preg_replace("~$thispattern~", $thisreplace, $expout );
7b8bc256 980 $a_count++;
981 }
982 break;
c81415c7 983 case ESSAY:
33ed59a2 984 if (!empty($question->options->answers)) {
985 foreach ($question->options->answers as $answer) {
986 $percent = 100 * $answer->fraction;
987 $expout .= "<answer fraction=\"$percent\">\n";
988 $expout .= " <feedback>".$this->writetext( $answer->feedback )."</feedback>\n";
989 // fraction tag is deprecated
990 // $expout .= " <fraction>{$answer->fraction}</fraction>\n";
991 $expout .= "</answer>\n";
992 }
c81415c7 993 }
725ba2a0 994 break;
995 case CALCULATED:
9c1c6c7f 996 case CALCULATEDSIMPLE:
04d8268d
PP
997 case CALCULATEDMULTI:
998 $expout .= " <synchronize>{$question->options->synchronize}</synchronize>\n";
999 $expout .= " <single>{$question->options->single}</single>\n";
1000 $expout .= " <answernumbering>{$question->options->answernumbering}</answernumbering>\n";
1001 $expout .= " <shuffleanswers>".$this->writetext($question->options->shuffleanswers, 3)."</shuffleanswers>\n";
1002 $expout .= " <correctfeedback>".$this->writetext($question->options->correctfeedback, 3)."</correctfeedback>\n";
1003 $expout .= " <partiallycorrectfeedback>".$this->writetext($question->options->partiallycorrectfeedback, 3)."</partiallycorrectfeedback>\n";
1004 $expout .= " <incorrectfeedback>".$this->writetext($question->options->incorrectfeedback, 3)."</incorrectfeedback>\n";
725ba2a0 1005 foreach ($question->options->answers as $answer) {
1006 $tolerance = $answer->tolerance;
1007 $tolerancetype = $answer->tolerancetype;
1008 $correctanswerlength= $answer->correctanswerlength ;
1009 $correctanswerformat= $answer->correctanswerformat;
1010 $percent = 100 * $answer->fraction;
1011 $expout .= "<answer fraction=\"$percent\">\n";
1012 // "<text/>" tags are an added feature, old files won't have them
1013 $expout .= " <text>{$answer->answer}</text>\n";
1014 $expout .= " <tolerance>$tolerance</tolerance>\n";
1015 $expout .= " <tolerancetype>$tolerancetype</tolerancetype>\n";
1016 $expout .= " <correctanswerformat>$correctanswerformat</correctanswerformat>\n";
a800639b 1017 $expout .= " <correctanswerlength>$correctanswerlength</correctanswerlength>\n";
725ba2a0 1018 $expout .= " <feedback>".$this->writetext( $answer->feedback )."</feedback>\n";
1019 $expout .= "</answer>\n";
1020 }
04d8268d
PP
1021 $expout .= " <unitgradingtype>{$question->options->unitgradingtype}</unitgradingtype>\n";
1022 $expout .= " <unitpenalty>{$question->options->unitpenalty}</unitpenalty>\n";
1023 $expout .= " <showunits>{$question->options->showunits}</showunits>\n";
1024 $expout .= " <unitsleft>{$question->options->unitsleft}</unitsleft>\n";
1025 $expout .= " <instructions>".$this->writetext($question->options->instructions, 3)."</instructions>\n";
725ba2a0 1026 $units = $question->options->units;
1027 if (count($units)) {
1028 $expout .= "<units>\n";
1029 foreach ($units as $unit) {
1030 $expout .= " <unit>\n";
1031 $expout .= " <multiplier>{$unit->multiplier}</multiplier>\n";
1032 $expout .= " <unit_name>{$unit->unit}</unit_name>\n";
1033 $expout .= " </unit>\n";
1034 }
1035 $expout .= "</units>\n";
88bc20c3 1036 }
9c1c6c7f 1037 //The tag $question->export_process has been set so we get all the data items in the database
1038 // from the function $QTYPES['calculated']->get_question_options(&$question);
1039 // calculatedsimple defaults to calculated
c9e4ba36 1040 if( isset($question->options->datasets)&&count($question->options->datasets)){// there should be
725ba2a0 1041 $expout .= "<dataset_definitions>\n";
c9e4ba36 1042 foreach ($question->options->datasets as $def) {
725ba2a0 1043 $expout .= "<dataset_definition>\n";
1044 $expout .= " <status>".$this->writetext($def->status)."</status>\n";
1045 $expout .= " <name>".$this->writetext($def->name)."</name>\n";
9c1c6c7f 1046 if ( $question->qtype == CALCULATED){
1047 $expout .= " <type>calculated</type>\n";
1048 }else {
1049 $expout .= " <type>calculatedsimple</type>\n";
1050 }
725ba2a0 1051 $expout .= " <distribution>".$this->writetext($def->distribution)."</distribution>\n";
1052 $expout .= " <minimum>".$this->writetext($def->minimum)."</minimum>\n";
1053 $expout .= " <maximum>".$this->writetext($def->maximum)."</maximum>\n";
88bc20c3 1054 $expout .= " <decimals>".$this->writetext($def->decimals)."</decimals>\n";
725ba2a0 1055 $expout .= " <itemcount>$def->itemcount</itemcount>\n";
1056 if ($def->itemcount > 0 ) {
1057 $expout .= " <dataset_items>\n";
1058 foreach ($def->items as $item ){
1059 $expout .= " <dataset_item>\n";
1060 $expout .= " <number>".$item->itemnumber."</number>\n";
1061 $expout .= " <value>".$item->value."</value>\n";
1062 $expout .= " </dataset_item>\n";
88bc20c3 1063 }
725ba2a0 1064 $expout .= " </dataset_items>\n";
1065 $expout .= " <number_of_items>".$def-> number_of_items."</number_of_items>\n";
1066 }
1067 $expout .= "</dataset_definition>\n";
88bc20c3 1068 }
1069 $expout .= "</dataset_definitions>\n";
1070 }
c81415c7 1071 break;
84769fd8 1072 default:
a41e3287 1073 // try support by optional plugin
88bc20c3 1074 if (!$data = $this->try_exporting_using_qtypes( $question->qtype, $question )) {
fef8f84e 1075 echo $OUTPUT->notification( get_string( 'unsupportedexport','qformat_xml',$QTYPES[$question->qtype]->local_name() ) );
a41e3287 1076 }
392238d6 1077 $expout .= $data;
84769fd8 1078 }
1079
1080 // close the question tag
1081 $expout .= "</question>\n";
1082
84769fd8 1083 return $expout;
1084 }
1085}
1086
1087?>