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 - 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_external\external_api;
20
use core_external\external_function_parameters;
21
use core_external\external_single_structure;
22
use core_external\external_multiple_structure;
23
use core_external\external_value;
24
use core_external\external_warnings;
25
use moodle_exception;
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
29
global $CFG;
30
require_once($CFG->libdir . '/badgeslib.php');
31
 
32
/**
33
 * External service to get user badge.
34
 *
35
 * This is mainly used by the mobile application.
36
 *
37
 * @package   core_badges
38
 * @category  external
39
 * @copyright 2023 Rodrigo Mady <rodrigo.mady@moodle.com>
40
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41
 * @since     Moodle 4.3
42
 */
43
class get_user_badge_by_hash extends external_api {
44
    /**
45
     * Returns description of method parameters
46
     *
47
     * @return external_function_parameters
48
     */
49
    public static function execute_parameters(): external_function_parameters {
50
        return new external_function_parameters([
51
            'hash' => new external_value(PARAM_ALPHANUM, 'Badge issued hash', VALUE_REQUIRED),
52
        ]);
53
    }
54
 
55
    /**
56
     * Execute the get user badge.
57
     *
58
     * @param string $hash
59
     * @return array
60
     * @throws \restricted_context_exception
61
     */
62
    public static function execute(string $hash): array {
63
        global $CFG;
64
 
65
        // Initialize return variables.
66
        $warnings = [];
67
        $result   = [];
68
 
69
        // Validate the hash.
70
        [
71
            'hash' => $hash,
72
        ] = self::validate_parameters(self::execute_parameters(), [
73
            'hash' => $hash,
74
        ]);
75
 
76
        if (empty($CFG->enablebadges)) {
77
            throw new moodle_exception('badgesdisabled', 'badges');
78
        }
79
 
80
        // Get the badge by hash.
81
        $badge = badges_get_badge_by_hash($hash);
82
 
83
        if (!empty($badge)) {
84
            // Get the user that issued the badge.
85
            $user     = \core_user::get_user($badge->userid, '*', MUST_EXIST);
86
            $result[] = badges_prepare_badge_for_external($badge, $user);
87
        } else {
88
            $warnings[] = [
89
                'item'        => $hash,
90
                'warningcode' => 'badgeawardnotfound',
91
                'message'     => get_string('error:badgeawardnotfound', 'badges')
92
            ];
93
        }
94
 
95
        return [
96
            'badge'    => $result,
97
            'warnings' => $warnings
98
        ];
99
    }
100
 
101
    /**
102
     * Describe the return structure of the external service.
103
     *
104
     * @return external_single_structure
105
     */
106
    public static function execute_returns(): external_single_structure {
107
        return new external_single_structure([
108
            'badge'  => new external_multiple_structure(
109
                user_badge_exporter::get_read_structure()
110
            ),
111
            'warnings' => new external_warnings()
112
        ]);
113
    }
114
}