File:  [LON-CAPA] / loncom / interface / lonprintout.pm
Revision 1.14: download - view: text, annotated - select for diffs
Thu Feb 21 04:12:16 2002 UTC (22 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- should get all files through lonnet::ssi

    1: # The LearningOnline Network
    2: # Printout
    3: #
    4: # $Id: lonprintout.pm,v 1.14 2002/02/21 04:12:16 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>Printout:</h1><br></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">  Standard LaTeX output for current document<br />
   72: <input type="radio" name="choice" value="Standard LaTeX output for the primary sequence">  Standard LaTeX output for the primary sequence<br />
   73: <input type="radio" name="choice" value="Standard LaTeX output for the top level sequence">  Standard LaTeX output for the top level sequence<br />
   74: <input type="submit" value="Please make a choice">
   75: </form>
   76: </body>
   77: </html>
   78: ENDMENUOUT
   79: }
   80: 
   81: 
   82: sub output_data {
   83:     my $r = shift;
   84:     $r->print(<<ENDPART);
   85: <html>
   86: <head>
   87: <title>LON-CAPA output for printing</title>
   88: </head>
   89: <body bgcolor="FFFFFF">
   90: <hr>
   91: ENDPART
   92: 
   93:     my $choice = $ENV{'form.choice'};
   94:     my $result = '';
   95:     my %mystyle;
   96:     my $filename;
   97: 
   98:     if ($choice eq 'Standard LaTeX output for current document') {
   99:       my %moreenv;
  100:       my $currequest=$ENV{'request.filename'};
  101:       $moreenv{'form.grade_target'}='tex';
  102:       $moreenv{'request.filename'}=$ENV{'form.url'};
  103:       &Apache::lonnet::appenv(%moreenv);
  104:       my $texversion=&Apache::lonnet::ssi($ENV{'form.url'});
  105:       &Apache::lonnet::delenv('form.grade_target');
  106:       %moreenv = ();
  107:       $moreenv{'request.filename'}=$currequest;
  108:       &Apache::lonnet::appenv(%moreenv);
  109:       $texversion =~ s!\.gif!\.eps!;
  110:       $result .= $texversion;
  111:     } elsif ($choice eq 'Standard LaTeX output for the primary sequence') {
  112: 	my @master_seq = ();
  113: 	my $keyword = 0;
  114: 	my $output_seq = '';
  115: 	my $current_file = '/res/'.$ENV{'request.ambiguous'};
  116:         $current_file =~ s/(\/res\/physnet\/physnet)(\/m\d+)\/(.*)/$1$2$2\.sequence/;
  117: 	while ($current_file ne '') {
  118: 	    my $file=&Apache::lonnet::filelocation("",$current_file);
  119: 	    my $filecontents=&Apache::lonnet::getfile($file);
  120: 	    my @file_seq = &content_map($filecontents);
  121: 	    if (defined @file_seq) {
  122: #-- adding an additional array to the master one
  123: 		if (defined @master_seq) {
  124:                     my $old_value = $#master_seq;
  125: 		    my $total_value = $#master_seq + $#file_seq +2;
  126:                     for (my $j=0; $j<=$old_value-$keyword+1; $j++) {
  127: 			$master_seq[$total_value-$j] = $master_seq[$old_value-$j];
  128: 		    }
  129:                     for (my $j=0; $j<=$#file_seq; $j++){
  130: 			$master_seq[$keyword+$j] = $file_seq[$j];
  131: 		    }
  132: 		    @file_seq = ();
  133: 		    $keyword = 0;
  134: 		} else {
  135: 		    @master_seq = @file_seq;
  136:                     @file_seq = ();
  137: 		}
  138: 	    } 
  139: #-- checking wether .sequence file is among the set of files
  140: 	    $current_file = '';
  141: 	    for (my $i=0; $i<=$#file_seq; $i++) {
  142: 		$_ = $file_seq[$i];
  143: 		if (m/(.*)\.sequence/) {
  144: 		    $current_file = $_;
  145: 		    $keyword = $i;
  146: 		    last;
  147: 		}
  148: 	    }        
  149: 	}
  150: #-- produce an output string
  151: 	for (my $i=0;$i<=$#master_seq;$i++) {
  152: 	    $_ = $master_seq[$i];
  153: 	    m/\"(.*)\"/;
  154: 	    if (index($1,'-tc.xml',0)==-1) {
  155: 		my $file=&Apache::lonnet::filelocation("",$1);
  156: 		my $filecontents=&Apache::lonnet::getfile($file);
  157: 		$output_seq .= $filecontents;
  158: 	    }
  159: 	}
  160: #-- cleanup of output string (temporary cbi-specific)
  161: 	$output_seq =~ s/<physnet>//g;
  162: 	$output_seq =~ s/<\/physnet>//g;
  163: 	$output_seq = '<physnet>'.$output_seq.' </physnet>';
  164: #-- final accord
  165: 	$result = &Apache::lonxml::xmlparse('tex',$output_seq,'',%mystyle);
  166:     }  elsif ($choice eq 'Standard LaTeX output for the top level sequence') {
  167: 
  168: #-- where is the main sequence of the course?
  169: 
  170: 	my @master_seq = ();
  171: 	my $keyword = 0;
  172: 	my $output_seq = '';
  173: 
  174: 	my $main_seq = '/res/'.$ENV{'request.course.uri'};
  175: 	my $file=&Apache::lonnet::filelocation("",$main_seq);
  176: 	my $filecontents=&Apache::lonnet::getfile($file);
  177: 	my @file_seq = &content_map($filecontents);
  178: 
  179: #-- temporary solution (without sequence inside sequence) - have to be generalized
  180: 
  181:  
  182: 
  183: 
  184: #		if (defined @master_seq) {
  185: #                    my $old_value = $#master_seq;
  186: #		    my $total_value = $#master_seq + $#file_seq +2;
  187: #                    for (my $j=0; $j<=$old_value-$keyword+1; $j++) {
  188: #			$master_seq[$total_value-$j] = $master_seq[$old_value-$j];
  189: #		    }
  190: #                    for (my $j=0; $j<=$#file_seq; $j++){
  191: #			$master_seq[$keyword+$j] = $file_seq[$j];
  192: #		    }
  193: #		    @file_seq = ();
  194: #		    $keyword = 0;
  195: #		} else {
  196: 		    @master_seq = @file_seq;
  197: #                    @file_seq = ();
  198: #		}
  199: 	     
  200: #-- checking wether .sequence file is among the set of files
  201: #	    my  $current_file = '';
  202: #	    for (my $i=0; $i<=$#file_seq; $i++) {
  203: #		$_ = $file_seq[$i];
  204: #		if (m/(.*)\.sequence/) {
  205: #		    $current_file = $_;
  206: #		    $keyword = $i;
  207: #		    last;
  208: #		}
  209: #	    }        
  210: 	
  211: #-- produce an output string
  212: 	for (my $i=0;$i<=$#master_seq;$i++) {
  213: 	    $_ = $master_seq[$i];
  214: 	    m/\"(.*)\"/;
  215:             $_ = $1;
  216:             my $urlp = $1;
  217:             if (/\.problem/) {
  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!\.gif!\.eps!;
  224: 		$result .= $texversion;        
  225: 	    }
  226: 	}
  227: #-- additional cleanup for output	
  228: 	my $first_app = index($result,'\documentclass',0);
  229: 	$first_app = index($result,'\documentclass',$first_app+5);
  230: 	while ($first_app != -1) {
  231: 	    my $second_app = index($result,'begin{document}',$first_app);
  232: 	    $first_app = rindex($result,'\end{document}',$first_app);
  233: 	    substr($result,$first_app,$second_app-$first_app+15) = '\vskip 7 mm';
  234: 	    $first_app = index($result,'\documentclass',$first_app+5);
  235: 	}
  236:     }
  237: #-- writing .tex file in prtspool 
  238: 	{
  239: 	    my $temp_file;
  240: 	    $filename = "/home/httpd/prtspool/$ENV{'environment.firstname'}$ENV{'environment.lastname'}temp$ENV{'user.login.time'}.tex";
  241: 	    unless ($temp_file = Apache::File->new('>'.$filename)) {
  242: 		$r->log_error("Couldn't open $filename for output $!");
  243: 		return SERVER_ERROR; 
  244: 	    } 
  245: 		print $temp_file $result;
  246: 	}
  247: $r->print(<<FINALEND);
  248: <meta http-equiv="Refresh" content="0; url=/cgi-bin/printout.pl?$filename">
  249: </body>
  250: </html>
  251: FINALEND
  252: }
  253: 
  254: 
  255: 
  256: 
  257: sub content_map {
  258: #-- find a list of files to print
  259:     my $map_string = shift;
  260:     my @number_seq = ();
  261:     my @file_seq = ();
  262:     my $startlink = index($map_string,'<link',0);
  263:     my $endlink = index($map_string,'</link>',$startlink);
  264:     my $chunk = substr($map_string,$startlink,$endlink-$startlink+7);
  265:     $_ = $chunk;
  266:     m/from=\"(\d+)\"/;
  267:     push @number_seq,$1;
  268:     while ($startlink != -1) {
  269: 	$endlink = index($map_string,'</link>',$startlink);
  270: 	$chunk = substr($map_string,$startlink,$endlink-$startlink+7);
  271: 	substr($map_string,$startlink,$endlink-$startlink+7) = '';
  272: 	$_ = $chunk;
  273:         m/to=\"(\d+)\"/;
  274: 	push @number_seq,$1;
  275: 	$startlink = index($map_string,'from="'.$1.'"',$startlink);
  276:     }
  277:     my $stalink = index($map_string,' to="'.$number_seq[0].'"',$startlink);
  278:     while ($stalink != -1) {
  279: 	$startlink = rindex($map_string,'<link ',$stalink);
  280: 	$endlink = index($map_string,'</link>',$startlink);
  281: 	$chunk = substr($map_string,$startlink,$endlink-$startlink+7);
  282: 	substr($map_string,$startlink,$endlink-$startlink+7) = '';
  283: 	$_ = $chunk;
  284:         m/from=\"(\d+)\"/;
  285: 	unshift @number_seq,$1;
  286: 	$stalink = index($map_string,' to="'.$number_seq[0].'"',0);
  287:     }
  288:     for (my $i=0;$i<=$#number_seq;$i++) {
  289: 	$stalink = index($map_string,' id="'.$number_seq[$i].'"',0);
  290:         {    
  291: 	    my $ahed1 = index($map_string,'src="',$stalink);
  292: 	    my $ahed2 = index($map_string,'</resource>',$stalink);
  293: 	    if ($ahed1 != -1) {
  294: 		if ($ahed1 < $ahed2) {
  295: 		    $startlink = $ahed1;
  296: 		} else {
  297: 		    $startlink = rindex($map_string,'src="',$stalink);
  298: 		}
  299: 	    } else {
  300: 		$startlink = rindex($map_string,'src="',$stalink);
  301: 	    }
  302: 
  303: 	}
  304: 	$startlink = index($map_string,'"',$startlink);
  305: 	$endlink = index($map_string,'"',$startlink+1);
  306: 	$chunk = substr($map_string,$startlink,$endlink-$startlink+1);
  307: 	push @file_seq,$chunk;
  308:     }
  309:     return @file_seq;
  310: }
  311: 
  312: 
  313: 
  314: sub handler {
  315: 
  316:     my $r = shift;
  317:     $r->content_type('text/html');
  318:     $r->send_http_header;
  319: 
  320: #-- start form
  321:     &headerform($r);
  322: #-- menu for output
  323:     unless ($ENV{'form.phase'}) {
  324: 	&menu_for_output($r);
  325:     }
  326: #-- core part 
  327:     if ($ENV{'form.phase'} eq 'two') {
  328: 	&output_data($r);
  329: 
  330:     }
  331:     return OK;
  332: 
  333: } 
  334: 
  335: 1;
  336: __END__
  337: 
  338: 
  339: 
  340: 
  341: #### Test block
  342: #    my $ere;
  343: #    foreach $ere (%ENV) {
  344: #	$result .= ' SS '.$ere.' => '.$ENV{$ere}.' FF '."\n\n";
  345: #    }
  346: ####

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