Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 606: Transforming event having userid=0 creates exception fix. #729

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/transformer/events/core/course_viewed.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@
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 relateduserid or a valid userid for guest.
$userid = $event->userid == 0 && isset($CFG->siteguest) ? $CFG->siteguest : $event->userid;
Copy link
Contributor

@gordonmacqueen-lp gordonmacqueen-lp Jun 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mlynn-lp
Can the logic here be extracted out to a utility function?
It could take the config, event and an identifier for the field we are interested in (userid or relateduserid)

This way, it can be reused elsewhere too (like here).

$user = $repo->read_record_by_id('user', $userid);
$course = $repo->read_record_by_id('course', $event->courseid);
$lang = utils\get_course_lang($course);

Expand Down
8 changes: 7 additions & 1 deletion src/transformer/events/core/user_created.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@
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 a valid user for guest.
$relateduserid = $event->relateduserid == 0 && isset($CFG->siteguest) ? $CFG->siteguest : $event->relateduserid;
$user = $repo->read_record_by_id('user', $relateduserid);

$lang = $config['source_lang'];

return [[
Expand Down