Merge branch 'install_22_STABLE' of git://git.moodle.cz/moodle-install into MOODLE_22...
[moodle.git] / repository / draftfiles_manager.php
blobd48b09f5f229b49ac50ab2ff7aa6690fb708126c
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.
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/>.
21 // NOTE TO ALL DEVELOPERS: this script must deal only with draft area of current user, it has to use only file_storage and no file_browser!!
25 /**
26 * This file is used to manage draft files in non-javascript browsers
28 * @since 2.0
29 * @package core
30 * @subpackage repository
31 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 require_once('../config.php');
36 require_once($CFG->libdir.'/filelib.php');
37 require_once('lib.php');
39 require_sesskey();
40 require_login();
42 // disable blocks in this page
43 $PAGE->set_pagelayout('embedded');
45 // general parameters
46 $action = optional_param('action', '', PARAM_ALPHA);
47 $itemid = optional_param('itemid', '', PARAM_INT);
49 // parameters for repository
50 $contextid = optional_param('ctx_id', SYSCONTEXTID, PARAM_INT); // context ID
51 $courseid = optional_param('course', SITEID, PARAM_INT); // course ID
52 $env = optional_param('env', 'filepicker', PARAM_ALPHA); // opened in file picker, file manager or html editor
53 $filename = optional_param('filename', '', PARAM_FILE);
54 $targetpath = optional_param('targetpath', '', PARAM_PATH);
55 $maxfiles = optional_param('maxfiles', -1, PARAM_INT); // maxfiles
56 $maxbytes = optional_param('maxbytes', 0, PARAM_INT); // maxbytes
57 $subdirs = optional_param('subdirs', 0, PARAM_INT); // maxbytes
59 // draft area
60 $newdirname = optional_param('newdirname', '', PARAM_FILE);
61 $newfilename = optional_param('newfilename', '', PARAM_FILE);
62 // path in draft area
63 $draftpath = optional_param('draftpath', '/', PARAM_PATH);
65 // user context
66 $user_context = get_context_instance(CONTEXT_USER, $USER->id);
69 $PAGE->set_context($user_context);
71 $fs = get_file_storage();
73 $params = array('ctx_id' => $contextid, 'itemid' => $itemid, 'env' => $env, 'course'=>$courseid, 'maxbytes'=>$maxbytes, 'maxfiles'=>$maxfiles, 'subdirs'=>$subdirs, 'sesskey'=>sesskey());
74 $PAGE->set_url('/repository/draftfiles_manager.php', $params);
75 $filepicker_url = new moodle_url($CFG->httpswwwroot."/repository/filepicker.php", $params);
77 $params['action'] = 'browse';
78 $home_url = new moodle_url('/repository/draftfiles_manager.php', $params);
80 switch ($action) {
82 // delete draft files
83 case 'deletedraft':
84 if ($file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename)) {
85 if ($file->is_directory()) {
86 $pathname = $draftpath;
87 if ($file->get_parent_directory()) {
88 $draftpath = $file->get_parent_directory()->get_filepath();
89 } else {
90 $draftpath = '/';
93 // delete files in folder
94 $files = $fs->get_directory_files($user_context->id, 'user', 'draft', $itemid, $pathname, true);
95 foreach ($files as $storedfile) {
96 $storedfile->delete();
98 $file->delete();
99 } else {
100 $file->delete();
102 $home_url->param('draftpath', $draftpath);
103 $home_url->param('action', 'browse');
104 redirect($home_url);
106 break;
108 case 'renameform':
109 echo $OUTPUT->header();
110 echo '<div><a href="' . $home_url->out() . '">'.get_string('back', 'repository')."</a></div>";
111 $home_url->param('draftpath', $draftpath);
112 $home_url->param('action', 'rename');
113 echo ' <form method="post" action="'.$home_url->out().'">';
114 echo ' <input name="newfilename" type="text" value="'.s($filename).'" />';
115 echo ' <input name="filename" type="hidden" value="'.s($filename).'" />';
116 echo ' <input name="draftpath" type="hidden" value="'.s($draftpath).'" />';
117 echo ' <input type="submit" value="'.s(get_string('rename', 'moodle')).'" />';
118 echo ' </form>';
119 echo $OUTPUT->footer();
120 break;
122 case 'rename':
124 if ($fs->file_exists($user_context->id, 'user', 'draft', $itemid, $draftpath, $newfilename)) {
125 print_error('fileexists');
126 } else if ($file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename)) {
127 $newfile = $fs->create_file_from_storedfile(array('filename'=>$newfilename), $file);
128 $file->delete();
131 $home_url->param('action', 'browse');
132 $home_url->param('draftpath', $draftpath);
133 redirect($home_url);
134 break;
136 case 'downloaddir':
137 $zipper = new zip_packer();
139 $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, '.');
140 if ($file->get_parent_directory()) {
141 $parent_path = $file->get_parent_directory()->get_filepath();
142 $filename = trim($draftpath, '/').'.zip';
143 } else {
144 $parent_path = '/';
145 $filename = get_string('files').'.zip';
148 if ($newfile = $zipper->archive_to_storage(array($file), $user_context->id, 'user', 'draft', $itemid, $parent_path, $filename, $USER->id)) {
149 $fileurl = moodle_url::make_draftfile_url($itemid, '/', $filename)->out();
150 header('Location: ' . $fileurl );
151 } else {
152 print_error('cannotdownloaddir', 'repository');
154 break;
156 case 'zip':
157 $zipper = new zip_packer();
159 $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, '.');
160 if (!$file->get_parent_directory()) {
161 $parent_path = '/';
162 $filename = get_string('files').'.zip';
163 } else {
164 $parent_path = $file->get_parent_directory()->get_filepath();
165 $filepath = explode('/', trim($file->get_filepath(), '/'));
166 $filename = array_pop($filepath).'.zip';
169 $newfile = $zipper->archive_to_storage(array($file), $user_context->id, 'user', 'draft', $itemid, $parent_path, $filename, $USER->id);
171 $home_url->param('action', 'browse');
172 $home_url->param('draftpath', $parent_path);
173 redirect($home_url, get_string('ziped', 'repository'));
174 break;
176 case 'unzip':
177 $zipper = new zip_packer();
178 $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename);
180 if ($newfile = $file->extract_to_storage($zipper, $user_context->id, 'user', 'draft', $itemid, $draftpath, $USER->id)) {
181 $str = get_string('unzipped', 'repository');
182 } else {
183 $str = get_string('cannotunzip', 'error');
185 $home_url->param('action', 'browse');
186 $home_url->param('draftpath', $draftpath);
187 redirect($home_url, $str);
188 break;
190 case 'movefile':
191 if (!empty($targetpath)) {
192 if ($fs->file_exists($user_context->id, 'user', 'draft', $itemid, $targetpath, $filename)) {
193 print_error('cannotmovefile');
194 } else if ($file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename)) {
195 $newfile = $fs->create_file_from_storedfile(array('filepath'=>$targetpath), $file);
196 $file->delete();
197 } else {
198 var_dump('cannot find file');
199 die;
201 $home_url->param('action', 'browse');
202 $home_url->param('draftpath', $targetpath);
203 redirect($home_url);
205 echo $OUTPUT->header();
207 echo $OUTPUT->container_start();
208 echo html_writer::link($home_url, get_string('back', 'repository'));
209 echo $OUTPUT->container_end();
211 $data = new stdClass();
212 $home_url->param('action', 'movefile');
213 $home_url->param('draftpath', $draftpath);
214 $home_url->param('filename', $filename);
215 file_get_drafarea_folders($itemid, '/', $data);
216 print_draft_area_tree($data, true, $home_url);
217 echo $OUTPUT->footer();
218 break;
220 case 'mkdirform':
221 echo $OUTPUT->header();
223 echo $OUTPUT->container_start();
224 echo html_writer::link($home_url, get_string('back', 'repository'));
225 echo $OUTPUT->container_end();
227 $home_url->param('draftpath', $draftpath);
228 $home_url->param('action', 'mkdir');
229 echo ' <form method="post" action="'.$home_url->out().'">';
230 echo ' <input name="newdirname" type="text" />';
231 echo ' <input name="draftpath" type="hidden" value="'.s($draftpath).'" />';
232 echo ' <input type="submit" value="'.s(get_string('makeafolder', 'moodle')).'" />';
233 echo ' </form>';
234 echo $OUTPUT->footer();
235 break;
237 case 'mkdir':
239 $newfolderpath = $draftpath . trim($newdirname, '/') . '/';
240 $fs->create_directory($user_context->id, 'user', 'draft', $itemid, $newfolderpath);
241 $home_url->param('action', 'browse');
242 if (!empty($newdirname)) {
243 $home_url->param('draftpath', $newfolderpath);
244 $str = get_string('createfoldersuccess', 'repository');
245 } else {
246 $home_url->param('draftpath', $draftpath);
247 $str = get_string('createfolderfail', 'repository');
249 redirect($home_url, $str);
250 break;
252 case 'browse':
253 default:
254 $files = file_get_drafarea_files($itemid, $draftpath);
255 $info = file_get_draft_area_info($itemid);
256 $filecount = $info['filecount'];
258 echo $OUTPUT->header();
259 if ((!empty($files) or $draftpath != '/') and $env == 'filemanager') {
260 echo '<div class="fm-breadcrumb">';
261 $home_url->param('action', 'browse');
262 $home_url->param('draftpath', '/');
263 echo '<a href="'.$home_url->out().'">' . get_string('files') . '</a> ▶';
264 $trail = '';
265 if ($draftpath !== '/') {
266 $path = '/' . trim($draftpath, '/') . '/';
267 $parts = explode('/', $path);
268 foreach ($parts as $part) {
269 if (!empty($part)) {
270 $trail .= ('/'.$part.'/');
271 $data->path[] = array('name'=>$part, 'path'=>$trail);
272 $home_url->param('draftpath', $trail);
273 echo ' <a href="'.$home_url->out().'">'.$part.'</a> ▶ ';
277 echo '</div>';
280 $filepicker_url->param('draftpath', $draftpath);
281 $filepicker_url->param('savepath', $draftpath);
282 $filepicker_url->param('action', 'plugins');
283 echo '<div class="filemanager-toolbar">';
284 if ($env == 'filepicker') {
285 $maxfiles = 1;
287 if ($filecount < $maxfiles || $maxfiles == -1) {
288 echo ' <a href="'.$filepicker_url->out().'">'.get_string('addfile', 'repository').'</a>';
290 if ($env == 'filemanager') {
291 if (!empty($subdirs)) {
292 $home_url->param('action', 'mkdirform');
293 echo ' <a href="'.$home_url->out().'">'.get_string('makeafolder', 'moodle').'</a>';
295 $home_url->param('action', 'downloaddir');
296 echo html_writer::link($home_url, get_string('downloadfolder', 'repository'), array('target'=>'_blank'));
298 echo '</div>';
300 if (!empty($files->list)) {
301 echo '<ul>';
302 foreach ($files->list as $file) {
303 if ($file->type != 'folder') {
304 $drafturl = $file->url;
305 // a file
306 $fileicon = $OUTPUT->pix_url(file_extension_icon($file->filename))->out(false);
307 $type = mimeinfo('icon', $file->filename);
308 echo '<li>';
309 echo '<img src="'.$fileicon. '" class="iconsmall" />';
310 echo html_writer::link($drafturl, $file->filename);
312 $home_url->param('filename', $file->filename);
313 $home_url->param('draftpath', $file->filepath);
315 $home_url->param('action', 'deletedraft');
316 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('delete').'</a>]';
318 $home_url->param('action', 'movefile');
319 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('move').'</a>]';
321 $home_url->param('action', 'renameform');
322 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('rename').'</a>]';
324 if ($type == 'zip') {
325 $home_url->param('action', 'unzip');
326 $home_url->param('draftpath', $file->filepath);
327 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('unzip').'</a>]';
330 echo '</li>';
331 } else {
332 // a folder
333 echo '<li>';
334 echo '<img src="'.$OUTPUT->pix_url('f/folder') . '" class="iconsmall" />';
336 $home_url->param('action', 'browse');
337 $home_url->param('draftpath', $file->filepath);
338 $foldername = trim(array_pop(explode('/', trim($file->filepath, '/'))), '/');
339 echo html_writer::link($home_url, $foldername);
341 $home_url->param('draftpath', $file->filepath);
342 $home_url->param('filename', $file->filename);
343 $home_url->param('action', 'deletedraft');
344 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('delete').'</a>]';
346 $home_url->param('action', 'zip');
347 echo ' [<a href="'.$home_url->out().'" class="fm-operation">Zip</a>]';
348 echo '</li>';
351 echo '</ul>';
352 } else {
353 echo get_string('nofilesavailable', 'repository');
355 echo $OUTPUT->footer();
356 break;
359 function print_draft_area_tree($tree, $root, $url) {
360 echo '<ul>';
361 if ($root) {
362 $url->param('targetpath', '/');
363 if ($url->param('draftpath') == '/') {
364 echo '<li>'.get_string('files').'</li>';
365 } else {
366 echo '<li><a href="'.$url->out().'">'.get_string('files').'</a></li>';
368 echo '<ul>';
369 if (isset($tree->children)) {
370 $tree = $tree->children;
374 if (!empty($tree)) {
375 foreach ($tree as $node) {
376 echo '<li>';
377 $url->param('targetpath', $node->filepath);
378 if ($url->param('draftpath') != $node->filepath) {
379 echo '<a href="'.$url->out().'">'.$node->fullname.'</a>';
380 } else {
381 echo $node->fullname;
383 echo '</li>';
384 if (!empty($node->children)) {
385 print_draft_area_tree($node->children, false, $url);
389 if ($root) {
390 echo '</ul>';
392 echo '</ul>';