MDL-13766, improve repository plugins' icons
[moodle.git] / repository / local / lib.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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.
14 //
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/>.
18 /**
19  * repository_local class is used to browse moodle files
20  *
21  * @since 2.0
22  * @package    repository
23  * @subpackage local
24  * @copyright  2009 Dongsheng Cai <dongsheng@moodle.com>
25  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26  */
28 class repository_local extends repository {
30     /**
31      * local plugin doesn't require login, so list all files
32      * @return mixed
33      */
34     public function print_login() {
35         return $this->get_listing();
36     }
38     /**
39      * Get file listing
40      *
41      * @param string $encodedpath
42      * @return mixed
43      */
44     public function get_listing($encodedpath = '') {
45         global $CFG, $USER, $OUTPUT;
46         $ret = array();
47         $ret['dynload'] = true;
48         $ret['nosearch'] = true;
49         $ret['nologin'] = true;
50         $list = array();
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));
61             }
62         } else {
63             $itemid   = null;
64             $filename = null;
65             $filearea = null;
66             $filepath = null;
67             $component = null;
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);
72             } else {
73                 $context = get_system_context();
74             }
75         }
77         $browser = get_file_browser();
79         if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
80             // build path navigation
81             $pathnodes = array();
82             $encodedpath = base64_encode(serialize($fileinfo->get_params()));
83             $pathnodes[] = array('name'=>$fileinfo->get_visible_name(), 'path'=>$encodedpath);
84             $level = $fileinfo->get_parent();
85             while ($level) {
86                 $encodedpath = base64_encode(serialize($level->get_params()));
87                 $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath);
88                 $level = $level->get_parent();
89             }
90             if (!empty($pathnodes) && is_array($pathnodes)) {
91                 $pathnodes = array_reverse($pathnodes);
92                 $ret['path'] = $pathnodes;
93             }
94             // build file tree
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)) {
101                         //continue;
102                     //}
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') {
107                         //continue;
108                     //}
109                     $node = array(
110                         'title' => $child->get_visible_name(),
111                         'size' => 0,
112                         'date' => '',
113                         'path' => $encodedpath,
114                         'children'=>array(),
115                         'thumbnail' => $OUTPUT->pix_url('f/folder-32')->out(false)
116                     );
117                     $list[] = $node;
118                 } else {
119                     $encodedpath = base64_encode(serialize($child->get_params()));
120                     $node = array(
121                         'title' => $child->get_visible_name(),
122                         'size' => 0,
123                         'date' => '',
124                         'source'=> $encodedpath,
125                         'thumbnail' => $OUTPUT->pix_url(file_extension_icon($child->get_visible_name(), 32))->out(false)
126                     );
127                     $list[] = $node;
128                 }
129             }
130         } else {
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();
137             while ($level) {
138                 $encodedpath = base64_encode(serialize($level->get_params()));
139                 $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath);
140                 $level = $level->get_parent();
141             }
142             if (!empty($pathnodes) && is_array($pathnodes)) {
143                 $pathnodes = array_reverse($pathnodes);
144                 $ret['path'] = $pathnodes;
145             }
146             $list = array();
147         }
148         $ret['list'] = array_filter($list, array($this, 'filter'));
149         return $ret;
150     }
152     /**
153      * Local file don't support to link to external links
154      *
155      * @return int
156      */
157     public function supported_returntypes() {
158         return FILE_INTERNAL;
159     }
161     /**
162      * Copy a file to file area
163      *
164      * @global object $USER
165      * @global object $DB
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
171      */
172     public function copy_to_area($encoded, $draftitemid, $new_filepath, $new_filename) {
173         global $USER, $DB;
174         $info = array();
176         $browser = get_file_browser();
177         $fs = get_file_storage();
178         $user_context = get_context_instance(CONTEXT_USER, $USER->id);
180         // the final file
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');
192         }
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();
202         return $info;
203     }
204     function get_file_count($contextid) {
205         global $DB;
206     }