remind_course_expiration.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Course expiration reminder.
  5. *
  6. * @package chamilo.cron
  7. *
  8. * @author Imanol Losada <imanol.losada@beeznest.com>
  9. */
  10. require_once __DIR__.'/../inc/global.inc.php';
  11. /**
  12. * Initialization.
  13. */
  14. if (php_sapi_name() != 'cli') {
  15. exit; //do not run from browser
  16. }
  17. $isActive = api_get_setting('cron_remind_course_expiration_activate') === 'true';
  18. if (!$isActive) {
  19. exit;
  20. }
  21. $frequency = api_get_setting('cron_remind_course_expiration_frequency');
  22. // Days before expiration date to send reminders
  23. $today = gmdate("Y-m-d");
  24. $expirationDate = gmdate("Y-m-d", strtotime("$today + $frequency day"));
  25. $gradebookTable = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  26. $certificateTable = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
  27. $sessionTable = Database::get_main_table(TABLE_MAIN_SESSION);
  28. $sessionUserTable = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  29. $query = "
  30. SELECT DISTINCT category.session_id, certificate.user_id
  31. FROM $gradebookTable AS category
  32. LEFT JOIN $certificateTable AS certificate
  33. ON category.id = certificate.cat_id
  34. INNER JOIN $sessionTable AS session
  35. ON category.session_id = session.id
  36. WHERE
  37. session.access_end_date BETWEEN '$today' AND
  38. '$expirationDate' AND
  39. category.session_id IS NOT NULL";
  40. $sessionId = 0;
  41. $userIds = [];
  42. $sessions = [];
  43. $result = Database::query($query);
  44. $urlSessionTable = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  45. $urlTable = Database::get_main_table(TABLE_MAIN_ACCESS_URL);
  46. while ($row = Database::fetch_array($result)) {
  47. if ($sessionId != $row['session_id']) {
  48. $sessionId = $row['session_id'];
  49. $userIds = [];
  50. }
  51. if (!is_null($row['user_id'])) {
  52. array_push($userIds, $row['user_id']);
  53. }
  54. $sessions[$sessionId] = $userIds;
  55. }
  56. $usersToBeReminded = [];
  57. foreach ($sessions as $sessionId => $userIds) {
  58. $userId = 0;
  59. $userIds = $userIds ? " AND sessionUser.user_id NOT IN (".implode(", ", $userIds).")" : null;
  60. $query = "
  61. SELECT sessionUser.session_id, sessionUser.user_id, session.name, session.access_end_date
  62. FROM $sessionUserTable AS sessionUser
  63. INNER JOIN $sessionTable AS session
  64. ON sessionUser.session_id = session.id
  65. WHERE
  66. session_id = $sessionId$userIds";
  67. $result = Database::query($query);
  68. while ($row = Database::fetch_array($result)) {
  69. $usersToBeReminded[$row['user_id']][$row['session_id']] = [
  70. 'name' => $row['name'],
  71. 'access_end_date' => $row['access_end_date'],
  72. ];
  73. }
  74. }
  75. if ($usersToBeReminded) {
  76. $today = date_create($today);
  77. $administrator = [
  78. 'completeName' => api_get_person_name(
  79. api_get_setting("administratorName"),
  80. api_get_setting("administratorSurname"),
  81. null,
  82. PERSON_NAME_EMAIL_ADDRESS
  83. ),
  84. 'email' => api_get_setting("emailAdministrator"),
  85. ];
  86. echo "\n======================================================================\n\n";
  87. foreach ($usersToBeReminded as $userId => $sessions) {
  88. $user = api_get_user_info($userId);
  89. $userCompleteName = api_get_person_name(
  90. $user['firstname'],
  91. $user['lastname'],
  92. null,
  93. PERSON_NAME_EMAIL_ADDRESS
  94. );
  95. foreach ($sessions as $sessionId => $session) {
  96. $daysRemaining = date_diff($today, date_create($session['access_end_date']));
  97. $join = " INNER JOIN $urlSessionTable ON id = access_url_id";
  98. $result = Database::select(
  99. 'url',
  100. "$urlTable $join",
  101. [
  102. 'where' => [
  103. 'session_id = ?' => [
  104. $sessionId,
  105. ],
  106. ],
  107. 'limit' => '1',
  108. ]
  109. );
  110. $subjectTemplate = new Template(null, false, false, false, false, false);
  111. $subjectTemplate->assign('session_name', $session['name']);
  112. $subjectTemplate->assign(
  113. 'session_access_end_date',
  114. $session['access_end_date']
  115. );
  116. $subjectTemplate->assign(
  117. 'remaining_days',
  118. $daysRemaining->format("%d")
  119. );
  120. $subjectLayout = $subjectTemplate->get_template(
  121. 'mail/cron_remind_course_expiration_subject.tpl'
  122. );
  123. $bodyTemplate = new Template(null, false, false, false, false, false);
  124. $bodyTemplate->assign('complete_user_name', $userCompleteName);
  125. $bodyTemplate->assign('session_name', $session['name']);
  126. $bodyTemplate->assign(
  127. 'session_access_end_date',
  128. $session['access_end_date']
  129. );
  130. $bodyTemplate->assign(
  131. 'remaining_days',
  132. $daysRemaining->format("%d")
  133. );
  134. $bodyLayout = $bodyTemplate->get_template(
  135. 'mail/cron_remind_course_expiration_body.tpl'
  136. );
  137. api_mail_html(
  138. $userCompleteName,
  139. $user['email'],
  140. $subjectTemplate->fetch($subjectLayout),
  141. $bodyTemplate->fetch($bodyLayout),
  142. $administrator['completeName'],
  143. $administrator['email']
  144. );
  145. echo "Email sent to $userCompleteName (".$user['email'].")\n";
  146. echo "Session: ".$session['name']."\n";
  147. echo "Date end: ".$session['access_end_date']."\n";
  148. echo "Days remaining: ".$daysRemaining->format("%d")."\n\n";
  149. }
  150. echo "======================================================================\n\n";
  151. }
  152. } else {
  153. echo "No users to be reminded\n";
  154. }