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  namespace mod_data\external;
  18  
  19  use core\notification;
  20  use mod_data\manager;
  21  use mod_data\preset;
  22  
  23  defined('MOODLE_INTERNAL') || die();
  24  
  25  global $CFG;
  26  require_once($CFG->libdir . '/externallib.php');
  27  
  28  /**
  29   * This is the external method for deleting a saved preset.
  30   *
  31   * @package    mod_data
  32   * @since      Moodle 4.1
  33   * @copyright  2022 Amaia Anabitarte <amaia@moodle.com>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class delete_saved_preset extends \external_api {
  37      /**
  38       * Parameters.
  39       *
  40       * @return \external_function_parameters
  41       */
  42      public static function execute_parameters(): \external_function_parameters {
  43          return new \external_function_parameters([
  44              'dataid' => new \external_value(PARAM_INT, 'Id of the data activity', VALUE_REQUIRED),
  45              'presetnames' => new \external_multiple_structure(
  46                  new \external_value(PARAM_TEXT, 'The preset name to delete', VALUE_REQUIRED)
  47              )
  48          ]);
  49      }
  50  
  51      /**
  52       * Delete saved preset from the file system.
  53       *
  54       * @param  int $dataid Id of the data activity to check context and permissions.
  55       * @param  array $presetnames List of saved preset names to delete.
  56       * @return array True if the content has been deleted; false and the warning, otherwise.
  57       */
  58      public static function execute(int $dataid, array $presetnames): array {
  59          global $DB;
  60  
  61          $result = false;
  62          $warnings = [];
  63  
  64          $params = self::validate_parameters(self::execute_parameters(), ['dataid' => $dataid, 'presetnames' => $presetnames]);
  65  
  66          $instance = $DB->get_record('data', ['id' => $params['dataid']], '*', MUST_EXIST);
  67          $manager = manager::create_from_instance($instance);
  68  
  69          foreach ($params['presetnames'] as $presetname) {
  70              try {
  71                  $preset = preset::create_from_instance($manager, $presetname);
  72                  if ($preset->can_manage()) {
  73                      if ($preset->delete()) {
  74                          notification::success(get_string('presetdeleted', 'mod_data'));
  75                          $result = true;
  76                      } else {
  77                          // An error ocurred while deleting the preset.
  78                          $warnings[] = [
  79                              'item' => $presetname,
  80                              'warningcode' => 'failedpresetdelete',
  81                              'message' => get_string('failedpresetdelete', 'mod_data')
  82                          ];
  83                          notification::error(get_string('failedpresetdelete', 'mod_data'));
  84                      }
  85                  } else {
  86                      // The user has no permission to delete the preset.
  87                      $warnings[] = [
  88                          'item' => $presetname,
  89                          'warningcode' => 'cannotdeletepreset',
  90                          'message' => get_string('cannotdeletepreset', 'mod_data')
  91                      ];
  92                      notification::error(get_string('cannotdeletepreset', 'mod_data'));
  93                  }
  94              } catch (\moodle_exception $e) {
  95                  // The saved preset has not been deleted.
  96                  $warnings[] = [
  97                      'item' => $presetname,
  98                      'warningcode' => 'exception',
  99                      'message' => $e->getMessage()
 100                  ];
 101                  notification::error($e->getMessage());
 102              }
 103          }
 104  
 105          return [
 106              'result' => $result,
 107              'warnings' => $warnings
 108          ];
 109      }
 110  
 111      /**
 112       * Return.
 113       *
 114       * @return \external_single_structure
 115       */
 116      public static function execute_returns(): \external_single_structure {
 117          return new \external_single_structure([
 118              'result' => new \external_value(PARAM_BOOL, 'The processing result'),
 119              'warnings' => new \external_warnings()
 120          ]);
 121      }
 122  }