[system] / branches / rel-2-3-dev / webwork-modperl / lib / WeBWorK / ContentGenerator / Instructor / SendMail.pm Repository:
ViewVC logotype

Annotation of /branches/rel-2-3-dev/webwork-modperl/lib/WeBWorK/ContentGenerator/Instructor/SendMail.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2049 - (view) (download) (as text)
Original Path: trunk/webwork-modperl/lib/WeBWorK/ContentGenerator/Instructor/SendMail.pm

1 : sh002i 1663 ################################################################################
2 :     # WeBWorK Online Homework Delivery System
3 :     # Copyright © 2000-2003 The WeBWorK Project, http://openwebwork.sf.net/
4 : gage 2049 # $CVSHeader: webwork-modperl/lib/WeBWorK/ContentGenerator/Instructor/SendMail.pm,v 1.26 2004/05/07 18:49:40 sh002i Exp $
5 : sh002i 1663 #
6 :     # This program is free software; you can redistribute it and/or modify it under
7 :     # the terms of either: (a) the GNU General Public License as published by the
8 :     # Free Software Foundation; either version 2, or (at your option) any later
9 :     # version, or (b) the "Artistic License" which comes with this package.
10 :     #
11 :     # This program is distributed in the hope that it will be useful, but WITHOUT
12 :     # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 :     # FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the
14 :     # Artistic License for more details.
15 :     ################################################################################
16 :    
17 : gage 1368 package WeBWorK::ContentGenerator::Instructor::SendMail;
18 :     use base qw(WeBWorK::ContentGenerator::Instructor);
19 :    
20 :     =head1 NAME
21 :    
22 :     WeBWorK::ContentGenerator::Instructor::SendMail - Entry point for User-specific data editing
23 :    
24 :     =cut
25 :    
26 :     use strict;
27 :     use warnings;
28 :     use CGI qw();
29 : sh002i 1646 #use HTML::Entities;
30 : gage 1373 use Mail::Sender;
31 : gage 1368
32 : gage 1730 my $REFRESH_RESIZE_BUTTON = "Reorder, Resize and Update"; # handle submit value idiocy
33 : gage 1368 sub initialize {
34 :     my ($self) = @_;
35 : gage 1928 my $r = $self->r;
36 :     my $db = $r->db;
37 :     my $ce = $r->ce;
38 :     my $authz = $r->authz;
39 :     my $user = $r->param('user');
40 : gage 1368
41 :     unless ($authz->hasPermissions($user, "send_mail")) {
42 :     $self->{submitError} = "You are not authorized to send mail to students.";
43 :     return;
44 :     }
45 : gage 1369 #############################################################################################
46 :     # gather directory data
47 :     #############################################################################################
48 :     my $emailDirectory = $ce->{courseDirs}->{email};
49 :     my $scoringDirectory = $ce->{courseDirs}->{scoring};
50 :     my $templateDirectory = $ce->{courseDirs}->{templates};
51 : gage 1368
52 : gage 1369 my $action = $r->param('action');
53 :     my $openfilename = $r->param('openfilename');
54 :     my $savefilename = $r->param('savefilename');
55 : gage 1370
56 :    
57 :     #FIXME get these values from global course environment (see subroutines as well)
58 :     my $default_msg_file = 'default.msg';
59 :     my $old_default_msg_file = 'old_default.msg';
60 :    
61 : gage 1372
62 : gage 1370 # store data
63 :     $self->{defaultFrom} = 'FIXME from';
64 :     $self->{defaultReply} = 'FIXME reply';
65 :     $self->{rows} = (defined($r->param('rows'))) ? $r->param('rows') : $ce->{mail}->{editor_window_rows};
66 :     $self->{columns} = (defined($r->param('columns'))) ? $r->param('columns') : $ce->{mail}->{editor_window_columns};
67 :     $self->{default_msg_file} = $default_msg_file;
68 :     $self->{old_default_msg_file} = $old_default_msg_file;
69 : gage 1372 $self->{merge_file} = (defined($r->param('merge_file' ))) ? $r->param('merge_file') : 'None';
70 : gage 1376 $self->{preview_user} = (defined($r->param('preview_user'))) ? $r->param('preview_user') : $user;
71 : gage 1372
72 :    
73 :     #############################################################################################
74 :     # gather database data
75 :     #############################################################################################
76 :     # FIXME this might be better done in body? We don't always need all of this data. or do we?
77 : gage 1773 my @users = $db->listUsers;
78 : gage 1372 my @user_records = ();
79 : gage 1667 foreach my $userName (@users) {
80 :     my $userRecord = $db->getUser($userName); # checked
81 :     die "record for user $userName not found" unless $userRecord;
82 :     push(@user_records, $userRecord);
83 :     }
84 : gage 1720 ###########################
85 :     # Sort the users for presentation in the select list
86 :     ###########################
87 :     if (defined $r->param("sort_by") ) {
88 :     my $sort_method = $r->param("sort_by");
89 :     if ($sort_method eq 'section') {
90 :     @user_records = sort { (lc($a->section) cmp lc($b->section)) || (lc($a->last_name) cmp lc($b->last_name)) } @user_records;
91 :     } elsif ($sort_method eq 'recitation') {
92 :     @user_records = sort { (lc($a->recitation) cmp lc($b->recitation)) || (lc($a->last_name) cmp lc($b->last_name)) } @user_records;
93 :     } elsif ($sort_method eq 'alphabetical') {
94 :     @user_records = sort { (lc($a->last_name) cmp lc($b->last_name)) } @user_records;
95 : gage 1773 } elsif ($sort_method eq 'id' ) {
96 :     @user_records = sort { $a->user_id cmp $b->user_id } @user_records;
97 :     }
98 :     } else {
99 :     @user_records = sort { $a->user_id cmp $b->user_id } @user_records;
100 : gage 1720 }
101 : gage 1372
102 : gage 1720
103 :     # replace the user names by a sorted version.
104 :     @users = map {$_->user_id} @user_records;
105 : gage 1372 # store data
106 :     $self->{ra_users} = \@users;
107 :     $self->{ra_user_records} = \@user_records;
108 :    
109 :     #############################################################################################
110 :     # gather list of recipients
111 :     #############################################################################################
112 :     my @send_to = ();
113 :     #FIXME this (radio) is a lousy name
114 :     my $recipients = $r->param('radio');
115 : gage 1375 if (defined($recipients) and $recipients eq 'all_students') { #only active students #FIXME status check??
116 : gage 1372 foreach my $ur (@user_records) {
117 :     push(@send_to,$ur->user_id) if $ur->status eq 'C' and not($ur->user_id =~ /practice/);
118 :     }
119 : gage 1375 } elsif (defined($recipients) and $recipients eq 'studentID' ) {
120 : gage 1372 @send_to = $r->param('classList');
121 :     } else {
122 : gage 1375 # no recipients have been defined -- probably the first time on the page
123 : gage 1372 }
124 :     $self->{ra_send_to} = \@send_to;
125 : gage 1370 #################################################################
126 :     # Check the validity of the input file name
127 :     #################################################################
128 :     my $input_file = '';
129 :     #make sure an input message file was submitted and exists
130 :     #else use the default message
131 :     if ( defined($openfilename) ) {
132 :     if ( -e "${emailDirectory}/$openfilename") {
133 :     if ( -R "${emailDirectory}/$openfilename") {
134 :     $input_file = $openfilename;
135 : gage 1369 } else {
136 : gage 1370 warn join("",
137 :     "The file ${emailDirectory}/$openfilename is not readable by the webserver.",CGI::br(),
138 :     "Check that it's permissions are set correctly.",
139 :     );
140 : gage 1369 }
141 :     } else {
142 : gage 1370 $input_file = $default_msg_file;
143 :     warn join("",
144 :     "The file ${emailDirectory}/$openfilename cannot be found.",CGI::br(),
145 :     "Check whether it exists and whether the directory $emailDirectory can be read by the webserver.",CGI::br(),
146 :     "Using contents of the default message $default_msg_file instead.",
147 :     );
148 : gage 1369 }
149 : gage 1370 } else {
150 : gage 1372 $input_file = $default_msg_file;
151 : gage 1370 }
152 : gage 1372 $self->{input_file} =$input_file;
153 : gage 1370
154 :     #################################################################
155 :     # Determine the file name to save message into
156 :     #################################################################
157 : gage 1371 my $output_file = 'FIXME no output file specified';
158 : gage 1370 if (defined($action) and $action eq 'Save as Default') {
159 :     $output_file = $default_msg_file;
160 : gage 1953 } elsif ( defined($action) and ($action =~/save/i) and defined($savefilename) and $savefilename ){
161 : gage 1370 $output_file = $savefilename;
162 :     } elsif ( defined($input_file) ) {
163 :     $output_file = $input_file;
164 :     }
165 : gage 1953
166 : gage 1370 #################################################################
167 :     # Sanity check on save file name
168 :     #################################################################
169 :    
170 :     if ($output_file =~ /^[~.]/ || $output_file =~ /\.\./) {
171 : gage 1730 $self->submission_error("For security reasons, you cannot specify a message file from a directory",
172 :     "higher than the email directory (you can't use ../blah/blah for example). ",
173 : gage 1371 "Please specify a different file or move the needed file to the email directory",
174 :     );
175 : gage 1370 }
176 : gage 1371 unless ($output_file =~ m|\.msg$| ) {
177 :     $self->submission_error("Invalid file name.",
178 :     "The file name \"$output_file\" does not have a \".msg\" extension",
179 :     "All email file names must end in the extension \".msg\"",
180 :     "choose a file name with a \".msg\" extension.",
181 :     "The message was not saved.",
182 :     );
183 :     }
184 :     $self->{output_file} = $output_file; # this is ok. It will be put back in the text input box for re-editing.
185 : gage 1370 # FIXME $output_file can be blank if there was no savefilename
186 :    
187 :     #############################################################################################
188 :     # Determine input source
189 :     #############################################################################################
190 :     my $input_source = ( defined( $r->param('body') ) and $action ne 'Open' ) ? 'form' : 'file';
191 : gage 1953
192 : gage 1370 #############################################################################################
193 :     # Get inputs
194 :     #############################################################################################
195 :     my($from, $replyTo, $r_text, $subject);
196 :     if ($input_source eq 'file') {
197 : gage 1953
198 : gage 1370 ($from, $replyTo,$subject,$r_text) = $self->read_input_file("$emailDirectory/$input_file");
199 :    
200 : gage 1953
201 : gage 1370 } elsif ($input_source eq 'form') {
202 :     # read info from the form
203 :     # bail if there is no message body
204 : gage 1369
205 : gage 1370 $from = $r->param('from');
206 :     $replyTo = $r->param('replyTo');
207 :     $subject = $r->param('subject');
208 :     my $body = $r->param('body');
209 :     # Sanity check: body must contain non-white space
210 :     $self->submission_error('You didn\'t enter any message.') unless ($r->param('body') =~ /\S/);
211 :     $r_text = \$body;
212 : gage 1369
213 : gage 1370 }
214 :     # store data
215 :     $self->{from} = $from;
216 :     $self->{replyTo} = $replyTo;
217 :     $self->{subject} = $subject;
218 :     $self->{r_text} = $r_text;
219 :    
220 :    
221 : gage 1372
222 :     ###################################################################################
223 :     #Determine the appropriate script action from the buttons
224 :     ###################################################################################
225 :     # first time actions
226 :     # open new file
227 :     # open default file
228 :     # choose merge file actions
229 :     # chose merge button
230 :     # option actions
231 :     # 'reset rows'
232 :    
233 :     # save actions
234 :     # "save" button
235 :     # "save as" button
236 :     # "save as default" button
237 :     # preview actions
238 :     # 'preview' button
239 :     # email actions
240 :     # 'entire class'
241 :     # 'selected studentIDs'
242 :     # error actions (various)
243 :    
244 :    
245 : gage 1369 #############################################################################################
246 : gage 1370 # if no form is submitted, gather data needed to produce the mail form and return
247 : gage 1369 #############################################################################################
248 : gage 1372 my $to = $r->param('To');
249 :     my $script_action = '';
250 :    
251 :    
252 : gage 1773 if(not defined($action) or $action eq 'Open' or $action eq $REFRESH_RESIZE_BUTTON or $action eq 'Sort by'
253 : gage 1375 or $action eq 'Set merge file to:' ){
254 : gage 1953
255 : gage 1369 return '';
256 :     }
257 :    
258 :    
259 :    
260 :    
261 :    
262 :     #############################################################################################
263 : gage 1370 # If form is submitted deal with filled out forms
264 : gage 1369 # and various actions resulting from different buttons
265 :     #############################################################################################
266 :    
267 : gage 1372
268 : gage 1370 if ($action eq 'Save' or $action eq 'Save as:' or $action eq 'Save as Default') {
269 :    
270 :     # warn "FIXME Saving files action = $action outputFileName=$output_file";
271 : gage 1369
272 : gage 1370 #################################################################
273 :     # construct message body
274 :     #################################################################
275 :     my $temp_body = ${ $r_text };
276 :     $temp_body =~ s/\r\n/\n/g;
277 :     $temp_body = join("",
278 :     "From: $from \nReply-To: $replyTo\n" ,
279 :     "Subject: $subject\n" ,
280 :     "Message: \n $temp_body");
281 :     # warn "FIXME from $from | subject $subject |reply $replyTo|msg $temp_body";
282 :     #################################################################
283 :     # overwrite protection
284 :     #################################################################
285 :     if ($action eq 'Save as:' and -e "$emailDirectory/$output_file") {
286 : gage 1371 $self->submission_error("The file $emailDirectory/$output_file already exists and cannot be overwritten",
287 :     "The message was not saved");
288 : gage 1370 return;
289 :     }
290 :    
291 :     #################################################################
292 :     # Back up existing file?
293 :     #################################################################
294 : gage 1371 if ($action eq 'Save as Default' and -e "$emailDirectory/$default_msg_file") {
295 :     rename("$emailDirectory/$default_msg_file","$emailDirectory/$old_default_msg_file") or
296 :     die "Can't rename $emailDirectory/$default_msg_file to $emailDirectory/$old_default_msg_file ",
297 :     "Check permissions for webserver on directory $emailDirectory. $!";
298 :     $self->{message} .= "Backup file <code>$emailDirectory/$old_default_msg_file</code> created.".CGI::br();
299 : gage 1370 }
300 :     #################################################################
301 :     # Save the message
302 :     #################################################################
303 :     $self->saveProblem($temp_body, "${emailDirectory}/$output_file" );
304 : gage 1953 unless ( $self->{submitError} or not -w "${emailDirectory}/$output_file" ) { # if there are no errors report success
305 :     $self->{message} .= "Message saved to file <code>${emailDirectory}/$output_file</code>.";
306 :     }
307 :    
308 : gage 1372 } elsif ($action eq 'Preview') {
309 :     $self->{response} = 'preview';
310 : gage 1370
311 :     } elsif ($action eq 'Send Email') {
312 : gage 1373 $self->{response} = 'send_email';
313 :    
314 :     my @recipients = @{$self->{ra_send_to}};
315 : sh002i 2035 $self->addmessage(CGI::div({class=>'ResultsWithError'},
316 :     "No recipients selected")) unless @recipients;
317 : gage 1373 # get merge file
318 :     my $merge_file = ( defined($self->{merge_file}) ) ? $self->{merge_file} : 'None';
319 :     my $delimiter = ',';
320 : gage 1397 my $rh_merge_data = $self->read_scoring_file("$merge_file", "$delimiter");
321 : gage 1376 unless (ref($rh_merge_data) ) {
322 :     warn "no merge data file";
323 :     $self->submission_error("Can't read merge file $merge_file. No message sent");
324 :     return;
325 :     } ;
326 : gage 1373
327 : gage 1376
328 : gage 1373 foreach my $recipient (@recipients) {
329 :     #warn "FIXME sending email to $recipient";
330 : gage 1667 my $ur = $self->{db}->getUser($recipient); #checked
331 :     die "record for user $recipient not found" unless $ur;
332 : sh002i 1677 unless ($ur->email_address) {
333 : sh002i 2035 $self->addmessage(CGI::div({class=>'ResultsWithError'},
334 :     "user $recipient does not have an email address -- skipping"));
335 : sh002i 1677 next;
336 :     }
337 : gage 1373 my ($msg, $preview_header);
338 :     eval{ ($msg,$preview_header) = $self->process_message($ur,$rh_merge_data); };
339 :     warn "There were errors in processing user $ur, merge file $merge_file. $@" if $@;
340 :     my $mailer = Mail::Sender->new({
341 :     from => $from,
342 :     to => $ur->email_address,
343 :     smtp => $ce->{mail}->{smtpServer},
344 :     subject => $subject,
345 :     headers => "X-Remote-Host: ".$r->get_remote_host(),
346 :     });
347 :     unless (ref $mailer) {
348 : sh002i 1677 warn "Failed to create a mailer for user $recipient: $Mail::Sender::Error";
349 : gage 1373 next;
350 :     }
351 :     unless (ref $mailer->Open()) {
352 : sh002i 1677 warn "Failed to open the mailer for user $recipient: $Mail::Sender::Error";
353 : gage 1373 next;
354 :     }
355 :     my $MAIL = $mailer->GetHandle() or warn "Couldn't get handle";
356 :     print $MAIL $msg || warn "Couldn't print to $MAIL";
357 :     close $MAIL || warn "Couldn't close $MAIL";
358 :     #warn "FIXME mailed to ", $ur->email_address, "from $from subject $subject";
359 :    
360 :     }
361 :    
362 : gage 1370 } else {
363 : gage 1373 warn "Didn't recognize button $action";
364 : gage 1370 }
365 : gage 1369
366 :    
367 :    
368 :     } #end initialize
369 :    
370 : gage 1368
371 :    
372 :    
373 : gage 1928
374 : gage 1368 sub body {
375 : gage 1928 my ($self) = @_;
376 :     my $r = $self->r;
377 :     my $urlpath = $r->urlpath;
378 :     my $setID = $urlpath->arg("setID");
379 : gage 1372 my $response = (defined($self->{response}))? $self->{response} : '';
380 :     if ($response eq 'preview') {
381 :     $self->print_preview($setID);
382 : gage 1373 } elsif (($response eq 'send_email')){
383 :     $self->{message} .= CGI::h3("Email sent to "). join(" ", @{$self->{ra_send_to}});
384 : gage 1372 $self->print_form($setID);
385 : gage 1375 } else {
386 :     $self->print_form($setID);
387 : gage 1372 }
388 :    
389 :     }
390 :     sub print_preview {
391 : gage 1928 my ($self) = @_;
392 :     my $r = $self->r;
393 :     my $urlpath = $r->urlpath;
394 :     my $setID = $urlpath->arg("setID");
395 :    
396 : gage 1372 # get preview user
397 : gage 1928 my $ur = $r->db->getUser($self->{preview_user}); #checked
398 : gage 1667 die "record for preview user ".$self->{preview_user}. " not found." unless $ur;
399 : gage 1372
400 :     # get merge file
401 :     my $merge_file = ( defined($self->{merge_file}) ) ? $self->{merge_file} : 'None';
402 :     my $delimiter = ',';
403 : gage 1397 my $rh_merge_data = $self->read_scoring_file("$merge_file", "$delimiter");
404 : gage 1372
405 :     my ($msg, $preview_header) = $self->process_message($ur,$rh_merge_data);
406 :    
407 :     my $recipients = join(" ",@{$self->{ra_send_to} });
408 : gage 1373 my $errorMessage = defined($self->{submitError}) ? CGI::h3($self->{submitError} ) : '' ;
409 :     $msg = join("",
410 :     $errorMessage,
411 :     $preview_header,
412 :     "To: " , $ur->email_address,"\n",
413 :     "From: " , $self->{from} , "\n" ,
414 :     "Reply-To: " , $self->{replyTo} , "\n" ,
415 :     "Subject: " , $self->{subject} , "\n" ,"\n" ,
416 :     $msg , "\n"
417 :     );
418 : gage 1372
419 : gage 1373 return join("", '<pre>',$msg,"\n","\n",
420 : gage 1372 '</pre>',
421 :     CGI::p('Use browser back button to return from preview mode'),
422 :     CGI::h3('Emails to be sent to the following:'),
423 :     $recipients, "\n",
424 :    
425 :     );
426 :    
427 :     }
428 :     sub print_form {
429 : gage 1928 my ($self) = @_;
430 :     my $r = $self->r;
431 :     my $urlpath = $r->urlpath;
432 :     my $authz = $r->authz;
433 :     my $db = $r->db;
434 :     my $ce = $r->ce;
435 :     my $courseName = $urlpath->arg("courseID");
436 :     my $setID = $urlpath->arg("setID");
437 :     my $user = $r->param('user');
438 :    
439 : gage 1938 my $root = $ce->{webworkURLs}->{root};
440 :     my $sendMailPage = $urlpath->newFromModule($urlpath->module,courseID=>$courseName);
441 :     my $sendMailURL = $self->systemLink($sendMailPage, authen => 0);
442 : gage 1368
443 :     return CGI::em("You are not authorized to access the Instructor tools.") unless $authz->hasPermissions($user, "access_instructor_tools");
444 :    
445 :     my $userTemplate = $db->newUser;
446 :     my $permissionLevelTemplate = $db->newPermissionLevel;
447 :    
448 :     # This code will require changing if the permission and user tables ever have different keys.
449 : gage 1372 my @users = @{ $self->{ra_users} };
450 :     my $ra_user_records = $self->{ra_user_records};
451 :     my %classlistLabels = ();# %$hr_classlistLabels;
452 :     foreach my $ur (@{ $ra_user_records }) {
453 : gage 1720 $classlistLabels{$ur->user_id} = $ur->user_id.': '.$ur->last_name. ', '. $ur->first_name.' -- '.$ur->section." / ".$ur->recitation;
454 : gage 1372 }
455 : gage 1368
456 :    
457 :     ##############################################################################################################
458 :    
459 : gage 1372
460 : gage 1369 my $from = $self->{from};
461 :     my $subject = $self->{subject};
462 :     my $replyTo = $self->{replyTo};
463 :     my $columns = $self->{columns};
464 :     my $rows = $self->{rows};
465 : gage 1370 my $text = defined($self->{r_text}) ? ${ $self->{r_text} }: 'FIXME no text was produced by initialization!!';
466 :     my $input_file = $self->{input_file};
467 :     my $output_file = $self->{output_file};
468 : gage 1371 my @sorted_messages = $self->get_message_file_names;
469 :     my @sorted_merge_files = $self->get_merge_file_names;
470 :     my $merge_file = ( defined($self->{merge_file}) ) ? $self->{merge_file} : 'None';
471 : gage 1372 my $delimiter = ',';
472 : gage 1397 my $rh_merge_data = $self->read_scoring_file("$merge_file", "$delimiter");
473 : gage 1372 my @merge_keys = keys %$rh_merge_data;
474 :     my $preview_user = $self->{preview_user};
475 : gage 1667 my $preview_record = $db->getUser($preview_user); # checked
476 :     die "record for preview user ".$self->{preview_user}. " not found." unless $preview_record;
477 : gage 1372
478 : gage 1667
479 : gage 1372 #############################################################################################
480 :    
481 : gage 1938 print CGI::start_form({method=>"post", action=>$sendMailURL});
482 : gage 1372 print $self->hidden_authen_fields();
483 :     #############################################################################################
484 :     # begin upper table
485 :     #############################################################################################
486 :    
487 : gage 1368 print CGI::start_table({-border=>'2', -cellpadding=>'4'});
488 : gage 1720 print CGI::Tr({-align=>'left',-valign=>'top'},
489 : gage 1372 #############################################################################################
490 :     # first column
491 :     #############################################################################################
492 :    
493 : gage 1720 CGI::td(CGI::strong("Message file: $input_file"),"\n",CGI::br(),
494 : gage 1371 CGI::submit(-name=>'action', -value=>'Open'), '&nbsp;&nbsp;&nbsp;&nbsp;',"\n",
495 :     CGI::popup_menu(-name=>'openfilename',
496 :     -values=>\@sorted_messages,
497 :     -default=>$input_file
498 :     ), "\n",CGI::br(),
499 :    
500 :     "Save file to: $output_file","\n",CGI::br(),
501 : gage 1370 "\n", 'From:','&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', CGI::textfield(-name=>"from", -size=>30, -value=>$from, -override=>1),
502 :     "\n", CGI::br(),'Reply-To: ', CGI::textfield(-name=>"replyTo", -size=>30, -value=>$replyTo, -override=>1),
503 : gage 1376 "\n", CGI::br(),'Subject: ', CGI::br(), CGI::textarea(-name=>'subject', -default=>$subject, -rows=>3,-columns=>30, -override=>1),
504 : gage 1370 ),
505 : gage 1372 #############################################################################################
506 :     # second column
507 :     #############################################################################################
508 : gage 1773 CGI::td({-align=>'left',style=>'font-size:smaller'},
509 :    
510 :     CGI::strong("Send to:"),
511 : gage 1720 CGI::radio_group(-name=>'radio', -values=>['all_students','studentID'],
512 :     -labels=>{all_students=>'All',studentID => 'Selected'},
513 :     -default=>'studentID',
514 : gage 1773 -linebreak=>0
515 :     ), CGI::br(),CGI::br(),
516 :    
517 :     CGI::input({type=>'submit',value=>'Sort by',name=>'action'}),,
518 : gage 1720 CGI::radio_group(-name=>'sort_by', -values=>['id','alphabetical','section','recitation'],
519 : gage 1773 -labels=>{id=>'Login',alphabetical=>'Alph.',section => 'Sec.',recitation=>'Rec.'},
520 : gage 1720 -default=>defined($r->param("sort_by")) ? $r->param("sort_by") : 'id',
521 : gage 1773 -linebreak=>0
522 : gage 1720 ),
523 :    
524 : gage 1773 CGI::br(),CGI::br(),
525 : gage 1720 CGI::popup_menu(-name=>'classList',
526 :     -values=>\@users,
527 :     -labels=>\%classlistLabels,
528 :     -size => 10,
529 :     -multiple => 1,
530 :     -default=>$user
531 :     ),
532 :     ),
533 : gage 1370
534 : gage 1720
535 : gage 1372 #############################################################################################
536 :     # third column
537 :     #############################################################################################
538 : gage 1370 CGI::td({align=>'left'},
539 : gage 1720 "<b>Merge file:</b> $merge_file", CGI::br(),
540 : gage 1375 CGI::submit(-name=>'action', -value=>'Set merge file to:'),CGI::br(),
541 : gage 1371 CGI::popup_menu(-name=>'merge_file',
542 :     -values=>\@sorted_merge_files,
543 :     -default=>$merge_file,
544 : gage 1372 ), "\n",CGI::hr(),CGI::br(),
545 :     CGI::submit(-name=>'action', -value=>'preview',-label=>'Preview')," email to ",
546 :     CGI::popup_menu(-name=>'preview_user',
547 : gage 1370 -values=>\@users,
548 : gage 1372 #-labels=>\%classlistLabels,
549 :     -default=>$preview_user,
550 : gage 1369 ),
551 : gage 1372 CGI::hr(),
552 : gage 1720 CGI::submit(-name=>'action', -value=>'resize', -label=>$REFRESH_RESIZE_BUTTON),CGI::br(),
553 : gage 1370 " Rows: ", CGI::textfield(-name=>'rows', -size=>3, -value=>$rows),
554 :     " Columns: ", CGI::textfield(-name=>'columns', -size=>3, -value=>$columns),
555 :     CGI::br(),CGI::br(),
556 :     #show available macros
557 :     CGI::popup_menu(
558 :     -name=>'dummyName',
559 :     -values=>['', '$SID', '$FN', '$LN', '$SECTION', '$RECITATION','$STATUS', '$EMAIL', '$LOGIN', '$COL[3]', '$COL[-1]'],
560 :     -labels=>{''=>'list of insertable macros',
561 :     '$SID'=>'$SID - Student ID',
562 :     '$FN'=>'$FN - First name',
563 :     '$LN'=>'$LN - Last name',
564 : gage 1372 '$SECTION'=>'$SECTION',
565 : gage 1370 '$RECITATION'=>'$RECITATION',
566 :     '$STATUS'=>'$STATUS - C, Audit, Drop, etc.',
567 :     '$EMAIL'=>'$EMAIL - Email address',
568 :     '$LOGIN'=>'$LOGIN - Login',
569 : gage 1372 '$COL[3]'=>'$COL[3] - 3rd col',
570 : gage 1370 '$COL[-1]'=>'$COL[-1] - Last column'
571 :     }
572 :     ), "\n",
573 :     ),
574 : gage 1368
575 : gage 1370 ); # end Tr
576 : gage 1372 print CGI::end_table();
577 :     #############################################################################################
578 :     # end upper table
579 :     #############################################################################################
580 :    
581 :     # show merge file
582 :     # print "<pre>",(map {$_ =~s/\s/\./g;$_} map {sprintf('%-8.8s',$_);} 0..8),"</pre>";
583 :     # print CGI::popup_menu(
584 :     # -name=>'dummyName2',
585 :     # -values=>\@merge_keys,
586 :     # -labels=>$rh_merge_data,
587 :     # -multiple=>1,
588 :     # -size =>2,
589 :     #
590 :     # ), "\n",CGI::br();
591 :     # warn "merge keys ", join( " ",@merge_keys);
592 :     #############################################################################################
593 :     # merge file fragment and message text area field
594 :     #############################################################################################
595 : gage 1373 my @tmp2;
596 : gage 1667 eval{ @tmp2= @{$rh_merge_data->{ $db->getUser($preview_user)->student_id } };}; # checked
597 : gage 1730 if ($@ and $merge_file ne 'None') {
598 : gage 1381 print "No merge data for $preview_user in merge file: &lt;$merge_file&gt;",CGI::br();
599 : gage 1373 } else {
600 : gage 1950 print CGI::pre("",data_format(0..($#tmp2)),"<br>", data_format2(@tmp2));
601 : gage 1373 }
602 : gage 1368 #create a textbox with the subject and a textarea with the message
603 :     #print actual body of message
604 : gage 1369
605 : gage 1370 print "\n", CGI::p( $self->{message}) if defined($self->{message});
606 : gage 1369 print "\n", CGI::p( CGI::textarea(-name=>'body', -default=>$text, -rows=>$rows, -columns=>$columns, -override=>1));
607 : gage 1372
608 :     #############################################################################################
609 :     # action button table
610 :     #############################################################################################
611 : gage 1370 print CGI::table( { -border=>2,-cellpadding=>4},
612 :     CGI::Tr(
613 :     CGI::td( CGI::submit(-name=>'action', -value=>'Send Email') ), "\n",
614 :     CGI::td(CGI::submit(-name=>'action', -value=>'Save')," to $output_file"), " \n",
615 :     CGI::td(CGI::submit(-name=>'action', -value=>'Save as:'),
616 :     CGI::textfield(-name=>'savefilename', -size => 20, -value=> "$output_file", -override=>1)
617 :     ), "\n",
618 :     CGI::td(CGI::submit(-name=>'action', -value=>'Save as Default')),
619 :     )
620 :     );
621 : gage 1368
622 :     ##############################################################################################################
623 : gage 1369
624 :     print CGI::end_form();
625 : gage 1368 return "";
626 :     }
627 :    
628 : gage 1369 ##############################################################################
629 :     # Utility methods
630 :     ##############################################################################
631 : gage 1370 sub submission_error {
632 : gage 1369 my $self = shift;
633 : gage 1371 my $msg = join( " ", @_);
634 : gage 1730 $self->{submitError} .= CGI::br().$msg;
635 : gage 1369 return;
636 :     }
637 :    
638 : gage 1370 sub saveProblem {
639 :     my $self = shift;
640 :     my ($body, $probFileName)= @_;
641 :     local(*PROBLEM);
642 :     open (PROBLEM, ">$probFileName") ||
643 :     $self->submission_error("Could not open $probFileName for writing.
644 :     Check that the permissions for this problem are 660 (-rw-rw----)");
645 :     print PROBLEM $body;
646 :     close PROBLEM;
647 :     chmod 0660, "$probFileName" ||
648 :     $self->submission_error("
649 :     CAN'T CHANGE PERMISSIONS ON FILE $probFileName");
650 :     }
651 :    
652 :     sub read_input_file {
653 :     my $self = shift;
654 :     my $filePath = shift;
655 :     my ($text, @text);
656 :     my $header = '';
657 :     my ($subject, $from, $replyTo);
658 :     local(*FILE);
659 : gage 1371 if (-e "$filePath" and -r "$filePath") {
660 :     open FILE, "$filePath" || do { $self->submission_error("Can't open $filePath"); return};
661 :     while ($header !~ s/Message:\s*$//m and not eof(FILE)) {
662 : gage 1370 $header .= <FILE>;
663 :     }
664 :     $text = join( '', <FILE>);
665 :     $text =~ s/^\s*//; # remove initial white space if any.
666 :     $header =~ /^From:\s(.*)$/m;
667 :     $from = $1 or $from = $self->{defaultFrom};
668 :    
669 :     $header =~ /^Reply-To:\s(.*)$/m;
670 :     $replyTo = $1 or $replyTo = $self->{defaultReply};
671 :    
672 :     $header =~ /^Subject:\s(.*)$/m;
673 :     $subject = $1;
674 :    
675 :     } else {
676 :     $from = $self->{defaultFrom};
677 :     $replyTo = $self->{defaultReply};
678 : gage 1371 $text = (-e "$filePath") ? "FIXME file $filePath can't be read" :"FIXME file $filePath doesn't exist";
679 : gage 1370 $subject = "FIXME default subject";
680 :     }
681 :     return ($from, $replyTo, $subject, \$text);
682 :     }
683 : gage 1371
684 :    
685 : gage 1397 sub get_message_file_names {
686 :     my $self = shift;
687 :     return $self->read_dir($self->{ce}->{courseDirs}->{email}, '\\.msg$');
688 : gage 1371 }
689 : gage 1397 sub get_merge_file_names {
690 :     my $self = shift;
691 : gage 1730 return 'None', $self->read_dir($self->{ce}->{courseDirs}->{scoring}, '\\.csv$'); #FIXME ? check that only readable files are listed.
692 : gage 1371 }
693 : gage 1372
694 : gage 1397
695 : gage 1372 sub getRecord {
696 :     my $self = shift;
697 :     my $line = shift;
698 :     my $delimiter = shift;
699 :     $delimiter = ',' unless defined($delimiter);
700 :    
701 :     # Takes a delimited line as a parameter and returns an
702 :     # array. Note that all white space is removed. If the
703 :     # last field is empty, the last element of the returned
704 :     # array is also empty (unlike what the perl split command
705 :     # would return). E.G. @lineArray=&getRecord(\$delimitedLine).
706 :    
707 :     my(@lineArray);
708 : gage 2001 $line.="${delimiter}___"; # add final field which must be non-empty
709 :     @lineArray = split(/\s*${delimiter}\s*/,$line); # split line into fields
710 : gage 1372 $lineArray[0] =~s/^\s*//; # remove white space from first element
711 : gage 2001 pop @lineArray; # remove the last artificial field
712 : gage 1372 @lineArray;
713 :     }
714 :    
715 :     sub process_message {
716 :     my $self = shift;
717 :     my $ur = shift;
718 :     my $rh_merge_data = shift;
719 :     my $text = defined($self->{r_text}) ? ${ $self->{r_text} }:
720 : gage 1730 'FIXME no text was produced by initialization!!';
721 :     my $merge_file = ( defined($self->{merge_file}) ) ? $self->{merge_file} : 'None';
722 : gage 1372 #user macros that can be used in the email message
723 :     my $SID = $ur->student_id;
724 :     my $FN = $ur->first_name;
725 :     my $LN = $ur->last_name;
726 :     my $SECTION = $ur->section;
727 :     my $RECITATION = $ur->recitation;
728 :     my $STATUS = $ur->status;
729 :     my $EMAIL = $ur->email_address;
730 :     my $LOGIN = $ur->user_id;
731 : gage 1730
732 : gage 1372 # get record from merge file
733 :     # FIXME this is inefficient. The info should be cached
734 : gage 1373 my @COL = defined($rh_merge_data->{$SID}) ? @{$rh_merge_data->{$SID} } : ();
735 : gage 1730 if ($merge_file ne 'None' && not defined($rh_merge_data->{$SID}) ) {
736 :     $self->submission_error( "No merge data for $SID $FN $LN $LOGIN");
737 :     }
738 : gage 1372
739 :     my $endCol = @COL;
740 :     # for safety, only evaluate special variables
741 : gage 1373 my $msg = $text;
742 :     $msg =~ s/(\$SID)/eval($1)/ge;
743 :     $msg =~ s/(\$LN)/eval($1)/ge;
744 :     $msg =~ s/(\$FN)/eval($1)/ge;
745 :     $msg =~ s/(\$STATUS)/eval($1)/ge;
746 :     $msg =~ s/(\$SECTION)/eval($1)/ge;
747 :     $msg =~ s/(\$RECITATION)/eval($1)/ge;
748 :     $msg =~ s/(\$EMAIL)/eval($1)/ge;
749 :     $msg =~ s/(\$LOGIN)/eval($1)/ge;
750 :     $msg =~ s/\$COL\[ *-/\$COL\[$endCol-/g;
751 : gage 2049 $msg =~ s/(\$COL\[.*?\])/eval($1)/ge if defined($COL[0]); # prevents extraneous error messages.
752 : gage 1373
753 : gage 2049
754 : gage 1373 $msg =~ s/\r//g;
755 : gage 1372
756 : gage 1950 my $preview_header = CGI::pre("",data_format(0..($#COL)),"<br>", data_format2(@COL)).
757 : gage 1372 CGI::h3( "This sample mail would be sent to $EMAIL");
758 :    
759 :    
760 :     return $msg, $preview_header;
761 :     }
762 : gage 1948
763 :    
764 :     # Ê sub data_format {
765 :     #
766 :     # Ê Ê Ê Ê Êmap {$_ =~s/\s/\./g;$_} Ê Ê map {sprintf('%-8.8s',$_);} Ê@_;
767 : gage 1372 sub data_format {
768 : gage 1948 map {"COL[$_]".'&nbsp;'x(3-length($_));} @_; # problems if $_ has length bigger than 4
769 : gage 1372 }
770 : gage 1947 sub data_format2 {
771 : gage 1948 map {$_ =~s/\s/&nbsp;/g;$_} map {sprintf('%-8.8s',$_);} @_;
772 : gage 1947 }
773 : gage 1368 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9