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

1.1       banghart    1: #!/usr/bin/perl
                      2: # CGI-script to allow download of all essay submissions of 
                      3: # multiple students.
                      4: #
1.26    ! banghart    5: # $Id: multidownload.pl,v 1.25 2007/05/05 18:51:49 banghart Exp $
1.1       banghart    6: #
                      7: # Copyright Michigan State University Board of Trustees
                      8: #
                      9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                     10: #
                     11: # LON-CAPA is free software; you can redistribute it and/or modify
                     12: # it under the terms of the GNU General Public License as published by
                     13: # the Free Software Foundation; either version 2 of the License, or
                     14: # (at your option) any later version.
                     15: #
                     16: # LON-CAPA is distributed in the hope that it will be useful,
                     17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     19: # GNU General Public License for more details.
                     20: #
                     21: # You should have received a copy of the GNU General Public License
                     22: # along with LON-CAPA; if not, write to the Free Software
                     23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     24: #
                     25: # /home/httpd/html/adm/gpl.txt
                     26: #
                     27: # http://www.lon-capa.org/
                     28: #
                     29: 
                     30: use lib '/home/httpd/lib/perl';
                     31: use LONCAPA::loncgi;
                     32: use File::Path;
                     33: use File::Basename;
                     34: use File::Copy;
                     35: use IO::File;
                     36: use Apache::lonhtmlcommon();
1.16      banghart   37: use Apache::lonnavmaps;
1.1       banghart   38: use Apache::lonnet;
                     39: use Apache::loncommon();
                     40: use Apache::lonlocal;
                     41: use Apache::lonmsg();
1.2       banghart   42: use Apache::lonnet;
1.1       banghart   43: use LONCAPA::Enrollment;
                     44: use strict;
                     45: 
1.25      banghart   46: sub is_flat {
                     47:     my ($partlist, $res) = @_;
                     48:     my $flat_part = 1;
                     49:     my $flat_resp = 1;
                     50:     if (scalar(@$partlist) > 1) {
                     51:         $flat_part = 0;
                     52:     }
                     53:     foreach my $partid (@$partlist) {
                     54:         my @ids = $res->responseIds($partid);
                     55:         if (scalar(@ids) > 1 ) {
                     56:             $flat_resp = 0;
                     57:         }
                     58:     }
                     59:     return ($flat_part, $flat_resp);
                     60: }
                     61: sub get_part_resp_path {
                     62:     my ($flat_part, $flat_resp, $part_id, $resp_id) = @_;
                     63:     my $part_resp_path = "";
                     64:     if (!$flat_part) {
                     65:         $part_resp_path = "part$part_id/";
                     66:     } 
                     67:     if (!$flat_resp) {
                     68:         $part_resp_path .= "resp$resp_id/";
                     69:     }
                     70:     $part_resp_path =~ s/\/^//; 
                     71:     return('/'.$part_resp_path);
                     72: }
1.1       banghart   73: $|=1;
                     74: if (! &LONCAPA::loncgi::check_cookie_and_load_env()) {
                     75:     print <<END;
                     76: Content-type: text/html
                     77: 
                     78: <html>
                     79: <head><title>Bad Cookie</title></head>
                     80: <body>
                     81: Your cookie information is incorrect.
                     82: </body>
                     83: </html>
                     84: END
                     85:     return;
                     86: }
                     87: &Apache::lonlocal::get_language_handle();
                     88: &Apache::loncommon::content_type(undef,'text/html');
                     89: my $identifier = $ENV{'QUERY_STRING'};
1.8       banghart   90: my $unique_path = $identifier.time();
1.1       banghart   91: print(&Apache::loncommon::start_page('Multiple Downloads'));
1.26    ! banghart   92:     
1.6       banghart   93: 
                     94: my $scope = $env{'request.course.id'};
                     95: if ($env{'request.course.sec'}) {
                     96:     $scope .= '/'.$env{'request.course.sec'};
                     97: }
                     98: if (&Apache::lonnet::allowed('vgr',$scope) eq 'F') {
1.4       banghart   99:     my $symb = $env{'cgi.'.$identifier.'.symb'};
1.16      banghart  100:     my $navmap = Apache::lonnavmaps::navmap->new();
                    101:     my $res = $navmap->getBySymb($symb);
1.19      banghart  102:     my $partlist = $res->parts();
1.25      banghart  103:     my ($flat_part, $flat_resp) = &is_flat($partlist, $res);
1.16      banghart  104:     my ($zipout) = ($symb =~ /^.*\/(.+)\.problem$/);
1.10      banghart  105:     $zipout =~ s/\s/_/g;
1.23      banghart  106:     $zipout .= "$identifier.zip";
1.4       banghart  107:     my $courseid = $env{'request.course.id'};
                    108:     my @stuchecked = split(/\n/,$env{'cgi.'.$identifier.'.students'});
1.26    ! banghart  109:     my $number_of_students = scalar(@stuchecked);
        !           110:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('','Processing Status',
        !           111:                                                     'Preparing Zip File',$number_of_students,'inline','80');
1.4       banghart  112:     my @parts = split(/\n/,$env{'cgi.'.$identifier.'.parts'});
                    113:     my $doc_zip_root = $Apache::lonnet::perlvar{'lonZipDir'};
1.5       banghart  114:     my $uname = $env{'user.name'};
                    115:     my $udom = $env{'user.domain'};
1.16      banghart  116:     my $unique_user = $uname.":".$udom;
                    117:     &mkpath($doc_zip_root."/zipdir/$unique_user/$unique_path",0,0700);
1.22      banghart  118:     if (!open MANIFEST, ">$doc_zip_root/zipdir/$unique_user/$unique_path/manifest.txt") {
                    119:         &Apache::lonnet::logthis("Problem making manifest");
                    120:     }
1.24      banghart  121:     print MANIFEST &mt("Files contained in this zip:\n");
1.15      banghart  122:     my $file_problem = 0;
1.26    ! banghart  123:     my $current_student = 0;
1.4       banghart  124:     foreach my $stu (@stuchecked) {
1.26    ! banghart  125:         $current_student ++;
        !           126:         &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,"Processing student $current_student of $number_of_students");
1.15      banghart  127:         my %files_saved;
1.5       banghart  128:         my ($stuname,$studom,$fullname) = split(/:/,$stu);
                    129:         my %record = &Apache::lonnet::restore($symb,$courseid,$studom,$stuname);
1.20      banghart  130:         my $file_url = '/uploaded/'.$studom.'/'.$stuname.'/portfolio';
1.22      banghart  131:         print MANIFEST $fullname."\n";
1.25      banghart  132:          
1.19      banghart  133:         foreach my $partid (@$partlist) {
                    134:             my @ids = $res->responseIds($partid);
                    135:             foreach my $respid(@ids) {
1.25      banghart  136:                 my $part_resp_path = &get_part_resp_path($flat_part,$flat_resp, $partid, $respid);
                    137:                 &mkpath($doc_zip_root."/zipdir/$unique_user/$unique_path/$stuname/$part_resp_path",0,0700);
1.20      banghart  138:                 foreach my $file (split(',',$record{"resource.$partid.$respid.portfiles"})) {
                    139:                     $file = $file_url.$file;
1.19      banghart  140:                     my ($file_name_only) = ($file =~ /^.*\/(.+$)/);
1.22      banghart  141:                     print MANIFEST "\t$file_name_only \n";
1.19      banghart  142:                     &Apache::lonnet::repcopy($file);
                    143:                     my $source = &Apache::lonnet::filelocation("",$file);
1.25      banghart  144:                     
                    145:                     my $destination = "$doc_zip_root/zipdir/$unique_user/$unique_path/$stuname$part_resp_path/$file_name_only";
1.19      banghart  146:                     if (exists($files_saved{$destination})) {
                    147:                         # file has already been saved once
                    148:                         my ($file_name,$file_ext) = ($destination =~ /(^.*)(\..+$)/);
                    149:                         $destination = $file_name.$files_saved{$destination}.$file_ext;
                    150:                         $files_saved{$destination} ++;
                    151:                     }
                    152:                     $files_saved{$destination}++;
                    153:                     if (!&copy($source,$destination)) {
                    154:                         if (!$file_problem) {
                    155:                             print &mt("Unable to create: <br />");
                    156:                             $file_problem = 1;
                    157:                         }
                    158:                         print ("$stuname/part$partid/resp$respid/$file_name_only <br />");
1.14      banghart  159:                     }
                    160:                 }
1.2       banghart  161:             }
1.1       banghart  162:         }
                    163:     }
1.22      banghart  164:     
1.16      banghart  165:     &mkpath($doc_zip_root."/zipout/$unique_user",0,0700);
1.23      banghart  166:     my $statement;
                    167:     if (! -e "$doc_zip_root/zipout/$unique_user/$zipout") {
                    168:         $statement = "cd $doc_zip_root/zipdir/$unique_user/$unique_path\n";
                    169:         $statement .= "zip -r $doc_zip_root/zipout/$unique_user/$zipout * > /dev/null";
                    170:         system($statement);
                    171:     } else {
                    172:         # should happen only if user reloads page 
                    173:         &Apache::lonnet::logthis("$zipout is already there");
                    174:     }
1.16      banghart  175:     $statement = "rm -rf $doc_zip_root/zipdir/$unique_user/$unique_path";
1.4       banghart  176:     system($statement);
1.26    ! banghart  177:     &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state);
        !           178:     print('<p><a href="/zipspool/zipout/'.$unique_user.'/'.$zipout.'">'.
        !           179:             &mt("Click to download").'</a></p><br />');
1.22      banghart  180:     close MANIFEST;
1.4       banghart  181: } else {
1.13      banghart  182:     print(&mt('You are not authorized to download student submissions.'));
1.1       banghart  183: }
1.6       banghart  184: 1;
                    185: __END__;

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