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\external;
18
 
19
use context_course;
20
use core\moodlenet\utilities;
21
use core\oauth2\api;
22
use core_external\external_api;
23
use core_external\external_function_parameters;
24
use core_external\external_single_structure;
25
use core_external\external_value;
26
use core_external\external_warnings;
27
 
28
/**
29
 * The external API to het the activity information for MoodleNet sharing.
30
 *
31
 * @package    core
32
 * @copyright  2023 Huong Nguyen <huongnv13@gmail.com>
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
class moodlenet_get_share_info_activity extends external_api {
36
 
37
    /**
38
     * Returns description of parameters.
39
     *
40
     * @return external_function_parameters
41
     * @since Moodle 4.2
42
     */
43
    public static function execute_parameters(): external_function_parameters {
44
        return new external_function_parameters([
45
            'cmid' => new external_value(PARAM_INT, 'The cmid of the activity', VALUE_REQUIRED),
46
        ]);
47
    }
48
 
49
    /**
50
     * External function to get the activity information.
51
     *
52
     * @param int $cmid The course module id.
53
     * @return array
54
     * @since Moodle 4.2
55
     */
56
    public static function execute(int $cmid): array {
57
        global $CFG, $USER;
58
 
59
        [
60
            'cmid' => $cmid
61
        ] = self::validate_parameters(self::execute_parameters(), [
62
            'cmid' => $cmid
63
        ]);
64
 
65
        // Get course module.
66
        $coursemodule = get_coursemodule_from_id(false, $cmid);
67
        if (!$coursemodule) {
68
            return self::return_errors($cmid, 'errorgettingactivityinformation', get_string('invalidcoursemodule', 'error'));
69
        }
70
 
71
        // Get course.
72
        $course = get_course($coursemodule->course);
73
 
74
        // Check capability.
75
        $coursecontext = context_course::instance($course->id);
76
        $usercanshare = utilities::can_user_share($coursecontext, $USER->id);
77
        if (!$usercanshare) {
78
            return self::return_errors($cmid, 'errorpermission',
79
                get_string('nopermissions', 'error', get_string('moodlenet:sharetomoodlenet', 'moodle')));
80
        }
81
 
82
        $warnings = [];
83
        $supporturl = utilities::get_support_url();
84
        $issuerid = get_config('moodlenet', 'oauthservice');
85
 
86
        if (empty($issuerid)) {
87
            return self::return_errors(0, 'errorissuernotset', get_string('moodlenet:issuerisnotset', 'moodle'));
88
        }
89
 
90
        // Get the issuer.
91
        $issuer = api::get_issuer($issuerid);
92
        // Validate the issuer and check if it is enabled or not.
93
        if (!utilities::is_valid_instance($issuer)) {
94
            return self::return_errors($issuerid, 'errorissuernotenabled', get_string('moodlenet:issuerisnotenabled', 'moodle'));
95
        }
96
 
97
        return [
98
            'status' => true,
99
            'name' => $coursemodule->name,
100
            'type' => get_string('modulename', $coursemodule->modname),
101
            'server' => $issuer->get_display_name(),
102
            'supportpageurl' => $supporturl,
103
            'issuerid' => $issuerid,
104
            'warnings' => $warnings
105
        ];
106
    }
107
 
108
    /**
109
     * Describes the data returned from the external function.
110
     *
111
     * @return external_single_structure
112
     * @since Moodle 4.2
113
     */
114
    public static function execute_returns(): external_single_structure {
115
        return new external_single_structure([
116
            'name' => new external_value(PARAM_TEXT, 'Activity name'),
117
            'type' => new external_value(PARAM_TEXT, 'Activity type'),
118
            'server' => new external_value(PARAM_TEXT, 'MoodleNet server'),
119
            'supportpageurl' => new external_value(PARAM_URL, 'Support page URL'),
120
            'issuerid' => new external_value(PARAM_INT, 'MoodleNet issuer id'),
121
            'status' => new external_value(PARAM_BOOL, 'status: true if success'),
122
            'warnings' => new external_warnings()
123
        ]);
124
    }
125
 
126
    /**
127
     * Handle return error.
128
     *
129
     * @param int $itemid Item id.
130
     * @param string $warningcode Warning code.
131
     * @param string $message Message.
132
     * @param int $issuerid Issuer id.
133
     * @return array
134
     */
135
    protected static function return_errors(int $itemid, string $warningcode, string $message, int $issuerid = 0): array {
136
        $warnings[] = [
137
            'item' => $itemid,
138
            'warningcode' => $warningcode,
139
            'message' => $message
140
        ];
141
 
142
        return [
143
            'status' => false,
144
            'name' => '',
145
            'type' => '',
146
            'server' => '',
147
            'supportpageurl' => '',
148
            'issuerid' => $issuerid,
149
            'warnings' => $warnings
150
        ];
151
    }
152
}