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