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.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 400 and 403] [Versions 401 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   * This is the external method for deleting draft files.
  19   *
  20   * @package    core_files
  21   * @since      Moodle 3.10
  22   * @copyright  2020 Juan Leyva <juan@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core_files\external\delete;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  global $CFG;
  31  require_once($CFG->libdir . '/filelib.php');
  32  
  33  use core_external\external_api;
  34  use core_external\external_function_parameters;
  35  use core_external\external_multiple_structure;
  36  use core_external\external_single_structure;
  37  use core_external\external_value;
  38  use core_external\external_warnings;
  39  use context_user;
  40  
  41  /**
  42   * This is the external method for deleting draft files.
  43   *
  44   * @copyright  2020 Juan Leyva <juan@moodle.com>
  45   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  46   */
  47  class draft extends external_api {
  48  
  49      /**
  50       * Describes the parameters for execute.
  51       *
  52       * @return external_function_parameters
  53       * @since Moodle 3.10
  54       */
  55      public static function execute_parameters() : external_function_parameters {
  56          return new external_function_parameters (
  57              [
  58                  'draftitemid' => new external_value(PARAM_INT, 'Item id of the draft file area'),
  59                  'files' => new external_multiple_structure(
  60                      new external_single_structure(
  61                          [
  62                              'filepath'  => new external_value(PARAM_PATH, 'Path to the file or directory to delete.'),
  63                              'filename'  => new external_value(PARAM_FILE, 'Name of the file to delete.'),
  64                          ]
  65                      ), 'Files or directories to be deleted.'
  66                  ),
  67              ]
  68          );
  69      }
  70  
  71      /**
  72       * Delete the indicated files (or directories) from a user draft file area.
  73       *
  74       * @param  int    $draftitemid item id of the draft file area
  75       * @param  array  $files       files to be deleted
  76       * @return array of warnings and parent paths of the files deleted
  77       * @since Moodle 3.10
  78       */
  79      public static function execute(int $draftitemid, array $files) : array {
  80          global $CFG, $USER;
  81          require_once($CFG->dirroot . '/repository/lib.php');
  82  
  83          $params = self::validate_parameters(self::execute_parameters(), compact('draftitemid', 'files'));
  84          [$draftitemid, $files] = array_values($params);
  85  
  86          $usercontext = context_user::instance($USER->id);
  87          self::validate_context($usercontext);
  88  
  89          $files = array_map(function($file) {
  90              return (object) $file;
  91          }, $files);
  92          $parentpaths = repository_delete_selected_files($usercontext, 'user', 'draft', $draftitemid, $files);
  93  
  94          return [
  95              'parentpaths' => array_keys($parentpaths),
  96              'warnings' => [],
  97          ];
  98      }
  99  
 100      /**
 101       * Describes the execute return value.
 102       *
 103       * @return external_single_structure
 104       * @since Moodle 3.10
 105       */
 106      public static function execute_returns() : external_single_structure {
 107          return new external_single_structure(
 108              [
 109                  'parentpaths' => new external_multiple_structure(
 110                      new external_value(PARAM_PATH, 'Path to parent directory of the deleted files.')
 111                  ),
 112                  'warnings' => new external_warnings(),
 113              ]
 114          );
 115      }
 116  }