[MKDoc-commit] [ufi] Patch from Sam - contains a new script, tools/ufi_survey_reminder,

chris at mkdoc.demon.co.uk chris at mkdoc.demon.co.uk
Mon Mar 13 10:01:25 GMT 2006


Log Message:
-----------
[ufi] Patch from Sam - contains a new script, tools/ufi_survey_reminder, which
sends reminder emails for surveys.  This should be run from cron daily,
probably just after midnight since it will send email for surveys that are
within 24 hours of ending. For now it will only email people in the Portal and
Portal Administrator groups.

Tags:
----
mkdoc-1-6

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

-------------- next part --------------
--- /dev/null
+++ tools/ufi_survey_mailer.pl
@@ -0,0 +1,151 @@
+#!/usr/bin/perl
+
+# ------------------------------------------------------------------
+# ufi_survey_mailer.pl
+# ------------------------------------------------------------------
+# Author : Sam Tregar
+# Copyright : (c) MKDoc Holdings Ltd, 2006
+#
+# This file is part of MKDoc. 
+# 
+# MKDoc is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+# 
+# MKDoc is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with MKDoc; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+#
+# ------------------------------------------------------------------
+
+# This script sends reminder emails to centre administrators when a
+# survey is 24 hours from finished and they haven't filled it out.  It
+# should be run once a day from cron.  NOTE: running it more often
+# than once a day will result in duplicate emails!
+
+package UFI::MI::SurveyReminder;
+use strict;
+use warnings;
+use Text::Wrap;
+use MKDoc;
+use Encode;
+use base qw /flo::Plugin/;
+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 (
+        file => '/ufi/mi/survey_reminder',
+        language => $self->language() || 'en',
+    );
+    
+    $mail->send (
+	self       => $self,
+	__input__  => 'XML',
+	__output__ => 'XML',
+    );
+    
+    return 1; 
+}
+
+package main;
+use strict;
+use warnings;
+use Carp;
+use MKDoc::Handler::Initialize;
+use Text::Wrap;
+use DateTime;
+use flo::Record::Editor;
+
+MKDoc::Handler::Initialize::handler();
+
+my $dbh  = lib::sql::DBH->get();
+main();
+
+sub main {
+    my $current = current_survey();
+    exit unless $current;
+    my $diff = $current->{end_date} - DateTime->now();
+    exit unless $diff->delta_days == 0;
+
+    print "Sending reminders for survey ending $current->{end_date} to:\n\n";
+    my $ids = needs_reminder($current->{id});
+    foreach my $editor_id (@$ids) {
+        my $user = flo::Record::Editor->load($editor_id);
+
+        # FIX: remove this when testing is over
+        my ($allowed) = $dbh->selectrow_array(
+         'SELECT 1 FROM Editor_Grp, Grp
+          WHERE Editor_Grp.Grp_ID = Grp.ID AND 
+             Grp.Name IN (?,?) AND 
+             Editor_ID = ?', 
+         undef, "Portal", "Portal Administrators", $editor_id);
+        next unless $allowed;
+
+        print "    $user->{Email}\n";
+
+        UFI::MI::SurveyReminder->new(user => $user)->send();
+    }
+}
+
+# get current survey period if one exists
+sub current_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() >= Start_Date AND 
+                                   NOW() <= End_Date');
+    return undef unless $id;
+    
+    return { id => $id, 
+             start_date => _parse_mysql_date($start_date),
+             end_date   => _parse_mysql_date($end_date) };
+}
+
+# get a DateTime from a MySQL DATE
+sub _parse_mysql_date {
+    my $date = shift;
+    my ($year, $month, $day) = split('-', $date);
+    return DateTime->new(year => $year, month => $month, day => $day);
+}
+
+# find users attached to centres without surveys filed for a period
+sub needs_reminder {
+    my $id = shift;
+
+    # 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, $id);
+
+    # find editors of missing ones
+    my $missing = $dbh->selectcol_arrayref(
+      'SELECT DISTINCT(Editor_ID) 
+       FROM Editor_Centre 
+       WHERE Centre_ID NOT IN (' . join(',', ('?') x @$exists) . ')', 
+       undef, @$exists);
+
+    return $missing || [];
+}
+
+
+1;
+
+
+__END__


More information about the MKDoc-commit mailing list