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_local class is used to browse moodle files
24 * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 class repository_local extends repository {
31 * local 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;
52 if (!empty($encodedpath)) {
53 $params = unserialize(base64_decode($encodedpath));
54 if (is_array($params)) {
55 $component = is_null($params['component']) ? NULL : clean_param($params['component'], PARAM_ALPHAEXT);
56 $filearea = is_null($params['filearea']) ? NULL : clean_param($params['filearea'], PARAM_ALPHAEXT);
57 $itemid = is_null($params['itemid']) ? NULL : clean_param($params['itemid'], PARAM_INT);
58 $filepath = is_null($params['filepath']) ? NULL : clean_param($params['filepath'], PARAM_PATH);;
59 $filename = is_null($params['filename']) ? NULL : clean_param($params['filename'], PARAM_FILE);
60 $context = get_context_instance_by_id(clean_param($params['contextid'], PARAM_INT));
68 if (!empty($this->context)) {
69 list($context, $course, $cm) = get_context_info_array($this->context->id);
70 $courseid = is_object($course) ? $course->id : SITEID;
71 $context = get_context_instance(CONTEXT_COURSE, $courseid);
73 $context = get_system_context();
77 $browser = get_file_browser();
79 if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
80 // build path navigation
82 $encodedpath = base64_encode(serialize($fileinfo->get_params()));
83 $pathnodes[] = array('name'=>$fileinfo->get_visible_name(), 'path'=>$encodedpath);
84 $level = $fileinfo->get_parent();
86 $encodedpath = base64_encode(serialize($level->get_params()));
87 $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath);
88 $level = $level->get_parent();
90 if (!empty($pathnodes) && is_array($pathnodes)) {
91 $pathnodes = array_reverse($pathnodes);
92 $ret['path'] = $pathnodes;
95 $children = $fileinfo->get_children();
96 foreach ($children as $child) {
97 if ($child->is_directory()) {
98 $params = $child->get_params();
99 $subdir_children = $child->get_children();
100 //if (empty($subdir_children)) {
103 $encodedpath = base64_encode(serialize($params));
104 // hide user_private area from local plugin, user should
105 // use private file plugin to access private files
106 //if ($params['filearea'] == 'user_private') {
110 'title' => $child->get_visible_name(),
113 'path' => $encodedpath,
115 'thumbnail' => $OUTPUT->pix_url('f/folder-32')->out(false)
119 $encodedpath = base64_encode(serialize($child->get_params()));
121 'title' => $child->get_visible_name(),
124 'source'=> $encodedpath,
125 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($child->get_visible_name(), 32))->out(false)
131 // if file doesn't exist, build path nodes root of current context
132 $pathnodes = array();
133 $fileinfo = $browser->get_file_info($context, null, null, null, null, null);
134 $encodedpath = base64_encode(serialize($fileinfo->get_params()));
135 $pathnodes[] = array('name'=>$fileinfo->get_visible_name(), 'path'=>$encodedpath);
136 $level = $fileinfo->get_parent();
138 $encodedpath = base64_encode(serialize($level->get_params()));
139 $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath);
140 $level = $level->get_parent();
142 if (!empty($pathnodes) && is_array($pathnodes)) {
143 $pathnodes = array_reverse($pathnodes);
144 $ret['path'] = $pathnodes;
148 $ret['list'] = array_filter($list, array($this, 'filter'));
153 * Local file don't support to link to external links
157 public function supported_returntypes() {
158 return FILE_INTERNAL;
162 * Copy a file to file area
164 * @global object $USER
166 * @param string $encoded The metainfo of file, it is base64 encoded php serialized data
167 * @param string $draftitemid itemid
168 * @param string $new_filename The intended name of file
169 * @param string $new_filepath the new path in draft area
170 * @return array The information of file
172 public function copy_to_area($encoded, $draftitemid, $new_filepath, $new_filename) {
176 $browser = get_file_browser();
177 $fs = get_file_storage();
178 $user_context = get_context_instance(CONTEXT_USER, $USER->id);
181 $params = unserialize(base64_decode($encoded));
182 $contextid = clean_param($params['contextid'], PARAM_INT);
183 $fileitemid = clean_param($params['itemid'], PARAM_INT);
184 $filename = clean_param($params['filename'], PARAM_FILE);
185 $filepath = clean_param($params['filepath'], PARAM_PATH);;
186 $filearea = clean_param($params['filearea'], PARAM_ALPHAEXT);
187 $component = clean_param($params['component'], PARAM_ALPHAEXT);
188 $context = get_context_instance_by_id($contextid);
190 if ($existingfile = $fs->get_file($user_context->id, 'user', 'draft', $draftitemid, $new_filepath, $new_filename)) {
191 throw new moodle_exception('fileexists');
194 $file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename);
195 $file_info->copy_to_storage($user_context->id, 'user', 'draft', $draftitemid, $new_filepath, $new_filename);
197 $info['itemid'] = $draftitemid;
198 $info['title'] = $new_filename;
199 $info['contextid'] = $user_context->id;
200 $info['filesize'] = $file_info->get_filesize();
204 function get_file_count($contextid) {