course_finished.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Cron for send a email when the course are finished.
  5. *
  6. * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
  7. *
  8. * @package chamilo.cron
  9. */
  10. require_once __DIR__.'/../inc/global.inc.php';
  11. if (php_sapi_name() != 'cli') {
  12. exit; //do not run from browser
  13. }
  14. $isActive = api_get_setting('cron_remind_course_expiration_activate') === 'true';
  15. if (!$isActive) {
  16. exit;
  17. }
  18. $endDate = new DateTime('now', new DateTimeZone('UTC'));
  19. $endDate = $endDate->format('Y-m-d');
  20. $entityManager = Database::getManager();
  21. $sessionRepo = $entityManager->getRepository('ChamiloCoreBundle:Session');
  22. $accessUrlRepo = $entityManager->getRepository('ChamiloCoreBundle:AccessUrl');
  23. $sessions = $sessionRepo->createQueryBuilder('s')
  24. ->where('s.accessEndDate LIKE :date')
  25. ->setParameter('date', "$endDate%")
  26. ->getQuery()
  27. ->getResult();
  28. if (empty($sessions)) {
  29. echo "No sessions finishing today $endDate".PHP_EOL;
  30. exit;
  31. }
  32. $administrator = [
  33. 'complete_name' => api_get_person_name(
  34. api_get_setting('administratorName'),
  35. api_get_setting('administratorSurname'),
  36. null,
  37. PERSON_NAME_EMAIL_ADDRESS
  38. ),
  39. 'email' => api_get_setting('emailAdministrator'),
  40. ];
  41. foreach ($sessions as $session) {
  42. $sessionUsers = $session->getUsers();
  43. if (empty($sessionUsers)) {
  44. echo 'No users to send mail'.PHP_EOL;
  45. exit;
  46. }
  47. foreach ($sessionUsers as $sessionUser) {
  48. $user = $sessionUser->getUser();
  49. $subjectTemplate = new Template(null, false, false, false, false, false);
  50. $subjectTemplate->assign('session_name', $session->getName());
  51. $subjectLayout = $subjectTemplate->get_template(
  52. 'mail/cron_course_finished_subject.tpl'
  53. );
  54. $bodyTemplate = new Template(null, false, false, false, false, false);
  55. $bodyTemplate->assign('complete_user_name', UserManager::formatUserFullName($user));
  56. $bodyTemplate->assign('session_name', $session->getName());
  57. $bodyLayout = $bodyTemplate->get_template(
  58. 'mail/cron_course_finished_body.tpl'
  59. );
  60. api_mail_html(
  61. UserManager::formatUserFullName($user),
  62. $user->getEmail(),
  63. $subjectTemplate->fetch($subjectLayout),
  64. $bodyTemplate->fetch($bodyLayout),
  65. $administrator['complete_name'],
  66. $administrator['email']
  67. );
  68. echo '============'.PHP_EOL;
  69. echo "Email sent to: ".UserManager::formatUserFullName($user)." ({$user->getEmail()})".PHP_EOL;
  70. echo "Session: {$session->getName()}".PHP_EOL;
  71. echo "End date: {$session->getAccessEndDate()->format('Y-m-d h:i')}".PHP_EOL;
  72. }
  73. }