Merge branch 'wip-MDL-40038-master' of git://github.com/abgreeve/moodle
authorDamyon Wiese <damyon@moodle.com>
Tue, 20 Aug 2013 07:51:57 +0000 (15:51 +0800)
committerDamyon Wiese <damyon@moodle.com>
Tue, 20 Aug 2013 07:51:57 +0000 (15:51 +0800)
Conflicts:
files/tests/externallib_test.php

files/externallib.php
files/tests/externallib_test.php

index e086c6e..e97478d 100644 (file)
@@ -47,13 +47,16 @@ class core_files_external extends external_api {
     public static function get_files_parameters() {
         return new external_function_parameters(
             array(
-                'contextid' => new external_value(PARAM_INT, 'context id'),
-                'component' => new external_value(PARAM_TEXT, 'component'),
-                'filearea'  => new external_value(PARAM_TEXT, 'file area'),
-                'itemid'    => new external_value(PARAM_INT, 'associated id'),
-                'filepath'  => new external_value(PARAM_PATH, 'file path'),
-                'filename'  => new external_value(PARAM_FILE, 'file name'),
-                'modified' => new external_value(PARAM_INT, 'timestamp to return files changed after this time.', VALUE_DEFAULT, null)
+                'contextid'    => new external_value(PARAM_INT, 'context id Set to -1 to use contextlevel and instanceid.'),
+                'component'    => new external_value(PARAM_TEXT, 'component'),
+                'filearea'     => new external_value(PARAM_TEXT, 'file area'),
+                'itemid'       => new external_value(PARAM_INT, 'associated id'),
+                'filepath'     => new external_value(PARAM_PATH, 'file path'),
+                'filename'     => new external_value(PARAM_FILE, 'file name'),
+                'modified'     => new external_value(PARAM_INT, 'timestamp to return files changed after this time.', VALUE_DEFAULT, null),
+                'contextlevel' => new external_value(PARAM_ALPHA, 'The context level for the file location.', VALUE_DEFAULT, null),
+                'instanceid'   => new external_value(PARAM_INT, 'The instance id for where the file is located.', VALUE_DEFAULT, null)
+
             )
         );
     }
@@ -68,22 +71,41 @@ class core_files_external extends external_api {
      * @param string $filepath file path
      * @param string $filename file name
      * @param int $modified timestamp to return files changed after this time.
+     * @param string $contextlevel The context level for the file location.
+     * @param int $instanceid The instance id for where the file is located.
      * @return array
      * @since Moodle 2.2
      */
-    public static function get_files($contextid, $component, $filearea, $itemid, $filepath, $filename, $modified = null) {
-        global $CFG, $USER, $OUTPUT;
-        $fileinfo = self::validate_parameters(self::get_files_parameters(), array(
-                    'contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea,
-                    'itemid'=>$itemid, 'filepath'=>$filepath, 'filename'=>$filename, 'modified'=>$modified));
+    public static function get_files($contextid, $component, $filearea, $itemid, $filepath, $filename, $modified = null,
+                                     $contextlevel = null, $instanceid = null) {
+
+        $parameters = array(
+            'contextid'    => $contextid,
+            'component'    => $component,
+            'filearea'     => $filearea,
+            'itemid'       => $itemid,
+            'filepath'     => $filepath,
+            'filename'     => $filename,
+            'modified'     => $modified,
+            'contextlevel' => $contextlevel,
+            'instanceid'   => $instanceid);
+        $fileinfo = self::validate_parameters(self::get_files_parameters(), $parameters);
 
         $browser = get_file_browser();
 
-        if (empty($fileinfo['contextid'])) {
-            $context  = context_system::instance();
+        // We need to preserve backwards compatibility. Zero will use the system context and minus one will
+        // use the addtional parameters to determine the context.
+        // TODO MDL-40489 get_context_from_params should handle this logic.
+        if ($fileinfo['contextid'] == 0) {
+            $context = context_system::instance();
         } else {
-            $context  = context::instance_by_id($fileinfo['contextid']);
+            if ($fileinfo['contextid'] == -1) {
+                $fileinfo['contextid'] = null;
+            }
+            $context = self::get_context_from_params($fileinfo);
         }
+        self::validate_context($context);
+
         if (empty($fileinfo['component'])) {
             $fileinfo['component'] = null;
         }
@@ -104,6 +126,7 @@ class core_files_external extends external_api {
         $return['parents'] = array();
         $return['files'] = array();
         $list = array();
+
         if ($file = $browser->get_file_info(
             $context, $fileinfo['component'], $fileinfo['filearea'], $fileinfo['itemid'],
                 $fileinfo['filepath'], $fileinfo['filename'])) {
index 7f6de7d..cbb8b61 100644 (file)
@@ -175,4 +175,122 @@ class core_files_externallib_testcase extends advanced_testcase {
         $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
         $this->assertNotEmpty($file);
     }
+
+    public function test_get_files() {
+        global $USER, $DB;
+
+        $this->resetAfterTest();
+
+        $this->setAdminUser();
+        $USER->email = 'test@moodle.com';
+        // print_object($USER);
+
+        $course = $this->getDataGenerator()->create_course();
+        $record = new stdClass();
+        $record->course = $course->id;
+        $record->name = "Mod data upload test";
+
+        $record->intro = "Some intro of some sort";
+
+        $module = $this->getDataGenerator()->create_module('data', $record);
+
+        $field = data_get_field_new('file', $module);
+
+        $fielddetail = new stdClass();
+        $fielddetail->d = $module->id;
+        $fielddetail->mode = 'add';
+        $fielddetail->type = 'file';
+        $fielddetail->sesskey = sesskey();
+        $fielddetail->name = 'Upload file';
+        $fielddetail->description = 'Some description';
+        $fielddetail->param3 = '0';
+
+        $field->define_field($fielddetail);
+        $field->insert_field();
+        $recordid = data_add_record($module);
+
+        $timemodified = $DB->get_field('data_records', 'timemodified', array('id' => $recordid));
+
+        $datacontent = array();
+        $datacontent['fieldid'] = $field->field->id;
+        $datacontent['recordid'] = $recordid;
+        $datacontent['content'] = 'Simple4.txt';
+
+        $contentid = $DB->insert_record('data_content', $datacontent);
+
+        $context = context_module::instance($module->id);
+        $usercontext = context_user::instance($USER->id);
+        $component = 'mod_data';
+        $filearea = 'content';
+        $itemid = $contentid;
+        $filename = $datacontent['content'];
+        $filecontent = base64_encode("Let us create a nice simple file.");
+
+        $filerecord = array();
+        $filerecord['contextid'] = $context->id;
+        $filerecord['component'] = $component;
+        $filerecord['filearea'] = $filearea;
+        $filerecord['itemid'] = $itemid;
+        $filerecord['filepath'] = '/';
+        $filerecord['filename'] = $filename;
+
+        $fs = get_file_storage();
+        $file = $fs->create_file_from_string($filerecord, $filecontent);
+
+        $filename = '';
+        $testfilelisting = core_files_external::get_files($context->id, $component, $filearea, $itemid, '/', $filename);
+
+        $testdata = array();
+        $testdata['parents'] = array();
+        $testdata['parents']['0'] = array('contextid' => 1,
+                                          'component' => null,
+                                          'filearea' => null,
+                                          'itemid' => null,
+                                          'filepath' => null,
+                                          'filename' => 'System');
+        $testdata['parents']['1'] = array('contextid' => 3,
+                                          'component' => null,
+                                          'filearea' => null,
+                                          'itemid' => null,
+                                          'filepath' => null,
+                                          'filename' => 'Miscellaneous');
+        $testdata['parents']['2'] = array('contextid' => 15,
+                                          'component' => null,
+                                          'filearea' => null,
+                                          'itemid' => null,
+                                          'filepath' => null,
+                                          'filename' => 'Test course 1');
+        $testdata['parents']['3'] = array('contextid' => 20,
+                                          'component' => null,
+                                          'filearea' => null,
+                                          'itemid' => null,
+                                          'filepath' => null,
+                                          'filename' => 'Mod data upload test (Database)');
+        $testdata['parents']['4'] = array('contextid' => 20,
+                                          'component' => 'mod_data',
+                                          'filearea' => 'content',
+                                          'itemid' => null,
+                                          'filepath' => null,
+                                          'filename' => 'Fields');
+        $testdata['files'] = array();
+        $testdata['files']['0'] = array('contextid' => 20,
+                                        'component' => 'mod_data',
+                                        'filearea' => 'content',
+                                        'itemid' => 1,
+                                        'filepath' => '/',
+                                        'filename' => 'Simple4.txt',
+                                        'url' => 'http://www.example.com/moodle/pluginfile.php/20/mod_data/content/1/Simple4.txt',
+                                        'isdir' => null,
+                                        'timemodified' => $timemodified);
+
+        $this->assertEquals($testfilelisting, $testdata);
+
+        // Try again but without the context.
+        $nocontext = -1;
+        $modified = 0;
+        $contextlevel = 'module';
+        $instanceid = $module->id;
+        $testfilelisting = core_files_external::get_files($nocontext, $component, $filearea, $itemid, '/', $filename, $modified, $contextlevel, $instanceid);
+        $this->assertEquals($testfilelisting, $testdata);
+    }
 }