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
23 * @subpackage repository
24 * @copyright 2009 Dongsheng Cai
25 * @author Dongsheng Cai <dongsheng@moodle.com>
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 class repository_local extends repository {
32 * initialize local plugin
33 * @param int $repositoryid
35 * @param array $options
37 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
38 parent::__construct($repositoryid, $context, $options);
42 * local plugin doesn't require login, so list all files
45 public function print_login() {
46 return $this->get_listing();
50 * Not supported by File API yet
51 * @param string $search_text
54 public function search($search_text) {
61 * @param string $encodedpath
64 public function get_listing($encodedpath = '') {
65 global $CFG, $USER, $OUTPUT;
67 $ret['dynload'] = true;
68 $ret['nosearch'] = true;
69 $ret['nologin'] = true;
72 if (!empty($encodedpath)) {
73 $params = unserialize(base64_decode($encodedpath));
74 if (is_array($params)) {
75 $itemid = $params['itemid'];
76 $filename = $params['filename'];
77 $filearea = $params['filearea'];
78 $filepath = $params['filepath'];
79 $context = get_context_instance_by_id($params['contextid']);
86 $context = get_system_context();
90 $browser = get_file_browser();
92 if ($fileinfo = $browser->get_file_info($context, $filearea, $itemid, $filepath, $filename)) {
93 // build path navigation
95 $encodedpath = base64_encode(serialize($fileinfo->get_params()));
96 $pathnodes[] = array('name'=>$fileinfo->get_visible_name(), 'path'=>$encodedpath);
97 $level = $fileinfo->get_parent();
99 $encodedpath = base64_encode(serialize($level->get_params()));
100 $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath);
101 $level = $level->get_parent();
103 if (!empty($pathnodes) && is_array($pathnodes)) {
104 $pathnodes = array_reverse($pathnodes);
105 $ret['path'] = $pathnodes;
108 $children = $fileinfo->get_children();
109 foreach ($children as $child) {
110 if ($child->is_directory()) {
111 $params = $child->get_params();
112 $encodedpath = base64_encode(serialize($params));
113 // hide user_private area from local plugin, user should
114 // use private file plugin to access private files
115 if ($params['filearea'] == 'user_private') {
119 'title' => $child->get_visible_name(),
122 'path' => $encodedpath,
124 'thumbnail' => $OUTPUT->pix_url('f/folder-32') . ''
128 $encodedpath = base64_encode(serialize($child->get_params()));
129 $icon = 'f/'.str_replace('.gif', '', mimeinfo('icon', $child->get_visible_name())).'-32';
131 'title' => $child->get_visible_name(),
134 'source'=> $encodedpath,
135 'thumbnail' => $OUTPUT->pix_url($icon) . '',
141 } catch (Exception $e) {
142 throw new repository_exception('emptyfilelist', 'repository_local');
144 $ret['list'] = $list;
145 $ret['list'] = array_filter($list, array($this, 'filter'));
150 * Set repository name
152 * @return string repository name
154 public function get_name(){
155 return get_string('repositoryname', 'repository_local');;
159 * Local file don't support to link to external links
163 public function supported_returntypes() {
164 return FILE_INTERNAL;
168 * Copy a file to file area
170 * @global object $USER
172 * @param string $encoded The metainfo of file, it is base64 encoded php seriablized data
173 * @param string $new_filename The intended name of file
174 * @param string $new_itemid itemid
175 * @param string $new_filepath the new path in draft area
176 * @return array The information of file
178 public function copy_to_area($encoded, $new_filearea='user_draft', $new_itemid = '', $new_filepath = '/', $new_filename = '') {
182 $browser = get_file_browser();
183 $params = unserialize(base64_decode($encoded));
184 $user_context = get_context_instance(CONTEXT_USER, $USER->id);
186 $contextid = $params['contextid'];
187 $filearea = $params['filearea'];
188 $filepath = $params['filepath'];
189 $filename = $params['filename'];
190 $fileitemid = $params['itemid'];
191 $context = get_context_instance_by_id($contextid);
193 $file_info = $browser->get_file_info($context, $filearea, $fileitemid, $filepath, $filename);
194 $file_info->copy_to_storage($user_context->id, $new_filearea, $new_itemid, $new_filepath, $new_filename);
195 } catch (Exception $e) {
199 $info['itemid'] = $new_itemid;
200 $info['title'] = $new_filename;
201 $info['contextid'] = $user_context->id;
202 $info['filesize'] = $file_info->get_filesize();