weekly release 2.3dev
[moodle.git] / lib / filebrowser / file_info_context_coursecat.php
CommitLineData
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 *
64f93798
PS
22 * @package core
23 * @subpackage filebrowser
64a19b38 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
64f93798
PS
28defined('MOODLE_INTERNAL') || die();
29
b81fd01d 30/**
31 * Represents a course category context in the tree navigated by @see{file_browser}.
64f93798
PS
32 *
33 * @package core
34 * @subpackage filebrowser
35 * @copyright 2008 Petr Skoda (http://skodak.org)
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
b81fd01d 37 */
64f93798 38class file_info_context_coursecat extends file_info {
172dd12c 39 protected $category;
40
41 public function __construct($browser, $context, $category) {
42 parent::__construct($browser, $context);
43 $this->category = $category;
44 }
45
4cd9dfda 46 /**
64f93798
PS
47 * Return information about this specific context level
48 *
49 * @param $component
50 * @param $filearea
51 * @param $itemid
52 * @param $filepath
53 * @param $filename
4cd9dfda 54 */
64f93798
PS
55 public function get_file_info($component, $filearea, $itemid, $filepath, $filename) {
56 global $DB;
57
58 if (!$this->category->visible and !has_capability('moodle/category:viewhiddencategories', $this->context)) {
59 if (empty($component)) {
60 // we can not list the category contents, so try parent, or top system
61 if ($this->category->parent and $pc = $DB->get_record('course_categories', array('id'=>$this->category->parent))) {
62 $parent = get_context_instance(CONTEXT_COURSECAT, $pc->id);
63 return $this->browser->get_file_info($parent);
64 } else {
65 return $this->browser->get_file_info();
66 }
67 }
68 return null;
69 }
70
71 if (empty($component)) {
72 return $this;
73 }
74
75 $methodname = "get_area_{$component}_{$filearea}";
76 if (method_exists($this, $methodname)) {
77 return $this->$methodname($itemid, $filepath, $filename);
78 }
79
80 return null;
81 }
82
83 protected function get_area_coursecat_description($itemid, $filepath, $filename) {
84 global $CFG;
85
86 if (!has_capability('moodle/course:update', $this->context)) {
87 return null;
88 }
89
90 if (is_null($itemid)) {
91 return $this;
92 }
93
94 $fs = get_file_storage();
95
96 $filepath = is_null($filepath) ? '/' : $filepath;
97 $filename = is_null($filename) ? '.' : $filename;
98 $urlbase = $CFG->wwwroot.'/pluginfile.php';
99 if (!$storedfile = $fs->get_file($this->context->id, 'coursecat', 'description', 0, $filepath, $filename)) {
100 if ($filepath === '/' and $filename === '.') {
101 $storedfile = new virtual_root_file($this->context->id, 'coursecat', 'description', 0);
102 } else {
103 // not found
104 return null;
105 }
106 }
107
108 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('areacategoryintro', 'repository'), false, true, true, false);
172dd12c 109 }
110
4cd9dfda 111 /**
112 * Returns localised visible name.
113 * @return string
114 */
172dd12c 115 public function get_visible_name() {
1bbdde13 116 return format_string($this->category->name, true, array('context'=>$this->context));
172dd12c 117 }
118
4cd9dfda 119 /**
120 * Can I add new files or directories?
121 * @return bool
122 */
cf7ec8e9 123 public function is_writable() {
124 return false;
125 }
126
4cd9dfda 127 /**
128 * Is directory?
129 * @return bool
130 */
172dd12c 131 public function is_directory() {
132 return true;
133 }
134
4cd9dfda 135 /**
136 * Returns list of children.
137 * @return array of file_info instances
138 */
172dd12c 139 public function get_children() {
140 global $DB;
141
142 $children = array();
143
64f93798 144 if ($child = $this->get_area_coursecat_description(0, '/', '.')) {
172dd12c 145 $children[] = $child;
146 }
147
64f93798 148 $course_cats = $DB->get_records('course_categories', array('parent'=>$this->category->id), 'sortorder', 'id,visible');
172dd12c 149 foreach ($course_cats as $category) {
150 $context = get_context_instance(CONTEXT_COURSECAT, $category->id);
64f93798 151 if (!$category->visible and !has_capability('moodle/category:viewhiddencategories', $context)) {
172dd12c 152 continue;
153 }
154 if ($child = $this->browser->get_file_info($context)) {
155 $children[] = $child;
156 }
157 }
158
64f93798 159 $courses = $DB->get_records('course', array('category'=>$this->category->id), 'sortorder', 'id,visible');
172dd12c 160 foreach ($courses as $course) {
161 $context = get_context_instance(CONTEXT_COURSE, $course->id);
162 if (!$course->visible and !has_capability('moodle/course:viewhiddencourses', $context)) {
163 continue;
164 }
165 if ($child = $this->browser->get_file_info($context)) {
166 $children[] = $child;
167 }
168 }
169
170 return $children;
171 }
172
4cd9dfda 173 /**
174 * Returns parent file_info instance
175 * @return file_info or null for root
176 */
172dd12c 177 public function get_parent() {
178 $cid = get_parent_contextid($this->context);
179 $parent = get_context_instance_by_id($cid);
180 return $this->browser->get_file_info($parent);
181 }
182}