PageRenderTime 33ms CodeModel.GetById 3ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 1ms

/lib/filebrowser/file_info_context_course.php

https://bitbucket.org/ngmares/moodle
PHP | 700 lines | 336 code | 94 blank | 270 comment | 56 complexity | 2d9ac47b524a3fd41ec5450be82150d7 MD5 | raw file
  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.
 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 *
 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
 24 */
 25
 26defined('MOODLE_INTERNAL') || die();
 27
 28/**
 29 * Represents a course context in the tree navigated by {@link file_browser}.
 30 *
 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
 34 */
 35class file_info_context_course extends file_info {
 36    /** @var stdClass course object */
 37    protected $course;
 38
 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     */
 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     *
 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
 60     */
 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;
 65        }
 66
 67        if (!$this->course->visible and !has_capability('moodle/course:viewhiddencourses', $this->context)) {
 68            return null;
 69        }
 70
 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
 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
 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
 95     * @return file_info|null file_info instance or null if not found or access not allowed
 96     */
 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
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
129     * @return file_info|null file_info instance or null if not found or access not allowed
130     */
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
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
169     * @return file_info|null file_info instance or null if not found or access not allowed
170     */
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
180        if (is_null($itemid)) {
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 === '.') {
190                $storedfile = new virtual_root_file($this->context->id, 'course', 'legacy', 0);
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
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
206     * @return file_info|null file_info instance or null if not found or access not allowed
207     */
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
238    /**
239     * Gets a stored file for the automated backup filearea directory
240     *
241     * @param int $itemid item ID
242     * @param string $filepath file path
243     * @param string $filename file name
244     * @return file_info|null
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
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
282     * @return file_info|null file_info instance or null if not found or access not allowed
283     */
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
320    /**
321     * Returns localised visible name.
322     *
323     * @return string
324     */
325    public function get_visible_name() {
326        return ($this->course->id == SITEID) ? get_string('frontpage', 'admin') : format_string($this->course->fullname, true, array('context'=>$this->context));
327    }
328
329    /**
330     * Whether or not new files or directories can be added
331     *
332     * @return bool
333     */
334    public function is_writable() {
335        return false;
336    }
337
338    /**
339     * Whether or not this is a directory
340     *
341     * @return bool
342     */
343    public function is_directory() {
344        return true;
345    }
346
347    /**
348     * Returns list of children.
349     *
350     * @return array of file_info instances
351     */
352    public function get_children() {
353        $children = array();
354
355        if ($child = $this->get_area_course_summary(0, '/', '.')) {
356            $children[] = $child;
357        }
358        if ($child = $this->get_area_course_section(null, null, null)) {
359            $children[] = $child;
360        }
361        if ($child = $this->get_area_backup_section(null, null, null)) {
362            $children[] = $child;
363        }
364        if ($child = $this->get_area_backup_course(0, '/', '.')) {
365            $children[] = $child;
366        }
367        if ($child = $this->get_area_backup_automated(0, '/', '.')) {
368            $children[] = $child;
369        }
370        if ($child = $this->get_area_course_legacy(0, '/', '.')) {
371            $children[] = $child;
372        }
373
374        if (!has_capability('moodle/course:managefiles', $this->context)) {
375            // 'managefiles' capability is checked in every activity module callback.
376            // Don't even waste time on retrieving the modules if we can't browse the files anyway
377            return $children;
378        }
379
380        // now list all modules
381        $modinfo = get_fast_modinfo($this->course);
382        foreach ($modinfo->cms as $cminfo) {
383            if (empty($cminfo->uservisible)) {
384                continue;
385            }
386            $modcontext = get_context_instance(CONTEXT_MODULE, $cminfo->id);
387            if ($child = $this->browser->get_file_info($modcontext)) {
388                $children[] = $child;
389            }
390        }
391
392        return $children;
393    }
394
395    /**
396     * Returns parent file_info instance
397     *
398     * @todo error checking if get_parent_contextid() returns false
399     * @return file_info or null for root
400     */
401    public function get_parent() {
402        //TODO: error checking if get_parent_contextid() returns false
403        $pcid = get_parent_contextid($this->context);
404        $parent = get_context_instance_by_id($pcid);
405        return $this->browser->get_file_info($parent);
406    }
407}
408
409
410/**
411 * Subclass of file_info_stored for files in the course files area.
412 *
413 * @package   core_files
414 * @copyright 2008 Petr Skoda (http://skodak.org)
415 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
416 */
417class file_info_area_course_legacy extends file_info_stored {
418    /**
419     * Constructor
420     *
421     * @param file_browser $browser file browser instance
422     * @param stdClass $context context object
423     * @param stored_file $storedfile stored_file instance
424     */
425    public function __construct($browser, $context, $storedfile) {
426        global $CFG;
427        $urlbase = $CFG->wwwroot.'/file.php';
428        parent::__construct($browser, $context, $storedfile, $urlbase, get_string('coursefiles'), false, true, true, false);
429    }
430
431    /**
432     * Returns file download url
433     *
434     * @param bool $forcedownload whether or not force download
435     * @param bool $https whether or not force https
436     * @return string url
437     */
438    public function get_url($forcedownload=false, $https=false) {
439        if (!$this->is_readable()) {
440            return null;
441        }
442
443        if ($this->lf->is_directory()) {
444            return null;
445        }
446
447        $filepath = $this->lf->get_filepath();
448        $filename = $this->lf->get_filename();
449        $courseid = $this->context->instanceid;
450
451        $path = '/'.$courseid.$filepath.$filename;
452
453        return file_encode_url($this->urlbase, $path, $forcedownload, $https);
454    }
455
456    /**
457     * Returns list of children.
458     *
459     * @return array of file_info instances
460     */
461    public function get_children() {
462        if (!$this->lf->is_directory()) {
463            return array();
464        }
465
466        $result = array();
467        $fs = get_file_storage();
468
469        $storedfiles = $fs->get_directory_files($this->context->id, 'course', 'legacy', 0, $this->lf->get_filepath(), false, true, "filepath ASC, filename ASC");
470        foreach ($storedfiles as $file) {
471            $result[] = new file_info_area_course_legacy($this->browser, $this->context, $file);
472        }
473
474        return $result;
475    }
476}
477
478/**
479 * Represents a course category context in the tree navigated by {@link file_browser}.
480 *
481 * @package    core_files
482 * @copyright  2008 Petr Skoda (http://skodak.org)
483 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
484 */
485class file_info_area_course_section extends file_info {
486    /** @var stdClass course object */
487    protected $course;
488    /** @var file_info_context_course course file info object */
489    protected $courseinfo;
490
491    /**
492     * Constructor
493     *
494     * @param file_browser $browser file browser instance
495     * @param stdClass $context context object
496     * @param stdClass $course course object
497     * @param file_info_context_course $courseinfo file info instance
498     */
499    public function __construct($browser, $context, $course, file_info_context_course $courseinfo) {
500        parent::__construct($browser, $context);
501        $this->course     = $course;
502        $this->courseinfo = $courseinfo;
503    }
504
505    /**
506     * Returns list of standard virtual file/directory identification.
507     * The difference from stored_file parameters is that null values
508     * are allowed in all fields
509     *
510     * @return array with keys contextid, filearea, itemid, filepath and filename
511     */
512    public function get_params() {
513        return array('contextid' => $this->context->id,
514                     'component' => 'course',
515                     'filearea'  => 'section',
516                     'itemid'    => null,
517                     'filepath'  => null,
518                     'filename'  => null);
519    }
520
521    /**
522     * Returns localised visible name.
523     *
524     * @return string
525     */
526    public function get_visible_name() {
527        //$format = $this->course->format;
528        $sectionsname = get_string("coursesectionsummaries");
529
530        return $sectionsname;
531    }
532
533    /**
534     * Return whether or not new files or directories can be added
535     *
536     * @return bool
537     */
538    public function is_writable() {
539        return false;
540    }
541
542    /**
543     * Return whether or not this is a empty area
544     *
545     * @return bool
546     */
547    public function is_empty_area() {
548        $fs = get_file_storage();
549        return $fs->is_area_empty($this->context->id, 'course', 'section');
550    }
551
552    /**
553     * Return whether or not this is a empty area
554     *
555     * @return bool
556     */
557    public function is_directory() {
558        return true;
559    }
560
561    /**
562     * Returns list of children.
563     *
564     * @return array of file_info instances
565     */
566    public function get_children() {
567        global $DB;
568
569        $children = array();
570
571        $course_sections = $DB->get_records('course_sections', array('course'=>$this->course->id), 'section');
572        foreach ($course_sections as $section) {
573            if ($child = $this->courseinfo->get_file_info('course', 'section', $section->id, '/', '.')) {
574                $children[] = $child;
575            }
576        }
577
578        return $children;
579    }
580
581    /**
582     * Returns parent file_info instance
583     *
584     * @return file_info|null file_info or null for root
585     */
586    public function get_parent() {
587        return $this->courseinfo;
588    }
589}
590
591
592/**
593 * Implementation of course section backup area
594 *
595 * @package    core_files
596 * @copyright  2008 Petr Skoda (http://skodak.org)
597 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
598 */
599class file_info_area_backup_section extends file_info {
600    /** @var stdClass course object */
601    protected $course;
602    /** @var file_info_context_course course file info object */
603    protected $courseinfo;
604
605    /**
606     * Constructor
607     *
608     * @param file_browser $browser file browser instance
609     * @param stdClass $context context object
610     * @param stdClass $course course object
611     * @param file_info_context_course $courseinfo file info instance
612     */
613    public function __construct($browser, $context, $course, file_info_context_course $courseinfo) {
614        parent::__construct($browser, $context);
615        $this->course     = $course;
616        $this->courseinfo = $courseinfo;
617    }
618
619    /**
620     * Returns list of standard virtual file/directory identification.
621     * The difference from stored_file parameters is that null values
622     * are allowed in all fields
623     *
624     * @return array with keys contextid, component, filearea, itemid, filepath and filename
625     */
626    public function get_params() {
627        return array('contextid' => $this->context->id,
628                     'component' => 'backup',
629                     'filearea'  => 'section',
630                     'itemid'    => null,
631                     'filepath'  => null,
632                     'filename'  => null);
633    }
634
635    /**
636     * Returns localised visible name.
637     *
638     * @return string
639     */
640    public function get_visible_name() {
641        return get_string('sectionbackup', 'repository');
642    }
643
644    /**
645     * Return whether or not new files and directories can be added
646     *
647     * @return bool
648     */
649    public function is_writable() {
650        return false;
651    }
652
653    /**
654     * Whether or not this is an empty area
655     *
656     * @return bool
657     */
658    public function is_empty_area() {
659        $fs = get_file_storage();
660        return $fs->is_area_empty($this->context->id, 'backup', 'section');
661    }
662
663    /**
664     * Return whether or not this is a directory
665     *
666     * @return bool
667     */
668    public function is_directory() {
669        return true;
670    }
671
672    /**
673     * Returns list of children.
674     *
675     * @return array of file_info instances
676     */
677    public function get_children() {
678        global $DB;
679
680        $children = array();
681
682        $course_sections = $DB->get_records('course_sections', array('course'=>$this->course->id), 'section');
683        foreach ($course_sections as $section) {
684            if ($child = $this->courseinfo->get_file_info('backup', 'section', $section->id, '/', '.')) {
685                $children[] = $child;
686            }
687        }
688
689        return $children;
690    }
691
692    /**
693     * Returns parent file_info instance
694     *
695     * @return file_info or null for root
696     */
697    public function get_parent() {
698        return $this->browser->get_file_info($this->context);
699    }
700}