Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 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 namespace core_contentbank\external; 18 19 use core_contentbank\contentbank; 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 27 /** 28 * This is the external method for deleting a content. 29 * 30 * @package core_contentbank 31 * @since Moodle 3.9 32 * @copyright 2020 Sara Arjona <sara@moodle.com> 33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 34 */ 35 class delete_content extends external_api { 36 /** 37 * Parameters. 38 * 39 * @return external_function_parameters 40 */ 41 public static function execute_parameters(): external_function_parameters { 42 return new external_function_parameters([ 43 'contentids' => new external_multiple_structure( 44 new external_value(PARAM_INT, 'The content id to delete', VALUE_REQUIRED) 45 ) 46 ]); 47 } 48 49 /** 50 * Delete content from the contentbank. 51 * 52 * @param array $contentids List of content ids to delete. 53 * @return array True if the content has been deleted; false and the warning, otherwise. 54 */ 55 public static function execute(array $contentids): array { 56 global $DB; 57 58 $result = false; 59 $warnings = []; 60 61 $params = self::validate_parameters(self::execute_parameters(), ['contentids' => $contentids]); 62 $cb = new contentbank(); 63 foreach ($params['contentids'] as $contentid) { 64 try { 65 $record = $DB->get_record('contentbank_content', ['id' => $contentid], '*', MUST_EXIST); 66 $content = $cb->get_content_from_id($record->id); 67 $contenttype = $content->get_content_type_instance(); 68 $context = \context::instance_by_id($record->contextid, MUST_EXIST); 69 self::validate_context($context); 70 // Check capability. 71 if ($contenttype->can_delete($content)) { 72 // This content can be deleted. 73 if (!$contenttype->delete_content($content)) { 74 $warnings[] = [ 75 'item' => $contentid, 76 'warningcode' => 'contentnotdeleted', 77 'message' => get_string('contentnotdeleted', 'core_contentbank') 78 ]; 79 } 80 } else { 81 // The user has no permission to delete this content. 82 $warnings[] = [ 83 'item' => $contentid, 84 'warningcode' => 'nopermissiontodelete', 85 'message' => get_string('nopermissiontodelete', 'core_contentbank') 86 ]; 87 } 88 } catch (\moodle_exception $e) { 89 // The content or the context don't exist. 90 $warnings[] = [ 91 'item' => $contentid, 92 'warningcode' => 'exception', 93 'message' => $e->getMessage() 94 ]; 95 } 96 } 97 98 if (empty($warnings)) { 99 $result = true; 100 } 101 102 return [ 103 'result' => $result, 104 'warnings' => $warnings 105 ]; 106 } 107 108 /** 109 * Return. 110 * 111 * @return external_single_structure 112 */ 113 public static function execute_returns(): external_single_structure { 114 return new external_single_structure([ 115 'result' => new external_value(PARAM_BOOL, 'The processing result'), 116 'warnings' => new external_warnings() 117 ]); 118 } 119 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body