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 = json_decode(base64_decode($encodedpath), true);
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 = 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 = context_course
::instance($courseid);
80 if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
81 // build path navigation
83 $encodedpath = base64_encode(json_encode($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(json_encode($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(json_encode($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(json_encode($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 'isref' => $child->is_external_file(),
126 'thumbnail' => $OUTPUT->pix_url(file_file_icon($child, 90))->out(false)
128 if ($child->get_status() == 666) {
129 $node['originalmissing'] = true;
131 if ($imageinfo = $child->get_imageinfo()) {
132 $fileurl = new moodle_url($child->get_url());
133 $node['realthumbnail'] = $fileurl->out(false, array('preview' => 'thumb', 'oid' => $child->get_timemodified()));
134 $node['realicon'] = $fileurl->out(false, array('preview' => 'tinyicon', 'oid' => $child->get_timemodified()));
135 $node['image_width'] = $imageinfo['width'];
136 $node['image_height'] = $imageinfo['height'];
144 $ret['list'] = array_filter($list, array($this, 'filter'));
148 public function get_link($encoded) {
151 $browser = get_file_browser();
154 $params = unserialize(base64_decode($encoded));
155 $contextid = clean_param($params['contextid'], PARAM_INT
);
156 $fileitemid = clean_param($params['itemid'], PARAM_INT
);
157 $filename = clean_param($params['filename'], PARAM_FILE
);
158 $filepath = clean_param($params['filepath'], PARAM_PATH
);
159 $filearea = clean_param($params['filearea'], PARAM_AREA
);
160 $component = clean_param($params['component'], PARAM_COMPONENT
);
161 $context = context
::instance_by_id($contextid);
163 $file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename);
164 return $file_info->get_url();
168 * Return is the instance is visible
169 * (is the type visible ? is the context enable ?)
173 public function is_visible() {
174 global $COURSE; //TODO: this is deprecated (skodak)
175 if ($COURSE->legacyfiles
!= 2) {
176 // do not show repo if legacy files disabled in this course...
180 return parent
::is_visible();
184 * Return the repository name.
188 public function get_name() {
189 $context = $this->context
->get_course_context(false);
191 return get_string('courselegacyfilesofcourse', 'moodle', $context->get_context_name(false, true));
193 return get_string('courselegacyfiles');
197 public function supported_returntypes() {
198 return (FILE_INTERNAL | FILE_REFERENCE
);
201 public static function get_type_option_names() {
206 * Does this repository used to browse moodle files?
210 public function has_moodle_files() {
215 * Is this repository accessing private data?
219 public function contains_private_data() {