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 module 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 module 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_module
extends file_info
{
36 /** @var stdClass Course object */
38 /** @var cm_info Course module object */
40 /** @var string Module name */
42 /** @var array Available file areas */
44 /** @var array caches the result of last call to get_non_empty_children() */
45 protected $nonemptychildren;
50 * @param file_browser $browser file browser instance
51 * @param stdClass $context context object
52 * @param stdClass $course course object
53 * @param stdClass $cm course module object
54 * @param string $modname module name
56 public function __construct($browser, $context, $course, $cm, $modname) {
59 parent
::__construct($browser, $context);
60 $this->course
= $course;
61 $this->cm
= cm_info
::create($cm);
62 $this->modname
= $this->cm
->modname
;
63 $this->nonemptychildren
= null;
65 if ($functionname = component_callback_exists('mod_'.$modname, 'get_file_areas')) {
66 $cm = $this->cm
->get_course_module_record();
67 $this->areas
= $functionname($course, $cm, $context);
69 $this->areas
= array();
72 unset($this->areas
['intro']); // hardcoded, ignore attempts to override it
76 * Return information about this specific context level
78 * @param string $component component
79 * @param string $filearea file area
80 * @param int $itemid item ID
81 * @param string $filepath file path
82 * @param string $filename file name
83 * @return file_info|null
85 public function get_file_info($component, $filearea, $itemid, $filepath, $filename) {
86 // try to emulate require_login() tests here
91 $coursecontext = $this->context
->get_course_context(true);
92 if (!$this->course
->visible
and !has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
96 if (!is_viewing($this->context
) and !$this->browser
->is_enrolled($this->course
->id
)) {
97 // no peaking here if not enrolled or inspector
101 if (!$this->cm
->uservisible
) {
102 // activity hidden sorry
106 if (empty($component)) {
110 if ($component == 'mod_'.$this->modname
and $filearea === 'intro') {
111 return $this->get_area_intro($itemid, $filepath, $filename);
112 } else if ($component == 'backup' and $filearea === 'activity') {
113 return $this->get_area_backup($itemid, $filepath, $filename);
116 if ($functionname = component_callback_exists('mod_'.$this->modname
, 'get_file_info')) {
117 $cm = $this->cm
->get_course_module_record();
118 return $functionname($this->browser
, $this->areas
, $this->course
, $cm,
119 $this->context
, $filearea, $itemid, $filepath, $filename);
126 * Get a file from module intro area
128 * @param int $itemid item ID
129 * @param string $filepath file path
130 * @param string $filename file name
131 * @return file_info|null
133 protected function get_area_intro($itemid, $filepath, $filename) {
136 if (!plugin_supports('mod', $this->modname
, FEATURE_MOD_INTRO
, true) or !has_capability('moodle/course:managefiles', $this->context
)) {
140 $fs = get_file_storage();
142 $filepath = is_null($filepath) ?
'/' : $filepath;
143 $filename = is_null($filename) ?
'.' : $filename;
144 if (!$storedfile = $fs->get_file($this->context
->id
, 'mod_'.$this->modname
, 'intro', 0, $filepath, $filename)) {
145 if ($filepath === '/' and $filename === '.') {
146 $storedfile = new virtual_root_file($this->context
->id
, 'mod_'.$this->modname
, 'intro', 0);
153 $urlbase = $CFG->wwwroot
.'/pluginfile.php';
154 return new file_info_stored($this->browser
, $this->context
, $storedfile, $urlbase, get_string('moduleintro'), false, true, true, false);
158 * Get a file from module backup area
160 * @param int $itemid item ID
161 * @param string $filepath file path
162 * @param string $filename file name
163 * @return file_info|null
165 protected function get_area_backup($itemid, $filepath, $filename) {
168 if (!has_capability('moodle/backup:backupactivity', $this->context
)) {
172 $fs = get_file_storage();
174 $filepath = is_null($filepath) ?
'/' : $filepath;
175 $filename = is_null($filename) ?
'.' : $filename;
176 if (!$storedfile = $fs->get_file($this->context
->id
, 'backup', 'activity', 0, $filepath, $filename)) {
177 if ($filepath === '/' and $filename === '.') {
178 $storedfile = new virtual_root_file($this->context
->id
, 'backup', 'activity', 0);
185 $downloadable = has_capability('moodle/backup:downloadfile', $this->context
);
186 $uploadable = has_capability('moodle/restore:uploadfile', $this->context
);
188 $urlbase = $CFG->wwwroot
.'/pluginfile.php';
189 return new file_info_stored($this->browser
, $this->context
, $storedfile, $urlbase, get_string('activitybackup', 'repository'), false, $downloadable, $uploadable, false);
193 * Returns localised visible name.
197 public function get_visible_name() {
198 return $this->cm
->get_formatted_name().' ('.$this->cm
->get_module_type_name().')';
202 * Whether or not files or directories can be added
206 public function is_writable() {
211 * Whether or not this is an emtpy area
215 public function is_empty_area() {
216 if ($child = $this->get_area_backup(0, '/', '.')) {
217 if (!$child->is_empty_area()) {
221 if ($child = $this->get_area_intro(0, '/', '.')) {
222 if (!$child->is_empty_area()) {
227 foreach ($this->areas
as $area=>$desctiption) {
228 if ($child = $this->get_file_info('mod_'.$this->modname
, $area, null, null, null)) {
229 if (!$child->is_empty_area()) {
239 * Whether or not this is a directory
243 public function is_directory() {
248 * Returns list of children.
250 * @return array of file_info instances
252 public function get_children() {
253 return $this->get_filtered_children('*', false, true);
257 * Help function to return files matching extensions or their count
259 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
260 * @param bool|int $countonly if false returns the children, if an int returns just the
261 * count of children but stops counting when $countonly number of children is reached
262 * @param bool $returnemptyfolders if true returns items that don't have matching files inside
263 * @return array|int array of file_info instances or the count
265 private function get_filtered_children($extensions = '*', $countonly = false, $returnemptyfolders = false) {
267 // prepare list of areas including intro and backup
269 array('mod_'.$this->modname
, 'intro'),
270 array('backup', 'activity')
272 foreach ($this->areas
as $area => $desctiption) {
273 $areas[] = array('mod_'.$this->modname
, $area);
276 $params1 = array('contextid' => $this->context
->id
, 'emptyfilename' => '.');
277 list($sql2, $params2) = $this->build_search_files_sql($extensions);
279 foreach ($areas as $area) {
280 if (!$returnemptyfolders) {
281 // fast pre-check if there are any files in the filearea
282 $params1['component'] = $area[0];
283 $params1['filearea'] = $area[1];
284 if (!$DB->record_exists_sql('SELECT 1 from {files}
285 WHERE contextid = :contextid
286 AND filename <> :emptyfilename
287 AND component = :component
288 AND filearea = :filearea '.$sql2,
289 array_merge($params1, $params2))) {
293 if ($child = $this->get_file_info($area[0], $area[1], null, null, null)) {
294 if ($returnemptyfolders ||
$child->count_non_empty_children($extensions)) {
295 $children[] = $child;
296 if ($countonly !== false && count($children) >= $countonly) {
302 if ($countonly !== false) {
303 return count($children);
309 * Returns list of children which are either files matching the specified extensions
310 * or folders that contain at least one such file.
312 * @param string|array $extensions either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
313 * @return array of file_info instances
315 public function get_non_empty_children($extensions = '*') {
316 if ($this->nonemptychildren
!== null) {
317 return $this->nonemptychildren
;
319 $this->nonemptychildren
= $this->get_filtered_children($extensions);
320 return $this->nonemptychildren
;
324 * Returns the number of children which are either files matching the specified extensions
325 * or folders containing at least one such file.
327 * @param string|array $extensions for example '*' or array('.gif','.jpg')
328 * @param int $limit stop counting after at least $limit non-empty children are found
331 public function count_non_empty_children($extensions = '*', $limit = 1) {
332 if ($this->nonemptychildren
!== null) {
333 return count($this->nonemptychildren
);
335 return $this->get_filtered_children($extensions, $limit);
339 * Returns parent file_info instance
341 * @return file_info|null file_info or null for root
343 public function get_parent() {
344 $parent = $this->context
->get_parent_context();
345 return $this->browser
->get_file_info($parent);