|
Moodle
2.2.1
http://www.collinsharper.com
|
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 00030 require_once("$CFG->libdir/filebrowser/file_info.php"); 00031 00032 // general area types 00033 require_once("$CFG->libdir/filebrowser/file_info_stored.php"); 00034 require_once("$CFG->libdir/filebrowser/virtual_root_file.php"); 00035 00036 // description of available areas in each context level 00037 require_once("$CFG->libdir/filebrowser/file_info_context_system.php"); 00038 require_once("$CFG->libdir/filebrowser/file_info_context_user.php"); 00039 require_once("$CFG->libdir/filebrowser/file_info_context_coursecat.php"); 00040 require_once("$CFG->libdir/filebrowser/file_info_context_course.php"); 00041 require_once("$CFG->libdir/filebrowser/file_info_context_module.php"); 00042 00064 class file_browser { 00065 00076 public function get_file_info($context = NULL, $component = NULL, $filearea = NULL, $itemid = NULL, $filepath = NULL, $filename = NULL) { 00077 if (!$context) { 00078 $context = get_context_instance(CONTEXT_SYSTEM); 00079 } 00080 switch ($context->contextlevel) { 00081 case CONTEXT_SYSTEM: 00082 return $this->get_file_info_context_system($context, $component, $filearea, $itemid, $filepath, $filename); 00083 case CONTEXT_USER: 00084 return $this->get_file_info_context_user($context, $component, $filearea, $itemid, $filepath, $filename); 00085 case CONTEXT_COURSECAT: 00086 return $this->get_file_info_context_coursecat($context, $component, $filearea, $itemid, $filepath, $filename); 00087 case CONTEXT_COURSE: 00088 return $this->get_file_info_context_course($context, $component, $filearea, $itemid, $filepath, $filename); 00089 case CONTEXT_MODULE: 00090 return $this->get_file_info_context_module($context, $component, $filearea, $itemid, $filepath, $filename); 00091 } 00092 00093 return null; 00094 } 00095 00106 private function get_file_info_context_system($context, $component, $filearea, $itemid, $filepath, $filename) { 00107 $level = new file_info_context_system($this, $context); 00108 return $level->get_file_info($component, $filearea, $itemid, $filepath, $filename); 00109 // nothing supported at this context yet 00110 } 00111 00122 private function get_file_info_context_user($context, $component, $filearea, $itemid, $filepath, $filename) { 00123 global $DB, $USER; 00124 if ($context->instanceid == $USER->id) { 00125 $user = $USER; 00126 } else { 00127 $user = $DB->get_record('user', array('id'=>$context->instanceid)); 00128 } 00129 00130 if (isguestuser($user)) { 00131 // guests do not have any files 00132 return null; 00133 } 00134 00135 if ($user->deleted) { 00136 return null; 00137 } 00138 00139 $level = new file_info_context_user($this, $context, $user); 00140 return $level->get_file_info($component, $filearea, $itemid, $filepath, $filename); 00141 } 00142 00153 private function get_file_info_context_coursecat($context, $component, $filearea, $itemid, $filepath, $filename) { 00154 global $DB; 00155 00156 if (!$category = $DB->get_record('course_categories', array('id'=>$context->instanceid))) { 00157 return null; 00158 } 00159 00160 $level = new file_info_context_coursecat($this, $context, $category); 00161 return $level->get_file_info($component, $filearea, $itemid, $filepath, $filename); 00162 } 00163 00174 private function get_file_info_context_course($context, $component, $filearea, $itemid, $filepath, $filename) { 00175 global $DB, $COURSE; 00176 00177 if ($context->instanceid == $COURSE->id) { 00178 $course = $COURSE; 00179 } else if (!$course = $DB->get_record('course', array('id'=>$context->instanceid))) { 00180 return null; 00181 } 00182 00183 $level = new file_info_context_course($this, $context, $course); 00184 return $level->get_file_info($component, $filearea, $itemid, $filepath, $filename); 00185 } 00186 00197 private function get_file_info_context_module($context, $component, $filearea, $itemid, $filepath, $filename) { 00198 global $COURSE, $DB, $CFG; 00199 00200 00201 if (!$cm = get_coursemodule_from_id('', $context->instanceid)) { 00202 return null; 00203 } 00204 00205 if ($cm->course == $COURSE->id) { 00206 $course = $COURSE; 00207 } else if (!$course = $DB->get_record('course', array('id'=>$cm->course))) { 00208 return null; 00209 } 00210 00211 $modinfo = get_fast_modinfo($course); 00212 if (empty($modinfo->cms[$cm->id]->uservisible)) { 00213 return null; 00214 } 00215 00216 $modname = $modinfo->cms[$cm->id]->modname; 00217 00218 if (!file_exists("$CFG->dirroot/mod/$modname/lib.php")) { 00219 return null; 00220 } 00221 00222 // ok, we know that module exists, and user may access it 00223 00224 $level = new file_info_context_module($this, $context, $course, $cm, $modname); 00225 return $level->get_file_info($component, $filearea, $itemid, $filepath, $filename); 00226 } 00227 00228 }