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

View of /trunk/webwork/system/cgi/cgi-scripts/profDownloadClasslistFiles.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: 8580 byte(s)
initial import

    1 #!/usr/bin/perl
    2 
    3 
    4 
    5 ## This file is profDownloadClasslistFiles.pl
    6 ## It provides access to utilities for downloading classlist files
    7 ##
    8 
    9 ####################################################################
   10 # Copyright @ 1995-1998 University of Rochester
   11 # All Rights Reserved
   12 ####################################################################
   13 
   14 use lib '/ww/webwork/development/'; # mainWeBWorKDirectory;
   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     wwerror($0,"The script profLogin.pl did not receive the proper input data.","","");
   41 }
   42 
   43 # establish environment for this script
   44 
   45 &Global::getCourseEnvironment($inputs{'course'});
   46 
   47 
   48 my $cgiURL          = getWebworkCgiURL;
   49 my $databaseDirectory     = getCourseDatabaseDirectory;
   50 my $htmlURL             = getCourseHtmlURL;
   51 my $scriptDirectory       = getWebworkScriptDirectory;
   52 my $templateDirectory     = getCourseTemplateDirectory;
   53 my $dd                      = getDirDelim;
   54 
   55 require "${scriptDirectory}$Global::DBglue_pl";
   56 require "${scriptDirectory}$Global::FILE_pl";
   57 require "${scriptDirectory}$Global::HTMLglue_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 
   78 #&printEnvVars('html_top','');  ## printout top of html page and ENV VAR's
   79 #print "\ninputs are @array\n"; ## print inputs
   80 
   81 
   82 
   83 
   84 # get the rest of the information from the submitted form
   85 
   86 my $format          = $inputs{'format'};
   87 my $download        = $inputs{'download'};
   88 my $fileName        = $inputs{'fileName'};
   89 
   90 
   91 if ($download == 1) {
   92     unless ($fileName ne '') {&selectionError();}
   93     $fileName = "${templateDirectory}$fileName";
   94     unless (-r $fileName) {
   95         &wwerror($0,"The file $fileName is not readable by the webserver","","");
   96     }
   97     &downloadFile($format, $fileName);
   98     exit;
   99 }
  100 
  101 # print HTML text
  102 print &htmlTOP("Download Classlist Files");
  103 
  104 
  105 # print navigation buttons
  106 print qq!
  107 <A HREF="${cgiURL}profClasslist.pl?user=$inputs{'user'}&key=$inputs{'key'}&course=$inputs{'course'}">
  108 <IMG SRC="${Global::upImgUrl}" align="right" BORDER=1 ALT="[Up]"></A><p>
  109 !;
  110 
  111 print <<EOF;
  112 <h3 align="left">Download (or view) classlist files for $Course</h3> <p>
  113 EOF
  114 
  115 
  116 print <<EOF;
  117 <P>
  118 <HR NOSHADE>
  119 <H4 ALIGN=LEFT>
  120 Select files:
  121 </H4>
  122 
  123     <FORM METHOD = "POST" ACTION= "${cgiURL}profDownloadClasslistFiles.pl">
  124          <INPUT TYPE="SUBMIT" VALUE="Download or View file">
  125 
  126 EOF
  127 
  128  ## find the available files
  129 
  130 opendir CLASSLISTDIR, $templateDirectory or wweror($0,"Can't open directory $templateDirectory","","");
  131 my @allFiles = grep !/^\./, readdir CLASSLISTDIR;
  132 closedir  CLASSLISTDIR;
  133 
  134     ## sort the files
  135 
  136 my @classlistFiles = grep /\.lst$/,@allFiles;
  137 my @sortedNames = sort @classlistFiles;
  138     ## put the default classlist file first if it exists
  139 my $shortFileName = $defaultClasslistFile;
  140     if ($shortFileName =~ m|$dd|)  {
  141         $shortFileName =~ m|$dd([^$dd]*)$|;  ## extract filename from full path name
  142         $shortFileName = $1;
  143     }
  144 my @newSortedNames  = grep !/^$shortFileName$/, @sortedNames;
  145 if ($#newSortedNames != $#sortedNames) {
  146     unshift @newSortedNames,$shortFileName;
  147     @sortedNames = @newSortedNames;
  148 }
  149     ## print list of files
  150 
  151 print qq! <SELECT Name="fileName" >\n!;
  152 my ($ind,$label,$date,@stat);
  153 for $ind (@sortedNames) {
  154     $fileName = "${templateDirectory}$ind";
  155       if (-e $fileName) {
  156           @stat = stat($fileName);
  157           $date = $stat[9];
  158           $date = formatDateAndTime($date);
  159           $date =~ s|\s*at.*||;
  160           $label = "  Last Changed $date";
  161       }
  162 
  163   print "<OPTION VALUE = \"$ind\">$ind $label\n";
  164 }
  165     print qq!</SELECT>\n!;
  166 
  167  ## resume printing the rest of the form
  168 
  169 print <<EOF;
  170          <INPUT TYPE="HIDDEN" NAME="user" VALUE="$inputs{'user'}">
  171          <INPUT TYPE="HIDDEN" NAME="key" VALUE="$inputs{'key'}">
  172          <INPUT TYPE="HIDDEN" NAME="course" VALUE="$inputs{'course'}">
  173          <INPUT TYPE="HIDDEN" NAME="format" VALUE="$inputs{'format'}">
  174          <INPUT TYPE="HIDDEN" NAME="download" VALUE= 1 >
  175          </FORM>
  176 EOF
  177 
  178 
  179 
  180 print &htmlBOTTOM("profDownloadClasslistFiles.pl", \%inputs);
  181 
  182 # begin Timing code
  183 my $endTime = new Benchmark;
  184 &Global::logTimingInfo($beginTime,$endTime,"profClasslist.pl",$inputs{'course'},$inputs{'user'});
  185 # end Timing code
  186 exit;
  187 
  188 sub selectionError {
  189   print"content-type:\n\n<H2>Error:  You must make a selection!</H2>\n";
  190   print "<FORM METHOD=POST ACTION=\"${cgiURL}profDownloadClasslistFiles.pl\"><P>";
  191   print &sessionKeyInputs(\%inputs);
  192 print <<"ENDOFHTML";
  193 <INPUT TYPE=SUBMIT VALUE="Return to Download Classlist Files Page">
  194 </FORM>
  195 ENDOFHTML
  196   print &htmlBOTTOM("profDownloadClasslistFiles.pl", \%inputs);
  197   exit;
  198 }
  199 
  200 sub downloadFile {
  201     my ($format, $fileName) = @_;
  202     my (@stringArray, $string, $contentType);
  203 
  204     my $shortFileName   =   $fileName;
  205     if ($shortFileName  =~  m|$dd|)  {
  206         $shortFileName  =~  m|$dd([^$dd]*)$|;  ## extract filename from full path name
  207         $shortFileName  =   $1;
  208     }
  209     $shortFileName      =~  s|\..*||;      ## remove extension
  210 
  211 
  212     if ($format eq 'csv') {
  213         $contentType = 'content-type: webwork/csv';
  214         open (CLASSLISTFILE, "$fileName");
  215         @stringArray = <CLASSLISTFILE>;
  216         close (CLASSLISTFILE);
  217         $string = join '', @stringArray;
  218 
  219         print "Content-disposition: attachment; filename= \"${shortFileName}.csv\"\n";
  220         print "$contentType\n\n";
  221         print "$string";
  222     }
  223     elsif ($format eq 'html') {
  224         $contentType = 'content-type: text/html';
  225         $string = &delim2html($fileName);
  226         print "$contentType\n\n";
  227         print "$string";
  228     }
  229     elsif ($format eq 'text') {
  230         $contentType = 'content-type: text/plain';
  231         open (CLASSLISTFILE, "$fileName");
  232         @stringArray = <CLASSLISTFILE>;
  233         close (CLASSLISTFILE);
  234         $string = join '', @stringArray;
  235         print "$contentType\n\n";
  236         print "$string";
  237     }
  238     elsif ($format eq 'tab') {
  239         $contentType = 'content-type: webwork/txt';
  240         open (CLASSLISTFILE, "$fileName");
  241         @stringArray = <CLASSLISTFILE>;
  242         close (CLASSLISTFILE);
  243         $string = join '', @stringArray;
  244         $string =~ s|\t|_TAB_|g;
  245         $string =~ s|,|\t|g;
  246         print "Content-disposition: attachment; filename= \"${shortFileName}.txt\"\n";
  247         print "$contentType\n\n";
  248         print "$string";
  249     }
  250     else { wwerror($0, "Unknown format $format");}
  251     exit;
  252 }
  253 
  254 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9