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\exception\moodle_exception;
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_value;
24
use core_external\external_warnings;
25
 
26
defined('MOODLE_INTERNAL') || die();
27
 
28
global $CFG;
29
require_once($CFG->libdir . '/badgeslib.php');
30
 
31
/**
32
 * External service to get badge by id.
33
 *
34
 * @package   core_badges
35
 * @category  external
36
 * @copyright  2024 Daniel Ureña <durenadev@gmail.com>
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 * @since      Moodle 4.5
39
 */
40
class get_badge extends external_api {
41
 
42
    /**
43
     * Returns description of method parameters
44
     *
45
     * @return external_function_parameters
46
     */
47
    public static function execute_parameters(): external_function_parameters {
48
        return new external_function_parameters([
49
            'id' => new external_value(PARAM_INT, 'Badge id', VALUE_REQUIRED),
50
        ]);
51
    }
52
 
53
    /**
54
     * Execute the get badge by id.
55
     *
56
     * @param int $id
57
     * @return array
58
     * @throws moodle_exception
59
     */
60
    public static function execute(int $id): array {
61
        global $CFG, $PAGE;
62
 
63
        // Initialize return variables.
64
        $warnings = [];
65
 
66
        // Validate the id.
67
        [
68
            'id' => $id,
69
        ] = self::validate_parameters(self::execute_parameters(), [
70
            'id' => $id,
71
        ]);
72
 
73
        // Validate badges is not disabled.
74
        if (empty($CFG->enablebadges)) {
75
            throw new moodle_exception('badgesdisabled', 'badges');
76
        }
77
 
78
        // Get the badge by id.
79
        $badgeclass = new \core_badges\output\badgeclass($id);
80
        if (empty($badgeclass->badge)) {
81
            throw new moodle_exception('error:relatedbadgedoesntexist', 'badges');
82
        }
83
 
84
        $PAGE->set_context($badgeclass->context);
85
 
86
        $result = badges_prepare_badgeclass_for_external($badgeclass);
87
 
88
        return [
89
            'badge'    => $result,
90
            'warnings' => $warnings,
91
        ];
92
    }
93
 
94
    /**
95
     * Describe the return structure of the external service.
96
     *
97
     * @return external_single_structure
98
     */
99
    public static function execute_returns(): external_single_structure {
100
        return new external_single_structure([
101
            'badge'  => badgeclass_exporter::get_read_structure(),
102
            'warnings' => new external_warnings(),
103
        ]);
104
    }
105
}