|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 // 00018 00019 00020 // 00021 // 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!! 00022 // 00023 00024 00035 require_once('../config.php'); 00036 require_once($CFG->libdir.'/filelib.php'); 00037 require_once('lib.php'); 00038 00039 require_sesskey(); 00040 require_login(); 00041 00042 // disable blocks in this page 00043 $PAGE->set_pagelayout('embedded'); 00044 00045 // general parameters 00046 $action = optional_param('action', '', PARAM_ALPHA); 00047 $itemid = optional_param('itemid', '', PARAM_INT); 00048 00049 // parameters for repository 00050 $contextid = optional_param('ctx_id', SYSCONTEXTID, PARAM_INT); // context ID 00051 $courseid = optional_param('course', SITEID, PARAM_INT); // course ID 00052 $env = optional_param('env', 'filepicker', PARAM_ALPHA); // opened in file picker, file manager or html editor 00053 $filename = optional_param('filename', '', PARAM_FILE); 00054 $targetpath = optional_param('targetpath', '', PARAM_PATH); 00055 $maxfiles = optional_param('maxfiles', -1, PARAM_INT); // maxfiles 00056 $maxbytes = optional_param('maxbytes', 0, PARAM_INT); // maxbytes 00057 $subdirs = optional_param('subdirs', 0, PARAM_INT); // maxbytes 00058 00059 // draft area 00060 $newdirname = optional_param('newdirname', '', PARAM_FILE); 00061 $newfilename = optional_param('newfilename', '', PARAM_FILE); 00062 // path in draft area 00063 $draftpath = optional_param('draftpath', '/', PARAM_PATH); 00064 00065 // user context 00066 $user_context = get_context_instance(CONTEXT_USER, $USER->id); 00067 00068 00069 $PAGE->set_context($user_context); 00070 00071 $fs = get_file_storage(); 00072 00073 $params = array('ctx_id' => $contextid, 'itemid' => $itemid, 'env' => $env, 'course'=>$courseid, 'maxbytes'=>$maxbytes, 'maxfiles'=>$maxfiles, 'subdirs'=>$subdirs, 'sesskey'=>sesskey()); 00074 $PAGE->set_url('/repository/draftfiles_manager.php', $params); 00075 $filepicker_url = new moodle_url($CFG->httpswwwroot."/repository/filepicker.php", $params); 00076 00077 $params['action'] = 'browse'; 00078 $home_url = new moodle_url('/repository/draftfiles_manager.php', $params); 00079 00080 switch ($action) { 00081 00082 // delete draft files 00083 case 'deletedraft': 00084 if ($file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename)) { 00085 if ($file->is_directory()) { 00086 $pathname = $draftpath; 00087 if ($file->get_parent_directory()) { 00088 $draftpath = $file->get_parent_directory()->get_filepath(); 00089 } else { 00090 $draftpath = '/'; 00091 } 00092 00093 // delete files in folder 00094 $files = $fs->get_directory_files($user_context->id, 'user', 'draft', $itemid, $pathname, true); 00095 foreach ($files as $storedfile) { 00096 $storedfile->delete(); 00097 } 00098 $file->delete(); 00099 } else { 00100 $file->delete(); 00101 } 00102 $home_url->param('draftpath', $draftpath); 00103 $home_url->param('action', 'browse'); 00104 redirect($home_url); 00105 } 00106 break; 00107 00108 case 'renameform': 00109 echo $OUTPUT->header(); 00110 echo '<div><a href="' . $home_url->out() . '">'.get_string('back', 'repository')."</a></div>"; 00111 $home_url->param('draftpath', $draftpath); 00112 $home_url->param('action', 'rename'); 00113 echo ' <form method="post" action="'.$home_url->out().'">'; 00114 echo ' <input name="newfilename" type="text" value="'.s($filename).'" />'; 00115 echo ' <input name="filename" type="hidden" value="'.s($filename).'" />'; 00116 echo ' <input name="draftpath" type="hidden" value="'.s($draftpath).'" />'; 00117 echo ' <input type="submit" value="'.s(get_string('rename', 'moodle')).'" />'; 00118 echo ' </form>'; 00119 echo $OUTPUT->footer(); 00120 break; 00121 00122 case 'rename': 00123 00124 if ($fs->file_exists($user_context->id, 'user', 'draft', $itemid, $draftpath, $newfilename)) { 00125 print_error('fileexists'); 00126 } else if ($file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename)) { 00127 $newfile = $fs->create_file_from_storedfile(array('filename'=>$newfilename), $file); 00128 $file->delete(); 00129 } 00130 00131 $home_url->param('action', 'browse'); 00132 $home_url->param('draftpath', $draftpath); 00133 redirect($home_url); 00134 break; 00135 00136 case 'downloaddir': 00137 $zipper = new zip_packer(); 00138 00139 $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, '.'); 00140 if ($file->get_parent_directory()) { 00141 $parent_path = $file->get_parent_directory()->get_filepath(); 00142 $filename = trim($draftpath, '/').'.zip'; 00143 } else { 00144 $parent_path = '/'; 00145 $filename = get_string('files').'.zip'; 00146 } 00147 00148 if ($newfile = $zipper->archive_to_storage(array($file), $user_context->id, 'user', 'draft', $itemid, $parent_path, $filename, $USER->id)) { 00149 $fileurl = moodle_url::make_draftfile_url($itemid, '/', $filename)->out(); 00150 header('Location: ' . $fileurl ); 00151 } else { 00152 print_error('cannotdownloaddir', 'repository'); 00153 } 00154 break; 00155 00156 case 'zip': 00157 $zipper = new zip_packer(); 00158 00159 $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, '.'); 00160 if (!$file->get_parent_directory()) { 00161 $parent_path = '/'; 00162 $filename = get_string('files').'.zip'; 00163 } else { 00164 $parent_path = $file->get_parent_directory()->get_filepath(); 00165 $filepath = explode('/', trim($file->get_filepath(), '/')); 00166 $filename = array_pop($filepath).'.zip'; 00167 } 00168 00169 $newfile = $zipper->archive_to_storage(array($file), $user_context->id, 'user', 'draft', $itemid, $parent_path, $filename, $USER->id); 00170 00171 $home_url->param('action', 'browse'); 00172 $home_url->param('draftpath', $parent_path); 00173 redirect($home_url, get_string('ziped', 'repository')); 00174 break; 00175 00176 case 'unzip': 00177 $zipper = new zip_packer(); 00178 $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename); 00179 00180 if ($newfile = $file->extract_to_storage($zipper, $user_context->id, 'user', 'draft', $itemid, $draftpath, $USER->id)) { 00181 $str = get_string('unzipped', 'repository'); 00182 } else { 00183 $str = get_string('cannotunzip', 'error'); 00184 } 00185 $home_url->param('action', 'browse'); 00186 $home_url->param('draftpath', $draftpath); 00187 redirect($home_url, $str); 00188 break; 00189 00190 case 'movefile': 00191 if (!empty($targetpath)) { 00192 if ($fs->file_exists($user_context->id, 'user', 'draft', $itemid, $targetpath, $filename)) { 00193 print_error('cannotmovefile'); 00194 } else if ($file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename)) { 00195 $newfile = $fs->create_file_from_storedfile(array('filepath'=>$targetpath), $file); 00196 $file->delete(); 00197 } else { 00198 var_dump('cannot find file'); 00199 die; 00200 } 00201 $home_url->param('action', 'browse'); 00202 $home_url->param('draftpath', $targetpath); 00203 redirect($home_url); 00204 } 00205 echo $OUTPUT->header(); 00206 00207 echo $OUTPUT->container_start(); 00208 echo html_writer::link($home_url, get_string('back', 'repository')); 00209 echo $OUTPUT->container_end(); 00210 00211 $data = new stdClass(); 00212 $home_url->param('action', 'movefile'); 00213 $home_url->param('draftpath', $draftpath); 00214 $home_url->param('filename', $filename); 00215 file_get_drafarea_folders($itemid, '/', $data); 00216 print_draft_area_tree($data, true, $home_url); 00217 echo $OUTPUT->footer(); 00218 break; 00219 00220 case 'mkdirform': 00221 echo $OUTPUT->header(); 00222 00223 echo $OUTPUT->container_start(); 00224 echo html_writer::link($home_url, get_string('back', 'repository')); 00225 echo $OUTPUT->container_end(); 00226 00227 $home_url->param('draftpath', $draftpath); 00228 $home_url->param('action', 'mkdir'); 00229 echo ' <form method="post" action="'.$home_url->out().'">'; 00230 echo ' <input name="newdirname" type="text" />'; 00231 echo ' <input name="draftpath" type="hidden" value="'.s($draftpath).'" />'; 00232 echo ' <input type="submit" value="'.s(get_string('makeafolder', 'moodle')).'" />'; 00233 echo ' </form>'; 00234 echo $OUTPUT->footer(); 00235 break; 00236 00237 case 'mkdir': 00238 00239 $newfolderpath = $draftpath . trim($newdirname, '/') . '/'; 00240 $fs->create_directory($user_context->id, 'user', 'draft', $itemid, $newfolderpath); 00241 $home_url->param('action', 'browse'); 00242 if (!empty($newdirname)) { 00243 $home_url->param('draftpath', $newfolderpath); 00244 $str = get_string('createfoldersuccess', 'repository'); 00245 } else { 00246 $home_url->param('draftpath', $draftpath); 00247 $str = get_string('createfolderfail', 'repository'); 00248 } 00249 redirect($home_url, $str); 00250 break; 00251 00252 case 'browse': 00253 default: 00254 $files = file_get_drafarea_files($itemid, $draftpath); 00255 $info = file_get_draft_area_info($itemid); 00256 $filecount = $info['filecount']; 00257 00258 echo $OUTPUT->header(); 00259 if ((!empty($files) or $draftpath != '/') and $env == 'filemanager') { 00260 echo '<div class="fm-breadcrumb">'; 00261 $home_url->param('action', 'browse'); 00262 $home_url->param('draftpath', '/'); 00263 echo '<a href="'.$home_url->out().'">' . get_string('files') . '</a> ▶'; 00264 $trail = ''; 00265 if ($draftpath !== '/') { 00266 $path = '/' . trim($draftpath, '/') . '/'; 00267 $parts = explode('/', $path); 00268 foreach ($parts as $part) { 00269 if (!empty($part)) { 00270 $trail .= ('/'.$part.'/'); 00271 $data->path[] = array('name'=>$part, 'path'=>$trail); 00272 $home_url->param('draftpath', $trail); 00273 echo ' <a href="'.$home_url->out().'">'.$part.'</a> ▶ '; 00274 } 00275 } 00276 } 00277 echo '</div>'; 00278 } 00279 00280 $filepicker_url->param('draftpath', $draftpath); 00281 $filepicker_url->param('savepath', $draftpath); 00282 $filepicker_url->param('action', 'plugins'); 00283 echo '<div class="filemanager-toolbar">'; 00284 if ($env == 'filepicker') { 00285 $maxfiles = 1; 00286 } 00287 if ($filecount < $maxfiles || $maxfiles == -1) { 00288 echo ' <a href="'.$filepicker_url->out().'">'.get_string('addfile', 'repository').'</a>'; 00289 } 00290 if ($env == 'filemanager') { 00291 if (!empty($subdirs)) { 00292 $home_url->param('action', 'mkdirform'); 00293 echo ' <a href="'.$home_url->out().'">'.get_string('makeafolder', 'moodle').'</a>'; 00294 } 00295 $home_url->param('action', 'downloaddir'); 00296 echo html_writer::link($home_url, get_string('downloadfolder', 'repository'), array('target'=>'_blank')); 00297 } 00298 echo '</div>'; 00299 00300 if (!empty($files->list)) { 00301 echo '<ul>'; 00302 foreach ($files->list as $file) { 00303 if ($file->type != 'folder') { 00304 $drafturl = $file->url; 00305 // a file 00306 $fileicon = $OUTPUT->pix_url(file_extension_icon($file->filename))->out(false); 00307 $type = mimeinfo('icon', $file->filename); 00308 echo '<li>'; 00309 echo '<img src="'.$fileicon. '" class="iconsmall" />'; 00310 echo html_writer::link($drafturl, $file->filename); 00311 00312 $home_url->param('filename', $file->filename); 00313 $home_url->param('draftpath', $file->filepath); 00314 00315 $home_url->param('action', 'deletedraft'); 00316 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('delete').'</a>]'; 00317 00318 $home_url->param('action', 'movefile'); 00319 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('move').'</a>]'; 00320 00321 $home_url->param('action', 'renameform'); 00322 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('rename').'</a>]'; 00323 00324 if ($type == 'zip') { 00325 $home_url->param('action', 'unzip'); 00326 $home_url->param('draftpath', $file->filepath); 00327 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('unzip').'</a>]'; 00328 } 00329 00330 echo '</li>'; 00331 } else { 00332 // a folder 00333 echo '<li>'; 00334 echo '<img src="'.$OUTPUT->pix_url('f/folder') . '" class="iconsmall" />'; 00335 00336 $home_url->param('action', 'browse'); 00337 $home_url->param('draftpath', $file->filepath); 00338 $foldername = trim(array_pop(explode('/', trim($file->filepath, '/'))), '/'); 00339 echo html_writer::link($home_url, $foldername); 00340 00341 $home_url->param('draftpath', $file->filepath); 00342 $home_url->param('filename', $file->filename); 00343 $home_url->param('action', 'deletedraft'); 00344 echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('delete').'</a>]'; 00345 00346 $home_url->param('action', 'zip'); 00347 echo ' [<a href="'.$home_url->out().'" class="fm-operation">Zip</a>]'; 00348 echo '</li>'; 00349 } 00350 } 00351 echo '</ul>'; 00352 } else { 00353 echo get_string('nofilesavailable', 'repository'); 00354 } 00355 echo $OUTPUT->footer(); 00356 break; 00357 } 00358 00359 function print_draft_area_tree($tree, $root, $url) { 00360 echo '<ul>'; 00361 if ($root) { 00362 $url->param('targetpath', '/'); 00363 if ($url->param('draftpath') == '/') { 00364 echo '<li>'.get_string('files').'</li>'; 00365 } else { 00366 echo '<li><a href="'.$url->out().'">'.get_string('files').'</a></li>'; 00367 } 00368 echo '<ul>'; 00369 if (isset($tree->children)) { 00370 $tree = $tree->children; 00371 } 00372 } 00373 00374 if (!empty($tree)) { 00375 foreach ($tree as $node) { 00376 echo '<li>'; 00377 $url->param('targetpath', $node->filepath); 00378 if ($url->param('draftpath') != $node->filepath) { 00379 echo '<a href="'.$url->out().'">'.$node->fullname.'</a>'; 00380 } else { 00381 echo $node->fullname; 00382 } 00383 echo '</li>'; 00384 if (!empty($node->children)) { 00385 print_draft_area_tree($node->children, false, $url); 00386 } 00387 } 00388 } 00389 if ($root) { 00390 echo '</ul>'; 00391 } 00392 echo '</ul>'; 00393 }