File:  [LON-CAPA] / loncom / interface / lonprintout.pm
Revision 1.34: download - view: text, annotated - select for diffs
Thu Jun 27 18:47:59 2002 UTC (21 years, 11 months ago) by sakharuk
Branches: MAIN
CVS tags: version_0_4, stable_2002_july, STABLE, HEAD
See comments for structuretags.pm

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

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