3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * repository_coursefiles class is used to browse course files
23 * @subpackage coursefiles
24 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 class repository_coursefiles
extends repository
{
31 * coursefiles plugin doesn't require login, so list all files
34 public function print_login() {
35 return $this->get_listing();
41 * @param string $encodedpath
44 public function get_listing($encodedpath = '') {
45 global $CFG, $USER, $OUTPUT;
47 $ret['dynload'] = true;
48 $ret['nosearch'] = true;
49 $ret['nologin'] = true;
51 $component = 'course';
55 $browser = get_file_browser();
57 if (!empty($encodedpath)) {
58 $params = unserialize(base64_decode($encodedpath));
59 if (is_array($params)) {
60 $filepath = is_null($params['filepath']) ?
NULL : clean_param($params['filepath'], PARAM_PATH
);;
61 $filename = is_null($params['filename']) ?
NULL : clean_param($params['filename'], PARAM_FILE
);
62 $context = get_context_instance_by_id(clean_param($params['contextid'], PARAM_INT
));
67 list($context, $course, $cm) = get_context_info_array($this->context
->id
);
68 $courseid = is_object($course) ?
$course->id
: SITEID
;
69 $context = get_context_instance(CONTEXT_COURSE
, $courseid);
72 if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
73 // build path navigation
75 $encodedpath = base64_encode(serialize($fileinfo->get_params()));
76 $pathnodes[] = array('name'=>$fileinfo->get_visible_name(), 'path'=>$encodedpath);
77 $level = $fileinfo->get_parent();
79 $params = $level->get_params();
80 $encodedpath = base64_encode(serialize($params));
81 if ($params['contextid'] != $context->id
) {
84 $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath);
85 $level = $level->get_parent();
87 if (!empty($pathnodes) && is_array($pathnodes)) {
88 $pathnodes = array_reverse($pathnodes);
89 $ret['path'] = $pathnodes;
92 $children = $fileinfo->get_children();
93 foreach ($children as $child) {
94 if ($child->is_directory()) {
95 $params = $child->get_params();
96 $subdir_children = $child->get_children();
97 $encodedpath = base64_encode(serialize($params));
99 'title' => $child->get_visible_name(),
102 'path' => $encodedpath,
104 'thumbnail' => $OUTPUT->pix_url('f/folder-32')->out(false)
108 $encodedpath = base64_encode(serialize($child->get_params()));
110 'title' => $child->get_visible_name(),
113 'source'=> $encodedpath,
114 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($child->get_visible_name(), 32))->out(false)
122 $ret['list'] = array_filter($list, array($this, 'filter'));
126 public function get_link($encoded) {
129 $browser = get_file_browser();
132 $params = unserialize(base64_decode($encoded));
133 $contextid = clean_param($params['contextid'], PARAM_INT
);
134 $fileitemid = clean_param($params['itemid'], PARAM_INT
);
135 $filename = clean_param($params['filename'], PARAM_FILE
);
136 $filepath = clean_param($params['filepath'], PARAM_PATH
);;
137 $filearea = clean_param($params['filearea'], PARAM_ALPHAEXT
);
138 $component = clean_param($params['component'], PARAM_ALPHAEXT
);
139 $context = get_context_instance_by_id($contextid);
141 $file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename);
142 return $file_info->get_url();
146 * Return is the instance is visible
147 * (is the type visible ? is the context enable ?)
150 public function is_visible() {
151 global $COURSE; //TODO: this is deprecated (skodak)
152 if ($COURSE->legacyfiles
!= 2) {
153 // do not show repo if legacy files disabled in this course...
157 return parent
::is_visible();
160 public function get_name() {
161 list($context, $course, $cm) = get_context_info_array($this->context
->id
);
162 if (!empty($course)) {
163 return get_string('courselegacyfiles') . format_string($course->shortname
, true, array('context' => get_course_context($context)));
165 return get_string('courselegacyfiles');
169 public function supported_returntypes() {
170 return (FILE_INTERNAL | FILE_EXTERNAL
);
172 public static function get_type_option_names() {
177 * Does this repository used to browse moodle files?
181 public function has_moodle_files() {