Skip to content

Commit

Permalink
Merge branch 'MDL-42033' of git://github.com/jmvedrine/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
danpoltawski committed Feb 17, 2014
2 parents a47e7a1 + 624ff5b commit 0a9f5b6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
2 changes: 1 addition & 1 deletion question/format.php
Expand Up @@ -387,7 +387,7 @@ public function importprocess($category) {
$question->modifiedby = $USER->id;
$question->timemodified = time();
$fileoptions = array(
'subdirs' => false,
'subdirs' => true,
'maxfiles' => -1,
'maxbytes' => 0,
);
Expand Down
18 changes: 10 additions & 8 deletions question/format/xml/format.php
Expand Up @@ -169,23 +169,25 @@ public function import_files_as_draft($xml) {
}
$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(
'contextid' => context_user::instance($USER->id)->id,
'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;
}
Expand Down Expand Up @@ -1092,9 +1094,9 @@ public function write_files($files) {
if ($file->is_directory()) {
continue;
}
$string .= '<file name="' . $file->get_filename() . '" encoding="base64">';
$string .= '<file name="' . $file->get_filename() . '" path="' . $file->get_filepath() . '" encoding="base64">';
$string .= base64_encode($file->get_content());
$string .= '</file>';
$string .= "</file>\n";
}
return $string;
}
Expand Down
47 changes: 47 additions & 0 deletions question/format/xml/tests/xmlformat_test.php
Expand Up @@ -1473,4 +1473,51 @@ public function test_import_files_as_draft() {
$this->assertEquals('/', $file->filepath);
$this->assertEquals(6, $file->size);
}

public function test_import_truefalse_wih_files() {
$this->resetAfterTest();
$this->setAdminUser();

$xml = '<question type="truefalse">
<name>
<text>truefalse</text>
</name>
<questiontext format="html">
<text><![CDATA[<p><a href="@@PLUGINFILE@@/myfolder/moodle.txt">This text file</a> contains the word Moodle.</p>]]></text>
<file name="moodle.txt" path="/myfolder/" encoding="base64">TW9vZGxl</file>
</questiontext>
<generalfeedback format="html">
<text><![CDATA[<p>For further information, see the documentation about Moodle.</p>]]></text>
</generalfeedback>
<defaultgrade>1.0000000</defaultgrade>
<penalty>1.0000000</penalty>
<hidden>0</hidden>
<answer fraction="100" format="moodle_auto_format">
<text>true</text>
<feedback format="html">
<text></text>
</feedback>
</answer>
<answer fraction="0" format="moodle_auto_format">
<text>false</text>
<feedback format="html">
<text></text>
</feedback>
</answer>
</question>';
$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);
}
}

0 comments on commit 0a9f5b6

Please sign in to comment.