/
externallib.php
374 lines (302 loc) · 13.2 KB
/
externallib.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?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/>.
/**
* Proxy xAPI protocol
*
* @package local
* @subpackage lrsproxy
* @copyright 2016, Felix J. Garcia <fgarcia@um.es>
* Luis de la Torre Cubillo <ldelatorre@dia.uned.es>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->libdir . "/externallib.php");
require_once($CFG->dirroot. "/local/lrsproxy/locallib.php");
class lrsproxy_external extends external_api {
public static function echo_text_parameters () {
return new external_function_parameters(
array(
'text' => new external_value(PARAM_TEXT, 'Text to echo', VALUE_DEFAULT, '')
)
);
}
public static function echo_text ($text) {
global $USER;
// Parameter validation
$params = self::validate_parameters(self::echo_text_parameters(),
array('text' => $text));
// Context validation
$context = context_user::instance($USER->id);
self::validate_context($context);
// Capability checking (OPTIONAL but in most web service it should present)
// if (!has_capability('moodle/user:viewdetails', $context)) {
// throw new moodle_exception('cannotviewprofile');
// }
return $text;
}
public static function echo_text_returns () {
return new external_value(PARAM_TEXT, 'Echo text');
}
// proxy for https://github.com/adlnet/xAPI-Spec/blob/master/xAPI.md#722-poststatements
public static function store_statement_parameters () {
return new external_function_parameters(
array(
'statement' => new external_value(PARAM_RAW, 'Statement to store')
)
);
}
public static function store_statement ($statement) {
global $USER;
// Parameter validation
$params = self::validate_parameters(self::store_statement_parameters(),
array('statement' => $statement));
// Context validation
$context = context_user::instance($USER->id);
self::validate_context($context);
// Store statement
$response = tincan_store_statement($statement);
return $response;
}
public static function store_statement_returns () {
return new external_value(PARAM_RAW, 'Statement ID of stored statement');
}
// proxy for https://github.com/adlnet/xAPI-Spec/blob/master/xAPI.md#722-poststatements
public static function store_statements_parameters () {
return new external_function_parameters(
array(
'statements' => new external_multiple_structure(
new external_single_structure(
array(
'statement' => new external_value(PARAM_RAW, 'Statement to store')
)
)
)
)
);
}
public static function store_statements (array $statements) {
global $USER;
// Parameter validation
$params = self::validate_parameters(self::store_statements_parameters(),
array('statements' => $statements));
// Context validation
$context = context_user::instance($USER->id);
self::validate_context($context);
// Store statements
$response = array();
foreach ($params['statements'] as $element) {
array_push($response, tincan_store_statement(array_values($element)[0]));
}
return $response;
}
public static function store_statements_returns () {
return new external_multiple_structure(
new external_value(PARAM_RAW, 'Statement ID of stored statement')
);
}
// proxy for https://github.com/adlnet/xAPI-Spec/blob/master/xAPI.md#723-getstatements
public static function retrieve_statement_parameters () {
return new external_function_parameters(
array(
'statementId' => new external_value(PARAM_RAW, 'Statement ID to retrieve'),
)
);
}
public static function retrieve_statement ($statementid) {
global $USER;
// Parameter validation
$params = self::validate_parameters(self::retrieve_statement_parameters(),
array('statementId' => $statementid));
// Context validation
$context = context_user::instance($USER->id);
self::validate_context($context);
// Query statements
$response = tincan_retrieve_statement($statementid);
return $response;
}
public static function retrieve_statement_returns () {
return new external_value(PARAM_RAW, 'Statement requested if exists');
}
// proxy for https://github.com/adlnet/xAPI-Spec/blob/master/xAPI.md#723-getstatements
public static function fetch_statements_parameters () {
return new external_function_parameters(
array(
'registration' => new external_value(PARAM_RAW, 'Registration ID to fetch', VALUE_DEFAULT, null),
'agent' => new external_value(PARAM_RAW, 'Agent to fetch', VALUE_DEFAULT, null),
'verb' => new external_value(PARAM_RAW, 'Verb to fetch', VALUE_DEFAULT, null),
'activity' => new external_value(PARAM_RAW, 'Activity to fetch', VALUE_DEFAULT, null),
'since' => new external_value(PARAM_RAW, 'Only Statements stored since the specified timestamp (exclusive) are returned.', VALUE_DEFAULT, null),
'until' => new external_value(PARAM_RAW, 'Only Statements stored at or before the specified timestamp are returned.', VALUE_DEFAULT, null),
)
);
}
public static function fetch_statements ($registration, $agent, $verb, $activity, $since, $until) {
global $USER;
// Parameter validation
$params = self::validate_parameters(self::fetch_statements_parameters(), array(
'registration' => $registration,
'agent' => $agent,
'verb' => $verb,
'activity' => $activity,
'since' => $since,
'until' => $until));
// Context validation
$context = context_user::instance($USER->id);
self::validate_context($context);
// Query statements
$response = tincan_fetch_statements($registration, $agent, $verb, $activity, $since, $until);
return $response;
}
public static function fetch_statements_returns () {
return new external_value(PARAM_RAW, 'Statements requested if exists');
}
// proxy for https://github.com/adlnet/xAPI-Spec/blob/master/xAPI.md#74-state-api
public static function store_activity_state_parameters () {
return new external_function_parameters(
array(
'content' => new external_value(PARAM_RAW, 'State document to store'),
'activityId' => new external_value(PARAM_RAW, 'Activity ID associated with this state'),
'agent' => new external_value(PARAM_RAW, 'Agent associated with this state'),
'registration' => new external_value(PARAM_RAW, 'Registration ID associated with this state', VALUE_DEFAULT, null),
'stateId' => new external_value(PARAM_RAW, 'ID for the state, within the given context'),
)
);
}
public static function store_activity_state($content, $activityId, $agent, $registration, $stateId) {
global $USER;
// Parameter validation
$params = self::validate_parameters(self::store_activity_state_parameters(), array(
'content' => $content,
'activityId' => $activityId,
'agent' => $agent,
'registration' => $registration,
'stateId' => $stateId));
// Context validation
$context = context_user::instance($USER->id);
self::validate_context($context);
// Store activity state
$response = tincan_store_activity_state($content, $activityId, $agent, $registration, $stateId);
return $response;
}
public static function store_activity_state_returns() {
return new external_value(PARAM_RAW, 'State ID of stored state data');
}
// proxy for https://github.com/adlnet/xAPI-Spec/blob/master/xAPI.md#74-state-api
public static function retrieve_activity_state_parameters() {
return new external_function_parameters(
array(
'activityId' => new external_value(PARAM_RAW, 'Activity ID associated with this state'),
'agent' => new external_value(PARAM_RAW, 'Agent associated with this state'),
'stateId' => new external_value(PARAM_RAW, 'ID for the state, within the given context')
)
);
}
public static function retrieve_activity_state($activityId, $agent, $stateId) {
global $USER;
// Parameter validation
$params = self::validate_parameters(self::retrieve_activity_state_parameters(), array(
'activityId' => $activityId,
'agent' => $agent,
'stateId' => $stateId));
// Context validation
$context = context_user::instance($USER->id);
self::validate_context($context);
// Retrieve activity state
$response = tincan_retrieve_activity_state($activityId, $agent, $stateId);
return $response;
}
public static function retrieve_activity_state_returns() {
return new external_value(PARAM_RAW, 'Activity state value');
}
// proxy for https://github.com/adlnet/xAPI-Spec/blob/master/xAPI.md#74-state-api
public static function fetch_activity_states_parameters() {
return new external_function_parameters(
array(
'activityId' => new external_value(PARAM_RAW, 'Activity ID associated with state(s)'),
'agent' => new external_value(PARAM_RAW, 'Agent associated with state(s)')
)
);
}
public static function fetch_activity_states($activityId, $agent) {
global $USER;
// Parameter validation
$params = self::validate_parameters(self::fetch_activity_states_parameters(), array(
'activityId' => $activityId,
'agent' => $agent));
// Context validation
$context = context_user::instance($USER->id);
self::validate_context($context);
// Fetch states
$response = tincan_fetch_activity_states($activityId, $agent);
return $response;
}
public static function fetch_activity_states_returns() {
return new external_multiple_structure(
new external_value(PARAM_RAW, 'Activity state ID of stored state')
);
}
// proxy for https://github.com/adlnet/xAPI-Spec/blob/master/xAPI.md#74-state-api
public static function delete_activity_state_parameters() {
return new external_function_parameters(
array(
'activityId' => new external_value(PARAM_RAW, 'Activity ID associated with this state'),
'agent' => new external_value(PARAM_RAW, 'Agent associated with this state'),
'stateId' => new external_value(PARAM_RAW, 'ID for the state, within the given context')
)
);
}
public static function delete_activity_state($activityId, $agent, $stateId) {
global $USER;
// Parameter validation
$params = self::validate_parameters(self::delete_activity_state_parameters(), array(
'activityId' => $activityId,
'agent' => $agent,
'stateId' => $stateId));
// Context validation
$context = context_user::instance($USER->id);
self::validate_context($context);
// Delete state
$response = tincan_delete_activity_state($activityId, $agent, $stateId);
return $response;
}
public static function delete_activity_state_returns() {
return new external_value(PARAM_BOOL, 'Success or Failure');
}
// proxy for https://github.com/adlnet/xAPI-Spec/blob/master/xAPI.md#74-state-api
public static function clear_activity_states_parameters() {
return new external_function_parameters(
array(
'activityId' => new external_value(PARAM_RAW, 'Activity ID associated with this state'),
'agent' => new external_value(PARAM_RAW, 'Agent associated with this state')
)
);
}
public static function clear_activity_states($activityId, $agent) {
global $USER;
// Parameter validation
$params = self::validate_parameters(self::clear_activity_states_parameters(), array(
'activityId' => $activityId,
'agent' => $agent));
// Context validation
$context = context_user::instance($USER->id);
self::validate_context($context);
// Clear states
$response = tincan_clear_activity_states($activityId, $agent);
return $response;
}
public static function clear_activity_states_returns() {
return new external_value(PARAM_BOOL, 'Success or Failure');
}
}