Annotation of loncom/interface/lonprintout.pm, revision 1.15

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

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