MDL-30995 Completion Fixedup some more PHP DOC issues
[moodle.git] / lib / filebrowser / file_info_context_course.php
blob658daa5e41c03e53940dd64c469497767cf46fea
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Utility class for browsing of course files.
21 * @package core_files
22 * @copyright 2008 Petr Skoda (http://skodak.org)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 /**
29 * Represents a course context in the tree navigated by {@link file_browser}.
31 * @package core_files
32 * @copyright 2008 Petr Skoda (http://skodak.org)
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class file_info_context_course extends file_info {
36 /** @var stdClass course object */
37 protected $course;
39 /**
40 * Constructor
42 * @param file_browser $browser file browser instance
43 * @param stdClass $context context object
44 * @param stdClass $course course object
46 public function __construct($browser, $context, $course) {
47 parent::__construct($browser, $context);
48 $this->course = $course;
51 /**
52 * Return information about this specific context level
54 * @param string $component component
55 * @param string $filearea file area
56 * @param int $itemid item ID
57 * @param string $filepath file path
58 * @param string $filename file name
59 * @return file_info|null file_info instance or null if not found or access not allowed
61 public function get_file_info($component, $filearea, $itemid, $filepath, $filename) {
62 // try to emulate require_login() tests here
63 if (!isloggedin()) {
64 return null;
67 if (!$this->course->visible and !has_capability('moodle/course:viewhiddencourses', $this->context)) {
68 return null;
71 if (!is_viewing($this->context) and !is_enrolled($this->context)) {
72 // no peaking here if not enrolled or inspector
73 return null;
76 if (empty($component)) {
77 return $this;
80 $methodname = "get_area_{$component}_{$filearea}";
82 if (method_exists($this, $methodname)) {
83 return $this->$methodname($itemid, $filepath, $filename);
86 return null;
89 /**
90 * Gets a stored file for the course summary filearea directory
92 * @param int $itemid item ID
93 * @param string $filepath file path
94 * @param string $filename file name
95 * @return file_info|null file_info instance or null if not found or access not allowed
97 protected function get_area_course_summary($itemid, $filepath, $filename) {
98 global $CFG;
100 if (!has_capability('moodle/course:update', $this->context)) {
101 return null;
103 if (is_null($itemid)) {
104 return $this;
107 $fs = get_file_storage();
109 $filepath = is_null($filepath) ? '/' : $filepath;
110 $filename = is_null($filename) ? '.' : $filename;
111 if (!$storedfile = $fs->get_file($this->context->id, 'course', 'summary', 0, $filepath, $filename)) {
112 if ($filepath === '/' and $filename === '.') {
113 $storedfile = new virtual_root_file($this->context->id, 'course', 'summary', 0);
114 } else {
115 // not found
116 return null;
119 $urlbase = $CFG->wwwroot.'/pluginfile.php';
120 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('areacourseintro', 'repository'), false, true, true, false);
124 * Gets a stored file for the course section filearea directory
126 * @param int $itemid item ID
127 * @param string $filepath file path
128 * @param string $filename file name
129 * @return file_info|null file_info instance or null if not found or access not allowed
131 protected function get_area_course_section($itemid, $filepath, $filename) {
132 global $CFG, $DB;
134 if (!has_capability('moodle/course:update', $this->context)) {
135 return null;
138 if (empty($itemid)) {
139 // list all sections
140 return new file_info_area_course_section($this->browser, $this->context, $this->course, $this);
143 if (!$section = $DB->get_record('course_sections', array('course'=>$this->course->id, 'id'=>$itemid))) {
144 return null; // does not exist
147 $fs = get_file_storage();
149 $filepath = is_null($filepath) ? '/' : $filepath;
150 $filename = is_null($filename) ? '.' : $filename;
151 if (!$storedfile = $fs->get_file($this->context->id, 'course', 'section', $itemid, $filepath, $filename)) {
152 if ($filepath === '/' and $filename === '.') {
153 $storedfile = new virtual_root_file($this->context->id, 'course', 'section', $itemid);
154 } else {
155 // not found
156 return null;
159 $urlbase = $CFG->wwwroot.'/pluginfile.php';
160 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, $section->section, true, true, true, false);
164 * Gets a stored file for the course legacy filearea directory
166 * @param int $itemid item ID
167 * @param string $filepath file path
168 * @param string $filename file name
169 * @return file_info|null file_info instance or null if not found or access not allowed
171 protected function get_area_course_legacy($itemid, $filepath, $filename) {
172 if (!has_capability('moodle/course:managefiles', $this->context)) {
173 return null;
176 if ($this->course->id != SITEID and $this->course->legacyfiles != 2) {
177 // bad luck, legacy course files not used any more
180 if (is_null($itemid)) {
181 return $this;
184 $fs = get_file_storage();
186 $filepath = is_null($filepath) ? '/' : $filepath;
187 $filename = is_null($filename) ? '.' : $filename;
188 if (!$storedfile = $fs->get_file($this->context->id, 'course', 'legacy', 0, $filepath, $filename)) {
189 if ($filepath === '/' and $filename === '.') {
190 $storedfile = new virtual_root_file($this->context->id, 'course', 'legacy', 0);
191 } else {
192 // not found
193 return null;
197 return new file_info_area_course_legacy($this->browser, $this->context, $storedfile);
201 * Gets a stored file for the backup course filearea directory
203 * @param int $itemid item ID
204 * @param string $filepath file path
205 * @param string $filename file name
206 * @return file_info|null file_info instance or null if not found or access not allowed
208 protected function get_area_backup_course($itemid, $filepath, $filename) {
209 global $CFG;
211 if (!has_capability('moodle/backup:backupcourse', $this->context) and !has_capability('moodle/restore:restorecourse', $this->context)) {
212 return null;
214 if (is_null($itemid)) {
215 return $this;
218 $fs = get_file_storage();
220 $filepath = is_null($filepath) ? '/' : $filepath;
221 $filename = is_null($filename) ? '.' : $filename;
222 if (!$storedfile = $fs->get_file($this->context->id, 'backup', 'course', 0, $filepath, $filename)) {
223 if ($filepath === '/' and $filename === '.') {
224 $storedfile = new virtual_root_file($this->context->id, 'backup', 'course', 0);
225 } else {
226 // not found
227 return null;
231 $downloadable = has_capability('moodle/backup:downloadfile', $this->context);
232 $uploadable = has_capability('moodle/restore:uploadfile', $this->context);
234 $urlbase = $CFG->wwwroot.'/pluginfile.php';
235 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('coursebackup', 'repository'), false, $downloadable, $uploadable, false);
239 * Gets a stored file for the automated backup filearea directory
241 * @param int $itemid item ID
242 * @param string $filepath file path
243 * @param string $filename file name
244 * @return file_info|null
246 protected function get_area_backup_automated($itemid, $filepath, $filename) {
247 global $CFG;
249 if (!has_capability('moodle/restore:viewautomatedfilearea', $this->context)) {
250 return null;
252 if (is_null($itemid)) {
253 return $this;
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', 'automated', 0, $filepath, $filename)) {
261 if ($filepath === '/' and $filename === '.') {
262 $storedfile = new virtual_root_file($this->context->id, 'backup', 'automated', 0);
263 } else {
264 // not found
265 return null;
269 $downloadable = has_capability('moodle/site:config', $this->context);
270 $uploadable = false;
272 $urlbase = $CFG->wwwroot.'/pluginfile.php';
273 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('automatedbackup', 'repository'), true, $downloadable, $uploadable, false);
277 * Gets a stored file for the backup section filearea directory
279 * @param int $itemid item ID
280 * @param string $filepath file path
281 * @param string $filename file name
282 * @return file_info|null file_info instance or null if not found or access not allowed
284 protected function get_area_backup_section($itemid, $filepath, $filename) {
285 global $CFG, $DB;
287 if (!has_capability('moodle/backup:backupcourse', $this->context) and !has_capability('moodle/restore:restorecourse', $this->context)) {
288 return null;
291 if (empty($itemid)) {
292 // list all sections
293 return new file_info_area_backup_section($this->browser, $this->context, $this->course, $this);
296 if (!$section = $DB->get_record('course_sections', array('course'=>$this->course->id, 'id'=>$itemid))) {
297 return null; // does not exist
300 $fs = get_file_storage();
302 $filepath = is_null($filepath) ? '/' : $filepath;
303 $filename = is_null($filename) ? '.' : $filename;
304 if (!$storedfile = $fs->get_file($this->context->id, 'backup', 'section', $itemid, $filepath, $filename)) {
305 if ($filepath === '/' and $filename === '.') {
306 $storedfile = new virtual_root_file($this->context->id, 'backup', 'section', $itemid);
307 } else {
308 // not found
309 return null;
313 $downloadable = has_capability('moodle/backup:downloadfile', $this->context);
314 $uploadable = has_capability('moodle/restore:uploadfile', $this->context);
316 $urlbase = $CFG->wwwroot.'/pluginfile.php';
317 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, $section->id, true, $downloadable, $uploadable, false);
321 * Returns localised visible name.
323 * @return string
325 public function get_visible_name() {
326 return ($this->course->id == SITEID) ? get_string('frontpage', 'admin') : format_string($this->course->fullname, true, array('context'=>$this->context));
330 * Whether or not new files or directories can be added
332 * @return bool
334 public function is_writable() {
335 return false;
339 * Whether or not this is a directory
341 * @return bool
343 public function is_directory() {
344 return true;
348 * Returns list of children.
350 * @return array of file_info instances
352 public function get_children() {
353 $children = array();
355 if ($child = $this->get_area_course_summary(0, '/', '.')) {
356 $children[] = $child;
358 if ($child = $this->get_area_course_section(null, null, null)) {
359 $children[] = $child;
361 if ($child = $this->get_area_backup_section(null, null, null)) {
362 $children[] = $child;
364 if ($child = $this->get_area_backup_course(0, '/', '.')) {
365 $children[] = $child;
367 if ($child = $this->get_area_backup_automated(0, '/', '.')) {
368 $children[] = $child;
370 if ($child = $this->get_area_course_legacy(0, '/', '.')) {
371 $children[] = $child;
374 // now list all modules
375 $modinfo = get_fast_modinfo($this->course);
376 foreach ($modinfo->cms as $cminfo) {
377 if (empty($cminfo->uservisible)) {
378 continue;
380 $modcontext = get_context_instance(CONTEXT_MODULE, $cminfo->id);
381 if ($child = $this->browser->get_file_info($modcontext)) {
382 $children[] = $child;
386 return $children;
390 * Returns parent file_info instance
392 * @todo error checking if get_parent_contextid() returns false
393 * @return file_info or null for root
395 public function get_parent() {
396 //TODO: error checking if get_parent_contextid() returns false
397 $pcid = get_parent_contextid($this->context);
398 $parent = get_context_instance_by_id($pcid);
399 return $this->browser->get_file_info($parent);
405 * Subclass of file_info_stored for files in the course files area.
407 * @package core_files
408 * @copyright 2008 Petr Skoda (http://skodak.org)
409 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
411 class file_info_area_course_legacy extends file_info_stored {
413 * Constructor
415 * @param file_browser $browser file browser instance
416 * @param stdClass $context context object
417 * @param stored_file $storedfile stored_file instance
419 public function __construct($browser, $context, $storedfile) {
420 global $CFG;
421 $urlbase = $CFG->wwwroot.'/file.php';
422 parent::__construct($browser, $context, $storedfile, $urlbase, get_string('coursefiles'), false, true, true, false);
426 * Returns file download url
428 * @param bool $forcedownload whether or not force download
429 * @param bool $https whether or not force https
430 * @return string url
432 public function get_url($forcedownload=false, $https=false) {
433 if (!$this->is_readable()) {
434 return null;
437 if ($this->lf->is_directory()) {
438 return null;
441 $filepath = $this->lf->get_filepath();
442 $filename = $this->lf->get_filename();
443 $courseid = $this->context->instanceid;
445 $path = '/'.$courseid.$filepath.$filename;
447 return file_encode_url($this->urlbase, $path, $forcedownload, $https);
451 * Returns list of children.
453 * @return array of file_info instances
455 public function get_children() {
456 if (!$this->lf->is_directory()) {
457 return array();
460 $result = array();
461 $fs = get_file_storage();
463 $storedfiles = $fs->get_directory_files($this->context->id, 'course', 'legacy', 0, $this->lf->get_filepath(), false, true, "filepath ASC, filename ASC");
464 foreach ($storedfiles as $file) {
465 $result[] = new file_info_area_course_legacy($this->browser, $this->context, $file);
468 return $result;
473 * Represents a course category context in the tree navigated by {@link file_browser}.
475 * @package core_files
476 * @copyright 2008 Petr Skoda (http://skodak.org)
477 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
479 class file_info_area_course_section extends file_info {
480 /** @var stdClass course object */
481 protected $course;
482 /** @var file_info_context_course course file info object */
483 protected $courseinfo;
486 * Constructor
488 * @param file_browser $browser file browser instance
489 * @param stdClass $context context object
490 * @param stdClass $course course object
491 * @param file_info_context_course $courseinfo file info instance
493 public function __construct($browser, $context, $course, file_info_context_course $courseinfo) {
494 parent::__construct($browser, $context);
495 $this->course = $course;
496 $this->courseinfo = $courseinfo;
500 * Returns list of standard virtual file/directory identification.
501 * The difference from stored_file parameters is that null values
502 * are allowed in all fields
504 * @return array with keys contextid, filearea, itemid, filepath and filename
506 public function get_params() {
507 return array('contextid' => $this->context->id,
508 'component' => 'course',
509 'filearea' => 'section',
510 'itemid' => null,
511 'filepath' => null,
512 'filename' => null);
516 * Returns localised visible name.
518 * @return string
520 public function get_visible_name() {
521 //$format = $this->course->format;
522 $sectionsname = get_string("coursesectionsummaries");
524 return $sectionsname;
528 * Return whether or not new files or directories can be added
530 * @return bool
532 public function is_writable() {
533 return false;
537 * Return whether or not this is a empty area
539 * @return bool
541 public function is_empty_area() {
542 $fs = get_file_storage();
543 return $fs->is_area_empty($this->context->id, 'course', 'section');
547 * Return whether or not this is a empty area
549 * @return bool
551 public function is_directory() {
552 return true;
556 * Returns list of children.
558 * @return array of file_info instances
560 public function get_children() {
561 global $DB;
563 $children = array();
565 $course_sections = $DB->get_records('course_sections', array('course'=>$this->course->id), 'section');
566 foreach ($course_sections as $section) {
567 if ($child = $this->courseinfo->get_file_info('course', 'section', $section->id, '/', '.')) {
568 $children[] = $child;
572 return $children;
576 * Returns parent file_info instance
578 * @return file_info|null file_info or null for root
580 public function get_parent() {
581 return $this->courseinfo;
587 * Implementation of course section backup area
589 * @package core_files
590 * @copyright 2008 Petr Skoda (http://skodak.org)
591 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
593 class file_info_area_backup_section extends file_info {
594 /** @var stdClass course object */
595 protected $course;
596 /** @var file_info_context_course course file info object */
597 protected $courseinfo;
600 * Constructor
602 * @param file_browser $browser file browser instance
603 * @param stdClass $context context object
604 * @param stdClass $course course object
605 * @param file_info_context_course $courseinfo file info instance
607 public function __construct($browser, $context, $course, file_info_context_course $courseinfo) {
608 parent::__construct($browser, $context);
609 $this->course = $course;
610 $this->courseinfo = $courseinfo;
614 * Returns list of standard virtual file/directory identification.
615 * The difference from stored_file parameters is that null values
616 * are allowed in all fields
618 * @return array with keys contextid, component, filearea, itemid, filepath and filename
620 public function get_params() {
621 return array('contextid' => $this->context->id,
622 'component' => 'backup',
623 'filearea' => 'section',
624 'itemid' => null,
625 'filepath' => null,
626 'filename' => null);
630 * Returns localised visible name.
632 * @return string
634 public function get_visible_name() {
635 return get_string('sectionbackup', 'repository');
639 * Return whether or not new files and directories can be added
641 * @return bool
643 public function is_writable() {
644 return false;
648 * Whether or not this is an empty area
650 * @return bool
652 public function is_empty_area() {
653 $fs = get_file_storage();
654 return $fs->is_area_empty($this->context->id, 'backup', 'section');
658 * Return whether or not this is a directory
660 * @return bool
662 public function is_directory() {
663 return true;
667 * Returns list of children.
669 * @return array of file_info instances
671 public function get_children() {
672 global $DB;
674 $children = array();
676 $course_sections = $DB->get_records('course_sections', array('course'=>$this->course->id), 'section');
677 foreach ($course_sections as $section) {
678 if ($child = $this->courseinfo->get_file_info('backup', 'section', $section->id, '/', '.')) {
679 $children[] = $child;
683 return $children;
687 * Returns parent file_info instance
689 * @return file_info or null for root
691 public function get_parent() {
692 return $this->browser->get_file_info($this->context);