[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [websites/season-kde-org] /: Add notification mails module
From:       Sayak Banerjee <sayakb () kde ! org>
Date:       2013-08-31 19:58:30
Message-ID: E1VFrJW-0005Ql-TX () scm ! kde ! org
[Download RAW message or body]

Git commit aea9f489bd6d8332521941d811b2fc85537be742 by Sayak Banerjee.
Committed on 31/08/2013 at 19:58.
Pushed by sayakb into branch 'master'.

Add notification mails module

M  +1    -0    classes/class_module.php
M  +33   -231  cron.php
M  +16   -0    lang/en-gb.php
C  +68   -71   modules/mod_notifications.php [from: cron.php - 072% similarity]
M  +6    -0    skins/neverland/html/tpl_header.html
M  +3    -3    skins/neverland/html/tpl_manage_programs_item.html
A  +49   -0    skins/neverland/html/tpl_notifications.html
A  +25   -0    skins/neverland/html/tpl_queue_item.html

http://commits.kde.org/websites/season-kde-org/aea9f489bd6d8332521941d811b2fc85537be742


diff --git a/classes/class_module.php b/classes/class_module.php
index 13ed54e..21d0546 100755
--- a/classes/class_module.php
+++ b/classes/class_module.php
@@ -28,6 +28,7 @@ class module
             array('name' => 'edit_templates',    'access' => 'a'),
             array('name' => 'user_ban',          'access' => 'a'),
             array('name' => 'manage_programs',   'access' => 'a'),
+            array('name' => 'notifications',     'access' => 'a'),
         );
     }
 
diff --git a/cron.php b/cron.php
index 95cb56e..eac1b37 100755
--- a/cron.php
+++ b/cron.php
@@ -5,254 +5,56 @@
 * @copyright (c) 2012 KDE. All rights reserved.
 */
 
-// We are in CLI, assume OS cron is being used
-if (php_sapi_name() == 'cli')
-{
-    define('IN_PANDORA', true);
-
-    // We need longer execution time
-    set_time_limit(3600);
-
-    // Invoke required files
-    include_once('init.php');
+if (!defined('IN_PANDORA')) exit;
 
-    // Set default for sending mail
-    $name = $config->ldap_fullname;
-    $mail = $config->ldap_mail;
-    $mentor_name = $lang->get('no_mentor');
-
-    // Get all entries from the queue and their corresponding program data
-    $sql = "SELECT prg.id as program_id, " .
-           "       prg.title as program_title, " .
-           "       prg.dl_mentor as program_deadline, " .
-           "       prg.end_time as program_complete, " .
-           "       que.deadline as deadline_flag, " .
-           "       que.complete as complete_flag " .
-           "FROM {$db->prefix}queue que " .
-           "LEFT JOIN {$db->prefix}programs prg " .
-           "ON que.program_id = prg.id " .
-           "WHERE (prg.dl_mentor < TIMESTAMP(CURRENT_DATE()) " .
-           "OR prg.end_time < TIMESTAMP(CURRENT_DATE()))";
-    $program_data = $db->query($sql);
+// Use cache for cron
+if ($cache->is_available)
+{
+    // Get last run time
+    $last_run = $cache->get('last_run', 'cron');
 
-    // Traverse through each program
-    foreach ($program_data as $program)
+    if (!$last_run)
     {
-        // Set original deadline and complete flags
-        $deadline = $program['deadline_flag'];
-        $complete = $program['complete_flag'];
-
-        // All projects for this program
-        $sql = "SELECT id as project_id, " .
-               "       title as project_title, " .
-               "       is_accepted " .
-               "FROM {$db->prefix}projects " .
-               "WHERE program_id = {$program['program_id']}";
-        $project_data = $db->query($sql);
-
-        // Assign program name
-        $email->assign('program_name', $program['program_title']);
-
-        // Traverse through each project
-        foreach ($project_data as $project)
-        {
-            $project['student'] = '';
-            $project['mentor'] = '';
-            $project['passed'] = false;
-
-            // Get student data
-            $sql = "SELECT username, passed " .
-                   "FROM {$db->prefix}participants " .
-                   "WHERE program_id = {$program['program_id']} " .
-                   "AND project_id = {$project['project_id']} " .
-                   "AND role = 's'";
-            $student_data = $db->query($sql, true);
-
-            if ($student_data != null)
-            {
-                $project['student'] = $student_data['username'];
-                $project['passed'] = $student_data['passed'];
-            }
-
-            // Get mentor data
-            $sql = "SELECT username " .
-                   "FROM {$db->prefix}participants " .
-                   "WHERE program_id = {$program['program_id']} " .
-                   "AND project_id = {$project['project_id']} " .
-                   "AND role = 'm'";
-            $mentor_data = $db->query($sql, true);
-
-            if ($mentor_data != null)
-            {
-                $project['mentor'] = $mentor_data['username'];
-            }
-
-            // Get student and mentor data from LDAP
-            $student_data = $user->get_details($project['student'], array($name, \
                $mail));
-            $mentor_data  = $user->get_details($project['mentor'], array($name, \
                $mail));
-
-            // Set student data
-            if ($student_data !== false)
-            {
-                $student      = $project['student'];
-                $student_to   = $student_data[$name][0];
-                $student_name = "{$student_data[$name][0]} \
                &lt;{$student_data[$mail][0]}&gt;";
-                $student_mail = $student_data[$mail][0];
-            }
-
-            // Set mentor data
-            if ($mentor_data !== false)
-            {
-                $mentor      = $project['mentor'];
-                $mentor_to   = $mentor_data[$name][0];
-                $mentor_name = "{$mentor_data[$name][0]} \
                &lt;{$mentor_data[$mail][0]}&gt;";
-                $mentor_mail = $mentor_data[$mail][0];
-            }
-
-            // Assign data needed for the email
-            $email->assign(array(
-                'project_name'      => $project['project_title'],
-                'student_name'      => $student_name,
-                'mentor_name'       => $mentor_name,
-                'project_url'       => \
                "{$config->http_host}?q=view_projects&amp;prg=" .
-                                       \
                "{$program['program_id']}&amp;p={$project['project_id']}",
-            ));
-
-            // Send out status mails on deadline
-            if ($program['program_deadline'] < $core->timestamp && \
                $project['is_accepted'] != -1 && $deadline == 0)
-            {
-                // Output status to console
-                echo '[' . date('r') . '] ' . $lang->get('sending_status') . " \
                #{$project['project_id']} ";
-
-                // Set the template based on the status
-                $status = $project['is_accepted'] == 1 ? 'accept' : 'reject';
-
-                // Set initial flag values
-                $success_student = true;
-                $success_mentor  = true;
-
-                if ($student_data !== false && !empty($student_mail))
-                {
-                    $email->assign('recipient', $student_to);
-                    $success_student = $email->send($student_mail, \
                $lang->get('subject_status'), $status);
-                    sleep(2);
-                }
-
-                if ($mentor_data !== false && !empty($mentor_mail))
-                {
-                    $email->assign('recipient', $mentor_to);
-                    $success_mentor = $email->send($mentor_mail, \
                $lang->get('subject_status'), $status);
-                    sleep(2);
-                }
-
-                // Determine status for logging
-                $log_status = $success_student && $success_mentor ? \
                $lang->get('status_ok') : $lang->get('status_error');
-                echo "{$log_status}\n";
-            }
-
-            // Send out result mails on program completion
-            if ($program['program_complete'] < $core->timestamp && \
                $project['passed'] != -1 && $complete == 0)
-            {
-                // Output status to console
-                echo '[' . date('r') . '] ' . $lang->get('sending_result') . " \
                #{$project['project_id']} ";
-
-                // Set the template based on the status
-                $status = $project['passed'] == 1 ? 'pass' : 'fail';
-
-                // Set initial flag status
-                $success = true;
-
-                if ($student_data !== false && !empty($student_mail))
-                {
-                    $email->assign('recipient', $student_to);
-                    $success = $email->send($student_mail, \
                $lang->get('subject_result'), $status);
-                    sleep(2);
-                }
-
-                // Determine status for logging
-                $log_status = $success ? $lang->get('status_ok') : \
                $lang->get('status_error');
-                echo "{$log_status}\n";
-            }
-        }
-
-        // Set new flag values
-        $deadline = $program['program_deadline'] < $core->timestamp ? 1 : 0;
-        $complete = $program['program_complete'] < $core->timestamp ? 1 : 0;
-
-        // Update the queue entry if at least one flag is still unset
-        if ($deadline == 0 || $complete == 0)
-        {
-            $sql = "UPDATE {$db->prefix}queue " .
-                   "SET deadline = {$deadline}, " .
-                   "    complete = {$complete} " .
-                   "WHERE program_id = {$program['program_id']}";
-            $db->query($sql);
-        }
-
-        // Both flags set, remove the item from queue
-        else
-        {
-            $sql = "DELETE FROM {$db->prefix}queue " .
-                   "WHERE program_id = {$program['program_id']}";
-            $db->query($sql);
-        }
+        $last_run = 0;
     }
 }
 
-// We are in web more, utilize inbuilt cron feature
+// Use DB for cron
 else
 {
-    if (!defined('IN_PANDORA')) exit;
+    // Get last run time
+    $sql = "SELECT timestamp " .
+           "FROM {$db->prefix}cron";
+    $row = $db->query($sql, true);
 
-    // Use cache for cron
-    if ($cache->is_available)
+    if ($row != null)
     {
-        // Get last run time
-        $last_run = $cache->get('last_run', 'cron');
-
-        if (!$last_run)
-        {
-            $last_run = 0;
-        }
+        $last_run = $row['timestamp'];
     }
-
-    // Use DB for cron
     else
     {
-        // Get last run time
-        $sql = "SELECT timestamp " .
-               "FROM {$db->prefix}cron";
-        $row = $db->query($sql, true);
-
-        if ($row != null)
-        {
-            $last_run = $row['timestamp'];
-        }
-        else
-        {
-            $last_run = 0;
-        }
+        $last_run = 0;
     }
+}
 
-    // Check the time difference
-    if (($core->timestamp - $last_run) > 60)
+// Check the time difference
+if (($core->timestamp - $last_run) > 60)
+{
+    // Update new run time
+    if ($cache->is_available)
     {
-        // Update new run time
-        if ($cache->is_available)
-        {
-            $cache->put('last_run', $core->timestamp, 'cron');
-        }
-        else
-        {
-            $sql = "UPDATE {$db->prefix}cron " .
-                   "SET timestamp = {$core->timestamp}";
-            $db->query($sql);
-        }
-
-        // Cron tasks
-        $cache->purge('users');
-        $db->query("DELETE FROM {$db->prefix}session WHERE timestamp < \
{$user->max_age}"); +        $cache->put('last_run', $core->timestamp, 'cron');
     }
+    else
+    {
+        $sql = "UPDATE {$db->prefix}cron " .
+               "SET timestamp = {$core->timestamp}";
+        $db->query($sql);
+    }
+
+    // Cron tasks
+    $cache->purge('users');
+    $db->query("DELETE FROM {$db->prefix}session WHERE timestamp < \
{$user->max_age}");  }
 
 ?>
diff --git a/lang/en-gb.php b/lang/en-gb.php
index 3d7e7cf..06b796f 100755
--- a/lang/en-gb.php
+++ b/lang/en-gb.php
@@ -220,6 +220,22 @@ $lang_data = array(
     'role_g'                => 'Guest',
     'no_participants'       => 'This programs has no participants',
     'projects'              => 'Projects',
+
+    /* Module: notifications */
+    'notification_mails'    => 'Notification mails',
+    'mail_queue'            => 'Queued emails',
+    'mail_queue_exp'        => 'Each program has two types of emails: acceptance \
emails and result emails. Acceptance ' . +                               'emails are \
sent out to students and mentors only after the mentor deadline is reached ' . +      \
'while result emails are sent out once the program has ended. Use the <b>Send \
notifications</b> ' . +                               'button to trigger these mails. \
No matter when you trigger them, the mails will be sent ' . +                         \
'only past these deadlines.', +    'acceptance_mails'      => 'Acceptance emails',
+    'result_mails'          => 'Result emails',
+    'queue_empty'           => 'There are no items in the queue',
+    'send_notifications'    => 'Send notifications',
+    'sendmail_output'       => 'Sendmail output',
+    'processing_program'    => 'Processing program #',
+    'program_no_mail'       => 'No mails were sent for this program',
 );
 
 ?>
diff --git a/cron.php b/modules/mod_notifications.php
similarity index 72%
copy from cron.php
copy to modules/mod_notifications.php
index 95cb56e..2ceeffc 100755
--- a/cron.php
+++ b/modules/mod_notifications.php
@@ -5,22 +5,22 @@
 * @copyright (c) 2012 KDE. All rights reserved.
 */
 
-// We are in CLI, assume OS cron is being used
-if (php_sapi_name() == 'cli')
-{
-    define('IN_PANDORA', true);
+if (!defined('IN_PANDORA')) exit;
 
-    // We need longer execution time
-    set_time_limit(3600);
+// We need longer execution time
+set_time_limit(3600);
 
-    // Invoke required files
-    include_once('init.php');
+// Get the program's participant list
+$db->escape($program_id);
 
-    // Set default for sending mail
-    $name = $config->ldap_fullname;
-    $mail = $config->ldap_mail;
-    $mentor_name = $lang->get('no_mentor');
+// Set default for sending mail
+$name = $config->ldap_fullname;
+$mail = $config->ldap_mail;
+$mentor_name = $lang->get('no_mentor');
+$output = '';
 
+if (isset($_POST['process']))
+{
     // Get all entries from the queue and their corresponding program data
     $sql = "SELECT prg.id as program_id, " .
            "       prg.title as program_title, " .
@@ -32,7 +32,8 @@ if (php_sapi_name() == 'cli')
            "LEFT JOIN {$db->prefix}programs prg " .
            "ON que.program_id = prg.id " .
            "WHERE (prg.dl_mentor < TIMESTAMP(CURRENT_DATE()) " .
-           "OR prg.end_time < TIMESTAMP(CURRENT_DATE()))";
+           "OR prg.end_time < TIMESTAMP(CURRENT_DATE())) ";
+           "AND prg.is_active = 1";
     $program_data = $db->query($sql);
 
     // Traverse through each program
@@ -53,6 +54,10 @@ if (php_sapi_name() == 'cli')
         // Assign program name
         $email->assign('program_name', $program['program_title']);
 
+        // Add log line
+        $output .= '[' . date('r') . '] ' . $lang->get('processing_program') . \
"{$program['program_id']}...\n"; +        $processed = false;
+
         // Traverse through each project
         foreach ($project_data as $project)
         {
@@ -122,7 +127,7 @@ if (php_sapi_name() == 'cli')
             if ($program['program_deadline'] < $core->timestamp && \
$project['is_accepted'] != -1 && $deadline == 0)  {
                 // Output status to console
-                echo '[' . date('r') . '] ' . $lang->get('sending_status') . " \
#{$project['project_id']} "; +                $output .= '[' . date('r') . '] ' . \
$lang->get('sending_status') . " #{$project['project_id']}... ";  
                 // Set the template based on the status
                 $status = $project['is_accepted'] == 1 ? 'accept' : 'reject';
@@ -135,26 +140,25 @@ if (php_sapi_name() == 'cli')
                 {
                     $email->assign('recipient', $student_to);
                     $success_student = $email->send($student_mail, \
                $lang->get('subject_status'), $status);
-                    sleep(2);
                 }
 
                 if ($mentor_data !== false && !empty($mentor_mail))
                 {
                     $email->assign('recipient', $mentor_to);
                     $success_mentor = $email->send($mentor_mail, \
                $lang->get('subject_status'), $status);
-                    sleep(2);
                 }
 
                 // Determine status for logging
                 $log_status = $success_student && $success_mentor ? \
                $lang->get('status_ok') : $lang->get('status_error');
-                echo "{$log_status}\n";
+                $output .= "{$log_status}\n";
+                $processed = true;
             }
 
             // Send out result mails on program completion
             if ($program['program_complete'] < $core->timestamp && \
$project['passed'] != -1 && $complete == 0)  {
                 // Output status to console
-                echo '[' . date('r') . '] ' . $lang->get('sending_result') . " \
#{$project['project_id']} "; +                $output .= '[' . date('r') . '] ' . \
$lang->get('sending_result') . " #{$project['project_id']}... ";  
                 // Set the template based on the status
                 $status = $project['passed'] == 1 ? 'pass' : 'fail';
@@ -166,12 +170,12 @@ if (php_sapi_name() == 'cli')
                 {
                     $email->assign('recipient', $student_to);
                     $success = $email->send($student_mail, \
                $lang->get('subject_result'), $status);
-                    sleep(2);
                 }
 
                 // Determine status for logging
                 $log_status = $success ? $lang->get('status_ok') : \
                $lang->get('status_error');
-                echo "{$log_status}\n";
+                $output .= "{$log_status}\n";
+                $processed = true;
             }
         }
 
@@ -196,63 +200,56 @@ if (php_sapi_name() == 'cli')
                    "WHERE program_id = {$program['program_id']}";
             $db->query($sql);
         }
-    }
-}
-
-// We are in web more, utilize inbuilt cron feature
-else
-{
-    if (!defined('IN_PANDORA')) exit;
 
-    // Use cache for cron
-    if ($cache->is_available)
-    {
-        // Get last run time
-        $last_run = $cache->get('last_run', 'cron');
-
-        if (!$last_run)
+        // No mails were processed
+        if (!$processed)
         {
-            $last_run = 0;
+            $output .= '[' . date('r') . '] ' . $lang->get('program_no_mail');
         }
-    }
-
-    // Use DB for cron
-    else
-    {
-        // Get last run time
-        $sql = "SELECT timestamp " .
-               "FROM {$db->prefix}cron";
-        $row = $db->query($sql, true);
 
-        if ($row != null)
-        {
-            $last_run = $row['timestamp'];
-        }
-        else
-        {
-            $last_run = 0;
-        }
+        $output .= "\n\n";
     }
+}
 
-    // Check the time difference
-    if (($core->timestamp - $last_run) > 60)
-    {
-        // Update new run time
-        if ($cache->is_available)
-        {
-            $cache->put('last_run', $core->timestamp, 'cron');
-        }
-        else
-        {
-            $sql = "UPDATE {$db->prefix}cron " .
-                   "SET timestamp = {$core->timestamp}";
-            $db->query($sql);
-        }
-
-        // Cron tasks
-        $cache->purge('users');
-        $db->query("DELETE FROM {$db->prefix}session WHERE timestamp < \
                {$user->max_age}");
-    }
+// Get the queue data
+$sql = "SELECT prg.id as program_id, " .
+       "       prg.title as program_title, " .
+       "       que.deadline as deadline_flag, " .
+       "       que.complete as complete_flag " .
+       "FROM {$db->prefix}queue que " .
+       "LEFT JOIN {$db->prefix}programs prg " .
+       "ON que.program_id = prg.id " .
+       "WHERE prg.is_active = 1";
+$queue_data = $db->query($sql);
+
+// Populate the queue
+$queue_items = '';
+
+foreach ($queue_data as $queue)
+{
+    $skin->assign(array(
+        'program_id'                 => $queue['program_id'],
+        'program_title'              => $queue['program_title'],
+        'program_deadline_sent'      => $skin->visibility($queue['deadline_flag'] == \
1), +        'program_deadline_pending'   => \
$skin->visibility($queue['deadline_flag'] == 0), +        'program_complete_sent'     \
=> $skin->visibility($queue['complete_flag'] == 1), +        \
'program_complete_pending'   => $skin->visibility($queue['complete_flag'] == 0), +    \
)); +
+    $queue_items .= $skin->output('tpl_queue_item');
 }
 
+// Assign final skin data
+$skin->assign(array(
+    'notify_output'        => nl2br(trim($output)),
+    'queue_items'          => $queue_items,
+    'output_visibility'    => $skin->visibility(empty($output), true),
+    'queue_visibility'     => $skin->visibility(empty($queue_items), true),
+    'notice_visibility'    => $skin->visibility(empty($queue_items)),
+));
+
+// Output the module
+$module_title = $lang->get('notifications');
+$module_data = $skin->output('tpl_notifications');
+
 ?>
diff --git a/skins/neverland/html/tpl_header.html \
b/skins/neverland/html/tpl_header.html index 2bb46d9..4b77198 100755
--- a/skins/neverland/html/tpl_header.html
+++ b/skins/neverland/html/tpl_header.html
@@ -86,6 +86,12 @@ ocalization variable
                                 </li>
 
                                 <li>
+                                    <a href="?q=notifications">
+                                        {{notification_mails}}
+                                    </a>
+                                </li>
+
+                                <li>
                                     <a href="?q=edit_templates">
                                         {{edit_templates}}
                                     </a>
diff --git a/skins/neverland/html/tpl_manage_programs_item.html \
b/skins/neverland/html/tpl_manage_programs_item.html index 96f24eb..3559daf 100755
--- a/skins/neverland/html/tpl_manage_programs_item.html
+++ b/skins/neverland/html/tpl_manage_programs_item.html
@@ -2,7 +2,7 @@
     <td>
         [[program_title]]
     </td>
-    
+
     <td>
         [[program_description]]
     </td>
@@ -16,10 +16,10 @@
             <i class="icon icon-remove"></i>
         </div>
     </td>
-    
+
     <td class="align-center">
         <a href="?q=manage_programs&amp;a=editor&amp;p=[[program_id]]">
             <i class="icon-pencil"></i>
         </a>
     </td>
-</tr>
\ No newline at end of file
+</tr>
diff --git a/skins/neverland/html/tpl_notifications.html \
b/skins/neverland/html/tpl_notifications.html new file mode 100755
index 0000000..318ae51
--- /dev/null
+++ b/skins/neverland/html/tpl_notifications.html
@@ -0,0 +1,49 @@
+<h1>{{notification_mails}}</h1>
+<hr class="hr-head" />
+
+<div class="alert alert-info">
+    {{mail_queue_exp}}
+</div>
+
+<div class="pull-right">
+    <input type="submit" name="process" class="btn btn-primary" \
value="{{send_notifications}}" /> +</div>
+
+<h4>{{mail_queue}}</h4>
+
+<div class="alert alert-info [[notice_visibility]]">
+    {{queue_empty}}
+</div>
+
+<div class="[[queue_visibility]]">
+    <table class="table table-striped">
+        <thead>
+            <tr>
+                <th>
+                    {{program_title}}
+                </th>
+
+                <th class="align-center">
+                    {{acceptance_mails}}
+                </th>
+
+                <th class="align-center">
+                    {{result_mails}}
+                </th>
+            </tr>
+        </thead>
+
+        <tbody>
+            [[queue_items]]
+        </tbody>
+    </table>
+</div>
+
+<div class="[[output_visibility]]">
+    <br />
+    <h4>{{sendmail_output}}</h4>
+
+    <div class="well">
+        [[notify_output]]
+    </div>
+</div>
diff --git a/skins/neverland/html/tpl_queue_item.html \
b/skins/neverland/html/tpl_queue_item.html new file mode 100755
index 0000000..fa7aa91
--- /dev/null
+++ b/skins/neverland/html/tpl_queue_item.html
@@ -0,0 +1,25 @@
+<tr>
+    <td>
+        <a href="?q=program_home&prg=[[program_id]]">[[program_title]]</a>
+    </td>
+
+    <td>
+        <div class="align-center [[program_deadline_sent]]">
+            <i class="icon icon-ok"></i>
+        </div>
+
+        <div class="align-center [[program_deadline_pending]]">
+            <i class="icon icon-remove"></i>
+        </div>
+    </td>
+
+    <td>
+        <div class="align-center [[program_complete_sent]]">
+            <i class="icon icon-ok"></i>
+        </div>
+
+        <div class="align-center [[program_complete_pending]]">
+            <i class="icon icon-remove"></i>
+        </div>
+    </td>
+</tr>


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic