-
Notifications
You must be signed in to change notification settings - Fork 9
/
external.php
327 lines (297 loc) · 11.8 KB
/
external.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Opencast external API
*
* @package tool_opencast
* @category external
* @copyright 2018 Tobias Reischmann <tobias.reischmann@wi.uni-muenster.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.2
*/
defined('MOODLE_INTERNAL') || die;
require_once($CFG->libdir . '/externallib.php');
require_once($CFG->libdir . '/authlib.php');
/**
* Opencast external API
*
* @package tool_opencast
* @category external
* @copyright 2018 Tobias Reischmann <tobias.reischmann@wi.uni-muenster.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_opencast_external extends external_api {
/**
* Describes the parameters for getting courses for a opencast instructor.
*
* @return external_function_parameters
* @throws coding_exception
*/
public static function get_courses_for_instructor_parameters() {
return new external_function_parameters(
[
'username' => new external_value(core_user::get_property_type('username'), 'User Name'),
]
);
}
/**
* Describes the parameters for getting courses for a opencast learner.
*
* @return external_function_parameters
* @throws coding_exception
*/
public static function get_courses_for_learner_parameters() {
return new external_function_parameters(
[
'username' => new external_value(core_user::get_property_type('username'), 'User Name'),
]
);
}
/**
* Describes the parameters for getting groups for a opencast user.
*
* @return external_function_parameters
* @throws coding_exception
*/
public static function get_groups_for_learner_parameters() {
return new external_function_parameters(
[
'username' => new external_value(core_user::get_property_type('username'), 'User Name'),
]
);
}
/**
* Get all courses for a user, in which he has the capabilities of a instructor.
*
* @param string $username user name
* @return array list of course ids
* @throws coding_exception
* @throws dml_exception
* @throws invalid_parameter_exception
* @throws required_capability_exception
*/
public static function get_courses_for_instructor($username) {
self::validate_parameters(self::get_courses_for_instructor_parameters(), ['username' => $username]);
return self::get_courses_with_capability($username, 'tool/opencast:instructor');
}
/**
* Get all courses for a user, in which he has the capabilities of a learner.
*
* @param string $username user name
* @return array list of course ids
* @throws coding_exception
* @throws dml_exception
* @throws invalid_parameter_exception
* @throws required_capability_exception
*/
public static function get_courses_for_learner($username) {
self::validate_parameters(self::get_courses_for_learner_parameters(), ['username' => $username]);
return self::get_courses_with_capability($username, 'tool/opencast:learner');
}
/**
* Get all courses for a user, in which he has the capabilities of a learner.
*
* @param string $username user name
* @return array list of course ids
* @throws coding_exception
* @throws dml_exception
* @throws invalid_parameter_exception
* @throws required_capability_exception
*/
public static function get_groups_for_learner($username) {
self::validate_parameters(self::get_groups_for_learner_parameters(), ['username' => $username]);
$context = context_system::instance();
if (!has_capability('tool/opencast:externalapi', $context)) {
throw new required_capability_exception($context, 'tool/opencast:externalapi', 'nopermissions', '');
}
if (!has_capability('moodle/site:accessallgroups', $context)) {
throw new required_capability_exception($context, 'moodle/site:accessallgroups', 'nopermissions', '');
}
global $DB;
$user = core_user::get_user_by_username($username);
return $DB->get_records('groups_members', ['userid' => $user->id], '', 'groupid as id');
}
/**
* Returns all course ids where the user has the specific capability in.
* @param string $username the username
* @param string $capability the moodle capability
* @return array
* @throws coding_exception
* @throws dml_exception
* @throws required_capability_exception
*/
private static function get_courses_with_capability($username, $capability) {
$result = [];
$context = context_system::instance();
if (!has_capability('tool/opencast:externalapi', $context)) {
throw new required_capability_exception($context, 'tool/opencast:externalapi', 'nopermissions', '');
}
$user = core_user::get_user_by_username($username);
$courses = enrol_get_all_users_courses($user->id);
foreach ($courses as $course) {
$context = context_course::instance($course->id);
if (has_capability($capability, $context, $user)) {
$result[] = ['id' => $course->id];
}
}
return $result;
}
/**
* Describes the confirm_user return value.
*
* @return external_multiple_structure array of course ids
*/
public static function get_courses_for_instructor_returns() {
return new external_multiple_structure(
new external_single_structure(
[
'id' => new external_value(PARAM_INT, 'id of course'),
]
)
);
}
/**
* Describes the confirm_user return value.
*
* @return external_multiple_structure array of course ids
*/
public static function get_courses_for_learner_returns() {
return new external_multiple_structure(
new external_single_structure(
[
'id' => new external_value(PARAM_INT, 'id of course'),
]
)
);
}
/**
* Describes the confirm_user return value.
*
* @return external_multiple_structure array of course ids
*/
public static function get_groups_for_learner_returns() {
return new external_multiple_structure(
new external_single_structure(
[
'id' => new external_value(PARAM_INT, 'id of group'),
]
)
);
}
/**
* Describes the connection_test_tool return value.
*
* @return external_single_structure array the result of the connection test
*/
public static function connection_test_tool_returns() {
return new external_single_structure(
[
'testresult' => new external_value(PARAM_RAW, 'Opencast API URL Test result'),
]
);
}
/**
* Describes the parameters for testing the connection.
*
* @return external_function_parameters
* @throws coding_exception
*/
public static function connection_test_tool_parameters() {
return new external_function_parameters(
[
'apiurl' => new external_value(PARAM_TEXT, 'Opencast API URL'),
'apiusername' => new external_value(PARAM_TEXT, 'Opencast API User'),
'apipassword' => new external_value(PARAM_RAW, 'Opencast API Password'),
'apitimeout' => new external_value(PARAM_INT, 'API timeout', VALUE_DEFAULT, 2000),
'apiconnecttimeout' => new external_value(PARAM_INT, 'API connect timeout', VALUE_DEFAULT, 1000),
]
);
}
/**
* Builds a html tag for the alert of the connection test tool.
*
* @param string $connectiontestresult The result of a connection test.
* @param string $testsuccessfulstringidentifier The string identifier of a successful connection test.
* @param string $testfailedstringidentifier The string identifier of a failed connection test.
* @return string The html tag as string.
*/
private static function connection_test_tool_build_html_alert_tag($connectiontestresult,
string $testsuccessfulstringidentifier,
string $testfailedstringidentifier): string {
// Check, if the test was successful.
if ($connectiontestresult === true) {
return html_writer::tag(
'p',
get_string($testsuccessfulstringidentifier, 'tool_opencast'),
['class' => 'alert alert-success']
);
}
return html_writer::tag(
'p',
get_string($testfailedstringidentifier, 'tool_opencast', $connectiontestresult),
['class' => 'alert alert-danger']
);
}
/**
* Perform the connection test via Ajax call to be able to show it in Modal.
*
* @param string $apiurl Opencast API URL
* @param string $apiusername Opencast API username
* @param string $apipassword Opencast API password
* @param int $apitimeout Overall API request execution timeout in milliseconds
* @param int $apiconnecttimeout Connection timeout in milliseconds
* @return array
* @throws coding_exception
* @throws dml_exception
* @throws invalid_parameter_exception
* @throws required_capability_exception
*/
public static function connection_test_tool($apiurl, $apiusername, $apipassword, $apitimeout, $apiconnecttimeout) {
// Validate the parameters.
$params = self::validate_parameters(self::connection_test_tool_parameters(),
[
'apiurl' => $apiurl,
'apiusername' => $apiusername,
'apipassword' => $apipassword,
'apitimeout' => $apitimeout,
'apiconnecttimeout' => $apiconnecttimeout,
]
);
// Get a customized api instance to use.
$customizedapi = \tool_opencast\local\api::get_instance(null, [], [
'apiurl' => $params['apiurl'],
'apiusername' => $params['apiusername'],
'apipassword' => $params['apipassword'],
'apitimeout' => $params['apitimeout'],
'apiconnecttimeout' => $params['apiconnecttimeout'],
]);
// Test the URL.
$connectiontesturlresult = $customizedapi->connection_test_url();
$resulthtml = self::connection_test_tool_build_html_alert_tag(
$connectiontesturlresult,
'apiurltestsuccessfulshort',
'apiurltestfailedshort');
// Test the Credentials.
$connectiontestcredentialsresult = $customizedapi->connection_test_credentials();
$resulthtml .= self::connection_test_tool_build_html_alert_tag(
$connectiontestcredentialsresult,
'apicreadentialstestsuccessfulshort',
'apicreadentialstestfailedshort');
return [
'testresult' => $resulthtml,
];
}
}