course_finished.php 2.7 KB

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