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();
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
;
306 $fileicon = $OUTPUT->pix_url(file_extension_icon($file->filename
))->out(false);
307 $type = mimeinfo('icon', $file->filename
);
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>]';
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>]';
353 echo get_string('nofilesavailable', 'repository');
355 echo $OUTPUT->footer();
359 function print_draft_area_tree($tree, $root, $url) {
362 $url->param('targetpath', '/');
363 if ($url->param('draftpath') == '/') {
364 echo '<li>'.get_string('files').'</li>';
366 echo '<li><a href="'.$url->out().'">'.get_string('files').'</a></li>';
369 if (isset($tree->children
)) {
370 $tree = $tree->children
;
375 foreach ($tree as $node) {
377 $url->param('targetpath', $node->filepath
);
378 if ($url->param('draftpath') != $node->filepath
) {
379 echo '<a href="'.$url->out().'">'.$node->fullname
.'</a>';
381 echo $node->fullname
;
384 if (!empty($node->children
)) {
385 print_draft_area_tree($node->children
, false, $url);