From b5e699f543da6306f09207136b9a0a36f28c9164 Mon Sep 17 00:00:00 2001 From: Brendan Halley <4285050+BrendanHalley@users.noreply.github.com> Date: Tue, 18 Jun 2019 21:00:51 +1000 Subject: [PATCH] feat(Totara): Adds `\totara_program\event\program_assigned` event. (#583 - Thanks @BrendanHalley) --- classes/log/store.php | 21 ++++++- .../totara_program/program_assigned.php | 54 ++++++++++++++++ src/transformer/get_event_function_map.php | 9 ++- src/transformer/utils/extensions/base.php | 2 +- src/transformer/utils/totara/program.php | 34 ++++++++++ .../existing_program/data.json | 22 +++++++ .../existing_program/event.json | 8 +++ .../existing_program/statements.json | 63 +++++++++++++++++++ .../existing_program/test.php | 24 +++++++ 9 files changed, 232 insertions(+), 5 deletions(-) create mode 100644 src/transformer/events/totara_program/program_assigned.php create mode 100644 src/transformer/utils/totara/program.php create mode 100644 tests/totara_program/program_assigned/existing_program/data.json create mode 100644 tests/totara_program/program_assigned/existing_program/event.json create mode 100644 tests/totara_program/program_assigned/existing_program/statements.json create mode 100644 tests/totara_program/program_assigned/existing_program/test.php diff --git a/classes/log/store.php b/classes/log/store.php index c35e33a17..234556bdf 100755 --- a/classes/log/store.php +++ b/classes/log/store.php @@ -91,13 +91,11 @@ public function process_events(array $events) { $loginfo = function ($message = '') { debugging($message, DEBUG_DEVELOPER); }; + $handlerconfig = [ 'log_error' => $logerror, 'log_info' => $loginfo, 'transformer' => [ - 'source_url' => 'http://moodle.org', - 'source_name' => 'Moodle', - 'source_version' => $CFG->release, 'source_lang' => 'en', 'send_mbox' => $this->get_config('mbox', false), 'send_response_choices' => $this->get_config('sendresponsechoices', false), @@ -120,6 +118,23 @@ public function process_events(array $events) { 'lrs_resend_failed_batches' => $this->get_config('resendfailedbatches', false), ], ]; + + if (isset($CFG->totara_release)) { + $source = [ + 'source_url' => 'http://totaralearning.com', + 'source_name' => 'Totara Learn', + 'source_version' => $CFG->totara_version + ]; + } else { + $source = [ + 'source_url' => 'http://moodle.org', + 'source_name' => 'Moodle', + 'source_version' => $CFG->release + ]; + } + + $handlerconfig['transformer'] = array_merge($handlerconfig['transformer'], $source); + $loadedevents = \src\handler($handlerconfig, $events); return $loadedevents; } diff --git a/src/transformer/events/totara_program/program_assigned.php b/src/transformer/events/totara_program/program_assigned.php new file mode 100644 index 000000000..ebf9ec568 --- /dev/null +++ b/src/transformer/events/totara_program/program_assigned.php @@ -0,0 +1,54 @@ +. + +namespace src\transformer\events\totara_program; + +defined('MOODLE_INTERNAL') || die(); + +use src\transformer\utils as utils; + +function program_assigned(array $config, \stdClass $event) { + $repo = $config['repo']; + $user = $repo->read_record_by_id('user', $event->userid); + $program = $repo->read_record_by_id('prog', $event->objectid); + $lang = $config['source_lang']; + + return[[ + 'actor' => utils\get_user($config, $user), + 'verb' => [ + 'id' => 'http://activitystrea.ms/schema/1.0/assign', + 'display' => [ + $lang => 'assigned' + ], + ], + 'object' => utils\totara\program($config, $program, $lang), + 'timestamp' => utils\get_event_timestamp($event), + 'context' => [ + 'platform' => $config['source_name'], + 'language' => $lang, + 'extensions' => utils\extensions\base($config, $event), + 'contextActivities' => [ + 'grouping' => [ + utils\get_activity\site($config) + ], + 'category' => [ + utils\get_activity\source($config) + ] + ], + ] + ]]; + +} \ No newline at end of file diff --git a/src/transformer/get_event_function_map.php b/src/transformer/get_event_function_map.php index 40a1155cd..3b0bfda27 100644 --- a/src/transformer/get_event_function_map.php +++ b/src/transformer/get_event_function_map.php @@ -18,7 +18,7 @@ defined('MOODLE_INTERNAL') || die(); function get_event_function_map() { - return [ + $availableevents = [ '\core\event\course_completed' => 'core\course_completed', '\core\event\course_viewed' => 'core\course_viewed', '\core\event\user_created' => 'core\user_created', @@ -63,5 +63,12 @@ function get_event_function_map() { '\mod_url\event\course_module_viewed' => 'mod_url\course_module_viewed', '\mod_wiki\event\course_module_viewed' => 'all\course_module_viewed', '\mod_workshop\event\course_module_viewed' => 'all\course_module_viewed', + '\totara_program\event\program_assigned' => 'totara_program\program_assigned' ]; + + $environmentevents = class_exists("report_eventlist_list_generator") ? array_keys(\report_eventlist_list_generator::get_all_events_list(false)) : array_keys($availableevents); + + return array_filter($availableevents, function($k) use ($environmentevents) { + return in_array($k, $environmentevents); + }, ARRAY_FILTER_USE_KEY); } \ No newline at end of file diff --git a/src/transformer/utils/extensions/base.php b/src/transformer/utils/extensions/base.php index a7cb7aefa..0bea2551e 100644 --- a/src/transformer/utils/extensions/base.php +++ b/src/transformer/utils/extensions/base.php @@ -17,7 +17,7 @@ namespace src\transformer\utils\extensions; defined('MOODLE_INTERNAL') || die(); -function base(array $config, \stdClass $event, $course) { +function base(array $config, \stdClass $event, $course=null) { return array_merge( info($config, $event), jisc($config, $event, $course) diff --git a/src/transformer/utils/totara/program.php b/src/transformer/utils/totara/program.php new file mode 100644 index 000000000..783525ae7 --- /dev/null +++ b/src/transformer/utils/totara/program.php @@ -0,0 +1,34 @@ +. + +namespace src\transformer\utils\totara; +defined('MOODLE_INTERNAL') || die(); + +function program(array $config, \stdClass $program, $lang) { + $programname = $program->fullname ? $program->fullname : 'A Totara program'; + + $object = [ + 'id' => $config['app_url'].'/totara/program/view.php?id='.$program->id, + 'definition' => [ + 'type' => 'http://id.tincanapi.com/activitytype/lms/program', + 'name' => [ + $lang => $programname, + ], + ], + ]; + + return $object; +} diff --git a/tests/totara_program/program_assigned/existing_program/data.json b/tests/totara_program/program_assigned/existing_program/data.json new file mode 100644 index 000000000..cb3f1a680 --- /dev/null +++ b/tests/totara_program/program_assigned/existing_program/data.json @@ -0,0 +1,22 @@ +{ + "user": [ + { + "id": 1, + "firstname": "test_fullname", + "email": "test@test.com" + } + ], + "course": [ + { + "id": 1, + "fullname": "test_name", + "lang": "en" + } + ], + "prog": [ + { + "id": 1, + "fullname": "test_name" + } + ] +} \ No newline at end of file diff --git a/tests/totara_program/program_assigned/existing_program/event.json b/tests/totara_program/program_assigned/existing_program/event.json new file mode 100644 index 000000000..4c138f9c2 --- /dev/null +++ b/tests/totara_program/program_assigned/existing_program/event.json @@ -0,0 +1,8 @@ +{ + "id": 1, + "userid": 1, + "timecreated": 1560399491, + "objecttable": "prog_assignment", + "objectid": 1, + "eventname": "\\totara_program\\event\\program_assigned" +} \ No newline at end of file diff --git a/tests/totara_program/program_assigned/existing_program/statements.json b/tests/totara_program/program_assigned/existing_program/statements.json new file mode 100644 index 000000000..803285101 --- /dev/null +++ b/tests/totara_program/program_assigned/existing_program/statements.json @@ -0,0 +1,63 @@ +[ + { + "actor": { + "name": "test_fullname", + "account": { + "homePage": "http:\/\/www.example.org", + "name": "1" + } + }, + "verb": { + "id": "http:\/\/activitystrea.ms\/schema\/1.0\/assign", + "display": { + "en": "assigned" + } + }, + "object": { + "id": "http:\/\/www.example.org\/totara\/program\/view.php?id=1", + "definition": { + "type": "http:\/\/id.tincanapi.com\/activitytype\/lms\/program", + "name": { + "en": "test_name" + } + } + }, + "timestamp": "2019-06-13T05:18:11+01:00", + "context": { + "platform": "Moodle", + "language": "en", + "extensions": { + "http:\/\/lrs.learninglocker.net\/define\/extensions\/info": { + "http:\/\/moodle.org": "1.0.0", + "https:\/\/github.com\/xAPI-vle\/moodle-logstore_xapi": "0.0.0-development", + "event_name": "\\totara_program\\event\\program_assigned", + "event_function": "\\src\\transformer\\events\\totara_program\\program_assigned" + } + }, + "contextActivities": { + "grouping": [ + { + "id": "http:\/\/www.example.org", + "definition": { + "type": "http:\/\/id.tincanapi.com\/activitytype\/lms", + "name": { + "en": "test_name" + } + } + } + ], + "category": [ + { + "id": "http:\/\/moodle.org", + "definition": { + "type": "http:\/\/id.tincanapi.com\/activitytype\/source", + "name": { + "en": "Moodle" + } + } + } + ] + } + } + } +] \ No newline at end of file diff --git a/tests/totara_program/program_assigned/existing_program/test.php b/tests/totara_program/program_assigned/existing_program/test.php new file mode 100644 index 000000000..54a37ae69 --- /dev/null +++ b/tests/totara_program/program_assigned/existing_program/test.php @@ -0,0 +1,24 @@ +. + +namespace tests\totara_program\program_assigned\existing_program; +defined('MOODLE_INTERNAL') || die(); + +class test extends \tests\xapi_test_case { + protected function get_test_dir() { + return __DIR__; + } +} \ No newline at end of file