Parent Directory
|
Revision Log
nothing should change
1 #!/usr/local/bin/perl 2 3 4 5 ## This file is profDownloadScoringFiles.pl 6 ## It provides access to utilities for downloading scoring files 7 ## 8 9 #################################################################### 10 # Copyright @ 1995-1998 University of Rochester 11 # All Rights Reserved 12 #################################################################### 13 14 use lib '.'; use webworkInit; # WeBWorKInitLine 15 16 use CGI qw(:standard); 17 use Global; 18 use Auth; 19 use strict; 20 21 # begin Timing code 22 use Benchmark; 23 my $beginTime = new Benchmark; 24 # end Timing code 25 26 &CGI::ReadParse; 27 my %inputs =%main::in; 28 29 #my @inarray = %inputs; 30 #print "inputs are @inarray"; 31 32 # get information from CGI inputs (see also below for additional information) 33 34 my $Course = $inputs{'course'}; 35 my $User = $inputs{'user'}; 36 my $Session_key = $inputs{'key'}; 37 38 # verify that information has been received 39 unless($Course && $User && $Session_key) { 40 &Global::error("Script Error","The script profLogin.pl did not receive the proper input data.","",""); 41 die "The script profLogin.pl did not receive the proper input data."; 42 } 43 44 # establish environment for this script 45 46 &Global::getCourseEnvironment($inputs{'course'}); 47 48 49 my $cgiURL = getWebworkCgiURL; 50 my $databaseDirectory = getCourseDatabaseDirectory; 51 my $htmlURL = getCourseHtmlURL; 52 my $scriptDirectory = getWebworkScriptDirectory; 53 my $scoringDirectory = getCourseScoringDirectory; 54 my $dat = getDat; 55 my $dd = getDirDelim; 56 57 require "${scriptDirectory}$Global::DBglue_pl"; 58 require "${scriptDirectory}$Global::FILE_pl"; 59 require "${scriptDirectory}$Global::HTMLglue_pl"; 60 61 # log access 62 &Global::log_info('', query_string); 63 64 65 my $permissionsFile = &Global::getCoursePermissionsFile($inputs{'course'}); 66 my $permissions = &get_permissions($inputs{'user'}, $permissionsFile); 67 my $keyFile = &Global::getCourseKeyFile($inputs{'course'}); 68 69 #verify session key 70 &verify_key($inputs{'user'}, $inputs{'key'}, $keyFile, $inputs{'course'}); 71 72 # verify permissions are correct 73 if ($permissions != $Global::instructor_permissions ) { 74 print "permissions = $permissions instructor_permissions = $Global::instructor_permissions\n"; 75 print &html_NO_PERMISSION; 76 exit(0); 77 } 78 79 #&printEnvVars('html_top',''); ## printout top of html page and ENV VAR's 80 #print "\ninputs are @array\n"; ## print inputs 81 82 83 84 85 # get the rest of the information from the submitted form 86 87 my $format = $inputs{'format'}; 88 my $totalsFile = $inputs{'totalsFile'}; 89 my $scrFiles = $inputs{'scrFiles'}; 90 my $fulFiles = $inputs{'fulFiles'}; 91 my $allFiles = $inputs{'allFiles'}; 92 my $download = $inputs{'download'}; 93 my $fileName = $inputs{'fileName'}; 94 95 $totalsFile = 0 unless defined $totalsFile; 96 $scrFiles = 0 unless defined $scrFiles; 97 $fulFiles = 0 unless defined $fulFiles; 98 $allFiles = 0 unless defined $allFiles; 99 100 ## if totals file is the only one selected, download it immediately 101 if ($totalsFile == 1 and $scrFiles == 0 and $fulFiles == 0 and $allFiles == 0) { 102 $fileName = "${scoringDirectory}${Course}_totals.csv"; 103 unless (-r $fileName) { 104 &Global::error("Error:profDownloadScoringFiles.pl","The file $fileName is not readable by the webserver","",""); 105 } 106 &downloadFile($format, $fileName); 107 exit; 108 } 109 110 if ($download == 1) { 111 unless ($fileName ne '') {&selectionError();} 112 $fileName = "${scoringDirectory}$fileName"; 113 unless (-r $fileName) { 114 &Global::error("Error:profDownloadScoringFiles.pl","The file $fileName is not readable by the webserver","",""); 115 } 116 &downloadFile($format, $fileName); 117 exit; 118 } 119 120 # print HTML text 121 print &htmlTOP("Download Scoring Files"); 122 123 124 # print navigation buttons 125 print qq! 126 <A HREF="${cgiURL}profLogin.pl?user=$inputs{'user'}&key=$inputs{'key'}&course=$inputs{'course'}"> 127 <IMG SRC="${Global::upImgUrl}" align="right" BORDER=1 ALT="[Up]"></A><p> 128 !; 129 130 print <<EOF; 131 <h3 align="left">Download (or view) scoring files for $Course</h3> <p> 132 EOF 133 134 135 print <<EOF; 136 <P> 137 <HR NOSHADE> 138 <H4 ALIGN=LEFT> 139 Select files: 140 </H4> 141 142 <FORM METHOD = "POST" ACTION= "${cgiURL}profDownloadScoringFiles.pl"> 143 <INPUT TYPE="SUBMIT" VALUE="Download or View file"> 144 145 EOF 146 147 ## find the available files 148 149 opendir SCORINGDIR, $scoringDirectory or &Global::error("Error:profDownloadScoringFiles.pl","Can't open directory $scoringDirectory","",""); 150 my @allFiles = grep !/^\./, readdir SCORINGDIR; 151 closedir SCORINGDIR; 152 153 ## sort the files 154 155 my @totalsFiles = grep /${Course}_totals\.csv$/,@allFiles; 156 my @scrFiles = grep /scr\.csv$/,@allFiles; 157 my @fulFiles = grep /ful\.csv$/,@allFiles; 158 my @remainingFiles = grep !/${Course}_totals\.csv$/,@allFiles; 159 @remainingFiles = grep !/scr\.csv$/,,@remainingFiles; 160 @remainingFiles = grep !/ful\.csv$/,,@remainingFiles; 161 my @sortedNames = (); 162 if ($allFiles) { 163 @totalsFiles = sort @totalsFiles; 164 @scrFiles = sort @scrFiles; 165 @fulFiles = sort @fulFiles; 166 @remainingFiles = sort @remainingFiles; 167 @sortedNames = (@totalsFiles,@scrFiles,@fulFiles,@remainingFiles); 168 } 169 else { 170 if ($totalsFile) { 171 @totalsFiles = sort @totalsFiles; 172 @sortedNames = (@sortedNames,@totalsFiles); 173 } 174 if ($scrFiles) { 175 @scrFiles = sort @scrFiles; 176 @sortedNames = (@sortedNames, @scrFiles); 177 } 178 if ($fulFiles) { 179 @fulFiles = sort @fulFiles; 180 @sortedNames = (@sortedNames, @fulFiles); 181 } 182 } 183 ## print list of files 184 185 print qq! <SELECT Name="fileName" >\n!; 186 my ($ind); 187 for $ind (@sortedNames) { 188 print "<OPTION VALUE = \"$ind\">$ind\n"; 189 } 190 print qq!</SELECT>\n!; 191 192 ## resume printing the rest of the form 193 194 print <<EOF; 195 <INPUT TYPE="HIDDEN" NAME="user" VALUE="$inputs{'user'}"> 196 <INPUT TYPE="HIDDEN" NAME="key" VALUE="$inputs{'key'}"> 197 <INPUT TYPE="HIDDEN" NAME="course" VALUE="$inputs{'course'}"> 198 <INPUT TYPE="HIDDEN" NAME="format" VALUE="$inputs{'format'}"> 199 <INPUT TYPE="HIDDEN" NAME="download" VALUE= 1 > 200 </FORM> 201 EOF 202 203 204 205 print &htmlBOTTOM("profDownloadScoringFiles.pl", \%inputs); 206 207 # begin Timing code 208 my $endTime = new Benchmark; 209 &Global::logTimingInfo($beginTime,$endTime,"profScoring.pl",$inputs{'course'},$inputs{'user'}); 210 # end Timing code 211 exit; 212 213 sub selectionError { 214 print"content-type:\n\n<H2>Error: You must make a selection!</H2>\n"; 215 print "<FORM METHOD=POST ACTION=\"${cgiURL}profDownloadScoringFiles.pl\"><P>"; 216 print &sessionKeyInputs(\%inputs); 217 print <<"ENDOFHTML"; 218 <INPUT TYPE=SUBMIT VALUE="Return to Download Scoring Files Page"> 219 </FORM> 220 ENDOFHTML 221 print &htmlBOTTOM("profDownloadScoringFiles.pl", \%inputs); 222 exit; 223 } 224 225 sub downloadFile { 226 my ($format, $fileName) = @_; 227 my (@stringArray, $string, $contentType); 228 229 my $shortFileName = $fileName; 230 if ($shortFileName =~ m|$dd|) { 231 $shortFileName =~ m|$dd([^$dd]*)$|; ## extract filename from full path name 232 $shortFileName = $1; 233 } 234 $shortFileName =~ s|\..*||; ## remove extension 235 236 237 if ($format eq 'csv') { 238 $contentType = 'content-type: webwork/csv'; 239 open (SCOREFILE, "$fileName"); 240 @stringArray = <SCOREFILE>; 241 close (SCOREFILE); 242 $string = join '', @stringArray; 243 244 print "Content-disposition: attachment; filename= \"${shortFileName}.csv\"\n"; 245 print "$contentType\n\n"; 246 print "$string"; 247 } 248 elsif ($format eq 'html') { 249 $contentType = 'content-type: text/html'; 250 $string = &delim2html($fileName); 251 print "$contentType\n\n"; 252 print "$string"; 253 } 254 elsif ($format eq 'text') { 255 $contentType = 'content-type: text/plain'; 256 open (SCOREFILE, "$fileName"); 257 @stringArray = <SCOREFILE>; 258 close (SCOREFILE); 259 $string = join '', @stringArray; 260 print "$contentType\n\n"; 261 print "$string"; 262 } 263 elsif ($format eq 'tab') { 264 $contentType = 'content-type: webwork/txt'; 265 open (SCOREFILE, "$fileName"); 266 @stringArray = <SCOREFILE>; 267 close (SCOREFILE); 268 $string = join '', @stringArray; 269 $string =~ s|\t|_TAB_|g; 270 $string =~ s|,|\t|g; 271 print "Content-disposition: attachment; filename= \"${shortFileName}.txt\"\n"; 272 print "$contentType\n\n"; 273 print "$string"; 274 } 275 else { wwerror($0, "Unknown format $format");} 276 exit; 277 } 278 279
| aubreyja at gmail dot com | ViewVC Help |
| Powered by ViewVC 1.0.9 |