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 all commits
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
10 changes: 9 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,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;
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
11 changes: 10 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,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 [[
Expand Down