From be91e3eea8091f4a4c40ebe56799d052eebbbfc4 Mon Sep 17 00:00:00 2001 From: Ryan Smith <0ryansmith1994@gmail.com> Date: Fri, 31 Aug 2018 12:15:22 +0100 Subject: [PATCH 1/9] fix: Fixes LRS usage with proxy. --- classes/log/store.php | 9 +++++++++ src/loader/lrs.php | 4 ++++ tests/xapi_test_case.php | 1 + 3 files changed, 14 insertions(+) diff --git a/classes/log/store.php b/classes/log/store.php index 48eb0c7eb..b808bb00c 100755 --- a/classes/log/store.php +++ b/classes/log/store.php @@ -81,6 +81,14 @@ public function get_max_batch_size() { return $this->get_config('maxbatchsize', 100); } + private function get_proxy_endpoint() { + global $CFG; + if (!empty($CFG->proxyhost)) { + return $CFG->proxyhost.':'.$CFG->proxyport; + } + return null; + } + public function process_events(array $events) { global $DB; global $CFG; @@ -114,6 +122,7 @@ public function process_events(array $events) { 'lrs_username' => $this->get_config('username', ''), 'lrs_password' => $this->get_config('password', ''), 'lrs_max_batch_size' => $this->get_max_batch_size(), + 'lrs_proxy_endpoint' => $this->get_proxy_endpoint(), ], ]; $loadedevents = \src\handler($handlerconfig, $events); diff --git a/src/loader/lrs.php b/src/loader/lrs.php index af36eafc7..c17d0fff8 100644 --- a/src/loader/lrs.php +++ b/src/loader/lrs.php @@ -31,6 +31,7 @@ function send_http_statements(array $config, array $statements) { $endpoint = $config['lrs_endpoint']; $username = $config['lrs_username']; $password = $config['lrs_password']; + $proxyendpoint = $config['lrs_proxy_endpoint']; $url = correct_endpoint($endpoint).'/statements'; $auth = base64_encode($username.':'.$password); @@ -41,6 +42,9 @@ function send_http_statements(array $config, array $statements) { curl_setopt($request, CURLOPT_POSTFIELDS, $postdata); curl_setopt($request, CURLOPT_RETURNTRANSFER, true); curl_setopt($request, CURLOPT_SSL_VERIFYPEER, false); + if (isset($proxyendpoint)) { + curl_setopt($request, CURLOPT_PROXY, $proxyendpoint); + } curl_setopt($request, CURLOPT_HTTPHEADER, [ 'Authorization: Basic '.$auth, 'X-Experience-API-Version: 1.0.0', diff --git a/tests/xapi_test_case.php b/tests/xapi_test_case.php index 1fd4adc16..f90512985 100644 --- a/tests/xapi_test_case.php +++ b/tests/xapi_test_case.php @@ -54,6 +54,7 @@ public function test_create_event() { 'lrs_username' => '', 'lrs_password' => '', 'lrs_max_batch_size' => 1, + 'lrs_proxy_endpoint' => null, ], ]; $loadedevents = \src\handler($handlerconfig, [$event]); From f334b58cabedafd7b433e8bbd4ed9162aacdcbaa Mon Sep 17 00:00:00 2001 From: Ryan Smith <0ryansmith1994@gmail.com> Date: Fri, 31 Aug 2018 15:54:13 +0100 Subject: [PATCH 2/9] fix: Handles case where proxyport is not set. --- classes/log/store.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/classes/log/store.php b/classes/log/store.php index b808bb00c..40436ead2 100755 --- a/classes/log/store.php +++ b/classes/log/store.php @@ -84,7 +84,11 @@ public function get_max_batch_size() { private function get_proxy_endpoint() { global $CFG; if (!empty($CFG->proxyhost)) { - return $CFG->proxyhost.':'.$CFG->proxyport; + if (!empty($CFG->proxyport)) { + return $CFG->proxyhost.':'.$CFG->proxyport; + } else { + return $CFG->proxyhost; + } } return null; } From 44cfd24a0cb78f530aeadbb7fde0fabc11d09b02 Mon Sep 17 00:00:00 2001 From: Ryan Smith <0ryansmith1994@gmail.com> Date: Fri, 31 Aug 2018 16:23:19 +0100 Subject: [PATCH 3/9] Temporary commit. --- src/loader/lrs.php | 12 +--- src/loader/moodle_curl_lrs.php | 80 ++++++++++++++++++++++++++ src/loader/utils/correct_endpoint.php | 27 +++++++++ src/loader/utils/get_event_batches.php | 27 +++++++++ 4 files changed, 136 insertions(+), 10 deletions(-) create mode 100644 src/loader/moodle_curl_lrs.php create mode 100644 src/loader/utils/correct_endpoint.php create mode 100644 src/loader/utils/get_event_batches.php diff --git a/src/loader/lrs.php b/src/loader/lrs.php index c17d0fff8..db16cfe33 100644 --- a/src/loader/lrs.php +++ b/src/loader/lrs.php @@ -19,21 +19,13 @@ use src\loader\utils as utils; -function correct_endpoint($endpoint) { - $endswithstatements = substr($endpoint, -11) === "/statements"; - if ($endswithstatements) { - return substr($endpoint, 0, -11); - } - return rtrim($endpoint, '/'); -} - function send_http_statements(array $config, array $statements) { $endpoint = $config['lrs_endpoint']; $username = $config['lrs_username']; $password = $config['lrs_password']; $proxyendpoint = $config['lrs_proxy_endpoint']; - $url = correct_endpoint($endpoint).'/statements'; + $url = utils\correct_endpoint($endpoint).'/statements'; $auth = base64_encode($username.':'.$password); $postdata = json_encode($statements); @@ -89,7 +81,7 @@ function get_event_batches(array $config, array $transformedevents) { function load(array $config, array $events) { // Attempts to load events that were transformed successfully in batches. $successfultransformevents = utils\filter_transformed_events($events, true); - $batches = get_event_batches($config, $successfultransformevents); + $batches = utils\get_event_batches($config, $successfultransformevents); $loadedevents = array_reduce($batches, function ($result, $batch) use ($config) { $loadedbatchevents = load_transormed_events_to_lrs($config, $batch); return array_merge($result, $loadedbatchevents); diff --git a/src/loader/moodle_curl_lrs.php b/src/loader/moodle_curl_lrs.php new file mode 100644 index 000000000..d3f672c28 --- /dev/null +++ b/src/loader/moodle_curl_lrs.php @@ -0,0 +1,80 @@ +. + +namespace src\loader\moodle_curl_lrs; +defined('MOODLE_INTERNAL') || die(); + +use src\loader\utils as utils; + +function send_http_statements(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); + } +} + +function load_transormed_events_to_lrs(array $config, array $transformedevents) { + try { + $statements = array_reduce($transformedevents, function ($result, $transformedevent) { + $eventstatements = $transformedevent['statements']; + return array_merge($result, $eventstatements); + }, []); + send_http_statements($config, $statements); + $loadedevents = utils\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 = utils\construct_loaded_events($transformedevents, false); + return $loadedevents; + } +} + +function load(array $config, array $events) { + // Attempts to load events that were transformed successfully in batches. + $successfultransformevents = utils\filter_transformed_events($events, true); + $batches = utils\get_event_batches($config, $successfultransformevents); + $loadedevents = array_reduce($batches, function ($result, $batch) use ($config) { + $loadedbatchevents = load_transormed_events_to_lrs($config, $batch); + return array_merge($result, $loadedbatchevents); + }, []); + + // Flags events that weren't transformed successfully as events that didn't load. + $failedtransformevents = utils\filter_transformed_events($events, false); + $nonloadedevents = utils\construct_loaded_events($failedtransformevents, false); + + // Returns loaded and non-loaded events to avoid re-processing. + return array_merge($loadedevents, $nonloadedevents); +} \ No newline at end of file diff --git a/src/loader/utils/correct_endpoint.php b/src/loader/utils/correct_endpoint.php new file mode 100644 index 000000000..d88fb5277 --- /dev/null +++ b/src/loader/utils/correct_endpoint.php @@ -0,0 +1,27 @@ +. + +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, '/'); +} \ No newline at end of file diff --git a/src/loader/utils/get_event_batches.php b/src/loader/utils/get_event_batches.php new file mode 100644 index 000000000..b8643fb87 --- /dev/null +++ b/src/loader/utils/get_event_batches.php @@ -0,0 +1,27 @@ +. + +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]; +} \ No newline at end of file From 6d9ebac9fe21cf2261115df9cef478971b38a899 Mon Sep 17 00:00:00 2001 From: Ryan Smith <0ryansmith1994@gmail.com> Date: Fri, 31 Aug 2018 16:45:52 +0100 Subject: [PATCH 4/9] Uses filelib. --- classes/log/store.php | 2 +- src/loader/moodle_curl_lrs.php | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/classes/log/store.php b/classes/log/store.php index 40436ead2..240288cf5 100755 --- a/classes/log/store.php +++ b/classes/log/store.php @@ -121,7 +121,7 @@ public function process_events(array $events) { 'app_url' => $CFG->wwwroot, ], 'loader' => [ - 'loader' => 'lrs', + 'loader' => 'moodle_curl_lrs', 'lrs_endpoint' => $this->get_config('endpoint', ''), 'lrs_username' => $this->get_config('username', ''), 'lrs_password' => $this->get_config('password', ''), diff --git a/src/loader/moodle_curl_lrs.php b/src/loader/moodle_curl_lrs.php index d3f672c28..23c63193f 100644 --- a/src/loader/moodle_curl_lrs.php +++ b/src/loader/moodle_curl_lrs.php @@ -17,6 +17,9 @@ namespace src\loader\moodle_curl_lrs; defined('MOODLE_INTERNAL') || die(); +global $CFG; +require_once($CFG->libdir . '/filelib.php'); + use src\loader\utils as utils; function send_http_statements(array $config, array $statements) { @@ -29,7 +32,7 @@ function send_http_statements(array $config, array $statements) { $auth = base64_encode($username.':'.$password); $postdata = json_encode($statements); - $request = new curl(); + $request = new \curl(); $responsetext = $request->post($url, $postdata, [ 'CURLOPT_HTTPHEADER' => [ 'Authorization: Basic '.$auth, From 6ce95486e51606a5e47947ec22e727db07eb9040 Mon Sep 17 00:00:00 2001 From: Ryan Smith <0ryansmith1994@gmail.com> Date: Mon, 3 Sep 2018 13:03:18 +0100 Subject: [PATCH 5/9] refactor: Removes older fix. --- classes/log/store.php | 13 ------------- tests/xapi_test_case.php | 1 - 2 files changed, 14 deletions(-) diff --git a/classes/log/store.php b/classes/log/store.php index 240288cf5..0e301c1aa 100755 --- a/classes/log/store.php +++ b/classes/log/store.php @@ -81,18 +81,6 @@ public function get_max_batch_size() { return $this->get_config('maxbatchsize', 100); } - private function get_proxy_endpoint() { - global $CFG; - if (!empty($CFG->proxyhost)) { - if (!empty($CFG->proxyport)) { - return $CFG->proxyhost.':'.$CFG->proxyport; - } else { - return $CFG->proxyhost; - } - } - return null; - } - public function process_events(array $events) { global $DB; global $CFG; @@ -126,7 +114,6 @@ public function process_events(array $events) { 'lrs_username' => $this->get_config('username', ''), 'lrs_password' => $this->get_config('password', ''), 'lrs_max_batch_size' => $this->get_max_batch_size(), - 'lrs_proxy_endpoint' => $this->get_proxy_endpoint(), ], ]; $loadedevents = \src\handler($handlerconfig, $events); diff --git a/tests/xapi_test_case.php b/tests/xapi_test_case.php index f90512985..1fd4adc16 100644 --- a/tests/xapi_test_case.php +++ b/tests/xapi_test_case.php @@ -54,7 +54,6 @@ public function test_create_event() { 'lrs_username' => '', 'lrs_password' => '', 'lrs_max_batch_size' => 1, - 'lrs_proxy_endpoint' => null, ], ]; $loadedevents = \src\handler($handlerconfig, [$event]); From 66df44e67b3a88cec0f4f1aeede5ce79f2d292f5 Mon Sep 17 00:00:00 2001 From: Ryan Smith <0ryansmith1994@gmail.com> Date: Mon, 3 Sep 2018 13:29:48 +0100 Subject: [PATCH 6/9] refactor: Splits lrs loaders into utils. --- src/loader/lrs.php | 106 +++++++++------------------ src/loader/moodle_curl_lrs.php | 85 +++++++-------------- src/loader/utils/filelib.php | 23 ++++++ src/loader/utils/load_batch.php | 37 ++++++++++ src/loader/utils/load_in_batches.php | 36 +++++++++ 5 files changed, 157 insertions(+), 130 deletions(-) create mode 100644 src/loader/utils/filelib.php create mode 100644 src/loader/utils/load_batch.php create mode 100644 src/loader/utils/load_in_batches.php diff --git a/src/loader/lrs.php b/src/loader/lrs.php index db16cfe33..d3541578d 100644 --- a/src/loader/lrs.php +++ b/src/loader/lrs.php @@ -19,78 +19,38 @@ use src\loader\utils as utils; -function send_http_statements(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 = 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); - if (isset($proxyendpoint)) { - curl_setopt($request, CURLOPT_PROXY, $proxyendpoint); - } - 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); - } -} - -function load_transormed_events_to_lrs(array $config, array $transformedevents) { - try { - $statements = array_reduce($transformedevents, function ($result, $transformedevent) { - $eventstatements = $transformedevent['statements']; - return array_merge($result, $eventstatements); - }, []); - send_http_statements($config, $statements); - $loadedevents = utils\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 = utils\construct_loaded_events($transformedevents, false); - return $loadedevents; - } -} - -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 $events) { - // Attempts to load events that were transformed successfully in batches. - $successfultransformevents = utils\filter_transformed_events($events, true); - $batches = utils\get_event_batches($config, $successfultransformevents); - $loadedevents = array_reduce($batches, function ($result, $batch) use ($config) { - $loadedbatchevents = load_transormed_events_to_lrs($config, $batch); - return array_merge($result, $loadedbatchevents); - }, []); - - // Flags events that weren't transformed successfully as events that didn't load. - $failedtransformevents = utils\filter_transformed_events($events, false); - $nonloadedevents = utils\construct_loaded_events($failedtransformevents, false); - - // Returns loaded and non-loaded events to avoid re-processing. - return array_merge($loadedevents, $nonloadedevents); + $send_http_statements = 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 = 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); + if (isset($proxyendpoint)) { + curl_setopt($request, CURLOPT_PROXY, $proxyendpoint); + } + 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 utils\load_in_batches($config, $events, $send_http_statements); } \ No newline at end of file diff --git a/src/loader/moodle_curl_lrs.php b/src/loader/moodle_curl_lrs.php index 23c63193f..bf328ad09 100644 --- a/src/loader/moodle_curl_lrs.php +++ b/src/loader/moodle_curl_lrs.php @@ -18,66 +18,37 @@ 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 send_http_statements(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); - } -} - -function load_transormed_events_to_lrs(array $config, array $transformedevents) { - try { - $statements = array_reduce($transformedevents, function ($result, $transformedevent) { - $eventstatements = $transformedevent['statements']; - return array_merge($result, $eventstatements); - }, []); - send_http_statements($config, $statements); - $loadedevents = utils\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 = utils\construct_loaded_events($transformedevents, false); - return $loadedevents; - } -} - function load(array $config, array $events) { - // Attempts to load events that were transformed successfully in batches. - $successfultransformevents = utils\filter_transformed_events($events, true); - $batches = utils\get_event_batches($config, $successfultransformevents); - $loadedevents = array_reduce($batches, function ($result, $batch) use ($config) { - $loadedbatchevents = load_transormed_events_to_lrs($config, $batch); - return array_merge($result, $loadedbatchevents); - }, []); - - // Flags events that weren't transformed successfully as events that didn't load. - $failedtransformevents = utils\filter_transformed_events($events, false); - $nonloadedevents = utils\construct_loaded_events($failedtransformevents, false); - - // Returns loaded and non-loaded events to avoid re-processing. - return array_merge($loadedevents, $nonloadedevents); + $send_http_statements = 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, $send_http_statements); } \ No newline at end of file diff --git a/src/loader/utils/filelib.php b/src/loader/utils/filelib.php new file mode 100644 index 000000000..1a679cb51 --- /dev/null +++ b/src/loader/utils/filelib.php @@ -0,0 +1,23 @@ +. + +namespace src\loader\utils; + +defined('MOODLE_INTERNAL') || die(); + +class curl { + // This is just a dummy file to avoid failures in CI. +} diff --git a/src/loader/utils/load_batch.php b/src/loader/utils/load_batch.php new file mode 100644 index 000000000..3ef49289d --- /dev/null +++ b/src/loader/utils/load_batch.php @@ -0,0 +1,37 @@ +. + +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; + } +} diff --git a/src/loader/utils/load_in_batches.php b/src/loader/utils/load_in_batches.php new file mode 100644 index 000000000..b2ebb4c5d --- /dev/null +++ b/src/loader/utils/load_in_batches.php @@ -0,0 +1,36 @@ +. + +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); +} From 020139dff9d1346a3d7086ce7e9618a19741e0c8 Mon Sep 17 00:00:00 2001 From: Ryan Smith <0ryansmith1994@gmail.com> Date: Mon, 3 Sep 2018 13:50:31 +0100 Subject: [PATCH 7/9] refactor: Removes trailing whitespace to fix linting. --- .editorconfig | 1 + src/loader/lrs.php | 6 +++--- src/loader/moodle_curl_lrs.php | 6 +++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/.editorconfig b/.editorconfig index 1cb779bf7..bb87ae7b5 100644 --- a/.editorconfig +++ b/.editorconfig @@ -13,3 +13,4 @@ charset = utf-8 [*.{php,json}] indent_size = 4 +trim_trailing_whitespace=true diff --git a/src/loader/lrs.php b/src/loader/lrs.php index d3541578d..aef91dbcf 100644 --- a/src/loader/lrs.php +++ b/src/loader/lrs.php @@ -25,11 +25,11 @@ function load(array $config, array $events) { $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 = curl_init(); curl_setopt($request, CURLOPT_URL, $url); curl_setopt($request, CURLOPT_POSTFIELDS, $postdata); @@ -43,7 +43,7 @@ function load(array $config, array $events) { '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); diff --git a/src/loader/moodle_curl_lrs.php b/src/loader/moodle_curl_lrs.php index bf328ad09..6c84889e9 100644 --- a/src/loader/moodle_curl_lrs.php +++ b/src/loader/moodle_curl_lrs.php @@ -31,11 +31,11 @@ function load(array $config, array $events) { $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' => [ @@ -45,7 +45,7 @@ function load(array $config, array $events) { ], ]); $responsecode = $request->info['http_code']; - + if ($responsecode !== 200) { throw new \Exception($responsetext); } From 8551b3c2ead88f619080a6d1c811e1611089a62b Mon Sep 17 00:00:00 2001 From: Ryan Smith <0ryansmith1994@gmail.com> Date: Mon, 3 Sep 2018 13:54:24 +0100 Subject: [PATCH 8/9] ci(lint): Fixes linting errors. --- src/loader/lrs.php | 4 ++-- src/loader/moodle_curl_lrs.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/loader/lrs.php b/src/loader/lrs.php index aef91dbcf..010e91fe5 100644 --- a/src/loader/lrs.php +++ b/src/loader/lrs.php @@ -20,7 +20,7 @@ use src\loader\utils as utils; function load(array $config, array $events) { - $send_http_statements = function (array $config, array $statements) { + $sendhttpstatements = function (array $config, array $statements) { $endpoint = $config['lrs_endpoint']; $username = $config['lrs_username']; $password = $config['lrs_password']; @@ -52,5 +52,5 @@ function load(array $config, array $events) { throw new \Exception($responsetext); } }; - return utils\load_in_batches($config, $events, $send_http_statements); + return utils\load_in_batches($config, $events, $sendhttpstatements); } \ No newline at end of file diff --git a/src/loader/moodle_curl_lrs.php b/src/loader/moodle_curl_lrs.php index 6c84889e9..f78cfc068 100644 --- a/src/loader/moodle_curl_lrs.php +++ b/src/loader/moodle_curl_lrs.php @@ -26,7 +26,7 @@ use src\loader\utils as utils; function load(array $config, array $events) { - $send_http_statements = function (array $config, array $statements) { + $sendhttpstatements = function (array $config, array $statements) { $endpoint = $config['lrs_endpoint']; $username = $config['lrs_username']; $password = $config['lrs_password']; @@ -50,5 +50,5 @@ function load(array $config, array $events) { throw new \Exception($responsetext); } }; - return utils\load_in_batches($config, $events, $send_http_statements); + return utils\load_in_batches($config, $events, $sendhttpstatements); } \ No newline at end of file From 7d6cb1ee983c65a0ec84ef4eda732f2e04c4b60c Mon Sep 17 00:00:00 2001 From: Ryan Smith <0ryansmith1994@gmail.com> Date: Mon, 3 Sep 2018 13:56:53 +0100 Subject: [PATCH 9/9] refactor: Removes proxy endpoint option from lrs loader. --- src/loader/lrs.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/loader/lrs.php b/src/loader/lrs.php index 010e91fe5..781be00b5 100644 --- a/src/loader/lrs.php +++ b/src/loader/lrs.php @@ -35,9 +35,6 @@ function load(array $config, array $events) { curl_setopt($request, CURLOPT_POSTFIELDS, $postdata); curl_setopt($request, CURLOPT_RETURNTRANSFER, true); curl_setopt($request, CURLOPT_SSL_VERIFYPEER, false); - if (isset($proxyendpoint)) { - curl_setopt($request, CURLOPT_PROXY, $proxyendpoint); - } curl_setopt($request, CURLOPT_HTTPHEADER, [ 'Authorization: Basic '.$auth, 'X-Experience-API-Version: 1.0.0',