diff --git a/main/cron/forum_bulk_notification_to_course_teacher_for_new_post_in_last_X_days.php b/main/cron/forum_bulk_notification_to_course_teacher_for_new_post_in_last_X_days.php new file mode 100644 index 0000000000..459c06de52 --- /dev/null +++ b/main/cron/forum_bulk_notification_to_course_teacher_for_new_post_in_last_X_days.php @@ -0,0 +1,145 @@ +modify("-$numberOfDaysForLastActivy day"); +$startDate = $date->format('Y-m-d H:i:s'); + +$tablePost = Database::get_course_table(TABLE_FORUM_POST); +$tableThread = Database::get_course_table(TABLE_FORUM_THREAD); +$UpdatedThreads = []; +$sql = "SELECT * FROM $tableThread WHERE thread_date > '".$startDate."'"; +$result = Database::query($sql); +while ($row = Database::fetch_array($result)) { + $courseInfo = api_get_course_info_by_id($row['c_id']); + $updatedThreads[$row['c_id']]['courseName'] = $courseInfo['name']; + $sqlNbPost = "SELECT count(*) as nbPost FROM $tablePost WHERE thread_id = '".$row['iid']."' and post_date > '".$startDate."'"; + $resultNbPost = Database::query($sqlNbPost); + $rowNbPost = Database::fetch_array($resultNbPost); + $updatedThreads[$row['c_id']][$row['session_id']][$row['iid']] = [ + 'threadTitle' => $row['thread_title'], + 'threadNbPost' => $rowNbPost['nbPost'], + 'threadLink' => api_get_path(WEB_PATH) . 'main/forum/viewthread.php?cidReq=' . $courseInfo['code'] . '&id_session=' . $row['session_id'] . '&gidReq=0&gradebook=0&origin=&forum=' . $row['forum_id'] . '&thread=' . $row['iid'] + ]; + +} +foreach ($updatedThreads as $courseId => $sessions) { + foreach ($sessions as $sessionId => $threads) { + if ($sessionId === 0) { + $teacherList = CourseManager::getTeachersFromCourse($courseId, false); + foreach ($teacherList as $teacher) { + $usersToNotify[$teacher['id']][$courseId]['courseName'] = $sessions['courseName']; + $usersToNotify[$teacher['id']][$courseId][$sessionId] = $threads; + } + } else { + $courseCoachs = CourseManager::get_coachs_from_course( + $sessionId, + $courseId, + false + ); + foreach ($courseCoachs as $coach) { + $usersToNotify[$coach['user_id']][$courseId]['courseName'] = $sessions['courseName']; + $usersToNotify[$coach['user_id']][$courseId][$sessionId] = $threads; + } + } + } +} +foreach ($usersToNotify as $userId => $notifyInfo) { + sendMessage ($userId, $notifyInfo); +} + + + +/** + * Send the message to notify the specific user for all its courses and threads that have been updated, + * manage the corresponding template and send through MessageManager::send_message_simple. + * + * @param $toUserId + * @param $notifyInfo + * + * @return bool|int + */ +function sendMessage( + $toUserId, + $notifyInfo +) { + + $userInfo = api_get_user_info($toUserId); + $language = $userInfo['language']; + $subject = getUserLang('ForumBulkNotificationMailSubject', $language); + + $bodyTemplate = new Template( + null, + false, + false, + false, + false, + false + ); + + $userFullName = api_get_person_name($userInfo['firstname'], $userInfo['lastname']); + $bodyTemplate->assign('HelloX', sprintf(getUserLang('HelloX', $language), $userFullName)); + $bodyTemplate->assign('NotificationInYouForums', sprintf(getUserLang('NotificationInYouForums', $language))); + $bodyTemplate->assign('SignatureFormula', sprintf(getUserLang('SignatureFormula', $language))); + $bodyTemplate->assign('notifyInfo', $notifyInfo); + + $bodyLayout = $bodyTemplate->get_template( + 'mail/cron_forum_update_bulk_notification_body.tpl' + ); + + $content = $bodyTemplate->fetch($bodyLayout); + + return MessageManager::send_message_simple( + $toUserId, + $subject, + $content, + 1, + ); +} + +/** + * Returns a translated (localized) string by user language. + * + * @param $variable + * @param $language + * + * @return mixed + */ +function getUserLang($variable, $language) +{ + $languageFilesToLoad = api_get_language_files_to_load($language); + foreach ($languageFilesToLoad as $languageFile) { + include $languageFile; + } + + $translate = $variable; + if (isset($$variable)) { + $langVariable = $$variable; + $translate = $langVariable; + } + + return $translate; +} + +exit(); + diff --git a/main/lang/english/trad4all.inc.php b/main/lang/english/trad4all.inc.php index a6935994b2..cc59b9ad32 100644 --- a/main/lang/english/trad4all.inc.php +++ b/main/lang/english/trad4all.inc.php @@ -9034,4 +9034,5 @@ $ShowCareersHierarchy = "Show hierarchies"; $ParentCareer = "Parent career"; $CareerCannotBeDeletedAsItHasChildren = "This career cannot be deleted until all of its children have been deleted or moved to another career."; $EmailUsedTwiceInImportFile = "This email is already in use by another user in the same file."; +$NotificationInYourForums = "You have new posts in the following forums of your courses."; ?> \ No newline at end of file diff --git a/main/lang/french/trad4all.inc.php b/main/lang/french/trad4all.inc.php index d5314e8c82..5ecdc850e9 100644 --- a/main/lang/french/trad4all.inc.php +++ b/main/lang/french/trad4all.inc.php @@ -8968,4 +8968,5 @@ $ShowCareersHierarchy = "Montrer les hiérarchies"; $ParentCareer = "Filière parente"; $CareerCannotBeDeletedAsItHasChildren = "Cette filière ne peut pas être supprimée tant que tous ses enfants n'ont pas été supprimés ou déplacés vers une autre filière."; $EmailUsedTwiceInImportFile = "Ce courriel est déjà utilisé par un autre utilisateur du même fichier."; +$NotificationInYourForums = "Vous avez de nouveaux posts dans les forums suivants de vos cours."; ?> \ No newline at end of file diff --git a/main/lang/spanish/trad4all.inc.php b/main/lang/spanish/trad4all.inc.php index 2b16bec83b..7490606161 100644 --- a/main/lang/spanish/trad4all.inc.php +++ b/main/lang/spanish/trad4all.inc.php @@ -9059,4 +9059,5 @@ $ShowCareersHierarchy = "Mostrar jerarquías"; $ParentCareer = "Carrera padre"; $CareerCannotBeDeletedAsItHasChildren = "Esta carrera no puede ser eliminada hasta que las carreras hijas a ésta sean eliminadas o movidas a otra carrera."; $EmailUsedTwiceInImportFile = "Este correo electrónico ya está usado por otro usuario en el mismo archivo."; +$NotificationInYourForums = "Hay nuevos posts en los foros siguientes de tus cursos."; ?> \ No newline at end of file diff --git a/main/template/default/mail/cron_forum_update_bulk_notification_body.tpl b/main/template/default/mail/cron_forum_update_bulk_notification_body.tpl new file mode 100644 index 0000000000..7cd522b259 --- /dev/null +++ b/main/template/default/mail/cron_forum_update_bulk_notification_body.tpl @@ -0,0 +1,22 @@ +
+ {{HelloX}} +
+ ++ {{NotificationInYourForums}} +
+ +{% for course in notifyInfo %} +{{SignatureFormula}}