Commit | Line | Data |
---|---|---|
64a19b38 | 1 | <?php |
64a19b38 | 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. | |
13 | // | |
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/>. | |
16 | ||
17 | ||
18 | /** | |
19 | * Utility class for browsing of curse category files. | |
20 | * | |
d2b7803e | 21 | * @package core_files |
64a19b38 | 22 | * @copyright 2008 Petr Skoda (http://skodak.org) |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
24 | */ | |
172dd12c | 25 | |
64f93798 PS |
26 | defined('MOODLE_INTERNAL') || die(); |
27 | ||
b81fd01d | 28 | /** |
d2b7803e | 29 | * Represents a course category context in the tree navigated by {@link file_browser}. |
64f93798 | 30 | * |
d2b7803e | 31 | * @package core_files |
64f93798 PS |
32 | * @copyright 2008 Petr Skoda (http://skodak.org) |
33 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
b81fd01d | 34 | */ |
64f93798 | 35 | class file_info_context_coursecat extends file_info { |
d2b7803e | 36 | /** @var stdClass Category object */ |
172dd12c | 37 | protected $category; |
38 | ||
d2b7803e DC |
39 | /** |
40 | * Constructor | |
41 | * | |
42 | * @param file_browser $browser file browser instance | |
43 | * @param stdClass $context context object | |
44 | * @param stdClass $category category object | |
45 | */ | |
172dd12c | 46 | public function __construct($browser, $context, $category) { |
47 | parent::__construct($browser, $context); | |
48 | $this->category = $category; | |
49 | } | |
50 | ||
4cd9dfda | 51 | /** |
64f93798 PS |
52 | * Return information about this specific context level |
53 | * | |
d2b7803e DC |
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 fileinfo|null | |
4cd9dfda | 60 | */ |
64f93798 PS |
61 | public function get_file_info($component, $filearea, $itemid, $filepath, $filename) { |
62 | global $DB; | |
63 | ||
64 | if (!$this->category->visible and !has_capability('moodle/category:viewhiddencategories', $this->context)) { | |
65 | if (empty($component)) { | |
66 | // we can not list the category contents, so try parent, or top system | |
67 | if ($this->category->parent and $pc = $DB->get_record('course_categories', array('id'=>$this->category->parent))) { | |
b0c6dc1c | 68 | $parent = context_coursecat::instance($pc->id, IGNORE_MISSING); |
64f93798 PS |
69 | return $this->browser->get_file_info($parent); |
70 | } else { | |
71 | return $this->browser->get_file_info(); | |
72 | } | |
73 | } | |
74 | return null; | |
75 | } | |
76 | ||
77 | if (empty($component)) { | |
78 | return $this; | |
79 | } | |
80 | ||
81 | $methodname = "get_area_{$component}_{$filearea}"; | |
82 | if (method_exists($this, $methodname)) { | |
83 | return $this->$methodname($itemid, $filepath, $filename); | |
84 | } | |
85 | ||
86 | return null; | |
87 | } | |
88 | ||
d2b7803e DC |
89 | /** |
90 | * Return a file from course category description area | |
91 | * | |
92 | * @param int $itemid item ID | |
93 | * @param string $filepath file path | |
94 | * @param string $filename file name | |
95 | * @return fileinfo|null | |
96 | */ | |
64f93798 PS |
97 | protected function get_area_coursecat_description($itemid, $filepath, $filename) { |
98 | global $CFG; | |
99 | ||
100 | if (!has_capability('moodle/course:update', $this->context)) { | |
101 | return null; | |
102 | } | |
103 | ||
104 | if (is_null($itemid)) { | |
105 | return $this; | |
106 | } | |
107 | ||
108 | $fs = get_file_storage(); | |
109 | ||
110 | $filepath = is_null($filepath) ? '/' : $filepath; | |
111 | $filename = is_null($filename) ? '.' : $filename; | |
112 | $urlbase = $CFG->wwwroot.'/pluginfile.php'; | |
113 | if (!$storedfile = $fs->get_file($this->context->id, 'coursecat', 'description', 0, $filepath, $filename)) { | |
114 | if ($filepath === '/' and $filename === '.') { | |
115 | $storedfile = new virtual_root_file($this->context->id, 'coursecat', 'description', 0); | |
116 | } else { | |
117 | // not found | |
118 | return null; | |
119 | } | |
120 | } | |
121 | ||
122 | return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('areacategoryintro', 'repository'), false, true, true, false); | |
172dd12c | 123 | } |
124 | ||
4cd9dfda | 125 | /** |
126 | * Returns localised visible name. | |
d2b7803e | 127 | * |
4cd9dfda | 128 | * @return string |
129 | */ | |
172dd12c | 130 | public function get_visible_name() { |
1bbdde13 | 131 | return format_string($this->category->name, true, array('context'=>$this->context)); |
172dd12c | 132 | } |
133 | ||
4cd9dfda | 134 | /** |
d2b7803e DC |
135 | * Whether or not new files or directories can be added |
136 | * | |
4cd9dfda | 137 | * @return bool |
138 | */ | |
cf7ec8e9 | 139 | public function is_writable() { |
140 | return false; | |
141 | } | |
142 | ||
4cd9dfda | 143 | /** |
d2b7803e DC |
144 | * Whether or not this is a directory |
145 | * | |
4cd9dfda | 146 | * @return bool |
147 | */ | |
172dd12c | 148 | public function is_directory() { |
149 | return true; | |
150 | } | |
151 | ||
4cd9dfda | 152 | /** |
153 | * Returns list of children. | |
d2b7803e | 154 | * |
4cd9dfda | 155 | * @return array of file_info instances |
156 | */ | |
172dd12c | 157 | public function get_children() { |
158 | global $DB; | |
159 | ||
160 | $children = array(); | |
161 | ||
64f93798 | 162 | if ($child = $this->get_area_coursecat_description(0, '/', '.')) { |
172dd12c | 163 | $children[] = $child; |
164 | } | |
165 | ||
64f93798 | 166 | $course_cats = $DB->get_records('course_categories', array('parent'=>$this->category->id), 'sortorder', 'id,visible'); |
172dd12c | 167 | foreach ($course_cats as $category) { |
b0c6dc1c | 168 | $context = context_coursecat::instance($category->id); |
64f93798 | 169 | if (!$category->visible and !has_capability('moodle/category:viewhiddencategories', $context)) { |
172dd12c | 170 | continue; |
171 | } | |
172 | if ($child = $this->browser->get_file_info($context)) { | |
173 | $children[] = $child; | |
174 | } | |
175 | } | |
176 | ||
64f93798 | 177 | $courses = $DB->get_records('course', array('category'=>$this->category->id), 'sortorder', 'id,visible'); |
172dd12c | 178 | foreach ($courses as $course) { |
b0c6dc1c | 179 | $context = context_course::instance($course->id); |
172dd12c | 180 | if (!$course->visible and !has_capability('moodle/course:viewhiddencourses', $context)) { |
181 | continue; | |
182 | } | |
183 | if ($child = $this->browser->get_file_info($context)) { | |
184 | $children[] = $child; | |
185 | } | |
186 | } | |
187 | ||
188 | return $children; | |
189 | } | |
190 | ||
4cd9dfda | 191 | /** |
192 | * Returns parent file_info instance | |
d2b7803e DC |
193 | * |
194 | * @return file_info|null fileinfo instance or null for root directory | |
4cd9dfda | 195 | */ |
172dd12c | 196 | public function get_parent() { |
197 | $cid = get_parent_contextid($this->context); | |
5fbe2118 | 198 | $parent = context::instance_by_id($cid, IGNORE_MISSING); |
172dd12c | 199 | return $this->browser->get_file_info($parent); |
200 | } | |
201 | } |