Search moodle.org's
Developer Documentation

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   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.
   9  //
  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.
  14  //
  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/>.
  17  //
  18  
  19  
  20  //
  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!!
  22  //
  23  
  24  
  25  /**
  26   * This file is used to manage draft files in non-javascript browsers
  27   *
  28   * @since Moodle 2.0
  29   * @package    core
  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
  33   */
  34  
  35  require_once('../config.php');
  36  require_once($CFG->libdir.'/filelib.php');
  37  require_once ('lib.php');
  38  
  39  require_sesskey();
  40  require_login();
  41  
  42  // disable blocks in this page
  43  $PAGE->set_pagelayout('embedded');
  44  
  45  // general parameters
  46  $action      = optional_param('action', '',        PARAM_ALPHA);
  47  $itemid      = optional_param('itemid', '',        PARAM_INT);
  48  
  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.
  59  
  60  // draft area
  61  $newdirname  = optional_param('newdirname', '',    PARAM_FILE);
  62  $newfilename = optional_param('newfilename', '',   PARAM_FILE);
  63  // path in draft area
  64  $draftpath   = optional_param('draftpath', '/',    PARAM_PATH);
  65  
  66  // user context
  67  $user_context = context_user::instance($USER->id);
  68  
  69  
  70  $PAGE->set_context($user_context);
  71  
  72  $fs = get_file_storage();
  73  
  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($CFG->httpswwwroot."/repository/filepicker.php", $params);
  77  
  78  $params['action'] = 'browse';
  79  $home_url = new moodle_url('/repository/draftfiles_manager.php', $params);
  80  
  81  switch ($action) {
  82  
  83      // delete draft files
  84  case 'deletedraft':
  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();
  90              } else {
  91                  $draftpath = '/';
  92              }
  93  
  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();
  98              }
  99              $file->delete();
 100          } else {
 101              $file->delete();
 102          }
 103          $home_url->param('draftpath', $draftpath);
 104          $home_url->param('action', 'browse');
 105          redirect($home_url);
 106      }
 107      break;
 108  
 109  case 'renameform':
 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')).'" />';
 120      echo ' </form>';
 121      echo $OUTPUT->footer();
 122      break;
 123  
 124  case 'rename':
 125      repository::update_draftfile($itemid, $draftpath, $filename, array('filename' => $newfilename));
 126      $home_url->param('action', 'browse');
 127      $home_url->param('draftpath', $draftpath);
 128      redirect($home_url);
 129      break;
 130  
 131  case 'downloaddir':
 132      $zipper = new zip_packer();
 133  
 134      $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, '.');
 135      if ($draftpath === '/') {
 136          $filename = get_string('files').'.zip';
 137      } else {
 138          $filename = explode('/', trim($draftpath, '/'));
 139          $filename = array_pop($filename) . '.zip';
 140      }
 141  
 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);
 146      } else {
 147          print_error('cannotdownloaddir', 'repository');
 148      }
 149      break;
 150  
 151  case 'zip':
 152      $zipper = new zip_packer();
 153  
 154      $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, '.');
 155      if (!$file->get_parent_directory()) {
 156          $parent_path = '/';
 157          $filepath = '/';
 158          $filename = get_string('files').'.zip';
 159      } else {
 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';
 164      }
 165  
 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);
 168  
 169      $home_url->param('action', 'browse');
 170      $home_url->param('draftpath', $parent_path);
 171      redirect($home_url, get_string('ziped', 'repository'));
 172      break;
 173  
 174  case 'unzip':
 175      $zipper = new zip_packer();
 176      $file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $draftpath, $filename);
 177  
 178      if ($newfile = $file->extract_to_storage($zipper, $user_context->id, 'user', 'draft', $itemid, $draftpath, $USER->id)) {
 179          $str = get_string('unzipped', 'repository');
 180      } else {
 181          $str = get_string('cannotunzip', 'error');
 182      }
 183      $home_url->param('action', 'browse');
 184      $home_url->param('draftpath', $draftpath);
 185      redirect($home_url, $str);
 186      break;
 187  
 188  case 'movefile':
 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);
 193          redirect($home_url);
 194      }
 195      echo $OUTPUT->header();
 196  
 197      echo $OUTPUT->container_start();
 198      echo html_writer::link($home_url, get_string('back', 'repository'));
 199      echo $OUTPUT->container_end();
 200  
 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();
 208      break;
 209  
 210  case 'mkdirform':
 211      echo $OUTPUT->header();
 212  
 213      echo $OUTPUT->container_start();
 214      echo html_writer::link($home_url, get_string('back', 'repository'));
 215      echo $OUTPUT->container_end();
 216  
 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')).'" />';
 224      echo ' </form>';
 225      echo $OUTPUT->footer();
 226      break;
 227  
 228  case 'mkdir':
 229  
 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');
 236      } else {
 237          $home_url->param('draftpath', $draftpath);
 238          $str = get_string('createfolderfail', 'repository');
 239      }
 240      redirect($home_url, $str);
 241      break;
 242  
 243  case 'browse':
 244  default:
 245      $files = file_get_drafarea_files($itemid, $draftpath);
 246      $info = file_get_draft_area_info($itemid);
 247      $filecount = $info['filecount'];
 248  
 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> ▶';
 255          $trail = '';
 256          if ($draftpath !== '/') {
 257              $path = '/' . trim($draftpath, '/') . '/';
 258              $parts = explode('/', $path);
 259              foreach ($parts as $part) {
 260                  if ($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> ▶ ';
 265                  }
 266              }
 267          }
 268          echo '</div>';
 269      }
 270  
 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') {
 276          $maxfiles = 1;
 277      }
 278      if ($filecount < $maxfiles || $maxfiles == -1) {
 279          echo ' <a href="'.$filepicker_url->out().'">'.get_string('addfile', 'repository').'</a>';
 280      }
 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>';
 285          }
 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'));
 289          }
 290      }
 291      echo '</div>';
 292  
 293      if (!empty($files->list)) {
 294          echo '<ul>';
 295          foreach ($files->list as $file) {
 296              if ($file->type != 'folder') {
 297                  $drafturl = $file->url;
 298                  // a file
 299                  echo '<li>';
 300                  echo $OUTPUT->pix_icon(file_file_icon($file), '', 'moodle', array('class' => 'iconsmall'));
 301                  echo html_writer::link($drafturl, $file->filename);
 302  
 303                  $home_url->param('filename', $file->filename);
 304                  $home_url->param('draftpath', $file->filepath);
 305  
 306                  $home_url->param('action', 'deletedraft');
 307                  echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('delete').'</a>]';
 308  
 309                  $home_url->param('action', 'movefile');
 310                  echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('move').'</a>]';
 311  
 312                  $home_url->param('action', 'renameform');
 313                  echo ' [<a href="'.$home_url->out().'" class="fm-operation">'.get_string('rename').'</a>]';
 314  
 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>]';
 319                  }
 320  
 321                  echo '</li>';
 322              } else {
 323                  // a folder
 324                  echo '<li>';
 325                  echo '<img src="'.$OUTPUT->pix_url(file_folder_icon()) . '" class="iconsmall" />';
 326  
 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);
 332  
 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>]';
 337  
 338                  $home_url->param('action', 'zip');
 339                  echo ' [<a href="'.$home_url->out().'" class="fm-operation">Zip</a>]';
 340                  echo '</li>';
 341              }
 342          }
 343          echo '</ul>';
 344      } else {
 345          echo get_string('nofilesavailable', 'repository');
 346      }
 347      echo $OUTPUT->footer();
 348      break;
 349  }
 350  
 351  function print_draft_area_tree($tree, $root, $url) {
 352      echo '<ul>';
 353      if ($root) {
 354          $url->param('targetpath', '/');
 355          if ($url->param('draftpath') == '/') {
 356              echo '<li>'.get_string('files').'</li>';
 357          } else {
 358              echo '<li><a href="'.$url->out().'">'.get_string('files').'</a></li>';
 359          }
 360          echo '<ul>';
 361          if (isset($tree->children)) {
 362              $tree = $tree->children;
 363          }
 364      }
 365  
 366      if (!empty($tree)) {
 367          foreach ($tree as $node) {
 368              echo '<li>';
 369              $url->param('targetpath', $node->filepath);
 370              if ($url->param('draftpath') != $node->filepath) {
 371                  echo '<a href="'.$url->out().'">'.$node->fullname.'</a>';
 372              } else {
 373                  echo $node->fullname;
 374              }
 375              echo '</li>';
 376              if (!empty($node->children)) {
 377                  print_draft_area_tree($node->children, false, $url);
 378              }
 379          }
 380      }
 381      if ($root) {
 382          echo '</ul>';
 383      }
 384      echo '</ul>';
 385  }

Search This Site: