2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * This plugin is used to access coursefiles repository
21 * @package repository_coursefiles
22 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once($CFG->dirroot
. '/repository/lib.php');
28 * repository_coursefiles class is used to browse course files
31 * @package repository_coursefiles
32 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class repository_coursefiles
extends repository
{
38 * coursefiles plugin doesn't require login, so list all files
42 public function print_login() {
43 return $this->get_listing();
49 * @param string $encodedpath
52 public function get_listing($encodedpath = '', $page = '') {
53 global $CFG, $USER, $OUTPUT;
55 $ret['dynload'] = true;
56 $ret['nosearch'] = true;
57 $ret['nologin'] = true;
59 $component = 'course';
63 $browser = get_file_browser();
65 if (!empty($encodedpath)) {
66 $params = unserialize(base64_decode($encodedpath));
67 if (is_array($params)) {
68 $filepath = is_null($params['filepath']) ?
NULL : clean_param($params['filepath'], PARAM_PATH
);;
69 $filename = is_null($params['filename']) ?
NULL : clean_param($params['filename'], PARAM_FILE
);
70 $context = get_context_instance_by_id(clean_param($params['contextid'], PARAM_INT
));
75 list($context, $course, $cm) = get_context_info_array($this->context
->id
);
76 $courseid = is_object($course) ?
$course->id
: SITEID
;
77 $context = get_context_instance(CONTEXT_COURSE
, $courseid);
80 if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
81 // build path navigation
83 $encodedpath = base64_encode(serialize($fileinfo->get_params()));
84 $pathnodes[] = array('name'=>$fileinfo->get_visible_name(), 'path'=>$encodedpath);
85 $level = $fileinfo->get_parent();
87 $params = $level->get_params();
88 $encodedpath = base64_encode(serialize($params));
89 if ($params['contextid'] != $context->id
) {
92 $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath);
93 $level = $level->get_parent();
95 if (!empty($pathnodes) && is_array($pathnodes)) {
96 $pathnodes = array_reverse($pathnodes);
97 $ret['path'] = $pathnodes;
100 $children = $fileinfo->get_children();
101 foreach ($children as $child) {
102 if ($child->is_directory()) {
103 $params = $child->get_params();
104 $subdir_children = $child->get_children();
105 $encodedpath = base64_encode(serialize($params));
107 'title' => $child->get_visible_name(),
108 'datemodified' => $child->get_timemodified(),
109 'datecreated' => $child->get_timecreated(),
110 'path' => $encodedpath,
112 'thumbnail' => $OUTPUT->pix_url(file_folder_icon(90))->out(false)
116 $encodedpath = base64_encode(serialize($child->get_params()));
118 'title' => $child->get_visible_name(),
119 'size' => $child->get_filesize(),
120 'author' => $child->get_author(),
121 'license' => $child->get_license(),
122 'datemodified' => $child->get_timemodified(),
123 'datecreated' => $child->get_timecreated(),
124 'source'=> $encodedpath,
125 'thumbnail' => $OUTPUT->pix_url(file_file_icon($child, 90))->out(false)
127 if ($imageinfo = $child->get_imageinfo()) {
128 $fileurl = new moodle_url($child->get_url());
129 $node['realthumbnail'] = $fileurl->out(false, array('preview' => 'thumb', 'oid' => $child->get_timemodified()));
130 $node['realicon'] = $fileurl->out(false, array('preview' => 'tinyicon', 'oid' => $child->get_timemodified()));
131 $node['image_width'] = $imageinfo['width'];
132 $node['image_height'] = $imageinfo['height'];
140 $ret['list'] = array_filter($list, array($this, 'filter'));
144 public function get_link($encoded) {
147 $browser = get_file_browser();
150 $params = unserialize(base64_decode($encoded));
151 $contextid = clean_param($params['contextid'], PARAM_INT
);
152 $fileitemid = clean_param($params['itemid'], PARAM_INT
);
153 $filename = clean_param($params['filename'], PARAM_FILE
);
154 $filepath = clean_param($params['filepath'], PARAM_PATH
);;
155 $filearea = clean_param($params['filearea'], PARAM_AREA
);
156 $component = clean_param($params['component'], PARAM_COMPONENT
);
157 $context = get_context_instance_by_id($contextid);
159 $file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename);
160 return $file_info->get_url();
164 * Return is the instance is visible
165 * (is the type visible ? is the context enable ?)
169 public function is_visible() {
170 global $COURSE; //TODO: this is deprecated (skodak)
171 if ($COURSE->legacyfiles
!= 2) {
172 // do not show repo if legacy files disabled in this course...
176 return parent
::is_visible();
179 public function get_name() {
180 list($context, $course, $cm) = get_context_info_array($this->context
->id
);
181 if (!empty($course)) {
182 return get_string('courselegacyfiles') . format_string($course->shortname
, true, array('context' => get_course_context($context)));
184 return get_string('courselegacyfiles');
188 public function supported_returntypes() {
189 return (FILE_INTERNAL | FILE_REFERENCE
);
192 public static function get_type_option_names() {
197 * Does this repository used to browse moodle files?
201 public function has_moodle_files() {
206 * Unpack file info and pack it, mainly for data validation
208 * @param string $source
209 * @return string file referece
211 public function get_file_reference($source) {
212 $params = unserialize(base64_decode($source));
214 if (!is_array($params)) {
215 throw new repository_exception('invalidparams', 'repository');
218 $filename = is_null($params['filename']) ?
null : clean_param($params['filename'], PARAM_FILE
);
219 $filepath = is_null($params['filepath']) ?
null : clean_param($params['filepath'], PARAM_PATH
);;
220 $contextid = is_null($params['contextid']) ?
null : clean_param($params['contextid'], PARAM_INT
);
222 $reference = array();
223 // hard coded filearea, component and itemid for security
224 $reference['component'] = 'course';
225 $reference['filearea'] = 'legacy';
226 $reference['itemid'] = 0;
227 $reference['contextid'] = $contextid;
228 $reference['filepath'] = $filepath;
229 $reference['filename'] = $filename;
231 return file_storage
::pack_reference($reference);
235 * Get file from external repository by reference
236 * {@link repository::get_file_reference()}
237 * {@link repository::get_file()}
239 * @param stdClass $reference file reference db record
240 * @return stdClass|null|false
242 public function get_file_by_reference($reference) {
243 $fs = get_file_storage();
244 $ref = $reference->reference
;
245 $params = file_storage
::unpack_reference($ref);
246 if (!is_array($params)) {
247 throw new repository_exception('invalidparams', 'repository');
249 $filename = is_null($params['filename']) ?
null : clean_param($params['filename'], PARAM_FILE
);
250 $filepath = is_null($params['filepath']) ?
null : clean_param($params['filepath'], PARAM_PATH
);;
251 $contextid = is_null($params['contextid']) ?
null : clean_param($params['contextid'], PARAM_INT
);
253 // hard coded filearea, component and itemid for security
254 $storedfile = $fs->get_file($contextid, 'course', 'legacy', 0, $filepath, $filename);
256 $fileinfo = new stdClass
;
257 $fileinfo->contenthash
= $storedfile->get_contenthash();
258 $fileinfo->filesize
= $storedfile->get_filesize();
263 * Return human readable reference information
264 * {@link stored_file::get_reference()}
266 * @param string $reference
267 * @return string|null
269 public function get_reference_details($reference) {
270 $params = file_storage
::unpack_reference($reference);
271 list($context, $course, $cm) = get_context_info_array($params['contextid']);
273 if (!empty($course)) {
274 $coursename = '"' . format_string($course->shortname
, true, array('context' => get_course_context($context))) . '" ' . get_string('courselegacyfiles');
276 $coursename = get_string('courselegacyfiles');
278 // Indicate this is from user private area
279 return $coursename . ': ' . $params['filepath'] . $params['filename'];
283 * Return reference file life time
288 public function get_reference_file_lifetime($ref) {
289 // this should be realtime
294 * Repository method to serve file
296 * @param stored_file $storedfile
297 * @param int $lifetime Number of seconds before the file should expire from caches (default 24 hours)
298 * @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only
299 * @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin
300 * @param array $options additional options affecting the file serving
302 public function send_file($storedfile, $lifetime=86400 , $filter=0, $forcedownload=false, array $options = null) {
303 $fs = get_file_storage();
305 $reference = $storedfile->get_reference();
306 $params = file_storage
::unpack_reference($reference);
308 $filename = is_null($params['filename']) ?
null : clean_param($params['filename'], PARAM_FILE
);
309 $filepath = is_null($params['filepath']) ?
null : clean_param($params['filepath'], PARAM_PATH
);;
310 $contextid = is_null($params['contextid']) ?
null : clean_param($params['contextid'], PARAM_INT
);
312 // hard coded file area and component for security
313 $srcfile = $fs->get_file($contextid, 'course', 'legacy', 0, $filepath, $filename);
315 send_stored_file($srcfile, $lifetime, $filter, $forcedownload, $options);