File:  [LON-CAPA] / loncom / interface / lonprintout.pm
Revision 1.19: download - view: text, annotated - select for diffs
Thu Mar 21 18:46:15 2002 UTC (22 years, 2 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
now works with nested sequences correctly for the whole course output

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

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