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

1.1       banghart    1: #!/usr/bin/perl
                      2: # CGI-script to allow download of all essay submissions of 
                      3: # multiple students.
                      4: #
1.40    ! raeburn     5: # $Id: multidownload.pl,v 1.39 2011/12/21 21:25:40 www 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;
                     34: use Apache::lonhtmlcommon();
1.36      albertel   35: use Apache::lonnavmaps();
1.1       banghart   36: use Apache::loncommon();
                     37: use Apache::lonlocal;
                     38: use Apache::lonmsg();
1.2       banghart   39: use Apache::lonnet;
1.1       banghart   40: use LONCAPA::Enrollment;
                     41: use strict;
                     42: 
1.25      banghart   43: sub is_flat {
                     44:     my ($partlist, $res) = @_;
                     45:     my $flat_part = 1;
                     46:     my $flat_resp = 1;
                     47:     if (scalar(@$partlist) > 1) {
                     48:         $flat_part = 0;
                     49:     }
                     50:     foreach my $partid (@$partlist) {
                     51:         my @ids = $res->responseIds($partid);
                     52:         if (scalar(@ids) > 1 ) {
                     53:             $flat_resp = 0;
                     54:         }
                     55:     }
                     56:     return ($flat_part, $flat_resp);
                     57: }
1.36      albertel   58: 
                     59: 
1.25      banghart   60: sub get_part_resp_path {
                     61:     my ($flat_part, $flat_resp, $part_id, $resp_id) = @_;
                     62:     my $part_resp_path = "";
                     63:     if (!$flat_part) {
                     64:         $part_resp_path = "part$part_id/";
                     65:     } 
                     66:     if (!$flat_resp) {
                     67:         $part_resp_path .= "resp$resp_id/";
                     68:     }
                     69:     $part_resp_path =~ s/\/^//; 
                     70:     return('/'.$part_resp_path);
                     71: }
1.36      albertel   72: 
                     73: 
1.1       banghart   74: $|=1;
1.38      raeburn    75: &Apache::lonlocal::get_language_handle();
                     76: &Apache::loncommon::content_type(undef,'text/html');
1.40    ! raeburn    77: my ($nocookie,$identifier,$unique_path,$scope);
1.1       banghart   78: if (! &LONCAPA::loncgi::check_cookie_and_load_env()) {
1.38      raeburn    79:     print(&LONCAPA::loncgi::missing_cookie_msg());
1.40    ! raeburn    80:     $nocookie = 1;
1.1       banghart   81: }
1.36      albertel   82: 
1.40    ! raeburn    83: unless ($nocookie) {
        !            84:     $scope = $env{'request.course.id'};
        !            85:     if ($env{'request.course.sec'}) {
        !            86:         $scope .= '/'.$env{'request.course.sec'};
        !            87:     }
        !            88:     if ($ENV{'QUERY_STRING'} =~ /^\d+_\d+_\d+$/) {
        !            89:         $identifier = $ENV{'QUERY_STRING'};
        !            90:         $unique_path = $identifier.time();
        !            91:     }
        !            92:     print(&Apache::loncommon::start_page('Multiple Downloads'));
1.6       banghart   93: }
1.40    ! raeburn    94: if ($scope eq '') {
        !            95:     print(&mt('Invalid course context: you need to reselect your course role.'));
        !            96: } elsif ($identifier eq '') {
        !            97:     unless ($nocookie) {
        !            98:         if (&Apache::lonnet::allowed('vgr',$scope) eq 'F') {
        !            99:             print(&mt('Invalid query string; unable to download submissions.'));
        !           100:         } else {
        !           101:             print(&mt('You are not authorized to download student submissions.'));
        !           102:         }
        !           103:     }
        !           104: } elsif (&Apache::lonnet::allowed('vgr',$scope) eq 'F') {
1.4       banghart  105:     my $symb = $env{'cgi.'.$identifier.'.symb'};
1.16      banghart  106:     my $navmap = Apache::lonnavmaps::navmap->new();
                    107:     my $res = $navmap->getBySymb($symb);
1.19      banghart  108:     my $partlist = $res->parts();
1.25      banghart  109:     my ($flat_part, $flat_resp) = &is_flat($partlist, $res);
1.16      banghart  110:     my ($zipout) = ($symb =~ /^.*\/(.+)\.problem$/);
1.10      banghart  111:     $zipout =~ s/\s/_/g;
1.23      banghart  112:     $zipout .= "$identifier.zip";
1.4       banghart  113:     my $courseid = $env{'request.course.id'};
                    114:     my @stuchecked = split(/\n/,$env{'cgi.'.$identifier.'.students'});
1.26      banghart  115:     my $number_of_students = scalar(@stuchecked);
1.39      www       116:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('',$number_of_students);
1.4       banghart  117:     my @parts = split(/\n/,$env{'cgi.'.$identifier.'.parts'});
                    118:     my $doc_zip_root = $Apache::lonnet::perlvar{'lonZipDir'};
1.5       banghart  119:     my $uname = $env{'user.name'};
                    120:     my $udom = $env{'user.domain'};
1.16      banghart  121:     my $unique_user = $uname.":".$udom;
1.40    ! raeburn   122:     my $manifest;
1.16      banghart  123:     &mkpath($doc_zip_root."/zipdir/$unique_user/$unique_path",0,0700);
1.40    ! raeburn   124:     if (open(MANIFEST, ">$doc_zip_root/zipdir/$unique_user/$unique_path/manifest.txt")) {
        !           125:         $manifest = 1;
        !           126:         print MANIFEST (&mt("Zip file generated on [_1]",&Apache::lonlocal::locallocaltime(time()))."\n");
        !           127:         print MANIFEST (&mt("Course: [_1]",$env{"course.$courseid.description"})."\n");
        !           128:         print MANIFEST (&mt("Problem: [_1]",$res->compTitle)."\n");
        !           129:         print MANIFEST (&mt("Files contained in this zip:")."\n");
        !           130:     } else {
1.22      banghart  131:         &Apache::lonnet::logthis("Problem making manifest");
                    132:     }
1.15      banghart  133:     my $file_problem = 0;
1.26      banghart  134:     my $current_student = 0;
1.4       banghart  135:     foreach my $stu (@stuchecked) {
1.26      banghart  136:         $current_student ++;
1.39      www       137:         &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,&mt("Processing student [_1] of [_2]",$current_student,$number_of_students));
1.15      banghart  138:         my %files_saved;
1.5       banghart  139:         my ($stuname,$studom,$fullname) = split(/:/,$stu);
                    140:         my %record = &Apache::lonnet::restore($symb,$courseid,$studom,$stuname);
1.38      raeburn   141:         my $port_url = '/uploaded/'.$studom.'/'.$stuname.'/portfolio';
1.40    ! raeburn   142:         if ($manifest) {
        !           143:             print MANIFEST ($fullname."\n");
        !           144:         }
1.36      albertel  145:          
1.33      banghart  146:         my $submission_count = 0;
1.19      banghart  147:         foreach my $partid (@$partlist) {
                    148:             my @ids = $res->responseIds($partid);
1.36      albertel  149:             foreach my $respid (@ids) {
1.25      banghart  150:                 my $part_resp_path = &get_part_resp_path($flat_part,$flat_resp, $partid, $respid);
                    151:                 &mkpath($doc_zip_root."/zipdir/$unique_user/$unique_path/$stuname/$part_resp_path",0,0700);
1.38      raeburn   152:                 foreach my $origin ('portfiles','uploadedurl') {
                    153:                     my @files;
                    154:                     if ($record{"resource.$partid.$respid.$origin"} ne '') {
                    155:                         if ($origin eq 'portfiles') {
                    156:                             @files = (split(',',$record{"resource.$partid.$respid.$origin"}));
                    157:                         } else {
                    158:                             @files = ($record{"resource.$partid.$respid.$origin"});
                    159:                         }
                    160:                     }
                    161:                     foreach my $file (@files) {
                    162:                         if ($origin eq 'portfiles') { 
                    163:                             $file = $port_url.$file;
                    164:                         }
                    165:                         my ($file_name_only) = ($file =~ m{.*/([^/]+)$});
1.40    ! raeburn   166:                         if ($manifest) {
        !           167:                             print MANIFEST ("\t$file_name_only (".&mt("Part [_1]",$partid).
        !           168:                                             ") (".&mt("Response [_1]",$respid).")"."\n");
        !           169:                         }
1.38      raeburn   170:                         $submission_count ++;
                    171:                         &Apache::lonnet::repcopy($file);
                    172:                         my $source = &Apache::lonnet::filelocation("",$file);
                    173:                         my $destination = "$doc_zip_root/zipdir/$unique_user/$unique_path/$stuname$part_resp_path/$file_name_only";
                    174:                         if (exists($files_saved{$destination})) {
                    175:                             # file has already been saved once
                    176:                             my ($file_name,$file_ext) = 
                    177:                                 ($destination =~ /(^.*)(\..+$)/);
                    178:                             $destination = $file_name.$files_saved{$destination}.$file_ext;
                    179:                             $files_saved{$destination}++;
                    180:                         }
1.36      albertel  181:                         $files_saved{$destination}++;
1.38      raeburn   182:                         if (!&copy($source,$destination)) {
                    183:                             if (!$file_problem) {
                    184:                                 print('<br /><span class="LC_error">'.&mt("Unable to create: ")."</span><br />");
                    185:                                 $file_problem = 1;
                    186:                             }
                    187:                             print('<span class="LC_filename">'."$stuname/part$partid/resp$respid/$file_name_only".'</span><br />');
1.19      banghart  188:                         }
1.14      banghart  189:                     }
                    190:                 }
1.2       banghart  191:             }
1.1       banghart  192:         }
1.40    ! raeburn   193:         if ((!$submission_count) && ($manifest)) {
1.34      banghart  194:             print MANIFEST ("\t".&mt("No Files Submitted")."\n");
1.33      banghart  195:         }
1.1       banghart  196:     }
1.22      banghart  197:     
1.16      banghart  198:     &mkpath($doc_zip_root."/zipout/$unique_user",0,0700);
1.23      banghart  199:     my $statement;
                    200:     if (! -e "$doc_zip_root/zipout/$unique_user/$zipout") {
                    201:         $statement = "cd $doc_zip_root/zipdir/$unique_user/$unique_path\n";
                    202:         $statement .= "zip -r $doc_zip_root/zipout/$unique_user/$zipout * > /dev/null";
                    203:         system($statement);
                    204:     } else {
                    205:         # should happen only if user reloads page 
                    206:         &Apache::lonnet::logthis("$zipout is already there");
                    207:     }
1.16      banghart  208:     $statement = "rm -rf $doc_zip_root/zipdir/$unique_user/$unique_path";
1.4       banghart  209:     system($statement);
1.26      banghart  210:     &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state);
                    211:     print('<p><a href="/zipspool/zipout/'.$unique_user.'/'.$zipout.'">'.
                    212:             &mt("Click to download").'</a></p><br />');
1.40    ! raeburn   213:     if ($manifest) {
        !           214:         close(MANIFEST);
        !           215:     }
1.4       banghart  216: } else {
1.13      banghart  217:     print(&mt('You are not authorized to download student submissions.'));
1.1       banghart  218: }
1.40    ! raeburn   219: unless ($nocookie) {
        !           220:     print(&Apache::loncommon::end_page());
        !           221: }
1.6       banghart  222: 1;
                    223: __END__;

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