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