diff --git a/question/format.php b/question/format.php index 14f24839613..93280a78288 100644 --- a/question/format.php +++ b/question/format.php @@ -387,7 +387,7 @@ class qformat_default { $question->modifiedby = $USER->id; $question->timemodified = time(); $fileoptions = array( - 'subdirs' => false, + 'subdirs' => true, 'maxfiles' => -1, 'maxbytes' => 0, ); diff --git a/question/format/xml/format.php b/question/format/xml/format.php index df9e6094bbe..304d3794c8c 100644 --- a/question/format/xml/format.php +++ b/question/format/xml/format.php @@ -169,11 +169,13 @@ class qformat_xml extends qformat_default { } $fs = get_file_storage(); $itemid = file_get_unused_draft_itemid(); - $filenames = array(); + $filepaths = array(); foreach ($xml as $file) { - $filename = $file['@']['name']; - if (in_array($filename, $filenames)) { - debugging('Duplicate file in XML: ' . $filename, DEBUG_DEVELOPER); + $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( @@ -181,11 +183,11 @@ class qformat_xml extends qformat_default { 'component' => 'user', 'filearea' => 'draft', 'itemid' => $itemid, - 'filepath' => '/', + 'filepath' => $filepath, 'filename' => $filename, ); $fs->create_file_from_string($filerecord, base64_decode($file['#'])); - $filenames[] = $filename; + $filepaths[] = $fullpath; } return $itemid; } @@ -1092,9 +1094,9 @@ class qformat_xml extends qformat_default { if ($file->is_directory()) { continue; } - $string .= ''; + $string .= ''; $string .= base64_encode($file->get_content()); - $string .= ''; + $string .= "\n"; } return $string; } diff --git a/question/format/xml/tests/xmlformat_test.php b/question/format/xml/tests/xmlformat_test.php index 8396838b0d4..429f7876724 100644 --- a/question/format/xml/tests/xmlformat_test.php +++ b/question/format/xml/tests/xmlformat_test.php @@ -1473,4 +1473,51 @@ END; $this->assertEquals('/', $file->filepath); $this->assertEquals(6, $file->size); } + + public function test_import_truefalse_wih_files() { + $this->resetAfterTest(); + $this->setAdminUser(); + + $xml = ' + + truefalse + + + This text file contains the word Moodle.

]]>
+TW9vZGxl +
+ + For further information, see the documentation about Moodle.

]]>
+
+ 1.0000000 + 1.0000000 + 0 + + true + + + + + + false + + + + +
'; + $xmldata = xmlize($xml); + + $importer = new qformat_xml(); + $q = $importer->import_truefalse($xmldata['question']); + + $draftitemid = $q->questiontextitemid; + $files = file_get_drafarea_files($draftitemid, '/myfolder/'); + + $this->assertEquals(1, count($files->list)); + + $file = $files->list[0]; + $this->assertEquals('moodle.txt', $file->filename); + $this->assertEquals('/myfolder/', $file->filepath); + $this->assertEquals(6, $file->size); + } }