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 course 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 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_course
extends file_info
{
36 /** @var stdClass course object */
39 /** @var file_info_context_module[] cached child modules. See {@link get_child_module()} */
40 protected $childrenmodules = [];
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;
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
70 if (!$this->course
->visible
and !has_capability('moodle/course:viewhiddencourses', $this->context
)) {
74 if (!is_viewing($this->context
) and !$this->browser
->is_enrolled($this->course
->id
)) {
75 // no peaking here if not enrolled or inspector
79 if (empty($component)) {
83 $methodname = "get_area_{$component}_{$filearea}";
85 if (method_exists($this, $methodname)) {
86 return $this->$methodname($itemid, $filepath, $filename);
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
99 protected function get_course_areas($extensions = '*', $returnemptyfolders = false) {
104 'course_overviewfiles',
112 if ($returnemptyfolders) {
116 $params1 = ['contextid' => $this->context
->id
, 'emptyfilename' => '.'];
117 $sql1 = "SELECT " . $DB->sql_concat('f.component', "'_'", 'f.filearea') . "
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) {
138 if (!has_capability('moodle/course:update', $this->context
)) {
141 if (is_null($itemid)) {
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);
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) {
172 if (!has_capability('moodle/course:update', $this->context
)) {
175 if (is_null($itemid)) {
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);
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) {
206 if (!has_capability('moodle/course:update', $this->context
)) {
210 if (empty($itemid)) {
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);
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
)) {
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)) {
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);
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) {
283 if (!has_capability('moodle/backup:backupcourse', $this->context
) and !has_capability('moodle/restore:restorecourse', $this->context
)) {
286 if (is_null($itemid)) {
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);
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) {
321 if (!has_capability('moodle/restore:viewautomatedfilearea', $this->context
)) {
324 if (is_null($itemid)) {
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);
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
);
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) {
361 if (!has_capability('moodle/backup:backupcourse', $this->context
) and !has_capability('moodle/restore:restorecourse', $this->context
)) {
365 if (empty($itemid)) {
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);
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.
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
408 public function is_writable() {
413 * Whether or not this is a directory
417 public function is_directory() {
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) {
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) {
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
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;
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.
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;
501 if (($countonly !== false) && $cnt >= $countonly) {
507 $children = array_merge($children, array_values(array_filter($modulechildren)));
511 if ($countonly !== false) {
512 return count($children);
518 * Returns list of areas inside the course modules that have files with the given extension
520 * @param string $extensions
523 protected function get_module_areas_with_files($extensions = '*') {
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') . "
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);
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);
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);
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
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
{
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) {
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
626 public function get_url($forcedownload=false, $https=false) {
627 if (!$this->is_readable()) {
631 if ($this->lf
->is_directory()) {
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()) {
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);
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()) {
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;
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 */
707 /** @var file_info_context_course course file info object */
708 protected $courseinfo;
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',
741 * Returns localised visible name.
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
757 public function is_writable() {
762 * Return whether or not this is a empty area
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
776 public function is_directory() {
781 * Returns list of children.
783 * @return array of file_info instances
785 public function get_children() {
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;
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
808 public function count_non_empty_children($extensions = '*', $limit = 1) {
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
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));
826 foreach ($rs as $record) {
827 if ((++
$cnt) >= $limit) {
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 */
856 /** @var file_info_context_course course file info object */
857 protected $courseinfo;
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',
890 * Returns localised visible name.
894 public function get_visible_name() {
895 return get_string('sectionbackup', 'repository');
899 * Return whether or not new files and directories can be added
903 public function is_writable() {
908 * Whether or not this is an empty area
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
922 public function is_directory() {
927 * Returns list of children.
929 * @return array of file_info instances
931 public function get_children() {
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;
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
954 public function count_non_empty_children($extensions = '*', $limit = 1) {
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
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));
972 foreach ($rs as $record) {
973 if ((++
$cnt) >= $limit) {
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
);