File:  [LON-CAPA] / loncom / interface / multidownload.pl
Revision 1.22: download - view: text, annotated - select for diffs
Mon Apr 30 18:22:39 2007 UTC (17 years ago) by banghart
Branches: MAIN
CVS tags: HEAD
	Include manifest.txt in zip output listing files contained in zip.
	Needs refinement.

    1: #!/usr/bin/perl
    2: # CGI-script to allow download of all essay submissions of 
    3: # multiple students.
    4: #
    5: # $Id: multidownload.pl,v 1.22 2007/04/30 18:22:39 banghart Exp $
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: #
   11: # LON-CAPA is free software; you can redistribute it and/or modify
   12: # it under the terms of the GNU General Public License as published by
   13: # the Free Software Foundation; either version 2 of the License, or
   14: # (at your option) any later version.
   15: #
   16: # LON-CAPA is distributed in the hope that it will be useful,
   17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19: # GNU General Public License for more details.
   20: #
   21: # You should have received a copy of the GNU General Public License
   22: # along with LON-CAPA; if not, write to the Free Software
   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   24: #
   25: # /home/httpd/html/adm/gpl.txt
   26: #
   27: # http://www.lon-capa.org/
   28: #
   29: 
   30: use lib '/home/httpd/lib/perl';
   31: use LONCAPA::loncgi;
   32: use File::Path;
   33: use File::Basename;
   34: use File::Copy;
   35: use IO::File;
   36: use Apache::lonhtmlcommon();
   37: use Apache::lonnavmaps;
   38: use Apache::lonnet;
   39: use Apache::loncommon();
   40: use Apache::lonlocal;
   41: use Apache::lonmsg();
   42: use Apache::lonnet;
   43: use LONCAPA::Enrollment;
   44: use strict;
   45: 
   46: $|=1;
   47: if (! &LONCAPA::loncgi::check_cookie_and_load_env()) {
   48:     print <<END;
   49: Content-type: text/html
   50: 
   51: <html>
   52: <head><title>Bad Cookie</title></head>
   53: <body>
   54: Your cookie information is incorrect.
   55: </body>
   56: </html>
   57: END
   58:     return;
   59: }
   60: &Apache::lonlocal::get_language_handle();
   61: &Apache::loncommon::content_type(undef,'text/html');
   62: my $identifier = $ENV{'QUERY_STRING'};
   63: my $unique_path = $identifier.time();
   64: print(&Apache::loncommon::start_page('Multiple Downloads'));
   65: 
   66: my $scope = $env{'request.course.id'};
   67: if ($env{'request.course.sec'}) {
   68:     $scope .= '/'.$env{'request.course.sec'};
   69: }
   70: if (&Apache::lonnet::allowed('vgr',$scope) eq 'F') {
   71:     my $symb = $env{'cgi.'.$identifier.'.symb'};
   72:     my $navmap = Apache::lonnavmaps::navmap->new();
   73:     my $res = $navmap->getBySymb($symb);
   74:     my $partlist = $res->parts();
   75:     my ($zipout) = ($symb =~ /^.*\/(.+)\.problem$/);
   76:     $zipout =~ s/\s/_/g;
   77:     $zipout .= ".zip";
   78:     my $courseid = $env{'request.course.id'};
   79:     my @stuchecked = split(/\n/,$env{'cgi.'.$identifier.'.students'});
   80:     my @parts = split(/\n/,$env{'cgi.'.$identifier.'.parts'});
   81:     my $doc_zip_root = $Apache::lonnet::perlvar{'lonZipDir'};
   82:     my $uname = $env{'user.name'};
   83:     my $udom = $env{'user.domain'};
   84:     my $unique_user = $uname.":".$udom;
   85:     &mkpath($doc_zip_root."/zipdir/$unique_user/$unique_path",0,0700);
   86:     if (!open MANIFEST, ">$doc_zip_root/zipdir/$unique_user/$unique_path/manifest.txt") {
   87:         &Apache::lonnet::logthis("Problem making manifest");
   88:     }
   89:     print MANIFEST "Files contained in this zip:\n";
   90:     my $file_problem = 0;
   91:     foreach my $stu (@stuchecked) {
   92:         my %files_saved;
   93:         my ($stuname,$studom,$fullname) = split(/:/,$stu);
   94:         my %record = &Apache::lonnet::restore($symb,$courseid,$studom,$stuname);
   95:         my $file_url = '/uploaded/'.$studom.'/'.$stuname.'/portfolio';
   96:         print MANIFEST $fullname."\n";
   97:         foreach my $partid (@$partlist) {
   98:             my @ids = $res->responseIds($partid);
   99:             foreach my $respid(@ids) {
  100:                 &mkpath($doc_zip_root."/zipdir/$unique_user/$unique_path/$stuname/part$partid/resp$respid",0,0700);
  101:                 foreach my $file (split(',',$record{"resource.$partid.$respid.portfiles"})) {
  102:                     $file = $file_url.$file;
  103:                     my ($file_name_only) = ($file =~ /^.*\/(.+$)/);
  104:                     print MANIFEST "\t$file_name_only \n";
  105:                     &Apache::lonnet::repcopy($file);
  106:                     my $source = &Apache::lonnet::filelocation("",$file);
  107:                     my $destination = "$doc_zip_root/zipdir/$unique_user/$unique_path/$stuname/part$partid/resp$respid/$file_name_only";
  108:                     if (exists($files_saved{$destination})) {
  109:                         # file has already been saved once
  110:                         my ($file_name,$file_ext) = ($destination =~ /(^.*)(\..+$)/);
  111:                         $destination = $file_name.$files_saved{$destination}.$file_ext;
  112:                         $files_saved{$destination} ++;
  113:                     }
  114:                     $files_saved{$destination}++;
  115:                     if (!&copy($source,$destination)) {
  116:                         if (!$file_problem) {
  117:                             print &mt("Unable to create: <br />");
  118:                             $file_problem = 1;
  119:                         }
  120:                         print ("$stuname/part$partid/resp$respid/$file_name_only <br />");
  121:                     }
  122:                 }
  123:             }
  124:         }
  125:     }
  126:     
  127:     &mkpath($doc_zip_root."/zipout/$unique_user",0,0700);
  128:     my $statement = "cd $doc_zip_root/zipdir/$unique_user/$unique_path\n";
  129:     $statement .= "zip -r $doc_zip_root/zipout/$unique_user/$zipout * > /dev/null";
  130:     system($statement);
  131:     $statement = "rm -rf $doc_zip_root/zipdir/$unique_user/$unique_path";
  132:     system($statement);
  133:     print('<a href="/zipspool/zipout/'.$unique_user.'/'.$zipout.'">'.
  134:             &mt("Click to download").'</a><br />');
  135:     close MANIFEST;
  136: } else {
  137:     print(&mt('You are not authorized to download student submissions.'));
  138: }
  139: 1;
  140: __END__;

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>