Merge branch 'MDL-29276' of git://github.com/mouneyrac/moodle
[moodle.git] / lib / filebrowser / file_browser.php
blob298811a0d084c2c876cf8db6cf2bd0c299bc2f5d
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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/>.
19 /**
20 * Utility class for browsing of files.
22 * @package core
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");
32 // general area types
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");
43 /**
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.
59 * @package core
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
64 class file_browser {
66 /**
67 * Looks up file_info instance
68 * @param object $context
69 * @param string $component
70 * @param string $filearea
71 * @param int $itemid
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) {
77 if (!$context) {
78 $context = get_context_instance(CONTEXT_SYSTEM);
80 switch ($context->contextlevel) {
81 case CONTEXT_SYSTEM:
82 return $this->get_file_info_context_system($context, $component, $filearea, $itemid, $filepath, $filename);
83 case CONTEXT_USER:
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);
87 case CONTEXT_COURSE:
88 return $this->get_file_info_context_course($context, $component, $filearea, $itemid, $filepath, $filename);
89 case CONTEXT_MODULE:
90 return $this->get_file_info_context_module($context, $component, $filearea, $itemid, $filepath, $filename);
93 return null;
96 /**
97 * Returns info about the files at System context
98 * @param object $context
99 * @param string $component
100 * @param string $filearea
101 * @param int $itemid
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
117 * @param int $itemid
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) {
123 global $DB, $USER;
124 if ($context->instanceid == $USER->id) {
125 $user = $USER;
126 } else {
127 $user = $DB->get_record('user', array('id'=>$context->instanceid));
130 if (isguestuser($user)) {
131 // guests do not have any files
132 return null;
135 if ($user->deleted) {
136 return null;
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
148 * @param int $itemid
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) {
154 global $DB;
156 if (!$category = $DB->get_record('course_categories', array('id'=>$context->instanceid))) {
157 return null;
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
169 * @param int $itemid
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) {
175 global $DB, $COURSE;
177 if ($context->instanceid == $COURSE->id) {
178 $course = $COURSE;
179 } else if (!$course = $DB->get_record('course', array('id'=>$context->instanceid))) {
180 return null;
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
192 * @param int $itemid
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)) {
202 return null;
205 if ($cm->course == $COURSE->id) {
206 $course = $COURSE;
207 } else if (!$course = $DB->get_record('course', array('id'=>$cm->course))) {
208 return null;
211 $modinfo = get_fast_modinfo($course);
212 if (empty($modinfo->cms[$cm->id]->uservisible)) {
213 return null;
216 $modname = $modinfo->cms[$cm->id]->modname;
218 if (!file_exists("$CFG->dirroot/mod/$modname/lib.php")) {
219 return null;
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);