3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
20 * Utility class for browsing of files.
23 * @subpackage filebrowser
24 * @copyright 2008 Petr Skoda (http://skodak.org)
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') ||
die();
30 require_once("$CFG->libdir/filebrowser/file_info.php");
33 require_once("$CFG->libdir/filebrowser/file_info_stored.php");
34 require_once("$CFG->libdir/filebrowser/virtual_root_file.php");
36 // description of available areas in each context level
37 require_once("$CFG->libdir/filebrowser/file_info_context_system.php");
38 require_once("$CFG->libdir/filebrowser/file_info_context_user.php");
39 require_once("$CFG->libdir/filebrowser/file_info_context_coursecat.php");
40 require_once("$CFG->libdir/filebrowser/file_info_context_course.php");
41 require_once("$CFG->libdir/filebrowser/file_info_context_module.php");
44 * This class provides the main entry point for other code wishing to get
45 * information about files.
47 * The whole file storage for a Moodle site can be seen as a huge virtual tree.
48 * The spine of the tree is the tree of contexts (system, course-categories,
49 * courses, modules, also users). Then, within each context, there may be any number of
50 * file areas, and a file area contains folders and files. The various file_info
51 * subclasses return info about the things in this tree. They should be obtained
52 * from an instance of this class.
54 * This virtual tree is different for each user depending of his/her current permissions.
55 * Some branches such as draft areas are hidden, but accessible.
57 * Always use this abstraction when you need to access module files from core code.
60 * @subpackage filebrowser
61 * @copyright 2008 Petr Skoda (http://skodak.org)
62 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
67 * Looks up file_info instance
68 * @param object $context
69 * @param string $component
70 * @param string $filearea
72 * @param string $filepath
73 * @param string $filename
74 * @return file_info instance or null if not found or access not allowed
76 public function get_file_info($context = NULL, $component = NULL, $filearea = NULL, $itemid = NULL, $filepath = NULL, $filename = NULL) {
78 $context = get_context_instance(CONTEXT_SYSTEM
);
80 switch ($context->contextlevel
) {
82 return $this->get_file_info_context_system($context, $component, $filearea, $itemid, $filepath, $filename);
84 return $this->get_file_info_context_user($context, $component, $filearea, $itemid, $filepath, $filename);
85 case CONTEXT_COURSECAT
:
86 return $this->get_file_info_context_coursecat($context, $component, $filearea, $itemid, $filepath, $filename);
88 return $this->get_file_info_context_course($context, $component, $filearea, $itemid, $filepath, $filename);
90 return $this->get_file_info_context_module($context, $component, $filearea, $itemid, $filepath, $filename);
97 * Returns info about the files at System context
98 * @param object $context
99 * @param string $component
100 * @param string $filearea
102 * @param string $filepath
103 * @param string $filename
104 * @return file_info instance or null if not found or access not allowed
106 private function get_file_info_context_system($context, $component, $filearea, $itemid, $filepath, $filename) {
107 $level = new file_info_context_system($this, $context);
108 return $level->get_file_info($component, $filearea, $itemid, $filepath, $filename);
109 // nothing supported at this context yet
113 * Returns info about the files at User context
114 * @param object $context
115 * @param string $component
116 * @param string $filearea
118 * @param string $filepath
119 * @param string $filename
120 * @return file_info instance or null if not found or access not allowed
122 private function get_file_info_context_user($context, $component, $filearea, $itemid, $filepath, $filename) {
124 if ($context->instanceid
== $USER->id
) {
127 $user = $DB->get_record('user', array('id'=>$context->instanceid
));
130 if (isguestuser($user)) {
131 // guests do not have any files
135 if ($user->deleted
) {
139 $level = new file_info_context_user($this, $context, $user);
140 return $level->get_file_info($component, $filearea, $itemid, $filepath, $filename);
144 * Returns info about the files at Course category context
145 * @param object $context
146 * @param string $component
147 * @param string $filearea
149 * @param string $filepath
150 * @param string $filename
151 * @return file_info instance or null if not found or access not allowed
153 private function get_file_info_context_coursecat($context, $component, $filearea, $itemid, $filepath, $filename) {
156 if (!$category = $DB->get_record('course_categories', array('id'=>$context->instanceid
))) {
160 $level = new file_info_context_coursecat($this, $context, $category);
161 return $level->get_file_info($component, $filearea, $itemid, $filepath, $filename);
165 * Returns info about the files at Course category context
166 * @param object $context
167 * @param string $component
168 * @param string $filearea
170 * @param string $filepath
171 * @param string $filename
172 * @return file_info instance or null if not found or access not allowed
174 private function get_file_info_context_course($context, $component, $filearea, $itemid, $filepath, $filename) {
177 if ($context->instanceid
== $COURSE->id
) {
179 } else if (!$course = $DB->get_record('course', array('id'=>$context->instanceid
))) {
183 $level = new file_info_context_course($this, $context, $course);
184 return $level->get_file_info($component, $filearea, $itemid, $filepath, $filename);
188 * Returns info about the files at Course category context
189 * @param object $context
190 * @param string $component
191 * @param string $filearea
193 * @param string $filepath
194 * @param string $filename
195 * @return file_info instance or null if not found or access not allowed
197 private function get_file_info_context_module($context, $component, $filearea, $itemid, $filepath, $filename) {
198 global $COURSE, $DB, $CFG;
201 if (!$cm = get_coursemodule_from_id('', $context->instanceid
)) {
205 if ($cm->course
== $COURSE->id
) {
207 } else if (!$course = $DB->get_record('course', array('id'=>$cm->course
))) {
211 $modinfo = get_fast_modinfo($course);
212 if (empty($modinfo->cms
[$cm->id
]->uservisible
)) {
216 $modname = $modinfo->cms
[$cm->id
]->modname
;
218 if (!file_exists("$CFG->dirroot/mod/$modname/lib.php")) {
222 // ok, we know that module exists, and user may access it
224 $level = new file_info_context_module($this, $context, $course, $cm, $modname);
225 return $level->get_file_info($component, $filearea, $itemid, $filepath, $filename);