Annotation of loncom/interface/lonprintout.pm, revision 1.26

1.1       www         1: # The LearningOnline Network
                      2: # Printout
                      3: #
1.26    ! sakharuk    4: # $Id: lonprintout.pm,v 1.25 2002/05/02 21:21:38 sakharuk Exp $
1.11      albertel    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: #
1.1       www        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: #
1.3       sakharuk   38: # 9/17 Alex Sakharuk
                     39: #
1.1       www        40: package Apache::lonprintout;
                     41: 
                     42: use strict;
1.10      albertel   43: use Apache::Constants qw(:common :http);
1.2       sakharuk   44: use Apache::lonxml;
                     45: use Apache::lonnet;
1.13      sakharuk   46: use Apache::inputtags;
                     47: use Apache::edit;
1.5       sakharuk   48: use Apache::File();
1.1       www        49: 
1.3       sakharuk   50: 
                     51: 
                     52: sub headerform {
1.1       www        53:     my $r = shift;
1.3       sakharuk   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">
1.16      sakharuk   61: <h1>What do you want to print? Make a choice.</h1><br />
1.3       sakharuk   62: ENDHEADER
                     63: }
                     64: 
1.1       www        65: 
1.3       sakharuk   66: sub menu_for_output {
                     67:     my $r = shift;
                     68:     $r->print(<<ENDMENUOUT);
                     69: <input type="hidden" name="phase" value="two">
1.10      albertel   70: <input type="hidden" name="url" value="$ENV{'form.postdata'}">
1.25      sakharuk   71: <input type="radio" name="choice" value="Standard LaTeX output for current document" checked>  Current document
1.16      sakharuk   72: (you will print what you see on the screen)<br />
1.19      sakharuk   73: <input type="radio" name="choice" value="Standard LaTeX output for the primary sequence">  All problems from the primary sequence<br />
1.20      sakharuk   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 />
1.19      sakharuk   75: <input type="radio" name="choice" value="Standard LaTeX output for the top level sequence">  All problems from the top level sequence<br />
1.16      sakharuk   76: <br /><hr /><br />
                     77: <h1>And what page format do you prefer?</h1>
1.25      sakharuk   78: <input type="radio" name="layout" value="CBI"> Two columns landscape (<small>CBI</small>) <br />
                     79: <input type="radio" name="layout" value="CAPA" checked>  Two columns portrait (<small>CAPA</small>) <br />
1.16      sakharuk   80: <input type="submit" value="Submit your choice">
1.3       sakharuk   81: </form>
                     82: </body>
                     83: </html>
                     84: ENDMENUOUT
                     85: }
1.2       sakharuk   86: 
                     87: 
1.16      sakharuk   88: 
                     89: 
1.3       sakharuk   90: sub output_data {
                     91:     my $r = shift;
                     92:     $r->print(<<ENDPART);
                     93: <html>
                     94: <head>
                     95: <title>LON-CAPA output for printing</title>
                     96: </head>
                     97: <body bgcolor="FFFFFF">
                     98: <hr>
                     99: ENDPART
1.2       sakharuk  100: 
1.3       sakharuk  101:     my $choice = $ENV{'form.choice'};
1.16      sakharuk  102:     my $layout = $ENV{'form.layout'};
                    103:     my $laystyle = 'book';
1.2       sakharuk  104:     my $result = '';
                    105:     my %mystyle;
1.6       sakharuk  106:     my $filename;
1.5       sakharuk  107: 
1.3       sakharuk  108:     if ($choice eq 'Standard LaTeX output for current document') {
1.14      albertel  109:       my %moreenv;
                    110:       my $currequest=$ENV{'request.filename'};
                    111:       $moreenv{'form.grade_target'}='tex';
                    112:       $moreenv{'request.filename'}=$ENV{'form.url'};
                    113:       &Apache::lonnet::appenv(%moreenv);
                    114:       my $texversion=&Apache::lonnet::ssi($ENV{'form.url'});
                    115:       &Apache::lonnet::delenv('form.grade_target');
                    116:       %moreenv = ();
                    117:       $moreenv{'request.filename'}=$currequest;
                    118:       &Apache::lonnet::appenv(%moreenv);
                    119:       $result .= $texversion;
1.16      sakharuk  120: 
1.20      sakharuk  121:     } elsif ($choice eq 'Standard LaTeX output for the primary sequence' or $choice eq 'Standard LaTeX output for whole primary sequence') {
1.18      sakharuk  122: #-- where is the primary sequence containing file?
                    123: 	my %moreenv;
                    124: 	my $symbolic = &Apache::lonnet::symbread($ENV{'form.url'});
                    125: 	$_ = $symbolic;
                    126: 	m/([^_]+)_/;
                    127: 	my $primary_sequence = '/res/'.$1;
                    128: #-- open and analyses the primary sequence
                    129: 	my $sequence_file=&Apache::lonnet::filelocation("",$primary_sequence);
                    130: 	my $sequencefilecontents=&Apache::lonnet::getfile($sequence_file);
                    131: 	my @master_seq = &content_map($sequencefilecontents);
                    132: #-- produce an output string
                    133: 	for (my $i=0;$i<=$#master_seq;$i++) {
                    134: 	    $_ = $master_seq[$i];
                    135: 	    m/\"(.*)\"/;
                    136:             $_ = $1;
                    137:             my $urlp = $1;
1.20      sakharuk  138: 	    if ($choice eq 'Standard LaTeX output for the primary sequence') {
1.21      sakharuk  139: 		if (/\.(problem|exam|quiz|assess|survey|form|library)/) {
1.20      sakharuk  140: 		    my %moreenv;
                    141: 		    $moreenv{'form.grade_target'}='tex';
                    142: 		    &Apache::lonnet::appenv(%moreenv);
                    143: 		    my $texversion=&Apache::lonnet::ssi($urlp);
                    144: 		    &Apache::lonnet::delenv('form.grade_target');
                    145: 		    $result .= $texversion;        
                    146: 		}
                    147: 	    } else {
1.18      sakharuk  148: 		$moreenv{'form.grade_target'}='tex';
                    149: 		&Apache::lonnet::appenv(%moreenv);
                    150: 		my $texversion=&Apache::lonnet::ssi($urlp);
                    151: 		&Apache::lonnet::delenv('form.grade_target');
1.20      sakharuk  152: 		$result .= $texversion;    
1.16      sakharuk  153: 	    }
                    154: 	}
1.18      sakharuk  155: #-- additional cleanup for output	
                    156: 	my $first_app = index($result,'\documentclass',0);
                    157: 	$first_app = index($result,'\documentclass',$first_app+5);
                    158: 	while ($first_app != -1) {
                    159: 	    my $second_app = index($result,'begin{document}',$first_app);
                    160: 	    $first_app = rindex($result,'\end{document}',$first_app);
1.20      sakharuk  161: 	    substr($result,$first_app,$second_app-$first_app+15) = '\vskip 3 mm';
1.18      sakharuk  162: 	    $first_app = index($result,'\documentclass',$first_app+5);
1.7       sakharuk  163: 	}
1.13      sakharuk  164:     }  elsif ($choice eq 'Standard LaTeX output for the top level sequence') {
                    165: 	my @master_seq = ();
1.19      sakharuk  166: 	my @add_file_seq = ();
1.16      sakharuk  167: #-- where is the main sequence of the course?
1.13      sakharuk  168: 	my $main_seq = '/res/'.$ENV{'request.course.uri'};
                    169: 	my $file=&Apache::lonnet::filelocation("",$main_seq);
                    170: 	my $filecontents=&Apache::lonnet::getfile($file);
                    171: 	my @file_seq = &content_map($filecontents);
1.19      sakharuk  172: #-- do we have any other sequence inside?
                    173: 	my $i=0;
                    174: 	while ($i<=$#file_seq) {
                    175: 	    $_ = $file_seq[$i];
                    176: 	    if (/\.sequence$/) {
                    177: 		$file = &Apache::lonnet::filelocation("",$file_seq[$i]);
                    178: 		$filecontents=&Apache::lonnet::getfile($file);
                    179: 		@add_file_seq = &content_map($filecontents);
                    180: 		splice(@file_seq,$i,1,@add_file_seq);
                    181: 		@add_file_seq = ();
                    182: 		$i = -1;
                    183: 	    }
                    184: 	    $i++;
                    185: 	}
                    186: 	@master_seq = @file_seq;	
1.13      sakharuk  187: #-- produce an output string
                    188: 	for (my $i=0;$i<=$#master_seq;$i++) {
                    189: 	    $_ = $master_seq[$i];
                    190: 	    m/\"(.*)\"/;
                    191:             $_ = $1;
                    192:             my $urlp = $1;
1.21      sakharuk  193:             if (/\.(problem|exam|quiz|assess|survey|form|library)/) {
1.13      sakharuk  194: 		my %moreenv;
                    195: 		$moreenv{'form.grade_target'}='tex';
                    196: 		&Apache::lonnet::appenv(%moreenv);
                    197: 		my $texversion=&Apache::lonnet::ssi($urlp);
                    198: 		&Apache::lonnet::delenv('form.grade_target');
                    199: 		$result .= $texversion;        
                    200: 	    }
                    201: 	}
                    202: #-- additional cleanup for output	
                    203: 	my $first_app = index($result,'\documentclass',0);
                    204: 	$first_app = index($result,'\documentclass',$first_app+5);
                    205: 	while ($first_app != -1) {
                    206: 	    my $second_app = index($result,'begin{document}',$first_app);
                    207: 	    $first_app = rindex($result,'\end{document}',$first_app);
1.20      sakharuk  208: 	    substr($result,$first_app,$second_app-$first_app+15) = '\vskip 3 mm';
1.13      sakharuk  209: 	    $first_app = index($result,'\documentclass',$first_app+5);
                    210: 	}
1.7       sakharuk  211:     }
1.16      sakharuk  212: #-- corrections for the different page formats
                    213:     if ($layout eq 'CBI') {
                    214: 	$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}/;
                    215:         $laystyle = 'album';
                    216:     } elsif ($layout eq 'CAPA') {
                    217:         my $courseidinfo = $ENV{'request.role'};
                    218:         $_ = $courseidinfo;
                    219:         m/.*\/(.*)/;
                    220:         $courseidinfo = $ENV{'course.physnet_'.$1.'.description'};
                    221: 	$result =~ s/\\documentclass\[letterpaper\]{article}/\\documentclass\[twocolumn\]{article}/;
1.24      sakharuk  222: 	$result =~ s/\\begin{document}/\\textheight 25\.9cm\\oddsidemargin = -0\.57in\\evensidemargin = -0\.57in\\textwidth= 7\.7in\\renewcommand{\\ref}{\\keephidden\}\\begin{document}\\voffset=-1\.8cm\\setcounter{page}{1}\\noindent\\fbox{\\textbf{$ENV{'environment.firstname'} $ENV{'environment.lastname'}}}\\hskip 1\.4in $courseidinfo \\vskip 5 mm /;
1.16      sakharuk  223: 	$result =~ s/\\includegraphics/\\includegraphics\[width=9\.0 cm\]/g;
1.25      sakharuk  224: 	$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/;
1.16      sakharuk  225:     }
1.23      sakharuk  226: #-- LaTeX corrections     
                    227:     my $first_comment = index($result,'<!--',0);
                    228:     while ($first_comment != -1) {
                    229: 	my $end_comment = index($result,'-->',$first_comment);
                    230: 	substr($result,$first_comment,$end_comment-$first_comment+3) = '';
                    231: 	$first_comment = index($result,'<!--',$first_comment);
                    232:     }
1.17      sakharuk  233:     $result =~ s/^\s+$//gm; #remove empty lines
                    234:     $result =~ s/%/\\%/g;   #corrects %
1.20      sakharuk  235:     $result =~ s/(\s)+/$1/g; #removes more than one empty space
1.23      sakharuk  236:     $result =~ s/\\\\\s*\\vskip/\\vskip/gm;
1.22      sakharuk  237:     $result =~ s/ (<|>|) / \$$1\$ /g; #corrects < or >
1.24      sakharuk  238:     $result =~ s/\\\\\s*\\noindent\s*(\\\\)+/\\\\\\noindent /g;
1.23      sakharuk  239:     $result =~ s/{\\par }\s*\\\\/\\\\/gm;
1.24      sakharuk  240: 	$result =~ s/\\\\\s+\[/ \[/g;
                    241:     $result =~ s/&#952;/\$\\theta\$/g; #converts theta from html into tex
1.7       sakharuk  242: #-- writing .tex file in prtspool 
1.16      sakharuk  243:     my $temp_file;
1.20      sakharuk  244:     $filename = "/home/httpd/prtspool/$ENV{'user.name'}$ENV{'user.domain'}temp$ENV{'user.login.time'}.tex";
1.16      sakharuk  245:     unless ($temp_file = Apache::File->new('>'.$filename)) {
                    246: 	$r->log_error("Couldn't open $filename for output $!");
                    247: 	return SERVER_ERROR; 
                    248:     } 
                    249:     print $temp_file $result;
1.3       sakharuk  250: $r->print(<<FINALEND);
1.26    ! sakharuk  251: <meta http-equiv="Refresh" content="0; url=/cgi-bin/printout.pl?$filename&$laystyle">
1.3       sakharuk  252: </body>
                    253: </html>
                    254: FINALEND
                    255: }
1.2       sakharuk  256: 
1.3       sakharuk  257: 
1.6       sakharuk  258: 
                    259: 
1.3       sakharuk  260: sub content_map {
1.7       sakharuk  261: #-- find a list of files to print
1.3       sakharuk  262:     my $map_string = shift;
1.5       sakharuk  263:     my @number_seq = ();
1.7       sakharuk  264:     my @file_seq = ();
1.5       sakharuk  265:     my $startlink = index($map_string,'<link',0);
1.7       sakharuk  266:     my $endlink = index($map_string,'</link>',$startlink);
                    267:     my $chunk = substr($map_string,$startlink,$endlink-$startlink+7);
                    268:     $_ = $chunk;
                    269:     m/from=\"(\d+)\"/;
                    270:     push @number_seq,$1;
1.5       sakharuk  271:     while ($startlink != -1) {
1.7       sakharuk  272: 	$endlink = index($map_string,'</link>',$startlink);
                    273: 	$chunk = substr($map_string,$startlink,$endlink-$startlink+7);
1.5       sakharuk  274: 	substr($map_string,$startlink,$endlink-$startlink+7) = '';
                    275: 	$_ = $chunk;
                    276:         m/to=\"(\d+)\"/;
                    277: 	push @number_seq,$1;
1.13      sakharuk  278: 	$startlink = index($map_string,'from="'.$1.'"',$startlink);
1.18      sakharuk  279: 	$startlink = rindex($map_string,'<link ',$startlink);
1.5       sakharuk  280:     }
1.7       sakharuk  281:     my $stalink = index($map_string,' to="'.$number_seq[0].'"',$startlink);
                    282:     while ($stalink != -1) {
                    283: 	$startlink = rindex($map_string,'<link ',$stalink);
                    284: 	$endlink = index($map_string,'</link>',$startlink);
                    285: 	$chunk = substr($map_string,$startlink,$endlink-$startlink+7);
                    286: 	substr($map_string,$startlink,$endlink-$startlink+7) = '';
                    287: 	$_ = $chunk;
                    288:         m/from=\"(\d+)\"/;
                    289: 	unshift @number_seq,$1;
                    290: 	$stalink = index($map_string,' to="'.$number_seq[0].'"',0);
                    291:     }
                    292:     for (my $i=0;$i<=$#number_seq;$i++) {
                    293: 	$stalink = index($map_string,' id="'.$number_seq[$i].'"',0);
1.13      sakharuk  294:         {    
                    295: 	    my $ahed1 = index($map_string,'src="',$stalink);
                    296: 	    my $ahed2 = index($map_string,'</resource>',$stalink);
                    297: 	    if ($ahed1 != -1) {
                    298: 		if ($ahed1 < $ahed2) {
                    299: 		    $startlink = $ahed1;
                    300: 		} else {
                    301: 		    $startlink = rindex($map_string,'src="',$stalink);
                    302: 		}
                    303: 	    } else {
                    304: 		$startlink = rindex($map_string,'src="',$stalink);
                    305: 	    }
                    306: 
                    307: 	}
1.7       sakharuk  308: 	$startlink = index($map_string,'"',$startlink);
                    309: 	$endlink = index($map_string,'"',$startlink+1);
                    310: 	$chunk = substr($map_string,$startlink,$endlink-$startlink+1);
                    311: 	push @file_seq,$chunk;
                    312:     }
                    313:     return @file_seq;
                    314: }
1.5       sakharuk  315: 
1.3       sakharuk  316: 
                    317: 
                    318: sub handler {
                    319: 
                    320:     my $r = shift;
                    321:     $r->content_type('text/html');
                    322:     $r->send_http_header;
                    323: 
                    324: #-- start form
                    325:     &headerform($r);
                    326: #-- menu for output
1.16      sakharuk  327:     unless  ($ENV{'form.phase'}) {
1.3       sakharuk  328: 	&menu_for_output($r);
                    329:     }
                    330: #-- core part 
                    331:     if ($ENV{'form.phase'} eq 'two') {
                    332: 	&output_data($r);
                    333:     }
1.2       sakharuk  334:     return OK;
                    335: 
1.1       www       336: } 
                    337: 
                    338: 1;
                    339: __END__
1.6       sakharuk  340: 
                    341: 
1.13      sakharuk  342: 
                    343: 
                    344: #### Test block
1.6       sakharuk  345: #    my $ere;
                    346: #    foreach $ere (%ENV) {
1.13      sakharuk  347: #	$result .= ' SS '.$ere.' => '.$ENV{$ere}.' FF '."\n\n";
1.6       sakharuk  348: #    }
1.13      sakharuk  349: ####

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