Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

MDL-37313 Moodle XML import format should use draft file areas, not a…

…rrays
  • Loading branch information...
commit af8a12287205d7c0d3c9fc7585b440126980efc3 1 parent d63a406
Jean-Michel Vedrine jmvedrine authored
226 question/format/xml/format.php
@@ -149,6 +149,40 @@ public function getpath($xml, $path, $default, $istext=false, $error='') {
149 149 return $xml;
150 150 }
151 151
  152 + public function import_text_with_files($data, $path, $defaultvalue = '', $defaultformat = 'html') {
  153 + $field = array();
  154 + $field['text'] = $this->getpath($data,
  155 + array_merge($path, array('#', 'text', 0, '#')), $defaultvalue, true);
  156 + $field['format'] = $this->trans_format($this->getpath($data,
  157 + array_merge($path, array('@', 'format')), $defaultformat));
  158 + $itemid = $this->import_files_as_draft($this->getpath($data,
  159 + array_merge($path, array('#', 'file')), array(), false));
  160 + if (!empty($itemid)) {
  161 + $field['itemid'] = $itemid;
  162 + }
  163 + return $field;
  164 + }
  165 +
  166 + public function import_files_as_draft($xml) {
  167 + global $USER;
  168 + if (empty($xml)) {
  169 + return null;
  170 + }
  171 + $fs = get_file_storage();
  172 + $itemid = file_get_unused_draft_itemid();
  173 + foreach ($xml as $file) {
  174 + $filerecord = array(
  175 + 'contextid' => context_user::instance($USER->id)->id,
  176 + 'component' => 'user',
  177 + 'filearea' => 'draft',
  178 + 'itemid' => $itemid,
  179 + 'filepath' => '/',
  180 + 'filename' => $file['@']['name'],
  181 + );
  182 + $fs->create_file_from_string($filerecord, base64_decode($file['#']));
  183 + }
  184 + return $itemid;
  185 + }
152 186
153 187 /**
154 188 * import parts of question common to all types
@@ -156,7 +190,7 @@ public function getpath($xml, $path, $default, $istext=false, $error='') {
156 190 * @return object question object
157 191 */
158 192 public function import_headers($question) {
159   - global $CFG;
  193 + global $CFG, $USER;
160 194
161 195 // get some error strings
162 196 $error_noname = get_string('xmlimportnoname', 'qformat_xml');
@@ -169,35 +203,42 @@ public function import_headers($question) {
169 203 $qo->name = $this->clean_question_name($this->getpath($question,
170 204 array('#', 'name', 0, '#', 'text', 0, '#'), '', true,
171 205 get_string('xmlimportnoname', 'qformat_xml')));
172   - $qo->questiontext = $this->getpath($question,
173   - array('#', 'questiontext', 0, '#', 'text', 0, '#'), '', true);
174   - $qo->questiontextformat = $this->trans_format($this->getpath(
175   - $question, array('#', 'questiontext', 0, '@', 'format'), 'html'));
176   -
177   - $qo->questiontextfiles = $this->import_files($this->getpath($question,
178   - array('#', 'questiontext', 0, '#', 'file'), array(), false));
179   -
  206 + $questiontext = $this->import_text_with_files($question,
  207 + array('#', 'questiontext', 0));
  208 + $qo->questiontext = $questiontext['text'];
  209 + $qo->questiontextformat = $questiontext['format'];
  210 + if (!empty($questiontext['itemid'])) {
  211 + $qo->questiontextitemid = $questiontext['itemid'];
  212 + }
180 213 // Backwards compatibility, deal with the old image tag.
181 214 $filedata = $this->getpath($question, array('#', 'image_base64', '0', '#'), null, false);
182 215 $filename = $this->getpath($question, array('#', 'image', '0', '#'), null, false);
183 216 if ($filedata && $filename) {
184   - $data = new stdClass();
185   - $data->content = $filedata;
186   - $data->encoding = 'base64';
187   - // Question file areas don't support subdirs, so convert path to filename if necessary.
188   - $data->name = clean_param(str_replace('/', '_', $filename), PARAM_FILE);
189   - $qo->questiontextfiles[] = $data;
190   - $qo->questiontext .= ' <img src="@@PLUGINFILE@@/' . $data->name . '" />';
  217 + $fs = get_file_storage();
  218 + if (empty($qo->questiontextitemid)) {
  219 + $qo->questiontextitemid = file_get_unused_draft_itemid();
  220 + }
  221 + $filename = clean_param(str_replace('/', '_', $filename), PARAM_FILE);
  222 + $filerecord = array(
  223 + 'contextid' => context_user::instance($USER->id)->id,
  224 + 'component' => 'user',
  225 + 'filearea' => 'draft',
  226 + 'itemid' => $qo->questiontextitemid,
  227 + 'filepath' => '/',
  228 + 'filename' => $filename,
  229 + );
  230 + $fs->create_file_from_string($filerecord, base64_decode($filedata));
  231 + $qo->questiontext .= ' <img src="@@PLUGINFILE@@/' . $filename . '" />';
191 232 }
192 233
193 234 // restore files in generalfeedback
194   - $qo->generalfeedback = $this->getpath($question,
195   - array('#', 'generalfeedback', 0, '#', 'text', 0, '#'), $qo->generalfeedback, true);
196   - $qo->generalfeedbackfiles = array();
197   - $qo->generalfeedbackformat = $this->trans_format($this->getpath($question,
198   - array('#', 'generalfeedback', 0, '@', 'format'), $this->get_format($qo->questiontextformat)));
199   - $qo->generalfeedbackfiles = $this->import_files($this->getpath($question,
200   - array('#', 'generalfeedback', 0, '#', 'file'), array(), false));
  235 + $generalfeedback = $this->import_text_with_files($question,
  236 + array('#', 'generalfeedback', 0), $qo->generalfeedback, $this->get_format($qo->questiontextformat));
  237 + $qo->generalfeedback = $generalfeedback['text'];
  238 + $qo->generalfeedbackformat = $generalfeedback['format'];
  239 + if (!empty($generalfeedback['itemid'])) {
  240 + $qo->generalfeedbackitemid = $generalfeedback['itemid'];
  241 + }
201 242
202 243 $qo->defaultmark = $this->getpath($question,
203 244 array('#', 'defaultgrade', 0, '#'), $qo->defaultmark);
@@ -234,24 +275,15 @@ public function import_headers($question) {
234 275 public function import_answer($answer, $withanswerfiles = false, $defaultformat = 'html') {
235 276 $ans = new stdClass();
236 277
237   - $ans->answer = array();
238   - $ans->answer['text'] = $this->getpath($answer, array('#', 'text', 0, '#'), '', true);
239   - $ans->answer['format'] = $this->trans_format($this->getpath($answer,
240   - array('@', 'format'), $defaultformat));
241 278 if ($withanswerfiles) {
242   - $ans->answer['files'] = $this->import_files($this->getpath($answer,
243   - array('#', 'file'), array()));
  279 + $ans->answer = $this->import_text_with_files($answer, array(), '', $defaultformat);
244 280 } else {
  281 + $ans->answer = array();
  282 + $ans->answer['text'] = $this->getpath($answer, array('#', 'text', 0, '#'), '', true);
245 283 $ans->answer['format'] = FORMAT_PLAIN;
246 284 }
247 285
248   - $ans->feedback = array();
249   - $ans->feedback['text'] = $this->getpath($answer,
250   - array('#', 'feedback', 0, '#', 'text', 0, '#'), '', true);
251   - $ans->feedback['format'] = $this->trans_format($this->getpath($answer,
252   - array('#', 'feedback', 0, '@', 'format'), $defaultformat));
253   - $ans->feedback['files'] = $this->import_files($this->getpath($answer,
254   - array('#', 'feedback', 0, '#', 'file'), array()));
  286 + $ans->feedback = $this->import_text_with_files($answer, array('#', 'feedback', 0), '', $defaultformat);
255 287
256 288 $ans->fraction = $this->getpath($answer, array('@', 'fraction'), 0) / 100;
257 289
@@ -267,15 +299,8 @@ public function import_answer($answer, $withanswerfiles = false, $defaultformat
267 299 public function import_combined_feedback($qo, $questionxml, $withshownumpartscorrect = false) {
268 300 $fields = array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback');
269 301 foreach ($fields as $field) {
270   - $text = array();
271   - $text['text'] = $this->getpath($questionxml,
272   - array('#', $field, 0, '#', 'text', 0, '#'), '', true);
273   - $text['format'] = $this->trans_format($this->getpath($questionxml,
274   - array('#', $field, 0, '@', 'format'), $this->get_format($qo->questiontextformat)));
275   - $text['files'] = $this->import_files($this->getpath($questionxml,
276   - array('#', $field, 0, '#', 'file'), array(), false));
277   -
278   - $qo->$field = $text;
  302 + $qo->$field = $this->import_text_with_files($questionxml,
  303 + array('#', $field, 0), '', $this->get_format($qo->questiontextformat));
279 304 }
280 305
281 306 if ($withshownumpartscorrect) {
@@ -296,15 +321,13 @@ public function import_combined_feedback($qo, $questionxml, $withshownumpartscor
296 321 * @return object hint for storing in the database.
297 322 */
298 323 public function import_hint($hintxml, $defaultformat) {
  324 + $hint = new stdClass();
299 325 if (array_key_exists('hintcontent', $hintxml['#'])) {
300 326 // Backwards compatibility:
301 327
302   - $hint = new stdClass();
303   - $hint->hint = array('format' => FORMAT_HTML, 'files' => array());
304   - $hint->hint['text'] = $this->getpath($hintxml,
305   - array('#', 'hintcontent', 0, '#', 'text', 0, '#'), '', true);
306   - $hint->hint['format'] = $this->trans_format($defaultformat);
307   - $hint->hint['files'] = array();
  328 + $hint->hint = $this->import_text_with_files($hintxml,
  329 + array('#', 'hintcontent', 0), '', $defaultformat);
  330 +
308 331 $hint->shownumcorrect = $this->getpath($hintxml,
309 332 array('#', 'statenumberofcorrectresponses', 0, '#'), 0);
310 333 $hint->clearwrong = $this->getpath($hintxml,
@@ -314,14 +337,7 @@ public function import_hint($hintxml, $defaultformat) {
314 337
315 338 return $hint;
316 339 }
317   -
318   - $hint = new stdClass();
319   - $hint->hint['text'] = $this->getpath($hintxml,
320   - array('#', 'text', 0, '#'), '', true);
321   - $hint->hint['format'] = $this->trans_format($this->getpath($hintxml,
322   - array('@', 'format'), $defaultformat));
323   - $hint->hint['files'] = $this->import_files($this->getpath($hintxml,
324   - array('#', 'file'), array(), false));
  340 + $hint->hint = $this->import_text_with_files($hintxml, array(), '', $defaultformat);
325 341 $hint->shownumcorrect = array_key_exists('shownumcorrect', $hintxml['#']);
326 342 $hint->clearwrong = array_key_exists('clearwrong', $hintxml['#']);
327 343 $hint->options = $this->getpath($hintxml, array('#', 'options', 0, '#'), '', true);
@@ -440,9 +456,11 @@ public function import_multianswer($question) {
440 456 $qo->name = $this->clean_question_name($this->import_text($question['#']['name'][0]['#']['text']));
441 457 $qo->questiontextformat = $questiontext['format'];
442 458 $qo->questiontext = $qo->questiontext['text'];
443   - $qo->questiontextfiles = $this->import_files($this->getpath($question,
  459 + $itemid = $this->import_files($this->getpath($question,
444 460 array('#', 'questiontext', 0, '#', 'file'), array(), false));
445   -
  461 + if (!empty($itemid)) {
  462 + $qo->questiontextitemid = $itemid;
  463 + }
446 464 // Backwards compatibility, deal with the old image tag.
447 465 $filedata = $this->getpath($question, array('#', 'image_base64', '0', '#'), null, false);
448 466 $filename = $this->getpath($question, array('#', 'image', '0', '#'), null, false);
@@ -457,12 +475,13 @@ public function import_multianswer($question) {
457 475 }
458 476
459 477 // restore files in generalfeedback
460   - $qo->generalfeedback = $this->getpath($question,
461   - array('#', 'generalfeedback', 0, '#', 'text', 0, '#'), $qo->generalfeedback, true);
462   - $qo->generalfeedbackformat = $this->trans_format($this->getpath($question,
463   - array('#', 'generalfeedback', 0, '@', 'format'), $this->get_format($qo->questiontextformat)));
464   - $qo->generalfeedbackfiles = $this->import_files($this->getpath($question,
465   - array('#', 'generalfeedback', 0, '#', 'file'), array(), false));
  478 + $generalfeedback = $this->import_text_with_files($question,
  479 + array('#', 'generalfeedback', 0), $qo->generalfeedback, $this->get_format($qo->questiontextformat));
  480 + $qo->generalfeedback = $generalfeedback['text'];
  481 + $qo->generalfeedbackformat = $generalfeedback['format'];
  482 + if (!empty($generalfeedback['itemid'])) {
  483 + $qo->generalfeedbackitemid = $generalfeedback['itemid'];
  484 + }
466 485
467 486 $qo->penalty = $this->getpath($question,
468 487 array('#', 'penalty', 0, '#'), $this->defaultquestion()->penalty);
@@ -498,20 +517,9 @@ public function import_truefalse($question) {
498 517 foreach ($question['#']['answer'] as $answer) {
499 518 $answertext = $this->getpath($answer,
500 519 array('#', 'text', 0, '#'), '', true);
501   - $feedback = $this->getpath($answer,
502   - array('#', 'feedback', 0, '#', 'text', 0, '#'), '', true);
503   - $feedbackformat = $this->getpath($answer,
504   - array('#', 'feedback', 0, '@', 'format'), $this->get_format($qo->questiontextformat));
505   - $feedbackfiles = $this->getpath($answer,
506   - array('#', 'feedback', 0, '#', 'file'), array());
507   - $files = array();
508   - foreach ($feedbackfiles as $file) {
509   - $data = new stdClass();
510   - $data->content = $file['#'];
511   - $data->encoding = $file['@']['encoding'];
512   - $data->name = $file['@']['name'];
513   - $files[] = $data;
514   - }
  520 + $feedback = $this->import_text_with_files($answer,
  521 + array('#', 'feedback', 0), '', $this->get_format($qo->questiontextformat));
  522 +
515 523 if ($answertext != 'true' && $answertext != 'false') {
516 524 // Old style file, assume order is true/false.
517 525 $warning = true;
@@ -525,17 +533,11 @@ public function import_truefalse($question) {
525 533 if ($answertext == 'true') {
526 534 $qo->answer = ($answer['@']['fraction'] == 100);
527 535 $qo->correctanswer = $qo->answer;
528   - $qo->feedbacktrue = array();
529   - $qo->feedbacktrue['text'] = $feedback;
530   - $qo->feedbacktrue['format'] = $this->trans_format($feedbackformat);
531   - $qo->feedbacktrue['files'] = $files;
  536 + $qo->feedbacktrue = $feedback;
532 537 } else {
533 538 $qo->answer = ($answer['@']['fraction'] != 100);
534 539 $qo->correctanswer = $qo->answer;
535   - $qo->feedbackfalse = array();
536   - $qo->feedbackfalse['text'] = $feedback;
537   - $qo->feedbackfalse['format'] = $this->trans_format($feedbackformat);
538   - $qo->feedbackfalse['files'] = $files;
  540 + $qo->feedbackfalse = $feedback;
539 541 }
540 542 $first = false;
541 543 }
@@ -650,13 +652,8 @@ public function import_numerical($question) {
650 652 $qo->instructions['format'] = FORMAT_HTML;
651 653 $instructions = $this->getpath($question, array('#', 'instructions'), array());
652 654 if (!empty($instructions)) {
653   - $qo->instructions = array();
654   - $qo->instructions['text'] = $this->getpath($instructions,
655   - array('0', '#', 'text', '0', '#'), '', true);
656   - $qo->instructions['format'] = $this->trans_format($this->getpath($instructions,
657   - array('0', '@', 'format'), $this->get_format($qo->questiontextformat)));
658   - $qo->instructions['files'] = $this->import_files($this->getpath(
659   - $instructions, array('0', '#', 'file'), array()));
  655 + $qo->instructions = $this->import_text_with_files($instructions,
  656 + array('0'), '', $this->get_format($qo->questiontextformat));
660 657 }
661 658
662 659 if (is_null($qo->showunits)) {
@@ -691,14 +688,9 @@ public function import_match($question) {
691 688 $qo->subquestions = array();
692 689 $qo->subanswers = array();
693 690 foreach ($question['#']['subquestion'] as $subqxml) {
694   - $subquestion = array();
695   - $subquestion['text'] = $this->getpath($subqxml, array('#', 'text', 0, '#'), '', true);
696   - $subquestion['format'] = $this->trans_format($this->getpath($subqxml,
697   - array('@', 'format'), $this->get_format($qo->questiontextformat)));
698   - $subquestion['files'] = $this->import_files($this->getpath($subqxml,
699   - array('#', 'file'), array()));
700   -
701   - $qo->subquestions[] = $subquestion;
  691 + $qo->subquestions[] = $this->import_text_with_files($subqxml,
  692 + array(), '', $this->get_format($qo->questiontextformat));
  693 +
702 694 $answers = $this->getpath($subqxml, array('#', 'answer'), array());
703 695 $qo->subanswers[] = $this->getpath($subqxml,
704 696 array('#', 'answer', 0, '#', 'text', 0, '#'), '', true);
@@ -728,12 +720,8 @@ public function import_essay($question) {
728 720 array('#', 'responsefieldlines', 0, '#'), 15);
729 721 $qo->attachments = $this->getpath($question,
730 722 array('#', 'attachments', 0, '#'), 0);
731   - $qo->graderinfo['text'] = $this->getpath($question,
732   - array('#', 'graderinfo', 0, '#', 'text', 0, '#'), '', true);
733   - $qo->graderinfo['format'] = $this->trans_format($this->getpath($question,
734   - array('#', 'graderinfo', 0, '@', 'format'), $this->get_format($qo->questiontextformat)));
735   - $qo->graderinfo['files'] = $this->import_files($this->getpath($question,
736   - array('#', 'graderinfo', '0', '#', 'file'), array()));
  723 + $qo->graderinfo = $this->import_text_with_files($question,
  724 + array('#', 'graderinfo', 0), '', $this->get_format($qo->questiontextformat));
737 725
738 726 return $qo;
739 727 }
@@ -767,13 +755,8 @@ public function import_calculated($question) {
767 755 $qo->instructions = $this->getpath($question,
768 756 array('#', 'instructions', 0, '#', 'text', 0, '#'), '', true);
769 757 if (!empty($instructions)) {
770   - $qo->instructions = array();
771   - $qo->instructions['text'] = $this->getpath($instructions,
772   - array('0', '#', 'text', '0', '#'), '', true);
773   - $qo->instructions['format'] = $this->trans_format($this->getpath($instructions,
774   - array('0', '@', 'format'), $this->get_format($qo->questiontextformat)));
775   - $qo->instructions['files'] = $this->import_files($this->getpath($instructions,
776   - array('0', '#', 'file'), array()));
  758 + $qo->instructions = $this->import_text_with_files($instructions,
  759 + array('0'), '', $this->get_format($qo->questiontextformat));
777 760 }
778 761
779 762 // get answers array
@@ -817,13 +800,8 @@ public function import_calculated($question) {
817 800 }
818 801 $instructions = $this->getpath($question, array('#', 'instructions'), array());
819 802 if (!empty($instructions)) {
820   - $qo->instructions = array();
821   - $qo->instructions['text'] = $this->getpath($instructions,
822   - array('0', '#', 'text', '0', '#'), '', true);
823   - $qo->instructions['format'] = $this->trans_format($this->getpath($instructions,
824   - array('0', '@', 'format'), $this->get_format($qo->questiontextformat)));
825   - $qo->instructions['files'] = $this->import_files($this->getpath($instructions,
826   - array('0', '#', 'file'), array()));
  803 + $qo->instructions = $this->import_text_with_files($instructions,
  804 + array('0'), '', $this->get_format($qo->questiontextformat));
827 805 }
828 806
829 807 if (is_null($qo->unitpenalty)) {
101 question/format/xml/tests/xmlformat_test.php
@@ -202,9 +202,9 @@ public function test_import_hints_no_parts() {
202 202
203 203 $this->assertEquals(array(
204 204 array('text' => 'This is the first hint',
205   - 'format' => FORMAT_HTML, 'files' => array()),
  205 + 'format' => FORMAT_HTML),
206 206 array('text' => 'This is the second hint',
207   - 'format' => FORMAT_HTML, 'files' => array()),
  207 + 'format' => FORMAT_HTML),
208 208 ), $qo->hint);
209 209 $this->assertFalse(isset($qo->hintclearwrong));
210 210 $this->assertFalse(isset($qo->hintshownumcorrect));
@@ -232,9 +232,9 @@ public function test_import_hints_with_parts() {
232 232
233 233 $this->assertEquals(array(
234 234 array('text' => 'This is the first hint',
235   - 'format' => FORMAT_HTML, 'files' => array()),
  235 + 'format' => FORMAT_HTML),
236 236 array('text' => 'This is the second hint',
237   - 'format' => FORMAT_HTML, 'files' => array()),
  237 + 'format' => FORMAT_HTML),
238 238 ), $qo->hint);
239 239 $this->assertEquals(array(1, 0), $qo->hintclearwrong);
240 240 $this->assertEquals(array(0, 1), $qo->hintshownumcorrect);
@@ -360,7 +360,6 @@ public function test_import_essay_20() {
360 360 $expectedq->attachments = 0;
361 361 $expectedq->graderinfo['text'] = '';
362 362 $expectedq->graderinfo['format'] = FORMAT_MOODLE;
363   - $expectedq->graderinfo['files'] = array();
364 363
365 364 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
366 365 }
@@ -405,7 +404,6 @@ public function test_import_essay_21() {
405 404 $expectedq->attachments = -1;
406 405 $expectedq->graderinfo['text'] = '<p>Grade <b>generously</b>!</p>';
407 406 $expectedq->graderinfo['format'] = FORMAT_HTML;
408   - $expectedq->graderinfo['files'] = array();
409 407
410 408 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
411 409 }
@@ -531,12 +529,12 @@ public function test_import_match_19() {
531 529 $expectedq->questiontext = 'Match the upper and lower case letters.';
532 530 $expectedq->questiontextformat = FORMAT_HTML;
533 531 $expectedq->correctfeedback = array('text' => 'Well done.',
534   - 'format' => FORMAT_HTML, 'files' => array());
  532 + 'format' => FORMAT_HTML);
535 533 $expectedq->partiallycorrectfeedback = array('text' => 'Not entirely.',
536   - 'format' => FORMAT_HTML, 'files' => array());
  534 + 'format' => FORMAT_HTML);
537 535 $expectedq->shownumcorrect = false;
538 536 $expectedq->incorrectfeedback = array('text' => 'Completely wrong!',
539   - 'format' => FORMAT_HTML, 'files' => array());
  537 + 'format' => FORMAT_HTML);
540 538 $expectedq->generalfeedback = 'The answer is A -> a, B -> b and C -> c.';
541 539 $expectedq->generalfeedbackformat = FORMAT_HTML;
542 540 $expectedq->defaultmark = 1;
@@ -544,14 +542,14 @@ public function test_import_match_19() {
544 542 $expectedq->penalty = 0.3333333;
545 543 $expectedq->shuffleanswers = 0;
546 544 $expectedq->subquestions = array(
547   - array('text' => 'A', 'format' => FORMAT_HTML, 'files' => array()),
548   - array('text' => 'B', 'format' => FORMAT_HTML, 'files' => array()),
549   - array('text' => 'C', 'format' => FORMAT_HTML, 'files' => array()),
550   - array('text' => '', 'format' => FORMAT_HTML, 'files' => array()));
  545 + array('text' => 'A', 'format' => FORMAT_HTML),
  546 + array('text' => 'B', 'format' => FORMAT_HTML),
  547 + array('text' => 'C', 'format' => FORMAT_HTML),
  548 + array('text' => '', 'format' => FORMAT_HTML));
551 549 $expectedq->subanswers = array('a', 'b', 'c', 'd');
552 550 $expectedq->hint = array(
553   - array('text' => 'Hint 1', 'format' => FORMAT_HTML, 'files' => array()),
554   - array('text' => '', 'format' => FORMAT_HTML, 'files' => array()),
  551 + array('text' => 'Hint 1', 'format' => FORMAT_HTML),
  552 + array('text' => '', 'format' => FORMAT_HTML),
555 553 );
556 554 $expectedq->hintshownumcorrect = array(true, true);
557 555 $expectedq->hintclearwrong = array(false, true);
@@ -752,18 +750,15 @@ public function test_import_multichoice_19() {
752 750 $expectedq->questiontextformat = FORMAT_HTML;
753 751 $expectedq->correctfeedback = array(
754 752 'text' => '<p>Your answer is correct.</p>',
755   - 'format' => FORMAT_HTML,
756   - 'files' => array());
  753 + 'format' => FORMAT_HTML);
757 754 $expectedq->shownumcorrect = false;
758 755 $expectedq->partiallycorrectfeedback = array(
759 756 'text' => '<p>Your answer is partially correct.</p>',
760   - 'format' => FORMAT_HTML,
761   - 'files' => array());
  757 + 'format' => FORMAT_HTML);
762 758 $expectedq->shownumcorrect = true;
763 759 $expectedq->incorrectfeedback = array(
764 760 'text' => '<p>Your answer is incorrect.</p>',
765   - 'format' => FORMAT_HTML,
766   - 'files' => array());
  761 + 'format' => FORMAT_HTML);
767 762 $expectedq->generalfeedback = 'The even numbers are 2 and 4.';
768 763 $expectedq->defaultmark = 2;
769 764 $expectedq->length = 1;
@@ -772,20 +767,20 @@ public function test_import_multichoice_19() {
772 767 $expectedq->single = false;
773 768
774 769 $expectedq->answer = array(
775   - array('text' => '1', 'format' => FORMAT_HTML, 'files' => array()),
776   - array('text' => '2', 'format' => FORMAT_HTML, 'files' => array()),
777   - array('text' => '3', 'format' => FORMAT_HTML, 'files' => array()),
778   - array('text' => '4', 'format' => FORMAT_HTML, 'files' => array()));
  770 + array('text' => '1', 'format' => FORMAT_HTML),
  771 + array('text' => '2', 'format' => FORMAT_HTML),
  772 + array('text' => '3', 'format' => FORMAT_HTML),
  773 + array('text' => '4', 'format' => FORMAT_HTML));
779 774 $expectedq->fraction = array(0, 1, 0, 1);
780 775 $expectedq->feedback = array(
781   - array('text' => '', 'format' => FORMAT_HTML, 'files' => array()),
782   - array('text' => '', 'format' => FORMAT_HTML, 'files' => array()),
783   - array('text' => '', 'format' => FORMAT_HTML, 'files' => array()),
784   - array('text' => '', 'format' => FORMAT_HTML, 'files' => array()));
  776 + array('text' => '', 'format' => FORMAT_HTML),
  777 + array('text' => '', 'format' => FORMAT_HTML),
  778 + array('text' => '', 'format' => FORMAT_HTML),
  779 + array('text' => '', 'format' => FORMAT_HTML));
785 780
786 781 $expectedq->hint = array(
787   - array('text' => 'Hint 1.', 'format' => FORMAT_HTML, 'files' => array()),
788   - array('text' => 'Hint 2.', 'format' => FORMAT_HTML, 'files' => array()),
  782 + array('text' => 'Hint 1.', 'format' => FORMAT_HTML),
  783 + array('text' => 'Hint 2.', 'format' => FORMAT_HTML),
789 784 );
790 785 $expectedq->hintshownumcorrect = array(false, false);
791 786 $expectedq->hintclearwrong = array(false, false);
@@ -954,11 +949,11 @@ public function test_import_numerical_19() {
954 949 $expectedq->fraction = array(1, 0, 0);
955 950 $expectedq->feedback = array(
956 951 array('text' => 'Well done!',
957   - 'format' => FORMAT_HTML, 'files' => array()),
  952 + 'format' => FORMAT_HTML),
958 953 array('text' => 'What were you thinking?!',
959   - 'format' => FORMAT_HTML, 'files' => array()),
  954 + 'format' => FORMAT_HTML),
960 955 array('text' => 'Completely wrong.',
961   - 'format' => FORMAT_HTML, 'files' => array()));
  956 + 'format' => FORMAT_HTML));
962 957 $expectedq->tolerance = array(0.001, 1, 0);
963 958
964 959 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
@@ -1090,8 +1085,8 @@ public function test_import_shortanswer_19() {
1090 1085 $expectedq->answer = array('Beta', '*');
1091 1086 $expectedq->fraction = array(1, 0);
1092 1087 $expectedq->feedback = array(
1093   - array('text' => 'Well done!', 'format' => FORMAT_HTML, 'files' => array()),
1094   - array('text' => 'Doh!', 'format' => FORMAT_HTML, 'files' => array()));
  1088 + array('text' => 'Well done!', 'format' => FORMAT_HTML),
  1089 + array('text' => 'Doh!', 'format' => FORMAT_HTML));
1095 1090
1096 1091 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
1097 1092 }
@@ -1209,9 +1204,9 @@ public function test_import_truefalse_19() {
1209 1204 $expectedq->penalty = 1;
1210 1205
1211 1206 $expectedq->feedbacktrue = array('text' => 'Well done!',
1212   - 'format' => FORMAT_HTML, 'files' => array());
  1207 + 'format' => FORMAT_HTML);
1213 1208 $expectedq->feedbackfalse = array('text' => 'Doh!',
1214   - 'format' => FORMAT_HTML, 'files' => array());
  1209 + 'format' => FORMAT_HTML);
1215 1210 $expectedq->correctanswer = true;
1216 1211
1217 1212 $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
@@ -1313,8 +1308,8 @@ public function test_import_multianswer() {
1313 1308 $expectedqa->penalty = 0.5;
1314 1309
1315 1310 $expectedqa->hint = array(
1316   - array('text' => 'Hint 1', 'format' => FORMAT_HTML, 'files' => array()),
1317   - array('text' => 'Hint 2', 'format' => FORMAT_HTML, 'files' => array()),
  1311 + array('text' => 'Hint 1', 'format' => FORMAT_HTML),
  1312 + array('text' => 'Hint 2', 'format' => FORMAT_HTML),
1318 1313 );
1319 1314
1320 1315 $sa = new stdClass();
@@ -1406,4 +1401,30 @@ public function test_export_multianswer() {
1406 1401
1407 1402 $this->assert_same_xml($expectedxml, $xml);
1408 1403 }
  1404 +
  1405 + public function test_import_files_as_draft() {
  1406 + $this->resetAfterTest();
  1407 + $this->setAdminUser();
  1408 +
  1409 + $xml = <<<END
  1410 +<questiontext format="html">
  1411 + <text><![CDATA[<p><a href="@@PLUGINFILE@@/moodle.txt">This text file</a> contains the word 'Moodle'.</p>]]></text>
  1412 + <file name="moodle.txt" encoding="base64">TW9vZGxl</file>
  1413 +</questiontext>
  1414 +END;
  1415 +
  1416 + $textxml = xmlize($xml);
  1417 + $qo = new stdClass();
  1418 +
  1419 + $importer = new qformat_xml();
  1420 + $draftitemid = $importer->import_files_as_draft($textxml['questiontext']['#']['file']);
  1421 + $files = file_get_drafarea_files($draftitemid);
  1422 +
  1423 + $this->assertEquals(1, count($files->list));
  1424 +
  1425 + $file = $files->list[0];
  1426 + $this->assertEquals('moodle.txt', $file->filename);
  1427 + $this->assertEquals('/', $file->filepath);
  1428 + $this->assertEquals(6, $file->size);
  1429 + }
1409 1430 }

0 comments on commit af8a122

Please sign in to comment.
Something went wrong with that request. Please try again.