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 course 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();
31 * Represents a course context in the tree navigated by @see{file_browser}.
34 * @subpackage filebrowser
35 * @copyright 2008 Petr Skoda (http://skodak.org)
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class file_info_context_course extends file_info {
41 public function __construct($browser, $context, $course) {
42 parent::__construct($browser, $context);
43 $this->course = $course;
47 * Return information about this specific context level
55 public function get_file_info($component, $filearea, $itemid, $filepath, $filename) {
58 if (!$this->course->visible and !has_capability('moodle/course:viewhiddencourses', $this->context)) {
62 if (empty($component)) {
66 $methodname = "get_area_{$component}_{$filearea}";
68 if (method_exists($this, $methodname)) {
69 return $this->$methodname($itemid, $filepath, $filename);
75 protected function get_area_course_summary($itemid, $filepath, $filename) {
78 if (!has_capability('moodle/course:update', $this->context)) {
81 if (is_null($itemid)) {
85 $fs = get_file_storage();
87 $filepath = is_null($filepath) ? '/' : $filepath;
88 $filename = is_null($filename) ? '.' : $filename;
89 if (!$storedfile = $fs->get_file($this->context->id, 'course', 'summary', 0, $filepath, $filename)) {
90 if ($filepath === '/' and $filename === '.') {
91 $storedfile = new virtual_root_file($this->context->id, 'course', 'summary', 0);
97 $urlbase = $CFG->wwwroot.'/pluginfile.php';
98 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('areacourseintro', 'repository'), false, true, true, false);
102 protected function get_area_course_section($itemid, $filepath, $filename) {
105 if (!has_capability('moodle/course:update', $this->context)) {
109 if (empty($itemid)) {
111 return new file_info_area_course_section($this->browser, $this->context, $this->course, $this);
114 if (!$section = $DB->get_record('course_sections', array('course'=>$this->course->id, 'id'=>$itemid))) {
115 return null; // does not exist
118 $fs = get_file_storage();
120 $filepath = is_null($filepath) ? '/' : $filepath;
121 $filename = is_null($filename) ? '.' : $filename;
122 if (!$storedfile = $fs->get_file($this->context->id, 'course', 'section', $itemid, $filepath, $filename)) {
123 if ($filepath === '/' and $filename === '.') {
124 $storedfile = new virtual_root_file($this->context->id, 'course', 'section', $itemid);
130 $urlbase = $CFG->wwwroot.'/pluginfile.php';
131 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, $section->section, true, true, true, false);
135 protected function get_area_course_legacy($itemid, $filepath, $filename) {
136 if (!has_capability('moodle/course:managefiles', $this->context)) {
140 if ($this->course->id != SITEID and $this->course->legacyfiles != 2) {
141 // bad luck, legacy course files not used any more
144 if (is_null($itemid)) {
148 $fs = get_file_storage();
150 $filepath = is_null($filepath) ? '/' : $filepath;
151 $filename = is_null($filename) ? '.' : $filename;
152 if (!$storedfile = $fs->get_file($this->context->id, 'course', 'legacy', 0, $filepath, $filename)) {
153 if ($filepath === '/' and $filename === '.') {
154 $storedfile = new virtual_root_file($this->context->id, 'course', 'legacy', 0);
161 return new file_info_area_course_legacy($this->browser, $this->context, $storedfile);
164 protected function get_area_backup_course($itemid, $filepath, $filename) {
167 if (!has_capability('moodle/backup:backupcourse', $this->context) and !has_capability('moodle/restore:restorecourse', $this->context)) {
170 if (is_null($itemid)) {
174 $fs = get_file_storage();
176 $filepath = is_null($filepath) ? '/' : $filepath;
177 $filename = is_null($filename) ? '.' : $filename;
178 if (!$storedfile = $fs->get_file($this->context->id, 'backup', 'course', 0, $filepath, $filename)) {
179 if ($filepath === '/' and $filename === '.') {
180 $storedfile = new virtual_root_file($this->context->id, 'backup', 'course', 0);
187 $downloadable = has_capability('moodle/backup:downloadfile', $this->context);
188 $uploadable = has_capability('moodle/restore:uploadfile', $this->context);
190 $urlbase = $CFG->wwwroot.'/pluginfile.php';
191 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('coursebackup', 'repository'), false, $downloadable, $uploadable, false);
194 protected function get_area_backup_section($itemid, $filepath, $filename) {
197 if (!has_capability('moodle/backup:backupcourse', $this->context) and !has_capability('moodle/restore:restorecourse', $this->context)) {
201 if (empty($itemid)) {
203 return new file_info_area_backup_section($this->browser, $this->context, $this->course, $this);
206 if (!$section = $DB->get_record('course_sections', array('course'=>$this->course->id, 'id'=>$itemid))) {
207 return null; // does not exist
210 $fs = get_file_storage();
212 $filepath = is_null($filepath) ? '/' : $filepath;
213 $filename = is_null($filename) ? '.' : $filename;
214 if (!$storedfile = $fs->get_file($this->context->id, 'backup', 'section', $itemid, $filepath, $filename)) {
215 if ($filepath === '/' and $filename === '.') {
216 $storedfile = new virtual_root_file($this->context->id, 'backup', 'section', $itemid);
223 $downloadable = has_capability('moodle/backup:downloadfile', $this->context);
224 $uploadable = has_capability('moodle/restore:uploadfile', $this->context);
226 $urlbase = $CFG->wwwroot.'/pluginfile.php';
227 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, $section->id, true, $downloadable, $uploadable, false);
230 public function get_visible_name() {
231 return ($this->course->id == SITEID) ? get_string('frontpage', 'admin') : format_string($this->course->fullname, true, array('context'=>$this->context));
235 * Can I add new files or directories?
238 public function is_writable() {
246 public function is_directory() {
251 * Returns list of children.
252 * @return array of file_info instances
254 public function get_children() {
257 if ($child = $this->get_area_course_summary(0, '/', '.')) {
258 $children[] = $child;
260 if ($child = $this->get_area_course_section(null, null, null)) {
261 $children[] = $child;
263 if ($child = $this->get_area_backup_section(null, null, null)) {
264 $children[] = $child;
266 if ($child = $this->get_area_backup_course(0, '/', '.')) {
267 $children[] = $child;
269 if ($child = $this->get_area_course_legacy(0, '/', '.')) {
270 $children[] = $child;
273 // now list all modules
274 $modinfo = get_fast_modinfo($this->course);
275 foreach ($modinfo->cms as $cminfo) {
276 if (empty($cminfo->uservisible)) {
279 $modcontext = get_context_instance(CONTEXT_MODULE, $cminfo->id);
280 if ($child = $this->browser->get_file_info($modcontext)) {
281 $children[] = $child;
289 * Returns parent file_info instance
290 * @return file_info or null for root
292 public function get_parent() {
293 //TODO: error checking if get_parent_contextid() returns false
294 $pcid = get_parent_contextid($this->context);
295 $parent = get_context_instance_by_id($pcid);
296 return $this->browser->get_file_info($parent);
302 * Subclass of file_info_stored for files in the course files area.
305 * @subpackage filebrowser
306 * @copyright 2008 Petr Skoda (http://skodak.org)
307 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
309 class file_info_area_course_legacy extends file_info_stored {
310 public function __construct($browser, $context, $storedfile) {
312 $urlbase = $CFG->wwwroot.'/file.php';
313 parent::__construct($browser, $context, $storedfile, $urlbase, get_string('coursefiles'), false, true, true, false);
317 * Returns file download url
318 * @param bool $forcedownload
319 * @param bool $htts force https
322 public function get_url($forcedownload=false, $https=false) {
325 if (!$this->is_readable()) {
329 if ($this->lf->is_directory()) {
333 $filepath = $this->lf->get_filepath();
334 $filename = $this->lf->get_filename();
335 $courseid = $this->context->instanceid;
337 $path = '/'.$courseid.$filepath.$filename;
339 return file_encode_url($this->urlbase, $path, $forcedownload, $https);
343 * Returns list of children.
344 * @return array of file_info instances
346 public function get_children() {
347 if (!$this->lf->is_directory()) {
352 $fs = get_file_storage();
354 $storedfiles = $fs->get_directory_files($this->context->id, 'course', 'legacy', 0, $this->lf->get_filepath(), false, true, "filepath ASC, filename ASC");
355 foreach ($storedfiles as $file) {
356 $result[] = new file_info_area_course_legacy($this->browser, $this->context, $file);
364 * Represents a course category context in the tree navigated by @see{file_browser}.
367 * @subpackage filebrowser
368 * @copyright 2008 Petr Skoda (http://skodak.org)
369 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
371 class file_info_area_course_section extends file_info {
373 protected $courseinfo;
375 public function __construct($browser, $context, $course, file_info_context_course $courseinfo) {
376 parent::__construct($browser, $context);
377 $this->course = $course;
378 $this->courseinfo = $courseinfo;
382 * Returns list of standard virtual file/directory identification.
383 * The difference from stored_file parameters is that null values
384 * are allowed in all fields
385 * @return array with keys contextid, filearea, itemid, filepath and filename
387 public function get_params() {
388 return array('contextid' => $this->context->id,
389 'component' => 'course',
390 'filearea' => 'section',
397 * Returns localised visible name.
400 public function get_visible_name() {
401 $format = $this->course->format;
402 $sectionsname = get_string("coursesectionsummaries");
404 return $sectionsname;
408 * Can I add new files or directories?
411 public function is_writable() {
419 public function is_directory() {
424 * Returns list of children.
425 * @return array of file_info instances
427 public function get_children() {
432 $course_sections = $DB->get_records('course_sections', array('course'=>$this->course->id), 'section');
433 foreach ($course_sections as $section) {
434 if ($child = $this->courseinfo->get_file_info('course', 'section', $section->id, '/', '.')) {
435 $children[] = $child;
443 * Returns parent file_info instance
444 * @return file_info or null for root
446 public function get_parent() {
447 return $this->courseinfo;
453 * Implementation of course section backup area
456 * @subpackage filebrowser
457 * @copyright 2008 Petr Skoda (http://skodak.org)
458 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
460 class file_info_area_backup_section extends file_info {
462 protected $courseinfo;
464 public function __construct($browser, $context, $course, file_info_context_course $courseinfo) {
465 parent::__construct($browser, $context);
466 $this->course = $course;
467 $this->courseinfo = $courseinfo;
471 * Returns list of standard virtual file/directory identification.
472 * The difference from stored_file parameters is that null values
473 * are allowed in all fields
474 * @return array with keys contextid, component, filearea, itemid, filepath and filename
476 public function get_params() {
477 return array('contextid' => $this->context->id,
478 'component' => 'backup',
479 'filearea' => 'section',
486 * Returns localised visible name.
489 public function get_visible_name() {
490 $format = $this->course->format;
491 $sectionsname = get_string('sectionbackup', 'repository');
493 return $sectionsname;
497 * Can I add new files or directories?
500 public function is_writable() {
508 public function is_directory() {
513 * Returns list of children.
514 * @return array of file_info instances
516 public function get_children() {
521 $course_sections = $DB->get_records('course_sections', array('course'=>$this->course->id), 'section');
522 foreach ($course_sections as $section) {
523 if ($child = $this->courseinfo->get_file_info('backup', 'section', $section->id, '/', '.')) {
524 $children[] = $child;
532 * Returns parent file_info instance
533 * @return file_info or null for root
535 public function get_parent() {
536 return $this->browser->get_file_info($this->context);