File:  [LON-CAPA] / loncom / interface / lonprintout.pm
Revision 1.30: download - view: text, annotated - select for diffs
Tue May 21 00:44:03 2002 UTC (22 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- the file name should change everytime, otherwise we browsers start cachign the pdf file

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

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