Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Manage files in user draft area attached to texteditor.
  19   *
  20   * @package   atto_managefiles
  21   * @copyright 2014 Frédéric Massart
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require(__DIR__ . '/../../../../../config.php');
  26  require_once (__DIR__ . '/manage_form.php');
  27  require_once($CFG->libdir . '/filestorage/file_storage.php');
  28  require_once($CFG->dirroot . '/repository/lib.php');
  29  
  30  $itemid = required_param('itemid', PARAM_INT);
  31  $maxbytes = optional_param('maxbytes', 0, PARAM_INT);
  32  $subdirs = optional_param('subdirs', 0, PARAM_INT);
  33  $accepted_types = optional_param('accepted_types', '*', PARAM_RAW); // TODO Not yet passed to this script.
  34  $return_types = optional_param('return_types', null, PARAM_INT);
  35  $areamaxbytes = optional_param('areamaxbytes', FILE_AREA_MAX_BYTES_UNLIMITED, PARAM_INT);
  36  $contextid = optional_param('context', SYSCONTEXTID, PARAM_INT);
  37  $elementid = optional_param('elementid', '', PARAM_TEXT);
  38  $removeorphaneddrafts = optional_param('removeorphaneddrafts', 0, PARAM_INT);
  39  
  40  $context = context::instance_by_id($contextid);
  41  if ($context->contextlevel == CONTEXT_MODULE) {
  42      // Module context.
  43      $cm = $DB->get_record('course_modules', array('id' => $context->instanceid));
  44      require_login($cm->course, true, $cm);
  45  } else if (($coursecontext = $context->get_course_context(false)) && $coursecontext->id != SITEID) {
  46      // Course context or block inside the course.
  47      require_login($coursecontext->instanceid);
  48      $PAGE->set_context($context);
  49  } else {
  50      // Block that is not inside the course, user or system context.
  51      require_login();
  52      $PAGE->set_context($context);
  53  }
  54  
  55  // Guests can never manage files.
  56  if (isguestuser()) {
  57      print_error('noguest');
  58  }
  59  
  60  $title = get_string('managefiles', 'atto_managefiles');
  61  
  62  $PAGE->set_url('/lib/editor/atto/plugins/managefiles/manage.php');
  63  $PAGE->set_title($title);
  64  $PAGE->set_heading($title);
  65  $PAGE->set_pagelayout('popup');
  66  
  67  if ($return_types !== null) {
  68      // Links are allowed in textarea but never allowed in filemanager.
  69      $return_types = $return_types & ~FILE_EXTERNAL;
  70  }
  71  
  72  $options = array(
  73      'subdirs' => $subdirs,
  74      'maxbytes' => $maxbytes,
  75      'maxfiles' => -1,
  76      'accepted_types' => $accepted_types,
  77      'areamaxbytes' => $areamaxbytes,
  78      'return_types' => $return_types,
  79      'context' => $context
  80  );
  81  
  82  $usercontext = context_user::instance($USER->id);
  83  $fs = get_file_storage();
  84  $files = $fs->get_directory_files($usercontext->id, 'user', 'draft', $itemid, '/', !empty($subdirs), false);
  85  $filenames = array();
  86  foreach ($files as $file) {
  87      $filenames[$file->get_pathnamehash()] = ltrim($file->get_filepath(), '/') . $file->get_filename();
  88  }
  89  
  90  $mform = new atto_managefiles_manage_form(null,
  91      array('options' => $options, 'draftitemid' => $itemid, 'files' => $filenames, 'elementid' => $elementid,
  92          'removeorphaneddrafts' => $removeorphaneddrafts), 'post', '', array('id' => 'atto_managefiles_manageform'));
  93  
  94  if ($data = $mform->get_data()) {
  95      if (!empty($data->deletefile)) {
  96          foreach (array_keys($data->deletefile) as $filehash) {
  97              if ($file = $fs->get_file_by_hash($filehash)) {
  98                  // Make sure the user didn't modify the filehash to delete another file.
  99                  if ($file->get_component() == 'user' && $file->get_filearea() == 'draft'
 100                          && $file->get_itemid() == $itemid && $file->get_contextid() == $usercontext->id) {
 101                      $file->delete();
 102                  }
 103              }
 104          }
 105          $filenames = array_diff_key($filenames, $data->deletefile);
 106          $mform = new atto_managefiles_manage_form(null,
 107              array('options' => $options, 'draftitemid' => $itemid, 'files' => $filenames, 'elementid' => $data->elementid),
 108              'post', '', array('id' => 'atto_managefiles_manageform'));
 109      }
 110  }
 111  
 112  echo $OUTPUT->header();
 113  $mform->display();
 114  echo $OUTPUT->footer();