2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 namespace core\external
;
20 use core\moodlenet\utilities
;
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
;
29 * The external API to het the activity information for MoodleNet sharing.
32 * @copyright 2023 Huong Nguyen <huongnv13@gmail.com>
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class moodlenet_get_share_info_activity
extends external_api
{
38 * Returns description of parameters.
40 * @return external_function_parameters
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
),
50 * External function to get the activity information.
52 * @param int $cmid The course module id.
56 public static function execute(int $cmid): array {
61 ] = self
::validate_parameters(self
::execute_parameters(), [
66 $coursemodule = get_coursemodule_from_id(false, $cmid);
68 return self
::return_errors($cmid, 'errorgettingactivityinformation', get_string('invalidcoursemodule', 'error'));
72 $course = get_course($coursemodule->course
);
75 $coursecontext = context_course
::instance($course->id
);
76 $usercanshare = utilities
::can_user_share($coursecontext, $USER->id
);
78 return self
::return_errors($cmid, 'errorpermission',
79 get_string('nopermissions', 'error', get_string('moodlenet:sharetomoodlenet', 'moodle')));
83 $supporturl = utilities
::get_support_url();
84 $issuerid = get_config('moodlenet', 'oauthservice');
86 if (empty($issuerid)) {
87 return self
::return_errors(0, 'errorissuernotset', get_string('moodlenet:issuerisnotset', 'moodle'));
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'));
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
109 * Describes the data returned from the external function.
111 * @return external_single_structure
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()
127 * Handle return error.
129 * @param int $itemid Item id.
130 * @param string $warningcode Warning code.
131 * @param string $message Message.
132 * @param int $issuerid Issuer id.
135 protected static function return_errors(int $itemid, string $warningcode, string $message, int $issuerid = 0): array {
138 'warningcode' => $warningcode,
139 'message' => $message
147 'supportpageurl' => '',
148 'issuerid' => $issuerid,
149 'warnings' => $warnings