Annotation of loncom/interface/multidownload.pl, revision 1.42

1.1       banghart    1: #!/usr/bin/perl
                      2: # CGI-script to allow download of all essay submissions of 
                      3: # multiple students.
                      4: #
1.42    ! raeburn     5: # $Id: multidownload.pl,v 1.41 2017/11/05 18:19:41 raeburn Exp $
1.1       banghart    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;
1.41      raeburn    34: use Archive::Zip qw( :ERROR_CODES );
1.1       banghart   35: use Apache::lonhtmlcommon();
1.36      albertel   36: use Apache::lonnavmaps();
1.1       banghart   37: use Apache::loncommon();
                     38: use Apache::lonlocal;
                     39: use Apache::lonmsg();
1.2       banghart   40: use Apache::lonnet;
1.1       banghart   41: use LONCAPA::Enrollment;
1.41      raeburn    42: use LONCAPA;
1.1       banghart   43: use strict;
                     44: 
1.25      banghart   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: }
1.36      albertel   60: 
                     61: 
1.25      banghart   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: }
1.36      albertel   74: 
                     75: 
1.1       banghart   76: $|=1;
1.38      raeburn    77: &Apache::lonlocal::get_language_handle();
                     78: &Apache::loncommon::content_type(undef,'text/html');
1.41      raeburn    79: my ($nocookie,$identifier,$unique_path,$scope,$unique_user);
1.1       banghart   80: if (! &LONCAPA::loncgi::check_cookie_and_load_env()) {
1.38      raeburn    81:     print(&LONCAPA::loncgi::missing_cookie_msg());
1.40      raeburn    82:     $nocookie = 1;
1.1       banghart   83: }
1.36      albertel   84: 
1.40      raeburn    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:     }
1.41      raeburn    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:     }
1.40      raeburn    98:     print(&Apache::loncommon::start_page('Multiple Downloads'));
1.6       banghart   99: }
1.40      raeburn   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:     }
1.41      raeburn   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:     }
1.40      raeburn   118: } elsif (&Apache::lonnet::allowed('vgr',$scope) eq 'F') {
1.4       banghart  119:     my $symb = $env{'cgi.'.$identifier.'.symb'};
1.16      banghart  120:     my $navmap = Apache::lonnavmaps::navmap->new();
                    121:     my $res = $navmap->getBySymb($symb);
1.19      banghart  122:     my $partlist = $res->parts();
1.25      banghart  123:     my ($flat_part, $flat_resp) = &is_flat($partlist, $res);
1.16      banghart  124:     my ($zipout) = ($symb =~ /^.*\/(.+)\.problem$/);
1.10      banghart  125:     $zipout =~ s/\s/_/g;
1.41      raeburn   126:     $zipout =~ s/[^\w.\-]+//g;
1.23      banghart  127:     $zipout .= "$identifier.zip";
1.4       banghart  128:     my $courseid = $env{'request.course.id'};
                    129:     my @stuchecked = split(/\n/,$env{'cgi.'.$identifier.'.students'});
1.26      banghart  130:     my $number_of_students = scalar(@stuchecked);
1.39      www       131:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('',$number_of_students);
1.4       banghart  132:     my @parts = split(/\n/,$env{'cgi.'.$identifier.'.parts'});
                    133:     my $doc_zip_root = $Apache::lonnet::perlvar{'lonZipDir'};
1.40      raeburn   134:     my $manifest;
1.41      raeburn   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:     }
1.42    ! raeburn   138:     if (open(MANIFEST,'>',"$doc_zip_root/zipdir/$unique_user/$unique_path/manifest.txt")) {
1.40      raeburn   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 {
1.22      banghart  145:         &Apache::lonnet::logthis("Problem making manifest");
                    146:     }
1.15      banghart  147:     my $file_problem = 0;
1.26      banghart  148:     my $current_student = 0;
1.4       banghart  149:     foreach my $stu (@stuchecked) {
1.26      banghart  150:         $current_student ++;
1.39      www       151:         &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,&mt("Processing student [_1] of [_2]",$current_student,$number_of_students));
1.15      banghart  152:         my %files_saved;
1.5       banghart  153:         my ($stuname,$studom,$fullname) = split(/:/,$stu);
                    154:         my %record = &Apache::lonnet::restore($symb,$courseid,$studom,$stuname);
1.38      raeburn   155:         my $port_url = '/uploaded/'.$studom.'/'.$stuname.'/portfolio';
1.40      raeburn   156:         if ($manifest) {
                    157:             print MANIFEST ($fullname."\n");
                    158:         }
1.36      albertel  159:          
1.33      banghart  160:         my $submission_count = 0;
1.19      banghart  161:         foreach my $partid (@$partlist) {
                    162:             my @ids = $res->responseIds($partid);
1.36      albertel  163:             foreach my $respid (@ids) {
1.25      banghart  164:                 my $part_resp_path = &get_part_resp_path($flat_part,$flat_resp, $partid, $respid);
1.41      raeburn   165:                 &File::Path::mkpath($doc_zip_root."/zipdir/$unique_user/$unique_path/$stuname/$part_resp_path",0,0700);
1.38      raeburn   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{.*/([^/]+)$});
1.40      raeburn   180:                         if ($manifest) {
                    181:                             print MANIFEST ("\t$file_name_only (".&mt("Part [_1]",$partid).
                    182:                                             ") (".&mt("Response [_1]",$respid).")"."\n");
                    183:                         }
1.38      raeburn   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:                         }
1.36      albertel  195:                         $files_saved{$destination}++;
1.38      raeburn   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 />');
1.19      banghart  202:                         }
1.14      banghart  203:                     }
                    204:                 }
1.2       banghart  205:             }
1.1       banghart  206:         }
1.40      raeburn   207:         if ((!$submission_count) && ($manifest)) {
1.34      banghart  208:             print MANIFEST ("\t".&mt("No Files Submitted")."\n");
1.33      banghart  209:         }
1.1       banghart  210:     }
1.41      raeburn   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, });
1.23      banghart  232:     }
1.26      banghart  233:     &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state);
1.41      raeburn   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>');
1.40      raeburn   241:     }
1.4       banghart  242: } else {
1.41      raeburn   243:     print('<p class="LC_error">'.
                    244:           &mt('You are not authorized to download student submissions.').
                    245:           '</p>');
1.1       banghart  246: }
1.40      raeburn   247: unless ($nocookie) {
                    248:     print(&Apache::loncommon::end_page());
                    249: }
1.6       banghart  250: 1;
                    251: __END__;

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