]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-15931 refactored file_browser.php
authornicolasconnault <nicolasconnault>
Mon, 20 Apr 2009 08:18:33 +0000 (08:18 +0000)
committernicolasconnault <nicolasconnault>
Mon, 20 Apr 2009 08:18:33 +0000 (08:18 +0000)
lib/file/file_browser.php
lib/simpletest/testfilelib.php

index d6c196cab86bab372952e1246ad7e0b5cb7a3bbf..021e09d8c74441300c222d5f9cb940f759312106 100644 (file)
@@ -36,332 +36,420 @@ class file_browser {
     public function get_file_info($context, $filearea=null, $itemid=null, $filepath=null, $filename=null) {
         global $USER, $CFG, $DB, $COURSE;
 
-        $fs = get_file_storage();
+        if (empty($context->contextlevel)) {
+            throw new exception('incompletecontextobject');
+        } elseif ($contextname = $this->get_context_name($context->contextlevel)) {
+            $function_name = "get_file_info_$contextname";
 
-        if ($context->contextlevel == CONTEXT_SYSTEM) {
-            if (is_null($filearea)) {
-                return new file_info_system($this);
+            if (method_exists($this, $function_name)) {
+                return $this->$function_name($context, $filearea, $itemid, $filepath, $filename);
+            } else {
+                throw new exception('undefinedmethod');
             }
-            //TODO: question files browsing
+        } else {
+            throw new exception('invalidcontext');
+        }
+    }
 
-        } else if ($context->contextlevel == CONTEXT_USER) {
+    /**
+     * Returns info about the files at System context
+     * @param object $context
+     * @param string $filearea
+     * @return file_info_system
+     */
+    public function get_file_info_system($context, $filearea=null) {
+        if (is_null($filearea)) {
+            return new file_info_system($this);
+        }
+        //TODO: question files browsing
 
-            if ($context->instanceid == $USER->id) {
-                $user = $USER;
-            } else {
-                $user = $DB->get_record('user', array('id'=>$context->instanceid));
-            }
+        return null;
+    }
 
-            if (isguestuser($user) or empty($user->id)) {
-                // no guests or not logged in users here
-                return null;
-            }
+    /**
+     * Returns info about the files at User context
+     * @param object $context
+     * @param string $filearea
+     * @return file_info_system
+     */
+    public function get_file_info_user($context, $filearea=null, $itemid=null, $filepath=null, $filename=null) {
+        global $USER, $DB;
+        if ($context->instanceid == $USER->id) {
+            $user = $USER;
+        } else {
+            $user = $DB->get_record('user', array('id'=>$context->instanceid));
+        }
+
+        if (isguestuser($user) or empty($user->id)) {
+            // no guests or not logged in users here
+            return null;
+        }
 
-            if ($user->deleted) {
+        if ($user->deleted) {
+            return null;
+        }
+
+        if (is_null($filearea)) {
+            // access control: list areas only for myself
+            if ($context->instanceid != $USER->id) {
                 return null;
             }
 
-            if (is_null($filearea)) {
-                // access control: list areas only for myself
-                if ($context->instanceid != $USER->id) {
-                    return null;
-                }
+            return new file_info_user($this, $context);
 
-                return new file_info_user($this, $context);
+        } else {
+            $methodname = "get_file_info_$filearea";
+            if (method_exists($this, $methodname)) {
+                return $this->$methodname($user, $context, $filearea, $itemid, $filepath, $filename);
+            }
+        }
 
-            } else {
-                if ($filearea == 'user_private') {
-                    // access control: only my files for now, nobody else
-                    if ($context->instanceid != $USER->id) {
-                        return null;
-                    }
+        return null;
+    }
 
-                    if (is_null($itemid)) {
-                        return new file_info_user($this, $context);
-                    }
-                    $filepath = is_null($filepath) ? '/' : $filepath;
-                    $filename = is_null($filename) ? '.' : $filename;
-
-                    if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
-                        if ($filepath === '/' and $filename === '.') {
-                            $storedfile = new virtual_root_file($context->id, $filearea, 0);
-                        } else {
-                            // not found
-                            return null;
-                        }
-                    }
-                    $urlbase = $CFG->wwwroot.'/userfile.php';
-                    return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areauserpersonal', 'repository'), false, true, true, false);
+    private function get_file_info_user_private($user, $context, $filearea=null, $itemid=null, $filepath=null, $filename=null) {
+        global $USER, $CFG;
 
-                } else if ($filearea == 'user_profile') {
-                    if (is_null($itemid)) {
-                        return new file_info_user($this, $context);
-                    }
+        $fs = get_file_storage();
 
-                    // access controll here must match user edit forms
-                    if ($user->id == $USER->id) {
-                         if (!has_capability('moodle/user:editownprofile', get_context_instance(CONTEXT_SYSTEM))) {
-                            return null;
-                         }
-                    } else {
-                        if (!has_capability('moodle/user:editprofile', $context) and !has_capability('moodle/user:update', $context)) {
-                            return null;
-                        }
-                    }
+        // access control: only my files for now, nobody else
+        if ($context->instanceid != $USER->id) {
+            return null;
+        }
 
-                    $filepath = is_null($filepath) ? '/' : $filepath;
-                    $filename = is_null($filename) ? '.' : $filename;
+        if (is_null($itemid)) {
+            return new file_info_user($this, $context);
+        }
+        $filepath = is_null($filepath) ? '/' : $filepath;
+        $filename = is_null($filename) ? '.' : $filename;
 
-                    if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
-                        if ($filepath === '/' and $filename === '.') {
-                            $storedfile = new virtual_root_file($context->id, $filearea, 0);
-                        } else {
-                            // not found
-                            return null;
-                        }
-                    }
-                    $urlbase = $CFG->wwwroot.'/userfile.php';
-                    return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areauserprofile', 'repository'), false, true, true, false);
+        if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
+            if ($filepath === '/' and $filename === '.') {
+                $storedfile = new virtual_root_file($context->id, $filearea, 0);
+            } else {
+                // not found
+                return null;
+            }
+        }
+        $urlbase = $CFG->wwwroot.'/userfile.php';
+        return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areauserpersonal', 'repository'), false, true, true, false);
+    }
 
+    public function get_file_info_user_profile($user, $context, $filearea=null, $itemid=null, $filepath=null, $filename=null) {
+        global $USER, $CFG;
 
-                } else if ($filearea == 'user_draft') {
-                    // access control: only my files
-                    if ($context->instanceid != $USER->id) {
-                        return null;
-                    }
+        $fs = get_file_storage();
 
-                    if (empty($itemid)) {
-                        // do not browse itemids - you most know the draftid to see what is there
-                        return null;
-                    }
-                    $urlbase = $CFG->wwwroot.'/draftfile.php';
-
-                    $filepath = is_null($filepath) ? '/' : $filepath;
-                    $filename = is_null($filename) ? '.' : $filename;
-
-                    if (!$storedfile = $fs->get_file($context->id, $filearea, $itemid, $filepath, $filename)) {
-                        if ($filepath === '/' and $filename === '.') {
-                            $storedfile = new virtual_root_file($context->id, $filearea, $itemid);
-                        } else {
-                            // not found
-                            return null;
-                        }
-                    }
-                    return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areauserdraft', 'repository'), true, true, true, true);
-                }
-            }
+        if (is_null($itemid)) {
+            return new file_info_user($this, $context);
+        }
 
-        } else if ($context->contextlevel == CONTEXT_COURSECAT) {
-            if (!$category = $DB->get_record('course_categories', array('id'=>$context->instanceid))) {
+        // access controll here must match user edit forms
+        if ($user->id == $USER->id) {
+             if (!has_capability('moodle/user:editownprofile', get_context_instance(CONTEXT_SYSTEM))) {
+                return null;
+             }
+        } else {
+            if (!has_capability('moodle/user:editprofile', $context) and !has_capability('moodle/user:update', $context)) {
                 return null;
             }
+        }
 
-            if (!$category->visible and !has_capability('moodle/course:viewhiddencourses', $context)) {
+        $filepath = is_null($filepath) ? '/' : $filepath;
+        $filename = is_null($filename) ? '.' : $filename;
+
+        if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
+            if ($filepath === '/' and $filename === '.') {
+                $storedfile = new virtual_root_file($context->id, $filearea, 0);
+            } else {
+                // not found
                 return null;
             }
+        }
+        $urlbase = $CFG->wwwroot.'/userfile.php';
+        return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areauserprofile', 'repository'), false, true, true, false);
+    }
 
-            if (!is_null($filearea) and !in_array($filearea, array('coursecat_intro'))) {
-                // file area does not exist, sorry
-                $filearea = null;
-            }
+    public function get_file_info_user_draft($user, $context, $filearea=null, $itemid=null, $filepath=null, $filename=null) {
+        global $USER, $CFG;
+        $fs = get_file_storage();
 
-            if (is_null($filearea) or is_null($itemid)) {
-                return new file_info_coursecat($this, $context, $category);
+        // access control: only my files
+        if ($context->instanceid != $USER->id) {
+            return null;
+        }
 
-            } else {
-                if ($filearea == 'coursecat_intro') {
-                    if (!has_capability('moodle/course:update', $context)) {
-                        return null;
-                    }
+        if (empty($itemid)) {
+            // do not browse itemids - you most know the draftid to see what is there
+            return null;
+        }
+        $urlbase = $CFG->wwwroot.'/draftfile.php';
 
-                    $filepath = is_null($filepath) ? '/' : $filepath;
-                    $filename = is_null($filename) ? '.' : $filename;
-
-                    $urlbase = $CFG->wwwroot.'/pluginfile.php';
-                    if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
-                        if ($filepath === '/' and $filename === '.') {
-                            $storedfile = new virtual_root_file($context->id, $filearea, 0);
-                        } else {
-                            // not found
-                            return null;
-                        }
-                    }
-                    return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areacategoryintro', 'repository'), false, true, true, false);
-                }
-            }
+        $filepath = is_null($filepath) ? '/' : $filepath;
+        $filename = is_null($filename) ? '.' : $filename;
 
-        } else if ($context->contextlevel == CONTEXT_COURSE) {
-            if ($context->instanceid == $COURSE->id) {
-                $course = $COURSE;
-            } else if (!$course = $DB->get_record('course', array('id'=>$context->instanceid))) {
+        if (!$storedfile = $fs->get_file($context->id, $filearea, $itemid, $filepath, $filename)) {
+            if ($filepath === '/' and $filename === '.') {
+                $storedfile = new virtual_root_file($context->id, $filearea, $itemid);
+            } else {
+                // not found
                 return null;
             }
+        }
+        return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areauserdraft', 'repository'), true, true, true, true);
+    }
 
-            if (!$course->visible and !has_capability('moodle/course:viewhiddencourses', $context)) {
-                return null;
-            }
+    public function get_file_info_coursecat($context, $filearea=null, $itemid=null, $filepath=null, $filename=null) {
+        global $DB, $CFG;
 
-            if (!is_null($filearea) and !in_array($filearea, array('course_intro', 'course_content', 'course_section', 'course_backup'))) {
-                // file area does not exist, sorry
-                $filearea = null;
-            }
+        $fs = get_file_storage();
 
-            $filepath = is_null($filepath) ? '/' : $filepath;
-            $filename = is_null($filename) ? '.' : $filename;
+        if (!$category = $DB->get_record('course_categories', array('id'=>$context->instanceid))) {
+            return null;
+        }
 
-            if (is_null($filearea)) {
-                return new file_info_course($this, $context, $course);
+        if (!$category->visible and !has_capability('moodle/course:viewhiddencourses', $context)) {
+            return null;
+        }
 
-            } else {
-                if ($filearea === 'course_intro') {
-                    if (!has_capability('moodle/course:update', $context)) {
-                        return null;
-                    }
-                    if (is_null($itemid)) {
-                        return new file_info_course($this, $context, $course);
-                    }
+        if (!is_null($filearea) and !in_array($filearea, array('coursecat_intro'))) {
+            // file area does not exist, sorry
+            $filearea = null;
+        }
 
-                    $urlbase = $CFG->wwwroot.'/pluginfile.php';
-                    if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
-                        if ($filepath === '/' and $filename === '.') {
-                            $storedfile = new virtual_root_file($context->id, $filearea, 0);
-                        } else {
-                            // not found
-                            return null;
-                        }
-                    }
-                    return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areacourseintro', 'repository'), false, true, true, false);
+        if (is_null($filearea) or is_null($itemid)) {
+            return new file_info_coursecat($this, $context, $category);
 
-                } else if ($filearea === 'course_section') {
-                    if (!has_capability('moodle/course:update', $context)) {
-                        return null;
-                    }
-                    $urlbase = $CFG->wwwroot.'/pluginfile.php';
+        } else {
+            if ($filearea == 'coursecat_intro') {
+                if (!has_capability('moodle/course:update', $context)) {
+                    return null;
+                }
 
-                    if (empty($itemid)) {
-                        // list all sections
-                        return new file_info_coursesection($this, $context, $course);
-                    }
+                $filepath = is_null($filepath) ? '/' : $filepath;
+                $filename = is_null($filename) ? '.' : $filename;
 
-                    if (!$section = $DB->get_record('course_sections', array('course'=>$course->id, 'id'=>$itemid))) {
-                        return null; // does not exist
+                $urlbase = $CFG->wwwroot.'/pluginfile.php';
+                if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
+                    if ($filepath === '/' and $filename === '.') {
+                        $storedfile = new virtual_root_file($context->id, $filearea, 0);
+                    } else {
+                        // not found
+                        return null;
                     }
+                }
+                return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areacategoryintro', 'repository'), false, true, true, false);
+            }
+        }
 
-                    if (!$storedfile = $fs->get_file($context->id, $filearea, $itemid, $filepath, $filename)) {
-                        if ($filepath === '/' and $filename === '.') {
-                            $storedfile = new virtual_root_file($context->id, $filearea, $itemid);
-                        } else {
-                            // not found
-                            return null;
-                        }
-                    }
-                    return new file_info_stored($this, $context, $storedfile, $urlbase, $section->section, true, true, true, false);
+        return null;
+    }
 
-                } else if ($filearea == 'course_backup') {
-                    if (!has_capability('moodle/site:backup', $context) and !has_capability('moodle/site:restore', $context)) {
-                        return null;
-                    }
-                    if (is_null($itemid)) {
-                        return new file_info_course($this, $context, $course);
-                    }
+    public function get_file_info_course($context, $filearea=null, $itemid=null, $filepath=null, $filename=null) {
+        global $DB, $COURSE;
 
-                    $urlbase = $CFG->wwwroot.'/pluginfile.php';
-                    if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
-                        if ($filepath === '/' and $filename === '.') {
-                            $storedfile = new virtual_root_file($context->id, $filearea, 0);
-                        } else {
-                            // not found
-                            return null;
-                        }
-                    }
+        if ($context->instanceid == $COURSE->id) {
+            $course = $COURSE;
+        } else if (!$course = $DB->get_record('course', array('id'=>$context->instanceid))) {
+            return null;
+        }
 
-                    $downloadable = has_capability('moodle/site:backupdownload', $context);
-                    $uploadable   = has_capability('moodle/site:backupupload', $context);
-                    return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areabackup', 'repository'), false, $downloadable, $uploadable, false);
+        if (!$course->visible and !has_capability('moodle/course:viewhiddencourses', $context)) {
+            return null;
+        }
 
-                } else if ($filearea == 'course_content') {
-                    if (!has_capability('moodle/course:managefiles', $context)) {
-                        return null;
-                    }
-                    if (is_null($itemid)) {
-                        return new file_info_course($this, $context, $course);
-                    }
+        if (!is_null($filearea) and !in_array($filearea, array('course_intro', 'course_content', 'course_section', 'course_backup'))) {
+            // file area does not exist, sorry
+            $filearea = null;
+        }
 
-                    if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
-                        if ($filepath === '/' and $filename === '.') {
-                            $storedfile = new virtual_root_file($context->id, $filearea, 0);
-                        } else {
-                            // not found
-                            return null;
-                        }
-                    }
+        $filepath = is_null($filepath) ? '/' : $filepath;
+        $filename = is_null($filename) ? '.' : $filename;
 
-                    return new file_info_coursefile($this, $context, $storedfile);
-                }
-            }
+        if (is_null($filearea)) {
+            return new file_info_course($this, $context, $course);
 
-        } else if ($context->contextlevel == CONTEXT_MODULE) {
-            if (!$cm = get_coursemodule_from_id('', $context->instanceid)) {
-                return null;
+        } else {
+            $methodname = "get_file_info_$filearea";
+            if (method_exists($this, $methodname)) {
+                return $this->$methodname($course, $context, $filearea, $itemid, $filepath, $filename);
             }
+        }
+
+        return null;
+    }
+
+    public function get_file_info_course_intro($course, $context, $filearea=null, $itemid=null, $filepath=null, $filename=null) {
+        global $CFG;
+
+        $fs = get_file_storage();
 
-            if ($cm->course == $COURSE->id) {
-                $course = $COURSE;
-            } else if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
+        if (!has_capability('moodle/course:update', $context)) {
+            return null;
+        }
+        if (is_null($itemid)) {
+            return new file_info_course($this, $context, $course);
+        }
+
+        $urlbase = $CFG->wwwroot.'/pluginfile.php';
+        if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
+            if ($filepath === '/' and $filename === '.') {
+                $storedfile = new virtual_root_file($context->id, $filearea, 0);
+            } else {
+                // not found
                 return null;
             }
+        }
+        return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areacourseintro', 'repository'), false, true, true, false);
+
+    }
 
-            $modinfo = get_fast_modinfo($course);
+    public function get_file_info_course_section($course, $context, $filearea=null, $itemid=null, $filepath=null, $filename=null) {
+        global $CFG, $DB;
 
-            if (empty($modinfo->cms[$cm->id]->uservisible)) {
+        $fs = get_file_storage();
+
+        if (!has_capability('moodle/course:update', $context)) {
+            return null;
+        }
+        $urlbase = $CFG->wwwroot.'/pluginfile.php';
+
+        if (empty($itemid)) {
+            // list all sections
+            return new file_info_coursesection($this, $context, $course);
+        }
+
+        if (!$section = $DB->get_record('course_sections', array('course'=>$course->id, 'id'=>$itemid))) {
+            return null; // does not exist
+        }
+
+        if (!$storedfile = $fs->get_file($context->id, $filearea, $itemid, $filepath, $filename)) {
+            if ($filepath === '/' and $filename === '.') {
+                $storedfile = new virtual_root_file($context->id, $filearea, $itemid);
+            } else {
+                // not found
                 return null;
             }
+        }
+        return new file_info_stored($this, $context, $storedfile, $urlbase, $section->section, true, true, true, false);
 
-            $modname = $modinfo->cms[$cm->id]->modname;
+    }
 
-            $libfile = "$CFG->dirroot/mod/$modname/lib.php";
-            if (!file_exists($libfile)) {
+    public function get_file_info_course_backup($course, $context, $filearea=null, $itemid=null, $filepath=null, $filename=null) {
+        global $CFG;
+
+        $fs = get_file_storage();
+
+        if (!has_capability('moodle/site:backup', $context) and !has_capability('moodle/site:restore', $context)) {
+            return null;
+        }
+        if (is_null($itemid)) {
+            return new file_info_course($this, $context, $course);
+        }
+
+        $urlbase = $CFG->wwwroot.'/pluginfile.php';
+        if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
+            if ($filepath === '/' and $filename === '.') {
+                $storedfile = new virtual_root_file($context->id, $filearea, 0);
+            } else {
+                // not found
                 return null;
             }
-            require_once($libfile);
+        }
 
-            $fileinfofunction = $modname.'_get_file_areas';
-            if (function_exists($fileinfofunction)) {
-                $areas = $fileinfofunction($course, $cm, $context);
+        $downloadable = has_capability('moodle/site:backupdownload', $context);
+        $uploadable   = has_capability('moodle/site:backupupload', $context);
+        return new file_info_stored($this, $context, $storedfile, $urlbase, get_string('areabackup', 'repository'), false, $downloadable, $uploadable, false);
+
+    }
+
+    public function get_file_info_course_content($course, $context, $filearea=null, $itemid=null, $filepath=null, $filename=null) {
+        $fs = get_file_storage();
+
+        if (!has_capability('moodle/course:managefiles', $context)) {
+            return null;
+        }
+        if (is_null($itemid)) {
+            return new file_info_course($this, $context, $course);
+        }
+
+        if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
+            if ($filepath === '/' and $filename === '.') {
+                $storedfile = new virtual_root_file($context->id, $filearea, 0);
             } else {
+                // not found
                 return null;
             }
-            if (is_null($filearea) or is_null($itemid)) {
-                return new file_info_module($this, $course, $cm, $context, $areas);
+        }
 
-            } else if (!isset($areas[$filearea])) {
-                return null;
+        return new file_info_coursefile($this, $context, $storedfile);
+    }
 
-            } else if ($filearea === $modname.'_intro') {
-                if (!has_capability('moodle/course:managefiles', $context)) {
-                    return null;
-                }
+    public function get_file_info_module($context, $filearea=null, $itemid=null, $filepath=null, $filename=null) {
+        global $COURSE, $DB, $CFG;
 
-                $filepath = is_null($filepath) ? '/' : $filepath;
-                $filename = is_null($filename) ? '.' : $filename;
+        $fs = get_file_storage();
 
-                $urlbase = $CFG->wwwroot.'/pluginfile.php';
-                if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
-                    if ($filepath === '/' and $filename === '.') {
-                        $storedfile = new virtual_root_file($context->id, $filearea, 0);
-                    } else {
-                        // not found
-                        return null;
-                    }
-                }
-                return new file_info_stored($this, $context, $storedfile, $urlbase, $areas[$filearea], false, true, true, false);
+        if (!$cm = get_coursemodule_from_id('', $context->instanceid)) {
+            return null;
+        }
 
-            } else {
-                $fileinfofunction = $modname.'_get_file_info';
-                if (function_exists($fileinfofunction)) {
-                    return $fileinfofunction($this, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename);
+        if ($cm->course == $COURSE->id) {
+            $course = $COURSE;
+        } else if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
+            return null;
+        }
+
+        $modinfo = get_fast_modinfo($course);
+
+        if (empty($modinfo->cms[$cm->id]->uservisible)) {
+            return null;
+        }
+
+        $modname = $modinfo->cms[$cm->id]->modname;
+
+        $libfile = "$CFG->dirroot/mod/$modname/lib.php";
+        if (!file_exists($libfile)) {
+            return null;
+        }
+        require_once($libfile);
+
+        $fileinfofunction = $modname.'_get_file_areas';
+        if (function_exists($fileinfofunction)) {
+            $areas = $fileinfofunction($course, $cm, $context);
+        } else {
+            return null;
+        }
+        if (is_null($filearea) or is_null($itemid)) {
+            return new file_info_module($this, $course, $cm, $context, $areas);
+
+        } else if (!isset($areas[$filearea])) {
+            return null;
+
+        } else if ($filearea === $modname.'_intro') {
+            if (!has_capability('moodle/course:managefiles', $context)) {
+                return null;
+            }
+
+            $filepath = is_null($filepath) ? '/' : $filepath;
+            $filename = is_null($filename) ? '.' : $filename;
+
+            $urlbase = $CFG->wwwroot.'/pluginfile.php';
+            if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
+                if ($filepath === '/' and $filename === '.') {
+                    $storedfile = new virtual_root_file($context->id, $filearea, 0);
+                } else {
+                    // not found
+                    return null;
                 }
             }
+            return new file_info_stored($this, $context, $storedfile, $urlbase, $areas[$filearea], false, true, true, false);
+
+        } else {
+            $fileinfofunction = $modname.'_get_file_info';
+            if (function_exists($fileinfofunction)) {
+                return $fileinfofunction($this, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename);
+            }
         }
 
         return null;
@@ -423,4 +511,20 @@ class file_browser {
         return $return;
     }
 
+    private function get_context_name($contextlevel) {
+        switch ($contextlevel) {
+            case CONTEXT_SYSTEM:
+                return 'system';
+            case CONTEXT_USER:
+                return 'user';
+            case CONTEXT_COURSECAT:
+                return 'coursecat';
+            case CONTEXT_COURSE:
+                return 'course';
+            case CONTEXT_MODULE:
+                return 'module';
+        }
+
+        return false;
+    }
 }
index 455752fa82e83116b4b6f27e2d5ab1301eadeb1e..946cdfdf64a10ccc3e5b8a02bab32f80af11bb45 100644 (file)
@@ -41,51 +41,51 @@ class filelib_test extends UnitTestCase {
 
         $options = array('var1' => 'value1', 'var2' => 'value2');
         $this->assertEqual($CFG->wwwroot.'/file.php?file=%2Fpath%2Fto%2Ffile%2Ffile.txt&amp;var1=value1&amp;var2=value2', get_file_url($path, $options));
-        
+
         $this->assertEqual($CFG->httpswwwroot.'/file.php?file=%2Fpath%2Fto%2Ffile%2Ffile.txt&amp;var1=value1&amp;var2=value2', get_file_url($path, $options, 'httpscoursefile'));
-        
+
         $path = 'C:\\path\\to\\file.txt';
         $this->assertEqual($CFG->wwwroot.'/file.php?file=%2FC%3A%5Cpath%5Cto%5Cfile.txt&amp;var1=value1&amp;var2=value2', get_file_url($path, $options));
-        
+
         // With slasharguments on
         $CFG->slasharguments = true;
-        
+
         $path = '/path/to/file/file.txt';
         $this->assertEqual($CFG->wwwroot.'/file.php'.$path, get_file_url($path));
 
         $options = array('var1' => 'value1', 'var2' => 'value2');
         $this->assertEqual($CFG->wwwroot.'/file.php'.$path.'?var1=value1&amp;var2=value2', get_file_url($path, $options));
-        
+
         $this->assertEqual($CFG->httpswwwroot.'/file.php'.$path.'?var1=value1&amp;var2=value2', get_file_url($path, $options, 'httpscoursefile'));
-        
+
         $path = 'C:\\path\\to\\file.txt';
         $this->assertEqual($CFG->wwwroot.'/file.php/C%3A%5Cpath%5Cto%5Cfile.txt?var1=value1&amp;var2=value2', get_file_url($path, $options));
 
         $path = '/path/to/file/file.txt';
-        
-        $HTTPSPAGEREQUIRED = true; 
+
+        $HTTPSPAGEREQUIRED = true;
         $this->assertEqual($CFG->httpswwwroot.'/user/pix.php'.$path, get_file_url($path, null, 'user'));
-        $HTTPSPAGEREQUIRED = false; 
+        $HTTPSPAGEREQUIRED = false;
         $this->assertEqual($CFG->wwwroot.'/user/pix.php'.$path, get_file_url($path, null, 'user'));
 
         $this->assertEqual($CFG->wwwroot.'/question/exportfile.php'.$path, get_file_url($path, null, 'questionfile'));
         $this->assertEqual($CFG->wwwroot.'/rss/file.php'.$path, get_file_url($path, null, 'rssfile'));
-        
+
         // Test relative path
-        $path = 'relative/path/to/file.txt'; 
+        $path = 'relative/path/to/file.txt';
         $this->assertEqual($CFG->wwwroot.'/file.php/'.$path, get_file_url($path));
-        
+
         // Test with anchor in path
-        $path = 'relative/path/to/index.html#anchor1'; 
+        $path = 'relative/path/to/index.html#anchor1';
         $this->assertEqual($CFG->wwwroot.'/file.php/'.$path, get_file_url($path));
-        
+
         // Test with anchor and funny characters in path
-        $path = 'rela89èà7(##&$tive/path/to /indéx.html#anchor1'; 
+        $path = 'rela89èà7(##&$tive/path/to /indéx.html#anchor1';
         $this->assertEqual($CFG->wwwroot.'/file.php/rela89%C3%A8%C3%A07%28##%26%24tive/path/to%20/ind%C3%A9x.html#anchor1', get_file_url($path));
     }
 }
 
-require_once($CFG->libdir.'/file/file_browser.php'); 
+require_once($CFG->libdir.'/file/file_browser.php');
 /**
  * Tests for file_browser class
  * @note This class is barely testable. Only one of the methods doesn't make direct calls to complex global functions.
@@ -113,7 +113,8 @@ class file_browser_test extends UnitTestCase {
 }
 
 
-require_once($CFG->libdir.'/file/file_info_course.php'); 
+require_once($CFG->libdir.'/file/file_info_course.php');
+
 /**
  * Tests for file_info_course class
  * TODO we need a test course for this