Annotation of loncom/interface/multidownload.pl, revision 1.34
1.1 banghart 1: #!/usr/bin/perl
2: # CGI-script to allow download of all essay submissions of
3: # multiple students.
4: #
1.34 ! banghart 5: # $Id: multidownload.pl,v 1.33 2007/05/23 19:31:21 banghart 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 IO::File;
35: use Apache::lonhtmlcommon();
1.16 banghart 36: use Apache::lonnavmaps;
1.1 banghart 37: use Apache::lonnet;
38: use Apache::loncommon();
39: use Apache::lonlocal;
40: use Apache::lonmsg();
1.2 banghart 41: use Apache::lonnet;
1.1 banghart 42: use LONCAPA::Enrollment;
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: }
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.1 banghart 72: $|=1;
73: if (! &LONCAPA::loncgi::check_cookie_and_load_env()) {
74: print <<END;
75: Content-type: text/html
76:
77: <html>
78: <head><title>Bad Cookie</title></head>
79: <body>
80: Your cookie information is incorrect.
81: </body>
82: </html>
83: END
84: return;
85: }
86: &Apache::lonlocal::get_language_handle();
87: &Apache::loncommon::content_type(undef,'text/html');
88: my $identifier = $ENV{'QUERY_STRING'};
1.8 banghart 89: my $unique_path = $identifier.time();
1.1 banghart 90: print(&Apache::loncommon::start_page('Multiple Downloads'));
1.26 banghart 91:
1.6 banghart 92:
93: my $scope = $env{'request.course.id'};
94: if ($env{'request.course.sec'}) {
95: $scope .= '/'.$env{'request.course.sec'};
96: }
97: if (&Apache::lonnet::allowed('vgr',$scope) eq 'F') {
1.4 banghart 98: my $symb = $env{'cgi.'.$identifier.'.symb'};
1.16 banghart 99: my $navmap = Apache::lonnavmaps::navmap->new();
100: my $res = $navmap->getBySymb($symb);
1.19 banghart 101: my $partlist = $res->parts();
1.25 banghart 102: my ($flat_part, $flat_resp) = &is_flat($partlist, $res);
1.16 banghart 103: my ($zipout) = ($symb =~ /^.*\/(.+)\.problem$/);
1.10 banghart 104: $zipout =~ s/\s/_/g;
1.23 banghart 105: $zipout .= "$identifier.zip";
1.4 banghart 106: my $courseid = $env{'request.course.id'};
107: my @stuchecked = split(/\n/,$env{'cgi.'.$identifier.'.students'});
1.26 banghart 108: my $number_of_students = scalar(@stuchecked);
109: my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin('','Processing Status',
110: 'Preparing Zip File',$number_of_students,'inline','80');
1.4 banghart 111: my @parts = split(/\n/,$env{'cgi.'.$identifier.'.parts'});
112: my $doc_zip_root = $Apache::lonnet::perlvar{'lonZipDir'};
1.5 banghart 113: my $uname = $env{'user.name'};
114: my $udom = $env{'user.domain'};
1.16 banghart 115: my $unique_user = $uname.":".$udom;
116: &mkpath($doc_zip_root."/zipdir/$unique_user/$unique_path",0,0700);
1.27 banghart 117: if (!open(MANIFEST, ">$doc_zip_root/zipdir/$unique_user/$unique_path/manifest.txt")) {
1.22 banghart 118: &Apache::lonnet::logthis("Problem making manifest");
119: }
1.29 banghart 120: print MANIFEST (&mt("Zip file generated on [_1]",&Apache::lonlocal::locallocaltime(time()))."\n");
1.31 banghart 121: print MANIFEST (&mt("Course: [_1]",$env{"course.$courseid.description"})."\n");
1.32 banghart 122: print MANIFEST (&mt("Problem: [_1]",$res->compTitle)."\n");
1.29 banghart 123: print MANIFEST (&mt("Files contained in this zip:\n"));
1.15 banghart 124: my $file_problem = 0;
1.26 banghart 125: my $current_student = 0;
1.4 banghart 126: foreach my $stu (@stuchecked) {
1.26 banghart 127: $current_student ++;
128: &Apache::lonhtmlcommon::Update_PrgWin('',\%prog_state,"Processing student $current_student of $number_of_students");
1.15 banghart 129: my %files_saved;
1.5 banghart 130: my ($stuname,$studom,$fullname) = split(/:/,$stu);
131: my %record = &Apache::lonnet::restore($symb,$courseid,$studom,$stuname);
1.20 banghart 132: my $file_url = '/uploaded/'.$studom.'/'.$stuname.'/portfolio';
1.29 banghart 133: print MANIFEST ($fullname."\n");
1.33 banghart 134: my $submission_count = 0;
1.19 banghart 135: foreach my $partid (@$partlist) {
136: my @ids = $res->responseIds($partid);
137: foreach my $respid(@ids) {
1.25 banghart 138: my $part_resp_path = &get_part_resp_path($flat_part,$flat_resp, $partid, $respid);
139: &mkpath($doc_zip_root."/zipdir/$unique_user/$unique_path/$stuname/$part_resp_path",0,0700);
1.20 banghart 140: foreach my $file (split(',',$record{"resource.$partid.$respid.portfiles"})) {
141: $file = $file_url.$file;
1.19 banghart 142: my ($file_name_only) = ($file =~ /^.*\/(.+$)/);
1.29 banghart 143: print MANIFEST ("\t$file_name_only \n");
1.33 banghart 144: $submission_count ++;
1.19 banghart 145: &Apache::lonnet::repcopy($file);
146: my $source = &Apache::lonnet::filelocation("",$file);
1.25 banghart 147: my $destination = "$doc_zip_root/zipdir/$unique_user/$unique_path/$stuname$part_resp_path/$file_name_only";
1.19 banghart 148: if (exists($files_saved{$destination})) {
149: # file has already been saved once
150: my ($file_name,$file_ext) = ($destination =~ /(^.*)(\..+$)/);
151: $destination = $file_name.$files_saved{$destination}.$file_ext;
152: $files_saved{$destination} ++;
153: }
154: $files_saved{$destination}++;
155: if (!©($source,$destination)) {
156: if (!$file_problem) {
1.28 banghart 157: print(&mt("Unable to create: <br />"));
1.19 banghart 158: $file_problem = 1;
159: }
1.28 banghart 160: print("$stuname/part$partid/resp$respid/$file_name_only <br />");
1.14 banghart 161: }
162: }
1.2 banghart 163: }
1.1 banghart 164: }
1.33 banghart 165: if (!$submission_count) {
1.34 ! banghart 166: print MANIFEST ("\t".&mt("No Files Submitted")."\n");
1.33 banghart 167: }
1.1 banghart 168: }
1.22 banghart 169:
1.16 banghart 170: &mkpath($doc_zip_root."/zipout/$unique_user",0,0700);
1.23 banghart 171: my $statement;
172: if (! -e "$doc_zip_root/zipout/$unique_user/$zipout") {
173: $statement = "cd $doc_zip_root/zipdir/$unique_user/$unique_path\n";
174: $statement .= "zip -r $doc_zip_root/zipout/$unique_user/$zipout * > /dev/null";
175: system($statement);
176: } else {
177: # should happen only if user reloads page
178: &Apache::lonnet::logthis("$zipout is already there");
179: }
1.16 banghart 180: $statement = "rm -rf $doc_zip_root/zipdir/$unique_user/$unique_path";
1.4 banghart 181: system($statement);
1.26 banghart 182: &Apache::lonhtmlcommon::Close_PrgWin('',\%prog_state);
183: print('<p><a href="/zipspool/zipout/'.$unique_user.'/'.$zipout.'">'.
184: &mt("Click to download").'</a></p><br />');
1.29 banghart 185: close(MANIFEST);
1.4 banghart 186: } else {
1.13 banghart 187: print(&mt('You are not authorized to download student submissions.'));
1.1 banghart 188: }
1.6 banghart 189: 1;
190: __END__;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>