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

1.1       www         1: # The LearningOnline Network
                      2: # Printout
                      3: #
1.16    ! sakharuk    4: # $Id: lonprintout.pm,v 1.15 2002/02/21 04:13:20 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">
1.16    ! sakharuk   61: <h1>What do you want to print? Make a choice.</h1><br />
1.3       sakharuk   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'}">
1.16    ! sakharuk   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 />
1.10      albertel   73: <input type="radio" name="choice" value="Standard LaTeX output for the primary sequence">  Standard LaTeX output for the primary sequence<br />
1.16    ! sakharuk   74: <input type="radio" name="choice" value="Standard LaTeX output for the top level sequence">  All documents 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">
1.3       sakharuk   80: </form>
                     81: </body>
                     82: </html>
                     83: ENDMENUOUT
                     84: }
1.2       sakharuk   85: 
                     86: 
1.16    ! sakharuk   87: 
        !            88: 
1.3       sakharuk   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
1.2       sakharuk   99: 
1.3       sakharuk  100:     my $choice = $ENV{'form.choice'};
1.16    ! sakharuk  101:     my $layout = $ENV{'form.layout'};
        !           102:     my $laystyle = 'book';
1.2       sakharuk  103:     my $result = '';
                    104:     my %mystyle;
1.6       sakharuk  105:     my $filename;
1.5       sakharuk  106: 
1.3       sakharuk  107:     if ($choice eq 'Standard LaTeX output for current document') {
1.14      albertel  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;
1.16    ! sakharuk  119: 
1.8       sakharuk  120:     } elsif ($choice eq 'Standard LaTeX output for the primary sequence') {
1.16    ! sakharuk  121:       my @master_seq = ();
        !           122:       my $keyword = 0;
        !           123:       my $output_seq = '';
        !           124:       my $current_file = '/res/'.$ENV{'request.ambiguous'};
        !           125:       $current_file =~ s/(\/res\/physnet\/physnet)(\/m\d+)\/(.*)/$1$2$2\.sequence/;
        !           126:       while ($current_file ne '') {
        !           127: 	my $file=&Apache::lonnet::filelocation("",$current_file);
        !           128: 	my $filecontents=&Apache::lonnet::getfile($file);
        !           129: 	my @file_seq = &content_map($filecontents);
        !           130: 	if (defined @file_seq) {
1.7       sakharuk  131: #-- adding an additional array to the master one
1.16    ! sakharuk  132: 	  if (defined @master_seq) {
        !           133: 	    my $old_value = $#master_seq;
        !           134: 	    my $total_value = $#master_seq + $#file_seq +2;
        !           135: 	    for (my $j=0; $j<=$old_value-$keyword+1; $j++) {
        !           136: 	      $master_seq[$total_value-$j] = $master_seq[$old_value-$j];
        !           137: 	    }
        !           138: 	    for (my $j=0; $j<=$#file_seq; $j++){
        !           139: 	      $master_seq[$keyword+$j] = $file_seq[$j];
        !           140: 	    }
        !           141: 	    @file_seq = ();
        !           142: 	    $keyword = 0;
        !           143: 	  } else {
        !           144: 	    @master_seq = @file_seq;
        !           145: 	    @file_seq = ();
        !           146: 	  }
        !           147: 	}
1.7       sakharuk  148: #-- checking wether .sequence file is among the set of files
1.16    ! sakharuk  149: 	$current_file = '';
        !           150: 	for (my $i=0; $i<=$#file_seq; $i++) {
        !           151: 	  $_ = $file_seq[$i];
        !           152: 	  if (m/(.*)\.sequence/) {
        !           153: 	    $current_file = $_;
        !           154: 	    $keyword = $i;
        !           155: 	    last;
        !           156: 	  }
1.7       sakharuk  157: 	}
1.16    ! sakharuk  158:       }
1.7       sakharuk  159: #-- produce an output string
1.16    ! sakharuk  160:       for (my $i=0;$i<=$#master_seq;$i++) {
        !           161: 	$_ = $master_seq[$i];
        !           162: 	m/\"(.*)\"/;
        !           163: 	if (index($1,'-tc.xml',0)==-1) {
        !           164: 	  my $file=&Apache::lonnet::filelocation("",$1);
        !           165: 	  my $filecontents=&Apache::lonnet::getfile($file);
        !           166: 	  $output_seq .= $filecontents;
1.7       sakharuk  167: 	}
1.16    ! sakharuk  168:       }
1.7       sakharuk  169: #-- cleanup of output string (temporary cbi-specific)
1.16    ! sakharuk  170:       $output_seq =~ s/<physnet>//g;
        !           171:       $output_seq =~ s/<\/physnet>//g;
        !           172:       $output_seq = '<physnet>'.$output_seq.' </physnet>';
1.7       sakharuk  173: #-- final accord
1.16    ! sakharuk  174:       $result = &Apache::lonxml::xmlparse('tex',$output_seq,'',%mystyle);
1.13      sakharuk  175:     }  elsif ($choice eq 'Standard LaTeX output for the top level sequence') {
                    176: 	my @master_seq = ();
                    177: 	my $keyword = 0;
                    178: 	my $output_seq = '';
1.16    ! sakharuk  179: #-- where is the main sequence of the course?
1.13      sakharuk  180: 	my $main_seq = '/res/'.$ENV{'request.course.uri'};
                    181: 	my $file=&Apache::lonnet::filelocation("",$main_seq);
                    182: 	my $filecontents=&Apache::lonnet::getfile($file);
                    183: 	my @file_seq = &content_map($filecontents);
1.16    ! sakharuk  184: #-- temporary solution (without sequence inside sequence) - have to be generalized
1.13      sakharuk  185: 
                    186: 
                    187: 
                    188: 
                    189: #		if (defined @master_seq) {
                    190: #                    my $old_value = $#master_seq;
                    191: #		    my $total_value = $#master_seq + $#file_seq +2;
                    192: #                    for (my $j=0; $j<=$old_value-$keyword+1; $j++) {
                    193: #			$master_seq[$total_value-$j] = $master_seq[$old_value-$j];
                    194: #		    }
                    195: #                    for (my $j=0; $j<=$#file_seq; $j++){
                    196: #			$master_seq[$keyword+$j] = $file_seq[$j];
                    197: #		    }
                    198: #		    @file_seq = ();
                    199: #		    $keyword = 0;
                    200: #		} else {
                    201: 		    @master_seq = @file_seq;
                    202: #                    @file_seq = ();
                    203: #		}
1.16    ! sakharuk  204: 	
1.13      sakharuk  205: #-- checking wether .sequence file is among the set of files
                    206: #	    my  $current_file = '';
                    207: #	    for (my $i=0; $i<=$#file_seq; $i++) {
                    208: #		$_ = $file_seq[$i];
                    209: #		if (m/(.*)\.sequence/) {
                    210: #		    $current_file = $_;
                    211: #		    $keyword = $i;
                    212: #		    last;
                    213: #		}
                    214: #	    }        
                    215: 	
                    216: #-- produce an output string
                    217: 	for (my $i=0;$i<=$#master_seq;$i++) {
                    218: 	    $_ = $master_seq[$i];
                    219: 	    m/\"(.*)\"/;
                    220:             $_ = $1;
                    221:             my $urlp = $1;
                    222:             if (/\.problem/) {
                    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!\.gif!\.eps!;
                    229: 		$result .= $texversion;        
                    230: 	    }
                    231: 	}
                    232: #-- additional cleanup for output	
                    233: 	my $first_app = index($result,'\documentclass',0);
                    234: 	$first_app = index($result,'\documentclass',$first_app+5);
                    235: 	while ($first_app != -1) {
                    236: 	    my $second_app = index($result,'begin{document}',$first_app);
                    237: 	    $first_app = rindex($result,'\end{document}',$first_app);
                    238: 	    substr($result,$first_app,$second_app-$first_app+15) = '\vskip 7 mm';
                    239: 	    $first_app = index($result,'\documentclass',$first_app+5);
                    240: 	}
1.7       sakharuk  241:     }
1.16    ! sakharuk  242: #-- corrections for the different page formats
        !           243:     if ($layout eq 'CBI') {
        !           244: 	$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}/;
        !           245:         $laystyle = 'album';
        !           246:     } elsif ($layout eq 'CAPA') {
        !           247:         my $courseidinfo = $ENV{'request.role'};
        !           248:         $_ = $courseidinfo;
        !           249:         m/.*\/(.*)/;
        !           250:         $courseidinfo = $ENV{'course.physnet_'.$1.'.description'};
        !           251: 	$result =~ s/\\documentclass\[letterpaper\]{article}/\\documentclass\[twocolumn\]{article}/;
        !           252: 	$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\\newline\\noindent\\textit{Physics for Scientists and Engineers}\\newline\\noindent\\textbf{Due Thursday, March 7, 2002 at 8:00PM}\\vskip 5 mm/;
        !           253: 	$result =~ s/\\includegraphics/\\includegraphics\[width=9\.0 cm\]/g;
        !           254:     }
1.7       sakharuk  255: #-- writing .tex file in prtspool 
1.16    ! sakharuk  256:     my $temp_file;
        !           257:     $filename = "/home/httpd/prtspool/$ENV{'environment.firstname'}$ENV{'environment.lastname'}temp$ENV{'user.login.time'}.tex";
        !           258:     unless ($temp_file = Apache::File->new('>'.$filename)) {
        !           259: 	$r->log_error("Couldn't open $filename for output $!");
        !           260: 	return SERVER_ERROR; 
        !           261:     } 
        !           262:     print $temp_file $result;
1.3       sakharuk  263: $r->print(<<FINALEND);
1.16    ! sakharuk  264: <meta http-equiv="Refresh" content="0; url=/cgi-bin/printout.pl?$filename&$laystyle">
1.3       sakharuk  265: </body>
                    266: </html>
                    267: FINALEND
                    268: }
1.2       sakharuk  269: 
1.3       sakharuk  270: 
1.6       sakharuk  271: 
                    272: 
1.3       sakharuk  273: sub content_map {
1.7       sakharuk  274: #-- find a list of files to print
1.3       sakharuk  275:     my $map_string = shift;
1.5       sakharuk  276:     my @number_seq = ();
1.7       sakharuk  277:     my @file_seq = ();
1.5       sakharuk  278:     my $startlink = index($map_string,'<link',0);
1.7       sakharuk  279:     my $endlink = index($map_string,'</link>',$startlink);
                    280:     my $chunk = substr($map_string,$startlink,$endlink-$startlink+7);
                    281:     $_ = $chunk;
                    282:     m/from=\"(\d+)\"/;
                    283:     push @number_seq,$1;
1.5       sakharuk  284:     while ($startlink != -1) {
1.7       sakharuk  285: 	$endlink = index($map_string,'</link>',$startlink);
                    286: 	$chunk = substr($map_string,$startlink,$endlink-$startlink+7);
1.5       sakharuk  287: 	substr($map_string,$startlink,$endlink-$startlink+7) = '';
                    288: 	$_ = $chunk;
                    289:         m/to=\"(\d+)\"/;
                    290: 	push @number_seq,$1;
1.13      sakharuk  291: 	$startlink = index($map_string,'from="'.$1.'"',$startlink);
1.5       sakharuk  292:     }
1.7       sakharuk  293:     my $stalink = index($map_string,' to="'.$number_seq[0].'"',$startlink);
                    294:     while ($stalink != -1) {
                    295: 	$startlink = rindex($map_string,'<link ',$stalink);
                    296: 	$endlink = index($map_string,'</link>',$startlink);
                    297: 	$chunk = substr($map_string,$startlink,$endlink-$startlink+7);
                    298: 	substr($map_string,$startlink,$endlink-$startlink+7) = '';
                    299: 	$_ = $chunk;
                    300:         m/from=\"(\d+)\"/;
                    301: 	unshift @number_seq,$1;
                    302: 	$stalink = index($map_string,' to="'.$number_seq[0].'"',0);
                    303:     }
                    304:     for (my $i=0;$i<=$#number_seq;$i++) {
                    305: 	$stalink = index($map_string,' id="'.$number_seq[$i].'"',0);
1.13      sakharuk  306:         {    
                    307: 	    my $ahed1 = index($map_string,'src="',$stalink);
                    308: 	    my $ahed2 = index($map_string,'</resource>',$stalink);
                    309: 	    if ($ahed1 != -1) {
                    310: 		if ($ahed1 < $ahed2) {
                    311: 		    $startlink = $ahed1;
                    312: 		} else {
                    313: 		    $startlink = rindex($map_string,'src="',$stalink);
                    314: 		}
                    315: 	    } else {
                    316: 		$startlink = rindex($map_string,'src="',$stalink);
                    317: 	    }
                    318: 
                    319: 	}
1.7       sakharuk  320: 	$startlink = index($map_string,'"',$startlink);
                    321: 	$endlink = index($map_string,'"',$startlink+1);
                    322: 	$chunk = substr($map_string,$startlink,$endlink-$startlink+1);
                    323: 	push @file_seq,$chunk;
                    324:     }
                    325:     return @file_seq;
                    326: }
1.5       sakharuk  327: 
1.3       sakharuk  328: 
                    329: 
                    330: sub handler {
                    331: 
                    332:     my $r = shift;
                    333:     $r->content_type('text/html');
                    334:     $r->send_http_header;
                    335: 
                    336: #-- start form
                    337:     &headerform($r);
                    338: #-- menu for output
1.16    ! sakharuk  339:     unless  ($ENV{'form.phase'}) {
1.3       sakharuk  340: 	&menu_for_output($r);
                    341:     }
1.16    ! sakharuk  342: 
        !           343: 
1.3       sakharuk  344: #-- core part 
                    345:     if ($ENV{'form.phase'} eq 'two') {
                    346: 	&output_data($r);
1.6       sakharuk  347: 
1.3       sakharuk  348:     }
1.2       sakharuk  349:     return OK;
                    350: 
1.1       www       351: } 
                    352: 
                    353: 1;
                    354: __END__
1.6       sakharuk  355: 
                    356: 
1.13      sakharuk  357: 
                    358: 
                    359: #### Test block
1.6       sakharuk  360: #    my $ere;
                    361: #    foreach $ere (%ENV) {
1.13      sakharuk  362: #	$result .= ' SS '.$ere.' => '.$ENV{$ere}.' FF '."\n\n";
1.6       sakharuk  363: #    }
1.13      sakharuk  364: ####

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