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 = 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);
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();
123 repository
::update_draftfile($itemid, $draftpath, $filename, array('filename' => $newfilename));
124 $home_url->param('action', 'browse');
125 $home_url->param('draftpath', $draftpath);
130 $zipper = new zip_packer();
132 $file = $fs->get_file($user_context->id
, 'user', 'draft', $itemid, $draftpath, '.');
133 if ($draftpath === '/') {
134 $filename = get_string('files').'.zip';
136 $filename = explode('/', trim($draftpath, '/'));
137 $filename = array_pop($filename) . '.zip';
140 $newdraftitemid = file_get_unused_draft_itemid();
141 if ($newfile = $zipper->archive_to_storage(array('/' => $file), $user_context->id
, 'user', 'draft', $newdraftitemid, '/', $filename, $USER->id
)) {
142 $fileurl = moodle_url
::make_draftfile_url($newdraftitemid, '/', $filename)->out();
143 header('Location: ' . $fileurl);
145 print_error('cannotdownloaddir', 'repository');
150 $zipper = new zip_packer();
152 $file = $fs->get_file($user_context->id
, 'user', 'draft', $itemid, $draftpath, '.');
153 if (!$file->get_parent_directory()) {
156 $filename = get_string('files').'.zip';
158 $parent_path = $file->get_parent_directory()->get_filepath();
159 $filepath = explode('/', trim($file->get_filepath(), '/'));
160 $filepath = array_pop($filepath);
161 $filename = $filepath.'.zip';
164 $filename = repository
::get_unused_filename($itemid, $parent_path, $filename);
165 $newfile = $zipper->archive_to_storage(array($filepath => $file), $user_context->id
, 'user', 'draft', $itemid, $parent_path, $filename, $USER->id
);
167 $home_url->param('action', 'browse');
168 $home_url->param('draftpath', $parent_path);
169 redirect($home_url, get_string('ziped', 'repository'));
173 $zipper = new zip_packer();
174 $file = $fs->get_file($user_context->id
, 'user', 'draft', $itemid, $draftpath, $filename);
176 if ($newfile = $file->extract_to_storage($zipper, $user_context->id
, 'user', 'draft', $itemid, $draftpath, $USER->id
)) {
177 $str = get_string('unzipped', 'repository');
179 $str = get_string('cannotunzip', 'error');
181 $home_url->param('action', 'browse');
182 $home_url->param('draftpath', $draftpath);
183 redirect($home_url, $str);
187 if (!empty($targetpath)) {
188 repository
::update_draftfile($itemid, $draftpath, $filename, array('filepath' => $targetpath));
189 $home_url->param('action', 'browse');
190 $home_url->param('draftpath', $targetpath);
193 echo $OUTPUT->header();
195 echo $OUTPUT->container_start();
196 echo html_writer
::link($home_url, get_string('back', 'repository'));
197 echo $OUTPUT->container_end();
199 $data = new stdClass();
200 $home_url->param('action', 'movefile');
201 $home_url->param('draftpath', $draftpath);
202 $home_url->param('filename', $filename);
203 file_get_drafarea_folders($itemid, '/', $data);
204 print_draft_area_tree($data, true, $home_url);
205 echo $OUTPUT->footer();
209 echo $OUTPUT->header();
211 echo $OUTPUT->container_start();
212 echo html_writer
::link($home_url, get_string('back', 'repository'));
213 echo $OUTPUT->container_end();
215 $home_url->param('draftpath', $draftpath);
216 $home_url->param('action', 'mkdir');
217 echo ' <form method="post" action="'.$home_url->out().'">';
218 echo ' <input name="newdirname" type="text" />';
219 echo ' <input name="draftpath" type="hidden" value="'.s($draftpath).'" />';
220 echo ' <input type="submit" value="'.s(get_string('makeafolder', 'moodle')).'" />';
222 echo $OUTPUT->footer();
227 $newfolderpath = $draftpath . trim($newdirname, '/') . '/';
228 $fs->create_directory($user_context->id
, 'user', 'draft', $itemid, $newfolderpath);
229 $home_url->param('action', 'browse');
230 if (!empty($newdirname)) {
231 $home_url->param('draftpath', $newfolderpath);
232 $str = get_string('createfoldersuccess', 'repository');
234 $home_url->param('draftpath', $draftpath);
235 $str = get_string('createfolderfail', 'repository');
237 redirect($home_url, $str);
242 $files = file_get_drafarea_files($itemid, $draftpath);
243 $info = file_get_draft_area_info($itemid);
244 $filecount = $info['filecount'];
246 echo $OUTPUT->header();
247 if ((!empty($files) or $draftpath != '/') and $env == 'filemanager') {
248 echo '<div class="fm-breadcrumb">';
249 $home_url->param('action', 'browse');
250 $home_url->param('draftpath', '/');
251 echo '<a href="'.$home_url->out().'">' . get_string('files') . '</a> ▶';
253 if ($draftpath !== '/') {
254 $path = '/' . trim($draftpath, '/') . '/';
255 $parts = explode('/', $path);
256 foreach ($parts as $part) {
258 $trail .= ('/'.$part.'/');
259 $data->path
[] = array('name'=>$part, 'path'=>$trail);
260 $home_url->param('draftpath', $trail);
261 echo ' <a href="'.$home_url->out().'">'.$part.'</a> ▶ ';
268 $filepicker_url->param('draftpath', $draftpath);
269 $filepicker_url->param('savepath', $draftpath);
270 $filepicker_url->param('action', 'plugins');
271 echo '<div class="filemanager-toolbar">';
272 if ($env == 'filepicker') {
275 if ($filecount < $maxfiles ||
$maxfiles == -1) {
276 echo ' <a href="'.$filepicker_url->out().'">'.get_string('addfile', 'repository').'</a>';
278 if ($env == 'filemanager') {
279 if (!empty($subdirs)) {
280 $home_url->param('action', 'mkdirform');
281 echo ' <a href="'.$home_url->out().'">'.get_string('makeafolder', 'moodle').'</a>';
283 if (!empty($files->list)) {
284 $home_url->param('action', 'downloaddir');
285 echo ' ' . html_writer
::link($home_url, get_string('downloadfolder', 'repository'), array('target'=>'_blank'));
290 if (!empty($files->list)) {
292 foreach ($files->list as $file) {
293 if ($file->type
!= 'folder') {
294 $drafturl = $file->url
;
297 echo $OUTPUT->pix_icon(file_file_icon($file), '', 'moodle', array('class' => 'iconsmall'));
298 echo html_writer
::link($drafturl, $file->filename
);
300 $home_url->param('filename', $file->filename
);
301 $home_url->param('draftpath', $file->filepath
);
303 $home_url->param('action', 'deletedraft');
304 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('delete').'</a>]';
306 $home_url->param('action', 'movefile');
307 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('move').'</a>]';
309 $home_url->param('action', 'renameform');
310 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('rename').'</a>]';
312 if (file_extension_in_typegroup($file->filename
, 'archive', true)) {
313 $home_url->param('action', 'unzip');
314 $home_url->param('draftpath', $file->filepath
);
315 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('unzip').'</a>]';
322 echo '<img src="'.$OUTPUT->pix_url(file_folder_icon()) . '" class="iconsmall" />';
324 $home_url->param('action', 'browse');
325 $home_url->param('draftpath', $file->filepath
);
326 $filepathchunks = explode('/', trim($file->filepath
, '/'));
327 $foldername = trim(array_pop($filepathchunks), '/');
328 echo html_writer
::link($home_url, $foldername);
330 $home_url->param('draftpath', $file->filepath
);
331 $home_url->param('filename', $file->filename
);
332 $home_url->param('action', 'deletedraft');
333 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('delete').'</a>]';
335 $home_url->param('action', 'zip');
336 echo ' [<a href="'.$home_url->out().'" class="fm-operation">Zip</a>]';
342 echo get_string('nofilesavailable', 'repository');
344 echo $OUTPUT->footer();
348 function print_draft_area_tree($tree, $root, $url) {
351 $url->param('targetpath', '/');
352 if ($url->param('draftpath') == '/') {
353 echo '<li>'.get_string('files').'</li>';
355 echo '<li><a href="'.$url->out().'">'.get_string('files').'</a></li>';
358 if (isset($tree->children
)) {
359 $tree = $tree->children
;
364 foreach ($tree as $node) {
366 $url->param('targetpath', $node->filepath
);
367 if ($url->param('draftpath') != $node->filepath
) {
368 echo '<a href="'.$url->out().'">'.$node->fullname
.'</a>';
370 echo $node->fullname
;
373 if (!empty($node->children
)) {
374 print_draft_area_tree($node->children
, false, $url);