From 11ef2933f4d1a34c0679053dc448910c2872fea6 Mon Sep 17 00:00:00 2001 From: Michael Aherne <michael.aherne@strath.ac.uk> Date: Wed, 27 Jan 2016 10:44:32 +0000 Subject: [PATCH] Added maxbatchsize setting and implementation --- classes/log/store.php | 19 +++++++++++++++++-- lang/en/logstore_xapi.php | 4 ++++ settings.php | 4 ++++ tests/TestCase.php | 6 +++--- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/classes/log/store.php b/classes/log/store.php index 52c9e2b..a2af1f6 100644 --- a/classes/log/store.php +++ b/classes/log/store.php @@ -105,8 +105,23 @@ class store extends php_obj implements log_writer { $this->error_log_value('moodleevent', $moodleevents); $translatorevents = $translatorcontroller->createEvents($moodleevents); $this->error_log_value('translatorevents', $translatorevents); - $xapievents = $xapicontroller->createEvents($translatorevents); - $this->error_log_value('xapievents', $xapievents); + + if (empty($translatorevents)) { + return; + } + + // Split statements into batches. + $eventbatches = array($translatorevents); + $maxbatchsize = get_config('logstore_xapi', 'maxbatchsize'); + + if (!empty($maxbatchsize) && $maxbatchsize < count($translatorevents)) { + $eventbatches = array_chunk($translatorevents, $maxbatchsize); + } + + foreach ($eventbatches as $translatoreventsbatch) { + $xapievents = $xapicontroller->createEvents($translatoreventsbatch); + $this->error_log_value('xapievents', $xapievents); + } } diff --git a/lang/en/logstore_xapi.php b/lang/en/logstore_xapi.php index f8d450c..8502716 100644 --- a/lang/en/logstore_xapi.php +++ b/lang/en/logstore_xapi.php @@ -39,4 +39,8 @@ $string['backgroundmode'] = 'Send statements by scheduled task?'; $string['backgroundmode_desc'] = 'This will force Moodle to send the statements to the LRS in the background, via a cron task. This will make the process less close to real time, but will help to prevent unpredictable Moodle performance linked to the performance of the LRS.'; +$string['maxbatchsize'] = 'Maximum batch size'; +$string['maxbatchsize_desc'] = 'Statements are sent to the LRS in batches. This setting controls the maximum number of + statements that will be sent in a single operation. Setting this to zero will cause all available statements to + be sent at once, although this is not recommended.'; $string['taskemit'] = 'Emit records to LRS'; \ No newline at end of file diff --git a/settings.php b/settings.php index 269603c..35e853d 100644 --- a/settings.php +++ b/settings.php @@ -41,4 +41,8 @@ if ($hassiteconfig) { $settings->add(new admin_setting_configcheckbox('logstore_xapi/backgroundmode', get_string('backgroundmode', 'logstore_xapi'), get_string('backgroundmode_desc', 'logstore_xapi'), 0)); + + $settings->add(new admin_setting_configtext('logstore_xapi/maxbatchsize', + get_string('maxbatchsize', 'logstore_xapi'), + get_string('maxbatchsize_desc', 'logstore_xapi'), 30, PARAM_INT)); } diff --git a/tests/TestCase.php b/tests/TestCase.php index 94c58c5..dca1aea 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -30,13 +30,13 @@ abstract class TestCase extends PhpUnitTestCase { $input = $this->constructInput(); $moodle_events = $this->moodle_controller->createEvents([$input]); - $this->assertNotNull($moodle_events, 'Check that the event exists in the expander controller.'); + $this->assertNotNull($moodle_events, 'Check that the events exist in the expander controller.'); $translator_events = $this->translator_controller->createEvents($moodle_events); - $this->assertNotNull($translator_events, 'Check that the event exists in the translator controller.'); + $this->assertNotNull($translator_events, 'Check that the events exist in the translator controller.'); $xapi_events = $this->xapi_controller->createEvents($translator_events); - $this->assertNotNull($xapi_events, 'Check that the event exists in the emitter controller.'); + $this->assertNotNull($xapi_events, 'Check that the events exist in the emitter controller.'); $this->assertOutput($input, $xapi_events); } -- GitLab