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