MDL-37254 add missing AS keyword
[moodle.git] / lib / filebrowser / file_info_context_course.php
CommitLineData
64f93798 1<?php
64f93798
PS
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.
13//
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/>.
16
17
18/**
19 * Utility class for browsing of course files.
20 *
d2b7803e 21 * @package core_files
64f93798
PS
22 * @copyright 2008 Petr Skoda (http://skodak.org)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28/**
d2b7803e 29 * Represents a course context in the tree navigated by {@link file_browser}.
64f93798 30 *
d2b7803e 31 * @package core_files
64f93798
PS
32 * @copyright 2008 Petr Skoda (http://skodak.org)
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
35class file_info_context_course extends file_info {
d2b7803e 36 /** @var stdClass course object */
64f93798
PS
37 protected $course;
38
d2b7803e
DC
39 /**
40 * Constructor
41 *
42 * @param file_browser $browser file browser instance
43 * @param stdClass $context context object
44 * @param stdClass $course course object
45 */
64f93798
PS
46 public function __construct($browser, $context, $course) {
47 parent::__construct($browser, $context);
48 $this->course = $course;
49 }
50
51 /**
52 * Return information about this specific context level
53 *
d2b7803e
DC
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
64f93798
PS
60 */
61 public function get_file_info($component, $filearea, $itemid, $filepath, $filename) {
f6b07c4d
PS
62 // try to emulate require_login() tests here
63 if (!isloggedin()) {
64 return null;
65 }
66
64f93798
PS
67 if (!$this->course->visible and !has_capability('moodle/course:viewhiddencourses', $this->context)) {
68 return null;
69 }
70
f6b07c4d
PS
71 if (!is_viewing($this->context) and !is_enrolled($this->context)) {
72 // no peaking here if not enrolled or inspector
73 return null;
74 }
75
64f93798
PS
76 if (empty($component)) {
77 return $this;
78 }
79
80 $methodname = "get_area_{$component}_{$filearea}";
81
82 if (method_exists($this, $methodname)) {
83 return $this->$methodname($itemid, $filepath, $filename);
84 }
85
86 return null;
87 }
88
d2b7803e
DC
89 /**
90 * Gets a stored file for the course summary filearea directory
91 *
92 * @param int $itemid item ID
93 * @param string $filepath file path
94 * @param string $filename file name
4ab641ee 95 * @return file_info|null file_info instance or null if not found or access not allowed
d2b7803e 96 */
64f93798
PS
97 protected function get_area_course_summary($itemid, $filepath, $filename) {
98 global $CFG;
99
100 if (!has_capability('moodle/course:update', $this->context)) {
101 return null;
102 }
103 if (is_null($itemid)) {
104 return $this;
105 }
106
107 $fs = get_file_storage();
108
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;
117 }
118 }
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);
121 }
122
d2b7803e
DC
123 /**
124 * Gets a stored file for the course section filearea directory
125 *
126 * @param int $itemid item ID
127 * @param string $filepath file path
128 * @param string $filename file name
4ab641ee 129 * @return file_info|null file_info instance or null if not found or access not allowed
d2b7803e 130 */
64f93798
PS
131 protected function get_area_course_section($itemid, $filepath, $filename) {
132 global $CFG, $DB;
133
134 if (!has_capability('moodle/course:update', $this->context)) {
135 return null;
136 }
137
138 if (empty($itemid)) {
139 // list all sections
140 return new file_info_area_course_section($this->browser, $this->context, $this->course, $this);
141 }
142
143 if (!$section = $DB->get_record('course_sections', array('course'=>$this->course->id, 'id'=>$itemid))) {
144 return null; // does not exist
145 }
146
147 $fs = get_file_storage();
148
149 $filepath = is_null($filepath) ? '/' : $filepath;
150 $filename = is_null($filename) ? '.' : $filename;
151 if (!$storedfile = $fs->get_file($this->context->id, 'course', 'section', $itemid, $filepath, $filename)) {
152 if ($filepath === '/' and $filename === '.') {
153 $storedfile = new virtual_root_file($this->context->id, 'course', 'section', $itemid);
154 } else {
155 // not found
156 return null;
157 }
158 }
159 $urlbase = $CFG->wwwroot.'/pluginfile.php';
160 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, $section->section, true, true, true, false);
161 }
162
d2b7803e
DC
163 /**
164 * Gets a stored file for the course legacy filearea directory
165 *
166 * @param int $itemid item ID
167 * @param string $filepath file path
168 * @param string $filename file name
4ab641ee 169 * @return file_info|null file_info instance or null if not found or access not allowed
d2b7803e 170 */
64f93798
PS
171 protected function get_area_course_legacy($itemid, $filepath, $filename) {
172 if (!has_capability('moodle/course:managefiles', $this->context)) {
173 return null;
174 }
175
176 if ($this->course->id != SITEID and $this->course->legacyfiles != 2) {
177 // bad luck, legacy course files not used any more
178 }
179
d4848e8d 180 if (is_null($itemid)) {
64f93798
PS
181 return $this;
182 }
183
184 $fs = get_file_storage();
185
186 $filepath = is_null($filepath) ? '/' : $filepath;
187 $filename = is_null($filename) ? '.' : $filename;
188 if (!$storedfile = $fs->get_file($this->context->id, 'course', 'legacy', 0, $filepath, $filename)) {
189 if ($filepath === '/' and $filename === '.') {
4299f381 190 $storedfile = new virtual_root_file($this->context->id, 'course', 'legacy', 0);
64f93798
PS
191 } else {
192 // not found
193 return null;
194 }
195 }
196
197 return new file_info_area_course_legacy($this->browser, $this->context, $storedfile);
198 }
199
d2b7803e
DC
200 /**
201 * Gets a stored file for the backup course filearea directory
202 *
203 * @param int $itemid item ID
204 * @param string $filepath file path
205 * @param string $filename file name
4ab641ee 206 * @return file_info|null file_info instance or null if not found or access not allowed
d2b7803e 207 */
64f93798
PS
208 protected function get_area_backup_course($itemid, $filepath, $filename) {
209 global $CFG;
210
211 if (!has_capability('moodle/backup:backupcourse', $this->context) and !has_capability('moodle/restore:restorecourse', $this->context)) {
212 return null;
213 }
214 if (is_null($itemid)) {
215 return $this;
216 }
217
218 $fs = get_file_storage();
219
220 $filepath = is_null($filepath) ? '/' : $filepath;
221 $filename = is_null($filename) ? '.' : $filename;
222 if (!$storedfile = $fs->get_file($this->context->id, 'backup', 'course', 0, $filepath, $filename)) {
223 if ($filepath === '/' and $filename === '.') {
224 $storedfile = new virtual_root_file($this->context->id, 'backup', 'course', 0);
225 } else {
226 // not found
227 return null;
228 }
229 }
230
231 $downloadable = has_capability('moodle/backup:downloadfile', $this->context);
232 $uploadable = has_capability('moodle/restore:uploadfile', $this->context);
233
234 $urlbase = $CFG->wwwroot.'/pluginfile.php';
235 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('coursebackup', 'repository'), false, $downloadable, $uploadable, false);
236 }
237
bac233d3
SH
238 /**
239 * Gets a stored file for the automated backup filearea directory
240 *
d2b7803e
DC
241 * @param int $itemid item ID
242 * @param string $filepath file path
243 * @param string $filename file name
244 * @return file_info|null
bac233d3
SH
245 */
246 protected function get_area_backup_automated($itemid, $filepath, $filename) {
247 global $CFG;
248
249 if (!has_capability('moodle/restore:viewautomatedfilearea', $this->context)) {
250 return null;
251 }
252 if (is_null($itemid)) {
253 return $this;
254 }
255
256 $fs = get_file_storage();
257
258 $filepath = is_null($filepath) ? '/' : $filepath;
259 $filename = is_null($filename) ? '.' : $filename;
260 if (!$storedfile = $fs->get_file($this->context->id, 'backup', 'automated', 0, $filepath, $filename)) {
261 if ($filepath === '/' and $filename === '.') {
262 $storedfile = new virtual_root_file($this->context->id, 'backup', 'automated', 0);
263 } else {
264 // not found
265 return null;
266 }
267 }
268
269 $downloadable = has_capability('moodle/site:config', $this->context);
270 $uploadable = false;
271
272 $urlbase = $CFG->wwwroot.'/pluginfile.php';
273 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('automatedbackup', 'repository'), true, $downloadable, $uploadable, false);
274 }
275
d2b7803e
DC
276 /**
277 * Gets a stored file for the backup section filearea directory
278 *
279 * @param int $itemid item ID
280 * @param string $filepath file path
281 * @param string $filename file name
4ab641ee 282 * @return file_info|null file_info instance or null if not found or access not allowed
d2b7803e 283 */
64f93798
PS
284 protected function get_area_backup_section($itemid, $filepath, $filename) {
285 global $CFG, $DB;
286
287 if (!has_capability('moodle/backup:backupcourse', $this->context) and !has_capability('moodle/restore:restorecourse', $this->context)) {
288 return null;
289 }
290
291 if (empty($itemid)) {
292 // list all sections
293 return new file_info_area_backup_section($this->browser, $this->context, $this->course, $this);
294 }
295
296 if (!$section = $DB->get_record('course_sections', array('course'=>$this->course->id, 'id'=>$itemid))) {
297 return null; // does not exist
298 }
299
300 $fs = get_file_storage();
301
302 $filepath = is_null($filepath) ? '/' : $filepath;
303 $filename = is_null($filename) ? '.' : $filename;
304 if (!$storedfile = $fs->get_file($this->context->id, 'backup', 'section', $itemid, $filepath, $filename)) {
305 if ($filepath === '/' and $filename === '.') {
306 $storedfile = new virtual_root_file($this->context->id, 'backup', 'section', $itemid);
307 } else {
308 // not found
309 return null;
310 }
311 }
312
313 $downloadable = has_capability('moodle/backup:downloadfile', $this->context);
314 $uploadable = has_capability('moodle/restore:uploadfile', $this->context);
315
316 $urlbase = $CFG->wwwroot.'/pluginfile.php';
317 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, $section->id, true, $downloadable, $uploadable, false);
318 }
319
d2b7803e
DC
320 /**
321 * Returns localised visible name.
322 *
323 * @return string
324 */
64f93798 325 public function get_visible_name() {
1bbdde13 326 return ($this->course->id == SITEID) ? get_string('frontpage', 'admin') : format_string($this->course->fullname, true, array('context'=>$this->context));
64f93798
PS
327 }
328
329 /**
d2b7803e
DC
330 * Whether or not new files or directories can be added
331 *
64f93798
PS
332 * @return bool
333 */
334 public function is_writable() {
335 return false;
336 }
337
338 /**
d2b7803e
DC
339 * Whether or not this is a directory
340 *
64f93798
PS
341 * @return bool
342 */
343 public function is_directory() {
344 return true;
345 }
346
347 /**
348 * Returns list of children.
d2b7803e 349 *
64f93798
PS
350 * @return array of file_info instances
351 */
352 public function get_children() {
c84b7045
MG
353 return $this->get_filtered_children('*', false, true);
354 }
64f93798 355
c84b7045
MG
356 /**
357 * Help function to return files matching extensions or their count
358 *
359 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
ed7e6a73
MG
360 * @param bool|int $countonly if false returns the children, if an int returns just the
361 * count of children but stops counting when $countonly number of children is reached
c84b7045
MG
362 * @param bool $returnemptyfolders if true returns items that don't have matching files inside
363 * @return array|int array of file_info instances or the count
364 */
365 private function get_filtered_children($extensions = '*', $countonly = false, $returnemptyfolders = false) {
366 $areas = array(
367 array('course', 'summary'),
368 array('course', 'section'),
369 array('backup', 'section'),
370 array('backup', 'course'),
371 array('backup', 'automated'),
372 array('course', 'legacy')
373 );
374 $children = array();
375 foreach ($areas as $area) {
376 if ($child = $this->get_file_info($area[0], $area[1], 0, '/', '.')) {
377 if ($returnemptyfolders || $child->count_non_empty_children($extensions)) {
378 $children[] = $child;
1ffbad64 379 if (($countonly !== false) && count($children) >= $countonly) {
ed7e6a73 380 return $countonly;
c84b7045
MG
381 }
382 }
383 }
64f93798
PS
384 }
385
e2652f1a
MG
386 if (!has_capability('moodle/course:managefiles', $this->context)) {
387 // 'managefiles' capability is checked in every activity module callback.
388 // Don't even waste time on retrieving the modules if we can't browse the files anyway
c84b7045
MG
389 } else {
390 // now list all modules
391 $modinfo = get_fast_modinfo($this->course);
392 foreach ($modinfo->cms as $cminfo) {
393 if (empty($cminfo->uservisible)) {
394 continue;
395 }
396 $modcontext = context_module::instance($cminfo->id, IGNORE_MISSING);
397 if ($child = $this->browser->get_file_info($modcontext)) {
398 if ($returnemptyfolders || $child->count_non_empty_children($extensions)) {
399 $children[] = $child;
1ffbad64 400 if (($countonly !== false) && count($children) >= $countonly) {
ed7e6a73 401 return $countonly;
c84b7045
MG
402 }
403 }
404 }
64f93798
PS
405 }
406 }
407
ed7e6a73 408 if ($countonly !== false) {
c84b7045
MG
409 return count($children);
410 }
64f93798
PS
411 return $children;
412 }
413
c84b7045
MG
414 /**
415 * Returns list of children which are either files matching the specified extensions
416 * or folders that contain at least one such file.
417 *
418 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
419 * @return array of file_info instances
420 */
421 public function get_non_empty_children($extensions = '*') {
422 return $this->get_filtered_children($extensions, false);
423 }
424
425 /**
426 * Returns the number of children which are either files matching the specified extensions
427 * or folders containing at least one such file.
428 *
c84b7045 429 * @param string|array $extensions, for example '*' or array('.gif','.jpg')
ed7e6a73 430 * @param int $limit stop counting after at least $limit non-empty children are found
c84b7045
MG
431 * @return int
432 */
ed7e6a73
MG
433 public function count_non_empty_children($extensions = '*', $limit = 1) {
434 return $this->get_filtered_children($extensions, $limit);
c84b7045
MG
435 }
436
64f93798
PS
437 /**
438 * Returns parent file_info instance
d2b7803e
DC
439 *
440 * @todo error checking if get_parent_contextid() returns false
64f93798
PS
441 * @return file_info or null for root
442 */
443 public function get_parent() {
444 //TODO: error checking if get_parent_contextid() returns false
445 $pcid = get_parent_contextid($this->context);
446 $parent = get_context_instance_by_id($pcid);
447 return $this->browser->get_file_info($parent);
448 }
449}
450
451
452/**
453 * Subclass of file_info_stored for files in the course files area.
454 *
d2b7803e
DC
455 * @package core_files
456 * @copyright 2008 Petr Skoda (http://skodak.org)
457 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
64f93798
PS
458 */
459class file_info_area_course_legacy extends file_info_stored {
d2b7803e
DC
460 /**
461 * Constructor
462 *
463 * @param file_browser $browser file browser instance
464 * @param stdClass $context context object
465 * @param stored_file $storedfile stored_file instance
466 */
64f93798
PS
467 public function __construct($browser, $context, $storedfile) {
468 global $CFG;
469 $urlbase = $CFG->wwwroot.'/file.php';
470 parent::__construct($browser, $context, $storedfile, $urlbase, get_string('coursefiles'), false, true, true, false);
471 }
472
473 /**
474 * Returns file download url
d2b7803e
DC
475 *
476 * @param bool $forcedownload whether or not force download
477 * @param bool $https whether or not force https
64f93798
PS
478 * @return string url
479 */
480 public function get_url($forcedownload=false, $https=false) {
64f93798
PS
481 if (!$this->is_readable()) {
482 return null;
483 }
484
485 if ($this->lf->is_directory()) {
486 return null;
487 }
488
489 $filepath = $this->lf->get_filepath();
490 $filename = $this->lf->get_filename();
491 $courseid = $this->context->instanceid;
492
493 $path = '/'.$courseid.$filepath.$filename;
494
495 return file_encode_url($this->urlbase, $path, $forcedownload, $https);
496 }
497
498 /**
499 * Returns list of children.
d2b7803e 500 *
64f93798
PS
501 * @return array of file_info instances
502 */
503 public function get_children() {
504 if (!$this->lf->is_directory()) {
505 return array();
506 }
507
508 $result = array();
509 $fs = get_file_storage();
510
511 $storedfiles = $fs->get_directory_files($this->context->id, 'course', 'legacy', 0, $this->lf->get_filepath(), false, true, "filepath ASC, filename ASC");
512 foreach ($storedfiles as $file) {
513 $result[] = new file_info_area_course_legacy($this->browser, $this->context, $file);
514 }
515
516 return $result;
517 }
c84b7045
MG
518
519 /**
520 * Returns list of children which are either files matching the specified extensions
521 * or folders that contain at least one such file.
522 *
523 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg')
524 * @return array of file_info instances
525 */
526 public function get_non_empty_children($extensions = '*') {
527 if (!$this->lf->is_directory()) {
528 return array();
529 }
530
531 $result = array();
532 $fs = get_file_storage();
533
534 $storedfiles = $fs->get_directory_files($this->context->id, 'course', 'legacy', 0,
535 $this->lf->get_filepath(), false, true, "filepath, filename");
536 foreach ($storedfiles as $file) {
ed7e6a73 537 $extension = textlib::strtolower(pathinfo($file->get_filename(), PATHINFO_EXTENSION));
c84b7045
MG
538 if ($file->is_directory() || $extensions === '*' || (!empty($extension) && in_array('.'.$extension, $extensions))) {
539 $fileinfo = new file_info_area_course_legacy($this->browser, $this->context, $file, $this->urlbase, $this->topvisiblename,
540 $this->itemidused, $this->readaccess, $this->writeaccess, false);
541 if (!$file->is_directory() || $fileinfo->count_non_empty_children($extensions)) {
542 $result[] = $fileinfo;
543 }
544 }
545 }
546
547 return $result;
548 }
64f93798
PS
549}
550
551/**
d2b7803e 552 * Represents a course category context in the tree navigated by {@link file_browser}.
64f93798 553 *
d2b7803e 554 * @package core_files
64f93798
PS
555 * @copyright 2008 Petr Skoda (http://skodak.org)
556 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
557 */
558class file_info_area_course_section extends file_info {
d2b7803e 559 /** @var stdClass course object */
64f93798 560 protected $course;
d2b7803e 561 /** @var file_info_context_course course file info object */
64f93798
PS
562 protected $courseinfo;
563
d2b7803e
DC
564 /**
565 * Constructor
566 *
567 * @param file_browser $browser file browser instance
568 * @param stdClass $context context object
569 * @param stdClass $course course object
570 * @param file_info_context_course $courseinfo file info instance
571 */
64f93798
PS
572 public function __construct($browser, $context, $course, file_info_context_course $courseinfo) {
573 parent::__construct($browser, $context);
574 $this->course = $course;
575 $this->courseinfo = $courseinfo;
576 }
577
578 /**
579 * Returns list of standard virtual file/directory identification.
580 * The difference from stored_file parameters is that null values
581 * are allowed in all fields
d2b7803e 582 *
64f93798
PS
583 * @return array with keys contextid, filearea, itemid, filepath and filename
584 */
585 public function get_params() {
586 return array('contextid' => $this->context->id,
587 'component' => 'course',
588 'filearea' => 'section',
589 'itemid' => null,
590 'filepath' => null,
591 'filename' => null);
592 }
593
594 /**
595 * Returns localised visible name.
d2b7803e 596 *
64f93798
PS
597 * @return string
598 */
599 public function get_visible_name() {
08b4939b 600 //$format = $this->course->format;
64f93798
PS
601 $sectionsname = get_string("coursesectionsummaries");
602
603 return $sectionsname;
604 }
605
606 /**
d2b7803e
DC
607 * Return whether or not new files or directories can be added
608 *
64f93798
PS
609 * @return bool
610 */
611 public function is_writable() {
612 return false;
613 }
614
16741cac 615 /**
d2b7803e 616 * Return whether or not this is a empty area
16741cac
PS
617 *
618 * @return bool
619 */
620 public function is_empty_area() {
621 $fs = get_file_storage();
622 return $fs->is_area_empty($this->context->id, 'course', 'section');
623 }
624
64f93798 625 /**
d2b7803e
DC
626 * Return whether or not this is a empty area
627 *
64f93798
PS
628 * @return bool
629 */
630 public function is_directory() {
631 return true;
632 }
633
634 /**
635 * Returns list of children.
d2b7803e 636 *
64f93798
PS
637 * @return array of file_info instances
638 */
639 public function get_children() {
640 global $DB;
641
642 $children = array();
643
644 $course_sections = $DB->get_records('course_sections', array('course'=>$this->course->id), 'section');
645 foreach ($course_sections as $section) {
646 if ($child = $this->courseinfo->get_file_info('course', 'section', $section->id, '/', '.')) {
647 $children[] = $child;
648 }
649 }
650
651 return $children;
652 }
653
c84b7045
MG
654 /**
655 * Returns the number of children which are either files matching the specified extensions
656 * or folders containing at least one such file.
657 *
c84b7045 658 * @param string|array $extensions, for example '*' or array('.gif','.jpg')
ed7e6a73 659 * @param int $limit stop counting after at least $limit non-empty children are found
c84b7045
MG
660 * @return int
661 */
ed7e6a73 662 public function count_non_empty_children($extensions = '*', $limit = 1) {
c84b7045
MG
663 global $DB;
664 $params1 = array(
665 'courseid' => $this->course->id,
666 'contextid' => $this->context->id,
667 'component' => 'course',
668 'filearea' => 'section',
669 'emptyfilename' => '.');
ed7e6a73 670 $sql1 = "SELECT DISTINCT cs.id FROM {files} f, {course_sections} cs
c84b7045
MG
671 WHERE cs.course = :courseid
672 AND f.contextid = :contextid
673 AND f.component = :component
674 AND f.filearea = :filearea
675 AND f.itemid = cs.id
676 AND f.filename <> :emptyfilename";
677 list($sql2, $params2) = $this->build_search_files_sql($extensions);
ed7e6a73
MG
678 $rs = $DB->get_recordset_sql($sql1. ' '. $sql2, array_merge($params1, $params2));
679 $cnt = 0;
680 foreach ($rs as $record) {
681 if ((++$cnt) >= $limit) {
682 break;
683 }
684 }
685 $rs->close();
686 return $cnt;
c84b7045
MG
687 }
688
64f93798
PS
689 /**
690 * Returns parent file_info instance
d2b7803e
DC
691 *
692 * @return file_info|null file_info or null for root
64f93798
PS
693 */
694 public function get_parent() {
695 return $this->courseinfo;
696 }
697}
698
699
700/**
701 * Implementation of course section backup area
702 *
d2b7803e 703 * @package core_files
64f93798
PS
704 * @copyright 2008 Petr Skoda (http://skodak.org)
705 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
706 */
707class file_info_area_backup_section extends file_info {
d2b7803e 708 /** @var stdClass course object */
64f93798 709 protected $course;
d2b7803e 710 /** @var file_info_context_course course file info object */
64f93798
PS
711 protected $courseinfo;
712
d2b7803e
DC
713 /**
714 * Constructor
715 *
716 * @param file_browser $browser file browser instance
717 * @param stdClass $context context object
718 * @param stdClass $course course object
719 * @param file_info_context_course $courseinfo file info instance
720 */
64f93798
PS
721 public function __construct($browser, $context, $course, file_info_context_course $courseinfo) {
722 parent::__construct($browser, $context);
723 $this->course = $course;
724 $this->courseinfo = $courseinfo;
725 }
726
727 /**
728 * Returns list of standard virtual file/directory identification.
729 * The difference from stored_file parameters is that null values
730 * are allowed in all fields
d2b7803e 731 *
64f93798
PS
732 * @return array with keys contextid, component, filearea, itemid, filepath and filename
733 */
734 public function get_params() {
735 return array('contextid' => $this->context->id,
736 'component' => 'backup',
737 'filearea' => 'section',
738 'itemid' => null,
739 'filepath' => null,
740 'filename' => null);
741 }
742
743 /**
744 * Returns localised visible name.
d2b7803e 745 *
64f93798
PS
746 * @return string
747 */
748 public function get_visible_name() {
08b4939b 749 return get_string('sectionbackup', 'repository');
64f93798
PS
750 }
751
752 /**
d2b7803e
DC
753 * Return whether or not new files and directories can be added
754 *
64f93798
PS
755 * @return bool
756 */
757 public function is_writable() {
758 return false;
759 }
760
16741cac 761 /**
d2b7803e 762 * Whether or not this is an empty area
16741cac
PS
763 *
764 * @return bool
765 */
766 public function is_empty_area() {
767 $fs = get_file_storage();
768 return $fs->is_area_empty($this->context->id, 'backup', 'section');
769 }
770
64f93798 771 /**
d2b7803e
DC
772 * Return whether or not this is a directory
773 *
64f93798
PS
774 * @return bool
775 */
776 public function is_directory() {
777 return true;
778 }
779
780 /**
781 * Returns list of children.
d2b7803e 782 *
64f93798
PS
783 * @return array of file_info instances
784 */
785 public function get_children() {
786 global $DB;
787
788 $children = array();
789
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('backup', 'section', $section->id, '/', '.')) {
793 $children[] = $child;
794 }
795 }
796
797 return $children;
798 }
799
c84b7045
MG
800 /**
801 * Returns the number of children which are either files matching the specified extensions
802 * or folders containing at least one such file.
803 *
c84b7045 804 * @param string|array $extensions, for example '*' or array('.gif','.jpg')
ed7e6a73 805 * @param int $limit stop counting after at least $limit non-empty children are found
c84b7045
MG
806 * @return int
807 */
ed7e6a73 808 public function count_non_empty_children($extensions = '*', $limit = 1) {
c84b7045
MG
809 global $DB;
810 $params1 = array(
811 'courseid' => $this->course->id,
812 'contextid' => $this->context->id,
813 'component' => 'backup',
814 'filearea' => 'section',
815 'emptyfilename' => '.');
c92a386b 816 $sql1 = "SELECT DISTINCT cs.id AS sectionid FROM {files} f, {course_sections} cs
c84b7045
MG
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);
ed7e6a73
MG
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;
829 }
830 }
831 $rs->close();
832 return $cnt;
c84b7045
MG
833 }
834
64f93798
PS
835 /**
836 * Returns parent file_info instance
d2b7803e 837 *
64f93798
PS
838 * @return file_info or null for root
839 */
840 public function get_parent() {
841 return $this->browser->get_file_info($this->context);
842 }
843}