MDL-23156 incorrect itemid test in legacy browser
[moodle.git] / lib / filebrowser / file_info_context_course.php
CommitLineData
64f93798
PS
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18
19/**
20 * Utility class for browsing of course files.
21 *
22 * @package core
23 * @subpackage filebrowser
24 * @copyright 2008 Petr Skoda (http://skodak.org)
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28defined('MOODLE_INTERNAL') || die();
29
30/**
31 * Represents a course context in the tree navigated by @see{file_browser}.
32 *
33 * @package core
34 * @subpackage filebrowser
35 * @copyright 2008 Petr Skoda (http://skodak.org)
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class file_info_context_course extends file_info {
39 protected $course;
40
41 public function __construct($browser, $context, $course) {
42 parent::__construct($browser, $context);
43 $this->course = $course;
44 }
45
46 /**
47 * Return information about this specific context level
48 *
49 * @param $component
50 * @param $filearea
51 * @param $itemid
52 * @param $filepath
53 * @param $filename
54 */
55 public function get_file_info($component, $filearea, $itemid, $filepath, $filename) {
56 global $DB;
57
58 if (!$this->course->visible and !has_capability('moodle/course:viewhiddencourses', $this->context)) {
59 return null;
60 }
61
62 if (empty($component)) {
63 return $this;
64 }
65
66 $methodname = "get_area_{$component}_{$filearea}";
67
68 if (method_exists($this, $methodname)) {
69 return $this->$methodname($itemid, $filepath, $filename);
70 }
71
72 return null;
73 }
74
75 protected function get_area_course_summary($itemid, $filepath, $filename) {
76 global $CFG;
77
78 if (!has_capability('moodle/course:update', $this->context)) {
79 return null;
80 }
81 if (is_null($itemid)) {
82 return $this;
83 }
84
85 $fs = get_file_storage();
86
87 $filepath = is_null($filepath) ? '/' : $filepath;
88 $filename = is_null($filename) ? '.' : $filename;
89 if (!$storedfile = $fs->get_file($this->context->id, 'course', 'summary', 0, $filepath, $filename)) {
90 if ($filepath === '/' and $filename === '.') {
91 $storedfile = new virtual_root_file($this->context->id, 'course', 'summary', 0);
92 } else {
93 // not found
94 return null;
95 }
96 }
97 $urlbase = $CFG->wwwroot.'/pluginfile.php';
98 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('areacourseintro', 'repository'), false, true, true, false);
99 }
100
101
102 protected function get_area_course_section($itemid, $filepath, $filename) {
103 global $CFG, $DB;
104
105 if (!has_capability('moodle/course:update', $this->context)) {
106 return null;
107 }
108
109 if (empty($itemid)) {
110 // list all sections
111 return new file_info_area_course_section($this->browser, $this->context, $this->course, $this);
112 }
113
114 if (!$section = $DB->get_record('course_sections', array('course'=>$this->course->id, 'id'=>$itemid))) {
115 return null; // does not exist
116 }
117
118 $fs = get_file_storage();
119
120 $filepath = is_null($filepath) ? '/' : $filepath;
121 $filename = is_null($filename) ? '.' : $filename;
122 if (!$storedfile = $fs->get_file($this->context->id, 'course', 'section', $itemid, $filepath, $filename)) {
123 if ($filepath === '/' and $filename === '.') {
124 $storedfile = new virtual_root_file($this->context->id, 'course', 'section', $itemid);
125 } else {
126 // not found
127 return null;
128 }
129 }
130 $urlbase = $CFG->wwwroot.'/pluginfile.php';
131 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, $section->section, true, true, true, false);
132 }
133
134
135 protected function get_area_course_legacy($itemid, $filepath, $filename) {
136 if (!has_capability('moodle/course:managefiles', $this->context)) {
137 return null;
138 }
139
140 if ($this->course->id != SITEID and $this->course->legacyfiles != 2) {
141 // bad luck, legacy course files not used any more
142 }
143
d4848e8d 144 if (is_null($itemid)) {
64f93798
PS
145 return $this;
146 }
147
148 $fs = get_file_storage();
149
150 $filepath = is_null($filepath) ? '/' : $filepath;
151 $filename = is_null($filename) ? '.' : $filename;
152 if (!$storedfile = $fs->get_file($this->context->id, 'course', 'legacy', 0, $filepath, $filename)) {
153 if ($filepath === '/' and $filename === '.') {
154 $storedfile = new virtual_root_file($this->context->id, $filearea, 0);
155 } else {
156 // not found
157 return null;
158 }
159 }
160
161 return new file_info_area_course_legacy($this->browser, $this->context, $storedfile);
162 }
163
164 protected function get_area_backup_course($itemid, $filepath, $filename) {
165 global $CFG;
166
167 if (!has_capability('moodle/backup:backupcourse', $this->context) and !has_capability('moodle/restore:restorecourse', $this->context)) {
168 return null;
169 }
170 if (is_null($itemid)) {
171 return $this;
172 }
173
174 $fs = get_file_storage();
175
176 $filepath = is_null($filepath) ? '/' : $filepath;
177 $filename = is_null($filename) ? '.' : $filename;
178 if (!$storedfile = $fs->get_file($this->context->id, 'backup', 'course', 0, $filepath, $filename)) {
179 if ($filepath === '/' and $filename === '.') {
180 $storedfile = new virtual_root_file($this->context->id, 'backup', 'course', 0);
181 } else {
182 // not found
183 return null;
184 }
185 }
186
187 $downloadable = has_capability('moodle/backup:downloadfile', $this->context);
188 $uploadable = has_capability('moodle/restore:uploadfile', $this->context);
189
190 $urlbase = $CFG->wwwroot.'/pluginfile.php';
191 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('coursebackup', 'repository'), false, $downloadable, $uploadable, false);
192 }
193
194 protected function get_area_backup_section($itemid, $filepath, $filename) {
195 global $CFG, $DB;
196
197 if (!has_capability('moodle/backup:backupcourse', $this->context) and !has_capability('moodle/restore:restorecourse', $this->context)) {
198 return null;
199 }
200
201 if (empty($itemid)) {
202 // list all sections
203 return new file_info_area_backup_section($this->browser, $this->context, $this->course, $this);
204 }
205
206 if (!$section = $DB->get_record('course_sections', array('course'=>$this->course->id, 'id'=>$itemid))) {
207 return null; // does not exist
208 }
209
210 $fs = get_file_storage();
211
212 $filepath = is_null($filepath) ? '/' : $filepath;
213 $filename = is_null($filename) ? '.' : $filename;
214 if (!$storedfile = $fs->get_file($this->context->id, 'backup', 'section', $itemid, $filepath, $filename)) {
215 if ($filepath === '/' and $filename === '.') {
216 $storedfile = new virtual_root_file($this->context->id, 'backup', 'section', $itemid);
217 } else {
218 // not found
219 return null;
220 }
221 }
222
223 $downloadable = has_capability('moodle/backup:downloadfile', $this->context);
224 $uploadable = has_capability('moodle/restore:uploadfile', $this->context);
225
226 $urlbase = $CFG->wwwroot.'/pluginfile.php';
227 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, $section->id, true, $downloadable, $uploadable, false);
228 }
229
230 public function get_visible_name() {
231 return ($this->course->id == SITEID) ? get_string('frontpage', 'admin') : format_string($this->course->fullname);
232 }
233
234 /**
235 * Can I add new files or directories?
236 * @return bool
237 */
238 public function is_writable() {
239 return false;
240 }
241
242 /**
243 * Is directory?
244 * @return bool
245 */
246 public function is_directory() {
247 return true;
248 }
249
250 /**
251 * Returns list of children.
252 * @return array of file_info instances
253 */
254 public function get_children() {
255 $children = array();
256
257 if ($child = $this->get_area_course_summary(0, '/', '.')) {
258 $children[] = $child;
259 }
260 if ($child = $this->get_area_course_section(null, null, null)) {
261 $children[] = $child;
262 }
263 if ($child = $this->get_area_backup_section(null, null, null)) {
264 $children[] = $child;
265 }
266 if ($child = $this->get_area_backup_course(0, '/', '.')) {
267 $children[] = $child;
268 }
269 if ($child = $this->get_area_course_legacy(0, '/', '.')) {
270 $children[] = $child;
271 }
272
273 // now list all modules
274 $modinfo = get_fast_modinfo($this->course);
275 foreach ($modinfo->cms as $cminfo) {
276 if (empty($cminfo->uservisible)) {
277 continue;
278 }
279 $modcontext = get_context_instance(CONTEXT_MODULE, $cminfo->id);
280 if ($child = $this->browser->get_file_info($modcontext)) {
281 $children[] = $child;
282 }
283 }
284
285 return $children;
286 }
287
288 /**
289 * Returns parent file_info instance
290 * @return file_info or null for root
291 */
292 public function get_parent() {
293 //TODO: error checking if get_parent_contextid() returns false
294 $pcid = get_parent_contextid($this->context);
295 $parent = get_context_instance_by_id($pcid);
296 return $this->browser->get_file_info($parent);
297 }
298}
299
300
301/**
302 * Subclass of file_info_stored for files in the course files area.
303 *
304 * @package core
305 * @subpackage filebrowser
306 * @copyright 2008 Petr Skoda (http://skodak.org)
307 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
308 */
309class file_info_area_course_legacy extends file_info_stored {
310 public function __construct($browser, $context, $storedfile) {
311 global $CFG;
312 $urlbase = $CFG->wwwroot.'/file.php';
313 parent::__construct($browser, $context, $storedfile, $urlbase, get_string('coursefiles'), false, true, true, false);
314 }
315
316 /**
317 * Returns file download url
318 * @param bool $forcedownload
319 * @param bool $htts force https
320 * @return string url
321 */
322 public function get_url($forcedownload=false, $https=false) {
323 global $CFG;
324
325 if (!$this->is_readable()) {
326 return null;
327 }
328
329 if ($this->lf->is_directory()) {
330 return null;
331 }
332
333 $filepath = $this->lf->get_filepath();
334 $filename = $this->lf->get_filename();
335 $courseid = $this->context->instanceid;
336
337 $path = '/'.$courseid.$filepath.$filename;
338
339 return file_encode_url($this->urlbase, $path, $forcedownload, $https);
340 }
341
342 /**
343 * Returns list of children.
344 * @return array of file_info instances
345 */
346 public function get_children() {
347 if (!$this->lf->is_directory()) {
348 return array();
349 }
350
351 $result = array();
352 $fs = get_file_storage();
353
354 $storedfiles = $fs->get_directory_files($this->context->id, 'course', 'legacy', 0, $this->lf->get_filepath(), false, true, "filepath ASC, filename ASC");
355 foreach ($storedfiles as $file) {
356 $result[] = new file_info_area_course_legacy($this->browser, $this->context, $file);
357 }
358
359 return $result;
360 }
361}
362
363/**
364 * Represents a course category context in the tree navigated by @see{file_browser}.
365 *
366 * @package core
367 * @subpackage filebrowser
368 * @copyright 2008 Petr Skoda (http://skodak.org)
369 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
370 */
371class file_info_area_course_section extends file_info {
372 protected $course;
373 protected $courseinfo;
374
375 public function __construct($browser, $context, $course, file_info_context_course $courseinfo) {
376 parent::__construct($browser, $context);
377 $this->course = $course;
378 $this->courseinfo = $courseinfo;
379 }
380
381 /**
382 * Returns list of standard virtual file/directory identification.
383 * The difference from stored_file parameters is that null values
384 * are allowed in all fields
385 * @return array with keys contextid, filearea, itemid, filepath and filename
386 */
387 public function get_params() {
388 return array('contextid' => $this->context->id,
389 'component' => 'course',
390 'filearea' => 'section',
391 'itemid' => null,
392 'filepath' => null,
393 'filename' => null);
394 }
395
396 /**
397 * Returns localised visible name.
398 * @return string
399 */
400 public function get_visible_name() {
401 $format = $this->course->format;
402 $sectionsname = get_string("coursesectionsummaries");
403
404 return $sectionsname;
405 }
406
407 /**
408 * Can I add new files or directories?
409 * @return bool
410 */
411 public function is_writable() {
412 return false;
413 }
414
415 /**
416 * Is directory?
417 * @return bool
418 */
419 public function is_directory() {
420 return true;
421 }
422
423 /**
424 * Returns list of children.
425 * @return array of file_info instances
426 */
427 public function get_children() {
428 global $DB;
429
430 $children = array();
431
432 $course_sections = $DB->get_records('course_sections', array('course'=>$this->course->id), 'section');
433 foreach ($course_sections as $section) {
434 if ($child = $this->courseinfo->get_file_info('course', 'section', $section->id, '/', '.')) {
435 $children[] = $child;
436 }
437 }
438
439 return $children;
440 }
441
442 /**
443 * Returns parent file_info instance
444 * @return file_info or null for root
445 */
446 public function get_parent() {
447 return $this->courseinfo;
448 }
449}
450
451
452/**
453 * Implementation of course section backup area
454 *
455 * @package core
456 * @subpackage filebrowser
457 * @copyright 2008 Petr Skoda (http://skodak.org)
458 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
459 */
460class file_info_area_backup_section extends file_info {
461 protected $course;
462 protected $courseinfo;
463
464 public function __construct($browser, $context, $course, file_info_context_course $courseinfo) {
465 parent::__construct($browser, $context);
466 $this->course = $course;
467 $this->courseinfo = $courseinfo;
468 }
469
470 /**
471 * Returns list of standard virtual file/directory identification.
472 * The difference from stored_file parameters is that null values
473 * are allowed in all fields
474 * @return array with keys contextid, component, filearea, itemid, filepath and filename
475 */
476 public function get_params() {
477 return array('contextid' => $this->context->id,
478 'component' => 'backup',
479 'filearea' => 'section',
480 'itemid' => null,
481 'filepath' => null,
482 'filename' => null);
483 }
484
485 /**
486 * Returns localised visible name.
487 * @return string
488 */
489 public function get_visible_name() {
490 $format = $this->course->format;
491 $sectionsname = get_string('sectionbackup', 'repository');
492
493 return $sectionsname;
494 }
495
496 /**
497 * Can I add new files or directories?
498 * @return bool
499 */
500 public function is_writable() {
501 return false;
502 }
503
504 /**
505 * Is directory?
506 * @return bool
507 */
508 public function is_directory() {
509 return true;
510 }
511
512 /**
513 * Returns list of children.
514 * @return array of file_info instances
515 */
516 public function get_children() {
517 global $DB;
518
519 $children = array();
520
521 $course_sections = $DB->get_records('course_sections', array('course'=>$this->course->id), 'section');
522 foreach ($course_sections as $section) {
523 if ($child = $this->courseinfo->get_file_info('backup', 'section', $section->id, '/', '.')) {
524 $children[] = $child;
525 }
526 }
527
528 return $children;
529 }
530
531 /**
532 * Returns parent file_info instance
533 * @return file_info or null for root
534 */
535 public function get_parent() {
536 return $this->browser->get_file_info($this->context);
537 }
538}
539
540