Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]
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_single_structure; 23 use core_external\external_value; 24 use core_external\external_warnings; 25 26 /** 27 * This is the external method for renaming a content. 28 * 29 * @package core_contentbank 30 * @copyright 2020 Amaia Anabitarte <amaia@moodle.com> 31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 32 */ 33 class rename_content extends external_api { 34 /** 35 * rename_content parameters. 36 * 37 * @since Moodle 3.9 38 * @return external_function_parameters 39 */ 40 public static function execute_parameters(): external_function_parameters { 41 return new external_function_parameters( 42 [ 43 'contentid' => new external_value(PARAM_INT, 'The content id to rename', VALUE_REQUIRED), 44 'name' => new external_value(PARAM_RAW, 'The new name for the content', VALUE_REQUIRED), 45 ] 46 ); 47 } 48 49 /** 50 * Rename content from the contentbank. 51 * 52 * @since Moodle 3.9 53 * @param int $contentid The content id to rename. 54 * @param string $name The new name. 55 * @return array True if the content has been renamed; false and the warning, otherwise. 56 */ 57 public static function execute(int $contentid, string $name): array { 58 global $DB; 59 60 $result = false; 61 $warnings = []; 62 63 $params = self::validate_parameters(self::execute_parameters(), [ 64 'contentid' => $contentid, 65 'name' => $name, 66 ]); 67 $params['name'] = clean_param($params['name'], PARAM_TEXT); 68 69 // If name is empty don't try to rename and return a more detailed message. 70 if (trim($params['name']) === '') { 71 $warnings[] = [ 72 'item' => $params['contentid'], 73 'warningcode' => 'emptynamenotallowed', 74 'message' => get_string('emptynamenotallowed', 'core_contentbank') 75 ]; 76 } else { 77 try { 78 $record = $DB->get_record('contentbank_content', ['id' => $params['contentid']], '*', MUST_EXIST); 79 $cb = new contentbank(); 80 $content = $cb->get_content_from_id($record->id); 81 $contenttype = $content->get_content_type_instance(); 82 $context = \context::instance_by_id($record->contextid, MUST_EXIST); 83 self::validate_context($context); 84 // Check capability. 85 if ($contenttype->can_manage($content)) { 86 // This content can be renamed. 87 if ($contenttype->rename_content($content, $params['name'])) { 88 $result = true; 89 } else { 90 $warnings[] = [ 91 'item' => $params['contentid'], 92 'warningcode' => 'contentnotrenamed', 93 'message' => get_string('contentnotrenamed', 'core_contentbank') 94 ]; 95 } 96 } else { 97 // The user has no permission to manage this content. 98 $warnings[] = [ 99 'item' => $params['contentid'], 100 'warningcode' => 'nopermissiontomanage', 101 'message' => get_string('nopermissiontomanage', 'core_contentbank') 102 ]; 103 } 104 } catch (\moodle_exception $e) { 105 // The content or the context don't exist. 106 $warnings[] = [ 107 'item' => $params['contentid'], 108 'warningcode' => 'exception', 109 'message' => $e->getMessage() 110 ]; 111 } 112 } 113 114 return [ 115 'result' => $result, 116 'warnings' => $warnings 117 ]; 118 } 119 120 /** 121 * rename_content return. 122 * 123 * @since Moodle 3.9 124 * @return external_single_structure 125 */ 126 public static function execute_returns(): external_single_structure { 127 return new external_single_structure([ 128 'result' => new external_value(PARAM_BOOL, 'The processing result'), 129 'warnings' => new external_warnings() 130 ]); 131 } 132 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body