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