forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.php
1569 lines (1402 loc) · 64.3 KB
/
format.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Code for exporting questions as Moodle XML.
*
* @package qformat_xml
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/xmlize.php');
if (!class_exists('qformat_default')) {
// This is ugly, but this class is also (ab)used by mod/lesson, which defines
// a different base class in mod/lesson/format.php. Thefore, we can only
// include the proper base class conditionally like this. (We have to include
// the base class like this, otherwise it breaks third-party question types.)
// This may be reviewd, and a better fix found one day.
require_once($CFG->dirroot . '/question/format.php');
}
/**
* Importer for Moodle XML question format.
*
* See http://docs.moodle.org/en/Moodle_XML_format for a description of the format.
*
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qformat_xml extends qformat_default {
public function provide_import() {
return true;
}
public function provide_export() {
return true;
}
public function mime_type() {
return 'application/xml';
}
// IMPORT FUNCTIONS START HERE.
/**
* Translate human readable format name
* into internal Moodle code number
* @param string name format name from xml file
* @return int Moodle format code
*/
public function trans_format($name) {
$name = trim($name);
if ($name == 'moodle_auto_format') {
return FORMAT_MOODLE;
} else if ($name == 'html') {
return FORMAT_HTML;
} else if ($name == 'plain_text') {
return FORMAT_PLAIN;
} else if ($name == 'wiki_like') {
return FORMAT_WIKI;
} else if ($name == 'markdown') {
return FORMAT_MARKDOWN;
} else {
debugging("Unrecognised text format '{$name}' in the import file. Assuming 'html'.");
return FORMAT_HTML;
}
}
/**
* Translate human readable single answer option
* to internal code number
* @param string name true/false
* @return int internal code number
*/
public function trans_single($name) {
$name = trim($name);
if ($name == "false" || !$name) {
return 0;
} else {
return 1;
}
}
/**
* process text string from xml file
* @param array $text bit of xml tree after ['text']
* @return string processed text.
*/
public function import_text($text) {
// Quick sanity check.
if (empty($text)) {
return '';
}
$data = $text[0]['#'];
return trim($data);
}
/**
* return the value of a node, given a path to the node
* if it doesn't exist return the default value
* @param array xml data to read
* @param array path path to node expressed as array
* @param mixed default
* @param bool istext process as text
* @param string error if set value must exist, return false and issue message if not
* @return mixed value
*/
public function getpath($xml, $path, $default, $istext=false, $error='') {
foreach ($path as $index) {
if (!isset($xml[$index])) {
if (!empty($error)) {
$this->error($error);
return false;
} else {
return $default;
}
}
$xml = $xml[$index];
}
if ($istext) {
if (!is_string($xml)) {
$this->error(get_string('invalidxml', 'qformat_xml'));
}
$xml = trim($xml);
}
return $xml;
}
public function import_text_with_files($data, $path, $defaultvalue = '', $defaultformat = 'html') {
$field = array();
$field['text'] = $this->getpath($data,
array_merge($path, array('#', 'text', 0, '#')), $defaultvalue, true);
$field['format'] = $this->trans_format($this->getpath($data,
array_merge($path, array('@', 'format')), $defaultformat));
$itemid = $this->import_files_as_draft($this->getpath($data,
array_merge($path, array('#', 'file')), array(), false));
if (!empty($itemid)) {
$field['itemid'] = $itemid;
}
return $field;
}
public function import_files_as_draft($xml) {
global $USER;
if (empty($xml)) {
return null;
}
$fs = get_file_storage();
$itemid = file_get_unused_draft_itemid();
$filepaths = array();
foreach ($xml as $file) {
$filename = $this->getpath($file, array('@', 'name'), '', true);
$filepath = $this->getpath($file, array('@', 'path'), '/', true);
$fullpath = $filepath . $filename;
if (in_array($fullpath, $filepaths)) {
debugging('Duplicate file in XML: ' . $fullpath, DEBUG_DEVELOPER);
continue;
}
$filerecord = array(
'contextid' => context_user::instance($USER->id)->id,
'component' => 'user',
'filearea' => 'draft',
'itemid' => $itemid,
'filepath' => $filepath,
'filename' => $filename,
);
$fs->create_file_from_string($filerecord, base64_decode($file['#']));
$filepaths[] = $fullpath;
}
return $itemid;
}
/**
* import parts of question common to all types
* @param $question array question question array from xml tree
* @return object question object
*/
public function import_headers($question) {
global $CFG, $USER;
// This routine initialises the question object.
$qo = $this->defaultquestion();
// Question name.
$qo->name = $this->clean_question_name($this->getpath($question,
array('#', 'name', 0, '#', 'text', 0, '#'), '', true,
get_string('xmlimportnoname', 'qformat_xml')));
$questiontext = $this->import_text_with_files($question,
array('#', 'questiontext', 0));
$qo->questiontext = $questiontext['text'];
$qo->questiontextformat = $questiontext['format'];
if (!empty($questiontext['itemid'])) {
$qo->questiontextitemid = $questiontext['itemid'];
}
// Backwards compatibility, deal with the old image tag.
$filedata = $this->getpath($question, array('#', 'image_base64', '0', '#'), null, false);
$filename = $this->getpath($question, array('#', 'image', '0', '#'), null, false);
if ($filedata && $filename) {
$fs = get_file_storage();
if (empty($qo->questiontextitemid)) {
$qo->questiontextitemid = file_get_unused_draft_itemid();
}
$filename = clean_param(str_replace('/', '_', $filename), PARAM_FILE);
$filerecord = array(
'contextid' => context_user::instance($USER->id)->id,
'component' => 'user',
'filearea' => 'draft',
'itemid' => $qo->questiontextitemid,
'filepath' => '/',
'filename' => $filename,
);
$fs->create_file_from_string($filerecord, base64_decode($filedata));
$qo->questiontext .= ' <img src="@@PLUGINFILE@@/' . $filename . '" />';
}
// Restore files in generalfeedback.
$generalfeedback = $this->import_text_with_files($question,
array('#', 'generalfeedback', 0), $qo->generalfeedback, $this->get_format($qo->questiontextformat));
$qo->generalfeedback = $generalfeedback['text'];
$qo->generalfeedbackformat = $generalfeedback['format'];
if (!empty($generalfeedback['itemid'])) {
$qo->generalfeedbackitemid = $generalfeedback['itemid'];
}
$qo->defaultmark = $this->getpath($question,
array('#', 'defaultgrade', 0, '#'), $qo->defaultmark);
$qo->penalty = $this->getpath($question,
array('#', 'penalty', 0, '#'), $qo->penalty);
// Fix problematic rounding from old files.
if (abs($qo->penalty - 0.3333333) < 0.005) {
$qo->penalty = 0.3333333;
}
// Read the question tags.
if (!empty($CFG->usetags) && array_key_exists('tags', $question['#'])
&& !empty($question['#']['tags'][0]['#']['tag'])) {
require_once($CFG->dirroot.'/tag/lib.php');
$qo->tags = array();
foreach ($question['#']['tags'][0]['#']['tag'] as $tagdata) {
$qo->tags[] = $this->getpath($tagdata, array('#', 'text', 0, '#'), '', true);
}
}
return $qo;
}
/**
* Import the common parts of a single answer
* @param array answer xml tree for single answer
* @param bool $withanswerfiles if true, the answers are HTML (or $defaultformat)
* and so may contain files, otherwise the answers are plain text.
* @param array Default text format for the feedback, and the answers if $withanswerfiles
* is true.
* @return object answer object
*/
public function import_answer($answer, $withanswerfiles = false, $defaultformat = 'html') {
$ans = new stdClass();
if ($withanswerfiles) {
$ans->answer = $this->import_text_with_files($answer, array(), '', $defaultformat);
} else {
$ans->answer = array();
$ans->answer['text'] = $this->getpath($answer, array('#', 'text', 0, '#'), '', true);
$ans->answer['format'] = FORMAT_PLAIN;
}
$ans->feedback = $this->import_text_with_files($answer, array('#', 'feedback', 0), '', $defaultformat);
$ans->fraction = $this->getpath($answer, array('@', 'fraction'), 0) / 100;
return $ans;
}
/**
* Import the common overall feedback fields.
* @param object $question the part of the XML relating to this question.
* @param object $qo the question data to add the fields to.
* @param bool $withshownumpartscorrect include the shownumcorrect field.
*/
public function import_combined_feedback($qo, $questionxml, $withshownumpartscorrect = false) {
$fields = array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback');
foreach ($fields as $field) {
$qo->$field = $this->import_text_with_files($questionxml,
array('#', $field, 0), '', $this->get_format($qo->questiontextformat));
}
if ($withshownumpartscorrect) {
$qo->shownumcorrect = array_key_exists('shownumcorrect', $questionxml['#']);
// Backwards compatibility.
if (array_key_exists('correctresponsesfeedback', $questionxml['#'])) {
$qo->shownumcorrect = $this->trans_single($this->getpath($questionxml,
array('#', 'correctresponsesfeedback', 0, '#'), 1));
}
}
}
/**
* Import a question hint
* @param array $hintxml hint xml fragment.
* @param string $defaultformat the text format to assume for hints that do not specify.
* @return object hint for storing in the database.
*/
public function import_hint($hintxml, $defaultformat) {
$hint = new stdClass();
if (array_key_exists('hintcontent', $hintxml['#'])) {
// Backwards compatibility.
$hint->hint = $this->import_text_with_files($hintxml,
array('#', 'hintcontent', 0), '', $defaultformat);
$hint->shownumcorrect = $this->getpath($hintxml,
array('#', 'statenumberofcorrectresponses', 0, '#'), 0);
$hint->clearwrong = $this->getpath($hintxml,
array('#', 'clearincorrectresponses', 0, '#'), 0);
$hint->options = $this->getpath($hintxml,
array('#', 'showfeedbacktoresponses', 0, '#'), 0);
return $hint;
}
$hint->hint = $this->import_text_with_files($hintxml, array(), '', $defaultformat);
$hint->shownumcorrect = array_key_exists('shownumcorrect', $hintxml['#']);
$hint->clearwrong = array_key_exists('clearwrong', $hintxml['#']);
$hint->options = $this->getpath($hintxml, array('#', 'options', 0, '#'), '', true);
return $hint;
}
/**
* Import all the question hints
*
* @param object $qo the question data that is being constructed.
* @param array $questionxml The xml representing the question.
* @param bool $withparts whether the extra fields relating to parts should be imported.
* @param bool $withoptions whether the extra options field should be imported.
* @param string $defaultformat the text format to assume for hints that do not specify.
* @return array of objects representing the hints in the file.
*/
public function import_hints($qo, $questionxml, $withparts = false,
$withoptions = false, $defaultformat = 'html') {
if (!isset($questionxml['#']['hint'])) {
return;
}
foreach ($questionxml['#']['hint'] as $hintxml) {
$hint = $this->import_hint($hintxml, $defaultformat);
$qo->hint[] = $hint->hint;
if ($withparts) {
$qo->hintshownumcorrect[] = $hint->shownumcorrect;
$qo->hintclearwrong[] = $hint->clearwrong;
}
if ($withoptions) {
$qo->hintoptions[] = $hint->options;
}
}
}
/**
* Import files from a node in the XML.
* @param array $xml an array of <file> nodes from the the parsed XML.
* @return array of things representing files - in the form that save_question expects.
*/
public function import_files($xml) {
$files = array();
foreach ($xml as $file) {
$data = new stdClass();
$data->content = $file['#'];
$data->encoding = $file['@']['encoding'];
$data->name = $file['@']['name'];
$files[] = $data;
}
return $files;
}
/**
* import multiple choice question
* @param array question question array from xml tree
* @return object question object
*/
public function import_multichoice($question) {
// Get common parts.
$qo = $this->import_headers($question);
// Header parts particular to multichoice.
$qo->qtype = 'multichoice';
$single = $this->getpath($question, array('#', 'single', 0, '#'), 'true');
$qo->single = $this->trans_single($single);
$shuffleanswers = $this->getpath($question,
array('#', 'shuffleanswers', 0, '#'), 'false');
$qo->answernumbering = $this->getpath($question,
array('#', 'answernumbering', 0, '#'), 'abc');
$qo->shuffleanswers = $this->trans_single($shuffleanswers);
// There was a time on the 1.8 branch when it could output an empty
// answernumbering tag, so fix up any found.
if (empty($qo->answernumbering)) {
$qo->answernumbering = 'abc';
}
// Run through the answers.
$answers = $question['#']['answer'];
$acount = 0;
foreach ($answers as $answer) {
$ans = $this->import_answer($answer, true, $this->get_format($qo->questiontextformat));
$qo->answer[$acount] = $ans->answer;
$qo->fraction[$acount] = $ans->fraction;
$qo->feedback[$acount] = $ans->feedback;
++$acount;
}
$this->import_combined_feedback($qo, $question, true);
$this->import_hints($qo, $question, true, false, $this->get_format($qo->questiontextformat));
return $qo;
}
/**
* Import cloze type question
* @param array question question array from xml tree
* @return object question object
*/
public function import_multianswer($question) {
global $USER;
question_bank::get_qtype('multianswer');
$questiontext = $this->import_text_with_files($question,
array('#', 'questiontext', 0));
$qo = qtype_multianswer_extract_question($questiontext);
// Header parts particular to multianswer.
$qo->qtype = 'multianswer';
$qo->course = $this->course;
$qo->name = $this->clean_question_name($this->import_text($question['#']['name'][0]['#']['text']));
$qo->questiontextformat = $questiontext['format'];
$qo->questiontext = $qo->questiontext['text'];
if (!empty($questiontext['itemid'])) {
$qo->questiontextitemid = $questiontext['itemid'];
}
// Backwards compatibility, deal with the old image tag.
$filedata = $this->getpath($question, array('#', 'image_base64', '0', '#'), null, false);
$filename = $this->getpath($question, array('#', 'image', '0', '#'), null, false);
if ($filedata && $filename) {
$fs = get_file_storage();
if (empty($qo->questiontextitemid)) {
$qo->questiontextitemid = file_get_unused_draft_itemid();
}
$filename = clean_param(str_replace('/', '_', $filename), PARAM_FILE);
$filerecord = array(
'contextid' => context_user::instance($USER->id)->id,
'component' => 'user',
'filearea' => 'draft',
'itemid' => $qo->questiontextitemid,
'filepath' => '/',
'filename' => $filename,
);
$fs->create_file_from_string($filerecord, base64_decode($filedata));
$qo->questiontext .= ' <img src="@@PLUGINFILE@@/' . $filename . '" />';
}
// Restore files in generalfeedback.
$generalfeedback = $this->import_text_with_files($question,
array('#', 'generalfeedback', 0), $qo->generalfeedback, $this->get_format($qo->questiontextformat));
$qo->generalfeedback = $generalfeedback['text'];
$qo->generalfeedbackformat = $generalfeedback['format'];
if (!empty($generalfeedback['itemid'])) {
$qo->generalfeedbackitemid = $generalfeedback['itemid'];
}
$qo->penalty = $this->getpath($question,
array('#', 'penalty', 0, '#'), $this->defaultquestion()->penalty);
// Fix problematic rounding from old files.
if (abs($qo->penalty - 0.3333333) < 0.005) {
$qo->penalty = 0.3333333;
}
$this->import_hints($qo, $question, true, false, $this->get_format($qo->questiontextformat));
return $qo;
}
/**
* Import true/false type question
* @param array question question array from xml tree
* @return object question object
*/
public function import_truefalse($question) {
// Get common parts.
global $OUTPUT;
$qo = $this->import_headers($question);
// Header parts particular to true/false.
$qo->qtype = 'truefalse';
// In the past, it used to be assumed that the two answers were in the file
// true first, then false. Howevever that was not always true. Now, we
// try to match on the answer text, but in old exports, this will be a localised
// string, so if we don't find true or false, we fall back to the old system.
$first = true;
$warning = false;
foreach ($question['#']['answer'] as $answer) {
$answertext = $this->getpath($answer,
array('#', 'text', 0, '#'), '', true);
$feedback = $this->import_text_with_files($answer,
array('#', 'feedback', 0), '', $this->get_format($qo->questiontextformat));
if ($answertext != 'true' && $answertext != 'false') {
// Old style file, assume order is true/false.
$warning = true;
if ($first) {
$answertext = 'true';
} else {
$answertext = 'false';
}
}
if ($answertext == 'true') {
$qo->answer = ($answer['@']['fraction'] == 100);
$qo->correctanswer = $qo->answer;
$qo->feedbacktrue = $feedback;
} else {
$qo->answer = ($answer['@']['fraction'] != 100);
$qo->correctanswer = $qo->answer;
$qo->feedbackfalse = $feedback;
}
$first = false;
}
if ($warning) {
$a = new stdClass();
$a->questiontext = $qo->questiontext;
$a->answer = get_string($qo->correctanswer ? 'true' : 'false', 'qtype_truefalse');
echo $OUTPUT->notification(get_string('truefalseimporterror', 'qformat_xml', $a));
}
$this->import_hints($qo, $question, false, false, $this->get_format($qo->questiontextformat));
return $qo;
}
/**
* Import short answer type question
* @param array question question array from xml tree
* @return object question object
*/
public function import_shortanswer($question) {
// Get common parts.
$qo = $this->import_headers($question);
// Header parts particular to shortanswer.
$qo->qtype = 'shortanswer';
// Get usecase.
$qo->usecase = $this->getpath($question, array('#', 'usecase', 0, '#'), $qo->usecase);
// Run through the answers.
$answers = $question['#']['answer'];
$acount = 0;
foreach ($answers as $answer) {
$ans = $this->import_answer($answer, false, $this->get_format($qo->questiontextformat));
$qo->answer[$acount] = $ans->answer['text'];
$qo->fraction[$acount] = $ans->fraction;
$qo->feedback[$acount] = $ans->feedback;
++$acount;
}
$this->import_hints($qo, $question, false, false, $this->get_format($qo->questiontextformat));
return $qo;
}
/**
* Import description type question
* @param array question question array from xml tree
* @return object question object
*/
public function import_description($question) {
// Get common parts.
$qo = $this->import_headers($question);
// Header parts particular to shortanswer.
$qo->qtype = 'description';
$qo->defaultmark = 0;
$qo->length = 0;
return $qo;
}
/**
* Import numerical type question
* @param array question question array from xml tree
* @return object question object
*/
public function import_numerical($question) {
// Get common parts.
$qo = $this->import_headers($question);
// Header parts particular to numerical.
$qo->qtype = 'numerical';
// Get answers array.
$answers = $question['#']['answer'];
$qo->answer = array();
$qo->feedback = array();
$qo->fraction = array();
$qo->tolerance = array();
foreach ($answers as $answer) {
// Answer outside of <text> is deprecated.
$obj = $this->import_answer($answer, false, $this->get_format($qo->questiontextformat));
$qo->answer[] = $obj->answer['text'];
if (empty($qo->answer)) {
$qo->answer = '*';
}
$qo->feedback[] = $obj->feedback;
$qo->tolerance[] = $this->getpath($answer, array('#', 'tolerance', 0, '#'), 0);
// Fraction as a tag is deprecated.
$fraction = $this->getpath($answer, array('@', 'fraction'), 0) / 100;
$qo->fraction[] = $this->getpath($answer,
array('#', 'fraction', 0, '#'), $fraction); // Deprecated.
}
// Get the units array.
$qo->unit = array();
$units = $this->getpath($question, array('#', 'units', 0, '#', 'unit'), array());
if (!empty($units)) {
$qo->multiplier = array();
foreach ($units as $unit) {
$qo->multiplier[] = $this->getpath($unit, array('#', 'multiplier', 0, '#'), 1);
$qo->unit[] = $this->getpath($unit, array('#', 'unit_name', 0, '#'), '', true);
}
}
$qo->unitgradingtype = $this->getpath($question, array('#', 'unitgradingtype', 0, '#'), 0);
$qo->unitpenalty = $this->getpath($question, array('#', 'unitpenalty', 0, '#'), 0.1);
$qo->showunits = $this->getpath($question, array('#', 'showunits', 0, '#'), null);
$qo->unitsleft = $this->getpath($question, array('#', 'unitsleft', 0, '#'), 0);
$qo->instructions['text'] = '';
$qo->instructions['format'] = FORMAT_HTML;
$instructions = $this->getpath($question, array('#', 'instructions'), array());
if (!empty($instructions)) {
$qo->instructions = $this->import_text_with_files($instructions,
array('0'), '', $this->get_format($qo->questiontextformat));
}
if (is_null($qo->showunits)) {
// Set a good default, depending on whether there are any units defined.
if (empty($qo->unit)) {
$qo->showunits = 3; // This is qtype_numerical::UNITNONE, but we cannot refer to that constant here.
} else {
$qo->showunits = 0; // This is qtype_numerical::UNITOPTIONAL, but we cannot refer to that constant here.
}
}
$this->import_hints($qo, $question, false, false, $this->get_format($qo->questiontextformat));
return $qo;
}
/**
* Import matching type question
* @param array question question array from xml tree
* @return object question object
*/
public function import_match($question) {
// Get common parts.
$qo = $this->import_headers($question);
// Header parts particular to matching.
$qo->qtype = 'match';
$qo->shuffleanswers = $this->trans_single($this->getpath($question,
array('#', 'shuffleanswers', 0, '#'), 1));
// Run through subquestions.
$qo->subquestions = array();
$qo->subanswers = array();
foreach ($question['#']['subquestion'] as $subqxml) {
$qo->subquestions[] = $this->import_text_with_files($subqxml,
array(), '', $this->get_format($qo->questiontextformat));
$answers = $this->getpath($subqxml, array('#', 'answer'), array());
$qo->subanswers[] = $this->getpath($subqxml,
array('#', 'answer', 0, '#', 'text', 0, '#'), '', true);
}
$this->import_combined_feedback($qo, $question, true);
$this->import_hints($qo, $question, true, false, $this->get_format($qo->questiontextformat));
return $qo;
}
/**
* Import essay type question
* @param array question question array from xml tree
* @return object question object
*/
public function import_essay($question) {
// Get common parts.
$qo = $this->import_headers($question);
// Header parts particular to essay.
$qo->qtype = 'essay';
$qo->responseformat = $this->getpath($question,
array('#', 'responseformat', 0, '#'), 'editor');
$qo->responsefieldlines = $this->getpath($question,
array('#', 'responsefieldlines', 0, '#'), 15);
$qo->responserequired = $this->getpath($question,
array('#', 'responserequired', 0, '#'), 1);
$qo->attachments = $this->getpath($question,
array('#', 'attachments', 0, '#'), 0);
$qo->attachmentsrequired = $this->getpath($question,
array('#', 'attachmentsrequired', 0, '#'), 0);
$qo->graderinfo = $this->import_text_with_files($question,
array('#', 'graderinfo', 0), '', $this->get_format($qo->questiontextformat));
$qo->responsetemplate['text'] = $this->getpath($question,
array('#', 'responsetemplate', 0, '#', 'text', 0, '#'), '', true);
$qo->responsetemplate['format'] = $this->trans_format($this->getpath($question,
array('#', 'responsetemplate', 0, '@', 'format'), $this->get_format($qo->questiontextformat)));
return $qo;
}
/**
* Import a calculated question
* @param object $question the imported XML data.
*/
public function import_calculated($question) {
// Get common parts.
$qo = $this->import_headers($question);
// Header parts particular to calculated.
$qo->qtype = 'calculated';
$qo->synchronize = $this->getpath($question, array('#', 'synchronize', 0, '#'), 0);
$single = $this->getpath($question, array('#', 'single', 0, '#'), 'true');
$qo->single = $this->trans_single($single);
$shuffleanswers = $this->getpath($question, array('#', 'shuffleanswers', 0, '#'), 'false');
$qo->answernumbering = $this->getpath($question,
array('#', 'answernumbering', 0, '#'), 'abc');
$qo->shuffleanswers = $this->trans_single($shuffleanswers);
$this->import_combined_feedback($qo, $question);
$qo->unitgradingtype = $this->getpath($question,
array('#', 'unitgradingtype', 0, '#'), 0);
$qo->unitpenalty = $this->getpath($question, array('#', 'unitpenalty', 0, '#'), null);
$qo->showunits = $this->getpath($question, array('#', 'showunits', 0, '#'), 0);
$qo->unitsleft = $this->getpath($question, array('#', 'unitsleft', 0, '#'), 0);
$qo->instructions = $this->getpath($question,
array('#', 'instructions', 0, '#', 'text', 0, '#'), '', true);
if (!empty($instructions)) {
$qo->instructions = $this->import_text_with_files($instructions,
array('0'), '', $this->get_format($qo->questiontextformat));
}
// Get answers array.
$answers = $question['#']['answer'];
$qo->answers = array();
$qo->feedback = array();
$qo->fraction = array();
$qo->tolerance = array();
$qo->tolerancetype = array();
$qo->correctanswerformat = array();
$qo->correctanswerlength = array();
$qo->feedback = array();
foreach ($answers as $answer) {
$ans = $this->import_answer($answer, true, $this->get_format($qo->questiontextformat));
// Answer outside of <text> is deprecated.
if (empty($ans->answer['text'])) {
$ans->answer['text'] = '*';
}
$qo->answers[] = $ans->answer;
$qo->feedback[] = $ans->feedback;
$qo->tolerance[] = $answer['#']['tolerance'][0]['#'];
// Fraction as a tag is deprecated.
if (!empty($answer['#']['fraction'][0]['#'])) {
$qo->fraction[] = $answer['#']['fraction'][0]['#'];
} else {
$qo->fraction[] = $answer['@']['fraction'] / 100;
}
$qo->tolerancetype[] = $answer['#']['tolerancetype'][0]['#'];
$qo->correctanswerformat[] = $answer['#']['correctanswerformat'][0]['#'];
$qo->correctanswerlength[] = $answer['#']['correctanswerlength'][0]['#'];
}
// Get units array.
$qo->unit = array();
if (isset($question['#']['units'][0]['#']['unit'])) {
$units = $question['#']['units'][0]['#']['unit'];
$qo->multiplier = array();
foreach ($units as $unit) {
$qo->multiplier[] = $unit['#']['multiplier'][0]['#'];
$qo->unit[] = $unit['#']['unit_name'][0]['#'];
}
}
$instructions = $this->getpath($question, array('#', 'instructions'), array());
if (!empty($instructions)) {
$qo->instructions = $this->import_text_with_files($instructions,
array('0'), '', $this->get_format($qo->questiontextformat));
}
if (is_null($qo->unitpenalty)) {
// Set a good default, depending on whether there are any units defined.
if (empty($qo->unit)) {
$qo->showunits = 3; // This is qtype_numerical::UNITNONE, but we cannot refer to that constant here.
} else {
$qo->showunits = 0; // This is qtype_numerical::UNITOPTIONAL, but we cannot refer to that constant here.
}
}
$datasets = $question['#']['dataset_definitions'][0]['#']['dataset_definition'];
$qo->dataset = array();
$qo->datasetindex= 0;
foreach ($datasets as $dataset) {
$qo->datasetindex++;
$qo->dataset[$qo->datasetindex] = new stdClass();
$qo->dataset[$qo->datasetindex]->status =
$this->import_text($dataset['#']['status'][0]['#']['text']);
$qo->dataset[$qo->datasetindex]->name =
$this->import_text($dataset['#']['name'][0]['#']['text']);
$qo->dataset[$qo->datasetindex]->type =
$dataset['#']['type'][0]['#'];
$qo->dataset[$qo->datasetindex]->distribution =
$this->import_text($dataset['#']['distribution'][0]['#']['text']);
$qo->dataset[$qo->datasetindex]->max =
$this->import_text($dataset['#']['maximum'][0]['#']['text']);
$qo->dataset[$qo->datasetindex]->min =
$this->import_text($dataset['#']['minimum'][0]['#']['text']);
$qo->dataset[$qo->datasetindex]->length =
$this->import_text($dataset['#']['decimals'][0]['#']['text']);
$qo->dataset[$qo->datasetindex]->distribution =
$this->import_text($dataset['#']['distribution'][0]['#']['text']);
$qo->dataset[$qo->datasetindex]->itemcount = $dataset['#']['itemcount'][0]['#'];
$qo->dataset[$qo->datasetindex]->datasetitem = array();
$qo->dataset[$qo->datasetindex]->itemindex = 0;
$qo->dataset[$qo->datasetindex]->number_of_items =
$dataset['#']['number_of_items'][0]['#'];
$datasetitems = $dataset['#']['dataset_items'][0]['#']['dataset_item'];
foreach ($datasetitems as $datasetitem) {
$qo->dataset[$qo->datasetindex]->itemindex++;
$qo->dataset[$qo->datasetindex]->datasetitem[
$qo->dataset[$qo->datasetindex]->itemindex] = new stdClass();
$qo->dataset[$qo->datasetindex]->datasetitem[
$qo->dataset[$qo->datasetindex]->itemindex]->itemnumber =
$datasetitem['#']['number'][0]['#'];
$qo->dataset[$qo->datasetindex]->datasetitem[
$qo->dataset[$qo->datasetindex]->itemindex]->value =
$datasetitem['#']['value'][0]['#'];
}
}
$this->import_hints($qo, $question, false, false, $this->get_format($qo->questiontextformat));
return $qo;
}
/**
* This is not a real question type. It's a dummy type used to specify the
* import category. The format is:
* <question type="category">
* <category>tom/dick/harry</category>
* </question>
*/
protected function import_category($question) {
$qo = new stdClass();
$qo->qtype = 'category';
$qo->category = $this->import_text($question['#']['category'][0]['#']['text']);
return $qo;
}
/**
* Parse the array of lines into an array of questions
* this *could* burn memory - but it won't happen that much
* so fingers crossed!
* @param array of lines from the input file.
* @param stdClass $context
* @return array (of objects) question objects.
*/
protected function readquestions($lines) {
// We just need it as one big string.
$lines = implode('', $lines);
// This converts xml to big nasty data structure
// the 0 means keep white space as it is (important for markdown format).
try {
$xml = xmlize($lines, 0, 'UTF-8', true);
} catch (xml_format_exception $e) {
$this->error($e->getMessage(), '');
return false;
}
unset($lines); // No need to keep this in memory.
return $this->import_questions($xml['quiz']['#']['question']);
}
/**
* @param array $xml the xmlized xml
* @return stdClass[] question objects to pass to question type save_question_options
*/
public function import_questions($xml) {
$questions = array();
// Iterate through questions.
foreach ($xml as $questionxml) {
$qo = $this->import_question($questionxml);
// Stick the result in the $questions array.
if ($qo) {
$questions[] = $qo;
}
}
return $questions;
}
/**
* @param array $questionxml xml describing the question
* @return null|stdClass an object with data to be fed to question type save_question_options
*/
protected function import_question($questionxml) {
$questiontype = $questionxml['@']['type'];
if ($questiontype == 'multichoice') {
return $this->import_multichoice($questionxml);
} else if ($questiontype == 'truefalse') {
return $this->import_truefalse($questionxml);
} else if ($questiontype == 'shortanswer') {
return $this->import_shortanswer($questionxml);
} else if ($questiontype == 'numerical') {
return $this->import_numerical($questionxml);
} else if ($questiontype == 'description') {
return $this->import_description($questionxml);
} else if ($questiontype == 'matching' || $questiontype == 'match') {
return $this->import_match($questionxml);
} else if ($questiontype == 'cloze' || $questiontype == 'multianswer') {
return $this->import_multianswer($questionxml);
} else if ($questiontype == 'essay') {
return $this->import_essay($questionxml);
} else if ($questiontype == 'calculated') {
return $this->import_calculated($questionxml);
} else if ($questiontype == 'calculatedsimple') {
$qo = $this->import_calculated($questionxml);
$qo->qtype = 'calculatedsimple';
return $qo;
} else if ($questiontype == 'calculatedmulti') {
$qo = $this->import_calculated($questionxml);
$qo->qtype = 'calculatedmulti';
return $qo;
} else if ($questiontype == 'category') {
return $this->import_category($questionxml);
} else {
// Not a type we handle ourselves. See if the question type wants
// to handle it.
if (!$qo = $this->try_importing_using_qtypes($questionxml, null, null, $questiontype)) {
$this->error(get_string('xmltypeunsupported', 'qformat_xml', $questiontype));
return null;
}
return $qo;
}
}
// EXPORT FUNCTIONS START HERE.
public function export_file_extension() {
return '.xml';
}
/**
* Turn the internal question type name into a human readable form.
* (In the past, the code used to use integers internally. Now, it uses
* strings, so there is less need for this, but to maintain
* backwards-compatibility we change two of the type names.)
* @param string $qtype question type plugin name.
* @return string $qtype string to use in the file.
*/
protected function get_qtype($qtype) {
switch($qtype) {
case 'match':