Forum : Add cron to send notification of updates in all the courses to the teachers or coaches during the last X days -refs BT#20836

pull/4863/head
NicoDucou 2 years ago
parent e000b17b56
commit 5ef542a60d
  1. 145
      main/cron/forum_bulk_notification_to_course_teacher_for_new_post_in_last_X_days.php
  2. 1
      main/lang/english/trad4all.inc.php
  3. 1
      main/lang/french/trad4all.inc.php
  4. 1
      main/lang/spanish/trad4all.inc.php
  5. 22
      main/template/default/mail/cron_forum_update_bulk_notification_body.tpl

@ -0,0 +1,145 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Script to send notification to course's teachers when there are activities in a thread of a forum in his courses in the last X days
* The number of days is defined at the begining of the script with the variable
* It will send one sigle mail per user to notify with a line for each thread including thread's title, thread link and number of new post.
*
* @package chamilo.cron
*/
//This variable define the number of days in the past to look for activies in the forums
//This variable should be the same as the periodicity of the cron (if the cron is every day, then the variable should be equal to 1)
$numberOfDaysForLastActivy = 1;
require_once __DIR__.'/../inc/global.inc.php';
/**
* Initialization.
*/
if ('cli' != php_sapi_name()) {
exit; //do not run from browser
}
$date = new DateTime('now', new DateTimeZone('UTC'));
$date->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();

@ -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.";
?>

@ -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.";
?>

@ -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.";
?>

@ -0,0 +1,22 @@
<p>
{{HelloX}}
</p>
<p>
{{NotificationInYourForums}}
</p>
{% for course in notifyInfo %}
<h3>{{ course.courseName }}</h3>
<ul>
{% for session in course %}
{% for thread in session %}
<li><a href="{{ thread.threadLink }}">
<em class="fa fa-chevron-circle-right"></em>{{ thread.threadTitle }}
</a> : {{ thread.threadNbPost }} {{ NewForumPost }}
</li>
{% endfor %}
{% endfor %}
</ul>
{% endfor %}
<p>{{SignatureFormula}}</p>
Loading…
Cancel
Save