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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1370 Revision 1371
45 $self->{defaultReply} = 'FIXME reply'; 45 $self->{defaultReply} = 'FIXME reply';
46 $self->{rows} = (defined($r->param('rows'))) ? $r->param('rows') : $ce->{mail}->{editor_window_rows}; 46 $self->{rows} = (defined($r->param('rows'))) ? $r->param('rows') : $ce->{mail}->{editor_window_rows};
47 $self->{columns} = (defined($r->param('columns'))) ? $r->param('columns') : $ce->{mail}->{editor_window_columns}; 47 $self->{columns} = (defined($r->param('columns'))) ? $r->param('columns') : $ce->{mail}->{editor_window_columns};
48 $self->{default_msg_file} = $default_msg_file; 48 $self->{default_msg_file} = $default_msg_file;
49 $self->{old_default_msg_file} = $old_default_msg_file; 49 $self->{old_default_msg_file} = $old_default_msg_file;
50 $self->{merge_file} = (defined($r->param('merge_file'))) ? $r->param('merge_file') : 'None';
50################################################################# 51#################################################################
51# Check the validity of the input file name 52# Check the validity of the input file name
52################################################################# 53#################################################################
53 my $input_file = ''; 54 my $input_file = '';
54 #make sure an input message file was submitted and exists 55 #make sure an input message file was submitted and exists
77 $self->{input_file}=$input_file; 78 $self->{input_file}=$input_file;
78 79
79################################################################# 80#################################################################
80# Determine the file name to save message into 81# Determine the file name to save message into
81################################################################# 82#################################################################
82 my $output_file = ''; 83 my $output_file = 'FIXME no output file specified';
83 if (defined($action) and $action eq 'Save as Default') { 84 if (defined($action) and $action eq 'Save as Default') {
84 $output_file = $default_msg_file; 85 $output_file = $default_msg_file;
85 } elsif ( defined($savefilename) ){ 86 } elsif ( defined($action) and ($action =~/save/i) and defined($savefilename) ){
86 $output_file = $savefilename; 87 $output_file = $savefilename;
87 } elsif ( defined($input_file) ) { 88 } elsif ( defined($input_file) ) {
88 $output_file = $input_file; 89 $output_file = $input_file;
89 } 90 }
90 91# warn "FIXME savefilename $savefilename output file $output_file";
91 ################################################################# 92 #################################################################
92 # Sanity check on save file name 93 # Sanity check on save file name
93 ################################################################# 94 #################################################################
94 95
95 if ($output_file =~ /^[~.]/ || $output_file =~ /\.\./) { 96 if ($output_file =~ /^[~.]/ || $output_file =~ /\.\./) {
96 $self->submission_error("For security reasons, you cannot specify a merge file from a directory 97 $self->submission_error("For security reasons, you cannot specify a merge file from a directory",
97 higher than the email directory (you can't use ../blah/blah). 98 "higher than the email directory (you can't use ../blah/blah). ",
98 Please specify a different file or move the needed file to the email directory"); 99 "Please specify a different file or move the needed file to the email directory",
100 );
99 } 101 }
100 102 unless ($output_file =~ m|\.msg$| ) {
101 $self->{output_file} = $output_file; 103 $self->submission_error("Invalid file name.",
104 "The file name \"$output_file\" does not have a \".msg\" extension",
105 "All email file names must end in the extension \".msg\"",
106 "choose a file name with a \".msg\" extension.",
107 "The message was not saved.",
108 );
109 }
110 $self->{output_file} = $output_file; # this is ok. It will be put back in the text input box for re-editing.
102 # FIXME $output_file can be blank if there was no savefilename 111 # FIXME $output_file can be blank if there was no savefilename
103 112
104############################################################################################# 113#############################################################################################
105# Determine input source 114# Determine input source
106############################################################################################# 115#############################################################################################
109############################################################################################# 118#############################################################################################
110# Get inputs 119# Get inputs
111############################################################################################# 120#############################################################################################
112 my($from, $replyTo, $r_text, $subject); 121 my($from, $replyTo, $r_text, $subject);
113 if ($input_source eq 'file') { 122 if ($input_source eq 'file') {
114 123# warn "FIXME obtaining source from $emailDirectory/$input_file";
115 ($from, $replyTo,$subject,$r_text) = $self->read_input_file("$emailDirectory/$input_file"); 124 ($from, $replyTo,$subject,$r_text) = $self->read_input_file("$emailDirectory/$input_file");
125# warn "FIXME Done reading source";
116 126
117 } elsif ($input_source eq 'form') { 127 } elsif ($input_source eq 'form') {
118 # read info from the form 128 # read info from the form
119 # bail if there is no message body 129 # bail if there is no message body
120 130
136 146
137############################################################################################# 147#############################################################################################
138# if no form is submitted, gather data needed to produce the mail form and return 148# if no form is submitted, gather data needed to produce the mail form and return
139############################################################################################# 149#############################################################################################
140 150
141 if(not defined($action) or $action eq 'Open' or $action eq 'Resize message window' ){ 151 if(not defined($action) or $action eq 'Open' or $action eq 'Resize message window'
152 or $action eq 'Choose merge file' ){
142# warn "FIXME action is |$action| no further initialization required"; 153# warn "FIXME action is |$action| no further initialization required";
143 return ''; 154 return '';
144 } 155 }
145 156
146 157
197# warn "FIXME from $from | subject $subject |reply $replyTo|msg $temp_body"; 208# warn "FIXME from $from | subject $subject |reply $replyTo|msg $temp_body";
198 ################################################################# 209 #################################################################
199 # overwrite protection 210 # overwrite protection
200 ################################################################# 211 #################################################################
201 if ($action eq 'Save as:' and -e "$emailDirectory/$output_file") { 212 if ($action eq 'Save as:' and -e "$emailDirectory/$output_file") {
202 $self->submission_error("The file $emailDirectory/$output_file already exists and cannot be overwritten"); 213 $self->submission_error("The file $emailDirectory/$output_file already exists and cannot be overwritten",
214 "The message was not saved");
203 return; 215 return;
204 } 216 }
205 217
206 ################################################################# 218 #################################################################
207 # Back up existing file? 219 # Back up existing file?
208 ################################################################# 220 #################################################################
209 if ($action eq 'Save as Default') { 221 if ($action eq 'Save as Default' and -e "$emailDirectory/$default_msg_file") {
210 warn "FIXME backup existing default file"; 222 rename("$emailDirectory/$default_msg_file","$emailDirectory/$old_default_msg_file") or
223 die "Can't rename $emailDirectory/$default_msg_file to $emailDirectory/$old_default_msg_file ",
224 "Check permissions for webserver on directory $emailDirectory. $!";
225 $self->{message} .= "Backup file <code>$emailDirectory/$old_default_msg_file</code> created.".CGI::br();
211 } 226 }
212 ################################################################# 227 #################################################################
213 # Save the message 228 # Save the message
214 ################################################################# 229 #################################################################
215 $self->saveProblem($temp_body, "${emailDirectory}/$output_file" ); 230 $self->saveProblem($temp_body, "${emailDirectory}/$output_file" );
216 $self->{message} = "Message saved to file ${emailDirectory}/$output_file"; 231 $self->{message} .= "Message saved to file <code>${emailDirectory}/$output_file</code>.";
217# warn "FIXME saving to ${emailDirectory}/$output_file"; 232# warn "FIXME saving to ${emailDirectory}/$output_file";
218 } elsif ($action eq 'preview') { 233 } elsif ($action eq 'preview') {
219 234
220 235
221 } elsif ($action eq 'Send Email') { 236 } elsif ($action eq 'Send Email') {
636 my $columns = $self->{columns}; 651 my $columns = $self->{columns};
637 my $rows = $self->{rows}; 652 my $rows = $self->{rows};
638 my $text = defined($self->{r_text}) ? ${ $self->{r_text} }: 'FIXME no text was produced by initialization!!'; 653 my $text = defined($self->{r_text}) ? ${ $self->{r_text} }: 'FIXME no text was produced by initialization!!';
639 my $input_file = $self->{input_file}; 654 my $input_file = $self->{input_file};
640 my $output_file = $self->{output_file}; 655 my $output_file = $self->{output_file};
656 my @sorted_messages = $self->get_message_file_names;
657 my @sorted_merge_files = $self->get_merge_file_names;
658 my $merge_file = ( defined($self->{merge_file}) ) ? $self->{merge_file} : 'None';
641 659
642 CGI::popup_menu(-name=>'classList', 660
643 -values=>\@users,
644 -labels=>\%classlistLabels,
645 -size => 10,
646 -multiple => 1,
647 -default=>'Yourself'
648 );
649 print CGI::start_form({method=>"post", action=>$r->uri()}); 661 print CGI::start_form({method=>"post", action=>$r->uri()});
650#create list of sudents 662#create list of sudents
651# show professors's name and email address 663# show professors's name and email address
652# show replyTo field and From field 664# show replyTo field and From field
653 print CGI::start_table({-border=>'2', -cellpadding=>'4'}); 665 print CGI::start_table({-border=>'2', -cellpadding=>'4'});
654 print CGI::Tr({-align=>'left',-valign=>'VCENTER'}, 666 print CGI::Tr({-align=>'left',-valign=>'VCENTER'},
655 CGI::td("Input file: $input_file","\n",CGI::br(), 667 CGI::td("Message file: $input_file","\n",CGI::br(),
656 CGI::submit(-name=>'action', -value=>'open',-label=>'Open'), "\n", 668 CGI::submit(-name=>'action', -value=>'Open'), '&nbsp;&nbsp;&nbsp;&nbsp;',"\n",
657 CGI::textfield(-name=>'openfilename', -size => 20, -value=> "$input_file", -override=>1), "\n",CGI::br(), 669 #CGI::textfield(-name=>'openfilename', -size => 20, -value=> "$input_file", -override=>1), "\n",CGI::br(),
670 CGI::popup_menu(-name=>'openfilename',
671 -values=>\@sorted_messages,
672 -default=>$input_file
673 ), "\n",CGI::br(),
674
658 "Output file: $output_file","\n",CGI::br(), 675 "Save file to: $output_file","\n",CGI::br(),
659 "\n", 'From:','&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', CGI::textfield(-name=>"from", -size=>30, -value=>$from, -override=>1), 676 "\n", 'From:','&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', CGI::textfield(-name=>"from", -size=>30, -value=>$from, -override=>1),
660 "\n", CGI::br(),'Reply-To: ', CGI::textfield(-name=>"replyTo", -size=>30, -value=>$replyTo, -override=>1), 677 "\n", CGI::br(),'Reply-To: ', CGI::textfield(-name=>"replyTo", -size=>30, -value=>$replyTo, -override=>1),
661 "\n", CGI::br(),'Subject: ', CGI::br(), CGI::textarea(-name=>'subject', -default=>$subject, -rows=>3,-columns=>40, -override=>1), 678 "\n", CGI::br(),'Subject: ', CGI::br(), CGI::textarea(-name=>'subject', -default=>$subject, -rows=>3,-columns=>40, -override=>1),
662 ), 679 ),
663 CGI::td({-align=>'left'}, 680 CGI::td({-align=>'left'},
675 ), 692 ),
676 693
677 694
678 ), 695 ),
679 CGI::td({align=>'left'}, 696 CGI::td({align=>'left'},
697 CGI::submit(-name=>'action', -value=>'Choose merge file'),
698 CGI::popup_menu(-name=>'merge_file',
699 -values=>\@sorted_merge_files,
700 -default=>$merge_file,
701 ), "\n",CGI::br(),
680 CGI::submit(-name=>'preview', -value=>'preview',-label=>'Preview')," email to ", 702 CGI::submit(-name=>'preview', -value=>'preview',-label=>'Preview')," email to ",
681 CGI::popup_menu(-name=>'classList', 703 CGI::popup_menu(-name=>'classList',
682 -values=>\@users, 704 -values=>\@users,
683 -labels=>\%classlistLabels, 705 -labels=>\%classlistLabels,
684 -default=>'Yourself' 706 -default=>'Yourself'
739############################################################################## 761##############################################################################
740# Utility methods 762# Utility methods
741############################################################################## 763##############################################################################
742sub submission_error { 764sub submission_error {
743 my $self = shift; 765 my $self = shift;
744 my $msg = join " ", @_; 766 my $msg = join( " ", @_);
745# $cgi->start_html('-title' => 'User error'),
746# $cgi->h1('User error'),
747# $cgi->p,
748# $cgi->b(HTML::Entities::encode($msg)),
749# $cgi->p,
750# "Please hit the &quot;<B>Back</B>&quot; button on your browser to ",
751# "try again, or notify ", $cgi->br,
752# "&lt;", $cgi->a({href=>"mailto:$Global::webmaster"}, $Global::webmaster), "&gt; ",
753# "if you believe this message is in error.",
754# $cgi->end_html;
755 $self->{submitError}= CGI::b(HTML::Entities::encode($msg)).CGI::br(). 767 $self->{submitError}= $msg; #CGI::b(HTML::Entities::encode($msg));
756 qq{Please hit the &quot;<B>Back</B>&quot; button on your browser to 768# qq{Please hit the &quot;<B>Back</B>&quot; button on your browser to
757 try again, or notify your web master 769# try again, or notify your web master
758 if you believe this message is in error. 770# if you believe this message is in error.
759 }; 771# };
760 return; 772 return;
761} 773}
762 774
763sub saveProblem { 775sub saveProblem {
764 my $self = shift; 776 my $self = shift;
779 my $filePath = shift; 791 my $filePath = shift;
780 my ($text, @text); 792 my ($text, @text);
781 my $header = ''; 793 my $header = '';
782 my ($subject, $from, $replyTo); 794 my ($subject, $from, $replyTo);
783 local(*FILE); 795 local(*FILE);
784 if (-e "$filePath") { 796 if (-e "$filePath" and -r "$filePath") {
785 open FILE, "$filePath" || $self->submission_error("Can't open $filePath"); 797 open FILE, "$filePath" || do { $self->submission_error("Can't open $filePath"); return};
786 while ($header !~ s/Message:\s*$//m) { 798 while ($header !~ s/Message:\s*$//m and not eof(FILE)) {
787 $header .= <FILE>; 799 $header .= <FILE>;
788 } 800 }
789 $text = join( '', <FILE>); 801 $text = join( '', <FILE>);
790 $text =~ s/^\s*//; # remove initial white space if any. 802 $text =~ s/^\s*//; # remove initial white space if any.
791 $header =~ /^From:\s(.*)$/m; 803 $header =~ /^From:\s(.*)$/m;
798 $subject = $1; 810 $subject = $1;
799 811
800 } else { 812 } else {
801 $from = $self->{defaultFrom}; 813 $from = $self->{defaultFrom};
802 $replyTo = $self->{defaultReply}; 814 $replyTo = $self->{defaultReply};
803 $text = "FIXME file $filePath doesn't exist"; 815 $text = (-e "$filePath") ? "FIXME file $filePath can't be read" :"FIXME file $filePath doesn't exist";
804 $subject = "FIXME default subject"; 816 $subject = "FIXME default subject";
805 } 817 }
806 return ($from, $replyTo, $subject, \$text); 818 return ($from, $replyTo, $subject, \$text);
807} 819}
820
821sub get_message_file_names {
822 my $self = shift;
823 my $emailDirectory = $self->{ce}->{courseDirs}->{email};
824 #get all message files and create a list
825 local(*EMAILDIR);
826 opendir( EMAILDIR, $emailDirectory )|| die "Can't access directory $emailDirectory. Please check that webserver has permission to read this directory.";
827 my @messageFiles = grep /\.msg$/, readdir EMAILDIR; #all message files
828 closedir EMAILDIR;
829
830 return sort @messageFiles;
831}
832sub get_merge_file_names {
833 my $self = shift;
834 my $scoringDirectory = $self->{ce}->{courseDirs}->{scoring};
835 #get all message files and create a list
836 local(*SCORINGDIR);
837 opendir( SCORINGDIR, $scoringDirectory )|| die "Can't access directory $scoringDirectory.",
838 "Please check that webserver has permission to read this directory.";
839 my @mergeFiles = grep( /\.csv$/, readdir SCORINGDIR); #all message files
840 closedir SCORINGDIR;
841 @mergeFiles = sort @mergeFiles;
842# warn "FIXME scoring directory $scoringDirectory merge Files", join(" ", @mergeFiles);
843 unshift(@mergeFiles, 'None');
844 return @mergeFiles;
845}
8081; 8461;

Legend:
Removed from v.1370  
changed lines
  Added in v.1371

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9