Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/filebrowser/file_info_context_course.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00018 
00028 defined('MOODLE_INTERNAL') || die();
00029 
00038 class file_info_context_course extends file_info {
00039     protected $course;
00040 
00041     public function __construct($browser, $context, $course) {
00042         parent::__construct($browser, $context);
00043         $this->course   = $course;
00044     }
00045 
00055     public function get_file_info($component, $filearea, $itemid, $filepath, $filename) {
00056         // try to emulate require_login() tests here
00057         if (!isloggedin()) {
00058             return null;
00059         }
00060 
00061         if (!$this->course->visible and !has_capability('moodle/course:viewhiddencourses', $this->context)) {
00062             return null;
00063         }
00064 
00065         if (!is_viewing($this->context) and !is_enrolled($this->context)) {
00066             // no peaking here if not enrolled or inspector
00067             return null;
00068         }
00069 
00070         if (empty($component)) {
00071             return $this;
00072         }
00073 
00074         $methodname = "get_area_{$component}_{$filearea}";
00075 
00076         if (method_exists($this, $methodname)) {
00077             return $this->$methodname($itemid, $filepath, $filename);
00078         }
00079 
00080         return null;
00081     }
00082 
00083     protected function get_area_course_summary($itemid, $filepath, $filename) {
00084         global $CFG;
00085 
00086         if (!has_capability('moodle/course:update', $this->context)) {
00087             return null;
00088         }
00089         if (is_null($itemid)) {
00090             return $this;
00091         }
00092 
00093         $fs = get_file_storage();
00094 
00095         $filepath = is_null($filepath) ? '/' : $filepath;
00096         $filename = is_null($filename) ? '.' : $filename;
00097         if (!$storedfile = $fs->get_file($this->context->id, 'course', 'summary', 0, $filepath, $filename)) {
00098             if ($filepath === '/' and $filename === '.') {
00099                 $storedfile = new virtual_root_file($this->context->id, 'course', 'summary', 0);
00100             } else {
00101                 // not found
00102                 return null;
00103             }
00104         }
00105         $urlbase = $CFG->wwwroot.'/pluginfile.php';
00106         return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('areacourseintro', 'repository'), false, true, true, false);
00107     }
00108 
00109 
00110     protected function get_area_course_section($itemid, $filepath, $filename) {
00111         global $CFG, $DB;
00112 
00113         if (!has_capability('moodle/course:update', $this->context)) {
00114             return null;
00115         }
00116 
00117         if (empty($itemid)) {
00118             // list all sections
00119             return new file_info_area_course_section($this->browser, $this->context, $this->course, $this);
00120         }
00121 
00122         if (!$section = $DB->get_record('course_sections', array('course'=>$this->course->id, 'id'=>$itemid))) {
00123             return null; // does not exist
00124         }
00125 
00126         $fs = get_file_storage();
00127 
00128         $filepath = is_null($filepath) ? '/' : $filepath;
00129         $filename = is_null($filename) ? '.' : $filename;
00130         if (!$storedfile = $fs->get_file($this->context->id, 'course', 'section', $itemid, $filepath, $filename)) {
00131             if ($filepath === '/' and $filename === '.') {
00132                 $storedfile = new virtual_root_file($this->context->id, 'course', 'section', $itemid);
00133             } else {
00134                 // not found
00135                 return null;
00136             }
00137         }
00138         $urlbase = $CFG->wwwroot.'/pluginfile.php';
00139         return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, $section->section, true, true, true, false);
00140     }
00141 
00142 
00143     protected function get_area_course_legacy($itemid, $filepath, $filename) {
00144         if (!has_capability('moodle/course:managefiles', $this->context)) {
00145             return null;
00146         }
00147 
00148         if ($this->course->id != SITEID and $this->course->legacyfiles != 2) {
00149             // bad luck, legacy course files not used any more
00150         }
00151 
00152         if (is_null($itemid)) {
00153             return $this;
00154         }
00155 
00156         $fs = get_file_storage();
00157 
00158         $filepath = is_null($filepath) ? '/' : $filepath;
00159         $filename = is_null($filename) ? '.' : $filename;
00160         if (!$storedfile = $fs->get_file($this->context->id, 'course', 'legacy', 0, $filepath, $filename)) {
00161             if ($filepath === '/' and $filename === '.') {
00162                 $storedfile = new virtual_root_file($this->context->id, 'course', 'legacy', 0);
00163             } else {
00164                 // not found
00165                 return null;
00166             }
00167         }
00168 
00169         return new file_info_area_course_legacy($this->browser, $this->context, $storedfile);
00170     }
00171 
00172     protected function get_area_backup_course($itemid, $filepath, $filename) {
00173         global $CFG;
00174 
00175         if (!has_capability('moodle/backup:backupcourse', $this->context) and !has_capability('moodle/restore:restorecourse', $this->context)) {
00176             return null;
00177         }
00178         if (is_null($itemid)) {
00179             return $this;
00180         }
00181 
00182         $fs = get_file_storage();
00183 
00184         $filepath = is_null($filepath) ? '/' : $filepath;
00185         $filename = is_null($filename) ? '.' : $filename;
00186         if (!$storedfile = $fs->get_file($this->context->id, 'backup', 'course', 0, $filepath, $filename)) {
00187             if ($filepath === '/' and $filename === '.') {
00188                 $storedfile = new virtual_root_file($this->context->id, 'backup', 'course', 0);
00189             } else {
00190                 // not found
00191                 return null;
00192             }
00193         }
00194 
00195         $downloadable = has_capability('moodle/backup:downloadfile', $this->context);
00196         $uploadable   = has_capability('moodle/restore:uploadfile', $this->context);
00197 
00198         $urlbase = $CFG->wwwroot.'/pluginfile.php';
00199         return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('coursebackup', 'repository'), false, $downloadable, $uploadable, false);
00200     }
00201 
00210     protected function get_area_backup_automated($itemid, $filepath, $filename) {
00211         global $CFG;
00212 
00213         if (!has_capability('moodle/restore:viewautomatedfilearea', $this->context)) {
00214             return null;
00215         }
00216         if (is_null($itemid)) {
00217             return $this;
00218         }
00219 
00220         $fs = get_file_storage();
00221 
00222         $filepath = is_null($filepath) ? '/' : $filepath;
00223         $filename = is_null($filename) ? '.' : $filename;
00224         if (!$storedfile = $fs->get_file($this->context->id, 'backup', 'automated', 0, $filepath, $filename)) {
00225             if ($filepath === '/' and $filename === '.') {
00226                 $storedfile = new virtual_root_file($this->context->id, 'backup', 'automated', 0);
00227             } else {
00228                 // not found
00229                 return null;
00230             }
00231         }
00232 
00233         $downloadable = has_capability('moodle/site:config', $this->context);
00234         $uploadable   = false;
00235 
00236         $urlbase = $CFG->wwwroot.'/pluginfile.php';
00237         return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('automatedbackup', 'repository'), true, $downloadable, $uploadable, false);
00238     }
00239 
00240     protected function get_area_backup_section($itemid, $filepath, $filename) {
00241         global $CFG, $DB;
00242 
00243         if (!has_capability('moodle/backup:backupcourse', $this->context) and !has_capability('moodle/restore:restorecourse', $this->context)) {
00244             return null;
00245         }
00246 
00247         if (empty($itemid)) {
00248             // list all sections
00249             return new file_info_area_backup_section($this->browser, $this->context, $this->course, $this);
00250         }
00251 
00252         if (!$section = $DB->get_record('course_sections', array('course'=>$this->course->id, 'id'=>$itemid))) {
00253             return null; // does not exist
00254         }
00255 
00256         $fs = get_file_storage();
00257 
00258         $filepath = is_null($filepath) ? '/' : $filepath;
00259         $filename = is_null($filename) ? '.' : $filename;
00260         if (!$storedfile = $fs->get_file($this->context->id, 'backup', 'section', $itemid, $filepath, $filename)) {
00261             if ($filepath === '/' and $filename === '.') {
00262                 $storedfile = new virtual_root_file($this->context->id, 'backup', 'section', $itemid);
00263             } else {
00264                 // not found
00265                 return null;
00266             }
00267         }
00268 
00269         $downloadable = has_capability('moodle/backup:downloadfile', $this->context);
00270         $uploadable   = has_capability('moodle/restore:uploadfile', $this->context);
00271 
00272         $urlbase = $CFG->wwwroot.'/pluginfile.php';
00273         return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, $section->id, true, $downloadable, $uploadable, false);
00274     }
00275 
00276     public function get_visible_name() {
00277         return ($this->course->id == SITEID) ? get_string('frontpage', 'admin') : format_string($this->course->fullname, true, array('context'=>$this->context));
00278     }
00279 
00284     public function is_writable() {
00285         return false;
00286     }
00287 
00292     public function is_directory() {
00293         return true;
00294     }
00295 
00300     public function get_children() {
00301         $children = array();
00302 
00303         if ($child = $this->get_area_course_summary(0, '/', '.')) {
00304             $children[] = $child;
00305         }
00306         if ($child = $this->get_area_course_section(null, null, null)) {
00307             $children[] = $child;
00308         }
00309         if ($child = $this->get_area_backup_section(null, null, null)) {
00310             $children[] = $child;
00311         }
00312         if ($child = $this->get_area_backup_course(0, '/', '.')) {
00313             $children[] = $child;
00314         }
00315         if ($child = $this->get_area_backup_automated(0, '/', '.')) {
00316             $children[] = $child;
00317         }
00318         if ($child = $this->get_area_course_legacy(0, '/', '.')) {
00319             $children[] = $child;
00320         }
00321 
00322         // now list all modules
00323         $modinfo = get_fast_modinfo($this->course);
00324         foreach ($modinfo->cms as $cminfo) {
00325             if (empty($cminfo->uservisible)) {
00326                 continue;
00327             }
00328             $modcontext = get_context_instance(CONTEXT_MODULE, $cminfo->id);
00329             if ($child = $this->browser->get_file_info($modcontext)) {
00330                 $children[] = $child;
00331             }
00332         }
00333 
00334         return $children;
00335     }
00336 
00341     public function get_parent() {
00342         //TODO: error checking if get_parent_contextid() returns false
00343         $pcid = get_parent_contextid($this->context);
00344         $parent = get_context_instance_by_id($pcid);
00345         return $this->browser->get_file_info($parent);
00346     }
00347 }
00348 
00349 
00358 class file_info_area_course_legacy extends file_info_stored {
00359     public function __construct($browser, $context, $storedfile) {
00360         global $CFG;
00361         $urlbase = $CFG->wwwroot.'/file.php';
00362         parent::__construct($browser, $context, $storedfile, $urlbase, get_string('coursefiles'), false, true, true, false);
00363     }
00364 
00371     public function get_url($forcedownload=false, $https=false) {
00372         if (!$this->is_readable()) {
00373             return null;
00374         }
00375 
00376         if ($this->lf->is_directory()) {
00377             return null;
00378         }
00379 
00380         $filepath = $this->lf->get_filepath();
00381         $filename = $this->lf->get_filename();
00382         $courseid = $this->context->instanceid;
00383 
00384         $path = '/'.$courseid.$filepath.$filename;
00385 
00386         return file_encode_url($this->urlbase, $path, $forcedownload, $https);
00387     }
00388 
00393     public function get_children() {
00394         if (!$this->lf->is_directory()) {
00395             return array();
00396         }
00397 
00398         $result = array();
00399         $fs = get_file_storage();
00400 
00401         $storedfiles = $fs->get_directory_files($this->context->id, 'course', 'legacy', 0, $this->lf->get_filepath(), false, true, "filepath ASC, filename ASC");
00402         foreach ($storedfiles as $file) {
00403             $result[] = new file_info_area_course_legacy($this->browser, $this->context, $file);
00404         }
00405 
00406         return $result;
00407     }
00408 }
00409 
00418 class file_info_area_course_section extends file_info {
00419     protected $course;
00420     protected $courseinfo;
00421 
00422     public function __construct($browser, $context, $course, file_info_context_course $courseinfo) {
00423         parent::__construct($browser, $context);
00424         $this->course     = $course;
00425         $this->courseinfo = $courseinfo;
00426     }
00427 
00434     public function get_params() {
00435         return array('contextid' => $this->context->id,
00436                      'component' => 'course',
00437                      'filearea'  => 'section',
00438                      'itemid'    => null,
00439                      'filepath'  => null,
00440                      'filename'  => null);
00441     }
00442 
00447     public function get_visible_name() {
00448         //$format = $this->course->format;
00449         $sectionsname = get_string("coursesectionsummaries");
00450 
00451         return $sectionsname;
00452     }
00453 
00458     public function is_writable() {
00459         return false;
00460     }
00461 
00467     public function is_empty_area() {
00468         $fs = get_file_storage();
00469         return $fs->is_area_empty($this->context->id, 'course', 'section');
00470     }
00471 
00476     public function is_directory() {
00477         return true;
00478     }
00479 
00484     public function get_children() {
00485         global $DB;
00486 
00487         $children = array();
00488 
00489         $course_sections = $DB->get_records('course_sections', array('course'=>$this->course->id), 'section');
00490         foreach ($course_sections as $section) {
00491             if ($child = $this->courseinfo->get_file_info('course', 'section', $section->id, '/', '.')) {
00492                 $children[] = $child;
00493             }
00494         }
00495 
00496         return $children;
00497     }
00498 
00503     public function get_parent() {
00504         return $this->courseinfo;
00505     }
00506 }
00507 
00508 
00517 class file_info_area_backup_section extends file_info {
00518     protected $course;
00519     protected $courseinfo;
00520 
00521     public function __construct($browser, $context, $course, file_info_context_course $courseinfo) {
00522         parent::__construct($browser, $context);
00523         $this->course     = $course;
00524         $this->courseinfo = $courseinfo;
00525     }
00526 
00533     public function get_params() {
00534         return array('contextid' => $this->context->id,
00535                      'component' => 'backup',
00536                      'filearea'  => 'section',
00537                      'itemid'    => null,
00538                      'filepath'  => null,
00539                      'filename'  => null);
00540     }
00541 
00546     public function get_visible_name() {
00547         return get_string('sectionbackup', 'repository');
00548     }
00549 
00554     public function is_writable() {
00555         return false;
00556     }
00557 
00563     public function is_empty_area() {
00564         $fs = get_file_storage();
00565         return $fs->is_area_empty($this->context->id, 'backup', 'section');
00566     }
00567 
00572     public function is_directory() {
00573         return true;
00574     }
00575 
00580     public function get_children() {
00581         global $DB;
00582 
00583         $children = array();
00584 
00585         $course_sections = $DB->get_records('course_sections', array('course'=>$this->course->id), 'section');
00586         foreach ($course_sections as $section) {
00587             if ($child = $this->courseinfo->get_file_info('backup', 'section', $section->id, '/', '.')) {
00588                 $children[] = $child;
00589             }
00590         }
00591 
00592         return $children;
00593     }
00594 
00599     public function get_parent() {
00600         return $this->browser->get_file_info($this->context);
00601     }
00602 }
00603 
00604 
 All Data Structures Namespaces Files Functions Variables Enumerations