1: # The LearningOnline Network
2: # Printout
3: #
4: # $Id: lonprintout.pm,v 1.15 2002/02/21 04:13:20 albertel Exp $
5: #
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: # (Internal Server Error Handler
29: #
30: # (Login Screen
31: # 5/21/99,5/22,5/25,5/26,5/31,6/2,6/10,7/12,7/14,
32: # 1/14/00,5/29,5/30,6/1,6/29,7/1,11/9 Gerd Kortemeyer)
33: #
34: # 3/1/1 Gerd Kortemeyer)
35: #
36: # 3/1 Gerd Kortemeyer
37: #
38: # 9/17 Alex Sakharuk
39: #
40: package Apache::lonprintout;
41:
42: use strict;
43: use Apache::Constants qw(:common :http);
44: use Apache::lonxml;
45: use Apache::lonnet;
46: use Apache::inputtags;
47: use Apache::edit;
48: use Apache::File();
49:
50:
51:
52: sub headerform {
53: my $r = shift;
54: $r->print(<<ENDHEADER);
55: <html>
56: <head>
57: <title>LON-CAPA output for printing</title>
58: </head>
59: <body bgcolor="FFFFFF">
60: <form method="post" enctype="multipart/form-data" action="/adm/printout" name="printform">
61: <h1>Printout:</h1><br></br>
62: ENDHEADER
63: }
64:
65:
66: sub menu_for_output {
67: my $r = shift;
68: $r->print(<<ENDMENUOUT);
69: <input type="hidden" name="phase" value="two">
70: <input type="hidden" name="url" value="$ENV{'form.postdata'}">
71: <input type="radio" name="choice" value="Standard LaTeX output for current document"> Standard LaTeX output for current document<br />
72: <input type="radio" name="choice" value="Standard LaTeX output for the primary sequence"> Standard LaTeX output for the primary sequence<br />
73: <input type="radio" name="choice" value="Standard LaTeX output for the top level sequence"> Standard LaTeX output for the top level sequence<br />
74: <input type="submit" value="Please make a choice">
75: </form>
76: </body>
77: </html>
78: ENDMENUOUT
79: }
80:
81:
82: sub output_data {
83: my $r = shift;
84: $r->print(<<ENDPART);
85: <html>
86: <head>
87: <title>LON-CAPA output for printing</title>
88: </head>
89: <body bgcolor="FFFFFF">
90: <hr>
91: ENDPART
92:
93: my $choice = $ENV{'form.choice'};
94: my $result = '';
95: my %mystyle;
96: my $filename;
97:
98: if ($choice eq 'Standard LaTeX output for current document') {
99: my %moreenv;
100: my $currequest=$ENV{'request.filename'};
101: $moreenv{'form.grade_target'}='tex';
102: $moreenv{'request.filename'}=$ENV{'form.url'};
103: &Apache::lonnet::appenv(%moreenv);
104: my $texversion=&Apache::lonnet::ssi($ENV{'form.url'});
105: &Apache::lonnet::delenv('form.grade_target');
106: %moreenv = ();
107: $moreenv{'request.filename'}=$currequest;
108: &Apache::lonnet::appenv(%moreenv);
109: $result .= $texversion;
110: } elsif ($choice eq 'Standard LaTeX output for the primary sequence') {
111: my @master_seq = ();
112: my $keyword = 0;
113: my $output_seq = '';
114: my $current_file = '/res/'.$ENV{'request.ambiguous'};
115: $current_file =~ s/(\/res\/physnet\/physnet)(\/m\d+)\/(.*)/$1$2$2\.sequence/;
116: while ($current_file ne '') {
117: my $file=&Apache::lonnet::filelocation("",$current_file);
118: my $filecontents=&Apache::lonnet::getfile($file);
119: my @file_seq = &content_map($filecontents);
120: if (defined @file_seq) {
121: #-- adding an additional array to the master one
122: if (defined @master_seq) {
123: my $old_value = $#master_seq;
124: my $total_value = $#master_seq + $#file_seq +2;
125: for (my $j=0; $j<=$old_value-$keyword+1; $j++) {
126: $master_seq[$total_value-$j] = $master_seq[$old_value-$j];
127: }
128: for (my $j=0; $j<=$#file_seq; $j++){
129: $master_seq[$keyword+$j] = $file_seq[$j];
130: }
131: @file_seq = ();
132: $keyword = 0;
133: } else {
134: @master_seq = @file_seq;
135: @file_seq = ();
136: }
137: }
138: #-- checking wether .sequence file is among the set of files
139: $current_file = '';
140: for (my $i=0; $i<=$#file_seq; $i++) {
141: $_ = $file_seq[$i];
142: if (m/(.*)\.sequence/) {
143: $current_file = $_;
144: $keyword = $i;
145: last;
146: }
147: }
148: }
149: #-- produce an output string
150: for (my $i=0;$i<=$#master_seq;$i++) {
151: $_ = $master_seq[$i];
152: m/\"(.*)\"/;
153: if (index($1,'-tc.xml',0)==-1) {
154: my $file=&Apache::lonnet::filelocation("",$1);
155: my $filecontents=&Apache::lonnet::getfile($file);
156: $output_seq .= $filecontents;
157: }
158: }
159: #-- cleanup of output string (temporary cbi-specific)
160: $output_seq =~ s/<physnet>//g;
161: $output_seq =~ s/<\/physnet>//g;
162: $output_seq = '<physnet>'.$output_seq.' </physnet>';
163: #-- final accord
164: $result = &Apache::lonxml::xmlparse('tex',$output_seq,'',%mystyle);
165: } elsif ($choice eq 'Standard LaTeX output for the top level sequence') {
166:
167: #-- where is the main sequence of the course?
168:
169: my @master_seq = ();
170: my $keyword = 0;
171: my $output_seq = '';
172:
173: my $main_seq = '/res/'.$ENV{'request.course.uri'};
174: my $file=&Apache::lonnet::filelocation("",$main_seq);
175: my $filecontents=&Apache::lonnet::getfile($file);
176: my @file_seq = &content_map($filecontents);
177:
178: #-- temporary solution (without sequence inside sequence) - have to be generalized
179:
180:
181:
182:
183: # if (defined @master_seq) {
184: # my $old_value = $#master_seq;
185: # my $total_value = $#master_seq + $#file_seq +2;
186: # for (my $j=0; $j<=$old_value-$keyword+1; $j++) {
187: # $master_seq[$total_value-$j] = $master_seq[$old_value-$j];
188: # }
189: # for (my $j=0; $j<=$#file_seq; $j++){
190: # $master_seq[$keyword+$j] = $file_seq[$j];
191: # }
192: # @file_seq = ();
193: # $keyword = 0;
194: # } else {
195: @master_seq = @file_seq;
196: # @file_seq = ();
197: # }
198:
199: #-- checking wether .sequence file is among the set of files
200: # my $current_file = '';
201: # for (my $i=0; $i<=$#file_seq; $i++) {
202: # $_ = $file_seq[$i];
203: # if (m/(.*)\.sequence/) {
204: # $current_file = $_;
205: # $keyword = $i;
206: # last;
207: # }
208: # }
209:
210: #-- produce an output string
211: for (my $i=0;$i<=$#master_seq;$i++) {
212: $_ = $master_seq[$i];
213: m/\"(.*)\"/;
214: $_ = $1;
215: my $urlp = $1;
216: if (/\.problem/) {
217: my %moreenv;
218: $moreenv{'form.grade_target'}='tex';
219: &Apache::lonnet::appenv(%moreenv);
220: my $texversion=&Apache::lonnet::ssi($urlp);
221: &Apache::lonnet::delenv('form.grade_target');
222: $texversion =~ s!\.gif!\.eps!;
223: $result .= $texversion;
224: }
225: }
226: #-- additional cleanup for output
227: my $first_app = index($result,'\documentclass',0);
228: $first_app = index($result,'\documentclass',$first_app+5);
229: while ($first_app != -1) {
230: my $second_app = index($result,'begin{document}',$first_app);
231: $first_app = rindex($result,'\end{document}',$first_app);
232: substr($result,$first_app,$second_app-$first_app+15) = '\vskip 7 mm';
233: $first_app = index($result,'\documentclass',$first_app+5);
234: }
235: }
236: #-- writing .tex file in prtspool
237: {
238: my $temp_file;
239: $filename = "/home/httpd/prtspool/$ENV{'environment.firstname'}$ENV{'environment.lastname'}temp$ENV{'user.login.time'}.tex";
240: unless ($temp_file = Apache::File->new('>'.$filename)) {
241: $r->log_error("Couldn't open $filename for output $!");
242: return SERVER_ERROR;
243: }
244: print $temp_file $result;
245: }
246: $r->print(<<FINALEND);
247: <meta http-equiv="Refresh" content="0; url=/cgi-bin/printout.pl?$filename">
248: </body>
249: </html>
250: FINALEND
251: }
252:
253:
254:
255:
256: sub content_map {
257: #-- find a list of files to print
258: my $map_string = shift;
259: my @number_seq = ();
260: my @file_seq = ();
261: my $startlink = index($map_string,'<link',0);
262: my $endlink = index($map_string,'</link>',$startlink);
263: my $chunk = substr($map_string,$startlink,$endlink-$startlink+7);
264: $_ = $chunk;
265: m/from=\"(\d+)\"/;
266: push @number_seq,$1;
267: while ($startlink != -1) {
268: $endlink = index($map_string,'</link>',$startlink);
269: $chunk = substr($map_string,$startlink,$endlink-$startlink+7);
270: substr($map_string,$startlink,$endlink-$startlink+7) = '';
271: $_ = $chunk;
272: m/to=\"(\d+)\"/;
273: push @number_seq,$1;
274: $startlink = index($map_string,'from="'.$1.'"',$startlink);
275: }
276: my $stalink = index($map_string,' to="'.$number_seq[0].'"',$startlink);
277: while ($stalink != -1) {
278: $startlink = rindex($map_string,'<link ',$stalink);
279: $endlink = index($map_string,'</link>',$startlink);
280: $chunk = substr($map_string,$startlink,$endlink-$startlink+7);
281: substr($map_string,$startlink,$endlink-$startlink+7) = '';
282: $_ = $chunk;
283: m/from=\"(\d+)\"/;
284: unshift @number_seq,$1;
285: $stalink = index($map_string,' to="'.$number_seq[0].'"',0);
286: }
287: for (my $i=0;$i<=$#number_seq;$i++) {
288: $stalink = index($map_string,' id="'.$number_seq[$i].'"',0);
289: {
290: my $ahed1 = index($map_string,'src="',$stalink);
291: my $ahed2 = index($map_string,'</resource>',$stalink);
292: if ($ahed1 != -1) {
293: if ($ahed1 < $ahed2) {
294: $startlink = $ahed1;
295: } else {
296: $startlink = rindex($map_string,'src="',$stalink);
297: }
298: } else {
299: $startlink = rindex($map_string,'src="',$stalink);
300: }
301:
302: }
303: $startlink = index($map_string,'"',$startlink);
304: $endlink = index($map_string,'"',$startlink+1);
305: $chunk = substr($map_string,$startlink,$endlink-$startlink+1);
306: push @file_seq,$chunk;
307: }
308: return @file_seq;
309: }
310:
311:
312:
313: sub handler {
314:
315: my $r = shift;
316: $r->content_type('text/html');
317: $r->send_http_header;
318:
319: #-- start form
320: &headerform($r);
321: #-- menu for output
322: unless ($ENV{'form.phase'}) {
323: &menu_for_output($r);
324: }
325: #-- core part
326: if ($ENV{'form.phase'} eq 'two') {
327: &output_data($r);
328:
329: }
330: return OK;
331:
332: }
333:
334: 1;
335: __END__
336:
337:
338:
339:
340: #### Test block
341: # my $ere;
342: # foreach $ere (%ENV) {
343: # $result .= ' SS '.$ere.' => '.$ENV{$ere}.' FF '."\n\n";
344: # }
345: ####
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>