diff --git a/lib/accesslib.php b/lib/accesslib.php index a034f18e0a0..0d4cfd6a406 100644 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -1645,18 +1645,6 @@ function role_assign($roleid, $userid, $contextid, $component = '', $itemid = 0, core_course_category::role_assignment_changed($roleid, $context); - // Update the room membership and power levels when the user role changes. - if (\core_communication\api::is_available() && $coursecontext = $context->get_course_context(false)) { - $communication = \core_communication\api::load_by_instance( - $coursecontext, - 'core_course', - 'coursecommunication', - $coursecontext->instanceid, - ); - - $communication->update_room_membership([$userid]); - } - $event = \core\event\role_assigned::create(array( 'context' => $context, 'objectid' => $ra->roleid, @@ -1670,6 +1658,13 @@ function role_assign($roleid, $userid, $contextid, $component = '', $itemid = 0, $event->add_record_snapshot('role_assignments', $ra); $event->trigger(); + // Dispatch the hook for post role assignment actions. + $hook = new \core\hook\access\after_role_assigned( + context: $context, + userid: $userid, + ); + \core\di::get(\core\hook\manager::class)->dispatch($hook); + return $ra->id; } @@ -1763,19 +1758,12 @@ function role_unassign_all(array $params, $subcontexts = false, $includemanual = $event->trigger(); core_course_category::role_assignment_changed($ra->roleid, $context); - // Update the room membership and power levels when the user role changes. - if (\core_communication\api::is_available() && $coursecontext = $context->get_course_context(false)) { - $communication = \core_communication\api::load_by_instance( - $coursecontext, - 'core_course', - 'coursecommunication', - $coursecontext->instanceid, - ); - - $communication->update_room_membership([$ra->userid]); - } - - + // Dispatch the hook for post role assignment actions. + $hook = new \core\hook\access\after_role_unassigned( + context: $context, + userid: $ra->userid, + ); + \core\di::get(\core\hook\manager::class)->dispatch($hook); } } unset($ras); diff --git a/lib/classes/hook/access/after_role_assigned.php b/lib/classes/hook/access/after_role_assigned.php new file mode 100644 index 00000000000..3850854dd53 --- /dev/null +++ b/lib/classes/hook/access/after_role_assigned.php @@ -0,0 +1,44 @@ +. + +namespace core\hook\access; + +use context; + +/** + * Hook after a role is assigned. + * + * @package core + * @copyright 2024 Safat Shahin + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +#[\core\attribute\label('Allows plugins or features to perform actions after a role is assigned to a user.')] +#[\core\attribute\tags('role,', 'user')] +class after_role_assigned { + + /** + * Constructor for the hook. + * + * @param context $context The context of the role assignment. + * @param int $userid The user id of the user. + * + */ + public function __construct( + public readonly context $context, + public readonly int $userid, + ) { + } +} diff --git a/lib/classes/hook/access/after_role_unassigned.php b/lib/classes/hook/access/after_role_unassigned.php new file mode 100644 index 00000000000..1dd5d8591c9 --- /dev/null +++ b/lib/classes/hook/access/after_role_unassigned.php @@ -0,0 +1,44 @@ +. + +namespace core\hook\access; + +use context; + +/** + * Hook after a role is unassigned. + * + * @package core + * @copyright 2024 Safat Shahin + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +#[\core\attribute\label('Allows plugins or features to perform actions after a role is unassigned for a user.')] +#[\core\attribute\tags('role,', 'user')] +class after_role_unassigned { + + /** + * Constructor for the hook. + * + * @param context $context The context of the role assignment. + * @param int $userid The user id of the user. + * + */ + public function __construct( + public readonly context $context, + public readonly int $userid, + ) { + } +}