-
Notifications
You must be signed in to change notification settings - Fork 84
/
lib.php
409 lines (366 loc) · 12.3 KB
/
lib.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
<?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/>.
/**
* Generic library for the logstore_xapi plugin.
*
* @package logstore_xapi
* @copyright Jerret Fowler <[email protected]>
* Ryan Smith <https://www.linkedin.com/in/ryan-smith-uk/>
* David Pesce <[email protected]>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('XAPI_REPORT_ID_ERROR', 0);
define('XAPI_REPORT_ID_HISTORIC', 1);
// Type constants.
define('XAPI_IMPORT_TYPE_LIVE', 0);
define('XAPI_IMPORT_TYPE_HISTORIC', 1);
define('XAPI_IMPORT_TYPE_FAILED', 2);
// Report source.
define('XAPI_REPORT_SOURCE_LOG', 'logstore_xapi_log');
define('XAPI_REPORT_SOURCE_FAILED', 'logstore_xapi_failed_log');
define('XAPI_REPORT_SOURCE_HISTORICAL', 'logstore_standard_log');
// Error types.
define('XAPI_REPORT_ERRORTYPE_NETWORK', 101);
define('XAPI_REPORT_ERRORTYPE_RECIPE', 400);
define('XAPI_REPORT_ERRORTYPE_AUTH', 401);
define('XAPI_REPORT_ERRORTYPE_LRS', 500);
define('XAPI_REPORT_ERRORTYPE_TRANSFORM', 10000); // This high number has been set to avoid conflicting with other error codes.
// Resend values.
define('XAPI_REPORT_RESEND_FALSE', 0);
define('XAPI_REPORT_RESEND_TRUE', 1);
// Default values on url parameters.
define('XAPI_REPORT_STARTING_PAGE', 0);
define('XAPI_REPORT_PERPAGE_DEFAULT', 40);
define('XAPI_REPORT_ONPAGE_DEFAULT', '');
define('XAPI_REPORT_EVENTCONTEXT_DEFAULT', '');
define('XAPI_REPORT_EVENTNAMES_DEFAULT', array());
define('XAPI_REPORT_ERROTYPE_DEFAULT', '0');
define('XAPI_REPORT_RESPONSE_DEFAULT', '0');
define('XAPI_REPORT_USERNAME_DEFAULT', '');
define('XAPI_REPORT_DATEFROM_DEFAULT', 0);
define('XAPI_REPORT_DATETO_DEFAULT', 0);
/**
* Get all visible cohorts in the system.
*
* @return array Returns an array of all visible cohorts.
*/
function logstore_xapi_get_cohorts() {
global $DB;
$array = array("visible" => 1);
$cohorts = $DB->get_records("cohort", $array);
return $cohorts;
}
/**
* Get the selected cohorts from the settings.
*
* @return array Returns an array of selected cohort ids if the cohort is still visible.
* The cohort might have been made invisible or removed since the selection was made.
*/
function logstore_xapi_get_selected_cohorts() {
$arrvisible = logstore_xapi_get_cohorts();
$selected = get_config('logstore_xapi', 'cohorts');
$arrselected = explode(",", $selected);
$arr = array();
foreach ($arrselected as $arrselection) {
if (array_key_exists($arrselection, $arrvisible)) {
$arr[] = $arrselection;
}
}
return $arr;
}
/**
* Return all members for a cohort
*
* @param array $cohortids array of cohort ids
* @return array with cohort id keys containing arrays of user email addresses
*/
function logstore_xapi_get_cohort_members($cohortids) {
global $DB;
$members = array();
foreach ($cohortids as $cohortid) {
// Validate params.
$cohort = $DB->get_record('cohort', array('id' => $cohortid), '*', MUST_EXIST);
if (!empty($cohort)) {
$sql = "SELECT u.*
FROM {user} u, {cohort_members} cm
WHERE u.id = cm.userid AND cm.cohortid = ?
ORDER BY lastname ASC, firstname ASC";
$cohortmembers = $DB->get_records_sql($sql, array($cohort->id));
$members = array_merge($members, $cohortmembers);
}
}
return $members;
}
/**
* Get the selected cohorts from the settings.
*
* @return array Returns an array of user objects from cohorts and additional email addresses.
*/
function logstore_xapi_get_users_for_notifications() {
// Get selected cohort users, will return a blank array if no cohorts are set.
$cohorts = logstore_xapi_get_selected_cohorts();
$users = logstore_xapi_get_cohort_members($cohorts);
// Get the manually set email addresses from the config.
$emailaddresses = get_config('logstore_xapi', 'send_additional_email_addresses');
$emailaddresses = explode(",", $emailaddresses);
foreach ($emailaddresses as $email) {
// Remove whitespace from email addresses.
$email = preg_replace('/\s+/', '', $email);
if (validate_email($email)) {
// If the email address is valid then add it to the list of users.
$user = new stdClass();
$user->email = $email;
$users[] = $user;
}
}
return $users;
}
/**
* Gets the unique column values
*
* @param string $column
* @return array
* @throws dml_exception
*/
function logstore_xapi_get_distinct_options_from_failed_table($column) {
global $DB;
$options = [0 => get_string('any')];
$results = $DB->get_fieldset_select('logstore_xapi_failed_log', "DISTINCT $column", '');
if ($results) {
foreach ($results as $result) {
$options[$result] = $result;
}
}
return $options;
}
/**
* Get the available context's from the logstore standard log table
*
* @return array
* @throws dml_exception
*/
function logstore_xapi_get_logstore_standard_context_options() {
global $DB;
$options = [0 => get_string('any')];
$sql = 'SELECT DISTINCT(contextid)
FROM {logstore_standard_log} lssl
WHERE EXISTS (SELECT 1
FROM {context} c
WHERE c.id = lssl.contextid)';
$contextids = array_keys($DB->get_records_sql($sql));
foreach ($contextids as $contextid) {
$context = context::instance_by_id($contextid);
$options[$context->id] = $context->get_context_name();
}
asort($options);
return $options;
}
/**
* Retrieves the available and enabled events for this plugin and outputs it into an array
*
* @return array
*/
function logstore_xapi_get_event_names_array() {
if (!function_exists('\src\transformer\get_event_function_map')) {
global $CFG;
require_once($CFG->dirroot . '/admin/tool/log/store/xapi/src/transformer/get_event_function_map.php');
}
$eventnames = [];
$eventfunctionmap = \src\transformer\get_event_function_map();
foreach (array_keys($eventfunctionmap) as $eventname) {
$eventnames[$eventname] = $eventname;
}
return $eventnames;
}
/**
* Decode the json array stored in the response column. Will return false if json is invalid
*
* @param string $response JSON string response.
* @return array|bool
*/
function logstore_xapi_decode_response($response) {
$decode = json_decode($response, true);
// Check JSON is valid.
if (json_last_error() === JSON_ERROR_NONE) {
return $decode;
}
return false;
}
/**
* Generate the string for the info column in the report
*
* @param object $row
* @return string
* @throws coding_exception
*/
function logstore_xapi_get_info_string($row) {
if (!empty($row->errortype)) {
switch ($row->errortype) {
case XAPI_REPORT_ERRORTYPE_NETWORK:
return get_string('networkerror', 'logstore_xapi');
case XAPI_REPORT_ERRORTYPE_RECIPE:
// Recipe issue.
return get_string('recipeerror', 'logstore_xapi');
case XAPI_REPORT_ERRORTYPE_AUTH:
// Unauthorised, could be an issue with xAPI credentials.
return get_string('autherror', 'logstore_xapi');
case XAPI_REPORT_ERRORTYPE_LRS:
// The xAPI server error.
return get_string('lrserror', 'logstore_xapi');
case XAPI_REPORT_ERRORTYPE_TRANSFORM:
// Transform error.
return get_string('failedtransformresponse', 'logstore_xapi', $row->eventname);
default:
// Generic error catch all.
return get_string('unknownerror', 'logstore_xapi', $row->errortype);
break;
}
}
return ''; // Return blank if no errortype captured.
}
/**
* Get successful events.
*
* @param array $events An array of events.
* @return array
*/
function logstore_xapi_get_successful_events($events) {
$loadedevents = array_filter($events, function($loadedevent) {
return $loadedevent['loaded'] === true;
});
$successfulevents = array_map(function($loadedevent) {
return $loadedevent['event'];
}, $loadedevents);
return $successfulevents;
}
/**
* Take event data and add to the sent log if it doesn't exist already.
*
* @param stdObj $event raw event data
*/
function logstore_xapi_add_event_to_sent_log($event) {
global $DB;
$row = $DB->get_record('logstore_xapi_sent_log', ['logstorestandardlogid' => $event->logstorestandardlogid]);
if (empty($row)) {
$newrow = new stdClass();
$newrow->logstorestandardlogid = $event->logstorestandardlogid;
$newrow->type = $event->type;
$newrow->timecreated = time();
$DB->insert_record('logstore_xapi_sent_log', $newrow);
}
}
/**
* Extract events from logstore_xapi_log or logstore_xapi_failed_log.
*
* @param int $limitnum limit number
* @param int $log log source
* @param int $type event type
* @return array
*/
function logstore_xapi_extract_events($limitnum, $log, $type) {
global $DB;
$conditions = array("type" => $type);
$sort = '';
$fields = '*';
$limitfrom = 0;
$events = $DB->get_records($log, $conditions, $sort, $fields, $limitfrom, $limitnum);
return $events;
}
/**
* Get event ids.
*
* @param array $loadedevents raw events data
* @return array
*/
function logstore_xapi_get_event_ids($loadedevents) {
return array_map(function ($loadedevent) {
return $loadedevent['event']->id;
}, $loadedevents);
}
/**
* Delete processed events.
*
* @param array $events raw events data
*/
function logstore_xapi_delete_processed_events($events) {
global $DB;
$eventids = logstore_xapi_get_event_ids($events);
$DB->delete_records_list('logstore_xapi_log', 'id', $eventids);
}
/**
* Log the number of events using mtrace.
*
* @param array $events raw events data
*/
function logstore_xapi_record_successful_events($events) {
mtrace(count(logstore_xapi_get_successful_events($events)) . " " . get_string('successful_events', 'logstore_xapi'));
}
/**
* Take successful events and save each using logstore_xapi_add_event_to_sent_log.
*
* @param array $events raw events data
*/
function logstore_xapi_save_sent_events(array $events) {
$successfulevents = logstore_xapi_get_successful_events($events);
foreach ($successfulevents as $event) {
logstore_xapi_add_event_to_sent_log($event);
}
}
/**
* Get failed events as array.
*
* @param array $events An array of events.
* @return array
*/
function logstore_xapi_get_failed_events($events) {
$nonloadedevents = array_filter($events, function ($loadedevent) {
return $loadedevent['loaded'] === false;
});
$failedevents = array_map(function ($nonloadedevent) {
return $nonloadedevent['event'];
}, $nonloadedevents);
return $failedevents;
}
/**
* Store failed events in logstore_xapi_failed_log.
*
* @param array $events An array of events.
* @return void
*/
function logstore_xapi_store_failed_events($events) {
global $DB;
$failedevents = logstore_xapi_get_failed_events($events);
$DB->insert_records('logstore_xapi_failed_log', $failedevents);
mtrace(count($failedevents) . " " . get_string('failed_events', 'logstore_xapi'));
}
/**
* determine the type from the initial base table
*
* @param string $table
* @return int
*/
function logstore_xapi_get_type_from_table($table) {
switch ($table) {
case XAPI_REPORT_SOURCE_LOG:
return XAPI_IMPORT_TYPE_LIVE;
case XAPI_REPORT_SOURCE_FAILED:
return XAPI_IMPORT_TYPE_FAILED;
case XAPI_REPORT_SOURCE_HISTORICAL:
return XAPI_IMPORT_TYPE_HISTORIC;
default:
return XAPI_IMPORT_TYPE_LIVE;
}
}