Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
namespace core_badges\external;
18
 
19
use core_badges\badge;
20
use core_external\external_api;
21
use core_external\external_function_parameters;
22
use core_external\external_single_structure;
23
use core_external\external_multiple_structure;
24
use core_external\external_value;
25
use core_external\external_warnings;
26
use moodle_exception;
27
 
28
defined('MOODLE_INTERNAL') || die();
29
 
30
global $CFG;
31
require_once($CFG->libdir . '/badgeslib.php');
32
 
33
/**
34
 * External service to disable badges.
35
 *
36
 * @package   core_badges
37
 * @category  external
38
 * @copyright 2024 Sara Arjona <sara@moodle.com>
39
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40
 * @since     Moodle 4.5
41
 */
42
class disable_badges extends external_api {
43
 
44
    /**
45
     * Describes the parameters.
46
     *
47
     * @return external_function_parameters
48
     */
49
    public static function execute_parameters(): external_function_parameters {
50
        return new external_function_parameters([
51
            'badgeids' => new external_multiple_structure(
52
                new external_value(PARAM_TEXT, 'The badge identifiers to update', VALUE_REQUIRED),
53
            ),
54
        ]);
55
    }
56
 
57
    /**
58
     * Disable the given badges.
59
     *
60
     * @param array $badgeids List of badge identifiers to disable.
61
     * @return array List of results and warnings.
62
     */
63
    public static function execute(array $badgeids): array {
64
        global $CFG, $DB;
65
 
66
        $warnings = [];
67
 
68
        [
69
            'badgeids' => $badgeids,
70
        ] = self::validate_parameters(self::execute_parameters(), [
71
            'badgeids' => $badgeids,
72
        ]);
73
 
74
        // Check if badges are enabled.
75
        if (empty($CFG->enablebadges)) {
76
            throw new moodle_exception('badgesdisabled', 'badges');
77
        }
78
 
79
        foreach ($badgeids as $badgeid) {
80
            $badge = new badge($badgeid);
81
 
82
            // Check capabilities.
83
            $context = $badge->get_context();
84
            self::validate_context($context);
85
            if (!has_capability('moodle/badges:configurecriteria', $context)) {
86
                $warnings[] = [
87
                    'item'        => $badgeid,
88
                    'warningcode' => 'nopermissions',
89
                    'message'     => get_string('nopermissions', 'error'),
90
                ];
91
                continue;
92
            }
93
 
94
            // Check if course badges are enabled.
95
            if (empty($CFG->badges_allowcoursebadges) && ($badge->type == BADGE_TYPE_COURSE)) {
96
                $warnings[] = [
97
                    'item'        => $badgeid,
98
                    'warningcode' => 'coursebadgesdisabled',
99
                    'message'     => get_string('coursebadgesdisabled', 'badges'),
100
                ];
101
                continue;
102
            }
103
 
104
            $status = ($badge->status == BADGE_STATUS_ACTIVE) ? BADGE_STATUS_INACTIVE : BADGE_STATUS_INACTIVE_LOCKED;
105
            // Deactivate the badge.
106
            $badge->set_status($status);
107
        }
108
 
109
        return [
110
            'result' => empty($warnings),
111
            'warnings' => $warnings,
112
        ];
113
    }
114
 
115
    /**
116
     * Describe the return structure of the external service.
117
     *
118
     * @return external_single_structure
119
     */
120
    public static function execute_returns(): external_single_structure {
121
        return new external_single_structure([
122
            'result' => new external_value(PARAM_BOOL, 'The processing result'),
123
            'warnings' => new external_warnings(),
124
        ]);
125
    }
126
}