diff --git a/communication/provider/matrix/classes/communication_feature.php b/communication/provider/matrix/classes/communication_feature.php index c3076ef7300..657fec5ebb1 100644 --- a/communication/provider/matrix/classes/communication_feature.php +++ b/communication/provider/matrix/classes/communication_feature.php @@ -628,8 +628,9 @@ class communication_feature implements if (!empty($forceremoval)) { // Remove the users from the power levels if they are not admins. foreach ($forceremoval as $userid) { - if ($newuserpowerlevels < matrix_constants::POWER_LEVEL_MAXIMUM) { - unset($newuserpowerlevels[$userid]); + $muid = matrix_user_manager::get_matrixid_from_moodle($userid); + if (isset($newuserpowerlevels[$muid]) && $newuserpowerlevels[$muid] < matrix_constants::POWER_LEVEL_MAXIMUM) { + unset($newuserpowerlevels[$muid]); } } } @@ -639,7 +640,6 @@ class communication_feature implements return; } - // Update the power levels for the room. $this->matrixapi->update_room_power_levels( roomid: $this->get_room_id(), diff --git a/communication/provider/matrix/tests/communication_feature_test.php b/communication/provider/matrix/tests/communication_feature_test.php index e6d38b7c1da..91071e01022 100644 --- a/communication/provider/matrix/tests/communication_feature_test.php +++ b/communication/provider/matrix/tests/communication_feature_test.php @@ -568,4 +568,73 @@ class communication_feature_test extends \advanced_testcase { unset_config('matrixaccesstoken', 'communication_matrix'); $this->assertFalse($communicationprocessor->get_room_provider()->is_configured()); } + + /** + * Test inactive users are not included when being mapped to a new communication instance. + */ + public function test_inactive_users_are_not_mapped_to_new_communication(): void { + // Create a course without a communication provider set. + $course = $this->getDataGenerator()->create_course(); + + // Enrol some users that are both active and inactive (suspended). + $user1 = $this->getDataGenerator()->create_user(); + $user2 = $this->getDataGenerator()->create_user(); + $user3 = $this->getDataGenerator()->create_user(); + $user4 = $this->getDataGenerator()->create_user(); + + $this->getDataGenerator()->enrol_user( + userid: $user1->id, + courseid: $course->id, + roleidorshortname: 'teacher', + ); + + $this->getDataGenerator()->enrol_user( + userid: $user2->id, + courseid: $course->id, + roleidorshortname: 'student', + ); + + $this->getDataGenerator()->enrol_user( + userid: $user3->id, + courseid: $course->id, + roleidorshortname: 'teacher', + status: ENROL_USER_SUSPENDED, + ); + + $this->getDataGenerator()->enrol_user( + userid: $user4->id, + courseid: $course->id, + roleidorshortname: 'student', + status: ENROL_USER_SUSPENDED, + ); + + // Set Matrix as the communication provider and update. + $course->selectedcommunication = 'communication_matrix'; + $course->communication_matrixroomname = 'testroom'; + update_course($course); + + $communicationprocessor = processor::load_by_instance( + context: \core\context\course::instance($course->id), + component: 'core_course', + instancetype: 'coursecommunication', + instanceid: $course->id + ); + + $userids = $communicationprocessor->get_all_userids_for_instance(); + + $this->assertEquals( + expected: 2, + actual: count($userids), + ); + + $this->assertContains( + needle: $user1->id, + haystack: $userids, + ); + + $this->assertContains( + needle: $user2->id, + haystack: $userids, + ); + } } diff --git a/course/lib.php b/course/lib.php index 730cef314c8..7c476717bc6 100644 --- a/course/lib.php +++ b/course/lib.php @@ -2562,6 +2562,12 @@ function update_course($data, $editoroptions = NULL) { // Newly created providers complete the user mapping but do not queue the task // (it will be handled by the room creation task). if ($addusersrequired) { + // Ensure only active users are added to the room. + $activeusers = array_column( + enrol_get_course_users(courseid: $data->id, onlyactive: true), + 'id', + ); + $enrolledusers = array_intersect($activeusers, $enrolledusers); $communication->add_members_to_room($enrolledusers, $instanceexists); } }