64a19b38 |
1 | <?php |
2 | |
3 | // This file is part of Moodle - http://moodle.org/ |
4 | // |
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. |
9 | // |
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. |
14 | // |
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/>. |
17 | |
18 | |
19 | /** |
20 | * Utility class for browsing of curse category files. |
21 | * |
22 | * @package moodlecore |
23 | * @subpackage file-browser |
24 | * @copyright 2008 Petr Skoda (http://skodak.org) |
25 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
26 | */ |
172dd12c |
27 | |
b81fd01d |
28 | /** |
29 | * Represents a course category context in the tree navigated by @see{file_browser}. |
30 | */ |
172dd12c |
31 | class file_info_coursecat extends file_info { |
32 | protected $category; |
33 | |
34 | public function __construct($browser, $context, $category) { |
35 | parent::__construct($browser, $context); |
36 | $this->category = $category; |
37 | } |
38 | |
4cd9dfda |
39 | /** |
40 | * Returns list of standard virtual file/directory identification. |
41 | * The difference from stored_file parameters is that null values |
42 | * are allowed in all fields |
43 | * @return array with keys contextid, filearea, itemid, filepath and filename |
44 | */ |
172dd12c |
45 | public function get_params() { |
46 | return array('contextid'=>$this->context->id, |
47 | 'filearea' =>null, |
48 | 'itemid' =>null, |
49 | 'filepath' =>null, |
50 | 'filename' =>null); |
51 | } |
52 | |
4cd9dfda |
53 | /** |
54 | * Returns localised visible name. |
55 | * @return string |
56 | */ |
172dd12c |
57 | public function get_visible_name() { |
58 | return format_string($this->category->name); |
59 | } |
60 | |
4cd9dfda |
61 | /** |
62 | * Can I add new files or directories? |
63 | * @return bool |
64 | */ |
cf7ec8e9 |
65 | public function is_writable() { |
66 | return false; |
67 | } |
68 | |
4cd9dfda |
69 | /** |
70 | * Is directory? |
71 | * @return bool |
72 | */ |
172dd12c |
73 | public function is_directory() { |
74 | return true; |
75 | } |
76 | |
4cd9dfda |
77 | /** |
78 | * Returns list of children. |
79 | * @return array of file_info instances |
80 | */ |
172dd12c |
81 | public function get_children() { |
82 | global $DB; |
83 | |
84 | $children = array(); |
85 | |
86 | if ($child = $this->browser->get_file_info($this->context, 'coursecat_intro', 0)) { |
87 | $children[] = $child; |
88 | } |
89 | |
90 | $course_cats = $DB->get_records('course_categories', array('parent'=>$this->category->id), 'sortorder'); |
91 | foreach ($course_cats as $category) { |
92 | $context = get_context_instance(CONTEXT_COURSECAT, $category->id); |
93 | if (!$category->visible and !has_capability('moodle/course:viewhiddencourses', $context)) { |
94 | continue; |
95 | } |
96 | if ($child = $this->browser->get_file_info($context)) { |
97 | $children[] = $child; |
98 | } |
99 | } |
100 | |
101 | $courses = $DB->get_records('course', array('category'=>$this->category->id), 'sortorder'); |
102 | foreach ($courses as $course) { |
103 | $context = get_context_instance(CONTEXT_COURSE, $course->id); |
104 | if (!$course->visible and !has_capability('moodle/course:viewhiddencourses', $context)) { |
105 | continue; |
106 | } |
107 | if ($child = $this->browser->get_file_info($context)) { |
108 | $children[] = $child; |
109 | } |
110 | } |
111 | |
112 | return $children; |
113 | } |
114 | |
4cd9dfda |
115 | /** |
116 | * Returns parent file_info instance |
117 | * @return file_info or null for root |
118 | */ |
172dd12c |
119 | public function get_parent() { |
120 | $cid = get_parent_contextid($this->context); |
121 | $parent = get_context_instance_by_id($cid); |
122 | return $this->browser->get_file_info($parent); |
123 | } |
124 | } |