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
58 $areamaxbytes = optional_param('areamaxbytes', FILE_AREA_MAX_BYTES_UNLIMITED
, PARAM_INT
); // Area maxbytes.
61 $newdirname = optional_param('newdirname', '', PARAM_FILE
);
62 $newfilename = optional_param('newfilename', '', PARAM_FILE
);
64 $draftpath = optional_param('draftpath', '/', PARAM_PATH
);
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("/repository/filepicker.php", $params);
78 $params['action'] = 'browse';
79 $home_url = new moodle_url('/repository/draftfiles_manager.php', $params);
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();
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();
103 $home_url->param('draftpath', $draftpath);
104 $home_url->param('action', 'browse');
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')).'" />';
121 echo $OUTPUT->footer();
125 repository
::update_draftfile($itemid, $draftpath, $filename, array('filename' => $newfilename));
126 $home_url->param('action', 'browse');
127 $home_url->param('draftpath', $draftpath);
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';
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);
147 print_error('cannotdownloaddir', 'repository');
152 $zipper = new zip_packer();
154 $file = $fs->get_file($user_context->id
, 'user', 'draft', $itemid, $draftpath, '.');
155 if (!$file->get_parent_directory()) {
158 $filename = get_string('files').'.zip';
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'));
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');
181 $str = get_string('cannotunzip', 'error');
183 $home_url->param('action', 'browse');
184 $home_url->param('draftpath', $draftpath);
185 redirect($home_url, $str);
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);
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();
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')).'" />';
225 echo $OUTPUT->footer();
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');
237 $home_url->param('draftpath', $draftpath);
238 $str = get_string('createfolderfail', 'repository');
240 redirect($home_url, $str);
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> ▶';
256 if ($draftpath !== '/') {
257 $path = '/' . trim($draftpath, '/') . '/';
258 $parts = explode('/', $path);
259 foreach ($parts as $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> ▶ ';
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') {
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'));
293 if (!empty($files->list)) {
295 foreach ($files->list as $file) {
296 if ($file->type
!= 'folder') {
297 $drafturl = $file->url
;
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>]';
325 echo $OUTPUT->pix_icon(file_folder_icon(), get_string('folder'));
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>]';
345 echo get_string('nofilesavailable', 'repository');
347 echo $OUTPUT->footer();
351 function print_draft_area_tree($tree, $root, $url) {
354 $url->param('targetpath', '/');
355 if ($url->param('draftpath') == '/') {
356 echo '<li>'.get_string('files').'</li>';
358 echo '<li><a href="'.$url->out().'">'.get_string('files').'</a></li>';
361 if (isset($tree->children
)) {
362 $tree = $tree->children
;
367 foreach ($tree as $node) {
369 $url->param('targetpath', $node->filepath
);
370 if ($url->param('draftpath') != $node->filepath
) {
371 echo '<a href="'.$url->out().'">'.$node->fullname
.'</a>';
373 echo $node->fullname
;
376 if (!empty($node->children
)) {
377 print_draft_area_tree($node->children
, false, $url);