diff --git a/main/inc/lib/sessionmanager.lib.php b/main/inc/lib/sessionmanager.lib.php index 1bf94bb3276..7036dd4f24e 100755 --- a/main/inc/lib/sessionmanager.lib.php +++ b/main/inc/lib/sessionmanager.lib.php @@ -2052,6 +2052,22 @@ public static function subscribeUsersToSession( $course_list[] = $row['c_id']; } + // Build list of users already subscribed to the session as students. + // This allows us to avoid re-enrolling them into all courses again + // when they are already part of the session (preserves manual + // unsubscriptions at the course level). + $usersAlreadyInSession = []; + if (!empty($userList)) { + $userIdsStr = "'".implode("','", $userList)."'"; + $sql = "SELECT user_id FROM $tbl_session_rel_user + WHERE session_id = $sessionId AND relation_type = 0 + AND user_id IN ($userIdsStr)"; + $resUsersInSession = Database::query($sql); + while ($row = Database::fetch_array($resUsersInSession)) { + $usersAlreadyInSession[] = (int) $row['user_id']; + } + } + if ($session->getSendSubscriptionNotification() && is_array($userList) ) { @@ -2154,8 +2170,8 @@ public static function subscribeUsersToSession( $usersToSubscribeInCourse = array_filter( $userList, - function ($userId) use ($existingUsers) { - return !in_array($userId, $existingUsers); + function ($userId) use ($existingUsers, $usersAlreadyInSession) { + return !in_array($userId, $existingUsers) && !in_array($userId, $usersAlreadyInSession); } );