Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/filebrowser/file_info_context_coursecat.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_coursecat extends file_info {
00039     protected $category;
00040 
00041     public function __construct($browser, $context, $category) {
00042         parent::__construct($browser, $context);
00043         $this->category = $category;
00044     }
00045 
00055     public function get_file_info($component, $filearea, $itemid, $filepath, $filename) {
00056         global $DB;
00057 
00058         if (!$this->category->visible and !has_capability('moodle/category:viewhiddencategories', $this->context)) {
00059             if (empty($component)) {
00060                 // we can not list the category contents, so try parent, or top system
00061                 if ($this->category->parent and $pc = $DB->get_record('course_categories', array('id'=>$this->category->parent))) {
00062                     $parent = get_context_instance(CONTEXT_COURSECAT, $pc->id);
00063                     return $this->browser->get_file_info($parent);
00064                 } else {
00065                     return $this->browser->get_file_info();
00066                 }
00067             }
00068             return null;
00069         }
00070 
00071         if (empty($component)) {
00072             return $this;
00073         }
00074 
00075         $methodname = "get_area_{$component}_{$filearea}";
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_coursecat_description($itemid, $filepath, $filename) {
00084         global $CFG;
00085 
00086         if (!has_capability('moodle/course:update', $this->context)) {
00087             return null;
00088         }
00089 
00090         if (is_null($itemid)) {
00091             return $this;
00092         }
00093 
00094         $fs = get_file_storage();
00095 
00096         $filepath = is_null($filepath) ? '/' : $filepath;
00097         $filename = is_null($filename) ? '.' : $filename;
00098         $urlbase = $CFG->wwwroot.'/pluginfile.php';
00099         if (!$storedfile = $fs->get_file($this->context->id, 'coursecat', 'description', 0, $filepath, $filename)) {
00100             if ($filepath === '/' and $filename === '.') {
00101                 $storedfile = new virtual_root_file($this->context->id, 'coursecat', 'description', 0);
00102             } else {
00103                 // not found
00104                 return null;
00105             }
00106         }
00107 
00108         return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('areacategoryintro', 'repository'), false, true, true, false);
00109     }
00110 
00115     public function get_visible_name() {
00116         return format_string($this->category->name, true, array('context'=>$this->context));
00117     }
00118 
00123     public function is_writable() {
00124         return false;
00125     }
00126 
00131     public function is_directory() {
00132         return true;
00133     }
00134 
00139     public function get_children() {
00140         global $DB;
00141 
00142         $children = array();
00143 
00144         if ($child = $this->get_area_coursecat_description(0, '/', '.')) {
00145             $children[] = $child;
00146         }
00147 
00148         $course_cats = $DB->get_records('course_categories', array('parent'=>$this->category->id), 'sortorder', 'id,visible');
00149         foreach ($course_cats as $category) {
00150             $context = get_context_instance(CONTEXT_COURSECAT, $category->id);
00151             if (!$category->visible and !has_capability('moodle/category:viewhiddencategories', $context)) {
00152                 continue;
00153             }
00154             if ($child = $this->browser->get_file_info($context)) {
00155                 $children[] = $child;
00156             }
00157         }
00158 
00159         $courses = $DB->get_records('course', array('category'=>$this->category->id), 'sortorder', 'id,visible');
00160         foreach ($courses as $course) {
00161             $context = get_context_instance(CONTEXT_COURSE, $course->id);
00162             if (!$course->visible and !has_capability('moodle/course:viewhiddencourses', $context)) {
00163                 continue;
00164             }
00165             if ($child = $this->browser->get_file_info($context)) {
00166                 $children[] = $child;
00167             }
00168         }
00169 
00170         return $children;
00171     }
00172 
00177     public function get_parent() {
00178         $cid = get_parent_contextid($this->context);
00179         $parent = get_context_instance_by_id($cid);
00180         return $this->browser->get_file_info($parent);
00181     }
00182 }
 All Data Structures Namespaces Files Functions Variables Enumerations