From 565a94749adac79cbaad024df350b93eb71e98dc Mon Sep 17 00:00:00 2001 From: Nathan Nguyen Date: Mon, 5 Jun 2023 17:00:05 +1000 Subject: [PATCH] Issue#203 Insert tool_trigger_events id into event other data --- classes/helper/datafield_manager.php | 7 ++++++- classes/helper/processor_helper.php | 6 ++++++ tests/datafield_manager_test.php | 6 ++++++ tests/fixtures/user_event_fixture.php | 3 ++- tests/processor_helper_test.php | 3 +++ 5 files changed, 23 insertions(+), 2 deletions(-) diff --git a/classes/helper/datafield_manager.php b/classes/helper/datafield_manager.php index 5a6861b..71fa316 100644 --- a/classes/helper/datafield_manager.php +++ b/classes/helper/datafield_manager.php @@ -99,7 +99,12 @@ public function update_datafields($event, $stepresults) { if (isset($newfields['other']) && is_array($newfields['other'])) { foreach ($newfields['other'] as $key => $value) { if (is_scalar($value) || is_null($value)) { - $newfields["other_{$key}"] = $value; + // Retrieve ID from eventid in other data. + if ($key == 'eventid') { + $newfields['id'] = $value; + } else { + $newfields["other_{$key}"] = $value; + } } } unset($newfields['other']); diff --git a/classes/helper/processor_helper.php b/classes/helper/processor_helper.php index ca404ca..6b4cf0f 100644 --- a/classes/helper/processor_helper.php +++ b/classes/helper/processor_helper.php @@ -43,6 +43,12 @@ public function restore_event(\stdClass $data) { if ($data['other'] === false) { $data['other'] = array(); } + + // Insert eventid into other data. + if (isset($data['id'])) { + $data['other']['eventid'] = $data['id']; + } + unset($data['origin']); unset($data['ip']); unset($data['realuserid']); diff --git a/tests/datafield_manager_test.php b/tests/datafield_manager_test.php index dd4bd48..e04826c 100644 --- a/tests/datafield_manager_test.php +++ b/tests/datafield_manager_test.php @@ -51,6 +51,12 @@ public function test_get_datafields() { $dfprovider->update_datafields($this->event, $stepdata); $datafields = $dfprovider->get_datafields(); + // Check event id. + $this->assertEquals( + 1, + $datafields['id'] + ); + // A field from the event object. $this->assertEquals( $this->user2->id, diff --git a/tests/fixtures/user_event_fixture.php b/tests/fixtures/user_event_fixture.php index ea6d867..7829df8 100644 --- a/tests/fixtures/user_event_fixture.php +++ b/tests/fixtures/user_event_fixture.php @@ -62,7 +62,8 @@ public function setup_user_event() { 'other' => [ 'courseid' => $this->course->id, 'courseshortname' => $this->course->shortname, - 'coursefullname' => $this->course->fullname + 'coursefullname' => $this->course->fullname, + 'eventid' => 1, ] ]); diff --git a/tests/processor_helper_test.php b/tests/processor_helper_test.php index 3504926..d3551bf 100644 --- a/tests/processor_helper_test.php +++ b/tests/processor_helper_test.php @@ -53,6 +53,7 @@ public function setup():void { */ public function test_restore_event() { $data = (object) [ + 'id' => 1, 'eventname' => '\\core\\event\\user_loggedin', 'component' => 'core', 'action' => 'loggedin', @@ -98,6 +99,8 @@ public function test_restore_event() { $this->assertEquals($expectedevent->userid, $actual->userid); $this->assertEquals($expectedevent->objectid, $actual->objectid); $this->assertEquals($expectedevent->get_username(), $actual->get_username()); + // Tool trigger event id + $this->assertEquals(1, $actual->other['eventid']); } /**