|
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 00028 class repository_coursefiles extends repository { 00029 00034 public function print_login() { 00035 return $this->get_listing(); 00036 } 00037 00044 public function get_listing($encodedpath = '') { 00045 global $CFG, $USER, $OUTPUT; 00046 $ret = array(); 00047 $ret['dynload'] = true; 00048 $ret['nosearch'] = true; 00049 $ret['nologin'] = true; 00050 $list = array(); 00051 $component = 'course'; 00052 $filearea = 'legacy'; 00053 $itemid = 0; 00054 00055 $browser = get_file_browser(); 00056 00057 if (!empty($encodedpath)) { 00058 $params = unserialize(base64_decode($encodedpath)); 00059 if (is_array($params)) { 00060 $filepath = is_null($params['filepath']) ? NULL : clean_param($params['filepath'], PARAM_PATH);; 00061 $filename = is_null($params['filename']) ? NULL : clean_param($params['filename'], PARAM_FILE); 00062 $context = get_context_instance_by_id(clean_param($params['contextid'], PARAM_INT)); 00063 } 00064 } else { 00065 $filename = null; 00066 $filepath = null; 00067 list($context, $course, $cm) = get_context_info_array($this->context->id); 00068 $courseid = is_object($course) ? $course->id : SITEID; 00069 $context = get_context_instance(CONTEXT_COURSE, $courseid); 00070 } 00071 00072 if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) { 00073 // build path navigation 00074 $pathnodes = array(); 00075 $encodedpath = base64_encode(serialize($fileinfo->get_params())); 00076 $pathnodes[] = array('name'=>$fileinfo->get_visible_name(), 'path'=>$encodedpath); 00077 $level = $fileinfo->get_parent(); 00078 while ($level) { 00079 $params = $level->get_params(); 00080 $encodedpath = base64_encode(serialize($params)); 00081 if ($params['contextid'] != $context->id) { 00082 break; 00083 } 00084 $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath); 00085 $level = $level->get_parent(); 00086 } 00087 if (!empty($pathnodes) && is_array($pathnodes)) { 00088 $pathnodes = array_reverse($pathnodes); 00089 $ret['path'] = $pathnodes; 00090 } 00091 // build file tree 00092 $children = $fileinfo->get_children(); 00093 foreach ($children as $child) { 00094 if ($child->is_directory()) { 00095 $params = $child->get_params(); 00096 $subdir_children = $child->get_children(); 00097 $encodedpath = base64_encode(serialize($params)); 00098 $node = array( 00099 'title' => $child->get_visible_name(), 00100 'size' => 0, 00101 'date' => '', 00102 'path' => $encodedpath, 00103 'children'=>array(), 00104 'thumbnail' => $OUTPUT->pix_url('f/folder-32')->out(false) 00105 ); 00106 $list[] = $node; 00107 } else { 00108 $encodedpath = base64_encode(serialize($child->get_params())); 00109 $node = array( 00110 'title' => $child->get_visible_name(), 00111 'size' => 0, 00112 'date' => '', 00113 'source'=> $encodedpath, 00114 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($child->get_visible_name(), 32))->out(false) 00115 ); 00116 $list[] = $node; 00117 } 00118 } 00119 } else { 00120 $list = array(); 00121 } 00122 $ret['list'] = array_filter($list, array($this, 'filter')); 00123 return $ret; 00124 } 00125 00126 public function get_link($encoded) { 00127 $info = array(); 00128 00129 $browser = get_file_browser(); 00130 00131 // the final file 00132 $params = unserialize(base64_decode($encoded)); 00133 $contextid = clean_param($params['contextid'], PARAM_INT); 00134 $fileitemid = clean_param($params['itemid'], PARAM_INT); 00135 $filename = clean_param($params['filename'], PARAM_FILE); 00136 $filepath = clean_param($params['filepath'], PARAM_PATH);; 00137 $filearea = clean_param($params['filearea'], PARAM_AREA); 00138 $component = clean_param($params['component'], PARAM_COMPONENT); 00139 $context = get_context_instance_by_id($contextid); 00140 00141 $file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename); 00142 return $file_info->get_url(); 00143 } 00144 00150 public function is_visible() { 00151 global $COURSE; //TODO: this is deprecated (skodak) 00152 if ($COURSE->legacyfiles != 2) { 00153 // do not show repo if legacy files disabled in this course... 00154 return false; 00155 } 00156 00157 return parent::is_visible(); 00158 } 00159 00160 public function get_name() { 00161 list($context, $course, $cm) = get_context_info_array($this->context->id); 00162 if (!empty($course)) { 00163 return get_string('courselegacyfiles') . format_string($course->shortname, true, array('context' => get_course_context($context))); 00164 } else { 00165 return get_string('courselegacyfiles'); 00166 } 00167 } 00168 00169 public function supported_returntypes() { 00170 return (FILE_INTERNAL | FILE_EXTERNAL); 00171 } 00172 public static function get_type_option_names() { 00173 return array(); 00174 } 00175 00181 public function has_moodle_files() { 00182 return true; 00183 } 00184 }