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/>.
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!!
26 * This file is used to manage draft files in non-javascript browsers
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');
42 // disable blocks in this page
43 $PAGE->set_pagelayout('embedded');
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
60 $newdirname = optional_param('newdirname', '', PARAM_FILE
);
61 $newfilename = optional_param('newfilename', '', PARAM_FILE
);
63 $draftpath = optional_param('draftpath', '/', PARAM_PATH
);
66 $user_context = context_user
::instance($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);
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();
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();
102 $home_url->param('draftpath', $draftpath);
103 $home_url->param('action', 'browse');
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')).'" />';
119 echo $OUTPUT->footer();
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);
131 $home_url->param('action', 'browse');
132 $home_url->param('draftpath', $draftpath);
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';
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 );
152 print_error('cannotdownloaddir', 'repository');
157 $zipper = new zip_packer();
159 $file = $fs->get_file($user_context->id
, 'user', 'draft', $itemid, $draftpath, '.');
160 if (!$file->get_parent_directory()) {
162 $filename = get_string('files').'.zip';
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'));
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');
183 $str = get_string('cannotunzip', 'error');
185 $home_url->param('action', 'browse');
186 $home_url->param('draftpath', $draftpath);
187 redirect($home_url, $str);
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);
198 var_dump('cannot find file');
201 $home_url->param('action', 'browse');
202 $home_url->param('draftpath', $targetpath);
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();
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')).'" />';
234 echo $OUTPUT->footer();
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');
246 $home_url->param('draftpath', $draftpath);
247 $str = get_string('createfolderfail', 'repository');
249 redirect($home_url, $str);
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> ▶';
265 if ($draftpath !== '/') {
266 $path = '/' . trim($draftpath, '/') . '/';
267 $parts = explode('/', $path);
268 foreach ($parts as $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> ▶ ';
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') {
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'));
300 if (!empty($files->list)) {
302 foreach ($files->list as $file) {
303 if ($file->type
!= 'folder') {
304 $drafturl = $file->url
;
307 echo $OUTPUT->pix_icon(file_file_icon($file), '', 'moodle', array('class' => 'iconsmall'));
308 echo html_writer
::link($drafturl, $file->filename
);
310 $home_url->param('filename', $file->filename
);
311 $home_url->param('draftpath', $file->filepath
);
313 $home_url->param('action', 'deletedraft');
314 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('delete').'</a>]';
316 $home_url->param('action', 'movefile');
317 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('move').'</a>]';
319 $home_url->param('action', 'renameform');
320 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('rename').'</a>]';
322 if (file_extension_in_typegroup($file->filename
, 'archive', true)) {
323 $home_url->param('action', 'unzip');
324 $home_url->param('draftpath', $file->filepath
);
325 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('unzip').'</a>]';
332 echo '<img src="'.$OUTPUT->pix_url(file_folder_icon()) . '" class="iconsmall" />';
334 $home_url->param('action', 'browse');
335 $home_url->param('draftpath', $file->filepath
);
336 $filepathchunks = explode('/', trim($file->filepath
, '/'));
337 $foldername = trim(array_pop($filepathchunks), '/');
338 echo html_writer
::link($home_url, $foldername);
340 $home_url->param('draftpath', $file->filepath
);
341 $home_url->param('filename', $file->filename
);
342 $home_url->param('action', 'deletedraft');
343 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('delete').'</a>]';
345 $home_url->param('action', 'zip');
346 echo ' [<a href="'.$home_url->out().'" class="fm-operation">Zip</a>]';
352 echo get_string('nofilesavailable', 'repository');
354 echo $OUTPUT->footer();
358 function print_draft_area_tree($tree, $root, $url) {
361 $url->param('targetpath', '/');
362 if ($url->param('draftpath') == '/') {
363 echo '<li>'.get_string('files').'</li>';
365 echo '<li><a href="'.$url->out().'">'.get_string('files').'</a></li>';
368 if (isset($tree->children
)) {
369 $tree = $tree->children
;
374 foreach ($tree as $node) {
376 $url->param('targetpath', $node->filepath
);
377 if ($url->param('draftpath') != $node->filepath
) {
378 echo '<a href="'.$url->out().'">'.$node->fullname
.'</a>';
380 echo $node->fullname
;
383 if (!empty($node->children
)) {
384 print_draft_area_tree($node->children
, false, $url);