Browse Source

Merge pull request #773 from AngelFQC/BT9898

Create cron for send email when the course ends - refs BT#9898 #TMI
Angel Fernando Quiroz Campos 9 years ago
parent
commit
7a640aa560

+ 99 - 0
main/cron/course_finished.php

@@ -0,0 +1,99 @@
+<?php
+
+/* For licensing terms, see /license.txt */
+/**
+ * Cron for send a email when the course are finished
+ * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
+ * @package chamilo.cron
+ */
+require_once __DIR__ . '/../inc/global.inc.php';
+
+if (php_sapi_name() != 'cli') {
+    exit; //do not run from browser
+}
+
+$endDate = new DateTime('now', new DateTimeZone('UTC'));
+$endDate = $endDate->format('Y-m-d');
+
+$entityManager = Database::getManager();
+$sessionRepo = $entityManager->getRepository('ChamiloCoreBundle:Session');
+$accessUrlRepo = $entityManager->getRepository('ChamiloCoreBundle:AccessUrl');
+
+$sessions = $sessionRepo->createQueryBuilder('s')
+    ->where('s.accessEndDate LIKE :date')
+    ->setParameter('date', "$endDate%")
+    ->getQuery()
+    ->getResult();
+
+if (empty($sessions)) {
+    echo "No sessions finishing today $endDate" . PHP_EOL;
+    exit;
+}
+
+$administrator = [
+    'complete_name' => api_get_person_name(
+        api_get_setting('administratorName'),
+        api_get_setting('administratorSurname'),
+        null,
+        PERSON_NAME_EMAIL_ADDRESS
+    ),
+    'email' => api_get_setting('emailAdministrator')
+];
+
+foreach ($sessions as $session) {
+    $mailSubject = sprintf(
+        get_lang('MailCronCourseFinishedSubject'),
+        $session->getName()
+    );
+
+    $accessUrls = $accessUrlRepo->createQueryBuilder('au')
+        ->select('au')
+        ->innerJoin(
+            'ChamiloCoreBundle:AccessUrlRelSession',
+            'aus',
+            Doctrine\ORM\Query\Expr\Join::WITH,
+            'au.id = aus.accessUrlId'
+        )
+        ->where('aus.sessionId = :session')
+        ->setParameter('session', $session)
+        ->setMaxResults(1)
+        ->getQuery()
+        ->getResult();
+
+    $accessUrl = current($accessUrls);
+
+    $sessionUsers = $session->getUsers();
+
+    if (empty($sessionUsers)) {
+        echo 'No users to send mail' . PHP_EOL;
+        exit;
+    }
+
+    foreach ($sessionUsers as $sessionUser) {
+        $user = $sessionUser->getUser();
+
+        $mailBody = vsprintf(
+            get_lang('MailCronCourseFinishedBody'),
+            [
+                $user->getCompleteName(),
+                $session->getName(),
+                $accessUrl->getUrl(),
+                api_get_setting("siteName")
+            ]
+        );
+
+        api_mail_html(
+            $user->getCompleteName(),
+            $user->getEmail(),
+            $mailSubject,
+            $mailBody,
+            $administrator['complete_name'],
+            $administrator['email']
+        );
+
+        echo '============' . PHP_EOL;
+        echo "Email sent to: {$user->getCompleteName()} ({$user->getEmail()})" . PHP_EOL;
+        echo "Session: {$session->getName()}" . PHP_EOL;
+        echo "End date: {$session->getAccessEndDate()->format('Y-m-d h:i')}" . PHP_EOL;
+    }
+}

+ 1 - 1
main/cron/remind_course_expiration.php

@@ -93,7 +93,7 @@ if ($usersToBeReminded) {
         );
         foreach ($sessions as $sessionId => $session) {
             $daysRemaining = date_diff($today, date_create($session['access_end_date']));
-            $join = " INNER JOIN ".Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION)."ON id = access_url_id";
+            $join = " INNER JOIN ".Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION)." ON id = access_url_id";
             $result = Database::select(
                 'url',
                 Database::get_main_table(TABLE_MAIN_ACCESS_URL).$join,

+ 6 - 1
main/inc/lib/api.lib.php

@@ -7846,7 +7846,12 @@ function api_mail_html(
         }
     }
     $message = str_replace(array("\n\r", "\n", "\r"), '<br />', $message);
-    $mail->Body = '<html><head></head><body>'.$message.'</body></html>';
+
+    $mailView = new Template(null, false, false, false, false, false);
+    $mailView->assign('content', $message);
+    $layout = $mailView->get_template('mail/mail.tpl');
+
+    $mail->Body = $mailView->fetch($layout);
 
     // Attachment ...
     if (!empty($data_file)) {

+ 7 - 0
main/template/default/mail/footer.tpl

@@ -0,0 +1,7 @@
+<table border="0" cellpadding="10" cellspacing="0" width="100%">
+    <tr>
+        <td style="background-color: #2C3E50; border-bottom: 1px solid #2C3E50; box-shadow: 0px 1px 10px rgba(0, 0, 0, 0.1);">
+            &nbsp;
+        </td>
+    </tr>
+</table>

+ 12 - 0
main/template/default/mail/header.tpl

@@ -0,0 +1,12 @@
+<table border="0" cellpadding="10" cellspacing="0" width="100%">
+    <tr>
+        <td width="245">
+            <a href="{{ _p.web }}">
+                <img src="{{ _p.web_css_theme ~ 'images/header-logo.png' }}" alt="{{ _s.site_name }}">
+            </a>
+        </td>
+        <td width="100%">
+            &nbsp;
+        </td>
+    </tr>
+</table>

+ 33 - 0
main/template/default/mail/mail.tpl

@@ -0,0 +1,33 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+    <head>
+        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+        <title>{{ _s.institution }}</title>
+        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
+    </head>
+    <body style="margin: 0; padding: 0;">
+        <table border="0" cellpadding="0" cellspacing="0" width="100%">
+            <tr>
+                <td>
+                    <table align="center" border="0" cellpadding="0" cellspacing="0" width="600" style="border-collapse: collapse;">
+                        <tr>
+                            <td>
+                                {% include template ~ '/mail/header.tpl' %}
+                            </td>
+                        </tr>
+                        <tr>
+                            <td cellpadding="0" cellspacing="0" style="padding: 40px 10px">
+                                {{ content }}
+                            </td>
+                        </tr>
+                        <tr>
+                            <td>
+                                {% include template ~ '/mail/footer.tpl' %}
+                            </td>
+                        </tr>
+                    </table>
+                </td>
+            </tr>
+        </table>
+    </body>
+</html>