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 curse category 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 a course category 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_coursecat extends file_info {
36 /** @var stdClass Category object */
42 * @param file_browser $browser file browser instance
43 * @param stdClass $context context object
44 * @param stdClass $category category object
46 public function __construct($browser, $context, $category) {
47 parent::__construct($browser, $context);
48 $this->category = $category;
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 fileinfo|null
61 public function get_file_info($component, $filearea, $itemid, $filepath, $filename) {
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))) {
68 $parent = context_coursecat::instance($pc->id, IGNORE_MISSING);
69 return $this->browser->get_file_info($parent);
71 return $this->browser->get_file_info();
77 if (empty($component)) {
81 $methodname = "get_area_{$component}_{$filearea}";
82 if (method_exists($this, $methodname)) {
83 return $this->$methodname($itemid, $filepath, $filename);
90 * Return a file from course category description area
92 * @param int $itemid item ID
93 * @param string $filepath file path
94 * @param string $filename file name
95 * @return fileinfo|null
97 protected function get_area_coursecat_description($itemid, $filepath, $filename) {
100 if (!has_capability('moodle/course:update', $this->context)) {
104 if (is_null($itemid)) {
108 $fs = get_file_storage();
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);
122 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('areacategoryintro', 'repository'), false, true, true, false);
126 * Returns localised visible name.
130 public function get_visible_name() {
131 return format_string($this->category->name, true, array('context'=>$this->context));
135 * Whether or not new files or directories can be added
139 public function is_writable() {
144 * Whether or not this is a directory
148 public function is_directory() {
153 * Returns list of children.
155 * @return array of file_info instances
157 public function get_children() {
162 if ($child = $this->get_area_coursecat_description(0, '/', '.')) {
163 $children[] = $child;
166 $course_cats = $DB->get_records('course_categories', array('parent'=>$this->category->id), 'sortorder', 'id,visible');
167 foreach ($course_cats as $category) {
168 $context = context_coursecat::instance($category->id);
169 if (!$category->visible and !has_capability('moodle/category:viewhiddencategories', $context)) {
172 if ($child = $this->browser->get_file_info($context)) {
173 $children[] = $child;
177 $courses = $DB->get_records('course', array('category'=>$this->category->id), 'sortorder', 'id,visible');
178 foreach ($courses as $course) {
179 $context = context_course::instance($course->id);
180 if (!$course->visible and !has_capability('moodle/course:viewhiddencourses', $context)) {
183 if ($child = $this->browser->get_file_info($context)) {
184 $children[] = $child;
192 * Returns the number of children which are either files matching the specified extensions
193 * or folders containing at least one such file.
195 * @param string|array $extensions, for example '*' or array('.gif','.jpg')
196 * @param int $limit stop counting after at least $limit non-empty children are found
199 public function count_non_empty_children($extensions = '*', $limit = 1) {
202 if (($child = $this->get_area_coursecat_description(0, '/', '.'))
203 && $child->count_non_empty_children($extensions) && (++$cnt) >= $limit) {
207 $rs = $DB->get_recordset_sql('SELECT ctx.id AS contextid, c.visible
208 FROM {context} ctx, {course} c
209 WHERE ctx.instanceid = c.id
210 AND ctx.contextlevel = :courselevel
211 AND c.category = :categoryid
212 ORDER BY c.visible DESC', // retrieve visible courses first
213 array('categoryid' => $this->category->id, 'courselevel' => CONTEXT_COURSE));
214 foreach ($rs as $record) {
215 $context = context::instance_by_id($record->contextid);
216 if (!$record->visible and !has_capability('moodle/course:viewhiddencourses', $context)) {
219 if (($child = $this->browser->get_file_info($context))
220 && $child->count_non_empty_children($extensions) && (++$cnt) >= $limit) {
225 if ($cnt >= $limit) {
229 $rs = $DB->get_recordset_sql('SELECT ctx.id AS contextid, cat.visible
230 FROM {context} ctx, {course_categories} cat
231 WHERE ctx.instanceid = cat.id
232 AND ctx.contextlevel = :catlevel
233 AND cat.parent = :categoryid
234 ORDER BY cat.visible DESC', // retrieve visible categories first
235 array('categoryid' => $this->category->id, 'catlevel' => CONTEXT_COURSECAT));
236 foreach ($rs as $record) {
237 $context = context::instance_by_id($record->contextid);
238 if (!$record->visible and !has_capability('moodle/category:viewhiddencategories', $context)) {
241 if (($child = $this->browser->get_file_info($context))
242 && $child->count_non_empty_children($extensions) && (++$cnt) >= $limit) {
252 * Returns parent file_info instance
254 * @return file_info|null fileinfo instance or null for root directory
256 public function get_parent() {
257 $parent = $this->context->get_parent_context();
258 return $this->browser->get_file_info($parent);