File Coverage

blib/lib/Labyrinth/Plugin/Survey/Announce.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 16 16 100.0


line stmt bran cond sub pod time code
1             package Labyrinth::Plugin::Survey::Announce;
2              
3 2     2   8889 use warnings;
  2         4  
  2         72  
4 2     2   11 use strict;
  2         3  
  2         69  
5              
6 2     2   10 use vars qw($VERSION);
  2         3  
  2         120  
7             $VERSION = '0.05';
8              
9             =head1 NAME
10              
11             Labyrinth::Plugin::Survey::Announce - YAPC Surveys' announcements plugin for Labyrinth framework
12              
13             =head1 DESCRIPTION
14              
15             Provides all the announcement handling functionality for YAPC Surveys.
16              
17             =cut
18              
19             # -------------------------------------
20             # Library Modules
21              
22 2     2   10 use base qw(Labyrinth::Plugin::Base);
  2         4  
  2         1496  
23              
24             use Labyrinth::Audit;
25             use Labyrinth::DBUtils;
26             use Labyrinth::DTUtils;
27             use Labyrinth::Mailer;
28             use Labyrinth::MLUtils;
29             use Labyrinth::Support;
30             use Labyrinth::Users;
31             use Labyrinth::Variables;
32              
33             use HTML::Entities;
34             use Time::Piece;
35              
36             # -------------------------------------
37             # Variables
38              
39             # type: 0 = optional, 1 = mandatory
40             # html: 0 = none, 1 = text, 2 = textarea, 3 = full legal html
41              
42             my %fields = (
43             announceid => { type => 0, html => 0 },
44             hFrom => { type => 1, html => 2 }, # can contain "" which looks like a HTML tag
45             hSubject => { type => 1, html => 1 },
46             body => { type => 1, html => 2 },
47             publish => { type => 1, html => 0 },
48             );
49              
50             my (@mandatory,@allfields);
51             for(keys %fields) {
52             push @mandatory, $_ if($fields{$_}->{type});
53             push @allfields, $_;
54             }
55              
56             my @savefields = qw(hFrom hSubject body publish);
57             my $INDEXKEY = 'announceid';
58             my $ALLSQL = 'GetAnnounces';
59             my $SAVESQL = 'SaveAnnounce';
60             my $ADDSQL = 'AddAnnounce';
61             my $GETSQL = 'GetAnnounceByID';
62             my $DELETESQL = 'DeleteAnnounce';
63             my $LEVEL = ADMIN;
64              
65             my %adddata = (
66             announceid => 0,
67             hFrom => '',
68             hTo => '',
69             hSubject => '',
70             body => '',
71             );
72              
73             # -------------------------------------
74             # The Subs
75              
76             =head1 PUBLIC INTERFACE METHODS
77              
78             =head2 General Management Methods
79              
80             =over 4
81              
82             =item Admin
83              
84             Lists active announcements.
85              
86             =item Add
87              
88             Add an announcement.
89              
90             =item Edit
91              
92             Edit an announcement.
93              
94             =item Save
95              
96             Save an announcement.
97              
98             =item Delete
99              
100             Delete one or more announcements.
101              
102             =back
103              
104             =cut
105              
106             sub Admin {
107             return unless(AccessUser($LEVEL));
108             if($cgiparams{doaction}) {
109             if($cgiparams{doaction} eq 'Delete' ) { Delete(); }
110             }
111             my @rows = $dbi->GetQuery('hash',$ALLSQL);
112             $tvars{data} = \@rows if(@rows);
113             }
114              
115             sub Add {
116             return unless AccessUser($LEVEL);
117             $tvars{data}{ddpublish} = PublishSelect(undef,1);
118             }
119              
120             sub Edit {
121             return unless AccessUser($LEVEL);
122             return unless AuthorCheck($GETSQL,$INDEXKEY,$LEVEL);
123             $tvars{data}{ddpublish} = PublishSelect($tvars{data}{publish},1);
124              
125             my @user = $dbi->GetQuery('array','CountConfirmedUsers');
126             my @sent = $dbi->GetQuery('array','AnnounceSent',$cgiparams{$INDEXKEY});
127             my @wait = $dbi->GetQuery('array','AnnounceNotSent',$cgiparams{$INDEXKEY});
128             my @done = $dbi->GetQuery('hash','AdminSurveys');
129              
130             if(@user) {
131             if(@sent) {
132             $tvars{data}{sent} = $sent[0]->[0];
133             $tvars{data}{unsent} = $wait[0]->[0];
134             } else {
135             $tvars{data}{sent} = 0;
136             $tvars{data}{unsent} = $user[0]->[0];
137             }
138             if(@done) {
139             $tvars{data}{done} = scalar(@done);
140             $tvars{data}{undone} = $user[0]->[0] - scalar(@done);
141             } else {
142             $tvars{data}{done} = 0;
143             $tvars{data}{undone} = $user[0]->[0];
144             }
145             } else {
146             $tvars{data}{sent} = 0;
147             $tvars{data}{unsent} = 0;
148             $tvars{data}{done} = 0;
149             $tvars{data}{undone} = 0;
150             }
151             }
152              
153             sub Save {
154             return unless AccessUser($LEVEL);
155             return unless AuthorCheck($GETSQL,$INDEXKEY,$LEVEL);
156             for(keys %fields) {
157             if($fields{$_}->{html} == 1) { $cgiparams{$_} = CleanHTML($cgiparams{$_}) }
158             elsif($fields{$_}->{html} == 2) { $cgiparams{$_} = CleanTags($cgiparams{$_}) }
159             elsif($fields{$_}->{html} == 3) { $cgiparams{$_} = CleanLink($cgiparams{$_}) }
160             }
161             return if FieldCheck(\@allfields,\@mandatory);
162              
163             my @fields = map {$tvars{data}->{$_}} @savefields;
164             if($cgiparams{$INDEXKEY}) {
165             $dbi->DoQuery($SAVESQL,@fields,$cgiparams{$INDEXKEY});
166             } else {
167             $cgiparams{$INDEXKEY} = $dbi->IDQuery($ADDSQL,@fields);
168             }
169              
170             $tvars{thanks} = 1;
171             }
172              
173             sub Delete {
174             return unless AccessUser($LEVEL);
175             my @ids = CGIArray('LISTED');
176             return unless @ids;
177             $dbi->DoQuery($DELETESQL,{ids=>join(",",@ids)});
178             }
179              
180             =head2 Mail Management Methods
181              
182             =over 4
183              
184             =item Resend
185              
186             List users who have previously been sent the selected announcement.
187              
188             =item Unsent
189              
190             List users who have never previously been sent the selected announcement.
191              
192             =item Done
193              
194             List users who have submitted the main conference survey
195              
196             =item Undone
197              
198             List users who have not submitted the main conference survey
199              
200             =item SendOne
201              
202             Send announcement to selected users.
203              
204             =item SendAll
205              
206             Resend announcement to all users.
207              
208             =item SendNew
209              
210             Send announcement to users who have not previously been sent it.
211              
212             =item SendNot
213              
214             Send announcement to users who have not taken the main conference survey.
215              
216             =back
217              
218             =cut
219              
220             sub Resend {
221             return unless AccessUser($LEVEL);
222             return unless AuthorCheck($GETSQL,$INDEXKEY,$LEVEL);
223              
224             $cgiparams{sortname} ||= 'realname';
225              
226             my $sort = "ORDER BY u.$cgiparams{sortname} ";
227             $sort .= $cgiparams{sorttype} ? 'ASC' : 'DESC';
228             $tvars{sorttype} = $cgiparams{sorttype} ? 0 : 1;
229              
230             my @users = $dbi->GetQuery('hash','ListAnnounceSent',{'sort' => $sort},$cgiparams{$INDEXKEY});
231             $tvars{users} = \@users if(@users)
232             }
233              
234             sub Unsent {
235             return unless AccessUser($LEVEL);
236             return unless AuthorCheck($GETSQL,$INDEXKEY,$LEVEL);
237              
238             $cgiparams{sortname} ||= 'realname';
239              
240             my $sort = "ORDER BY u.$cgiparams{sortname} ";
241             $sort .= $cgiparams{sorttype} ? 'ASC' : 'DESC';
242             $tvars{sorttype} = $cgiparams{sorttype} ? 0 : 1;
243              
244             my @users = $dbi->GetQuery('hash','ListAnnounceUnsent',{'sort' => $sort},$cgiparams{$INDEXKEY});
245             $tvars{users} = \@users if(@users);
246             $tvars{sorttype} ||= 0;
247             }
248              
249             sub Done {
250             return unless AccessUser($LEVEL);
251             return unless AuthorCheck($GETSQL,$INDEXKEY,$LEVEL);
252              
253             $cgiparams{sortname} ||= 'realname';
254              
255             my $sort = "ORDER BY u.$cgiparams{sortname} ";
256             $sort .= $cgiparams{sorttype} ? 'ASC' : 'DESC';
257             $tvars{sorttype} = $cgiparams{sorttype} ? 0 : 1;
258              
259             my @users = $dbi->GetQuery('hash','AdminSurveys',{'sort' => $sort});
260             $tvars{users} = \@users if(@users)
261             }
262              
263             sub Undone {
264             return unless AccessUser($LEVEL);
265             return unless AuthorCheck($GETSQL,$INDEXKEY,$LEVEL);
266              
267             $cgiparams{sortname} ||= 'realname';
268              
269             my $sort = "ORDER BY u.$cgiparams{sortname} ";
270             $sort .= $cgiparams{sorttype} ? 'ASC' : 'DESC';
271             $tvars{sorttype} = $cgiparams{sorttype} ? 0 : 1;
272              
273             my @users = $dbi->GetQuery('hash','AdminSurveyNot',{'sort' => $sort});
274             $tvars{users} = \@users if(@users)
275             }
276              
277             sub SendOne {
278             return unless AccessUser($LEVEL);
279             my @ids = CGIArray('LISTED');
280             next unless(@ids);
281             my @users = $dbi->GetQuery('hash','ListSelectedUsers',{ids=>join(",",@ids)});
282             _send_announcement($cgiparams{$INDEXKEY},\@users);
283             }
284              
285             sub SendAll {
286             return unless AccessUser($LEVEL);
287             my @users = $dbi->GetQuery('hash','ListConfirmedUsers');
288             _send_announcement($cgiparams{$INDEXKEY},\@users);
289             }
290              
291             sub SendNew {
292             return unless AccessUser($LEVEL);
293             my @users = $dbi->GetQuery('hash','ListAnnounceUnsent',$cgiparams{$INDEXKEY});
294             _send_announcement($cgiparams{$INDEXKEY},\@users);
295             }
296              
297             sub SendNot {
298             return unless AccessUser($LEVEL);
299             my @users = $dbi->GetQuery('hash','AdminSurveyNot');
300             _send_announcement($cgiparams{$INDEXKEY},\@users);
301             }
302              
303             # -------------------------------------
304             # Private Subs
305              
306             sub _send_announcement {
307             my $id = shift;
308             my $users = shift;
309              
310             my %opts = (
311             template => 'mailer/announce.eml',
312             nowrap => 1
313             );
314              
315             $tvars{gotusers} = scalar(@$users);
316              
317             # get announcement details
318             return unless AuthorCheck($GETSQL,$INDEXKEY,$LEVEL);
319              
320             $tvars{mailsent} = 0;
321             for(qw( yapc_name yapc_host yapc_city yapc_mail yapc_http yapc_surv
322             talks_open survey_open survey_close)) {
323             $settings{$_} =~ s/\s*$//;
324             }
325              
326             for my $user (@$users) {
327             $opts{from} = $tvars{data}{hFrom};
328             $opts{subj} = $tvars{data}{hSubject};
329             $opts{body} = $tvars{data}{body};
330              
331             $user->{realname} = decode_entities($user->{realname} );
332              
333             my $t = localtime;
334             $opts{edate} = $t->strftime("%a, %d %b %Y %H:%M:%S +0100");
335             $opts{email} = $user->{email} or next;
336             $opts{recipient_email} = $user->{email} or next;
337             $opts{ename} = $user->{realname} || '';
338              
339             for my $key (qw(from subj body)) {
340             $opts{$key} =~ s/ENAME/$user->{realname}/g;
341             $opts{$key} =~ s/EMAIL/$user->{email}/g;
342             $opts{$key} =~ s!ECODE!$user->{code}/$user->{userid}!g;
343              
344             $opts{$key} =~ s/YAPC_CONF/$settings{yapc_name}/g;
345             $opts{$key} =~ s/YAPC_HOST/$settings{yapc_host}/g;
346             $opts{$key} =~ s/YAPC_CITY/$settings{yapc_city}/g;
347             $opts{$key} =~ s/YAPC_MAIL/$settings{yapc_mail}/g;
348             $opts{$key} =~ s/YAPC_HTTP/$settings{yapc_http}/g;
349             $opts{$key} =~ s/YAPC_SURV/$settings{yapc_surv}/g;
350             $opts{$key} =~ s/TALK_OPEN/$settings{talks_open}/g;
351             $opts{$key} =~ s/YAPC_OPEN/$settings{survey_open}/g;
352             $opts{$key} =~ s/YAPC_CLOSE/$settings{survey_close}/g;
353              
354             $opts{$key} =~ s/\r/ /g; # a bodge
355             }
356              
357             #use Data::Dumper;
358             #LogDebug("opts=".Dumper(\%opts));
359             MailSend(%opts);
360             $dbi->DoQuery('InsertAnnounceIndex',$cgiparams{$INDEXKEY},$user->{userid},time());
361              
362             # if sent update index
363             $tvars{mailsent}++ if(MailSent());
364             }
365              
366             $tvars{thanks} = $tvars{mailsent} ? 2 : 3;
367             }
368              
369             1;
370              
371             __END__