Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   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   tinymce_managefiles
  21   * @copyright 2013 Marina Glancy
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require('../../../../../config.php');
  26  require_once ('manage_form.php');
  27  require_once($CFG->libdir.'/filestorage/file_storage.php');
  28  
  29  $itemid = required_param('itemid', PARAM_INT);
  30  $maxbytes = optional_param('maxbytes', 0, PARAM_INT);
  31  $subdirs = optional_param('subdirs', 0, PARAM_INT);
  32  $accepted_types = optional_param('accepted_types', '*', PARAM_RAW); // TODO not yet passed to this script
  33  $return_types = optional_param('return_types', null, PARAM_INT);
  34  $areamaxbytes = optional_param('areamaxbytes', FILE_AREA_MAX_BYTES_UNLIMITED, PARAM_INT);
  35  $contextid = optional_param('context', SYSCONTEXTID, PARAM_INT);
  36  
  37  $context = context::instance_by_id($contextid);
  38  if ($context->contextlevel == CONTEXT_MODULE) {
  39      // Module context.
  40      $cm = $DB->get_record('course_modules', array('id' => $context->instanceid));
  41      require_login($cm->course, true, $cm);
  42  } else if (($coursecontext = $context->get_course_context(false)) && $coursecontext->id != SITEID) {
  43      // Course context or block inside the course.
  44      require_login($coursecontext->instanceid);
  45      $PAGE->set_context($context);
  46  } else {
  47      // Block that is not inside the course, user or system context.
  48      require_login();
  49      $PAGE->set_context($context);
  50  }
  51  if (isguestuser()) {
  52      // Guests can never manage files.
  53      throw new \moodle_exception('noguest');
  54  }
  55  
  56  $title = get_string('manageareafiles', 'tinymce_managefiles');
  57  
  58  $PAGE->set_url('/lib/editor/tinymce/plugins/managefiles/manage.php');
  59  $PAGE->set_title($title);
  60  $PAGE->set_heading($title);
  61  $PAGE->set_pagelayout('popup');
  62  
  63  if ($return_types !== null) {
  64      $return_types = $return_types ^ 1; // links are allowed in textarea but never allowed in filemanager
  65  }
  66  
  67  $options = array(
  68      'subdirs' => $subdirs,
  69      'maxbytes' => $maxbytes,
  70      'maxfiles' => -1,
  71      'accepted_types' => $accepted_types,
  72      'areamaxbytes' => $areamaxbytes,
  73      'return_types' => $return_types,
  74      'context' => $context
  75  );
  76  
  77  $usercontext = context_user::instance($USER->id);
  78  $fs = get_file_storage();
  79  $files = $fs->get_directory_files($usercontext->id, 'user', 'draft', $itemid, '/', !empty($subdirs), false);
  80  $filenames = array();
  81  foreach ($files as $file) {
  82      $filenames[] = ltrim($file->get_filepath(), '/'). $file->get_filename();
  83  }
  84  
  85  $mform = new tinymce_managefiles_manage_form(null,
  86          array('options' => $options, 'draftitemid' => $itemid, 'files' => $filenames),
  87          'post', '', array('id' => 'tinymce_managefiles_manageform'));
  88  
  89  if ($data = $mform->get_data()) {
  90      if (!empty($data->deletefile)) {
  91          foreach (array_keys($data->deletefile) as $filename) {
  92              $filepath = '/';
  93              if (!empty($subdirs) && strlen(dirname($filename))  ) {
  94                  $filepath = '/'. dirname($filename). '/';
  95              }
  96              if ($file = $fs->get_file($usercontext->id, 'user', 'draft', $itemid,
  97                      $filepath, basename($filename))) {
  98                  $file->delete();
  99              }
 100          }
 101          $filenames = array_diff($filenames, array_keys($data->deletefile));
 102          $mform = new tinymce_managefiles_manage_form(null,
 103                  array('options' => $options, 'draftitemid' => $itemid, 'files' => $filenames),
 104                  'post', '', array('id' => 'tinymce_managefiles_manageform'));
 105      }
 106  }
 107  
 108  echo $OUTPUT->header();
 109  $mform->display();
 110  echo $OUTPUT->footer();