[MKDoc-commit] Implemented summary emails.

sam at mkdoc.demon.co.uk sam at mkdoc.demon.co.uk
Mon Mar 20 05:57:45 GMT 2006


Log Message:
-----------
Implemented summary emails.  Changed reminder emails to send at 48 hours left rather than 24.  Added code to ensure that no duplicate emails will be sent.

Tags:
----
mkdoc-1-6

Modified Files:
--------------
    mkd/tools:
        ufi_survey_mailer.pl

-------------- next part --------------
Index: ufi_survey_mailer.pl
===================================================================
RCS file: /var/spool/cvs/mkd/tools/Attic/ufi_survey_mailer.pl,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -Ltools/ufi_survey_mailer.pl -Ltools/ufi_survey_mailer.pl -u -r1.1.2.1 -r1.1.2.2
--- tools/ufi_survey_mailer.pl
+++ tools/ufi_survey_mailer.pl
@@ -39,14 +39,11 @@
 use Petal::Mail;
 use Carp qw(croak);
 
-sub template_path { 'ufi/mi/survey_reminder' }
-
 sub user {
     my $self = shift;
     return $self->{user};
 }
 
-
 sub send {
     my $self = shift;
     my $mail = new Petal::Mail (
@@ -54,7 +51,37 @@
         language => $self->language() || 'en',
     );
     
-    $mail->send (
+    # $mail->send (
+    print STDERR $mail->process(
+	self       => $self,
+	__input__  => 'XML',
+	__output__ => 'XML',
+    );
+    
+    return 1; 
+}
+
+package UFI::MI::SurveySummary;
+use strict;
+use warnings;
+use Text::Wrap;
+use MKDoc;
+use Encode;
+use base qw /flo::Plugin/;
+use Petal::Mail;
+use Carp qw(croak);
+
+sub admin_email { 'admin at example.com' }
+
+sub send {
+    my $self = shift;
+    my $mail = new Petal::Mail (
+        file => '/ufi/mi/survey_summary',
+        language => $self->language() || 'en',
+    );
+    
+    # $mail->send (
+    print STDERR $mail->process(
 	self       => $self,
 	__input__  => 'XML',
 	__output__ => 'XML',
@@ -78,13 +105,70 @@
 main();
 
 sub main {
-    my $current = current_survey();
-    exit unless $current;
-    my $diff = $current->{end_date} - DateTime->now();
-    exit unless $diff->delta_days == 0;
+    # send reminders when 24 to 48 hours remain
+    if (my $current = current_survey()) {
+        my $diff = $current->{end_date} - DateTime->now();
+        my ($years, $months, $days) = 
+          $diff->in_units('years', 'months', 'days');
+        if ($years == 0 and $months == 0 and $days == 1) {
+            send_reminders($current);
+        }
+    }
+
+    # send the summary when a survey ended yesterday
+    if (my $previous = previous_survey()) {
+        my $diff = DateTime->now() - $previous->{end_date};
+        my ($years, $months, $days) = 
+          $diff->in_units('years', 'months', 'days');
+        if ($years == 0 and $months == 0 and $days == 1) {
+            send_summary($previous);
+        }
+    }
+
+    exit;
+}
+
+sub send_summary {
+    my $survey = shift;
+    print "Sending summary for survey ending $survey->{end_date} to:\n\n";
+
+    my ($total) 
+      = $dbh->selectrow_array('SELECT COUNT(*) FROM Centre_Details');
+    my ($submitted) 
+      = $dbh->selectrow_array('SELECT COUNT(*) FROM MI_Survey 
+                               WHERE MI_Survey_Period_ID = ?', undef, 
+                              $survey->{id});
+
+    # get an exclude list of centres with surveys filed
+    my $exists = $dbh->selectcol_arrayref(
+       'SELECT Centre_ID FROM MI_Survey 
+        WHERE MI_Survey_Period_ID = ?', undef, $survey->{id});
+
+    # find editors of missing ones
+    my $results = $dbh->selectall_arrayref(
+      'SELECT Centre_ID, Centre_Name
+       FROM Centre_Details
+       WHERE Centre_ID NOT IN (' . join(',', ('?') x @$exists) . ')
+       ORDER BY Centre_Name', 
+       undef, @$exists);
+    my @missing = map { { id => $_->[0], name => $_->[1] } } @$results;
+
+    UFI::MI::SurveySummary->new(total      => $total,
+                                submitted  => $submitted,
+                                start_date => $survey->{start_date},
+                                end_date   => $survey->{end_date},
+                                id         => $survey->{id},
+                                missing    => \@missing,
+                               )->send();
+}
+
+sub send_reminders {
+    my $current = shift;
 
     print "Sending reminders for survey ending $current->{end_date} to:\n\n";
     my $ids = needs_reminder($current->{id});
+
+    my %already_mailed;
     foreach my $editor_id (@$ids) {
         my $user = flo::Record::Editor->load($editor_id);
 
@@ -97,12 +181,32 @@
          undef, "Portal", "Portal Administrators", $editor_id);
         next unless $allowed;
 
+        # make sure no-one gets multiple emails, even if they have
+        # multiple accounts
+        next if $already_mailed{$user->{Email}}++;
+
         print "    $user->{Email}\n";
 
         UFI::MI::SurveyReminder->new(user => $user)->send();
     }
 }
 
+# get the previous survey period if one exists
+sub previous_survey {
+    # find the current survey
+    my ($id, $start_date, $end_date) = 
+      $dbh->selectrow_array('SELECT MI_Survey_Period_ID, Start_Date, End_Date
+                             FROM MI_Survey_Period
+                             WHERE NOW() >= End_Date
+                             ORDER BY End_Date DESC
+                             LIMIT 1');
+    return undef unless $id;
+    
+    return { id => $id, 
+             start_date => _parse_mysql_date($start_date),
+             end_date   => _parse_mysql_date($end_date) };
+}
+
 # get current survey period if one exists
 sub current_survey {
     # find the current survey


More information about the MKDoc-commit mailing list