|
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 00038 class file_info_context_module extends file_info { 00039 protected $course; 00040 protected $cm; 00041 protected $modname; 00042 protected $areas; 00043 00044 public function __construct($browser, $context, $course, $cm, $modname) { 00045 global $CFG; 00046 00047 parent::__construct($browser, $context); 00048 $this->course = $course; 00049 $this->cm = $cm; 00050 $this->modname = $modname; 00051 00052 include_once("$CFG->dirroot/mod/$modname/lib.php"); 00053 00054 //find out all supported areas 00055 $functionname = 'mod_'.$modname.'_get_file_areas'; 00056 $functionname_old = $modname.'_get_file_areas'; 00057 00058 if (function_exists($functionname)) { 00059 $this->areas = $functionname($course, $cm, $context); 00060 } else if (function_exists($functionname_old)) { 00061 $this->areas = $functionname_old($course, $cm, $context); 00062 } else { 00063 $this->areas = array(); 00064 } 00065 unset($this->areas['intro']); // hardcoded, ignore attempts to override it 00066 } 00067 00077 public function get_file_info($component, $filearea, $itemid, $filepath, $filename) { 00078 // try to emulate require_login() tests here 00079 if (!isloggedin()) { 00080 return null; 00081 } 00082 00083 $coursecontext = get_course_context($this->context); 00084 if (!$this->course->visible and !has_capability('moodle/course:viewhiddencourses', $coursecontext)) { 00085 return null; 00086 } 00087 00088 if (!is_viewing($this->context) and !is_enrolled($this->context)) { 00089 // no peaking here if not enrolled or inspector 00090 return null; 00091 } 00092 00093 $modinfo = get_fast_modinfo($this->course); 00094 $cminfo = $modinfo->get_cm($this->cm->id); 00095 if (!$cminfo->uservisible) { 00096 // activity hidden sorry 00097 return null; 00098 } 00099 00100 if (empty($component)) { 00101 return $this; 00102 } 00103 00104 if ($component == 'mod_'.$this->modname and $filearea === 'intro') { 00105 return $this->get_area_intro($itemid, $filepath, $filename); 00106 } else if ($component == 'backup' and $filearea === 'activity') { 00107 return $this->get_area_backup($itemid, $filepath, $filename); 00108 } 00109 00110 $functionname = 'mod_'.$this->modname.'_get_file_info'; 00111 $functionname_old = $this->modname.'_get_file_info'; 00112 00113 if (function_exists($functionname)) { 00114 return $functionname($this->browser, $this->areas, $this->course, $this->cm, $this->context, $filearea, $itemid, $filepath, $filename); 00115 } else if (function_exists($functionname_old)) { 00116 return $functionname_old($this->browser, $this->areas, $this->course, $this->cm, $this->context, $filearea, $itemid, $filepath, $filename); 00117 } 00118 00119 return null; 00120 } 00121 00122 protected function get_area_intro($itemid, $filepath, $filename) { 00123 global $CFG; 00124 00125 if (!plugin_supports('mod', $this->modname, FEATURE_MOD_INTRO, true) or !has_capability('moodle/course:managefiles', $this->context)) { 00126 return null; 00127 } 00128 00129 $fs = get_file_storage(); 00130 00131 $filepath = is_null($filepath) ? '/' : $filepath; 00132 $filename = is_null($filename) ? '.' : $filename; 00133 if (!$storedfile = $fs->get_file($this->context->id, 'mod_'.$this->modname, 'intro', 0, $filepath, $filename)) { 00134 if ($filepath === '/' and $filename === '.') { 00135 $storedfile = new virtual_root_file($this->context->id, 'mod_'.$this->modname, 'intro', 0); 00136 } else { 00137 // not found 00138 return null; 00139 } 00140 } 00141 00142 $urlbase = $CFG->wwwroot.'/pluginfile.php'; 00143 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('moduleintro'), false, true, true, false); 00144 } 00145 00146 protected function get_area_backup($itemid, $filepath, $filename) { 00147 global $CFG; 00148 00149 if (!has_capability('moodle/backup:backupactivity', $this->context)) { 00150 return null; 00151 } 00152 00153 $fs = get_file_storage(); 00154 00155 $filepath = is_null($filepath) ? '/' : $filepath; 00156 $filename = is_null($filename) ? '.' : $filename; 00157 if (!$storedfile = $fs->get_file($this->context->id, 'backup', 'activity', 0, $filepath, $filename)) { 00158 if ($filepath === '/' and $filename === '.') { 00159 $storedfile = new virtual_root_file($this->context->id, 'backup', 'activity', 0); 00160 } else { 00161 // not found 00162 return null; 00163 } 00164 } 00165 00166 $downloadable = has_capability('moodle/backup:downloadfile', $this->context); 00167 $uploadable = has_capability('moodle/restore:uploadfile', $this->context); 00168 00169 $urlbase = $CFG->wwwroot.'/pluginfile.php'; 00170 return new file_info_stored($this->browser, $this->context, $storedfile, $urlbase, get_string('activitybackup', 'repository'), false, $downloadable, $uploadable, false); 00171 } 00172 00177 public function get_visible_name() { 00178 return $this->cm->name.' ('.get_string('modulename', $this->cm->modname).')'; 00179 } 00180 00185 public function is_writable() { 00186 return false; 00187 } 00188 00194 public function is_empty_area() { 00195 if ($child = $this->get_area_backup(0, '/', '.')) { 00196 if (!$child->is_empty_area()) { 00197 return false; 00198 } 00199 } 00200 if ($child = $this->get_area_intro(0, '/', '.')) { 00201 if (!$child->is_empty_area()) { 00202 return false; 00203 } 00204 } 00205 00206 foreach ($this->areas as $area=>$desctiption) { 00207 if ($child = $this->get_file_info('mod_'.$this->modname, $area, null, null, null)) { 00208 if (!$child->is_empty_area()) { 00209 return false; 00210 } 00211 } 00212 } 00213 00214 return true; 00215 } 00216 00221 public function is_directory() { 00222 return true; 00223 } 00224 00229 public function get_children() { 00230 $children = array(); 00231 00232 if ($child = $this->get_area_backup(0, '/', '.')) { 00233 $children[] = $child; 00234 } 00235 if ($child = $this->get_area_intro(0, '/', '.')) { 00236 $children[] = $child; 00237 } 00238 00239 foreach ($this->areas as $area=>$desctiption) { 00240 if ($child = $this->get_file_info('mod_'.$this->modname, $area, null, null, null)) { 00241 $children[] = $child; 00242 } 00243 } 00244 00245 return $children; 00246 } 00247 00252 public function get_parent() { 00253 $pcid = get_parent_contextid($this->context); 00254 $parent = get_context_instance_by_id($pcid); 00255 return $this->browser->get_file_info($parent); 00256 } 00257 }