MDL-78551 core: Register hook callbacks and dispatchers
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Hook listener callbacks.
|
||||
*
|
||||
* @package core
|
||||
* @copyright 2023 Safat Shahin <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$callbacks = [
|
||||
[
|
||||
'hook' => \core_group\hook\after_group_created::class,
|
||||
'callback' => \core_communication\hook_listener::class . '::create_group_communication',
|
||||
],
|
||||
[
|
||||
'hook' => \core_group\hook\after_group_updated::class,
|
||||
'callback' => \core_communication\hook_listener::class . '::update_group_communication',
|
||||
],
|
||||
[
|
||||
'hook' => \core_group\hook\after_group_deleted::class,
|
||||
'callback' => \core_communication\hook_listener::class . '::delete_group_communication',
|
||||
],
|
||||
[
|
||||
'hook' => \core_group\hook\after_group_membership_added::class,
|
||||
'callback' => \core_communication\hook_listener::class . '::add_members_to_group_room',
|
||||
],
|
||||
[
|
||||
'hook' => \core_group\hook\after_group_membership_removed::class,
|
||||
'callback' => \core_communication\hook_listener::class . '::remove_members_from_group_room',
|
||||
],
|
||||
[
|
||||
'hook' => \core_course\hook\after_course_created::class,
|
||||
'callback' => \core_communication\hook_listener::class . '::create_course_communication',
|
||||
],
|
||||
[
|
||||
'hook' => \core_course\hook\after_course_updated::class,
|
||||
'callback' => \core_communication\hook_listener::class . '::update_course_communication',
|
||||
],
|
||||
[
|
||||
'hook' => \core_course\hook\before_course_delete::class,
|
||||
'callback' => \core_communication\hook_listener::class . '::delete_course_communication',
|
||||
],
|
||||
[
|
||||
'hook' => \core_user\hook\before_user_update::class,
|
||||
'callback' => \core_communication\hook_listener::class . '::update_user_room_memberships',
|
||||
],
|
||||
[
|
||||
'hook' => \core_user\hook\before_user_deleted::class,
|
||||
'callback' => \core_communication\hook_listener::class . '::delete_user_room_memberships',
|
||||
],
|
||||
[
|
||||
'hook' => \core\hook\access\after_role_assigned::class,
|
||||
'callback' => \core_communication\hook_listener::class . '::update_user_membership_for_role_changes',
|
||||
],
|
||||
[
|
||||
'hook' => \core\hook\access\after_role_unassigned::class,
|
||||
'callback' => \core_communication\hook_listener::class . '::update_user_membership_for_role_changes',
|
||||
],
|
||||
[
|
||||
'hook' => \core_enrol\hook\after_enrol_instance_status_updated::class,
|
||||
'callback' => \core_communication\hook_listener::class . '::update_communication_memberships_for_enrol_status_change',
|
||||
],
|
||||
[
|
||||
'hook' => \core_enrol\hook\before_enrol_instance_delete::class,
|
||||
'callback' => \core_communication\hook_listener::class . '::remove_communication_memberships_for_enrol_instance_deletion',
|
||||
],
|
||||
[
|
||||
'hook' => \core_enrol\hook\after_user_enrolled::class,
|
||||
'callback' => \core_communication\hook_listener::class . '::add_communication_membership_for_enrolled_user',
|
||||
],
|
||||
[
|
||||
'hook' => \core_enrol\hook\before_user_enrolment_update::class,
|
||||
'callback' => \core_communication\hook_listener::class . '::update_communication_membership_for_updated_user_enrolment',
|
||||
],
|
||||
[
|
||||
'hook' => \core_enrol\hook\before_user_enrolment_remove::class,
|
||||
'callback' => \core_communication\hook_listener::class . '::remove_communication_membership_for_unenrolled_user',
|
||||
],
|
||||
];
|
||||
+12
-38
@@ -3563,7 +3563,6 @@ function delete_user(stdClass $user) {
|
||||
debugging('Local administrator accounts can not be deleted.');
|
||||
return false;
|
||||
}
|
||||
|
||||
// Allow plugins to use this user object before we completely delete it.
|
||||
if ($pluginsfunction = get_plugins_with_function('pre_user_delete')) {
|
||||
foreach ($pluginsfunction as $plugintype => $plugins) {
|
||||
@@ -3573,28 +3572,18 @@ function delete_user(stdClass $user) {
|
||||
}
|
||||
}
|
||||
|
||||
// Dispatch the hook for pre user update actions.
|
||||
$hook = new \core_user\hook\before_user_deleted(
|
||||
user: $user,
|
||||
);
|
||||
\core\di::get(\core\hook\manager::class)->dispatch($hook);
|
||||
|
||||
// Keep user record before updating it, as we have to pass this to user_deleted event.
|
||||
$olduser = clone $user;
|
||||
|
||||
// Keep a copy of user context, we need it for event.
|
||||
$usercontext = context_user::instance($user->id);
|
||||
|
||||
// Remove user from communication rooms immediately.
|
||||
if (core_communication\api::is_available()) {
|
||||
foreach (enrol_get_users_courses($user->id) as $course) {
|
||||
$communication = \core_communication\processor::load_by_instance(
|
||||
context: \core\context\course::instance($course->id),
|
||||
component: 'core_course',
|
||||
instancetype: 'coursecommunication',
|
||||
instanceid: $course->id,
|
||||
);
|
||||
if ($communication !== null) {
|
||||
$communication->get_room_user_provider()->remove_members_from_room([$user->id]);
|
||||
$communication->delete_instance_user_mapping([$user->id]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Delete all grades - backup is kept in grade_grades_history table.
|
||||
grade_user_delete($user->id);
|
||||
|
||||
@@ -4641,6 +4630,12 @@ function delete_course($courseorid, $showfeedback = true) {
|
||||
}
|
||||
}
|
||||
|
||||
// Dispatch the hook for pre course delete actions.
|
||||
$hook = new \core_course\hook\before_course_delete(
|
||||
course: $course,
|
||||
);
|
||||
\core\di::get(\core\hook\manager::class)->dispatch($hook);
|
||||
|
||||
// Tell the search manager we are about to delete a course. This prevents us sending updates
|
||||
// for each individual context being deleted.
|
||||
\core_search\manager::course_deleting_start($courseid);
|
||||
@@ -4651,30 +4646,9 @@ function delete_course($courseorid, $showfeedback = true) {
|
||||
// Make the course completely empty.
|
||||
remove_course_contents($courseid, $showfeedback);
|
||||
|
||||
// Communication provider delete associated information.
|
||||
$communication = \core_communication\api::load_by_instance(
|
||||
$context,
|
||||
'core_course',
|
||||
'coursecommunication',
|
||||
$course->id
|
||||
);
|
||||
|
||||
// Delete the course and related context instance.
|
||||
context_helper::delete_instance(CONTEXT_COURSE, $courseid);
|
||||
|
||||
// Update communication room membership of enrolled users.
|
||||
require_once($CFG->libdir . '/enrollib.php');
|
||||
$courseusers = enrol_get_course_users($courseid);
|
||||
$enrolledusers = [];
|
||||
|
||||
foreach ($courseusers as $user) {
|
||||
$enrolledusers[] = $user->id;
|
||||
}
|
||||
|
||||
$communication->remove_members_from_room($enrolledusers);
|
||||
|
||||
$communication->delete_room();
|
||||
|
||||
$DB->delete_records("course", array("id" => $courseid));
|
||||
$DB->delete_records("course_format_options", array("courseid" => $courseid));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user