File:  [LON-CAPA] / loncom / interface / multidownload.pl
Revision 1.43: download - view: text, annotated - select for diffs
Wed Sep 2 14:52:08 2020 UTC (3 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, version_2_11_X, version_2_11_4_uiuc, version_2_11_4_msu, version_2_11_4, version_2_11_3_uiuc, version_2_11_3_msu, version_2_11_3, HEAD
- If problem has file upload items (in essayresponse) in multiple parts
  can select which parts to include when downloading submitted files

    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.43 2020/09/02 14:52:08 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 @getparts;
  134:     if (ref($partlist) eq 'ARRAY') {
  135:         if (@parts) {
  136:             foreach my $posspart (@{$partlist}) {
  137:                 if (grep(/^\Q$posspart\E$/,@parts)) {
  138:                     unless (grep(/^\Q$posspart\E$/,@getparts)) {
  139:                         push(@getparts,$posspart);
  140:                     }
  141:                 }
  142:             }
  143:         } else {
  144:             @getparts = @{$partlist};
  145:         }
  146:     }
  147:     if (!@getparts) {
  148:         print(&mt('No problem parts specified for retrieval of submissions.'));
  149:     } elsif (!$number_of_students) {
  150:         print(&mt('No students selected for retrieval of submissions.'));
  151:     } else {
  152:         my $doc_zip_root = $Apache::lonnet::perlvar{'lonZipDir'};
  153:         my $manifest;
  154:         unless (-d "$doc_zip_root/zipdir/$unique_user/$unique_path") {
  155:             &File::Path::mkpath($doc_zip_root."/zipdir/$unique_user/$unique_path",0,0700);
  156:         }
  157:         if (open(MANIFEST,'>',"$doc_zip_root/zipdir/$unique_user/$unique_path/manifest.txt")) {
  158:             $manifest = 1;
  159:             print MANIFEST (&mt("Zip file generated on [_1]",&Apache::lonlocal::locallocaltime(time()))."\n");
  160:             print MANIFEST (&mt("Course: [_1]",$env{"course.$courseid.description"})."\n");
  161:             print MANIFEST (&mt("Problem: [_1]",$res->compTitle)."\n");
  162:             print MANIFEST (&mt("Files contained in this zip:")."\n");
  163:         } else {
  164:             &Apache::lonnet::logthis("Problem making manifest");
  165:         }
  166:         my $file_problem = 0;
  167:         my $current_student = 0;
  168:         foreach my $stu (@stuchecked) {
  169:             $current_student ++;
  170:             &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,&mt("Processing student [_1] of [_2]",$current_student,$number_of_students));
  171:             my %files_saved;
  172:             my ($stuname,$studom,$fullname) = split(/:/,$stu);
  173:             my %record = &Apache::lonnet::restore($symb,$courseid,$studom,$stuname);
  174:             my $port_url = '/uploaded/'.$studom.'/'.$stuname.'/portfolio';
  175:             if ($manifest) {
  176:                 print MANIFEST ($fullname."\n");
  177:             }
  178: 
  179:             my $submission_count = 0;
  180:             foreach my $partid (@getparts) {
  181:                 my @ids = $res->responseIds($partid);
  182:                 foreach my $respid (@ids) {
  183:                     my $part_resp_path = &get_part_resp_path($flat_part,$flat_resp, $partid, $respid);
  184:                     &File::Path::mkpath($doc_zip_root."/zipdir/$unique_user/$unique_path/$stuname/$part_resp_path",0,0700);
  185:                     foreach my $origin ('portfiles','uploadedurl') {
  186:                         my @files;
  187:                         if ($record{"resource.$partid.$respid.$origin"} ne '') {
  188:                             if ($origin eq 'portfiles') {
  189:                                 @files = (split(',',$record{"resource.$partid.$respid.$origin"}));
  190:                             } else {
  191:                                 @files = ($record{"resource.$partid.$respid.$origin"});
  192:                             }
  193:                         }
  194:                         foreach my $file (@files) {
  195:                             if ($origin eq 'portfiles') { 
  196:                                 $file = $port_url.$file;
  197:                             }
  198:                             my ($file_name_only) = ($file =~ m{.*/([^/]+)$});
  199:                             if ($manifest) {
  200:                                 print MANIFEST ("\t$file_name_only (".&mt("Part [_1]",$partid).
  201:                                                 ") (".&mt("Response [_1]",$respid).")"."\n");
  202:                             }
  203:                             $submission_count ++;
  204:                             &Apache::lonnet::repcopy($file);
  205:                             my $source = &Apache::lonnet::filelocation("",$file);
  206:                             my $destination = "$doc_zip_root/zipdir/$unique_user/$unique_path/$stuname$part_resp_path/$file_name_only";
  207:                             if (exists($files_saved{$destination})) {
  208:                                 # file has already been saved once
  209:                                 my ($file_name,$file_ext) = 
  210:                                     ($destination =~ /(^.*)(\..+$)/);
  211:                                 $destination = $file_name.$files_saved{$destination}.$file_ext;
  212:                                 $files_saved{$destination}++;
  213:                             }
  214:                             $files_saved{$destination}++;
  215:                             if (!&copy($source,$destination)) {
  216:                                 if (!$file_problem) {
  217:                                     print('<br /><span class="LC_error">'.&mt("Unable to create: ")."</span><br />");
  218:                                     $file_problem = 1;
  219:                                 }
  220:                                 print('<span class="LC_filename">'."$stuname/part$partid/resp$respid/$file_name_only".'</span><br />');
  221:                             }
  222:                         }
  223:                     }
  224:                 }
  225:             }
  226:             if ((!$submission_count) && ($manifest)) {
  227:                 print MANIFEST ("\t".&mt("No Files Submitted")."\n");
  228:             }
  229:         }
  230:         if ($manifest) {
  231:             close(MANIFEST);
  232:         }
  233:         my $madezip;
  234:         unless (-d "$doc_zip_root/zipout/$unique_user") {
  235:             &File::Path::mkpath($doc_zip_root."/zipout/$unique_user",0,0700);
  236:         }
  237:         if ((-d "$doc_zip_root/zipout/$unique_user") &&
  238:             (-d "$doc_zip_root/zipdir/$unique_user/$unique_path")) {
  239:             if (!-e "$doc_zip_root/zipout/$unique_user/$zipout") {
  240:                 my $zip = Archive::Zip->new();
  241:                 $zip->addTree("$doc_zip_root/zipdir/$unique_user/$unique_path");
  242:                 if ($zip->writeToFileNamed("$doc_zip_root/zipout/$unique_user/$zipout") == AZ_OK) {
  243:                     $madezip = 1;
  244:                 }
  245:             } else {
  246:                 $madezip = 1;
  247:                 # should happen only if user reloads page
  248:                 &Apache::lonnet::logthis("$zipout is already there");
  249:             }
  250:             &File::Path::remove_tree("$doc_zip_root/zipdir/$unique_user/$unique_path",{ safe => 1, });
  251:         }
  252:         &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state);
  253:         if ($madezip) {
  254:             print('<p><a href="/zipspool/zipout/'.$unique_user.'/'.$zipout.'">'.
  255:                   &mt("Click to download").'</a></p><br />');
  256:         } else {
  257:             print('<p class="LC_error">'.
  258:                   &mt('Failed to create zip archive of student submissions').
  259:                   '</p>');
  260:         }
  261:     }
  262: } else {
  263:     print('<p class="LC_error">'.
  264:           &mt('You are not authorized to download student submissions.').
  265:           '</p>');
  266: }
  267: unless ($nocookie) {
  268:     print(&Apache::loncommon::end_page());
  269: }
  270: 1;
  271: __END__;

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