diff --git a/.upgradenotes/MDL-83432-2024101113112646.yml b/.upgradenotes/MDL-83432-2024101113112646.yml new file mode 100644 index 00000000000..f62114b7a7d --- /dev/null +++ b/.upgradenotes/MDL-83432-2024101113112646.yml @@ -0,0 +1,13 @@ +issueNumber: MDL-83432 +notes: + core_enrol: + - message: >- + The `after_user_enrolled` hook now contains a `roleid` property to allow + for listeners to determine which role was assigned during user enrolment + (if any) + + + The base enrolment `enrol_plugin::send_course_welcome_message_to_user` + method also now accepts a `$roleid` parameter in order to correctly + populate the `courserole` placeholder + type: changed diff --git a/enrol/classes/hook/after_user_enrolled.php b/enrol/classes/hook/after_user_enrolled.php index 17d5b194a30..5e5dc1af8fb 100644 --- a/enrol/classes/hook/after_user_enrolled.php +++ b/enrol/classes/hook/after_user_enrolled.php @@ -39,6 +39,8 @@ class after_user_enrolled { public readonly stdClass $enrolinstance, /** @var stdClass The user enrolment instance */ public readonly stdClass $userenrolmentinstance, + /** @var int|null The assigned role ID */ + public readonly ?int $roleid = null, ) { } diff --git a/enrol/manual/classes/user_enrolment_callbacks.php b/enrol/manual/classes/user_enrolment_callbacks.php index ac46dbb3388..daa9ba8a4b1 100644 --- a/enrol/manual/classes/user_enrolment_callbacks.php +++ b/enrol/manual/classes/user_enrolment_callbacks.php @@ -40,6 +40,7 @@ class user_enrolment_callbacks { userid: $hook->get_userid(), sendoption: $instance->customint1, message: $instance->customtext1, + roleid: $hook->roleid, ); } } diff --git a/lib/enrollib.php b/lib/enrollib.php index 08a724fc509..3beb03643c8 100644 --- a/lib/enrollib.php +++ b/lib/enrollib.php @@ -2159,6 +2159,7 @@ abstract class enrol_plugin { $hook = new \core_enrol\hook\after_user_enrolled( enrolinstance: $instance, userenrolmentinstance: $ue, + roleid: $roleid, ); \core\di::get(\core\hook\manager::class)->dispatch($hook); @@ -3644,18 +3645,23 @@ abstract class enrol_plugin { * @param int $userid User ID. * @param int $sendoption Send email from constant ENROL_SEND_EMAIL_FROM_* * @param null|string $message Message to send to the user. + * @param int|null $roleid The assigned role ID */ public function send_course_welcome_message_to_user( stdClass $instance, int $userid, int $sendoption, ?string $message = '', + ?int $roleid = null, ): void { global $DB; $context = context_course::instance($instance->courseid); $user = core_user::get_user($userid); $course = get_course($instance->courseid); - $courserole = $DB->get_record('role', ['id' => $instance->roleid]); + + // Fallback to the instance role ID if parameter not specified. + $courseroleid = $roleid ?: $instance->roleid; + $courserole = $DB->get_record('role', ['id' => $courseroleid]); $a = new stdClass(); $a->coursename = format_string($course->fullname, true, ['context' => $context, 'escape' => false]);