[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 module 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 26 defined('MOODLE_INTERNAL') || die(); 27 28 /** 29 * Represents a module 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 */ 35 class file_info_context_module extends file_info { 36 /** @var stdClass Course object */ 37 protected $course; 38 /** @var stdClass Course module object */ 39 protected $cm; 40 /** @var string Module name */ 41 protected $modname; 42 /** @var array Available file areas */ 43 protected $areas; 44 /** @var array caches the result of last call to get_non_empty_children() */ 45 protected $nonemptychildren; 46 47 /** 48 * Constructor 49 * 50 * @param file_browser $browser file browser instance 51 * @param stdClass $context context object 52 * @param stdClass $course course object 53 * @param stdClass $cm course module object 54 * @param string $modname module name 55 */ 56 public function __construct($browser, $context, $course, $cm, $modname) { 57 global $CFG; 58 59 parent::__construct($browser, $context); 60 $this->course = $course; 61 $this->cm = $cm; 62 $this->modname = $modname; 63 $this->nonemptychildren = null; 64 65 include_once("$CFG->dirroot/mod/$modname/lib.php"); 66 67 //find out all supported areas 68 $functionname = 'mod_'.$modname.'_get_file_areas'; 69 $functionname_old = $modname.'_get_file_areas'; 70 71 if (function_exists($functionname)) { 72 $this->areas = $functionname($course, $cm, $context); 73 } else if (function_exists($functionname_old)) { 74 $this->areas = $functionname_old($course, $cm, $context); 75 } else { 76 $this->areas = array(); 77 } 78 unset($this->areas['intro']); // hardcoded, ignore attempts to override it 79 } 80 81 /** 82 * Return information about this specific context level 83 * 84 * @param string $component component 85 * @param string $filearea file area 86 * @param int $itemid item ID 87 * @param string $filepath file path 88 * @param string $filename file name 89 * @return file_info|null 90 */ 91 public function get_file_info($component, $filearea, $itemid, $filepath, $filename) { 92 // try to emulate require_login() tests here 93 if (!isloggedin()) { 94 return null; 95 } 96 97 $coursecontext = $this->context->get_course_context(true); 98 if (!$this->course->visible and !has_capability('moodle/course:viewhiddencourses', $coursecontext)) { 99 return null; 100 } 101 102 if (!is_viewing($this->context) and !is_enrolled($this->context)) { 103 // no peaking here if not enrolled or inspector 104 return null; 105 } 106 107 $modinfo = get_fast_modinfo($this->course); 108 $cminfo = $modinfo->get_cm($this->cm->id); 109 if (!$cminfo->uservisible) { 110 // activity hidden sorry 111 return null; 112 } 113 114 if (empty($component)) { 115 return $this; 116 } 117 118 if ($component == 'mod_'.$this->modname and $filearea === 'intro') { 119 return $this->get_area_intro($itemid, $filepath, $filename); 120 } else if ($component == 'backup' and $filearea === 'activity') { 121 return $this->get_area_backup($itemid, $filepath, $filename); 122 } 123 124 $functionname = 'mod_'.$this->modname.'_get_file_info'; 125 $functionname_old = $this->modname.'_get_file_info'; 126 127 if (function_exists($functionname)) { 128 return $functionname($this->browser, $this->areas, $this->course, $this->cm, $this->context, $filearea, $itemid, $filepath, $filename); 129 } else if (function_exists($functionname_old)) { 130 return $functionname_old($this->browser, $this->areas, $this->course, $this->cm, $this->context, $filearea, $itemid, $filepath, $filename); 131 } 132 133 return null; 134 } 135 136 /** 137 * Get a file from module intro area 138 * 139 * @param int $itemid item ID 140 * @param string $filepath file path 141 * @param string $filename file name 142 * @return file_info|null 143 */ 144 protected function get_area_intro($itemid, $filepath, $filename) { 145 global $CFG; 146 147 if (!plugin_supports('mod', $this->modname, FEATURE_MOD_INTRO, true) or !has_capability('moodle/course:managefiles', $this->context)) { 148 return null; 149 } 150 151 $fs = get_file_storage(); 152 153 $filepath = is_null($filepath) ? '/' : $filepath; 154 $filename = is_null($filename) ? '.' : $filename; 155 if (!$storedfile = $fs->get_file($this->context->id, 'mod_'.$this->modname, 'intro', 0, $filepath, $filename)) { 156 if ($filepath === '/' and $filename === '.') { 157 $storedfile = new virtual_root_file($this->context->id, 'mod_'.$this->modname, 'intro', 0); 158 } else { 159 // not found 160 return null; 161 } 162 } 163 164 $urlbase = $CFG->wwwroot.'/pluginfile.php'; 165 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('moduleintro'), false, true, true, false); 166 } 167 168 /** 169 * Get a file from module backup area 170 * 171 * @param int $itemid item ID 172 * @param string $filepath file path 173 * @param string $filename file name 174 * @return file_info|null 175 */ 176 protected function get_area_backup($itemid, $filepath, $filename) { 177 global $CFG; 178 179 if (!has_capability('moodle/backup:backupactivity', $this->context)) { 180 return null; 181 } 182 183 $fs = get_file_storage(); 184 185 $filepath = is_null($filepath) ? '/' : $filepath; 186 $filename = is_null($filename) ? '.' : $filename; 187 if (!$storedfile = $fs->get_file($this->context->id, 'backup', 'activity', 0, $filepath, $filename)) { 188 if ($filepath === '/' and $filename === '.') { 189 $storedfile = new virtual_root_file($this->context->id, 'backup', 'activity', 0); 190 } else { 191 // not found 192 return null; 193 } 194 } 195 196 $downloadable = has_capability('moodle/backup:downloadfile', $this->context); 197 $uploadable = has_capability('moodle/restore:uploadfile', $this->context); 198 199 $urlbase = $CFG->wwwroot.'/pluginfile.php'; 200 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('activitybackup', 'repository'), false, $downloadable, $uploadable, false); 201 } 202 203 /** 204 * Returns localised visible name. 205 * 206 * @return string 207 */ 208 public function get_visible_name() { 209 return $this->cm->name.' ('.get_string('modulename', $this->cm->modname).')'; 210 } 211 212 /** 213 * Whether or not files or directories can be added 214 * 215 * @return bool 216 */ 217 public function is_writable() { 218 return false; 219 } 220 221 /** 222 * Whether or not this is an emtpy area 223 * 224 * @return bool 225 */ 226 public function is_empty_area() { 227 if ($child = $this->get_area_backup(0, '/', '.')) { 228 if (!$child->is_empty_area()) { 229 return false; 230 } 231 } 232 if ($child = $this->get_area_intro(0, '/', '.')) { 233 if (!$child->is_empty_area()) { 234 return false; 235 } 236 } 237 238 foreach ($this->areas as $area=>$desctiption) { 239 if ($child = $this->get_file_info('mod_'.$this->modname, $area, null, null, null)) { 240 if (!$child->is_empty_area()) { 241 return false; 242 } 243 } 244 } 245 246 return true; 247 } 248 249 /** 250 * Whether or not this is a directory 251 * 252 * @return bool 253 */ 254 public function is_directory() { 255 return true; 256 } 257 258 /** 259 * Returns list of children. 260 * 261 * @return array of file_info instances 262 */ 263 public function get_children() { 264 return $this->get_filtered_children('*', false, true); 265 } 266 267 /** 268 * Help function to return files matching extensions or their count 269 * 270 * @param string|array $extensions, either '*' or array of lowercase extensions, i.e. array('.gif','.jpg') 271 * @param bool|int $countonly if false returns the children, if an int returns just the 272 * count of children but stops counting when $countonly number of children is reached 273 * @param bool $returnemptyfolders if true returns items that don't have matching files inside 274 * @return array|int array of file_info instances or the count 275 */ 276 private function get_filtered_children($extensions = '*', $countonly = false, $returnemptyfolders = false) { 277 global $DB; 278 // prepare list of areas including intro and backup 279 $areas = array( 280 array('mod_'.$this->modname, 'intro'), 281 array('backup', 'activity') 282 ); 283 foreach ($this->areas as $area => $desctiption) { 284 $areas[] = array('mod_'.$this->modname, $area); 285 } 286 287 $params1 = array('contextid' => $this->context->id, 'emptyfilename' => '.'); 288 list($sql2, $params2) = $this->build_search_files_sql($extensions); 289 $children = array(); 290 foreach ($areas as $area) { 291 if (!$returnemptyfolders) { 292 // fast pre-check if there are any files in the filearea 293 $params1['component'] = $area[0]; 294 $params1['filearea'] = $area[1]; 295 if (!$DB->record_exists_sql('SELECT 1 from {files} 296 WHERE contextid = :contextid 297 AND filename <> :emptyfilename 298 AND component = :component 299 AND filearea = :filearea '.$sql2, 300 array_merge($params1, $params2))) { 301 continue; 302 } 303 } 304 if ($child = $this->get_file_info($area[0], $area[1], null, null, null)) { 305 if ($returnemptyfolders || $child->count_non_empty_children($extensions)) { 306 $children[] = $child; 307 if ($countonly !== false && count($children) >= $countonly) { 308 break; 309 } 310 } 311 } 312 } 313 if ($countonly !== false) { 314 return count($children); 315 } 316 return $children; 317 } 318 319 /** 320 * Returns list of children which are either files matching the specified extensions 321 * or folders that contain at least one such file. 322 * 323 * @param string|array $extensions either '*' or array of lowercase extensions, i.e. array('.gif','.jpg') 324 * @return array of file_info instances 325 */ 326 public function get_non_empty_children($extensions = '*') { 327 if ($this->nonemptychildren !== null) { 328 return $this->nonemptychildren; 329 } 330 $this->nonemptychildren = $this->get_filtered_children($extensions); 331 return $this->nonemptychildren; 332 } 333 334 /** 335 * Returns the number of children which are either files matching the specified extensions 336 * or folders containing at least one such file. 337 * 338 * @param string|array $extensions for example '*' or array('.gif','.jpg') 339 * @param int $limit stop counting after at least $limit non-empty children are found 340 * @return int 341 */ 342 public function count_non_empty_children($extensions = '*', $limit = 1) { 343 if ($this->nonemptychildren !== null) { 344 return count($this->nonemptychildren); 345 } 346 return $this->get_filtered_children($extensions, $limit); 347 } 348 349 /** 350 * Returns parent file_info instance 351 * 352 * @return file_info|null file_info or null for root 353 */ 354 public function get_parent() { 355 $parent = $this->context->get_parent_context(); 356 return $this->browser->get_file_info($parent); 357 } 358 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |