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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1059 - (download) (as text) (annotate)
Sat Jun 7 04:30:31 2003 UTC (9 years, 11 months ago) by gage
File size: 14479 byte(s)
Restoring header #!/usr/local/bin/webwork-perl line
--Mike

    1 #!/usr/local/bin/webwork-perl
    2 
    3 ## This file is profFormEditClasslistFiles.pl
    4 ## It provides access to utilities for downloading Classlist files
    5 ##
    6 
    7 ####################################################################
    8 # Copyright @ 1995-1998 University of Rochester
    9 # All Rights Reserved
   10 ####################################################################
   11 
   12 use lib '.'; use webworkInit; # WeBWorKInitLine
   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 $templateDirectory     = getCourseTemplateDirectory;
   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 my $defaultClasslistFile = getCourseClasslistFile($Course);
   67 
   68 #verify session key
   69 &verify_key($inputs{'user'}, $inputs{'key'}, $keyFile, $inputs{'course'});
   70 
   71 # verify permissions are correct
   72 if ($permissions != $Global::instructor_permissions ) {
   73     print "permissions = $permissions instructor_permissions = $Global::instructor_permissions\n";
   74     print &html_NO_PERMISSION;
   75     exit(0);
   76 }
   77 # get the rest of the information from the submitted form
   78 
   79 my $fileName        = $inputs{'fileName'};
   80 my $action          = $inputs{'action'};
   81 my $readWrite       = $inputs{'readWrite'};
   82 
   83 
   84 
   85 ## if totals file is the only one selected, view it immediately
   86 
   87 OPTIONSWITCH: {
   88     if ($action eq '') {
   89         &selectFilesForm();
   90         last  OPTIONSWITCH;
   91     }
   92     if ($action eq 'readFromFile') {
   93         &createFormFromFile("${templateDirectory}$fileName");
   94         last  OPTIONSWITCH;
   95     }
   96     if ($action eq 'saveToFile') {
   97         &saveToFile(\%inputs);
   98         last  OPTIONSWITCH;
   99     }
  100     if ($action eq 'duplicateLastRow') {
  101         &duplicateLastRow(\%inputs);
  102         last  OPTIONSWITCH;
  103     }
  104     if ($action eq 'addEmptyRow') {
  105         &addEmptyRow(\%inputs);
  106         last  OPTIONSWITCH;
  107     }
  108     if ($action eq 'deleteLastRow') {
  109         &deleteLastRow(\%inputs);
  110         last  OPTIONSWITCH;
  111     }
  112 
  113 }
  114 
  115 # begin Timing code
  116 my $endTime = new Benchmark;
  117 &Global::logTimingInfo($beginTime,$endTime,"profClasslist.pl",$inputs{'course'},$inputs{'user'});
  118 # end Timing code
  119 
  120 exit;
  121 
  122 ## subroutines
  123 
  124 
  125 sub selectFilesForm {
  126 
  127 # print HTML text
  128 print &htmlTOP("Select Classlist File for simple editing");
  129 
  130 # print navigation buttons
  131 print qq!
  132 <A HREF="${cgiURL}profClasslist.pl?user=$inputs{'user'}&key=$inputs{'key'}&course=$inputs{'course'}">
  133 <IMG SRC="${Global::upImgUrl}" align="right" BORDER=1 ALT="[Up]"></A><p>
  134 !;
  135 
  136 print <<EOF;
  137 <h3 align="left">Simple editing of classlist files for $Course</h3> <p>
  138 EOF
  139 
  140 
  141 print <<EOF;
  142 <P>
  143 <HR NOSHADE>
  144 <H4 ALIGN=LEFT>
  145 Select files:
  146 </H4>
  147 
  148     <FORM METHOD = "POST" ACTION= "${cgiURL}profFormEditClasslistFiles.pl">
  149          <INPUT TYPE="SUBMIT" VALUE="Edit file">
  150 
  151 EOF
  152 
  153  ## find the available files
  154 
  155 opendir CLASSLISTDIR, $templateDirectory or wwerror($0,"Can't open directory $templateDirectory","","");
  156 my @allFiles = grep !/^\./, readdir CLASSLISTDIR;
  157 closedir  CLASSLISTDIR;
  158 
  159     ## sort the files
  160 
  161 my @classlistFiles = grep /\.lst$/,@allFiles;
  162 my @sortedNames = sort @classlistFiles;
  163     ## put the default classlist file first if it exists
  164 my $shortFileName = $defaultClasslistFile;
  165     if ($shortFileName =~ m|$dd|)  {
  166         $shortFileName =~ m|$dd([^$dd]*)$|;  ## extract filename from full path name
  167         $shortFileName = $1;
  168     }
  169 my @newSortedNames  = grep !/^$shortFileName$/, @sortedNames;
  170 if ($#newSortedNames != $#sortedNames) {
  171     unshift @newSortedNames,$shortFileName;
  172     @sortedNames = @newSortedNames;
  173 }
  174 
  175     ## print list of files
  176 
  177 print qq! <SELECT Name="fileName" >\n!;
  178 my ($ind,$label,$date,@stat);
  179 for $ind (@sortedNames) {
  180     $fileName = "${templateDirectory}$ind";
  181       if (-e $fileName) {
  182           @stat = stat($fileName);
  183           $date = $stat[9];
  184           $date = formatDateAndTime($date);
  185           $date =~ s|\s*at.*||;
  186           $label = "  Last Changed $date";
  187       }
  188 
  189   print "<OPTION VALUE = \"$ind\">$ind $label\n";
  190 }
  191     print qq!</SELECT>\n!;
  192 
  193  ## resume printing the rest of the form
  194 
  195 print <<EOF;
  196          <INPUT TYPE="HIDDEN" NAME="user" VALUE="$inputs{'user'}">
  197          <INPUT TYPE="HIDDEN" NAME="key" VALUE="$inputs{'key'}">
  198          <INPUT TYPE="HIDDEN" NAME="course" VALUE="$inputs{'course'}">
  199          <INPUT TYPE="HIDDEN" NAME="action" VALUE="readFromFile">
  200          </FORM>
  201 EOF
  202 print &htmlBOTTOM("profFormEditClasslistFiles.pl", \%inputs);
  203 }
  204 
  205 
  206 
  207 sub selectionError {
  208   print"content-type:\n\n<H2>Error:  You must make a selection!</H2>\n";
  209   print "<FORM METHOD=POST ACTION=\"${cgiURL}profFormEditClasslistFiles.pl\"><P>";
  210   print &sessionKeyInputs(\%inputs);
  211 print <<"ENDOFHTML";
  212 <INPUT TYPE=SUBMIT VALUE="Return to Form Edit Classlist Files Page">
  213 </FORM>
  214 ENDOFHTML
  215   print &htmlBOTTOM("profFormEditClasslistFiles.pl", \%inputs);
  216 }
  217 
  218 
  219 sub createFormFromFile {
  220     my ($fileName) = @_;
  221     &printTopOfForm();
  222     print  'Three backups of your file will be kept, but please be careful.<BR>
  223     The format is: <pre> Student_ID  Last_Name  First_Name  Status   Comment  Section  Recitation  Email_Address  Student_Login</pre>';
  224   my $string = &delim2html($fileName,'htmlform');
  225     print "${string}<BR>";
  226     &printSubmitButtons();
  227     &printBottomOfForm();
  228 }
  229 
  230 sub createFormFromDelimitedArray {
  231     my ($inArrayref,$fileName) = @_;
  232     &printTopOfForm();
  233     print  'Three backups of your file will be kept, but please be careful.<BR>
  234     The format is: <pre> Student_ID  Last_Name  First_Name  Status   Comment  Section  Recitation  Email_Address  Student_Login</pre>';
  235 
  236     my $shortFileName = $fileName;
  237     if ($shortFileName =~ m|$dd|)  {
  238         $shortFileName =~ m|$dd([^$dd]*)$|;  ## extract filename from full path name
  239         $shortFileName = $1;
  240     }
  241     $shortFileName =~ s|\..*||;      ## remove extension
  242   my $string = &delimitedArray2html($inArrayref, $shortFileName,'htmlform');
  243     print "${string}<BR>";
  244     &printSubmitButtons();
  245     &printBottomOfForm();
  246 }
  247 
  248 sub saveToFile {
  249     my ($inputsref) = @_;
  250     my $outString = &htmlForm2delim($inputsref);
  251 #print "$outString";
  252 
  253 #print "readWrite is $readWrite";
  254 
  255     unless ($readWrite eq 'readWrite') {
  256         &readWriteError();
  257         exit;
  258     }
  259 
  260     my $fulFileName     =   $inputs{'fileName'};
  261     my $shortFileName   =   $fulFileName;
  262     if ($shortFileName  =~  m|$dd|)  {
  263         $shortFileName  =~  m|$dd([^$dd]*)$|;  ## extract filename from full path name
  264         $shortFileName  =   $1;
  265     }
  266     $shortFileName      =~  s|\..*||;      ## remove extension
  267 
  268     $fulFileName = "${templateDirectory}$fulFileName";
  269 
  270     my @outArray = split /\n/, $outString;
  271     my $msg = checkClasslistArray($Global::noOfFieldsInClasslist,\@outArray,'form');
  272 
  273     unless ($msg eq 'OK') {
  274         $msg .= "<BR>GO BACK AND CORRECT YOUR FORM. <BR>FILE NOT SAVED.";
  275         &wwerror("$0","$msg","","", query_string());
  276     }
  277     ## now that we know the file has a valid format, we can save it
  278 
  279     &backupFile("$fulFileName");   ##make up to three backups
  280     my $gid = $Global::numericalGroupID;
  281     $gid = $Global::numericalGroupID;    ## hack to remove warning message about $Global::numericalGroupID used only once
  282     my $tmpFileName = "${fulFileName}.tmp";
  283     &createFile($tmpFileName, $Global::classlist_file_permission, $gid);
  284     open(CLASSLISTFH,">$tmpFileName")   or
  285       &wwerror("$0","Can't open $tmpFileName");
  286     print CLASSLISTFH "$outString";
  287     close CLASSLISTFH;
  288     move("$tmpFileName","$fulFileName") or
  289       &wwerror("$0","Can't save $fulFileName");
  290     &readWriteSuccess($fulFileName);
  291 }
  292 
  293 sub readWriteError {
  294     print"content-type:\n\n<H2>No changes made:  You are in read only mode</H2>\n";
  295     print "Use your browser's BacK Button to return to the previous page or<BR>";
  296     print "<FORM METHOD=POST ACTION=\"${cgiURL}profClasslist.pl\"><P>";
  297     print &sessionKeyInputs(\%inputs);
  298     print <<"ENDOFHTML";
  299     <INPUT TYPE=SUBMIT VALUE="Return to Classlist Page">
  300     </FORM>
  301 ENDOFHTML
  302     print &htmlBOTTOM("profFormEditClasslistFiles.pl", \%inputs);
  303 }
  304 
  305 sub readWriteSuccess {
  306   my ($savedFile) = @_;
  307   print"content-type:\n\n<H2>Success, file saved. </H2>\n";
  308   print "The contentsof your page have been saved in the file:<BR>$savedFile";
  309 
  310   print "<FORM METHOD=POST ACTION=\"${cgiURL}profClasslist.pl\"><P>";
  311   print &sessionKeyInputs(\%inputs);
  312 print <<"ENDOFHTML";
  313 <INPUT TYPE=SUBMIT VALUE="Return to Classlist Page">
  314 </FORM>
  315 ENDOFHTML
  316   print &htmlBOTTOM("profFormEditClasslistFiles.pl", \%inputs);
  317 }
  318 
  319 sub duplicateLastRow {
  320     my ($inputsref) = @_;
  321     my $inString = &htmlForm2delim($inputsref);
  322 # print "$inString";
  323   my @inArray = split("\n",$inString);
  324   my $row = $inArray[$#inArray];
  325   push @inArray, $row;
  326     &createFormFromDelimitedArray(\@inArray,$fileName);
  327     exit;
  328 }
  329 
  330 sub addEmptyRow {
  331     my ($inputsref) = @_;
  332     my $inString = &htmlForm2delim($inputsref);
  333 # print "$inString";
  334   my @inArray = split("\n",$inString);
  335   my $row = $inArray[$#inArray];
  336   $row =~ s|[^,]*?,|,|g;
  337   $row =~ s|,[^,]*|,|g;
  338   push @inArray, $row;
  339     &createFormFromDelimitedArray(\@inArray,$fileName);
  340     exit;
  341 }
  342 
  343 sub deleteLastRow {
  344     my ($inputsref) = @_;
  345     my $inString = &htmlForm2delim($inputsref);
  346 #print "$inString";
  347   my @inArray = split("\n",$inString);
  348   pop @inArray;
  349     &createFormFromDelimitedArray(\@inArray,$fileName);
  350     exit;
  351 }
  352 
  353 
  354 
  355 sub printTopOfForm {
  356     print "content-type: text/html \n\n" ;
  357 
  358     # print navigation buttons
  359     print qq!
  360     <A HREF="${cgiURL}profClasslist.pl?user=$inputs{'user'}&key=$inputs{'key'}&course=$inputs{'course'}">
  361     <IMG SRC="${Global::upImgUrl}" align="right" BORDER=1 ALT="[Up]"></A><p>
  362     !;
  363     &printSubmitButtons;
  364 
  365 }
  366 
  367 sub printSubmitButtons {
  368     print <<EOF;
  369     <FORM METHOD = "POST" ACTION= "${cgiURL}profFormEditClasslistFiles.pl">
  370     <INPUT TYPE="SUBMIT" NAME = "action" VALUE='saveToFile'>
  371     <INPUT TYPE="SUBMIT" NAME = "action" VALUE='duplicateLastRow'>
  372     <INPUT TYPE="SUBMIT" NAME = "action" VALUE='addEmptyRow'>
  373     <INPUT TYPE="SUBMIT" NAME = "action" VALUE='deleteLastRow'>
  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".</strong><BR>
  378 
  379 EOF
  380 }
  381 
  382 sub printBottomOfForm {
  383    print <<EOF;
  384          <INPUT TYPE="HIDDEN" NAME="user" VALUE="$inputs{'user'}">
  385          <INPUT TYPE="HIDDEN" NAME="key" VALUE="$inputs{'key'}">
  386          <INPUT TYPE="HIDDEN" NAME="course" VALUE="$inputs{'course'}">
  387          <INPUT TYPE="HIDDEN" NAME="fileName" VALUE="$fileName">
  388          </FORM>
  389 EOF
  390 print &htmlBOTTOM("profFormEditClasslistFiles.pl", \%inputs);
  391 }
  392 
  393 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9