Newer
Older
<?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/>.
namespace src\loader\lrs;
defined('MOODLE_INTERNAL') || die();
function correct_endpoint($endpoint) {
$endswithstatements = substr($endpoint, -11) === "/statements";
if ($endswithstatements) {
return substr($endpoint, 0, -11);
}
return rtrim($endpoint, '/');
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
}
function load_transormed_events_to_lrs(array $config, array $transformedevents) {
$endpoint = $config['lrs_endpoint'];
$username = $config['lrs_username'];
$password = $config['lrs_password'];
$url = correct_endpoint($endpoint).'/statements';
$auth = base64_encode($username.':'.$password);
$statements = array_reduce($transformedevents, function ($result, $transformedevent) {
$eventstatements = $transformedevent['statements'];
return array_merge($result, $eventstatements);
}, []);
$postdata = json_encode($statements);
$request = curl_init();
curl_setopt($request, CURLOPT_URL, $url);
curl_setopt($request, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($request, CURLOPT_HTTPHEADER, [
'Authorization: Basic '.$auth,
'X-Experience-API-Version: 1.0.0',
'Content-Type: application/json',
]);
$responsetext = curl_exec($request);
$responsecode = curl_getinfo($request, CURLINFO_RESPONSE_CODE);
curl_close($request);
if ($responsecode !== 200) {
throw new \Exception($responsetext);
return [];
}
return $transformedevents;
}
function get_event_batches(array $config, array $transformedevents) {
$maxbatchsize = $config['lrs_max_batch_size'];
if (!empty($maxbatchsize) && $maxbatchsize < count($transformedevents)) {
return array_chunk($transformedevents, $maxbatchsize);
}
return [$transformedevents];
}
function load(array $config, array $transformedevents) {
$batches = get_event_batches($config, $transformedevents);
$loadedevents = array_reduce($batches, function ($result, $batch) use ($config) {
$loadedbatchevents = load_transormed_events_to_lrs($config, $batch);
return array_merge($result, $loadedbatchevents);
}, []);
return $loadedevents;
}