Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
16
 
17
namespace core_blog\external;
18
 
19
use core_external\external_api;
20
use core_external\external_function_parameters;
21
use core_external\external_single_structure;
22
use core_external\external_value;
23
use core_external\external_warnings;
24
use context_course;
25
use moodle_exception;
26
 
27
/**
28
 * This is the external method for deleting a blog post entry.
29
 *
30
 * @package    core_blog
31
 * @copyright  2024 Juan Leyva <juan@moodle.com>
32
 * @category   external
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
class delete_entry extends external_api {
36
 
37
    /**
38
     * Describes the external function parameters.
39
     *
40
     * @return external_function_parameters
41
     */
42
    public static function execute_parameters(): external_function_parameters {
43
        return new external_function_parameters(
44
            [
45
                'entryid' => new external_value(PARAM_INT, 'The entry id to remove.'),
46
            ]
47
        );
48
    }
49
 
50
    /**
51
     * Deletes a blog entry.
52
     *
53
     * @param int $entryid The entry id to remove.
54
     * @throws moodle_exception;
55
     * @return array result of the operation
56
     */
57
    public static function execute(int $entryid): array {
58
        global $USER, $CFG;
59
 
60
        require_once($CFG->dirroot . '/blog/lib.php');
61
        require_once($CFG->dirroot . '/blog/locallib.php');
62
 
63
        $params = self::validate_parameters(self::execute_parameters(), ['entryid' => $entryid]);
64
 
65
        if (empty($CFG->enableblogs)) {
66
            throw new moodle_exception('blogdisable', 'blog');
67
        }
68
 
69
        if (!$entry = new \blog_entry($params['entryid'])) {
70
            throw new moodle_exception('wrongentryid', 'blog');
71
        }
72
 
73
        $courseid = !empty($entry->courseid) ? $entry->courseid : SITEID;
74
        $context = context_course::instance($courseid);
75
 
76
        self::validate_context($context);
77
 
78
        if (!blog_user_can_edit_entry($entry)) {
79
            throw new \moodle_exception('nopermissionstodeleteentry', 'blog');
80
        }
81
 
82
        $entry->delete();
83
 
84
        $result = [
85
            'status' => true,
86
            'warnings' => [],
87
        ];
88
        return $result;
89
    }
90
 
91
    /**
92
     * Return.
93
     *
94
     * @return external_single_structure
95
     */
96
    public static function execute_returns() : external_single_structure {
97
        return new external_single_structure(
98
            [
99
                'status' => new external_value(PARAM_BOOL, 'True indicates the entry was deleted.'),
100
                'warnings' => new external_warnings(),
101
            ]
102
        );
103
    }
104
}