File:  [LON-CAPA] / loncom / interface / multidownload.pl
Revision 1.41: download - view: text, annotated - select for diffs
Sun Nov 5 18:19:41 2017 UTC (6 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Replace use of system() with use of File::Path::remove_tree() and
  Archive::Zip. LONCAPA-prerequisites will need perl-Archive-Zip added.

    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.41 2017/11/05 18:19:41 raeburn Exp $
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: use lib '/home/httpd/lib/perl';
   30: use LONCAPA::loncgi;
   31: use File::Path;
   32: use File::Basename;
   33: use File::Copy;
   34: use Archive::Zip qw( :ERROR_CODES );
   35: use Apache::lonhtmlcommon();
   36: use Apache::lonnavmaps();
   37: use Apache::loncommon();
   38: use Apache::lonlocal;
   39: use Apache::lonmsg();
   40: use Apache::lonnet;
   41: use LONCAPA::Enrollment;
   42: use LONCAPA;
   43: use strict;
   44: 
   45: sub is_flat {
   46:     my ($partlist, $res) = @_;
   47:     my $flat_part = 1;
   48:     my $flat_resp = 1;
   49:     if (scalar(@$partlist) > 1) {
   50:         $flat_part = 0;
   51:     }
   52:     foreach my $partid (@$partlist) {
   53:         my @ids = $res->responseIds($partid);
   54:         if (scalar(@ids) > 1 ) {
   55:             $flat_resp = 0;
   56:         }
   57:     }
   58:     return ($flat_part, $flat_resp);
   59: }
   60: 
   61: 
   62: sub get_part_resp_path {
   63:     my ($flat_part, $flat_resp, $part_id, $resp_id) = @_;
   64:     my $part_resp_path = "";
   65:     if (!$flat_part) {
   66:         $part_resp_path = "part$part_id/";
   67:     } 
   68:     if (!$flat_resp) {
   69:         $part_resp_path .= "resp$resp_id/";
   70:     }
   71:     $part_resp_path =~ s/\/^//; 
   72:     return('/'.$part_resp_path);
   73: }
   74: 
   75: 
   76: $|=1;
   77: &Apache::lonlocal::get_language_handle();
   78: &Apache::loncommon::content_type(undef,'text/html');
   79: my ($nocookie,$identifier,$unique_path,$scope,$unique_user);
   80: if (! &LONCAPA::loncgi::check_cookie_and_load_env()) {
   81:     print(&LONCAPA::loncgi::missing_cookie_msg());
   82:     $nocookie = 1;
   83: }
   84: 
   85: unless ($nocookie) {
   86:     $scope = $env{'request.course.id'};
   87:     if ($env{'request.course.sec'}) {
   88:         $scope .= '/'.$env{'request.course.sec'};
   89:     }
   90:     if ($ENV{'QUERY_STRING'} =~ /^\d+_\d+_\d+$/) {
   91:         $identifier = $ENV{'QUERY_STRING'};
   92:         $unique_path = $identifier.time();
   93:     }
   94:     if (($env{'user.name'} =~ /^$LONCAPA::match_username$/) &&
   95:         ($env{'user.domain'} =~ /^$LONCAPA::match_domain$/)) {
   96:         $unique_user = $env{'user.name'}.':'.$env{'user.domain'};
   97:     }
   98:     print(&Apache::loncommon::start_page('Multiple Downloads'));
   99: }
  100: if ($scope eq '') {
  101:     print(&mt('Invalid course context: you need to reselect your course role.'));
  102: } elsif ($identifier eq '') {
  103:     unless ($nocookie) {
  104:         if (&Apache::lonnet::allowed('vgr',$scope) eq 'F') {
  105:             print(&mt('Invalid query string; unable to download submissions.'));
  106:         } else {
  107:             print(&mt('You are not authorized to download student submissions.'));
  108:         }
  109:     }
  110: } elsif ($unique_user eq '') {
  111:     unless ($nocookie) {
  112:         if (&Apache::lonnet::allowed('vgr',$scope) eq 'F') {
  113:             print(&mt('Characters in your username and/or domain prevent download of submissions.'));
  114:         } else {
  115:             print(&mt('You are not authorized to download student submissions.'));
  116:         }
  117:     }
  118: } elsif (&Apache::lonnet::allowed('vgr',$scope) eq 'F') {
  119:     my $symb = $env{'cgi.'.$identifier.'.symb'};
  120:     my $navmap = Apache::lonnavmaps::navmap->new();
  121:     my $res = $navmap->getBySymb($symb);
  122:     my $partlist = $res->parts();
  123:     my ($flat_part, $flat_resp) = &is_flat($partlist, $res);
  124:     my ($zipout) = ($symb =~ /^.*\/(.+)\.problem$/);
  125:     $zipout =~ s/\s/_/g;
  126:     $zipout =~ s/[^\w.\-]+//g;
  127:     $zipout .= "$identifier.zip";
  128:     my $courseid = $env{'request.course.id'};
  129:     my @stuchecked = split(/\n/,$env{'cgi.'.$identifier.'.students'});
  130:     my $number_of_students = scalar(@stuchecked);
  131:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('',$number_of_students);
  132:     my @parts = split(/\n/,$env{'cgi.'.$identifier.'.parts'});
  133:     my $doc_zip_root = $Apache::lonnet::perlvar{'lonZipDir'};
  134:     my $manifest;
  135:     unless (-d "$doc_zip_root/zipdir/$unique_user/$unique_path") {
  136:         &File::Path::mkpath($doc_zip_root."/zipdir/$unique_user/$unique_path",0,0700);
  137:     }
  138:     if (open(MANIFEST, ">$doc_zip_root/zipdir/$unique_user/$unique_path/manifest.txt")) {
  139:         $manifest = 1;
  140:         print MANIFEST (&mt("Zip file generated on [_1]",&Apache::lonlocal::locallocaltime(time()))."\n");
  141:         print MANIFEST (&mt("Course: [_1]",$env{"course.$courseid.description"})."\n");
  142:         print MANIFEST (&mt("Problem: [_1]",$res->compTitle)."\n");
  143:         print MANIFEST (&mt("Files contained in this zip:")."\n");
  144:     } else {
  145:         &Apache::lonnet::logthis("Problem making manifest");
  146:     }
  147:     my $file_problem = 0;
  148:     my $current_student = 0;
  149:     foreach my $stu (@stuchecked) {
  150:         $current_student ++;
  151:         &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,&mt("Processing student [_1] of [_2]",$current_student,$number_of_students));
  152:         my %files_saved;
  153:         my ($stuname,$studom,$fullname) = split(/:/,$stu);
  154:         my %record = &Apache::lonnet::restore($symb,$courseid,$studom,$stuname);
  155:         my $port_url = '/uploaded/'.$studom.'/'.$stuname.'/portfolio';
  156:         if ($manifest) {
  157:             print MANIFEST ($fullname."\n");
  158:         }
  159:          
  160:         my $submission_count = 0;
  161:         foreach my $partid (@$partlist) {
  162:             my @ids = $res->responseIds($partid);
  163:             foreach my $respid (@ids) {
  164:                 my $part_resp_path = &get_part_resp_path($flat_part,$flat_resp, $partid, $respid);
  165:                 &File::Path::mkpath($doc_zip_root."/zipdir/$unique_user/$unique_path/$stuname/$part_resp_path",0,0700);
  166:                 foreach my $origin ('portfiles','uploadedurl') {
  167:                     my @files;
  168:                     if ($record{"resource.$partid.$respid.$origin"} ne '') {
  169:                         if ($origin eq 'portfiles') {
  170:                             @files = (split(',',$record{"resource.$partid.$respid.$origin"}));
  171:                         } else {
  172:                             @files = ($record{"resource.$partid.$respid.$origin"});
  173:                         }
  174:                     }
  175:                     foreach my $file (@files) {
  176:                         if ($origin eq 'portfiles') { 
  177:                             $file = $port_url.$file;
  178:                         }
  179:                         my ($file_name_only) = ($file =~ m{.*/([^/]+)$});
  180:                         if ($manifest) {
  181:                             print MANIFEST ("\t$file_name_only (".&mt("Part [_1]",$partid).
  182:                                             ") (".&mt("Response [_1]",$respid).")"."\n");
  183:                         }
  184:                         $submission_count ++;
  185:                         &Apache::lonnet::repcopy($file);
  186:                         my $source = &Apache::lonnet::filelocation("",$file);
  187:                         my $destination = "$doc_zip_root/zipdir/$unique_user/$unique_path/$stuname$part_resp_path/$file_name_only";
  188:                         if (exists($files_saved{$destination})) {
  189:                             # file has already been saved once
  190:                             my ($file_name,$file_ext) = 
  191:                                 ($destination =~ /(^.*)(\..+$)/);
  192:                             $destination = $file_name.$files_saved{$destination}.$file_ext;
  193:                             $files_saved{$destination}++;
  194:                         }
  195:                         $files_saved{$destination}++;
  196:                         if (!&copy($source,$destination)) {
  197:                             if (!$file_problem) {
  198:                                 print('<br /><span class="LC_error">'.&mt("Unable to create: ")."</span><br />");
  199:                                 $file_problem = 1;
  200:                             }
  201:                             print('<span class="LC_filename">'."$stuname/part$partid/resp$respid/$file_name_only".'</span><br />');
  202:                         }
  203:                     }
  204:                 }
  205:             }
  206:         }
  207:         if ((!$submission_count) && ($manifest)) {
  208:             print MANIFEST ("\t".&mt("No Files Submitted")."\n");
  209:         }
  210:     }
  211:     if ($manifest) {
  212:         close(MANIFEST);
  213:     }
  214:     my $madezip;
  215:     unless (-d "$doc_zip_root/zipout/$unique_user") {
  216:         &File::Path::mkpath($doc_zip_root."/zipout/$unique_user",0,0700);
  217:     }
  218:     if ((-d "$doc_zip_root/zipout/$unique_user") &&
  219:         (-d "$doc_zip_root/zipdir/$unique_user/$unique_path")) {
  220:         if (!-e "$doc_zip_root/zipout/$unique_user/$zipout") {
  221:              my $zip = Archive::Zip->new();
  222:              $zip->addTree("$doc_zip_root/zipdir/$unique_user/$unique_path");
  223:              if ($zip->writeToFileNamed("$doc_zip_root/zipout/$unique_user/$zipout") == AZ_OK) {
  224:                  $madezip = 1;
  225:              }
  226:         } else {
  227:             $madezip = 1;
  228:             # should happen only if user reloads page
  229:             &Apache::lonnet::logthis("$zipout is already there");
  230:         }
  231:         &File::Path::remove_tree("$doc_zip_root/zipdir/$unique_user/$unique_path",{ safe => 1, });
  232:     }
  233:     &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state);
  234:     if ($madezip) {
  235:         print('<p><a href="/zipspool/zipout/'.$unique_user.'/'.$zipout.'">'.
  236:              &mt("Click to download").'</a></p><br />');
  237:     } else {
  238:         print('<p class="LC_error">'.
  239:               &mt('Failed to create zip archive of student submissions').
  240:               '</p>');
  241:     }
  242: } else {
  243:     print('<p class="LC_error">'.
  244:           &mt('You are not authorized to download student submissions.').
  245:           '</p>');
  246: }
  247: unless ($nocookie) {
  248:     print(&Apache::loncommon::end_page());
  249: }
  250: 1;
  251: __END__;

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