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