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