2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * Utility class for browsing of system 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();
29 * Represents the system context in the tree navigated by {@link file_browser}.
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_system extends file_info {
40 * @param file_browser $browser file_browser instance
41 * @param stdClass $context context object
43 public function __construct($browser, $context) {
44 parent::__construct($browser, $context);
48 * Return information about this specific part of context level
50 * @param string $component component
51 * @param string $filearea file area
52 * @param int $itemid item ID
53 * @param string $filepath file path
54 * @param string $filename file name
55 * @return file_info|null file_info instance or null if not found or access not allowed
57 public function get_file_info($component, $filearea, $itemid, $filepath, $filename) {
58 if (empty($component)) {
62 $methodname = "get_area_{$component}_{$filearea}";
64 if (method_exists($this, $methodname)) {
65 return $this->$methodname($itemid, $filepath, $filename);
72 * Gets a stored file for the backup course filearea directory.
74 * @param int $itemid item ID
75 * @param string $filepath file path
76 * @param string $filename file name
77 * @return file_info|null file_info instance or null if not found or access not allowed
79 protected function get_area_backup_course($itemid, $filepath, $filename) {
86 if (!has_any_capability(array('moodle/backup:backupcourse', 'moodle/restore:restorecourse'), $this->context)) {
90 if (is_null($itemid)) {
94 $fs = get_file_storage();
96 $filepath = is_null($filepath) ? '/' : $filepath;
97 $filename = is_null($filename) ? '.' : $filename;
98 if (!$storedfile = $fs->get_file($this->context->id, 'backup', 'course', 0, $filepath, $filename)) {
99 if ($filepath === '/' && $filename === '.') {
100 $storedfile = new virtual_root_file($this->context->id, 'backup', 'course', 0);
107 $downloadable = has_capability('moodle/backup:downloadfile', $this->context);
108 $uploadable = has_capability('moodle/restore:uploadfile', $this->context);
110 $urlbase = $CFG->wwwroot . '/pluginfile.php';
111 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase,
112 get_string('coursebackup', 'repository'), false, $downloadable, $uploadable, false);
116 * Returns localised visible name.
120 public function get_visible_name() {
121 return get_string('arearoot', 'repository');
125 * Whether or not new files or directories can be added
129 public function is_writable() {
134 * Whether or not this is a directory
138 public function is_directory() {
143 * Returns list of children.
145 * @return array of file_info instances
147 public function get_children() {
152 $course_cats = $DB->get_records('course_categories', array('parent'=>0), 'sortorder', 'id,visible');
153 foreach ($course_cats as $category) {
154 $context = context_coursecat::instance($category->id);
155 if (!$category->visible and !has_capability('moodle/category:viewhiddencategories', $context)) {
158 if ($child = $this->browser->get_file_info($context)) {
159 $children[] = $child;
163 $courses = $DB->get_records('course', array('category'=>0), 'sortorder', 'id,visible');
164 foreach ($courses as $course) {
165 if (!$course->visible and !has_capability('moodle/course:viewhiddencourses', $context)) {
168 $context = context_course::instance($course->id);
169 if ($child = $this->browser->get_file_info($context)) {
170 $children[] = $child;
178 * Returns parent file_info instance
180 * @return file_info|null file_info instance or null for root
182 public function get_parent() {