MDL-48624 behat: Added extended timeout to ensure step pass
[moodle.git] / lib / filebrowser / file_info_context_course.php
blob67f842c9f0a19487192ee84ce170b18f073004f7
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 /**
40 * Constructor
42 * @param file_browser $browser file browser instance
43 * @param stdClass $context context object
44 * @param stdClass $course course object
46 public function __construct($browser, $context, $course) {
47 parent::__construct($browser, $context);
48 $this->course = $course;
51 /**
52 * Return information about this specific context level
54 * @param string $component component
55 * @param string $filearea file area
56 * @param int $itemid item ID
57 * @param string $filepath file path
58 * @param string $filename file name
59 * @return file_info|null file_info instance or null if not found or access not allowed
61 public function get_file_info($component, $filearea, $itemid, $filepath, $filename) {
62 // try to emulate require_login() tests here
63 if (!isloggedin()) {
64 return null;
67 if (!$this->course->visible and !has_capability('moodle/course:viewhiddencourses', $this->context)) {
68 return null;
71 if (!is_viewing($this->context) and !is_enrolled($this->context)) {
72 // no peaking here if not enrolled or inspector
73 return null;
76 if (empty($component)) {
77 return $this;
80 $methodname = "get_area_{$component}_{$filearea}";
82 if (method_exists($this, $methodname)) {
83 return $this->$methodname($itemid, $filepath, $filename);
86 return null;
89 /**
90 * Gets a stored file for the course summary filearea directory
92 * @param int $itemid item ID
93 * @param string $filepath file path
94 * @param string $filename file name
95 * @return file_info|null file_info instance or null if not found or access not allowed
97 protected function get_area_course_summary($itemid, $filepath, $filename) {
98 global $CFG;
100 if (!has_capability('moodle/course:update', $this->context)) {
101 return null;
103 if (is_null($itemid)) {
104 return $this;
107 $fs = get_file_storage();
109 $filepath = is_null($filepath) ? '/' : $filepath;
110 $filename = is_null($filename) ? '.' : $filename;
111 if (!$storedfile = $fs->get_file($this->context->id, 'course', 'summary', 0, $filepath, $filename)) {
112 if ($filepath === '/' and $filename === '.') {
113 $storedfile = new virtual_root_file($this->context->id, 'course', 'summary', 0);
114 } else {
115 // not found
116 return null;
119 $urlbase = $CFG->wwwroot.'/pluginfile.php';
120 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('areacourseintro', 'repository'), false, true, true, false);
124 * Gets a stored file for the course images filearea directory
126 * @param int $itemid item ID
127 * @param string $filepath file path
128 * @param string $filename file name
129 * @return file_info|null file_info instance or null if not found or access not allowed
131 protected function get_area_course_overviewfiles($itemid, $filepath, $filename) {
132 global $CFG;
134 if (!has_capability('moodle/course:update', $this->context)) {
135 return null;
137 if (is_null($itemid)) {
138 return $this;
141 $fs = get_file_storage();
143 $filepath = is_null($filepath) ? '/' : $filepath;
144 $filename = is_null($filename) ? '.' : $filename;
145 if (!$storedfile = $fs->get_file($this->context->id, 'course', 'overviewfiles', 0, $filepath, $filename)) {
146 if ($filepath === '/' and $filename === '.') {
147 $storedfile = new virtual_root_file($this->context->id, 'course', 'overviewfiles', 0);
148 } else {
149 // not found
150 return null;
153 $urlbase = $CFG->wwwroot.'/pluginfile.php';
154 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('areacourseoverviewfiles', 'repository'), false, true, true, false);
158 * Gets a stored file for the course section filearea directory
160 * @param int $itemid item ID
161 * @param string $filepath file path
162 * @param string $filename file name
163 * @return file_info|null file_info instance or null if not found or access not allowed
165 protected function get_area_course_section($itemid, $filepath, $filename) {
166 global $CFG, $DB;
168 if (!has_capability('moodle/course:update', $this->context)) {
169 return null;
172 if (empty($itemid)) {
173 // list all sections
174 return new file_info_area_course_section($this->browser, $this->context, $this->course, $this);
177 if (!$section = $DB->get_record('course_sections', array('course'=>$this->course->id, 'id'=>$itemid))) {
178 return null; // does not exist
181 $fs = get_file_storage();
183 $filepath = is_null($filepath) ? '/' : $filepath;
184 $filename = is_null($filename) ? '.' : $filename;
185 if (!$storedfile = $fs->get_file($this->context->id, 'course', 'section', $itemid, $filepath, $filename)) {
186 if ($filepath === '/' and $filename === '.') {
187 $storedfile = new virtual_root_file($this->context->id, 'course', 'section', $itemid);
188 } else {
189 // not found
190 return null;
193 $urlbase = $CFG->wwwroot.'/pluginfile.php';
194 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, $section->section, true, true, true, false);
198 * Gets a stored file for the course legacy filearea directory
200 * @param int $itemid item ID
201 * @param string $filepath file path
202 * @param string $filename file name
203 * @return file_info|null file_info instance or null if not found or access not allowed
205 protected function get_area_course_legacy($itemid, $filepath, $filename) {
206 if (!has_capability('moodle/course:managefiles', $this->context)) {
207 return null;
210 if ($this->course->id != SITEID and $this->course->legacyfiles != 2) {
211 // bad luck, legacy course files not used any more
214 if (is_null($itemid)) {
215 return $this;
218 $fs = get_file_storage();
220 $filepath = is_null($filepath) ? '/' : $filepath;
221 $filename = is_null($filename) ? '.' : $filename;
222 if (!$storedfile = $fs->get_file($this->context->id, 'course', 'legacy', 0, $filepath, $filename)) {
223 if ($filepath === '/' and $filename === '.') {
224 $storedfile = new virtual_root_file($this->context->id, 'course', 'legacy', 0);
225 } else {
226 // not found
227 return null;
231 return new file_info_area_course_legacy($this->browser, $this->context, $storedfile);
235 * Gets a stored file for the backup course filearea directory
237 * @param int $itemid item ID
238 * @param string $filepath file path
239 * @param string $filename file name
240 * @return file_info|null file_info instance or null if not found or access not allowed
242 protected function get_area_backup_course($itemid, $filepath, $filename) {
243 global $CFG;
245 if (!has_capability('moodle/backup:backupcourse', $this->context) and !has_capability('moodle/restore:restorecourse', $this->context)) {
246 return null;
248 if (is_null($itemid)) {
249 return $this;
252 $fs = get_file_storage();
254 $filepath = is_null($filepath) ? '/' : $filepath;
255 $filename = is_null($filename) ? '.' : $filename;
256 if (!$storedfile = $fs->get_file($this->context->id, 'backup', 'course', 0, $filepath, $filename)) {
257 if ($filepath === '/' and $filename === '.') {
258 $storedfile = new virtual_root_file($this->context->id, 'backup', 'course', 0);
259 } else {
260 // not found
261 return null;
265 $downloadable = has_capability('moodle/backup:downloadfile', $this->context);
266 $uploadable = has_capability('moodle/restore:uploadfile', $this->context);
268 $urlbase = $CFG->wwwroot.'/pluginfile.php';
269 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('coursebackup', 'repository'), false, $downloadable, $uploadable, false);
273 * Gets a stored file for the automated backup 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
280 protected function get_area_backup_automated($itemid, $filepath, $filename) {
281 global $CFG;
283 if (!has_capability('moodle/restore:viewautomatedfilearea', $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', 'automated', 0, $filepath, $filename)) {
295 if ($filepath === '/' and $filename === '.') {
296 $storedfile = new virtual_root_file($this->context->id, 'backup', 'automated', 0);
297 } else {
298 // not found
299 return null;
303 $downloadable = has_capability('moodle/site:config', $this->context);
304 $uploadable = false;
306 $urlbase = $CFG->wwwroot.'/pluginfile.php';
307 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('automatedbackup', 'repository'), true, $downloadable, $uploadable, false);
311 * Gets a stored file for the backup section 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 file_info instance or null if not found or access not allowed
318 protected function get_area_backup_section($itemid, $filepath, $filename) {
319 global $CFG, $DB;
321 if (!has_capability('moodle/backup:backupcourse', $this->context) and !has_capability('moodle/restore:restorecourse', $this->context)) {
322 return null;
325 if (empty($itemid)) {
326 // list all sections
327 return new file_info_area_backup_section($this->browser, $this->context, $this->course, $this);
330 if (!$section = $DB->get_record('course_sections', array('course'=>$this->course->id, 'id'=>$itemid))) {
331 return null; // does not exist
334 $fs = get_file_storage();
336 $filepath = is_null($filepath) ? '/' : $filepath;
337 $filename = is_null($filename) ? '.' : $filename;
338 if (!$storedfile = $fs->get_file($this->context->id, 'backup', 'section', $itemid, $filepath, $filename)) {
339 if ($filepath === '/' and $filename === '.') {
340 $storedfile = new virtual_root_file($this->context->id, 'backup', 'section', $itemid);
341 } else {
342 // not found
343 return null;
347 $downloadable = has_capability('moodle/backup:downloadfile', $this->context);
348 $uploadable = has_capability('moodle/restore:uploadfile', $this->context);
350 $urlbase = $CFG->wwwroot.'/pluginfile.php';
351 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, $section->id, true, $downloadable, $uploadable, false);
355 * Returns localised visible name.
357 * @return string
359 public function get_visible_name() {
360 return ($this->course->id == SITEID) ? get_string('frontpage', 'admin') : format_string(get_course_display_name_for_list($this->course), true, array('context'=>$this->context));
364 * Whether or not new files or directories can be added
366 * @return bool
368 public function is_writable() {
369 return false;
373 * Whether or not this is a directory
375 * @return bool
377 public function is_directory() {
378 return true;
382 * Returns list of children.
384 * @return array of file_info instances
386 public function get_children() {
387 return $this->get_filtered_children('*', false, true);
391 * Help function to return files matching extensions or their count
393 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
394 * @param bool|int $countonly if false returns the children, if an int returns just the
395 * count of children but stops counting when $countonly number of children is reached
396 * @param bool $returnemptyfolders if true returns items that don't have matching files inside
397 * @return array|int array of file_info instances or the count
399 private function get_filtered_children($extensions = '*', $countonly = false, $returnemptyfolders = false) {
400 $areas = array(
401 array('course', 'summary'),
402 array('course', 'overviewfiles'),
403 array('course', 'section'),
404 array('backup', 'section'),
405 array('backup', 'course'),
406 array('backup', 'automated'),
407 array('course', 'legacy')
409 $children = array();
410 foreach ($areas as $area) {
411 if ($child = $this->get_file_info($area[0], $area[1], 0, '/', '.')) {
412 if ($returnemptyfolders || $child->count_non_empty_children($extensions)) {
413 $children[] = $child;
414 if (($countonly !== false) && count($children) >= $countonly) {
415 return $countonly;
421 if (!has_capability('moodle/course:managefiles', $this->context)) {
422 // 'managefiles' capability is checked in every activity module callback.
423 // Don't even waste time on retrieving the modules if we can't browse the files anyway
424 } else {
425 // now list all modules
426 $modinfo = get_fast_modinfo($this->course);
427 foreach ($modinfo->cms as $cminfo) {
428 if (empty($cminfo->uservisible)) {
429 continue;
431 $modcontext = context_module::instance($cminfo->id, IGNORE_MISSING);
432 if ($child = $this->browser->get_file_info($modcontext)) {
433 if ($returnemptyfolders || $child->count_non_empty_children($extensions)) {
434 $children[] = $child;
435 if (($countonly !== false) && count($children) >= $countonly) {
436 return $countonly;
443 if ($countonly !== false) {
444 return count($children);
446 return $children;
450 * Returns list of children which are either files matching the specified extensions
451 * or folders that contain at least one such file.
453 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
454 * @return array of file_info instances
456 public function get_non_empty_children($extensions = '*') {
457 return $this->get_filtered_children($extensions, false);
461 * Returns the number of children which are either files matching the specified extensions
462 * or folders containing at least one such file.
464 * @param string|array $extensions, for example '*' or array('.gif','.jpg')
465 * @param int $limit stop counting after at least $limit non-empty children are found
466 * @return int
468 public function count_non_empty_children($extensions = '*', $limit = 1) {
469 return $this->get_filtered_children($extensions, $limit);
473 * Returns parent file_info instance
475 * @return file_info or null for root
477 public function get_parent() {
478 $parent = $this->context->get_parent_context();
479 return $this->browser->get_file_info($parent);
485 * Subclass of file_info_stored for files in the course files area.
487 * @package core_files
488 * @copyright 2008 Petr Skoda (http://skodak.org)
489 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
491 class file_info_area_course_legacy extends file_info_stored {
493 * Constructor
495 * @param file_browser $browser file browser instance
496 * @param stdClass $context context object
497 * @param stored_file $storedfile stored_file instance
499 public function __construct($browser, $context, $storedfile) {
500 global $CFG;
501 $urlbase = $CFG->wwwroot.'/file.php';
502 parent::__construct($browser, $context, $storedfile, $urlbase, get_string('coursefiles'), false, true, true, false);
506 * Returns file download url
508 * @param bool $forcedownload whether or not force download
509 * @param bool $https whether or not force https
510 * @return string url
512 public function get_url($forcedownload=false, $https=false) {
513 if (!$this->is_readable()) {
514 return null;
517 if ($this->lf->is_directory()) {
518 return null;
521 $filepath = $this->lf->get_filepath();
522 $filename = $this->lf->get_filename();
523 $courseid = $this->context->instanceid;
525 $path = '/'.$courseid.$filepath.$filename;
527 return file_encode_url($this->urlbase, $path, $forcedownload, $https);
531 * Returns list of children.
533 * @return array of file_info instances
535 public function get_children() {
536 if (!$this->lf->is_directory()) {
537 return array();
540 $result = array();
541 $fs = get_file_storage();
543 $storedfiles = $fs->get_directory_files($this->context->id, 'course', 'legacy', 0, $this->lf->get_filepath(), false, true, "filepath ASC, filename ASC");
544 foreach ($storedfiles as $file) {
545 $result[] = new file_info_area_course_legacy($this->browser, $this->context, $file);
548 return $result;
552 * Returns list of children which are either files matching the specified extensions
553 * or folders that contain at least one such file.
555 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
556 * @return array of file_info instances
558 public function get_non_empty_children($extensions = '*') {
559 if (!$this->lf->is_directory()) {
560 return array();
563 $result = array();
564 $fs = get_file_storage();
566 $storedfiles = $fs->get_directory_files($this->context->id, 'course', 'legacy', 0,
567 $this->lf->get_filepath(), false, true, "filepath, filename");
568 foreach ($storedfiles as $file) {
569 $extension = core_text::strtolower(pathinfo($file->get_filename(), PATHINFO_EXTENSION));
570 if ($file->is_directory() || $extensions === '*' || (!empty($extension) && in_array('.'.$extension, $extensions))) {
571 $fileinfo = new file_info_area_course_legacy($this->browser, $this->context, $file, $this->urlbase, $this->topvisiblename,
572 $this->itemidused, $this->readaccess, $this->writeaccess, false);
573 if (!$file->is_directory() || $fileinfo->count_non_empty_children($extensions)) {
574 $result[] = $fileinfo;
579 return $result;
584 * Represents a course category context in the tree navigated by {@link file_browser}.
586 * @package core_files
587 * @copyright 2008 Petr Skoda (http://skodak.org)
588 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
590 class file_info_area_course_section extends file_info {
591 /** @var stdClass course object */
592 protected $course;
593 /** @var file_info_context_course course file info object */
594 protected $courseinfo;
597 * Constructor
599 * @param file_browser $browser file browser instance
600 * @param stdClass $context context object
601 * @param stdClass $course course object
602 * @param file_info_context_course $courseinfo file info instance
604 public function __construct($browser, $context, $course, file_info_context_course $courseinfo) {
605 parent::__construct($browser, $context);
606 $this->course = $course;
607 $this->courseinfo = $courseinfo;
611 * Returns list of standard virtual file/directory identification.
612 * The difference from stored_file parameters is that null values
613 * are allowed in all fields
615 * @return array with keys contextid, filearea, itemid, filepath and filename
617 public function get_params() {
618 return array('contextid' => $this->context->id,
619 'component' => 'course',
620 'filearea' => 'section',
621 'itemid' => null,
622 'filepath' => null,
623 'filename' => null);
627 * Returns localised visible name.
629 * @return string
631 public function get_visible_name() {
632 //$format = $this->course->format;
633 $sectionsname = get_string("coursesectionsummaries");
635 return $sectionsname;
639 * Return whether or not new files or directories can be added
641 * @return bool
643 public function is_writable() {
644 return false;
648 * Return whether or not this is a empty area
650 * @return bool
652 public function is_empty_area() {
653 $fs = get_file_storage();
654 return $fs->is_area_empty($this->context->id, 'course', 'section');
658 * Return whether or not this is a empty area
660 * @return bool
662 public function is_directory() {
663 return true;
667 * Returns list of children.
669 * @return array of file_info instances
671 public function get_children() {
672 global $DB;
674 $children = array();
676 $course_sections = $DB->get_records('course_sections', array('course'=>$this->course->id), 'section');
677 foreach ($course_sections as $section) {
678 if ($child = $this->courseinfo->get_file_info('course', 'section', $section->id, '/', '.')) {
679 $children[] = $child;
683 return $children;
687 * Returns the number of children which are either files matching the specified extensions
688 * or folders containing at least one such file.
690 * @param string|array $extensions, for example '*' or array('.gif','.jpg')
691 * @param int $limit stop counting after at least $limit non-empty children are found
692 * @return int
694 public function count_non_empty_children($extensions = '*', $limit = 1) {
695 global $DB;
696 $params1 = array(
697 'courseid' => $this->course->id,
698 'contextid' => $this->context->id,
699 'component' => 'course',
700 'filearea' => 'section',
701 'emptyfilename' => '.');
702 $sql1 = "SELECT DISTINCT cs.id FROM {files} f, {course_sections} cs
703 WHERE cs.course = :courseid
704 AND f.contextid = :contextid
705 AND f.component = :component
706 AND f.filearea = :filearea
707 AND f.itemid = cs.id
708 AND f.filename <> :emptyfilename";
709 list($sql2, $params2) = $this->build_search_files_sql($extensions);
710 $rs = $DB->get_recordset_sql($sql1. ' '. $sql2, array_merge($params1, $params2));
711 $cnt = 0;
712 foreach ($rs as $record) {
713 if ((++$cnt) >= $limit) {
714 break;
717 $rs->close();
718 return $cnt;
722 * Returns parent file_info instance
724 * @return file_info|null file_info or null for root
726 public function get_parent() {
727 return $this->courseinfo;
733 * Implementation of course section backup area
735 * @package core_files
736 * @copyright 2008 Petr Skoda (http://skodak.org)
737 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
739 class file_info_area_backup_section extends file_info {
740 /** @var stdClass course object */
741 protected $course;
742 /** @var file_info_context_course course file info object */
743 protected $courseinfo;
746 * Constructor
748 * @param file_browser $browser file browser instance
749 * @param stdClass $context context object
750 * @param stdClass $course course object
751 * @param file_info_context_course $courseinfo file info instance
753 public function __construct($browser, $context, $course, file_info_context_course $courseinfo) {
754 parent::__construct($browser, $context);
755 $this->course = $course;
756 $this->courseinfo = $courseinfo;
760 * Returns list of standard virtual file/directory identification.
761 * The difference from stored_file parameters is that null values
762 * are allowed in all fields
764 * @return array with keys contextid, component, filearea, itemid, filepath and filename
766 public function get_params() {
767 return array('contextid' => $this->context->id,
768 'component' => 'backup',
769 'filearea' => 'section',
770 'itemid' => null,
771 'filepath' => null,
772 'filename' => null);
776 * Returns localised visible name.
778 * @return string
780 public function get_visible_name() {
781 return get_string('sectionbackup', 'repository');
785 * Return whether or not new files and directories can be added
787 * @return bool
789 public function is_writable() {
790 return false;
794 * Whether or not this is an empty area
796 * @return bool
798 public function is_empty_area() {
799 $fs = get_file_storage();
800 return $fs->is_area_empty($this->context->id, 'backup', 'section');
804 * Return whether or not this is a directory
806 * @return bool
808 public function is_directory() {
809 return true;
813 * Returns list of children.
815 * @return array of file_info instances
817 public function get_children() {
818 global $DB;
820 $children = array();
822 $course_sections = $DB->get_records('course_sections', array('course'=>$this->course->id), 'section');
823 foreach ($course_sections as $section) {
824 if ($child = $this->courseinfo->get_file_info('backup', 'section', $section->id, '/', '.')) {
825 $children[] = $child;
829 return $children;
833 * Returns the number of children which are either files matching the specified extensions
834 * or folders containing at least one such file.
836 * @param string|array $extensions, for example '*' or array('.gif','.jpg')
837 * @param int $limit stop counting after at least $limit non-empty children are found
838 * @return int
840 public function count_non_empty_children($extensions = '*', $limit = 1) {
841 global $DB;
842 $params1 = array(
843 'courseid' => $this->course->id,
844 'contextid' => $this->context->id,
845 'component' => 'backup',
846 'filearea' => 'section',
847 'emptyfilename' => '.');
848 $sql1 = "SELECT DISTINCT cs.id AS sectionid FROM {files} f, {course_sections} cs
849 WHERE cs.course = :courseid
850 AND f.contextid = :contextid
851 AND f.component = :component
852 AND f.filearea = :filearea
853 AND f.itemid = cs.id
854 AND f.filename <> :emptyfilename";
855 list($sql2, $params2) = $this->build_search_files_sql($extensions);
856 $rs = $DB->get_recordset_sql($sql1. ' '. $sql2, array_merge($params1, $params2));
857 $cnt = 0;
858 foreach ($rs as $record) {
859 if ((++$cnt) >= $limit) {
860 break;
863 $rs->close();
864 return $cnt;
868 * Returns parent file_info instance
870 * @return file_info or null for root
872 public function get_parent() {
873 return $this->browser->get_file_info($this->context);