Merge branch 'MDL-40047-master' of git://github.com/ankitagarwal/moodle
[moodle.git] / repository / draftfiles_manager.php
blobcc096419fd79c1e3e80cee9e157d045fa5cf7209
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
58 $areamaxbytes = optional_param('areamaxbytes', FILE_AREA_MAX_BYTES_UNLIMITED, PARAM_INT); // Area maxbytes.
60 // draft area
61 $newdirname = optional_param('newdirname', '', PARAM_FILE);
62 $newfilename = optional_param('newfilename', '', PARAM_FILE);
63 // path in draft area
64 $draftpath = optional_param('draftpath', '/', PARAM_PATH);
66 // user context
67 $user_context = context_user::instance($USER->id);
70 $PAGE->set_context($user_context);
72 $fs = get_file_storage();
74 $params = array('ctx_id' => $contextid, 'itemid' => $itemid, 'env' => $env, 'course'=>$courseid, 'maxbytes'=>$maxbytes, 'areamaxbytes'=>$areamaxbytes, 'maxfiles'=>$maxfiles, 'subdirs'=>$subdirs, 'sesskey'=>sesskey());
75 $PAGE->set_url('/repository/draftfiles_manager.php', $params);
76 $filepicker_url = new moodle_url($CFG->httpswwwroot."/repository/filepicker.php", $params);
78 $params['action'] = 'browse';
79 $home_url = new moodle_url('/repository/draftfiles_manager.php', $params);
81 switch ($action) {
83 // delete draft files
84 case 'deletedraft':
85 if ($file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename)) {
86 if ($file->is_directory()) {
87 $pathname = $draftpath;
88 if ($file->get_parent_directory()) {
89 $draftpath = $file->get_parent_directory()->get_filepath();
90 } else {
91 $draftpath = '/';
94 // delete files in folder
95 $files = $fs->get_directory_files($user_context->id, 'user', 'draft', $itemid, $pathname, true);
96 foreach ($files as $storedfile) {
97 $storedfile->delete();
99 $file->delete();
100 } else {
101 $file->delete();
103 $home_url->param('draftpath', $draftpath);
104 $home_url->param('action', 'browse');
105 redirect($home_url);
107 break;
109 case 'renameform':
110 echo $OUTPUT->header();
111 echo '<div><a href="' . $home_url->out() . '">'.get_string('back', 'repository')."</a></div>";
112 $home_url->param('draftpath', $draftpath);
113 $home_url->param('action', 'rename');
114 echo ' <form method="post" action="'.$home_url->out().'">';
115 echo html_writer::label(get_string('enternewname', 'repository'), 'newfilename', array('class' => 'accesshide'));
116 echo ' <input id="newfilename" name="newfilename" type="text" value="'.s($filename).'" />';
117 echo ' <input name="filename" type="hidden" value="'.s($filename).'" />';
118 echo ' <input name="draftpath" type="hidden" value="'.s($draftpath).'" />';
119 echo ' <input type="submit" value="'.s(get_string('rename', 'moodle')).'" />';
120 echo ' </form>';
121 echo $OUTPUT->footer();
122 break;
124 case 'rename':
125 repository::update_draftfile($itemid, $draftpath, $filename, array('filename' => $newfilename));
126 $home_url->param('action', 'browse');
127 $home_url->param('draftpath', $draftpath);
128 redirect($home_url);
129 break;
131 case 'downloaddir':
132 $zipper = new zip_packer();
134 $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, '.');
135 if ($draftpath === '/') {
136 $filename = get_string('files').'.zip';
137 } else {
138 $filename = explode('/', trim($draftpath, '/'));
139 $filename = array_pop($filename) . '.zip';
142 $newdraftitemid = file_get_unused_draft_itemid();
143 if ($newfile = $zipper->archive_to_storage(array('/' => $file), $user_context->id, 'user', 'draft', $newdraftitemid, '/', $filename, $USER->id)) {
144 $fileurl = moodle_url::make_draftfile_url($newdraftitemid, '/', $filename)->out();
145 header('Location: ' . $fileurl);
146 } else {
147 print_error('cannotdownloaddir', 'repository');
149 break;
151 case 'zip':
152 $zipper = new zip_packer();
154 $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, '.');
155 if (!$file->get_parent_directory()) {
156 $parent_path = '/';
157 $filepath = '/';
158 $filename = get_string('files').'.zip';
159 } else {
160 $parent_path = $file->get_parent_directory()->get_filepath();
161 $filepath = explode('/', trim($file->get_filepath(), '/'));
162 $filepath = array_pop($filepath);
163 $filename = $filepath.'.zip';
166 $filename = repository::get_unused_filename($itemid, $parent_path, $filename);
167 $newfile = $zipper->archive_to_storage(array($filepath => $file), $user_context->id, 'user', 'draft', $itemid, $parent_path, $filename, $USER->id);
169 $home_url->param('action', 'browse');
170 $home_url->param('draftpath', $parent_path);
171 redirect($home_url, get_string('ziped', 'repository'));
172 break;
174 case 'unzip':
175 $zipper = new zip_packer();
176 $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename);
178 if ($newfile = $file->extract_to_storage($zipper, $user_context->id, 'user', 'draft', $itemid, $draftpath, $USER->id)) {
179 $str = get_string('unzipped', 'repository');
180 } else {
181 $str = get_string('cannotunzip', 'error');
183 $home_url->param('action', 'browse');
184 $home_url->param('draftpath', $draftpath);
185 redirect($home_url, $str);
186 break;
188 case 'movefile':
189 if (!empty($targetpath)) {
190 repository::update_draftfile($itemid, $draftpath, $filename, array('filepath' => $targetpath));
191 $home_url->param('action', 'browse');
192 $home_url->param('draftpath', $targetpath);
193 redirect($home_url);
195 echo $OUTPUT->header();
197 echo $OUTPUT->container_start();
198 echo html_writer::link($home_url, get_string('back', 'repository'));
199 echo $OUTPUT->container_end();
201 $data = new stdClass();
202 $home_url->param('action', 'movefile');
203 $home_url->param('draftpath', $draftpath);
204 $home_url->param('filename', $filename);
205 file_get_drafarea_folders($itemid, '/', $data);
206 print_draft_area_tree($data, true, $home_url);
207 echo $OUTPUT->footer();
208 break;
210 case 'mkdirform':
211 echo $OUTPUT->header();
213 echo $OUTPUT->container_start();
214 echo html_writer::link($home_url, get_string('back', 'repository'));
215 echo $OUTPUT->container_end();
217 $home_url->param('draftpath', $draftpath);
218 $home_url->param('action', 'mkdir');
219 echo ' <form method="post" action="'.$home_url->out().'">';
220 echo html_writer::label(get_string('entername', 'repository'), 'newdirname', array('class' => 'accesshide'));
221 echo ' <input name="newdirname" id="newdirname" type="text" />';
222 echo ' <input name="draftpath" type="hidden" value="'.s($draftpath).'" />';
223 echo ' <input type="submit" value="'.s(get_string('makeafolder', 'moodle')).'" />';
224 echo ' </form>';
225 echo $OUTPUT->footer();
226 break;
228 case 'mkdir':
230 $newfolderpath = $draftpath . trim($newdirname, '/') . '/';
231 $fs->create_directory($user_context->id, 'user', 'draft', $itemid, $newfolderpath);
232 $home_url->param('action', 'browse');
233 if (!empty($newdirname)) {
234 $home_url->param('draftpath', $newfolderpath);
235 $str = get_string('createfoldersuccess', 'repository');
236 } else {
237 $home_url->param('draftpath', $draftpath);
238 $str = get_string('createfolderfail', 'repository');
240 redirect($home_url, $str);
241 break;
243 case 'browse':
244 default:
245 $files = file_get_drafarea_files($itemid, $draftpath);
246 $info = file_get_draft_area_info($itemid);
247 $filecount = $info['filecount'];
249 echo $OUTPUT->header();
250 if ((!empty($files) or $draftpath != '/') and $env == 'filemanager') {
251 echo '<div class="fm-breadcrumb">';
252 $home_url->param('action', 'browse');
253 $home_url->param('draftpath', '/');
254 echo '<a href="'.$home_url->out().'">' . get_string('files') . '</a> ▶';
255 $trail = '';
256 if ($draftpath !== '/') {
257 $path = '/' . trim($draftpath, '/') . '/';
258 $parts = explode('/', $path);
259 foreach ($parts as $part) {
260 if ($part != '') {
261 $trail .= ('/'.$part.'/');
262 $data->path[] = array('name'=>$part, 'path'=>$trail);
263 $home_url->param('draftpath', $trail);
264 echo ' <a href="'.$home_url->out().'">'.$part.'</a> ▶ ';
268 echo '</div>';
271 $filepicker_url->param('draftpath', $draftpath);
272 $filepicker_url->param('savepath', $draftpath);
273 $filepicker_url->param('action', 'plugins');
274 echo '<div class="filemanager-toolbar">';
275 if ($env == 'filepicker') {
276 $maxfiles = 1;
278 if ($filecount < $maxfiles || $maxfiles == -1) {
279 echo ' <a href="'.$filepicker_url->out().'">'.get_string('addfile', 'repository').'</a>';
281 if ($env == 'filemanager') {
282 if (!empty($subdirs)) {
283 $home_url->param('action', 'mkdirform');
284 echo ' <a href="'.$home_url->out().'">'.get_string('makeafolder', 'moodle').'</a>';
286 if (!empty($files->list)) {
287 $home_url->param('action', 'downloaddir');
288 echo ' ' . html_writer::link($home_url, get_string('downloadfolder', 'repository'), array('target'=>'_blank'));
291 echo '</div>';
293 if (!empty($files->list)) {
294 echo '<ul>';
295 foreach ($files->list as $file) {
296 if ($file->type != 'folder') {
297 $drafturl = $file->url;
298 // a file
299 echo '<li>';
300 echo $OUTPUT->pix_icon(file_file_icon($file), '', 'moodle', array('class' => 'iconsmall'));
301 echo html_writer::link($drafturl, $file->filename);
303 $home_url->param('filename', $file->filename);
304 $home_url->param('draftpath', $file->filepath);
306 $home_url->param('action', 'deletedraft');
307 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('delete').'</a>]';
309 $home_url->param('action', 'movefile');
310 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('move').'</a>]';
312 $home_url->param('action', 'renameform');
313 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('rename').'</a>]';
315 if (file_extension_in_typegroup($file->filename, 'archive', true)) {
316 $home_url->param('action', 'unzip');
317 $home_url->param('draftpath', $file->filepath);
318 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('unzip').'</a>]';
321 echo '</li>';
322 } else {
323 // a folder
324 echo '<li>';
325 echo '<img src="'.$OUTPUT->pix_url(file_folder_icon()) . '" class="iconsmall" />';
327 $home_url->param('action', 'browse');
328 $home_url->param('draftpath', $file->filepath);
329 $filepathchunks = explode('/', trim($file->filepath, '/'));
330 $foldername = trim(array_pop($filepathchunks), '/');
331 echo html_writer::link($home_url, $foldername);
333 $home_url->param('draftpath', $file->filepath);
334 $home_url->param('filename', $file->filename);
335 $home_url->param('action', 'deletedraft');
336 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('delete').'</a>]';
338 $home_url->param('action', 'zip');
339 echo ' [<a href="'.$home_url->out().'" class="fm-operation">Zip</a>]';
340 echo '</li>';
343 echo '</ul>';
344 } else {
345 echo get_string('nofilesavailable', 'repository');
347 echo $OUTPUT->footer();
348 break;
351 function print_draft_area_tree($tree, $root, $url) {
352 echo '<ul>';
353 if ($root) {
354 $url->param('targetpath', '/');
355 if ($url->param('draftpath') == '/') {
356 echo '<li>'.get_string('files').'</li>';
357 } else {
358 echo '<li><a href="'.$url->out().'">'.get_string('files').'</a></li>';
360 echo '<ul>';
361 if (isset($tree->children)) {
362 $tree = $tree->children;
366 if (!empty($tree)) {
367 foreach ($tree as $node) {
368 echo '<li>';
369 $url->param('targetpath', $node->filepath);
370 if ($url->param('draftpath') != $node->filepath) {
371 echo '<a href="'.$url->out().'">'.$node->fullname.'</a>';
372 } else {
373 echo $node->fullname;
375 echo '</li>';
376 if (!empty($node->children)) {
377 print_draft_area_tree($node->children, false, $url);
381 if ($root) {
382 echo '</ul>';
384 echo '</ul>';