-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
9 changed files
with
236 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ charset = utf-8 | |
|
||
[*.{php,json}] | ||
indent_size = 4 | ||
trim_trailing_whitespace=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?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\moodle_curl_lrs; | ||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
global $CFG; | ||
if (!isset($CFG)) { | ||
$CFG = (object) [ 'libdir' => 'utils' ]; | ||
} | ||
require_once($CFG->libdir . '/filelib.php'); | ||
|
||
use src\loader\utils as utils; | ||
|
||
function load(array $config, array $events) { | ||
$sendhttpstatements = function (array $config, array $statements) { | ||
$endpoint = $config['lrs_endpoint']; | ||
$username = $config['lrs_username']; | ||
$password = $config['lrs_password']; | ||
$proxyendpoint = $config['lrs_proxy_endpoint']; | ||
|
||
$url = utils\correct_endpoint($endpoint).'/statements'; | ||
$auth = base64_encode($username.':'.$password); | ||
$postdata = json_encode($statements); | ||
|
||
$request = new \curl(); | ||
$responsetext = $request->post($url, $postdata, [ | ||
'CURLOPT_HTTPHEADER' => [ | ||
'Authorization: Basic '.$auth, | ||
'X-Experience-API-Version: 1.0.0', | ||
'Content-Type: application/json', | ||
], | ||
]); | ||
$responsecode = $request->info['http_code']; | ||
|
||
if ($responsecode !== 200) { | ||
throw new \Exception($responsetext); | ||
} | ||
}; | ||
return utils\load_in_batches($config, $events, $sendhttpstatements); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?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\utils; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
function correct_endpoint($endpoint) { | ||
$endswithstatements = substr($endpoint, -11) === "/statements"; | ||
if ($endswithstatements) { | ||
return substr($endpoint, 0, -11); | ||
} | ||
return rtrim($endpoint, '/'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?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\utils; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
class curl { | ||
// This is just a dummy file to avoid failures in CI. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?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\utils; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
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]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?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\utils; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
function load_batch(array $config, array $transformedevents, callable $loader) { | ||
try { | ||
$statements = array_reduce($transformedevents, function ($result, $transformedevent) { | ||
$eventstatements = $transformedevent['statements']; | ||
return array_merge($result, $eventstatements); | ||
}, []); | ||
$loader($config, $statements); | ||
$loadedevents = construct_loaded_events($transformedevents, true); | ||
return $loadedevents; | ||
} catch (\Exception $e) { | ||
$logerror = $config['log_error']; | ||
$logerror("Failed load for event id #" . $eventobj->id . ": " . $e->getMessage()); | ||
$logerror($e->getTraceAsString()); | ||
$loadedevents = construct_loaded_events($transformedevents, false); | ||
return $loadedevents; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?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\utils; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
function load_in_batches(array $config, array $events, callable $loader) { | ||
// Attempts to load events that were transformed successfully in batches. | ||
$successfultransformevents = filter_transformed_events($events, true); | ||
$batches = get_event_batches($config, $successfultransformevents); | ||
$loadedevents = array_reduce($batches, function ($result, $batch) use ($config, $loader) { | ||
$loadedbatchevents = load_batch($config, $batch, $loader); | ||
return array_merge($result, $loadedbatchevents); | ||
}, []); | ||
|
||
// Flags events that weren't transformed successfully as events that didn't load. | ||
$failedtransformevents = filter_transformed_events($events, false); | ||
$nonloadedevents = construct_loaded_events($failedtransformevents, false); | ||
|
||
// Returns loaded and non-loaded events to avoid re-processing. | ||
return array_merge($loadedevents, $nonloadedevents); | ||
} |