Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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   tiny_media
  21   * @copyright 2022, Stevani Andolo <stevani@hotmail.com.au>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require(__DIR__ . '/../../../../../config.php');
  26  require_once($CFG->libdir . '/filestorage/file_storage.php');
  27  require_once($CFG->dirroot . '/repository/lib.php');
  28  
  29  $itemid = required_param('itemid', PARAM_INT) ?? 0;
  30  $maxbytes = optional_param('maxbytes', 0, PARAM_INT);
  31  $subdirs = optional_param('subdirs', 0, PARAM_INT);
  32  $acceptedtypes = optional_param('accepted_types', '*', PARAM_RAW); // TODO Not yet passed to this script.
  33  $returntypes = 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  $elementid = optional_param('elementid', '', PARAM_TEXT);
  37  $removeorphaneddrafts = optional_param('removeorphaneddrafts', 0, PARAM_INT);
  38  
  39  $context = context::instance_by_id($contextid);
  40  if ($context->contextlevel == CONTEXT_MODULE) {
  41      // Module context.
  42      $cm = $DB->get_record('course_modules', ['id' => $context->instanceid]);
  43      require_login($cm->course, true, $cm);
  44  } else if (($coursecontext = $context->get_course_context(false)) && $coursecontext->id != SITEID) {
  45      // Course context or block inside the course.
  46      require_login($coursecontext->instanceid);
  47      $PAGE->set_context($context);
  48  } else {
  49      // Block that is not inside the course, user or system context.
  50      require_login();
  51      $PAGE->set_context($context);
  52  }
  53  
  54  // Guests can never manage files.
  55  if (isguestuser()) {
  56      throw new \moodle_exception('noguest');
  57  }
  58  
  59  $title = get_string('managefiles', 'tiny_media');
  60  
  61  $url = new moodle_url('/lib/editor/tiny/plugins/media/manage.php', [
  62      'itemid' => $itemid,
  63      'maxbytes' => $maxbytes,
  64      'subdirs' => $subdirs,
  65      'accepted_types' => $acceptedtypes,
  66      'return_types' => $returntypes,
  67      'areamaxbytes' => $areamaxbytes,
  68      'context' => $contextid,
  69      'elementid' => $elementid,
  70      'removeorphaneddrafts' => $removeorphaneddrafts,
  71  ]);
  72  
  73  $PAGE->set_url($url);
  74  $PAGE->set_title($title);
  75  $PAGE->set_heading($title);
  76  $PAGE->set_pagelayout('popup');
  77  
  78  if ($returntypes !== null) {
  79      // Links are allowed in textarea but never allowed in filemanager.
  80      $returntypes = $returntypes & ~FILE_EXTERNAL;
  81  }
  82  
  83  // These are the options required for the filepicker.
  84  $options = [
  85      'subdirs' => $subdirs,
  86      'maxbytes' => $maxbytes,
  87      'maxfiles' => -1,
  88      'accepted_types' => $acceptedtypes,
  89      'areamaxbytes' => $areamaxbytes,
  90      'return_types' => $returntypes,
  91      'context' => $context
  92  ];
  93  
  94  $usercontext = context_user::instance($USER->id);
  95  $fs = get_file_storage();
  96  $files = $fs->get_directory_files($usercontext->id, 'user', 'draft', $itemid, '/', !empty($subdirs), false);
  97  $filenames = [];
  98  foreach ($files as $file) {
  99      $filenames[$file->get_pathnamehash()] = ltrim($file->get_filepath(), '/') . $file->get_filename();
 100  }
 101  
 102  $mform = new tiny_media\form\manage_files_form(null, [
 103      'context' => $usercontext,
 104      'options' => $options,
 105      'draftitemid' => $itemid,
 106      'files' => $filenames,
 107      'elementid' => $elementid,
 108      'removeorphaneddrafts' => $removeorphaneddrafts,
 109      ], 'post', '', [
 110          'id' => 'tiny_media_form',
 111      ]
 112  );
 113  
 114  if ($data = $mform->get_data()) {
 115      if (!empty($data->deletefile)) {
 116          foreach (array_keys($data->deletefile) as $filehash) {
 117              if ($file = $fs->get_file_by_hash($filehash)) {
 118                  // Make sure the user didn't modify the filehash to delete another file.
 119                  if ($file->get_component() !== 'user' || $file->get_filearea() !== 'draft') {
 120                      // The file must belong to the user/draft area.
 121                      continue;
 122                  }
 123                  if ($file->get_contextid() != $usercontext->id) {
 124                      // The user must own the file - that is it must be in their user draft file area.
 125                      continue;
 126                  }
 127                  if ($file->get_itemid() != $itemid) {
 128                      // It must be the file they requested be deleted.
 129                      continue;
 130                  }
 131                  $file->delete();
 132              }
 133          }
 134      }
 135      // Redirect to prevent re-posting the form.
 136      redirect($url);
 137  }
 138  
 139  $mform->set_data(array_merge($options, [
 140      'files_filemanager' => $itemid,
 141      'itemid' => $itemid,
 142      'elementid' => $elementid,
 143      'context' => $context->id,
 144  ]));
 145  
 146  echo $OUTPUT->header();
 147  $mform->display();
 148  echo $OUTPUT->footer();