1: # The LearningOnline Network
2: # Printout
3: #
4: # $Id: lonprintout.pm,v 1.36 2002/07/01 20:56:20 sakharuk 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: use POSIX qw(strftime);
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>What do you want to print? Make a choice.</h1><br />
62: ENDHEADER
63: }
64:
65:
66: sub menu_for_output {
67: my $r = shift;
68: $r->print(<<ENDMENUOUT1);
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" checked> Current document
72: (you will print what you see on the screen)<br />
73: <input type="radio" name="choice" value="Standard LaTeX output for the primary sequence"> All problems from the primary sequence<br />
74: <input type="radio" name="choice" value="Standard LaTeX output for whole primary sequence"> The whole primary sequence (problems plus all html and xml files)<br />
75: <input type="radio" name="choice" value="Standard LaTeX output for the top level sequence"> All problems from the top level sequence<br />
76: <br />
77: ENDMENUOUT1
78: my $subdirtoprint = &Apache::lonnet::filelocation("",$ENV{'form.url'});
79: $subdirtoprint =~ s/\/[^\/]+$//;
80: if (&Apache::lonnet::allowed('bre',$subdirtoprint) eq 'F') {
81: $r->print(<<ENDMENUOUT2);
82: <input type="radio" name="choice" value="Subdirectory print"> All problems from current subdirectory (where this particular problem is)<br />
83: ENDMENUOUT2
84: }
85: $r->print(<<ENDMENUOUT3);
86: <br /><hr /><br />
87: <h1>And what page format do you prefer?</h1>
88: <table>
89: <tr>
90: <td>
91: <input type="radio" name="layout" value="CBI"> Landscape <br />
92: <input type="radio" name="layout" value="CAPA" checked> Portrait <br />
93: </td>
94: <td> </td>
95: <td rawspan="2">
96: Number of columns: <input type="text" size="2" name="numberofcolumns" value="2">
97: </td>
98: </tr>
99: </table>
100: </br>
101: <input type="submit" value="Submit your choice">
102: </form>
103: </body>
104: </html>
105: ENDMENUOUT3
106: }
107:
108:
109:
110:
111: sub output_data {
112: my $r = shift;
113: $r->print(<<ENDPART);
114: <html>
115: <head>
116: <title>LON-CAPA output for printing</title>
117: </head>
118: <body bgcolor="FFFFFF">
119: <hr>
120: ENDPART
121:
122: my $choice = $ENV{'form.choice'};
123: my $layout = $ENV{'form.layout'};
124: my $numberofcolumns = $ENV{'form.numberofcolumns'};
125: my $laystyle = 'book';
126: my $result = '';
127: my $number_of_columns = 1;
128:
129: if ($choice eq 'Standard LaTeX output for current document') {
130: #-- single document - problem, page, html, xml
131: my %moreenv;
132: $moreenv{'form.grade_target'}='tex';
133: $moreenv{'request.filename'}=$ENV{'form.url'};
134: &Apache::lonnet::appenv(%moreenv);
135: my $texversion=&Apache::lonnet::ssi($ENV{'form.url'});
136: &Apache::lonnet::delenv('form.grade_target');
137: $result .= $texversion;
138: $result = &additional_cleanup($result);
139: if ($ENV{'form.url'}=~m/\.page\s*$/) {($result,$number_of_columns) = &page_cleanup($result);}
140: } elsif ($choice eq 'Standard LaTeX output for the primary sequence' or $choice eq 'Standard LaTeX output for whole primary sequence') {
141: #-- minimal sequence to which the current document belongs
142: #-- where is the primary sequence containing file?
143: my $symbolic = &Apache::lonnet::symbread($ENV{'form.url'});
144: $_ = $symbolic;
145: m/([^_]+)_/;
146: my $primary_sequence = '/res/'.$1;
147: #-- open and analyses the primary sequence
148: my $sequence_file=&Apache::lonnet::filelocation("",$primary_sequence);
149: my $sequencefilecontents=&Apache::lonnet::getfile($sequence_file);
150: my @master_seq = &content_map($sequencefilecontents);
151: #-- produce an output string
152: for (my $i=0;$i<=$#master_seq;$i++) {
153: $_ = $master_seq[$i];
154: m/\"(.*)\"/;
155: $_ = $1;
156: my $urlp = $1;
157: if ($choice eq 'Standard LaTeX output for the primary sequence') {
158: if ($urlp =~ m/\.(problem|exam|quiz|assess|survey|form|library)/) {
159: my %moreenv;
160: $moreenv{'form.grade_target'}='tex';
161: &Apache::lonnet::appenv(%moreenv);
162: my $texversion=&Apache::lonnet::ssi($urlp);
163: &Apache::lonnet::delenv('form.grade_target');
164: $result .= $texversion;
165: }
166: } else {
167: my %moreenv;
168: $moreenv{'form.grade_target'}='tex';
169: &Apache::lonnet::appenv(%moreenv);
170: my $texversion=&Apache::lonnet::ssi($urlp);
171: &Apache::lonnet::delenv('form.grade_target');
172: $result .= $texversion;
173: }
174: }
175: $result = &additional_cleanup($result);
176: } elsif ($choice eq 'Standard LaTeX output for the top level sequence') {
177: my @master_seq = ();
178: my @add_file_seq = ();
179: #-- where is the main sequence of the course?
180: my $main_seq = '/res/'.$ENV{'request.course.uri'};
181: my $file=&Apache::lonnet::filelocation("",$main_seq);
182: my $filecontents=&Apache::lonnet::getfile($file);
183: my @file_seq = &content_map($filecontents);
184: #-- do we have any other sequence inside?
185: my $i=0;
186: while ($i<=$#file_seq) {
187: $_ = $file_seq[$i];
188: if (/\.sequence$/) {
189: $file = &Apache::lonnet::filelocation("",$file_seq[$i]);
190: $filecontents=&Apache::lonnet::getfile($file);
191: @add_file_seq = &content_map($filecontents);
192: splice(@file_seq,$i,1,@add_file_seq);
193: @add_file_seq = ();
194: $i = -1;
195: }
196: $i++;
197: }
198: @master_seq = @file_seq;
199: #-- produce an output string
200: for (my $i=0;$i<=$#master_seq;$i++) {
201: $_ = $master_seq[$i];
202: m/\"(.*)\"/;
203: $_ = $1;
204: my $urlp = $1;
205: if (/\.(problem|exam|quiz|assess|survey|form|library)/) {
206: my %moreenv;
207: $moreenv{'form.grade_target'}='tex';
208: &Apache::lonnet::appenv(%moreenv);
209: my $texversion=&Apache::lonnet::ssi($urlp);
210: &Apache::lonnet::delenv('form.grade_target');
211: $result .= $texversion;
212: }
213: }
214: $result = &additional_cleanup($result);
215: } elsif ($choice eq 'Subdirectory print') {
216: my $subdirtoprint = &Apache::lonnet::filelocation("",$ENV{'form.url'});
217: $subdirtoprint =~ s/\/[^\/]+$//;
218: my @list_of_files = ();
219: my $localdirectory = $subdirtoprint;
220: $localdirectory =~ s/.*(\/res\/)/$1/;
221: my @content_directory = &Apache::lonnet::dirlist($localdirectory);
222: for (my $iy=0;$iy<=$#content_directory;$iy++) {
223: my @tempo_array = split(/&/,$content_directory[$iy]);
224: $content_directory[$iy] = $tempo_array[0];
225: if ($content_directory[$iy] =~ m/^[^\.]+\.problem$/) {
226: push @list_of_files,$content_directory[$iy];
227: }
228: }
229: $localdirectory =~ s/\/$//;
230: for (my $i=0;$i<=$#list_of_files;$i++) {
231: my $urlp = $localdirectory.'/'.$list_of_files[$i];
232: my %moreenv;
233: $moreenv{'form.grade_target'}='tex';
234: &Apache::lonnet::appenv(%moreenv);
235: my $texversion=&Apache::lonnet::ssi($urlp);
236: &Apache::lonnet::delenv('form.grade_target');
237: $texversion =~ s/(\\begin{document})/$1 {\\tiny\\begin{verbatim}$urlp\\end{verbatim}}/;
238: $result .= $texversion;
239: }
240: $result = &additional_cleanup($result);
241:
242:
243:
244:
245:
246:
247: }
248: #-- corrections for the different page formats
249: if ($layout eq 'CBI' and $numberofcolumns eq '1') {
250: } elsif ($layout eq 'CBI' and $numberofcolumns eq '2') {
251: $result =~ s/\\begin{document}/\\setlength{\\oddsidemargin}{-40pt}\\setlength{\\evensidemargin}{-60pt}\\setlength{\\topmargin}{200pt}\\setlength{\\textwidth}{4\.4in}\\setlength{\\textheight}{6\.8in}\\setlength{\\parindent}{20pt}\\setlength{\\marginparwidth}{90pt}\\setlength{\\textfloatsep}{8pt plus 2\.0pt minus 4\.0pt} \\begin{document}/;
252: $laystyle = 'album';
253: } elsif ($layout eq 'CAPA') {
254: my $courseidinfo = $ENV{'request.role'};
255: $_ = $courseidinfo;
256: m/.*\/(.*)/;
257: $courseidinfo = $ENV{'course.physnet_'.$1.'.description'};
258: # $result =~ s/\\documentclass\[letterpaper\]{article}/\\documentclass\[twocolumn\]{article}/;
259: $result =~ s/\\documentclass\[letterpaper\]{article}/\\documentclass{article}/;
260: $result =~ s/\\begin{document}/\\textheight 25\.9cm\\oddsidemargin = -0\.57in\\evensidemargin = -0\.57in\\textwidth= 9cm\\newlength{\\minipagewidth}\\setlength{\\minipagewidth}{\\textwidth\/$number_of_columns-0\.2cm}\\renewcommand{\\ref}{\\keephidden\}\\begin{document}\\voffset=-1\.8cm\\setcounter{page}{1}\\parbox{\\minipagewidth}{\\noindent\\fbox{\\textbf{$ENV{'environment.firstname'} $ENV{'environment.lastname'}}}\\hskip 1\.4in $courseidinfo} \\vskip 5 mm /;
261: $result =~ s/\\includegraphics{/\\includegraphics\[width=9\.0 cm\]{/g;
262: $result =~ s/(\\end{document})/\\newline\\noindent\\makebox\[9.0cm\]\[b\]{\\hrulefill}\\newline\\noindent\\tiny Dept\. of Physics and Astronomy, MSU\\makebox\[1.5cm\]\[b\]{\\hfill}LON-CAPA\\copyright MSU GNU\/GPL $1/;
263: $result =~ s/(\\end{longtable}\s*)(\\newline\\noindent\\makebox\[9\.0cm\]\[b\]{\\hrulefill})/$2$1/g;
264: $result =~ s/(\\end{longtable}\s*)\\newline/$1/g;
265: }
266: #-- LaTeX corrections
267: my $first_comment = index($result,'<!--',0);
268: while ($first_comment != -1) {
269: my $end_comment = index($result,'-->',$first_comment);
270: substr($result,$first_comment,$end_comment-$first_comment+3) = '';
271: $first_comment = index($result,'<!--',$first_comment);
272: }
273: $result =~ s/^\s+$//gm; #remove empty lines
274: $result =~ s/%/\\%/g; #corrects %
275: $result =~ s/(\s)+/$1/g; #removes more than one empty space
276: $result =~ s/\\\\\s*\\vskip/\\vskip/gm;
277: $result =~ s/ (<|>|) / \$$1\$ /g; #corrects < or >
278: $result =~ s/\\\\\s*\\noindent\s*(\\\\)+/\\\\\\noindent /g;
279: $result =~ s/{\\par }\s*\\\\/\\\\/gm;
280: $result =~ s/\\\\\s+\[/ \[/g;
281: $result =~ s/θ/\$\\theta\$/g; #converts theta from html into tex
282: $result =~ s/\b_+\b/\\makebox\[1 cm\]\[b\]{\\hrulefill}/g;
283: #-- writing .tex file in prtspool
284: my $temp_file;
285: my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout_".time."_".rand(10000000).".tex";
286: unless ($temp_file = Apache::File->new('>'.$filename)) {
287: $r->log_error("Couldn't open $filename for output $!");
288: return SERVER_ERROR;
289: }
290: print $temp_file $result;
291: $r->print(<<FINALEND);
292: <meta http-equiv="Refresh" content="0; url=/cgi-bin/printout.pl?$filename&$laystyle">
293: </body>
294: </html>
295: FINALEND
296: }
297:
298: sub additional_cleanup {
299: my $result = shift;
300: my $first_app = index($result,'\documentclass',0);
301: $first_app = index($result,'\documentclass',$first_app+5);
302: while ($first_app != -1) {
303: my $second_app = index($result,'begin{document}',$first_app);
304: $first_app = rindex($result,'\end{document}',$first_app);
305: substr($result,$first_app,$second_app-$first_app+15) = '\vskip 3 mm';
306: $first_app = index($result,'\documentclass',$first_app+5);
307: }
308: return $result;
309: }
310: sub page_cleanup {
311: my $result = shift;
312: $_ = $result;
313: m/\\end{document}(\d*)$/;
314: my $number_of_columns = $1;
315: my $insert = '{';
316: for (my $id=1;$id<=$number_of_columns;$id++) { $insert .='l'; }
317: $insert .= '}';
318: $result =~ s/(\\begin{longtable})INSERTTHEHEADOFLONGTABLE/$1$insert/g;
319: $result =~ s/&\s*REMOVETHEHEADOFLONGTABLE\\\\/\\\\/g;
320: $result =~ s/(\\vskip\s*\d+\s*mm)/}\\\\\\parbox{\\minipagewidth}{/g;
321: $result =~ s/\\parbox{\\minipagewidth}{}\s*\\\\\s*(\\parbox{\\minipagewidth})/$1/g;
322: return $result,$number_of_columns;
323: }
324:
325: sub content_map {
326: #-- find a list of files to print
327: my $map_string = shift;
328: my @number_seq = ();
329: my @file_seq = ();
330: my $startlink = index($map_string,'<link',0);
331: my $endlink = index($map_string,'</link>',$startlink);
332: my $chunk = substr($map_string,$startlink,$endlink-$startlink+7);
333: $_ = $chunk;
334: m/from=\"(\d+)\"/;
335: push @number_seq,$1;
336: while ($startlink != -1) {
337: $endlink = index($map_string,'</link>',$startlink);
338: $chunk = substr($map_string,$startlink,$endlink-$startlink+7);
339: substr($map_string,$startlink,$endlink-$startlink+7) = '';
340: $_ = $chunk;
341: m/to=\"(\d+)\"/;
342: push @number_seq,$1;
343: $startlink = index($map_string,'from="'.$1.'"',$startlink);
344: $startlink = rindex($map_string,'<link ',$startlink);
345: }
346: my $stalink = index($map_string,' to="'.$number_seq[0].'"',$startlink);
347: while ($stalink != -1) {
348: $startlink = rindex($map_string,'<link ',$stalink);
349: $endlink = index($map_string,'</link>',$startlink);
350: $chunk = substr($map_string,$startlink,$endlink-$startlink+7);
351: substr($map_string,$startlink,$endlink-$startlink+7) = '';
352: $_ = $chunk;
353: m/from=\"(\d+)\"/;
354: unshift @number_seq,$1;
355: $stalink = index($map_string,' to="'.$number_seq[0].'"',0);
356: }
357: for (my $i=0;$i<=$#number_seq;$i++) {
358: $stalink = index($map_string,' id="'.$number_seq[$i].'"',0);
359: {
360: my $ahed1 = index($map_string,'src="',$stalink);
361: my $ahed2 = index($map_string,'</resource>',$stalink);
362: if ($ahed1 != -1) {
363: if ($ahed1 < $ahed2) {
364: $startlink = $ahed1;
365: } else {
366: $startlink = rindex($map_string,'src="',$stalink);
367: }
368: } else {
369: $startlink = rindex($map_string,'src="',$stalink);
370: }
371:
372: }
373: $startlink = index($map_string,'"',$startlink);
374: $endlink = index($map_string,'"',$startlink+1);
375: $chunk = substr($map_string,$startlink,$endlink-$startlink+1);
376: push @file_seq,$chunk;
377: }
378: return @file_seq;
379: }
380:
381:
382:
383: sub handler {
384:
385: my $r = shift;
386: $r->content_type('text/html');
387: $r->send_http_header;
388:
389: #-- start form
390: &headerform($r);
391: #-- menu for output
392: unless ($ENV{'form.phase'}) {
393: &menu_for_output($r);
394: }
395: #-- core part
396: if ($ENV{'form.phase'} eq 'two') {
397: &output_data($r);
398: }
399: return OK;
400:
401: }
402:
403: 1;
404: __END__
405:
406:
407:
408:
409: #### Test block
410: # my $ere;
411: # foreach $ere (%ENV) {
412: # $result .= ' SS '.$ere.' => '.$ENV{$ere}.' FF '."\n\n";
413: # }
414: ####
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>