[system] / trunk / webwork2 / lib / WeBWorK / ContentGenerator / Instructor / SendMail.pm Repository:
ViewVC logotype

Annotation of /trunk/webwork2/lib/WeBWorK/ContentGenerator/Instructor/SendMail.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1953 - (view) (download) (as text)

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9