diff --git a/src/transformer/events/core/course_viewed.php b/src/transformer/events/core/course_viewed.php index 51ecd1494..644be281c 100644 --- a/src/transformer/events/core/course_viewed.php +++ b/src/transformer/events/core/course_viewed.php @@ -21,8 +21,16 @@ use src\transformer\utils as utils; function course_viewed(array $config, \stdClass $event) { + global $CFG; + $repo = $config['repo']; - $user = $repo->read_record_by_id('user', $event->userid); + + // Get the userid or a valid userid for guest. + if (!isset($event->userid)) { + $event->userid = 0; + } + $userid = $event->userid == 0 && isset($CFG->siteguest) ? $CFG->siteguest : $event->userid; + $user = $repo->read_record_by_id('user', $userid); $course = $repo->read_record_by_id('course', $event->courseid); $lang = utils\get_course_lang($course); diff --git a/src/transformer/events/core/user_created.php b/src/transformer/events/core/user_created.php index 611fcb020..a3e26a80c 100644 --- a/src/transformer/events/core/user_created.php +++ b/src/transformer/events/core/user_created.php @@ -21,8 +21,17 @@ use src\transformer\utils as utils; function user_created(array $config, \stdClass $event) { + global $CFG; + $repo = $config['repo']; - $user = $repo->read_record_by_id('user', $event->relateduserid); + + // Get the relateduserid or a valid relateduserid from the guest userid. + if (!isset($event->relateduserid)) { + $event->relateduserid = 0; + } + $relateduserid = $event->relateduserid == 0 && isset($CFG->siteguest) ? $CFG->siteguest : $event->relateduserid; + $user = $repo->read_record_by_id('user', $relateduserid); + $lang = $config['source_lang']; return [[