[system] / trunk / webwork / system / cgi / cgi-scripts / profFormEditScoringFiles.pl Repository:
ViewVC logotype

View of /trunk/webwork/system/cgi/cgi-scripts/profFormEditScoringFiles.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (download) (as text) (annotate)
Thu Jun 14 17:08:51 2001 UTC (18 years, 8 months ago) by sam
File size: 13313 byte(s)
initial import

    1 #!/usr/bin/perl
    2 
    3 ## This file is profFormEditScoringFiles.pl
    4 ## It provides access to utilities for downloading scoring files
    5 ##
    6 
    7 ####################################################################
    8 # Copyright @ 1995-1998 University of Rochester
    9 # All Rights Reserved
   10 ####################################################################
   11 
   12 use lib '/ww/webwork/development/'; # mainWeBWorKDirectory;
   13 use CGI qw(:standard);
   14 use Global;
   15 use Auth;
   16 use strict;
   17 use File::Copy;
   18 
   19 # begin Timing code
   20 use Benchmark;
   21 my $beginTime = new Benchmark;
   22 # end Timing code
   23 
   24 &CGI::ReadParse;
   25 my %inputs =%main::in;
   26 
   27 # get information from CGI inputs  (see also below for additional information)
   28 
   29 my $Course      = $inputs{'course'};
   30 my $User      = $inputs{'user'};
   31 my $Session_key   = $inputs{'key'};
   32 
   33 my @array = %inputs;
   34 # print "inputs are @array";
   35 
   36 # verify that information has been received
   37 unless($Course && $User && $Session_key) {
   38     &wwerror("$0","The script did not receive the proper input data.","","");
   39 }
   40 
   41 
   42 # establish environment for this script
   43 
   44 &Global::getCourseEnvironment($inputs{'course'});
   45 
   46 my $cgiURL          = getWebworkCgiURL;
   47 my $databaseDirectory     = getCourseDatabaseDirectory;
   48 my $htmlURL             = getCourseHtmlURL;
   49 my $scriptDirectory       = getWebworkScriptDirectory;
   50 my $scoringDirectory        = getCourseScoringDirectory;
   51 my $DAT                     = getDat;
   52 my $dd                      = getDirDelim;
   53 
   54 require "${scriptDirectory}$Global::DBglue_pl";
   55 require "${scriptDirectory}$Global::FILE_pl";
   56 require "${scriptDirectory}$Global::HTMLglue_pl";
   57 require "${scriptDirectory}$Global::SCRtools_pl";
   58 
   59 # log access
   60 &Global::log_info('', query_string);
   61 
   62 
   63 my $permissionsFile = &Global::getCoursePermissionsFile($inputs{'course'});
   64 my $permissions = &get_permissions($inputs{'user'}, $permissionsFile);
   65 my $keyFile = &Global::getCourseKeyFile($inputs{'course'});
   66 
   67 #verify session key
   68 &verify_key($inputs{'user'}, $inputs{'key'}, $keyFile, $inputs{'course'});
   69 
   70 # verify permissions are correct
   71 if ($permissions != $Global::instructor_permissions ) {
   72     print "permissions = $permissions instructor_permissions = $Global::instructor_permissions\n";
   73     print &html_NO_PERMISSION;
   74     exit(0);
   75 }
   76 # get the rest of the information from the submitted form
   77 
   78 
   79 my $totalsFile      = $inputs{'totalsFile'};
   80 my $scrFiles        = $inputs{'scrFiles'};
   81 my $fulFiles        = $inputs{'fulFiles'};
   82 my $fileName        = $inputs{'fileName'};
   83 my $action          = $inputs{'action'};
   84 my $readWrite       = $inputs{'readWrite'};
   85 
   86 ## define values even if check boxes not checked
   87 $totalsFile = 0 unless defined $totalsFile;
   88 $scrFiles = 0 unless defined $scrFiles;
   89 $fulFiles = 0 unless defined $fulFiles;
   90 
   91 ## if totals file is the only one selected, view it immediately
   92 
   93 OPTIONSWITCH: {
   94     if ($totalsFile == 1  and  $scrFiles == 0  and  $fulFiles == 0 and $action eq '') {
   95         $fileName = "${scoringDirectory}${Course}_totals.csv";
   96         unless (-r $fileName) {
   97             &wwerror("$0","The file $fileName is not readable by the webserver","","");
   98         }
   99         &createFormFromFile($fileName);
  100        last  OPTIONSWITCH;
  101     }
  102     if ($scrFiles > 0  or  $fulFiles > 0 and $action eq '') {
  103         &selectFilesForm();
  104         last  OPTIONSWITCH;
  105     }
  106     if ($action eq 'readFromFile') {
  107         &createFormFromFile("${scoringDirectory}$fileName");
  108         last  OPTIONSWITCH;
  109     }
  110     if ($action eq 'saveToFile') {
  111         &saveToFile(\%inputs);
  112         last  OPTIONSWITCH;
  113     }
  114     if ($action eq 'addEmptyColumn') {
  115         &addColumn(\%inputs);
  116         last  OPTIONSWITCH;
  117     }
  118     if ($action eq 'deleteLastColumn') {
  119         &deleteLastColumn(\%inputs);
  120         last  OPTIONSWITCH;
  121     }
  122     if ($totalsFile == 0  and  $scrFiles == 0  and  $fulFiles == 0 and $action eq '') {
  123        &wwerror("$0","You must check at least one check box","","");
  124        last  OPTIONSWITCH;
  125     }
  126 }
  127 
  128 # begin Timing code
  129 my $endTime = new Benchmark;
  130 &Global::logTimingInfo($beginTime,$endTime,"profScoring.pl",$inputs{'course'},$inputs{'user'});
  131 # end Timing code
  132 
  133 exit;
  134 
  135 ## subroutines
  136 
  137 
  138 sub selectFilesForm {
  139 
  140 # print HTML text
  141 print &htmlTOP("Select Scoring File for simple editing");
  142 
  143 # print navigation buttons
  144 print qq!
  145 <A HREF="${cgiURL}profLogin.pl?user=$inputs{'user'}&key=$inputs{'key'}&course=$inputs{'course'}">
  146 <IMG SRC="${Global::upImgUrl}" align="right" BORDER=1 ALT="[Up]"></A><p>
  147 !;
  148 
  149 print <<EOF;
  150 <h3 align="left">Simple editing of scoring files for $Course</h3> <p>
  151 EOF
  152 
  153 
  154 print <<EOF;
  155 <P>
  156 <HR NOSHADE>
  157 <H4 ALIGN=LEFT>
  158 Select files:
  159 </H4>
  160 
  161     <FORM METHOD = "POST" ACTION= "${cgiURL}profFormEditScoringFiles.pl">
  162          <INPUT TYPE="SUBMIT" VALUE="Edit file">
  163 
  164 EOF
  165 
  166  ## find the available files
  167 
  168 opendir SCORINGDIR, $scoringDirectory or wwerror("$0","Can't open directory $scoringDirectory","","");
  169 my @allFiles = grep !/^\./, readdir SCORINGDIR;
  170 closedir  SCORINGDIR;
  171 
  172     ## sort the files
  173 
  174 my @totalsFiles = grep /${Course}_totals\.csv$/,@allFiles;
  175 my @scrFiles =  grep /scr\.csv$/,@allFiles;
  176 my @fulFiles =  grep /ful\.csv$/,@allFiles;
  177 my  @sortedNames = ();
  178 
  179 if ($totalsFile) {
  180     @totalsFiles = sort @totalsFiles;
  181     @sortedNames = (@sortedNames,@totalsFiles);
  182 }
  183 if ($scrFiles) {
  184     @scrFiles = sort @scrFiles;
  185     @sortedNames = (@sortedNames, @scrFiles);
  186 }
  187 if ($fulFiles) {
  188     @fulFiles = sort @fulFiles;
  189     @sortedNames = (@sortedNames, @fulFiles);
  190 }
  191 
  192 ## print list of files
  193 
  194 print qq! <SELECT Name="fileName" >\n!;
  195 my ($ind);
  196 for $ind (@sortedNames) {
  197   print "<OPTION VALUE = \"$ind\">$ind\n";
  198 }
  199     print qq!</SELECT>\n!;
  200 
  201  ## resume printing the rest of the form
  202 
  203 print <<EOF;
  204          <INPUT TYPE="HIDDEN" NAME="user" VALUE="$inputs{'user'}">
  205          <INPUT TYPE="HIDDEN" NAME="key" VALUE="$inputs{'key'}">
  206          <INPUT TYPE="HIDDEN" NAME="course" VALUE="$inputs{'course'}">
  207          <INPUT TYPE="HIDDEN" NAME="action" VALUE="readFromFile">
  208          </FORM>
  209 EOF
  210 print &htmlBOTTOM("profFormEditScoringFiles.pl", \%inputs);
  211 }
  212 
  213 
  214 
  215 sub selectionError {
  216   print"content-type:\n\n<H2>Error:  You must make a selection!</H2>\n";
  217   print "<FORM METHOD=POST ACTION=\"${cgiURL}profFormEditScoringFiles.pl\"><P>";
  218   print &sessionKeyInputs(\%inputs);
  219 print <<"ENDOFHTML";
  220 <INPUT TYPE=SUBMIT VALUE="Return to Form Edit Scoring Files Page">
  221 </FORM>
  222 ENDOFHTML
  223   print &htmlBOTTOM("profFormEditScoringFiles.pl", \%inputs);
  224 }
  225 
  226 sub createFormFromFile {
  227     my ($fileName) = @_;
  228     &printTopOfForm();
  229   my $string = &delim2html($fileName,'htmlform');
  230     print "$string";
  231     &printBottomOfForm();
  232 }
  233 
  234 
  235 sub saveToFile {
  236     my ($inputsref) = @_;
  237     my $outString = &htmlForm2delim($inputsref);
  238 #print "$outString";
  239 
  240 #print "readWrite is $readWrite";
  241 
  242     unless ($readWrite eq 'readWrite') {
  243         &readWriteError();
  244         exit;
  245     }
  246 
  247     my $fulFileName     =   $inputs{'fileName'};
  248     my $shortFileName   =   $fulFileName;
  249     if ($shortFileName  =~  m|$dd|)  {
  250         $shortFileName  =~  m|$dd([^$dd]*)$|;  ## extract filename from full path name
  251         $shortFileName  =   $1;
  252     }
  253     $shortFileName      =~  s|\..*||;      ## remove extension
  254 
  255     ## check for validity of format before saving file
  256     my $i=1;
  257     while(-e "${scoringDirectory}temp$i.$DAT") {$i++;}      ##don't overwrite existing files
  258   my $tempFileName ="${scoringDirectory}temp$i.$DAT";
  259     open(SCORES,">$tempFileName")   or
  260       &wwerror("$0","Can't open $tempFileName","","", query_string());
  261     print SCORES "$outString";
  262     close SCORES;
  263   my $shortTempFileName = "temp$i";
  264     my $msg = htmlcheckdat($shortTempFileName);
  265             ## remove temp file
  266     unlink $tempFileName or &wwerror($0, "Could not remove the file $shortTempFileName");
  267     unless ($msg eq 'OK') {
  268         $msg .= "<BR>GO BACK AND CORRECT YOUR FORM. <BR>FILE NOT SAVED.";
  269         &wwerror("$0","$msg","","", query_string());
  270     }
  271     ## now that we know the file has a valid format, we can save it
  272 
  273     &backup("$shortFileName");   ##make up to three backups
  274     my $gid = $Global::numericalGroupID;
  275     $gid = $Global::numericalGroupID;    ## hack to remove warning message about $Global::numericalGroupID used only once
  276 
  277     my $tmpFileName = "${fulFileName}.tmp";
  278     &createFile($tmpFileName, 0660, $gid);
  279     open(SCORES,">$tmpFileName")   or
  280       &wwerror("$0","Can't open $tmpFileName");
  281     print SCORES "$outString";
  282     close SCORES;
  283     move("$tmpFileName","$fulFileName") or
  284       &wwerror("$0","Can't save $fulFileName");
  285     &readWriteSuccess($fulFileName);
  286 }
  287 
  288 sub readWriteError {
  289     print"content-type:\n\n<H2>No changes made:  You are in read only mode</H2>\n";
  290     print "Use your browser's BacK Button to return to the previous page or<BR>";
  291     print "<FORM METHOD=POST ACTION=\"${cgiURL}profScoring.pl\"><P>";
  292     print &sessionKeyInputs(\%inputs);
  293     print <<"ENDOFHTML";
  294     <INPUT TYPE=SUBMIT VALUE="Return to Scoring Page">
  295     </FORM>
  296 ENDOFHTML
  297     print &htmlBOTTOM("profFormEditScoringFiles.pl", \%inputs);
  298 }
  299 
  300 sub readWriteSuccess {
  301   my ($savedFile) = @_;
  302   print"content-type:\n\n<H2>Success, file saved. </H2>\n";
  303   print "The contentsof your page have been saved in the file:<BR>$savedFile";
  304 
  305   print "<FORM METHOD=POST ACTION=\"${cgiURL}profScoring.pl\"><P>";
  306   print &sessionKeyInputs(\%inputs);
  307 print <<"ENDOFHTML";
  308 <INPUT TYPE=SUBMIT VALUE="Return to Scoring Page">
  309 </FORM>
  310 ENDOFHTML
  311   print &htmlBOTTOM("profFormEditScoringFiles.pl", \%inputs);
  312 }
  313 
  314 sub addColumn {
  315     my ($inputsref) = @_;
  316     my $inString = &htmlForm2delim($inputsref);
  317 #print "$inString";
  318   my @inArray = split("\n",$inString);
  319   my $row;
  320   my @outArray =();
  321   foreach $row (@inArray) {
  322     $row .= ', ';
  323     push @outArray, $row;
  324   }
  325     &createFormFromDelimitedArray(\@outArray,$fileName);
  326     exit;
  327 }
  328 
  329 
  330 sub deleteLastColumn {
  331     my ($inputsref) = @_;
  332     my $inString = &htmlForm2delim($inputsref);
  333 #print "$inString";
  334   my @inArray = split("\n",$inString);
  335   my $row;
  336   my @outArray =();
  337   foreach $row (@inArray) {
  338     $row =~ s|,[^,]*$||;
  339     push @outArray, $row;
  340   }
  341     &createFormFromDelimitedArray(\@outArray,$fileName);
  342     exit;
  343 }
  344 
  345 sub createFormFromDelimitedArray {
  346     my ($inArrayref,$fileName) = @_;
  347     &printTopOfForm();
  348 
  349     my $shortFileName = $fileName;
  350     if ($shortFileName =~ m|$dd|)  {
  351         $shortFileName =~ m|$dd([^$dd]*)$|;  ## extract filename from full path name
  352         $shortFileName = $1;
  353     }
  354     $shortFileName =~ s|\..*||;      ## remove extension
  355   my $string = &delimitedArray2html($inArrayref, $shortFileName,'htmlform');
  356     print "$string";
  357     &printBottomOfForm();
  358 }
  359 
  360 sub printTopOfForm {
  361     print "content-type: text/html \n\n" ;
  362 
  363     # print navigation buttons
  364     print qq!
  365     <A HREF="${cgiURL}profScoring.pl?user=$inputs{'user'}&key=$inputs{'key'}&course=$inputs{'course'}">
  366     <IMG SRC="${Global::upImgUrl}" align="right" BORDER=1 ALT="[Up]"></A><p>
  367     !;
  368 
  369     print <<EOF;
  370     <FORM METHOD = "POST" ACTION= "${cgiURL}profFormEditScoringFiles.pl">
  371     <INPUT TYPE="SUBMIT" NAME = "action" VALUE="saveToFile">
  372     <INPUT TYPE="SUBMIT" NAME = "action" VALUE="addEmptyColumn">
  373     <INPUT TYPE="SUBMIT" NAME = "action" VALUE="deleteLastColumn">
  374     <INPUT TYPE="RESET" VALUE = "Reset Form"> <BR>
  375         <INPUT TYPE="radio" CHECKED NAME="readWrite" VALUE="readOnly" > Read Only Mode for saveToFile <BR>
  376         <INPUT TYPE="radio" NAME="readWrite" VALUE="readWrite"> Read/Write Mode for saveToFile<BR>
  377         <strong>Changes are not saved until the "saveToFile" button is pressed and you see the message "Success, file saved".<BR>
  378         Three backups of your file will be kept, but please be careful.</strong><BR>
  379 
  380 EOF
  381 }
  382 
  383 sub printBottomOfForm {
  384    print <<EOF;
  385          <INPUT TYPE="HIDDEN" NAME="user" VALUE="$inputs{'user'}">
  386          <INPUT TYPE="HIDDEN" NAME="key" VALUE="$inputs{'key'}">
  387          <INPUT TYPE="HIDDEN" NAME="course" VALUE="$inputs{'course'}">
  388          <INPUT TYPE="HIDDEN" NAME="fileName" VALUE="$fileName">
  389          </FORM>
  390 EOF
  391 print &htmlBOTTOM("profFormEditScoringFiles.pl", \%inputs);
  392 }

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9