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

1.1       banghart    1: #!/usr/bin/perl
                      2: # CGI-script to allow download of all essay submissions of 
                      3: # multiple students.
                      4: #
1.43    ! raeburn     5: # $Id: multidownload.pl,v 1.42 2018/05/02 16:59:44 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'});
1.43    ! raeburn   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.'));
1.40      raeburn   151:     } else {
1.43    ! raeburn   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");
1.40      raeburn   165:         }
1.43    ! raeburn   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:                             }
1.38      raeburn   193:                         }
1.43    ! raeburn   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:                             }
1.38      raeburn   214:                             $files_saved{$destination}++;
1.43    ! raeburn   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 />');
1.38      raeburn   221:                             }
1.19      banghart  222:                         }
1.14      banghart  223:                     }
                    224:                 }
1.2       banghart  225:             }
1.43    ! raeburn   226:             if ((!$submission_count) && ($manifest)) {
        !           227:                 print MANIFEST ("\t".&mt("No Files Submitted")."\n");
        !           228:             }
        !           229:         }
        !           230:         if ($manifest) {
        !           231:             close(MANIFEST);
1.1       banghart  232:         }
1.43    ! raeburn   233:         my $madezip;
        !           234:         unless (-d "$doc_zip_root/zipout/$unique_user") {
        !           235:             &File::Path::mkpath($doc_zip_root."/zipout/$unique_user",0,0700);
1.33      banghart  236:         }
1.43    ! raeburn   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 />');
1.41      raeburn   256:         } else {
1.43    ! raeburn   257:             print('<p class="LC_error">'.
        !           258:                   &mt('Failed to create zip archive of student submissions').
        !           259:                   '</p>');
        !           260:         }
1.40      raeburn   261:     }
1.4       banghart  262: } else {
1.41      raeburn   263:     print('<p class="LC_error">'.
                    264:           &mt('You are not authorized to download student submissions.').
                    265:           '</p>');
1.1       banghart  266: }
1.40      raeburn   267: unless ($nocookie) {
                    268:     print(&Apache::loncommon::end_page());
                    269: }
1.6       banghart  270: 1;
                    271: __END__;

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