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