File:  [LON-CAPA] / loncom / interface / lonprintout.pm
Revision 1.31: download - view: text, annotated - select for diffs
Wed May 22 21:10:42 2002 UTC (22 years ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
 corrected to print subdirectories

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

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