diff --git a/public/main/inc/lib/course.lib.php b/public/main/inc/lib/course.lib.php index 47ce26f39d9..f0998c17e7a 100644 --- a/public/main/inc/lib/course.lib.php +++ b/public/main/inc/lib/course.lib.php @@ -896,6 +896,18 @@ public static function subscribeUser( ) ); + $subject = get_lang('You have been enrolled in the course').' '.$course->getTitle(); + $message = sprintf(get_lang('Hello %s, you have been enrolled in the course %s.'), UserManager::formatUserFullName($user, true), $course->getTitle()); + + MessageManager::send_message_simple( + $userId, + $subject, + $message, + api_get_user_id(), + false, + true + ); + $send = (int) api_get_course_setting('email_alert_to_teacher_on_new_user_in_course', $course); if (1 === $send) { diff --git a/public/main/inc/lib/sessionmanager.lib.php b/public/main/inc/lib/sessionmanager.lib.php index eff09bb834c..95b7f06864a 100644 --- a/public/main/inc/lib/sessionmanager.lib.php +++ b/public/main/inc/lib/sessionmanager.lib.php @@ -2615,7 +2615,9 @@ public static function subscribe_users_to_session_course( $user_list, $courseId, $session_id, - ['visibility' => $session_visibility] + ['visibility' => $session_visibility], + true, + true ); } @@ -9913,15 +9915,16 @@ public static function insertUsersInCourse( int $courseId, int $sessionId, array $relationInfo = [], - bool $updateSession = true + bool $updateSession = true, + bool $sendNotification = false ) { $em = Database::getManager(); $course = api_get_course_entity($courseId); $session = api_get_session_entity($sessionId); - $relationInfo = array_merge(['visibility' => 0, 'status' => Session::STUDENT], $relationInfo); + $usersToInsert = []; foreach ($studentIds as $studentId) { $user = api_get_user_entity($studentId); $session->addUserInCourse($relationInfo['status'], $user, $course) @@ -9930,15 +9933,40 @@ public static function insertUsersInCourse( Event::logUserSubscribedInCourseSession($user, $course, $session); if ($updateSession) { - $session->addUserInSession(Session::STUDENT, $user); + if (!$session->hasUserInSession($user, Session::STUDENT)) { + $session->addUserInSession(Session::STUDENT, $user); + } } + + $usersToInsert[] = $studentId; } - try { - $em->persist($session); - $em->flush(); - } catch (\Exception $e) { - error_log("Error executing flush: " . $e->getMessage()); + $em->persist($session); + $em->flush(); + + if ($sendNotification && !empty($usersToInsert)) { + foreach ($usersToInsert as $userId) { + $user = api_get_user_entity($userId); + $courseTitle = $course->getTitle(); + $sessionTitle = $session->getTitle(); + + $subject = sprintf(get_lang('You have been enrolled in the course %s for the session %s'), $courseTitle, $sessionTitle); + $message = sprintf( + get_lang('Hello %s, you have been enrolled in the course %s for the session %s.'), + UserManager::formatUserFullName($user, true), + $courseTitle, + $sessionTitle + ); + + MessageManager::send_message_simple( + $userId, + $subject, + $message, + api_get_user_id(), + false, + true + ); + } } } diff --git a/public/main/session/add_users_to_session_course.php b/public/main/session/add_users_to_session_course.php index 6fcc8130524..5df038624bb 100644 --- a/public/main/session/add_users_to_session_course.php +++ b/public/main/session/add_users_to_session_course.php @@ -356,7 +356,9 @@ function change_select(val) { SessionManager::subscribe_users_to_session_course( $UserList, $id_session, - $courseInfo['code'] + $courseInfo['code'], + SESSION_VISIBLE_READ_ONLY, + true ); Display::addFlash(Display::return_message(get_lang('Update successful'))); @@ -661,12 +663,12 @@ function change_select(val) { if ('multiple' === $add_type) { if (is_array($extra_field_list)) { if (is_array($new_field_list) && count($new_field_list) > 0) { - echo '