Event USER_ENROLLED : how to catch the real roleid ?

Event USER_ENROLLED : how to catch the real roleid ?

by Pascal Maury -
Number of replies: 1
Picture of Plugin developers

Hi,

I developped a local plugin to enrol users automatically in groups when they are enrolled in a course.

I created the function "local_groupautoplus_user_enrolled($eventdata)" and I got details from Object $evendata

Example 1 : when an user is enrolled via a cohort :

Object $evendata
[id] => 382935
[status] => 0
[enrolid] => 10855
[userid] => 622
[timestart] => 0
[timeend] => 0
[modifierid] => 193
[timecreated] => 1413565732
[timemodified] => 1413565732
[enrol] => cohort
[courseid] => 3218


Example 2 : when an user is enrolled manually :

Object $evendata
[id] => 382934
[status] => 0
[enrolid] => 397
[userid] => 42
[timestart] => 1413496800
[timeend] => 0
[modifierid] => 193
[timecreated] => 1413565569
[timemodified] => 1413565569
[enrol] => manual
[courseid] => 3218


I need to get the roleid defined for this user when enrolled :
> for example 1, it is easy : I got the roleid in table enrol : "SELECT roleid FROM enrol where id = $evendata->enrolid"



> for example 2, it is more complicated : if I use the same request, I got a roleid which is the "default roleid" for the "manual enrolments" method, not the real role assigned.
To find it, I have to get it from the table role_assignments



The problem is : when I want to get the roleid from role_assignments, I have got this error :

Can not find data record in database table role_assignments.

Debug info: SELECT roleid FROM {role_assignments} WHERE contextid = ? AND userid = ?
[array (
0 => 130111,
1 => '42',
)]
Error code: invalidrecord

The fact is : the table role_assignments is filled AFTER that the function "local_groupautoplus_user_enrolled($eventdata)" is called ...


How can I get the real roleid which is not in the Object $evendata ... ?

Average of ratings: -
In reply to Pascal Maury

Re: Event USER_ENROLLED : how to catch the real roleid ?

by Artur Welp -

I had this problem today. More than one year after, so i think it is no longer useful.


It is not possible get the user role in the event: core\event\user_enrolment_created, because Moodle deal with enrolment_created and core\event\role_assigned in different ways.


First he create the enrolment, this trigger an event core\event\user_enrolment_created at this point the user role is not defined yet.

Than he assign the role to the user, this trigger an event core\event\role_assigned. Probably this is the event you want.


This can be seen in the file lib/enrollib.php from line 1321 onward.

//Line 1320 - File lib/enrollib.php
if ($inserted) {
// Trigger event.
$event = \core\event\user_enrolment_created::create(
array(
'objectid' => $ue->id,
'courseid' => $courseid,
'context' => $context,
'relateduserid' => $ue->userid,
'other' => array('enrol' => $name)
)
);
$event->trigger();
// Check if course contacts cache needs to be cleared.
require_once($CFG->libdir . '/coursecatlib.php');
coursecat::user_enrolment_changed($courseid, $ue->userid,
$ue->status, $ue->timestart, $ue->timeend);
}

if ($roleid) {
// this must be done after the enrolment event so that the role_assigned event is triggered afterwards
if ($this->roles_protected()) {
role_assign($roleid, $userid, $context->id, 'enrol_'.$name, $instance->id);
} else {
role_assign($roleid, $userid, $context->id);
}
}


So this is my code to get the user role:

public static function my_plugin_role_assigned(core\event\role_assigned $enrolment_data){        
global $DB;

//strange var name, better change it
$enrolment_data_data = $enrolment_data->get_data();
$snapshotid = $enrolment_data->get_data()['other']['id'];
$snapshot = $enrolment_data->get_record_snapshot('role_assignments', $snapshotid);

$roleid = $snapshot->roleid;
$rolename = $DB->get_records_sql("SELECT shortname from {role} WHERE id = ?", array($roleid));
$rolename = array_pop($rolename);
$rolename = $rolename->shortname;

if($rolename == 'editingteacher'){
//My stuff
}

}

Tip: Instead of use roleid, use role shortname, because if you export something to other Moodle installation, the roleid may be different.


Average of ratings: Useful (2)