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) {
56 // try to emulate require_login() tests here
61 if (!$this->course
->visible
and !has_capability('moodle/course:viewhiddencourses', $this->context
)) {
65 if (!is_viewing($this->context
) and !is_enrolled($this->context
)) {
66 // no peaking here if not enrolled or inspector
70 if (empty($component)) {
74 $methodname = "get_area_{$component}_{$filearea}";
76 if (method_exists($this, $methodname)) {
77 return $this->$methodname($itemid, $filepath, $filename);
83 protected function get_area_course_summary($itemid, $filepath, $filename) {
86 if (!has_capability('moodle/course:update', $this->context
)) {
89 if (is_null($itemid)) {
93 $fs = get_file_storage();
95 $filepath = is_null($filepath) ?
'/' : $filepath;
96 $filename = is_null($filename) ?
'.' : $filename;
97 if (!$storedfile = $fs->get_file($this->context
->id
, 'course', 'summary', 0, $filepath, $filename)) {
98 if ($filepath === '/' and $filename === '.') {
99 $storedfile = new virtual_root_file($this->context
->id
, 'course', 'summary', 0);
105 $urlbase = $CFG->wwwroot
.'/pluginfile.php';
106 return new file_info_stored($this->browser
, $this->context
, $storedfile, $urlbase, get_string('areacourseintro', 'repository'), false, true, true, false);
110 protected function get_area_course_section($itemid, $filepath, $filename) {
113 if (!has_capability('moodle/course:update', $this->context
)) {
117 if (empty($itemid)) {
119 return new file_info_area_course_section($this->browser
, $this->context
, $this->course
, $this);
122 if (!$section = $DB->get_record('course_sections', array('course'=>$this->course
->id
, 'id'=>$itemid))) {
123 return null; // does not exist
126 $fs = get_file_storage();
128 $filepath = is_null($filepath) ?
'/' : $filepath;
129 $filename = is_null($filename) ?
'.' : $filename;
130 if (!$storedfile = $fs->get_file($this->context
->id
, 'course', 'section', $itemid, $filepath, $filename)) {
131 if ($filepath === '/' and $filename === '.') {
132 $storedfile = new virtual_root_file($this->context
->id
, 'course', 'section', $itemid);
138 $urlbase = $CFG->wwwroot
.'/pluginfile.php';
139 return new file_info_stored($this->browser
, $this->context
, $storedfile, $urlbase, $section->section
, true, true, true, false);
143 protected function get_area_course_legacy($itemid, $filepath, $filename) {
144 if (!has_capability('moodle/course:managefiles', $this->context
)) {
148 if ($this->course
->id
!= SITEID
and $this->course
->legacyfiles
!= 2) {
149 // bad luck, legacy course files not used any more
152 if (is_null($itemid)) {
156 $fs = get_file_storage();
158 $filepath = is_null($filepath) ?
'/' : $filepath;
159 $filename = is_null($filename) ?
'.' : $filename;
160 if (!$storedfile = $fs->get_file($this->context
->id
, 'course', 'legacy', 0, $filepath, $filename)) {
161 if ($filepath === '/' and $filename === '.') {
162 $storedfile = new virtual_root_file($this->context
->id
, 'course', 'legacy', 0);
169 return new file_info_area_course_legacy($this->browser
, $this->context
, $storedfile);
172 protected function get_area_backup_course($itemid, $filepath, $filename) {
175 if (!has_capability('moodle/backup:backupcourse', $this->context
) and !has_capability('moodle/restore:restorecourse', $this->context
)) {
178 if (is_null($itemid)) {
182 $fs = get_file_storage();
184 $filepath = is_null($filepath) ?
'/' : $filepath;
185 $filename = is_null($filename) ?
'.' : $filename;
186 if (!$storedfile = $fs->get_file($this->context
->id
, 'backup', 'course', 0, $filepath, $filename)) {
187 if ($filepath === '/' and $filename === '.') {
188 $storedfile = new virtual_root_file($this->context
->id
, 'backup', 'course', 0);
195 $downloadable = has_capability('moodle/backup:downloadfile', $this->context
);
196 $uploadable = has_capability('moodle/restore:uploadfile', $this->context
);
198 $urlbase = $CFG->wwwroot
.'/pluginfile.php';
199 return new file_info_stored($this->browser
, $this->context
, $storedfile, $urlbase, get_string('coursebackup', 'repository'), false, $downloadable, $uploadable, false);
203 * Gets a stored file for the automated backup filearea directory
206 * @param string $filepath
207 * @param string $filename
208 * @return file_info_context_course
210 protected function get_area_backup_automated($itemid, $filepath, $filename) {
213 if (!has_capability('moodle/restore:viewautomatedfilearea', $this->context
)) {
216 if (is_null($itemid)) {
220 $fs = get_file_storage();
222 $filepath = is_null($filepath) ?
'/' : $filepath;
223 $filename = is_null($filename) ?
'.' : $filename;
224 if (!$storedfile = $fs->get_file($this->context
->id
, 'backup', 'automated', 0, $filepath, $filename)) {
225 if ($filepath === '/' and $filename === '.') {
226 $storedfile = new virtual_root_file($this->context
->id
, 'backup', 'automated', 0);
233 $downloadable = has_capability('moodle/site:config', $this->context
);
236 $urlbase = $CFG->wwwroot
.'/pluginfile.php';
237 return new file_info_stored($this->browser
, $this->context
, $storedfile, $urlbase, get_string('automatedbackup', 'repository'), true, $downloadable, $uploadable, false);
240 protected function get_area_backup_section($itemid, $filepath, $filename) {
243 if (!has_capability('moodle/backup:backupcourse', $this->context
) and !has_capability('moodle/restore:restorecourse', $this->context
)) {
247 if (empty($itemid)) {
249 return new file_info_area_backup_section($this->browser
, $this->context
, $this->course
, $this);
252 if (!$section = $DB->get_record('course_sections', array('course'=>$this->course
->id
, 'id'=>$itemid))) {
253 return null; // does not exist
256 $fs = get_file_storage();
258 $filepath = is_null($filepath) ?
'/' : $filepath;
259 $filename = is_null($filename) ?
'.' : $filename;
260 if (!$storedfile = $fs->get_file($this->context
->id
, 'backup', 'section', $itemid, $filepath, $filename)) {
261 if ($filepath === '/' and $filename === '.') {
262 $storedfile = new virtual_root_file($this->context
->id
, 'backup', 'section', $itemid);
269 $downloadable = has_capability('moodle/backup:downloadfile', $this->context
);
270 $uploadable = has_capability('moodle/restore:uploadfile', $this->context
);
272 $urlbase = $CFG->wwwroot
.'/pluginfile.php';
273 return new file_info_stored($this->browser
, $this->context
, $storedfile, $urlbase, $section->id
, true, $downloadable, $uploadable, false);
276 public function get_visible_name() {
277 return ($this->course
->id
== SITEID
) ?
get_string('frontpage', 'admin') : format_string($this->course
->fullname
, true, array('context'=>$this->context
));
281 * Can I add new files or directories?
284 public function is_writable() {
292 public function is_directory() {
297 * Returns list of children.
298 * @return array of file_info instances
300 public function get_children() {
303 if ($child = $this->get_area_course_summary(0, '/', '.')) {
304 $children[] = $child;
306 if ($child = $this->get_area_course_section(null, null, null)) {
307 $children[] = $child;
309 if ($child = $this->get_area_backup_section(null, null, null)) {
310 $children[] = $child;
312 if ($child = $this->get_area_backup_course(0, '/', '.')) {
313 $children[] = $child;
315 if ($child = $this->get_area_backup_automated(0, '/', '.')) {
316 $children[] = $child;
318 if ($child = $this->get_area_course_legacy(0, '/', '.')) {
319 $children[] = $child;
322 // now list all modules
323 $modinfo = get_fast_modinfo($this->course
);
324 foreach ($modinfo->cms
as $cminfo) {
325 if (empty($cminfo->uservisible
)) {
328 $modcontext = get_context_instance(CONTEXT_MODULE
, $cminfo->id
);
329 if ($child = $this->browser
->get_file_info($modcontext)) {
330 $children[] = $child;
338 * Returns parent file_info instance
339 * @return file_info or null for root
341 public function get_parent() {
342 //TODO: error checking if get_parent_contextid() returns false
343 $pcid = get_parent_contextid($this->context
);
344 $parent = get_context_instance_by_id($pcid);
345 return $this->browser
->get_file_info($parent);
351 * Subclass of file_info_stored for files in the course files area.
354 * @subpackage filebrowser
355 * @copyright 2008 Petr Skoda (http://skodak.org)
356 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
358 class file_info_area_course_legacy
extends file_info_stored
{
359 public function __construct($browser, $context, $storedfile) {
361 $urlbase = $CFG->wwwroot
.'/file.php';
362 parent
::__construct($browser, $context, $storedfile, $urlbase, get_string('coursefiles'), false, true, true, false);
366 * Returns file download url
367 * @param bool $forcedownload
368 * @param bool $htts force https
371 public function get_url($forcedownload=false, $https=false) {
372 if (!$this->is_readable()) {
376 if ($this->lf
->is_directory()) {
380 $filepath = $this->lf
->get_filepath();
381 $filename = $this->lf
->get_filename();
382 $courseid = $this->context
->instanceid
;
384 $path = '/'.$courseid.$filepath.$filename;
386 return file_encode_url($this->urlbase
, $path, $forcedownload, $https);
390 * Returns list of children.
391 * @return array of file_info instances
393 public function get_children() {
394 if (!$this->lf
->is_directory()) {
399 $fs = get_file_storage();
401 $storedfiles = $fs->get_directory_files($this->context
->id
, 'course', 'legacy', 0, $this->lf
->get_filepath(), false, true, "filepath ASC, filename ASC");
402 foreach ($storedfiles as $file) {
403 $result[] = new file_info_area_course_legacy($this->browser
, $this->context
, $file);
411 * Represents a course category context in the tree navigated by @see{file_browser}.
414 * @subpackage filebrowser
415 * @copyright 2008 Petr Skoda (http://skodak.org)
416 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
418 class file_info_area_course_section
extends file_info
{
420 protected $courseinfo;
422 public function __construct($browser, $context, $course, file_info_context_course
$courseinfo) {
423 parent
::__construct($browser, $context);
424 $this->course
= $course;
425 $this->courseinfo
= $courseinfo;
429 * Returns list of standard virtual file/directory identification.
430 * The difference from stored_file parameters is that null values
431 * are allowed in all fields
432 * @return array with keys contextid, filearea, itemid, filepath and filename
434 public function get_params() {
435 return array('contextid' => $this->context
->id
,
436 'component' => 'course',
437 'filearea' => 'section',
444 * Returns localised visible name.
447 public function get_visible_name() {
448 //$format = $this->course->format;
449 $sectionsname = get_string("coursesectionsummaries");
451 return $sectionsname;
455 * Can I add new files or directories?
458 public function is_writable() {
463 * Is this empty area?
467 public function is_empty_area() {
468 $fs = get_file_storage();
469 return $fs->is_area_empty($this->context
->id
, 'course', 'section');
476 public function is_directory() {
481 * Returns list of children.
482 * @return array of file_info instances
484 public function get_children() {
489 $course_sections = $DB->get_records('course_sections', array('course'=>$this->course
->id
), 'section');
490 foreach ($course_sections as $section) {
491 if ($child = $this->courseinfo
->get_file_info('course', 'section', $section->id
, '/', '.')) {
492 $children[] = $child;
500 * Returns parent file_info instance
501 * @return file_info or null for root
503 public function get_parent() {
504 return $this->courseinfo
;
510 * Implementation of course section backup area
513 * @subpackage filebrowser
514 * @copyright 2008 Petr Skoda (http://skodak.org)
515 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
517 class file_info_area_backup_section
extends file_info
{
519 protected $courseinfo;
521 public function __construct($browser, $context, $course, file_info_context_course
$courseinfo) {
522 parent
::__construct($browser, $context);
523 $this->course
= $course;
524 $this->courseinfo
= $courseinfo;
528 * Returns list of standard virtual file/directory identification.
529 * The difference from stored_file parameters is that null values
530 * are allowed in all fields
531 * @return array with keys contextid, component, filearea, itemid, filepath and filename
533 public function get_params() {
534 return array('contextid' => $this->context
->id
,
535 'component' => 'backup',
536 'filearea' => 'section',
543 * Returns localised visible name.
546 public function get_visible_name() {
547 return get_string('sectionbackup', 'repository');
551 * Can I add new files or directories?
554 public function is_writable() {
559 * Is this empty area?
563 public function is_empty_area() {
564 $fs = get_file_storage();
565 return $fs->is_area_empty($this->context
->id
, 'backup', 'section');
572 public function is_directory() {
577 * Returns list of children.
578 * @return array of file_info instances
580 public function get_children() {
585 $course_sections = $DB->get_records('course_sections', array('course'=>$this->course
->id
), 'section');
586 foreach ($course_sections as $section) {
587 if ($child = $this->courseinfo
->get_file_info('backup', 'section', $section->id
, '/', '.')) {
588 $children[] = $child;
596 * Returns parent file_info instance
597 * @return file_info or null for root
599 public function get_parent() {
600 return $this->browser
->get_file_info($this->context
);