MDL-63050 cachestore_redis: Update hExists to check empty
[moodle.git] / lib / filebrowser / file_info_context_course.php
blob7afca6c60fe945104e7e0b91a4202e2750263bd7
1 <?php
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.
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/>.
18 /**
19 * Utility class for browsing of course files.
21 * @package core_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();
28 /**
29 * Represents a course context in the tree navigated by {@link file_browser}.
31 * @package core_files
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_course extends file_info {
36 /** @var stdClass course object */
37 protected $course;
39 /** @var file_info_context_module[] cached child modules. See {@link get_child_module()} */
40 protected $childrenmodules = [];
42 /**
43 * Constructor
45 * @param file_browser $browser file browser instance
46 * @param stdClass $context context object
47 * @param stdClass $course course object
49 public function __construct($browser, $context, $course) {
50 parent::__construct($browser, $context);
51 $this->course = $course;
54 /**
55 * Return information about this specific context level
57 * @param string $component component
58 * @param string $filearea file area
59 * @param int $itemid item ID
60 * @param string $filepath file path
61 * @param string $filename file name
62 * @return file_info|null file_info instance or null if not found or access not allowed
64 public function get_file_info($component, $filearea, $itemid, $filepath, $filename) {
65 // try to emulate require_login() tests here
66 if (!isloggedin()) {
67 return null;
70 if (!$this->course->visible and !has_capability('moodle/course:viewhiddencourses', $this->context)) {
71 return null;
74 if (!is_viewing($this->context) and !$this->browser->is_enrolled($this->course->id)) {
75 // no peaking here if not enrolled or inspector
76 return null;
79 if (empty($component)) {
80 return $this;
83 $methodname = "get_area_{$component}_{$filearea}";
85 if (method_exists($this, $methodname)) {
86 return $this->$methodname($itemid, $filepath, $filename);
89 return null;
92 /**
93 * Returns list of areas inside this course
95 * @param string $extensions Only return areas that have files with these extensions
96 * @param bool $returnemptyfolders return all areas always, if true it will ignore the previous argument
97 * @return array
99 protected function get_course_areas($extensions = '*', $returnemptyfolders = false) {
100 global $DB;
102 $allareas = [
103 'course_summary',
104 'course_overviewfiles',
105 'course_section',
106 'backup_section',
107 'backup_course',
108 'backup_automated',
109 'course_legacy'
112 if ($returnemptyfolders) {
113 return $allareas;
116 $params1 = ['contextid' => $this->context->id, 'emptyfilename' => '.'];
117 $sql1 = "SELECT " . $DB->sql_concat('f.component', "'_'", 'f.filearea') . "
118 FROM {files} f
119 WHERE f.filename <> :emptyfilename AND f.contextid = :contextid ";
120 $sql3 = ' GROUP BY f.component, f.filearea';
121 list($sql2, $params2) = $this->build_search_files_sql($extensions);
122 $areaswithfiles = $DB->get_fieldset_sql($sql1 . $sql2 . $sql3, array_merge($params1, $params2));
124 return array_intersect($allareas, $areaswithfiles);
128 * Gets a stored file for the course summary filearea directory
130 * @param int $itemid item ID
131 * @param string $filepath file path
132 * @param string $filename file name
133 * @return file_info|null file_info instance or null if not found or access not allowed
135 protected function get_area_course_summary($itemid, $filepath, $filename) {
136 global $CFG;
138 if (!has_capability('moodle/course:update', $this->context)) {
139 return null;
141 if (is_null($itemid)) {
142 return $this;
145 $fs = get_file_storage();
147 $filepath = is_null($filepath) ? '/' : $filepath;
148 $filename = is_null($filename) ? '.' : $filename;
149 if (!$storedfile = $fs->get_file($this->context->id, 'course', 'summary', 0, $filepath, $filename)) {
150 if ($filepath === '/' and $filename === '.') {
151 $storedfile = new virtual_root_file($this->context->id, 'course', 'summary', 0);
152 } else {
153 // not found
154 return null;
157 $urlbase = $CFG->wwwroot.'/pluginfile.php';
158 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('areacourseintro', 'repository'), false, true, true, false);
162 * Gets a stored file for the course images filearea directory
164 * @param int $itemid item ID
165 * @param string $filepath file path
166 * @param string $filename file name
167 * @return file_info|null file_info instance or null if not found or access not allowed
169 protected function get_area_course_overviewfiles($itemid, $filepath, $filename) {
170 global $CFG;
172 if (!has_capability('moodle/course:update', $this->context)) {
173 return null;
175 if (is_null($itemid)) {
176 return $this;
179 $fs = get_file_storage();
181 $filepath = is_null($filepath) ? '/' : $filepath;
182 $filename = is_null($filename) ? '.' : $filename;
183 if (!$storedfile = $fs->get_file($this->context->id, 'course', 'overviewfiles', 0, $filepath, $filename)) {
184 if ($filepath === '/' and $filename === '.') {
185 $storedfile = new virtual_root_file($this->context->id, 'course', 'overviewfiles', 0);
186 } else {
187 // not found
188 return null;
191 $urlbase = $CFG->wwwroot.'/pluginfile.php';
192 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('areacourseoverviewfiles', 'repository'), false, true, true, false);
196 * Gets a stored file for the course section filearea directory
198 * @param int $itemid item ID
199 * @param string $filepath file path
200 * @param string $filename file name
201 * @return file_info|null file_info instance or null if not found or access not allowed
203 protected function get_area_course_section($itemid, $filepath, $filename) {
204 global $CFG, $DB;
206 if (!has_capability('moodle/course:update', $this->context)) {
207 return null;
210 if (empty($itemid)) {
211 // list all sections
212 return new file_info_area_course_section($this->browser, $this->context, $this->course, $this);
215 if (!$section = $DB->get_record('course_sections', array('course'=>$this->course->id, 'id'=>$itemid))) {
216 return null; // does not exist
219 $fs = get_file_storage();
221 $filepath = is_null($filepath) ? '/' : $filepath;
222 $filename = is_null($filename) ? '.' : $filename;
223 if (!$storedfile = $fs->get_file($this->context->id, 'course', 'section', $itemid, $filepath, $filename)) {
224 if ($filepath === '/' and $filename === '.') {
225 $storedfile = new virtual_root_file($this->context->id, 'course', 'section', $itemid);
226 } else {
227 // not found
228 return null;
231 $urlbase = $CFG->wwwroot.'/pluginfile.php';
232 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, $section->section, true, true, true, false);
236 * Gets a stored file for the course legacy filearea directory
238 * @param int $itemid item ID
239 * @param string $filepath file path
240 * @param string $filename file name
241 * @return file_info|null file_info instance or null if not found or access not allowed
243 protected function get_area_course_legacy($itemid, $filepath, $filename) {
244 if (!has_capability('moodle/course:managefiles', $this->context)) {
245 return null;
248 if ($this->course->id != SITEID and $this->course->legacyfiles != 2) {
249 // bad luck, legacy course files not used any more
252 if (is_null($itemid)) {
253 return $this;
256 $fs = get_file_storage();
258 $filepath = is_null($filepath) ? '/' : $filepath;
259 $filename = is_null($filename) ? '.' : $filename;
260 if (!$storedfile = $fs->get_file($this->context->id, 'course', 'legacy', 0, $filepath, $filename)) {
261 if ($filepath === '/' and $filename === '.') {
262 $storedfile = new virtual_root_file($this->context->id, 'course', 'legacy', 0);
263 } else {
264 // not found
265 return null;
269 return new file_info_area_course_legacy($this->browser, $this->context, $storedfile);
273 * Gets a stored file for the backup course filearea directory
275 * @param int $itemid item ID
276 * @param string $filepath file path
277 * @param string $filename file name
278 * @return file_info|null file_info instance or null if not found or access not allowed
280 protected function get_area_backup_course($itemid, $filepath, $filename) {
281 global $CFG;
283 if (!has_capability('moodle/backup:backupcourse', $this->context) and !has_capability('moodle/restore:restorecourse', $this->context)) {
284 return null;
286 if (is_null($itemid)) {
287 return $this;
290 $fs = get_file_storage();
292 $filepath = is_null($filepath) ? '/' : $filepath;
293 $filename = is_null($filename) ? '.' : $filename;
294 if (!$storedfile = $fs->get_file($this->context->id, 'backup', 'course', 0, $filepath, $filename)) {
295 if ($filepath === '/' and $filename === '.') {
296 $storedfile = new virtual_root_file($this->context->id, 'backup', 'course', 0);
297 } else {
298 // not found
299 return null;
303 $downloadable = has_capability('moodle/backup:downloadfile', $this->context);
304 $uploadable = has_capability('moodle/restore:uploadfile', $this->context);
306 $urlbase = $CFG->wwwroot.'/pluginfile.php';
307 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('coursebackup', 'repository'), false, $downloadable, $uploadable, false);
311 * Gets a stored file for the automated backup filearea directory
313 * @param int $itemid item ID
314 * @param string $filepath file path
315 * @param string $filename file name
316 * @return file_info|null
318 protected function get_area_backup_automated($itemid, $filepath, $filename) {
319 global $CFG;
321 if (!has_capability('moodle/restore:viewautomatedfilearea', $this->context)) {
322 return null;
324 if (is_null($itemid)) {
325 return $this;
328 $fs = get_file_storage();
330 $filepath = is_null($filepath) ? '/' : $filepath;
331 $filename = is_null($filename) ? '.' : $filename;
332 if (!$storedfile = $fs->get_file($this->context->id, 'backup', 'automated', 0, $filepath, $filename)) {
333 if ($filepath === '/' and $filename === '.') {
334 $storedfile = new virtual_root_file($this->context->id, 'backup', 'automated', 0);
335 } else {
336 // not found
337 return null;
341 // Automated backup files are only downloadable if the user has both 'backup:downloadfile and 'restore:userinfo'.
342 $downloadable = has_capability('moodle/backup:downloadfile', $this->context) &&
343 has_capability('moodle/restore:userinfo', $this->context);
344 $uploadable = false;
346 $urlbase = $CFG->wwwroot.'/pluginfile.php';
347 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('automatedbackup', 'repository'), true, $downloadable, $uploadable, false);
351 * Gets a stored file for the backup section filearea directory
353 * @param int $itemid item ID
354 * @param string $filepath file path
355 * @param string $filename file name
356 * @return file_info|null file_info instance or null if not found or access not allowed
358 protected function get_area_backup_section($itemid, $filepath, $filename) {
359 global $CFG, $DB;
361 if (!has_capability('moodle/backup:backupcourse', $this->context) and !has_capability('moodle/restore:restorecourse', $this->context)) {
362 return null;
365 if (empty($itemid)) {
366 // list all sections
367 return new file_info_area_backup_section($this->browser, $this->context, $this->course, $this);
370 if (!$section = $DB->get_record('course_sections', array('course'=>$this->course->id, 'id'=>$itemid))) {
371 return null; // does not exist
374 $fs = get_file_storage();
376 $filepath = is_null($filepath) ? '/' : $filepath;
377 $filename = is_null($filename) ? '.' : $filename;
378 if (!$storedfile = $fs->get_file($this->context->id, 'backup', 'section', $itemid, $filepath, $filename)) {
379 if ($filepath === '/' and $filename === '.') {
380 $storedfile = new virtual_root_file($this->context->id, 'backup', 'section', $itemid);
381 } else {
382 // not found
383 return null;
387 $downloadable = has_capability('moodle/backup:downloadfile', $this->context);
388 $uploadable = has_capability('moodle/restore:uploadfile', $this->context);
390 $urlbase = $CFG->wwwroot.'/pluginfile.php';
391 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, $section->id, true, $downloadable, $uploadable, false);
395 * Returns localised visible name.
397 * @return string
399 public function get_visible_name() {
400 return ($this->course->id == SITEID) ? get_string('frontpage', 'admin') : format_string(get_course_display_name_for_list($this->course), true, array('context'=>$this->context));
404 * Whether or not new files or directories can be added
406 * @return bool
408 public function is_writable() {
409 return false;
413 * Whether or not this is a directory
415 * @return bool
417 public function is_directory() {
418 return true;
422 * Returns list of children.
424 * @return array of file_info instances
426 public function get_children() {
427 return $this->get_filtered_children('*', false, true);
431 * Returns the child module if it is accessible by the current user
433 * @param cm_info|int $cm
434 * @return file_info_context_module|null
436 protected function get_child_module($cm) {
437 $cmid = is_object($cm) ? $cm->id : $cm;
438 if (!array_key_exists($cmid, $this->childrenmodules)) {
439 $this->childrenmodules[$cmid] = null;
440 if (!($cm instanceof cm_info)) {
441 $cms = get_fast_modinfo($this->course)->cms;
442 $cm = array_key_exists($cmid, $cms) ? $cms[$cmid] : null;
444 if ($cm && $cm->uservisible) {
445 $this->childrenmodules[$cmid] = new file_info_context_module($this->browser,
446 $cm->context, $this->course, $cm, $cm->modname);
449 return $this->childrenmodules[$cmid];
453 * Help function to return files matching extensions or their count
455 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
456 * @param bool|int $countonly if false returns the children, if an int returns just the
457 * count of children but stops counting when $countonly number of children is reached
458 * @param bool $returnemptyfolders if true returns items that don't have matching files inside
459 * @return array|int array of file_info instances or the count
461 private function get_filtered_children($extensions = '*', $countonly = false, $returnemptyfolders = false) {
462 $children = array();
464 $courseareas = $this->get_course_areas($extensions, $returnemptyfolders);
465 foreach ($courseareas as $areaname) {
466 $area = explode('_', $areaname, 2);
467 if ($child = $this->get_file_info($area[0], $area[1], 0, '/', '.')) {
468 $children[] = $child;
469 if (($countonly !== false) && count($children) >= $countonly) {
470 return $countonly;
475 $cnt = count($children);
476 if (!has_capability('moodle/course:managefiles', $this->context)) {
477 // 'managefiles' capability is checked in every activity module callback.
478 // Don't even waste time on retrieving the modules if we can't browse the files anyway
479 } else {
480 if ($returnemptyfolders) {
481 $modinfo = get_fast_modinfo($this->course);
482 foreach ($modinfo->cms as $cminfo) {
483 if ($child = $this->get_child_module($cminfo)) {
484 $children[] = $child;
485 $cnt++;
488 } else if ($moduleareas = $this->get_module_areas_with_files($extensions)) {
489 // We found files in some of the modules.
490 // Create array of children modules ordered with the same way as cms in modinfo.
491 $modulechildren = array_fill_keys(array_keys(get_fast_modinfo($this->course)->get_cms()), null);
492 foreach ($moduleareas as $area) {
493 if ($modulechildren[$area->cmid]) {
494 // We already found non-empty area within the same module, do not analyse other areas.
495 continue;
497 if ($child = $this->get_child_module($area->cmid)) {
498 if ($child->get_file_info($area->component, $area->filearea, $area->itemid, null, null)) {
499 $modulechildren[$area->cmid] = $child;
500 $cnt++;
501 if (($countonly !== false) && $cnt >= $countonly) {
502 return $cnt;
507 $children = array_merge($children, array_values(array_filter($modulechildren)));
511 if ($countonly !== false) {
512 return count($children);
514 return $children;
518 * Returns list of areas inside the course modules that have files with the given extension
520 * @param string $extensions
521 * @return array
523 protected function get_module_areas_with_files($extensions = '*') {
524 global $DB;
526 $params1 = ['contextid' => $this->context->id,
527 'emptyfilename' => '.',
528 'contextlevel' => CONTEXT_MODULE,
529 'depth' => $this->context->depth + 1,
530 'pathmask' => $this->context->path . '/%'];
531 $sql1 = "SELECT ctx.id AS contextid, f.component, f.filearea, f.itemid, ctx.instanceid AS cmid, " .
532 context_helper::get_preload_record_columns_sql('ctx') . "
533 FROM {files} f
534 INNER JOIN {context} ctx ON ctx.id = f.contextid
535 WHERE f.filename <> :emptyfilename
536 AND ctx.contextlevel = :contextlevel
537 AND ctx.depth = :depth
538 AND " . $DB->sql_like('ctx.path', ':pathmask') . " ";
539 $sql3 = ' GROUP BY ctx.id, f.component, f.filearea, f.itemid, ctx.instanceid,
540 ctx.path, ctx.depth, ctx.contextlevel
541 ORDER BY ctx.id, f.component, f.filearea, f.itemid';
542 list($sql2, $params2) = $this->build_search_files_sql($extensions);
543 $areas = [];
544 if ($rs = $DB->get_recordset_sql($sql1. $sql2 . $sql3, array_merge($params1, $params2))) {
545 foreach ($rs as $record) {
546 context_helper::preload_from_record($record);
547 $areas[] = $record;
549 $rs->close();
552 // Sort areas so 'backup' and 'intro' are in the beginning of the list, they are the easiest to check access to.
553 usort($areas, function($a, $b) {
554 $aeasy = ($a->filearea === 'intro' && substr($a->component, 0, 4) === 'mod_') ||
555 ($a->filearea === 'activity' && $a->component === 'backup');
556 $beasy = ($b->filearea === 'intro' && substr($b->component, 0, 4) === 'mod_') ||
557 ($b->filearea === 'activity' && $b->component === 'backup');
558 return $aeasy == $beasy ? 0 : ($aeasy ? -1 : 1);
560 return $areas;
564 * Returns list of children which are either files matching the specified extensions
565 * or folders that contain at least one such file.
567 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
568 * @return array of file_info instances
570 public function get_non_empty_children($extensions = '*') {
571 return $this->get_filtered_children($extensions, false);
575 * Returns the number of children which are either files matching the specified extensions
576 * or folders containing at least one such file.
578 * @param string|array $extensions, for example '*' or array('.gif','.jpg')
579 * @param int $limit stop counting after at least $limit non-empty children are found
580 * @return int
582 public function count_non_empty_children($extensions = '*', $limit = 1) {
583 return $this->get_filtered_children($extensions, $limit);
587 * Returns parent file_info instance
589 * @return file_info or null for root
591 public function get_parent() {
592 $parent = $this->context->get_parent_context();
593 return $this->browser->get_file_info($parent);
599 * Subclass of file_info_stored for files in the course files area.
601 * @package core_files
602 * @copyright 2008 Petr Skoda (http://skodak.org)
603 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
605 class file_info_area_course_legacy extends file_info_stored {
607 * Constructor
609 * @param file_browser $browser file browser instance
610 * @param stdClass $context context object
611 * @param stored_file $storedfile stored_file instance
613 public function __construct($browser, $context, $storedfile) {
614 global $CFG;
615 $urlbase = $CFG->wwwroot.'/file.php';
616 parent::__construct($browser, $context, $storedfile, $urlbase, get_string('coursefiles'), false, true, true, false);
620 * Returns file download url
622 * @param bool $forcedownload whether or not force download
623 * @param bool $https whether or not force https
624 * @return string url
626 public function get_url($forcedownload=false, $https=false) {
627 if (!$this->is_readable()) {
628 return null;
631 if ($this->lf->is_directory()) {
632 return null;
635 $filepath = $this->lf->get_filepath();
636 $filename = $this->lf->get_filename();
637 $courseid = $this->context->instanceid;
639 $path = '/'.$courseid.$filepath.$filename;
641 return file_encode_url($this->urlbase, $path, $forcedownload, $https);
645 * Returns list of children.
647 * @return array of file_info instances
649 public function get_children() {
650 if (!$this->lf->is_directory()) {
651 return array();
654 $result = array();
655 $fs = get_file_storage();
657 $storedfiles = $fs->get_directory_files($this->context->id, 'course', 'legacy', 0, $this->lf->get_filepath(), false, true, "filepath ASC, filename ASC");
658 foreach ($storedfiles as $file) {
659 $result[] = new file_info_area_course_legacy($this->browser, $this->context, $file);
662 return $result;
666 * Returns list of children which are either files matching the specified extensions
667 * or folders that contain at least one such file.
669 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
670 * @return array of file_info instances
672 public function get_non_empty_children($extensions = '*') {
673 if (!$this->lf->is_directory()) {
674 return array();
677 $result = array();
678 $fs = get_file_storage();
680 $storedfiles = $fs->get_directory_files($this->context->id, 'course', 'legacy', 0,
681 $this->lf->get_filepath(), false, true, "filepath, filename");
682 foreach ($storedfiles as $file) {
683 $extension = core_text::strtolower(pathinfo($file->get_filename(), PATHINFO_EXTENSION));
684 if ($file->is_directory() || $extensions === '*' || (!empty($extension) && in_array('.'.$extension, $extensions))) {
685 $fileinfo = new file_info_area_course_legacy($this->browser, $this->context, $file, $this->urlbase, $this->topvisiblename,
686 $this->itemidused, $this->readaccess, $this->writeaccess, false);
687 if (!$file->is_directory() || $fileinfo->count_non_empty_children($extensions)) {
688 $result[] = $fileinfo;
693 return $result;
698 * Represents a course category context in the tree navigated by {@link file_browser}.
700 * @package core_files
701 * @copyright 2008 Petr Skoda (http://skodak.org)
702 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
704 class file_info_area_course_section extends file_info {
705 /** @var stdClass course object */
706 protected $course;
707 /** @var file_info_context_course course file info object */
708 protected $courseinfo;
711 * Constructor
713 * @param file_browser $browser file browser instance
714 * @param stdClass $context context object
715 * @param stdClass $course course object
716 * @param file_info_context_course $courseinfo file info instance
718 public function __construct($browser, $context, $course, file_info_context_course $courseinfo) {
719 parent::__construct($browser, $context);
720 $this->course = $course;
721 $this->courseinfo = $courseinfo;
725 * Returns list of standard virtual file/directory identification.
726 * The difference from stored_file parameters is that null values
727 * are allowed in all fields
729 * @return array with keys contextid, filearea, itemid, filepath and filename
731 public function get_params() {
732 return array('contextid' => $this->context->id,
733 'component' => 'course',
734 'filearea' => 'section',
735 'itemid' => null,
736 'filepath' => null,
737 'filename' => null);
741 * Returns localised visible name.
743 * @return string
745 public function get_visible_name() {
746 //$format = $this->course->format;
747 $sectionsname = get_string("coursesectionsummaries");
749 return $sectionsname;
753 * Return whether or not new files or directories can be added
755 * @return bool
757 public function is_writable() {
758 return false;
762 * Return whether or not this is a empty area
764 * @return bool
766 public function is_empty_area() {
767 $fs = get_file_storage();
768 return $fs->is_area_empty($this->context->id, 'course', 'section');
772 * Return whether or not this is a empty area
774 * @return bool
776 public function is_directory() {
777 return true;
781 * Returns list of children.
783 * @return array of file_info instances
785 public function get_children() {
786 global $DB;
788 $children = array();
790 $course_sections = $DB->get_records('course_sections', array('course'=>$this->course->id), 'section');
791 foreach ($course_sections as $section) {
792 if ($child = $this->courseinfo->get_file_info('course', 'section', $section->id, '/', '.')) {
793 $children[] = $child;
797 return $children;
801 * Returns the number of children which are either files matching the specified extensions
802 * or folders containing at least one such file.
804 * @param string|array $extensions, for example '*' or array('.gif','.jpg')
805 * @param int $limit stop counting after at least $limit non-empty children are found
806 * @return int
808 public function count_non_empty_children($extensions = '*', $limit = 1) {
809 global $DB;
810 $params1 = array(
811 'courseid' => $this->course->id,
812 'contextid' => $this->context->id,
813 'component' => 'course',
814 'filearea' => 'section',
815 'emptyfilename' => '.');
816 $sql1 = "SELECT DISTINCT cs.id FROM {files} f, {course_sections} cs
817 WHERE cs.course = :courseid
818 AND f.contextid = :contextid
819 AND f.component = :component
820 AND f.filearea = :filearea
821 AND f.itemid = cs.id
822 AND f.filename <> :emptyfilename";
823 list($sql2, $params2) = $this->build_search_files_sql($extensions);
824 $rs = $DB->get_recordset_sql($sql1. ' '. $sql2, array_merge($params1, $params2));
825 $cnt = 0;
826 foreach ($rs as $record) {
827 if ((++$cnt) >= $limit) {
828 break;
831 $rs->close();
832 return $cnt;
836 * Returns parent file_info instance
838 * @return file_info|null file_info or null for root
840 public function get_parent() {
841 return $this->courseinfo;
847 * Implementation of course section backup area
849 * @package core_files
850 * @copyright 2008 Petr Skoda (http://skodak.org)
851 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
853 class file_info_area_backup_section extends file_info {
854 /** @var stdClass course object */
855 protected $course;
856 /** @var file_info_context_course course file info object */
857 protected $courseinfo;
860 * Constructor
862 * @param file_browser $browser file browser instance
863 * @param stdClass $context context object
864 * @param stdClass $course course object
865 * @param file_info_context_course $courseinfo file info instance
867 public function __construct($browser, $context, $course, file_info_context_course $courseinfo) {
868 parent::__construct($browser, $context);
869 $this->course = $course;
870 $this->courseinfo = $courseinfo;
874 * Returns list of standard virtual file/directory identification.
875 * The difference from stored_file parameters is that null values
876 * are allowed in all fields
878 * @return array with keys contextid, component, filearea, itemid, filepath and filename
880 public function get_params() {
881 return array('contextid' => $this->context->id,
882 'component' => 'backup',
883 'filearea' => 'section',
884 'itemid' => null,
885 'filepath' => null,
886 'filename' => null);
890 * Returns localised visible name.
892 * @return string
894 public function get_visible_name() {
895 return get_string('sectionbackup', 'repository');
899 * Return whether or not new files and directories can be added
901 * @return bool
903 public function is_writable() {
904 return false;
908 * Whether or not this is an empty area
910 * @return bool
912 public function is_empty_area() {
913 $fs = get_file_storage();
914 return $fs->is_area_empty($this->context->id, 'backup', 'section');
918 * Return whether or not this is a directory
920 * @return bool
922 public function is_directory() {
923 return true;
927 * Returns list of children.
929 * @return array of file_info instances
931 public function get_children() {
932 global $DB;
934 $children = array();
936 $course_sections = $DB->get_records('course_sections', array('course'=>$this->course->id), 'section');
937 foreach ($course_sections as $section) {
938 if ($child = $this->courseinfo->get_file_info('backup', 'section', $section->id, '/', '.')) {
939 $children[] = $child;
943 return $children;
947 * Returns the number of children which are either files matching the specified extensions
948 * or folders containing at least one such file.
950 * @param string|array $extensions, for example '*' or array('.gif','.jpg')
951 * @param int $limit stop counting after at least $limit non-empty children are found
952 * @return int
954 public function count_non_empty_children($extensions = '*', $limit = 1) {
955 global $DB;
956 $params1 = array(
957 'courseid' => $this->course->id,
958 'contextid' => $this->context->id,
959 'component' => 'backup',
960 'filearea' => 'section',
961 'emptyfilename' => '.');
962 $sql1 = "SELECT DISTINCT cs.id AS sectionid FROM {files} f, {course_sections} cs
963 WHERE cs.course = :courseid
964 AND f.contextid = :contextid
965 AND f.component = :component
966 AND f.filearea = :filearea
967 AND f.itemid = cs.id
968 AND f.filename <> :emptyfilename";
969 list($sql2, $params2) = $this->build_search_files_sql($extensions);
970 $rs = $DB->get_recordset_sql($sql1. ' '. $sql2, array_merge($params1, $params2));
971 $cnt = 0;
972 foreach ($rs as $record) {
973 if ((++$cnt) >= $limit) {
974 break;
977 $rs->close();
978 return $cnt;
982 * Returns parent file_info instance
984 * @return file_info or null for root
986 public function get_parent() {
987 return $this->browser->get_file_info($this->context);