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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1928 - (view) (download) (as text)
Original Path: trunk/webwork2/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 1928 # $CVSHeader: webwork-modperl/lib/WeBWorK/ContentGenerator/Instructor/SendMail.pm,v 1.18 2004/02/03 00:53:31 gage 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 1371 } elsif ( defined($action) and ($action =~/save/i) and defined($savefilename) ){
161 : gage 1370 $output_file = $savefilename;
162 :     } elsif ( defined($input_file) ) {
163 :     $output_file = $input_file;
164 :     }
165 : gage 1371 # warn "FIXME savefilename $savefilename output file $output_file";
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 :     # warn "FIXME input source is $input_source from $input_file";
192 :     #############################################################################################
193 :     # Get inputs
194 :     #############################################################################################
195 :     my($from, $replyTo, $r_text, $subject);
196 :     if ($input_source eq 'file') {
197 : gage 1371 # warn "FIXME obtaining source from $emailDirectory/$input_file";
198 : gage 1370 ($from, $replyTo,$subject,$r_text) = $self->read_input_file("$emailDirectory/$input_file");
199 : gage 1371 # warn "FIXME Done reading source";
200 : gage 1370
201 :     } 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 1370 # warn "FIXME action is |$action| no further initialization required";
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 1371 $self->{message} .= "Message saved to file <code>${emailDirectory}/$output_file</code>.";
305 : gage 1370 # warn "FIXME saving to ${emailDirectory}/$output_file";
306 : gage 1372 } elsif ($action eq 'Preview') {
307 :     $self->{response} = 'preview';
308 : gage 1370
309 :     } elsif ($action eq 'Send Email') {
310 : gage 1373 $self->{response} = 'send_email';
311 :    
312 :     my @recipients = @{$self->{ra_send_to}};
313 :     warn "No recipients selected " unless @recipients;
314 :     # get merge file
315 :     my $merge_file = ( defined($self->{merge_file}) ) ? $self->{merge_file} : 'None';
316 :     my $delimiter = ',';
317 : gage 1397 my $rh_merge_data = $self->read_scoring_file("$merge_file", "$delimiter");
318 : gage 1376 unless (ref($rh_merge_data) ) {
319 :     warn "no merge data file";
320 :     $self->submission_error("Can't read merge file $merge_file. No message sent");
321 :     return;
322 :     } ;
323 : gage 1373
324 : gage 1376
325 : gage 1373 foreach my $recipient (@recipients) {
326 :     #warn "FIXME sending email to $recipient";
327 : gage 1667 my $ur = $self->{db}->getUser($recipient); #checked
328 :     die "record for user $recipient not found" unless $ur;
329 : sh002i 1677 unless ($ur->email_address) {
330 :     warn "user $recipient does not have an email address -- skipping";
331 :     next;
332 :     }
333 : gage 1373 my ($msg, $preview_header);
334 :     eval{ ($msg,$preview_header) = $self->process_message($ur,$rh_merge_data); };
335 :     warn "There were errors in processing user $ur, merge file $merge_file. $@" if $@;
336 :     my $mailer = Mail::Sender->new({
337 :     from => $from,
338 :     to => $ur->email_address,
339 :     smtp => $ce->{mail}->{smtpServer},
340 :     subject => $subject,
341 :     headers => "X-Remote-Host: ".$r->get_remote_host(),
342 :     });
343 :     unless (ref $mailer) {
344 : sh002i 1677 warn "Failed to create a mailer for user $recipient: $Mail::Sender::Error";
345 : gage 1373 next;
346 :     }
347 :     unless (ref $mailer->Open()) {
348 : sh002i 1677 warn "Failed to open the mailer for user $recipient: $Mail::Sender::Error";
349 : gage 1373 next;
350 :     }
351 :     my $MAIL = $mailer->GetHandle() or warn "Couldn't get handle";
352 :     print $MAIL $msg || warn "Couldn't print to $MAIL";
353 :     close $MAIL || warn "Couldn't close $MAIL";
354 :     #warn "FIXME mailed to ", $ur->email_address, "from $from subject $subject";
355 :    
356 :     }
357 :    
358 : gage 1370 } else {
359 : gage 1373 warn "Didn't recognize button $action";
360 : gage 1370 }
361 : gage 1369
362 :    
363 :    
364 :     } #end initialize
365 :    
366 : gage 1368
367 :    
368 :    
369 : gage 1928
370 : gage 1368 sub body {
371 : gage 1928 my ($self) = @_;
372 :     my $r = $self->r;
373 :     my $urlpath = $r->urlpath;
374 :     my $setID = $urlpath->arg("setID");
375 : gage 1372 my $response = (defined($self->{response}))? $self->{response} : '';
376 :     if ($response eq 'preview') {
377 :     $self->print_preview($setID);
378 : gage 1373 } elsif (($response eq 'send_email')){
379 :     $self->{message} .= CGI::h3("Email sent to "). join(" ", @{$self->{ra_send_to}});
380 : gage 1372 $self->print_form($setID);
381 : gage 1375 } else {
382 :     $self->print_form($setID);
383 : gage 1372 }
384 :    
385 :     }
386 :     sub print_preview {
387 : gage 1928 my ($self) = @_;
388 :     my $r = $self->r;
389 :     my $urlpath = $r->urlpath;
390 :     my $setID = $urlpath->arg("setID");
391 :    
392 : gage 1372 # get preview user
393 : gage 1928 my $ur = $r->db->getUser($self->{preview_user}); #checked
394 : gage 1667 die "record for preview user ".$self->{preview_user}. " not found." unless $ur;
395 : gage 1372
396 :     # get merge file
397 :     my $merge_file = ( defined($self->{merge_file}) ) ? $self->{merge_file} : 'None';
398 :     my $delimiter = ',';
399 : gage 1397 my $rh_merge_data = $self->read_scoring_file("$merge_file", "$delimiter");
400 : gage 1372
401 :     my ($msg, $preview_header) = $self->process_message($ur,$rh_merge_data);
402 :    
403 :     my $recipients = join(" ",@{$self->{ra_send_to} });
404 : gage 1373 my $errorMessage = defined($self->{submitError}) ? CGI::h3($self->{submitError} ) : '' ;
405 :     $msg = join("",
406 :     $errorMessage,
407 :     $preview_header,
408 :     "To: " , $ur->email_address,"\n",
409 :     "From: " , $self->{from} , "\n" ,
410 :     "Reply-To: " , $self->{replyTo} , "\n" ,
411 :     "Subject: " , $self->{subject} , "\n" ,"\n" ,
412 :     $msg , "\n"
413 :     );
414 : gage 1372
415 : gage 1373 return join("", '<pre>',$msg,"\n","\n",
416 : gage 1372 '</pre>',
417 :     CGI::p('Use browser back button to return from preview mode'),
418 :     CGI::h3('Emails to be sent to the following:'),
419 :     $recipients, "\n",
420 :    
421 :     );
422 :    
423 :     }
424 :     sub print_form {
425 : gage 1928 my ($self) = @_;
426 :     my $r = $self->r;
427 :     my $urlpath = $r->urlpath;
428 :     my $authz = $r->authz;
429 :     my $db = $r->db;
430 :     my $ce = $r->ce;
431 :     my $courseName = $urlpath->arg("courseID");
432 :     my $setID = $urlpath->arg("setID");
433 :     my $user = $r->param('user');
434 :    
435 : gage 1368 my $root = $ce->{webworkURLs}->{root};
436 : gage 1928
437 : gage 1368
438 :     return CGI::em("You are not authorized to access the Instructor tools.") unless $authz->hasPermissions($user, "access_instructor_tools");
439 :    
440 :     my $userTemplate = $db->newUser;
441 :     my $permissionLevelTemplate = $db->newPermissionLevel;
442 :    
443 :     # This code will require changing if the permission and user tables ever have different keys.
444 : gage 1372 my @users = @{ $self->{ra_users} };
445 :     my $ra_user_records = $self->{ra_user_records};
446 :     my %classlistLabels = ();# %$hr_classlistLabels;
447 :     foreach my $ur (@{ $ra_user_records }) {
448 : gage 1720 $classlistLabels{$ur->user_id} = $ur->user_id.': '.$ur->last_name. ', '. $ur->first_name.' -- '.$ur->section." / ".$ur->recitation;
449 : gage 1372 }
450 : gage 1368
451 :    
452 :     ##############################################################################################################
453 :    
454 : gage 1372
455 : gage 1369 my $from = $self->{from};
456 :     my $subject = $self->{subject};
457 :     my $replyTo = $self->{replyTo};
458 :     my $columns = $self->{columns};
459 :     my $rows = $self->{rows};
460 : gage 1370 my $text = defined($self->{r_text}) ? ${ $self->{r_text} }: 'FIXME no text was produced by initialization!!';
461 :     my $input_file = $self->{input_file};
462 :     my $output_file = $self->{output_file};
463 : gage 1371 my @sorted_messages = $self->get_message_file_names;
464 :     my @sorted_merge_files = $self->get_merge_file_names;
465 :     my $merge_file = ( defined($self->{merge_file}) ) ? $self->{merge_file} : 'None';
466 : gage 1372 my $delimiter = ',';
467 : gage 1397 my $rh_merge_data = $self->read_scoring_file("$merge_file", "$delimiter");
468 : gage 1372 my @merge_keys = keys %$rh_merge_data;
469 :     my $preview_user = $self->{preview_user};
470 : gage 1667 my $preview_record = $db->getUser($preview_user); # checked
471 :     die "record for preview user ".$self->{preview_user}. " not found." unless $preview_record;
472 : gage 1372
473 : gage 1667
474 : gage 1372 #############################################################################################
475 :    
476 : gage 1369 print CGI::start_form({method=>"post", action=>$r->uri()});
477 : gage 1372 print $self->hidden_authen_fields();
478 :     #############################################################################################
479 :     # begin upper table
480 :     #############################################################################################
481 :    
482 : gage 1368 print CGI::start_table({-border=>'2', -cellpadding=>'4'});
483 : gage 1720 print CGI::Tr({-align=>'left',-valign=>'top'},
484 : gage 1372 #############################################################################################
485 :     # first column
486 :     #############################################################################################
487 :    
488 : gage 1720 CGI::td(CGI::strong("Message file: $input_file"),"\n",CGI::br(),
489 : gage 1371 CGI::submit(-name=>'action', -value=>'Open'), '&nbsp;&nbsp;&nbsp;&nbsp;',"\n",
490 :     CGI::popup_menu(-name=>'openfilename',
491 :     -values=>\@sorted_messages,
492 :     -default=>$input_file
493 :     ), "\n",CGI::br(),
494 :    
495 :     "Save file to: $output_file","\n",CGI::br(),
496 : gage 1370 "\n", 'From:','&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', CGI::textfield(-name=>"from", -size=>30, -value=>$from, -override=>1),
497 :     "\n", CGI::br(),'Reply-To: ', CGI::textfield(-name=>"replyTo", -size=>30, -value=>$replyTo, -override=>1),
498 : gage 1376 "\n", CGI::br(),'Subject: ', CGI::br(), CGI::textarea(-name=>'subject', -default=>$subject, -rows=>3,-columns=>30, -override=>1),
499 : gage 1370 ),
500 : gage 1372 #############################################################################################
501 :     # second column
502 :     #############################################################################################
503 : gage 1773 CGI::td({-align=>'left',style=>'font-size:smaller'},
504 :    
505 :     CGI::strong("Send to:"),
506 : gage 1720 CGI::radio_group(-name=>'radio', -values=>['all_students','studentID'],
507 :     -labels=>{all_students=>'All',studentID => 'Selected'},
508 :     -default=>'studentID',
509 : gage 1773 -linebreak=>0
510 :     ), CGI::br(),CGI::br(),
511 :    
512 :     CGI::input({type=>'submit',value=>'Sort by',name=>'action'}),,
513 : gage 1720 CGI::radio_group(-name=>'sort_by', -values=>['id','alphabetical','section','recitation'],
514 : gage 1773 -labels=>{id=>'Login',alphabetical=>'Alph.',section => 'Sec.',recitation=>'Rec.'},
515 : gage 1720 -default=>defined($r->param("sort_by")) ? $r->param("sort_by") : 'id',
516 : gage 1773 -linebreak=>0
517 : gage 1720 ),
518 :    
519 : gage 1773 CGI::br(),CGI::br(),
520 : gage 1720 CGI::popup_menu(-name=>'classList',
521 :     -values=>\@users,
522 :     -labels=>\%classlistLabels,
523 :     -size => 10,
524 :     -multiple => 1,
525 :     -default=>$user
526 :     ),
527 :     ),
528 : gage 1370
529 : gage 1720
530 : gage 1372 #############################################################################################
531 :     # third column
532 :     #############################################################################################
533 : gage 1370 CGI::td({align=>'left'},
534 : gage 1720 "<b>Merge file:</b> $merge_file", CGI::br(),
535 : gage 1375 CGI::submit(-name=>'action', -value=>'Set merge file to:'),CGI::br(),
536 : gage 1371 CGI::popup_menu(-name=>'merge_file',
537 :     -values=>\@sorted_merge_files,
538 :     -default=>$merge_file,
539 : gage 1372 ), "\n",CGI::hr(),CGI::br(),
540 :     CGI::submit(-name=>'action', -value=>'preview',-label=>'Preview')," email to ",
541 :     CGI::popup_menu(-name=>'preview_user',
542 : gage 1370 -values=>\@users,
543 : gage 1372 #-labels=>\%classlistLabels,
544 :     -default=>$preview_user,
545 : gage 1369 ),
546 : gage 1372 CGI::hr(),
547 : gage 1720 CGI::submit(-name=>'action', -value=>'resize', -label=>$REFRESH_RESIZE_BUTTON),CGI::br(),
548 : gage 1370 " Rows: ", CGI::textfield(-name=>'rows', -size=>3, -value=>$rows),
549 :     " Columns: ", CGI::textfield(-name=>'columns', -size=>3, -value=>$columns),
550 :     CGI::br(),CGI::br(),
551 :     #show available macros
552 :     CGI::popup_menu(
553 :     -name=>'dummyName',
554 :     -values=>['', '$SID', '$FN', '$LN', '$SECTION', '$RECITATION','$STATUS', '$EMAIL', '$LOGIN', '$COL[3]', '$COL[-1]'],
555 :     -labels=>{''=>'list of insertable macros',
556 :     '$SID'=>'$SID - Student ID',
557 :     '$FN'=>'$FN - First name',
558 :     '$LN'=>'$LN - Last name',
559 : gage 1372 '$SECTION'=>'$SECTION',
560 : gage 1370 '$RECITATION'=>'$RECITATION',
561 :     '$STATUS'=>'$STATUS - C, Audit, Drop, etc.',
562 :     '$EMAIL'=>'$EMAIL - Email address',
563 :     '$LOGIN'=>'$LOGIN - Login',
564 : gage 1372 '$COL[3]'=>'$COL[3] - 3rd col',
565 : gage 1370 '$COL[-1]'=>'$COL[-1] - Last column'
566 :     }
567 :     ), "\n",
568 :     ),
569 : gage 1368
570 : gage 1370 ); # end Tr
571 : gage 1372 print CGI::end_table();
572 :     #############################################################################################
573 :     # end upper table
574 :     #############################################################################################
575 :    
576 :     # show merge file
577 :     # print "<pre>",(map {$_ =~s/\s/\./g;$_} map {sprintf('%-8.8s',$_);} 0..8),"</pre>";
578 :     # print CGI::popup_menu(
579 :     # -name=>'dummyName2',
580 :     # -values=>\@merge_keys,
581 :     # -labels=>$rh_merge_data,
582 :     # -multiple=>1,
583 :     # -size =>2,
584 :     #
585 :     # ), "\n",CGI::br();
586 :     # warn "merge keys ", join( " ",@merge_keys);
587 :     #############################################################################################
588 :     # merge file fragment and message text area field
589 :     #############################################################################################
590 : gage 1373 my @tmp2;
591 : gage 1667 eval{ @tmp2= @{$rh_merge_data->{ $db->getUser($preview_user)->student_id } };}; # checked
592 : gage 1730 if ($@ and $merge_file ne 'None') {
593 : gage 1381 print "No merge data for $preview_user in merge file: &lt;$merge_file&gt;",CGI::br();
594 : gage 1373 } else {
595 :     print CGI::pre("",data_format(0..($#tmp2)),"\n", data_format(@tmp2));
596 :     }
597 : gage 1368 #create a textbox with the subject and a textarea with the message
598 :     #print actual body of message
599 : gage 1369
600 : gage 1370 print "\n", CGI::p( $self->{message}) if defined($self->{message});
601 : gage 1369 print "\n", CGI::p( CGI::textarea(-name=>'body', -default=>$text, -rows=>$rows, -columns=>$columns, -override=>1));
602 : gage 1372
603 :     #############################################################################################
604 :     # action button table
605 :     #############################################################################################
606 : gage 1370 print CGI::table( { -border=>2,-cellpadding=>4},
607 :     CGI::Tr(
608 :     CGI::td( CGI::submit(-name=>'action', -value=>'Send Email') ), "\n",
609 :     CGI::td(CGI::submit(-name=>'action', -value=>'Save')," to $output_file"), " \n",
610 :     CGI::td(CGI::submit(-name=>'action', -value=>'Save as:'),
611 :     CGI::textfield(-name=>'savefilename', -size => 20, -value=> "$output_file", -override=>1)
612 :     ), "\n",
613 :     CGI::td(CGI::submit(-name=>'action', -value=>'Save as Default')),
614 :     )
615 :     );
616 : gage 1368
617 :     ##############################################################################################################
618 : gage 1369
619 :     print CGI::end_form();
620 : gage 1368 return "";
621 :     }
622 :    
623 : gage 1369 ##############################################################################
624 :     # Utility methods
625 :     ##############################################################################
626 : gage 1370 sub submission_error {
627 : gage 1369 my $self = shift;
628 : gage 1371 my $msg = join( " ", @_);
629 : gage 1730 $self->{submitError} .= CGI::br().$msg;
630 : gage 1369 return;
631 :     }
632 :    
633 : gage 1370 sub saveProblem {
634 :     my $self = shift;
635 :     my ($body, $probFileName)= @_;
636 :     local(*PROBLEM);
637 :     open (PROBLEM, ">$probFileName") ||
638 :     $self->submission_error("Could not open $probFileName for writing.
639 :     Check that the permissions for this problem are 660 (-rw-rw----)");
640 :     print PROBLEM $body;
641 :     close PROBLEM;
642 :     chmod 0660, "$probFileName" ||
643 :     $self->submission_error("
644 :     CAN'T CHANGE PERMISSIONS ON FILE $probFileName");
645 :     }
646 :    
647 :     sub read_input_file {
648 :     my $self = shift;
649 :     my $filePath = shift;
650 :     my ($text, @text);
651 :     my $header = '';
652 :     my ($subject, $from, $replyTo);
653 :     local(*FILE);
654 : gage 1371 if (-e "$filePath" and -r "$filePath") {
655 :     open FILE, "$filePath" || do { $self->submission_error("Can't open $filePath"); return};
656 :     while ($header !~ s/Message:\s*$//m and not eof(FILE)) {
657 : gage 1370 $header .= <FILE>;
658 :     }
659 :     $text = join( '', <FILE>);
660 :     $text =~ s/^\s*//; # remove initial white space if any.
661 :     $header =~ /^From:\s(.*)$/m;
662 :     $from = $1 or $from = $self->{defaultFrom};
663 :    
664 :     $header =~ /^Reply-To:\s(.*)$/m;
665 :     $replyTo = $1 or $replyTo = $self->{defaultReply};
666 :    
667 :     $header =~ /^Subject:\s(.*)$/m;
668 :     $subject = $1;
669 :    
670 :     } else {
671 :     $from = $self->{defaultFrom};
672 :     $replyTo = $self->{defaultReply};
673 : gage 1371 $text = (-e "$filePath") ? "FIXME file $filePath can't be read" :"FIXME file $filePath doesn't exist";
674 : gage 1370 $subject = "FIXME default subject";
675 :     }
676 :     return ($from, $replyTo, $subject, \$text);
677 :     }
678 : gage 1371
679 :    
680 : gage 1397 sub get_message_file_names {
681 :     my $self = shift;
682 :     return $self->read_dir($self->{ce}->{courseDirs}->{email}, '\\.msg$');
683 : gage 1371 }
684 : gage 1397 sub get_merge_file_names {
685 :     my $self = shift;
686 : gage 1730 return 'None', $self->read_dir($self->{ce}->{courseDirs}->{scoring}, '\\.csv$'); #FIXME ? check that only readable files are listed.
687 : gage 1371 }
688 : gage 1372
689 : gage 1397
690 : gage 1372 sub getRecord {
691 :     my $self = shift;
692 :     my $line = shift;
693 :     my $delimiter = shift;
694 :     $delimiter = ',' unless defined($delimiter);
695 :    
696 :     # Takes a delimited line as a parameter and returns an
697 :     # array. Note that all white space is removed. If the
698 :     # last field is empty, the last element of the returned
699 :     # array is also empty (unlike what the perl split command
700 :     # would return). E.G. @lineArray=&getRecord(\$delimitedLine).
701 :    
702 :     my(@lineArray);
703 :     $line.=$delimiter; # add 'A' to end of line so that
704 :     # last field is never empty
705 :     @lineArray = split(/\s*${delimiter}\s*/,$line);
706 :     $lineArray[0] =~s/^\s*//; # remove white space from first element
707 :     @lineArray;
708 :     }
709 :    
710 :     sub process_message {
711 :     my $self = shift;
712 :     my $ur = shift;
713 :     my $rh_merge_data = shift;
714 :     my $text = defined($self->{r_text}) ? ${ $self->{r_text} }:
715 : gage 1730 'FIXME no text was produced by initialization!!';
716 :     my $merge_file = ( defined($self->{merge_file}) ) ? $self->{merge_file} : 'None';
717 : gage 1372 #user macros that can be used in the email message
718 :     my $SID = $ur->student_id;
719 :     my $FN = $ur->first_name;
720 :     my $LN = $ur->last_name;
721 :     my $SECTION = $ur->section;
722 :     my $RECITATION = $ur->recitation;
723 :     my $STATUS = $ur->status;
724 :     my $EMAIL = $ur->email_address;
725 :     my $LOGIN = $ur->user_id;
726 : gage 1730
727 : gage 1372 # get record from merge file
728 :     # FIXME this is inefficient. The info should be cached
729 : gage 1373 my @COL = defined($rh_merge_data->{$SID}) ? @{$rh_merge_data->{$SID} } : ();
730 : gage 1730 if ($merge_file ne 'None' && not defined($rh_merge_data->{$SID}) ) {
731 :     $self->submission_error( "No merge data for $SID $FN $LN $LOGIN");
732 :     }
733 : gage 1372
734 :     my $endCol = @COL;
735 :     # for safety, only evaluate special variables
736 : gage 1373 my $msg = $text;
737 :     $msg =~ s/(\$SID)/eval($1)/ge;
738 :     $msg =~ s/(\$LN)/eval($1)/ge;
739 :     $msg =~ s/(\$FN)/eval($1)/ge;
740 :     $msg =~ s/(\$STATUS)/eval($1)/ge;
741 :     $msg =~ s/(\$SECTION)/eval($1)/ge;
742 :     $msg =~ s/(\$RECITATION)/eval($1)/ge;
743 :     $msg =~ s/(\$EMAIL)/eval($1)/ge;
744 :     $msg =~ s/(\$LOGIN)/eval($1)/ge;
745 :     $msg =~ s/\$COL\[ *-/\$COL\[$endCol-/g;
746 :     $msg =~ s/(\$COL\[.*?\])/eval($1)/ge;
747 :    
748 :     $msg =~ s/\r//g;
749 : gage 1372
750 :     my $preview_header = CGI::pre("",data_format(0..($#COL)),"\n", data_format(@COL)).
751 :     CGI::h3( "This sample mail would be sent to $EMAIL");
752 :    
753 :    
754 :     return $msg, $preview_header;
755 :     }
756 :     sub data_format {
757 :     map {$_ =~s/\s/\./g;$_} map {sprintf('%-8.8s',$_);} @_;
758 :     }
759 : gage 1368 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9