File:  [LON-CAPA] / loncom / interface / lonprintout.pm
Revision 1.142: download - view: text, annotated - select for diffs
Fri May 2 14:17:57 2003 UTC (21 years, 1 month ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
Now you can print the primary sequence with only problems or with everything else. In addition you can easily print the problems from the whole course (or from part of the course). To print problems from the part of the course we added additional menu (thanks Jeremy). But the navmap menu for item #3 (the same as for item #2 - see previous submission) contains more information than we really need and has to be changed.

    1: # The LearningOnline Network
    2: # Printout
    3: #
    4: # $Id: lonprintout.pm,v 1.142 2003/05/02 14:17:57 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::loncommon;
   47: use Apache::inputtags;
   48: use Apache::grades;
   49: use Apache::edit;
   50: use Apache::File();
   51: use Apache::lonnavmaps;
   52: use POSIX qw(strftime);
   53: use GDBM_File;
   54: 
   55: 
   56: my %hash;
   57: my $LaTeXwidth = 0;
   58: 
   59: sub headerform {
   60:     my $r = shift;
   61:     $r->print(<<ENDHEADER);
   62: <html>
   63: <head>
   64: <title>LON-CAPA output for printing</title>
   65: </head>
   66: <body bgcolor="FFFFFF">
   67: <form method="post" enctype="multipart/form-data" action="/adm/printout" name="printform">
   68: ENDHEADER
   69: 
   70:     if ($ENV{'form.postdata'}=~m|^/res/|) {
   71: 	$r->print(<<ENDHEADER1);
   72: <b>Path to current document: </b><tt>$ENV{'form.postdata'}</tt><p>
   73: ENDHEADER1
   74:     }
   75: }
   76: 
   77: 
   78: sub menu_for_output {
   79:     my $r = shift;
   80:     my ($title_for_single_resource,$title_for_sequence,$title_for_main_map) = &details_for_menu;
   81:     my $assignment = $title_for_sequence;
   82:     if ($title_for_single_resource ne '') {$title_for_single_resource = '"'.$title_for_single_resource.'"';}
   83:     if ($title_for_sequence ne '') {$title_for_sequence = '"'.$title_for_sequence.'"';}
   84:     if ($title_for_main_map ne '') {$title_for_main_map = '"'.$title_for_main_map.'"';}
   85:     $ENV{'form.postdata'} =~ s|http://[^/]+||;
   86:     my $subdir_to_print = $ENV{'form.postdata'};
   87:     $subdir_to_print =~ m/\/([^\/]+)$/;
   88:     $subdir_to_print =~ s/\Q$1\E//;
   89:     $r->print(<<ENDMENUOUT1);
   90: <h1>What do you want to print? Make a choice.</h1><br />
   91: <input type="hidden" name="phase" value="two">
   92: <input type="hidden" name="url" value="$ENV{'form.postdata'}">
   93: <input type="hidden" name="assignment" value="$assignment">
   94: <input type="radio" name="choice" value="Standard LaTeX output for current document" checked>  Current document <b>$title_for_single_resource</b>
   95: (prints what you just saw on the screen)<br />
   96: ENDMENUOUT1
   97:     if ($ENV{'form.postdata'}=~/\/res\//) {
   98: 	$r->print(<<ENDMENUOUT2);
   99: <input type="radio" name="choice" value="Standard LaTeX output for the primary sequence">  Problem(s) from <b>$title_for_sequence</b><br />
  100: <input type="radio" name="choice" value="Standard LaTeX output for whole primary sequence">  Problem(s) plus page(s) or html/xml file(s) from <b>$title_for_sequence</b><br />
  101: ENDMENUOUT2
  102:     }
  103:     if (($ENV{'request.role'}=~m/^cc\./ or $ENV{'request.role'}=~m/^in\./ or $ENV{'request.role'}=~m/^ta\./) and ($ENV{'form.postdata'}=~/\/res\//)) { 
  104: 	$r->print(<<ENDMENUOUT6);
  105: <input type="radio" name="choice" value="Standard LaTeX output for the top level sequence">  All problems in this course (<b>warning:</b> this may be time consuming) <br />
  106: <br />
  107: <input type="radio" name="choice" value="All class print">  All problems from <b>$title_for_sequence</b> for selected students<br /><br />
  108: ENDMENUOUT6
  109:     }
  110:       my $subdirtoprint = &Apache::lonnet::filelocation("",$ENV{'form.url'});
  111:       $subdirtoprint =~ s/\/[^\/]+$//;
  112:       if ((&Apache::lonnet::allowed('bre',$subdirtoprint) eq 'F') and ($ENV{'form.postdata'}=~/\.(problem|exam|quiz|assess|survey|form|library|page|xml|html|htm|xhtml|xhtm)/)) {
  113: 	  $r->print(<<ENDMENUOUT4);
  114:   <input type="radio" name="choice" value="Subdirectory print">  Problems from current subdirectory <b>$subdir_to_print</b><br />
  115: ENDMENUOUT4
  116:       }
  117:     $r->print(<<ENDMENUOUT5);
  118: <br /><hr /><br />
  119: <h1>And what page format do you prefer?</h1>
  120: <table>
  121:  <tr>
  122:    <td>
  123:      <input type="radio" name="layout" value="CBI"> Landscape <br />
  124:      <input type="radio" name="layout" value="CAPA" checked>  Portrait <br />
  125:    </td>
  126:    <td>&nbsp;</td>
  127:    <td rawspan="2">
  128:      Number of columns: <select name="numberofcolumns">
  129:                          <option> 1 </option>
  130:                          <option selected> 2 </option>
  131:                         </select> 
  132:    </td>
  133:    <td rawspan="2">
  134:      Paper size (format [width x height]): <select name="papersize">
  135:                                             <option selected> Letter [8 1/2x11 in] </option>
  136:                                             <option> Legal [8 1/2x14 in] </option>
  137:                                             <option> Ledger/Tabloid [11x17 in] </option>
  138:                                             <option> Executive [7 1/2x10 in] </option>
  139:                                             <option> A2 [420x594 mm] </option>
  140:                                             <option> A3 [297x420 mm] </option>
  141:                                             <option> A4 [210x297 mm] </option>
  142:                                             <option> A5 [148x210 mm] </option>
  143:                                             <option> A6 [105x148 mm] </option>
  144:                                            </select> 
  145:    </td>
  146:  </tr>
  147: 
  148: </table>
  149: </br> 
  150: <input type="submit" value="Submit your choice">
  151: ENDMENUOUT5
  152: }
  153: 
  154: 
  155: sub sequence_content_menu {
  156:     my ($key_to,$r) = @_;
  157:     my $primary_sequence;
  158:     my $symbolic = &Apache::lonnet::symbread($ENV{'form.url'});
  159:     my @sequence = split('___',$symbolic);
  160:     $primary_sequence=&Apache::lonnet::clutter($sequence[0]);
  161:     my @master_seq = &coming_from_hash($primary_sequence);
  162:     my @titles_master_seq = (); 
  163:     for (my $i=0;$i<=$#master_seq;$i++) {
  164: 	my ($path,$title,$symb) = split /&&/,$master_seq[$i];
  165: 	$master_seq[$i] = $path.'&&'.$symb;
  166:         push @titles_master_seq,  $title;
  167:     }
  168:     my @master_seq_view = @master_seq;
  169:     if ($ENV{'user.adv'}) {
  170:       for (my $i=0;$i<=$#master_seq_view;$i++) {
  171: 	 $master_seq_view[$i]=~/___\d+___(.+)$/;
  172: 	 $master_seq_view[$i]='/'.$1;
  173:       }
  174:     } else {
  175:       for (my $i=0;$i<=$#master_seq_view;$i++) {
  176:          $master_seq_view[$i]=' ';
  177:       }
  178:     }
  179:     $r->print(<<ENDMENUOUT1);
  180: <input type="hidden" name="url" value="$ENV{'form.url'}">
  181: <input type="hidden" name="choice" value="$ENV{'form.choice'}">
  182: <input type="hidden" name="layout" value="$ENV{'form.layout'}">
  183: <input type="hidden" name="papersize" value="$ENV{'form.papersize'}">
  184: <input type="hidden" name="assignment" value="$ENV{'form.assignment'}">
  185: <input type="hidden" name="numberofcolumns" value="$ENV{'form.numberofcolumns'}">
  186: <h1>Mark item(s) which you want to print</h1>
  187: <script>
  188:     function checkall() {
  189: 	for (i=0; i<document.forms.printform.elements.length; i++) {
  190:             if (document.forms.printform.elements[i].name.indexOf('whatfile')==0) {
  191: 	      document.forms.printform.elements[i].checked=true;
  192:             }
  193:         }
  194:     }
  195:     function uncheckall() {
  196: 	for (i=0; i<document.forms.printform.elements.length; i++) {
  197:             if 
  198:           (document.forms.printform.elements[i].name.indexOf('whatfile')==0) {
  199: 	      document.forms.printform.elements[i].checked=false;
  200:             }
  201:         }
  202:     }
  203: </script>
  204: <b>
  205: <input type=button onClick="checkall()" value="Check All">&nbsp;
  206: <input type=button onClick="uncheckall()" value="Uncheck">&nbsp;
  207: <input type="submit" value="Submit">
  208: <p>
  209: ENDMENUOUT1
  210:     my $inc=0; 
  211:     for (my $i=0;$i<=$#master_seq_view;$i++) {
  212:       if ($key_to==1 && $master_seq[$i]=~/\.(problem|exam|quiz|assess|survey|form|library)$/) {
  213: 	$r->print('<br /><input type=checkbox name="whatfile'.$inc.'" value="'.$master_seq[$i].'"> '."\n".'<b>'.
  214:                   $titles_master_seq[$i].'</b>&nbsp;&nbsp;&nbsp;<small>'.$master_seq_view[$i].'</small>');
  215:         $inc++;
  216:     } elsif ($key_to==0 && $master_seq[$i]=~/\.(problem|exam|quiz|assess|survey|form|library|xml|htm|html|page)$/) {
  217: 	$r->print('<br /><input type=checkbox name="whatfile'.$inc.'" value="'.$master_seq[$i].'"> '."\n".'<b>'.
  218:                   $titles_master_seq[$i].'</b>&nbsp;&nbsp;&nbsp;<small>'.$master_seq_view[$i].'</small>');
  219:         $inc++;
  220:       }
  221:     }
  222:     $r->print(<<ENDMENUOUT2);
  223: <br /><br />
  224: <input type="hidden" name="numberoffiles" value="$inc">
  225: <input type="hidden" name="phase" value="three">
  226: <input type=button onClick="checkall()" value="Check All">&nbsp;
  227: <input type=button onClick="uncheckall()" value="Uncheck">&nbsp;
  228: <input type="submit" value="Submit">
  229: ENDMENUOUT2
  230: 
  231: }
  232: 
  233: 
  234: sub problem_choice_menu {
  235:     my $r = shift;
  236:     my $subdirtoprint = &Apache::lonnet::filelocation("",$ENV{'form.url'});
  237:     $subdirtoprint =~ s/\/[^\/]+$//;
  238:     my @list_of_files = ();
  239:     my @content_directory = ();
  240:     $r->print("Subdir: " . $subdirtoprint . "<br />");
  241:     if ($subdirtoprint =~ m|/home/([^/]+)/public_html|) {
  242: 	#localmachine CSTR space
  243: 	my $user=$1;
  244: 	my $domain=$Apache::lonnet::perlvar{'lonDefDomain'};
  245: 	@content_directory = &Apache::lonnet::dirlist($subdirtoprint,
  246: 						      $domain, $user,'');
  247:     } else {
  248:         #local library server res space (allows to print only from the own library server)
  249: 	@content_directory = &Apache::lonnet::dirlist($subdirtoprint,$ENV{'user.domain'}, $ENV{'user.name'},'');
  250:     }
  251:     for (my $iy=0;$iy<=$#content_directory;$iy++) {
  252: 	my @tempo_array = split(/&/,$content_directory[$iy]);
  253: 	if ($tempo_array[0] =~ m/^[^\.]+\.(problem|exam|quiz|assess|survey|form|library)$/) {
  254: 	    push(@list_of_files,$tempo_array[0]);
  255: 	}
  256:     }
  257:     $subdirtoprint =~ s/\/$//;
  258:     for (my $i=0;$i<=$#list_of_files;$i++) {
  259: 	$list_of_files[$i] = $subdirtoprint.'/'.$list_of_files[$i];
  260:     }
  261:     $r->print(<<ENDMENUOUT1);
  262: <input type="hidden" name="url" value="$ENV{'form.url'}">
  263: <input type="hidden" name="choice" value="$ENV{'form.choice'}">
  264: <input type="hidden" name="layout" value="$ENV{'form.layout'}">
  265: <input type="hidden" name="numberofcolumns" value="$ENV{'form.numberofcolumns'}">
  266: <h1>Mark problems which you want to print</h1>
  267: <script>
  268:     function checkall() {
  269: 	for (i=0; i<document.forms.printform.elements.length; i++) {
  270:             if 
  271:           (document.forms.printform.elements[i].name.indexOf('whattoprint')==0) {
  272: 	      document.forms.printform.elements[i].checked=true;
  273:             }
  274:         }
  275:     }
  276: 
  277: 
  278:     function uncheckall() {
  279: 	for (i=0; i<document.forms.printform.elements.length; i++) {
  280:             if 
  281:           (document.forms.printform.elements[i].name.indexOf('whattoprint')==0) {
  282: 	      document.forms.printform.elements[i].checked=false;
  283:             }
  284:         }
  285:     }
  286: </script>
  287: <input type=button onClick="checkall()" value="Check All">&nbsp;
  288: <input type=button onClick="uncheckall()" value="Uncheck">&nbsp;
  289: <input type="submit" value="Submit">
  290: <p>
  291: ENDMENUOUT1
  292:     my $i=0;
  293:     foreach my $file (sort @list_of_files) {
  294: 	$r->print('<br /><input type=checkbox name="whattoprint'.$i.'" value="'.$file.'"> '.
  295:                   $file);
  296: 	$i++;
  297:     }
  298:     $r->print(<<ENDMENUOUT2);
  299: <br /><br />
  300: <input type="hidden" name="papersize" value="$ENV{'form.papersize'}">
  301: <input type="hidden" name="numberofproblems" value="$i">
  302: <input type="hidden" name="phase" value="three">
  303: <input type=button onClick="checkall()" value="Check All">&nbsp;
  304: <input type=button onClick="uncheckall()" value="Uncheck">&nbsp;
  305: <input type="submit" value="Submit">
  306: ENDMENUOUT2
  307: }
  308: 
  309: 
  310: sub additional_class_menu {
  311:     my $r = shift;
  312:     $r->print(<<ENDMENUOUT1);
  313: <input type="hidden" name="url" value="$ENV{'form.url'}">
  314: <input type="hidden" name="choice" value="$ENV{'form.choice'}">
  315: <input type="hidden" name="layout" value="$ENV{'form.layout'}">
  316: <input type="hidden" name="numberofcolumns" value="$ENV{'form.numberofcolumns'}">
  317: <input type="hidden" name="assignment" value="$ENV{'form.assignment'}">
  318: <h1>Select student(s) whose assignment you want to print</h1>
  319: ENDMENUOUT1
  320:     my %courselist=&Apache::lonnet::dump(
  321:                    'classlist',
  322: 		   $ENV{'course.'.$ENV{'request.course.id'}.'.domain'},
  323: 		   $ENV{'course.'.$ENV{'request.course.id'}.'.num'});
  324:     my $now=time;
  325:     $r->print(<<ENDDISHEADER);
  326: <script>
  327:     function checkall() {
  328: 	for (i=0; i<document.forms.printform.elements.length; i++) {
  329:             if 
  330:           (document.forms.printform.elements[i].name.indexOf('whomtoprint')==0) {
  331: 	      document.forms.printform.elements[i].checked=true;
  332:             }
  333:         }
  334:     }
  335: 
  336:     function checksec() {
  337: 	for (i=0; i<document.forms.printform.elements.length; i++) {
  338:             if 
  339:           (document.forms.printform.elements[i].value.indexOf
  340:            (document.forms.printform.chksec.value)==0) {
  341: 	      document.forms.printform.elements[i].checked=true;
  342:             }
  343:         }
  344:     }
  345: 
  346:     function uncheckall() {
  347: 	for (i=0; i<document.forms.printform.elements.length; i++) {
  348:             if 
  349:           (document.forms.printform.elements[i].name.indexOf('whomtoprint')==0) {
  350: 	      document.forms.printform.elements[i].checked=false;
  351:             }
  352:         }
  353:     }
  354: </script>
  355: <input type=button onClick="checkall()" value="Check All">&nbsp;
  356: <input type=button onClick="checksec()" value="Type Section/Group then click">
  357: <input type=text size=5 name=chksec>&nbsp;
  358: <input type=button onClick="uncheckall()" value="Uncheck">
  359: <p>
  360: ENDDISHEADER
  361:     my $i = 0;
  362:     foreach (sort keys %courselist) {
  363:         my ($end,$start)=split(/\:/,$courselist{$_});
  364:         my $active=1;
  365:         if (($end) && ($now>$end)) { $active=0; }
  366:         if ($active) {
  367:            my ($sname,$sdom)=split(/\:/,$_);
  368:            my %reply=&Apache::lonnet::get('environment',
  369:               ['firstname','middlename','lastname','generation'],
  370:               $sdom,$sname);
  371:            my $section=&Apache::lonnet::usection
  372: 	       ($sdom,$sname,$ENV{'request.course.id'});
  373: 	   if ($section == -1) { $section = ''; }
  374:            $r->print(
  375:         '<br /><input type=checkbox name="whomtoprint'.$i.'" value="'.$section.':'.$_.'"> '.
  376: 		      $reply{'firstname'}.' '. 
  377:                       $reply{'middlename'}.' '.
  378:                       $reply{'lastname'}.' '.
  379:                       $reply{'generation'}.
  380:                       ' ('.$_.') '.$section);
  381: 	   $i++;
  382:         } 
  383:     }
  384:     $r->print(<<ENDMENUOUT2);
  385: <br /><br /><br />
  386: <table><tr><td width=300>Number of blank pages added to each student assignments to print these assignments on physically different sheets (<b>requires manual addjustment</b>): <select name="addedpages"><option selected>0</option>
  387:                                                                             <option>1</option>
  388:                                                                             <option>2</option>
  389:                                                                             <option>3</option>
  390:                                                   </select></td></tr></table>
  391: <br /> 
  392: <input type="hidden" name="papersize" value="$ENV{'form.papersize'}">
  393: <input type="hidden" name="numberofstudents" value="$i">
  394: <input type="hidden" name="phase" value="three">
  395: <input type="submit" value="Submit">
  396: ENDMENUOUT2
  397: }
  398: 
  399: 
  400: sub additional_print_menu { 
  401:     my $r = shift;
  402:     my $what_to_print = '';
  403:     for (my $i=0; $i<$ENV{'form.numberofstudents'};$i++) {
  404: 	$what_to_print .= '<input type="hidden" name="whomtoprint'.$i.'" value="'.$ENV{'form.whomtoprint'.$i}.'">';
  405:     }
  406:     for (my $i=0; $i<$ENV{'form.numberofproblems'};$i++) {
  407: 	$what_to_print .= '<input type="hidden" name="whattoprint'.$i.'" value="'.$ENV{'form.whattoprint'.$i}.'">';
  408:     }
  409:     for (my $i=0; $i<$ENV{'form.numberoffiles'};$i++) {
  410: 	$what_to_print .= '<input type="hidden" name="whatfile'.$i.'" value="'.$ENV{'form.whatfile'.$i}.'">';
  411:     }
  412:     $r->print(<<ENDMENUOUT);
  413:     $what_to_print
  414: <input type="hidden" name="url" value="$ENV{'form.url'}">
  415: <input type="hidden" name="choice" value="$ENV{'form.choice'}">
  416: <input type="hidden" name="layout" value="$ENV{'form.layout'}">
  417: <input type="hidden" name="papersize" value="$ENV{'form.papersize'}">
  418: <input type="hidden" name="numberofstudents" value="$ENV{'form.numberofstudents'}">
  419: <input type="hidden" name="numberofproblems" value="$ENV{'form.numberofproblems'}">
  420: <input type="hidden" name="numberoffiles" value="$ENV{'form.numberoffiles'}">
  421: <input type="hidden" name="numberofcolumns" value="$ENV{'form.numberofcolumns'}">
  422: <input type="hidden" name="assignment" value="$ENV{'form.assignment'}">
  423: <input type="hidden" name="addedpages" value="$ENV{'form.addedpages'}">
  424: Define one column layout parameters: <br />
  425: <b>Width</b>: <input type="text" name="width" width="8" value="9cm"> <br />
  426: <b>Height</b>: <input type="text" name="height" width="8" value="25.9cm"> <br />
  427: <b>Left margin</b>: <input type="text" name="leftmargin" width="8" value="-0.57in"> <br />
  428: <input type="hidden" name="phase" value="four">
  429: <input type="submit" value="Submit">
  430: </form>
  431: </body>
  432: </html>
  433: ENDMENUOUT
  434: }
  435: 
  436: 
  437: sub output_data {
  438:     my $r = shift;
  439:     $r->print(<<ENDPART);
  440: <html>
  441: <head>
  442: <title>LON-CAPA output for printing</title>
  443: </head>
  444: <body bgcolor="FFFFFF">
  445: <hr>
  446: ENDPART
  447: 
  448:     my $choice = $ENV{'form.choice'};
  449:     my $layout = $ENV{'form.layout'};
  450:     my $numberofcolumns = $ENV{'form.numberofcolumns'};  
  451:     my $papersize = $ENV{'form.papersize'};          
  452:     my $laystyle = 'book';
  453:     my $assignment =  $ENV{'form.assignment'};
  454:     if ($choice eq 'Subdirectory print') {
  455: 	if ($ENV{'form.layout'} eq '') {$layout = 'CAPA';}
  456: 	if ($ENV{'form.papersize'} eq '') {$papersize = 'Letter [8 1/2x11 in]';}
  457: 	if ($ENV{'form.numberofcolumns'} eq '') {$numberofcolumns = 2;}
  458:     }
  459:     if (($choice eq 'Standard LaTeX output for current document') &&
  460: 	($ENV{'form.url'}=~m|^/~|)) {
  461: 	if ($ENV{'form.layout'} eq '') {$layout = 'CAPA';}
  462: 	if ($ENV{'form.papersize'} eq '') {$papersize = 'Letter [8 1/2x11 in]';}
  463: 	if ($ENV{'form.numberofcolumns'} eq '') {$numberofcolumns = 2;}
  464:     }
  465:     my $result = '';
  466:     my $number_of_columns = 1; #used only for pages to determine the width of the cell
  467:     my $selectionmade = '';
  468:     my ($textwidth,$textheight,$oddoffset,$evenoffset) = &page_format($papersize,$layout,$numberofcolumns);
  469:     if ($numberofcolumns == 1) {
  470: 	$textwidth=$ENV{'form.width'};
  471: 	$textheight=$ENV{'form.height'};
  472:     }
  473:     my $LaTeXwidth; 
  474:     if ($textwidth=~/(\d+\s*cm)/) {
  475: 	$LaTeXwidth = $1*10;
  476:     } elsif ($textwidth=~/(\d+\s*mm)/) {
  477: 	$LaTeXwidth = $1;
  478:     }
  479:     if ($choice eq 'Standard LaTeX output for current document') {
  480:       #-- single document - problem, page, html, xml, ...  
  481:       $selectionmade = 1;
  482:       if ($ENV{'form.url'}=~/\.(problem|exam|quiz|assess|survey|form|library|page|xml|html|htm|xhtml|xhtm)$/) {
  483:         my %moreenv;
  484:         $moreenv{'form.grade_target'}='tex';
  485:         if (&Apache::lonnet::allowed('bre',$ENV{'form.url'})) {
  486: 	  $ENV{'form.url'}=~s/http:\/\/[^\/]+//;
  487:         }
  488:         $moreenv{'request.filename'}=$ENV{'form.url'};
  489:         $moreenv{'form.textwidth'}=$LaTeXwidth;
  490:         &Apache::lonnet::appenv(%moreenv);
  491:         my $texversion=&Apache::lonnet::ssi($ENV{'form.url'});
  492:         &Apache::lonnet::delenv('form.grade_target','form.textwidth','form.counter');
  493:         $result .= $texversion;
  494:         if ($ENV{'form.url'}=~m/\.page\s*$/) {
  495: 	  ($result,$number_of_columns) = &page_cleanup($result);
  496:         }
  497:       } else {
  498: 	  $result='\documentclass[letterpaper]{article}\usepackage{calc}\begin{document}Printout of this type of document is currently not supported: ';
  499: 	  if ($ENV{'form.url'}=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) {
  500: 	      $result.=$1;
  501: 	  } else {
  502: 	      $result.=$ENV{'form.url'};
  503: 	  }
  504: 	  $result.=' \end{document}'
  505:       }
  506:     } elsif ($choice eq 'Standard LaTeX output for the primary sequence' or 
  507:              $choice eq 'Standard LaTeX output for whole primary sequence') {
  508:       #-- minimal sequence to which the current document belongs
  509:         #-- where is the primary sequence containing file?
  510:         my @master_seq = ();
  511: 	for (my $i=0; $i<$ENV{'form.numberoffiles'};$i++) {
  512: 	    if ($ENV{'form.whatfile'.$i}=~/\S/) {
  513: 		push @master_seq,$ENV{'form.whatfile'.$i};
  514: 	    }
  515: 	}
  516:         #-- produce an output string
  517: 	my $flag_latex_header_remove = 'NO';
  518: 	my $flag_page_in_sequence = 'NO';
  519: 	for (my $i=0;$i<=$#master_seq;$i++) {
  520:            my ($urlp,$symb) = split /&&/, $master_seq[$i];
  521: 	    if ($choice eq 'Standard LaTeX output for the primary sequence') {
  522:               $selectionmade = 2;
  523: 	    } elsif ($choice eq 'Standard LaTeX output for whole primary sequence') {
  524:               $selectionmade = 3;
  525: 	    }
  526: 	   my %moreenv;
  527: 	   $moreenv{'form.grade_target'}='tex';
  528: 	   $moreenv{'form.textwidth'}=$LaTeXwidth;
  529: 	   &Apache::lonnet::appenv(%moreenv);
  530: 	   #&Apache::lonnet::logthis("Trying to get $urlp with symb $symb");
  531: 	   my $texversion=&Apache::lonnet::ssi($urlp,('symb'=>$symb));
  532: 	   &Apache::lonnet::delenv('form.grade_target','form.textwidth');
  533: 	   if ($urlp =~ m/\.page/) {
  534: 	     ($texversion,my $number_of_columns_page) = &page_cleanup($texversion);
  535: 	     if ($number_of_columns_page > $number_of_columns) {$number_of_columns=$number_of_columns_page;} 
  536: 	     $texversion =~ s/\\end{document}\d*/\\end{document}/;
  537: 	     $flag_page_in_sequence = 'YES';
  538: 	   } 
  539: 	   if ($flag_latex_header_remove ne 'NO') {
  540: 	     $texversion = &latex_header_footer_remove($texversion);
  541: 	   } else {
  542: 	     $texversion =~ s/\\end{document}//;
  543: 	   }
  544: 	   $result .= $texversion;         
  545: 	   $flag_latex_header_remove = 'YES';   
  546: 	}
  547: 	&Apache::lonnet::delenv('form.counter');
  548: 	if ($flag_page_in_sequence eq 'YES') {$result =~ s/\\usepackage{calc}/\\usepackage{calc}\\usepackage{longtable}/;}	
  549: 	$result .= '\end{document}';
  550:     }  elsif ($choice eq 'Standard LaTeX output for the top level sequence') {
  551:         # where is the main sequence of the course?
  552: 	$selectionmade = 4;
  553: 	my $main_seq=&Apache::lonnet::clutter($ENV{'request.course.uri'});
  554: 	my @file_seq = &coming_from_hash($main_seq);
  555: 	for (my $i=0;$i<=$#file_seq;$i++) {
  556: 	    my ($path,$title,$symb) = split /&&/,$file_seq[$i];
  557: 	    $file_seq[$i] = $path.'&&'.$symb;
  558: 	}
  559: 	my $flag_latex_header_remove = 'NO';
  560:         #-- produce an output string
  561: 	for (my $i=0;$i<=$#file_seq;$i++) {
  562: 	   if (not $file_seq[$i]=~/^&&/) {
  563: 	     my ($urlp,$symb) = split /&&/, $file_seq[$i];
  564: 	     $urlp=~s/\/home\/httpd\/html//;	    
  565: 	     if ($urlp=~m/\.(problem|exam|quiz|assess|survey|form|library)/) {
  566: 		my %moreenv;
  567: 		$moreenv{'form.grade_target'}='tex';
  568: 		$moreenv{'form.textwidth'}=$LaTeXwidth;
  569: 		&Apache::lonnet::appenv(%moreenv);
  570: 		my $texversion=&Apache::lonnet::ssi($urlp,('symb'=>$symb));
  571: 		&Apache::lonnet::delenv('form.grade_target','form.textwidth');		    
  572: 		if ($flag_latex_header_remove ne 'NO') {
  573: 		    $texversion = &latex_header_footer_remove($texversion);
  574: 		} else {
  575: 		    $texversion =~ s/\\end{document}//;
  576: 		}
  577: 		$result .= $texversion;  
  578: 		$flag_latex_header_remove = 'YES';
  579: 	     }		
  580: 	   }
  581: 	}	    
  582: 	$result .= '\end{document}';
  583:     } elsif ($choice eq 'All class print') { 
  584:     #-- prints assignments for whole class or for selected students  
  585: 	$selectionmade = 5;
  586:         my @students = ();
  587: 	for (my $i=0; $i<$ENV{'form.numberofstudents'};$i++) {
  588: 	    if ($ENV{'form.whomtoprint'.$i}=~/:/) {
  589: 		push @students,$ENV{'form.whomtoprint'.$i};
  590: 	    }
  591: 	}
  592: 	#where is the primary sequence containing current resource (the same for all students)?
  593: 	my $symbolic = &Apache::lonnet::symbread($ENV{'form.url'});
  594: 	my @sequence = split('___',$symbolic);
  595: 	my $primary_sequence;
  596: 
  597: 	$primary_sequence=&Apache::lonnet::clutter($sequence[0]);
  598: 	my @master_seq = &coming_from_hash($primary_sequence); 
  599: 	for (my $i=0;$i<=$#master_seq;$i++) {
  600: 	    my ($path,$title,$symb) = split /&&/,$master_seq[$i];
  601: 	    $master_seq[$i] = $path.'&&'.$symb;
  602: 	}
  603:         #loop over students
  604: 	my $flag_latex_header_remove = 'NO'; 
  605: 	my %moreenv;
  606: 	$moreenv{'form.textwidth'}=$LaTeXwidth;
  607: 	&Apache::lonnet::appenv(%moreenv);
  608: 	my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin($r,'Print Status','Class Print Status',$#students+1);
  609: 	foreach my $person (@students) {
  610: 	    my $current_output = ''; 
  611: 	    my ($usersection,$username,$userdomain) = split /:/,$person;
  612: 	    my $fullname = &get_name($username,$userdomain);
  613:             #goes through all resources, checks if they are available for current student, and produces output   
  614: 	    foreach my $curresline (@master_seq)  {
  615: 		my ($curres,$symb) = split /&&/, $curresline;
  616:                 if ($curres=~ m/\.(problem|exam|quiz|assess|survey|form|library)/) {
  617: 		    my ($map,$id,$res_url) = split(/___/,$symb);
  618: 		    if (&Apache::lonnet::allowed('bre',$res_url)) {
  619: 			my $rendered = &Apache::loncommon::get_student_view($symb,$username,$userdomain,
  620:                                                                         $ENV{'request.course.id'},'tex');
  621: 			if ($flag_latex_header_remove eq 'YES') {
  622: 			    $rendered = &latex_header_footer_remove($rendered);
  623: 			} else {
  624: 			    $rendered =~ s/\\end{document}//;
  625: 			}
  626: 			$current_output .= $rendered;
  627: 		    }
  628: 		    $flag_latex_header_remove = 'YES';
  629: 		}
  630: 	    }
  631: 	    my $courseidinfo = $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
  632: 	    if ($current_output=~/\\documentclass/) {
  633: 		$current_output =~ s/\\begin{document}/\\setlength{\\topmargin}{1cm}\\usepackage{fancyhdr}\\pagestyle{fancy}\\rhead{}\\chead{} \\begin{document}\\noindent\\lhead{\\textit{\\textbf{$fullname}} - $courseidinfo \\hfill \\thepage \\\\ \\textit{$assignment}}\\vskip 3 mm /;
  634: 	    } else {
  635: 		my $blanspages = '';
  636: 		for (my $j=0;$j<$ENV{'form.addedpages'};$j++) {$blanspages.='\clearpage\strut\clearpage';}
  637: 		$current_output = '\strut\\newline\\noindent\\makebox[\\textwidth/$number_of_columns][b]{\\hrulefill}\\newline\\noindent{\\tiny Printed from LON-CAPA\\copyright MSU{\\hfill} Licensed under GNU General Public License }\\newpage \\vskip 3mm'.$blanspages.'\setcounter{page}{1}\noindent\parbox{\minipagewidth}{\noindent\\lhead{\\textit{\\textbf{'.$fullname.'}} - '.$courseidinfo.' \\hfill \\thepage \\\\ \\textit{'.$assignment.'}}\hskip 1.4in } \vskip 5 mm '.$current_output;
  638: 	    }
  639: 	    $result .= $current_output;
  640: 	    &Apache::lonnet::delenv('form.counter');	    
  641: 	    &Apache::lonxml::init_counter(); 
  642: 	    &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
  643: 						    'last student '.$fullname);
  644: 	}
  645: 	&Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  646: 	$result .= '\end{document}';
  647: 	&Apache::lonnet::delenv('form.textwidth');
  648:     } elsif ($choice eq 'Subdirectory print') {      
  649:     #prints selected problems from the subdirectory 
  650: 	$selectionmade = 6;
  651:         my @list_of_files = ();
  652: 	for (my $i=0; $i<$ENV{'form.numberofproblems'};$i++) {
  653: 	    if ($ENV{'form.whattoprint'.$i}=~/^\//) {
  654: 		push @list_of_files,$ENV{'form.whattoprint'.$i};
  655: 	    }
  656: 	}	
  657: 	my $flag_latex_header_remove = 'NO';  
  658: 	for (my $i=0;$i<=$#list_of_files;$i++) {
  659: 	    my $urlp = $list_of_files[$i];
  660: 	    if ($urlp=~/\//) {
  661: 		my %moreenv;
  662: 		$moreenv{'form.grade_target'}='tex';
  663: 		$moreenv{'form.textwidth'}=$LaTeXwidth;
  664: 		&Apache::lonnet::appenv(%moreenv);
  665: 		if ($urlp =~ m|/home/([^/]+)/public_html|) {
  666: 		    $urlp =~ s|/home/([^/]*)/public_html|/~$1|;
  667: 		} else {
  668: 		    $urlp =~ s|^/home/httpd/html||;
  669: 		}
  670: 		my $texversion=&Apache::lonnet::ssi($urlp);
  671: 		&Apache::lonnet::delenv('form.grade_target','form.textwidth');
  672:                 #this chunck is responsible for printing the path to problem
  673: 		my $newurlp = '';
  674: 		my $HowMany = length($urlp)*2;
  675: 		if ($HowMany > $LaTeXwidth) {
  676: 		    my @temporrary = split '/',$urlp;
  677: 		    my $HowManyNew = 0;
  678: 		    for (my $ii=0;$ii<=$#temporrary;$ii++) {
  679: 			if ($temporrary[$ii] ne '') {
  680: 			    $HowManyNew += length($temporrary[$ii])*2;
  681: 			    if ($HowManyNew < $LaTeXwidth ) {
  682: 				$newurlp .=  '/'.$temporrary[$ii];
  683: 			    } else {
  684: 				$HowManyNew = 0;
  685: 				$newurlp .=  '|\vskip -1 mm \noindent \verb|';
  686: 				$ii--;
  687: 			    }
  688: 			}
  689: 		    }
  690: 		}
  691: 		$texversion =~ s/(\\begin{minipage}{\\textwidth})/$1 {\\small\\noindent\\verb|$newurlp\|\\vskip 0 mm}/;
  692: 		if ($flag_latex_header_remove ne 'NO') {
  693: 		    $texversion = &latex_header_footer_remove($texversion);
  694: 		} else {
  695: 		    $texversion =~ s/\\end{document}//;
  696: 		}
  697: 		$result .= $texversion;
  698: 	    }
  699: 	    $flag_latex_header_remove = 'YES';  
  700: 	}
  701: 	$result .= '\end{document}';      	
  702:     }
  703: #-------------------------------------------------------- corrections for the different page formats
  704:     $result = &page_format_transformation($papersize,$layout,$numberofcolumns,$choice,$result,$assignment);
  705:     if ($layout eq 'CBI') {
  706:         $laystyle = 'album';
  707:     }
  708:     $result = &latex_corrections($number_of_columns,$result);
  709:     #changes page's parameters for the one column output 
  710:     if ($ENV{'form.numberofcolumns'} == 1) {
  711: 	$result =~ s/\\textwidth= 9cm/\\textwidth= $ENV{'form.width'}/;
  712: 	$result =~ s/\\textheight 25\.9cm/\\textheight $ENV{'form.height'}/;
  713: 	$result =~ s/\\evensidemargin = -0\.57in/\\evensidemargin= $ENV{'form.leftmargin'}/;
  714: 	$result =~ s/\\oddsidemargin = -0\.57in/\\oddsidemargin= $ENV{'form.leftmargin'}/;
  715:     }
  716: #-- writing .tex file in prtspool 
  717:     my $temp_file;
  718:     my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout_".time."_".rand(10000000).".tex";
  719:     unless ($temp_file = Apache::File->new('>'.$filename)) {
  720: 	$r->log_error("Couldn't open $filename for output $!");
  721: 	return SERVER_ERROR; 
  722:     } 
  723:     print $temp_file $result;
  724: $r->print(<<FINALEND);
  725: <meta http-equiv="Refresh" content="0; url=/cgi-bin/printout.pl?$filename&$laystyle&$numberofcolumns&$selectionmade">
  726: </body>
  727: </html>
  728: FINALEND
  729: }
  730: 
  731: 
  732: sub coming_from_hash {
  733: 
  734:     my $mainsequence = shift;
  735:     my @resourcelist = ();
  736:     my $mapid = $hash{'map_pc_'.$mainsequence};
  737:     my $mapstart = $hash{'map_start_'.$mainsequence};
  738:     my $mapfinish = $hash{'map_finish_'.$mainsequence};
  739:     my $current_resource = $mapstart;
  740:     while ($current_resource ne $mapfinish) {
  741: 	if ($hash{'src_'.$current_resource}=~/\.sequence$/) {
  742: 	    push @resourcelist,&coming_from_hash($hash{'src_'.$current_resource});
  743: 	} else {
  744: 	    my $presymb=&Apache::lonnet::declutter($mainsequence);
  745: 	    my $rid = $current_resource;
  746: 	    if ($rid=~/,/) {
  747: 		my @rid = split /,/, $rid;
  748: 		foreach my $rid_element (@rid) {
  749: 		    if ($rid_element =~ m/^$mapid\.(\d*)/) {
  750: 			$rid = $1; 
  751: 			last;
  752: 		    }
  753: 		}
  754: 	    } else {
  755: 	        $rid =~ m/^$mapid\.(\d*)/;
  756:                 $rid = $1;  
  757: 	    }
  758: 	    my $symb = $presymb.'___'.$rid.'___'.
  759: 		&Apache::lonnet::declutter($hash{'src_'.$current_resource});
  760: 	    push @resourcelist,$hash{'src_'.$current_resource}.'&&'.$hash{'title_'.$current_resource}.'&&'.$symb;
  761: 	}
  762: 	$current_resource = $hash{'goesto_'.$hash{'to_'.$current_resource}};
  763:     } 
  764:     #needs if final resource in the map (type="finish") contains something
  765: 	if ($hash{'src_'.$current_resource}=~/\.sequence$/) {
  766: 	    push @resourcelist,&coming_from_hash($hash{'src_'.$current_resource});
  767: 	} else {
  768: 	    my $presymb=&Apache::lonnet::declutter($mainsequence);
  769: 	    my $rid = $current_resource;
  770: 	    if ($rid=~/,/) {
  771: 		my @rid = split /,/, $rid;
  772: 		foreach my $rid_element (@rid) {
  773: 		    if ($rid_element =~ m/^$mapid\.(\d*)/) {
  774: 			$rid = $1; 
  775: 			last;
  776: 		    }
  777: 		}
  778: 	    } else {
  779: 	        $rid =~ m/^$mapid\.(\d*)/;
  780:                 $rid = $1;  
  781: 	    }
  782: 	    my $symb = $presymb.'___'.$rid.'___'.
  783: 		&Apache::lonnet::declutter($hash{'src_'.$current_resource});
  784: 	    push @resourcelist,$hash{'src_'.$current_resource}.'&&'.$hash{'title_'.$current_resource}.'&&'.$symb;
  785: 	}
  786: 	$current_resource = $hash{'goesto_'.$hash{'to_'.$current_resource}};
  787:     return @resourcelist;
  788: }
  789: 
  790: 
  791: sub latex_header_footer_remove {
  792:     my $text = shift;
  793:     $text =~ s/\\end{document}//;
  794:     $text =~ s/\\documentclass([^&]*)\\begin{document}//;
  795:     return $text;
  796: }
  797: 
  798: 
  799: sub character_chart {
  800:     my $result = shift;	
  801:     $result =~ s/&\#0?0?(7|9);//g;
  802:     $result =~ s/&\#0?(10|13);//g;
  803:     $result =~ s/&\#0?32;/ /g;
  804:     $result =~ s/&\#0?33;/!/g;
  805:     $result =~ s/&(\#0?34|quot);/\"/g;
  806:     $result =~ s/&\#0?35;/\\\#/g;
  807:     $result =~ s/&\#0?36;/\\\$/g;
  808:     $result =~ s/&\#0?37;/\\%/g; 
  809:     $result =~ s/&(\#0?38|amp);/\\&/g; 
  810:     $result =~ s/&\#(0?39|146);/\'/g;
  811:     $result =~ s/&\#0?40;/(/g;
  812:     $result =~ s/&\#0?41;/)/g;
  813:     $result =~ s/&\#0?42;/\*/g;
  814:     $result =~ s/&\#0?43;/\+/g;
  815:     $result =~ s/&\#(0?44|130);/,/g;
  816:     $result =~ s/&\#0?45;/-/g;
  817:     $result =~ s/&\#0?46;/\./g;
  818:     $result =~ s/&\#0?47;/\//g;
  819:     $result =~ s/&\#0?48;/0/g;
  820:     $result =~ s/&\#0?49;/1/g;
  821:     $result =~ s/&\#0?50;/2/g;
  822:     $result =~ s/&\#0?51;/3/g;
  823:     $result =~ s/&\#0?52;/4/g;
  824:     $result =~ s/&\#0?53;/5/g;
  825:     $result =~ s/&\#0?54;/6/g;
  826:     $result =~ s/&\#0?55;/7/g;
  827:     $result =~ s/&\#0?56;/8/g;
  828:     $result =~ s/&\#0?57;/9/g;
  829:     $result =~ s/&\#0?58;/:/g;
  830:     $result =~ s/&\#0?59;/;/g;
  831:     $result =~ s/&(\#0?60|lt|\#139);/\$<\$/g;
  832:     $result =~ s/&\#0?61;/\$=\$/g;
  833:     $result =~ s/&(\#0?62|gt|\#155);/\$>\$/g;
  834:     $result =~ s/&\#0?63;/\?/g;
  835:     $result =~ s/&\#0?65;/A/g;
  836:     $result =~ s/&\#0?66;/B/g;
  837:     $result =~ s/&\#0?67;/C/g;
  838:     $result =~ s/&\#0?68;/D/g;
  839:     $result =~ s/&\#0?69;/E/g;
  840:     $result =~ s/&\#0?70;/F/g;
  841:     $result =~ s/&\#0?71;/G/g;
  842:     $result =~ s/&\#0?72;/H/g;
  843:     $result =~ s/&\#0?73;/I/g;
  844:     $result =~ s/&\#0?74;/J/g;
  845:     $result =~ s/&\#0?75;/K/g;
  846:     $result =~ s/&\#0?76;/L/g;
  847:     $result =~ s/&\#0?77;/M/g;
  848:     $result =~ s/&\#0?78;/N/g;
  849:     $result =~ s/&\#0?79;/O/g;
  850:     $result =~ s/&\#0?80;/P/g;
  851:     $result =~ s/&\#0?81;/Q/g;
  852:     $result =~ s/&\#0?82;/R/g;
  853:     $result =~ s/&\#0?83;/S/g;
  854:     $result =~ s/&\#0?84;/T/g;
  855:     $result =~ s/&\#0?85;/U/g;
  856:     $result =~ s/&\#0?86;/V/g;
  857:     $result =~ s/&\#0?87;/W/g;
  858:     $result =~ s/&\#0?88;/X/g;
  859:     $result =~ s/&\#0?89;/Y/g;
  860:     $result =~ s/&\#0?90;/Z/g;
  861:     $result =~ s/&\#0?91;/[/g;
  862:     $result =~ s/&\#0?92;/\$\\setminus\$/g;
  863:     $result =~ s/&\#0?93;/]/g;
  864:     $result =~ s/&\#(0?94|136);/\$\\wedge\$/g;
  865:     $result =~ s/&\#(0?95|138|154);/\\underline{\\makebox[2mm]{\\strut}}/g;
  866:     $result =~ s/&\#(0?96|145);/\`/g;
  867:     $result =~ s/&\#0?97;/a/g;
  868:     $result =~ s/&\#0?98;/b/g;
  869:     $result =~ s/&\#0?99;/c/g;
  870:     $result =~ s/&\#100;/d/g;
  871:     $result =~ s/&\#101;/e/g;
  872:     $result =~ s/&\#102;/f/g;
  873:     $result =~ s/&\#103;/g/g;
  874:     $result =~ s/&\#104;/h/g;
  875:     $result =~ s/&\#105;/i/g;
  876:     $result =~ s/&\#106;/j/g;
  877:     $result =~ s/&\#107;/k/g;
  878:     $result =~ s/&\#108;/l/g;
  879:     $result =~ s/&\#109;/m/g;
  880:     $result =~ s/&\#110;/n/g;
  881:     $result =~ s/&\#111;/o/g;
  882:     $result =~ s/&\#112;/p/g;
  883:     $result =~ s/&\#113;/q/g;
  884:     $result =~ s/&\#114;/r/g;
  885:     $result =~ s/&\#115;/s/g;
  886:     $result =~ s/&\#116;/t/g;
  887:     $result =~ s/&\#117;/u/g;
  888:     $result =~ s/&\#118;/v/g;
  889:     $result =~ s/&\#119;/w/g;
  890:     $result =~ s/&\#120;/x/g;
  891:     $result =~ s/&\#121;/y/g;
  892:     $result =~ s/&\#122;/z/g;
  893:     $result =~ s/&\#123;/\\{/g;
  894:     $result =~ s/&\#124;/\|/g;
  895:     $result =~ s/&\#125;/\\}/g;
  896:     $result =~ s/&\#126;/\~/g;
  897:     $result =~ s/&\#131;/\\textflorin /g;
  898:     $result =~ s/&\#132;/\"/g;
  899:     $result =~ s/&\#133;/\$\\ldots\$/g;
  900:     $result =~ s/&\#134;/\$\\dagger\$/g;
  901:     $result =~ s/&\#135;/\$\\ddagger\$/g;
  902:     $result =~ s/&\#137;/\\textperthousand /g;
  903:     $result =~ s/&\#140;/{\\OE}/g;
  904:     $result =~ s/&\#147;/\`\`/g;
  905:     $result =~ s/&\#148;/\'\'/g;
  906:     $result =~ s/&\#149;/\$\\bullet\$/g;
  907:     $result =~ s/&\#150;/--/g;
  908:     $result =~ s/&\#151;/---/g;
  909:     $result =~ s/&\#152;/\$\\sim\$/g;
  910:     $result =~ s/&\#153;/\\texttrademark /g;
  911:     $result =~ s/&\#156;/\\oe/g;
  912:     $result =~ s/&\#159;/\\\"Y/g;
  913:     $result =~ s/&(\#160|nbsp);//g;
  914:     $result =~ s/&(\#161|iexcl);/!\`/g;
  915:     $result =~ s/&(\#162|cent);/\\textcent /g;
  916:     $result =~ s/&(\#163|pound);/\\pounds /g; 
  917:     $result =~ s/&(\#164|curren);/\\textcurrency /g;
  918:     $result =~ s/&(\#165|yen);/\\textyen /g;
  919:     $result =~ s/&(\#166|brvbar);/\\textbrokenbar /g;
  920:     $result =~ s/&(\#167|sect);/\\textsection /g;
  921:     $result =~ s/&(\#168|uml);/\\texthighdieresis /g;
  922:     $result =~ s/&(\#169|copy);/\\copyright /g;
  923:     $result =~ s/&(\#170|ordf);/\\textordfeminine /g;
  924:     $result =~ s/&(\#172|not);/\$\\neg\$/g;
  925:     $result =~ s/&(\#173|shy);/ - /g;
  926:     $result =~ s/&(\#174|reg);/\\textregistered /g;
  927:     $result =~ s/&(\#175|macr);/\$^{-}\$/g;
  928:     $result =~ s/&(\#176|deg);/\$^{\\circ}\$/g;
  929:     $result =~ s/&(\#177|plusmn);/\$\\pm\$/g;
  930:     $result =~ s/&(\#178|sup2);/\$^2\$/g;
  931:     $result =~ s/&(\#179|sup3);/\$^3\$/g;
  932:     $result =~ s/&(\#180|acute);/\\textacute /g;
  933:     $result =~ s/&(\#181|micro);/\$\\mu\$/g;
  934:     $result =~ s/&(\#182|para);/\\P/g;
  935:     $result =~ s/&(\#183|middot);/\$\\cdot\$/g;
  936:     $result =~ s/&(\#184|cedil);/\\c{\\strut}/g;
  937:     $result =~ s/&(\#185|sup1);/\$^1\$/g;
  938:     $result =~ s/&(\#186|ordm);/\\textordmasculine /g;
  939:     $result =~ s/&(\#188|frac14);/\\textonequarter /g;
  940:     $result =~ s/&(\#189|frac12);/\\textonehalf /g;
  941:     $result =~ s/&(\#190|frac34);/\\textthreequarters /g;
  942:     $result =~ s/&(\#191|iquest);/?\`/g;   
  943:     $result =~ s/&(\#192|Agrave);/\\\`{A}/g;  
  944:     $result =~ s/&(\#193|Aacute);/\\\'{A}/g; 
  945:     $result =~ s/&(\#194|Acirc);/\\^{A}/g;
  946:     $result =~ s/&(\#195|Atilde);/\\~{A}/g;
  947:     $result =~ s/&(\#196|Auml);/\\\"{A}/g; 
  948:     $result =~ s/&(\#197|Aring);/{\\AA}/g;
  949:     $result =~ s/&(\#198|AElig);/{\\AE}/g;
  950:     $result =~ s/&(\#199|Ccedil);/\\c{c}/g;
  951:     $result =~ s/&(\#200|Egrave);/\\\`{E}/g;  
  952:     $result =~ s/&(\#201|Eacute);/\\\'{E}/g;    
  953:     $result =~ s/&(\#202|Ecirc);/\\^{E}/g;
  954:     $result =~ s/&(\#203|Euml);/\\\"{E}/g;
  955:     $result =~ s/&(\#204|Igrave);/\\\`{I}/g;
  956:     $result =~ s/&(\#205|Iacute);/\\\'{I}/g;    
  957:     $result =~ s/&(\#206|Icirc);/\\^{I}/g;
  958:     $result =~ s/&(\#207|Iuml);/\\\"{I}/g;    
  959:     $result =~ s/&(\#209|Ntilde);/\\~{N}/g;
  960:     $result =~ s/&(\#210|Ograve);/\\\`{O}/g;
  961:     $result =~ s/&(\#211|Oacute);/\\\'{O}/g;
  962:     $result =~ s/&(\#212|Ocirc);/\\^{O}/g;
  963:     $result =~ s/&(\#213|Otilde);/\\~{O}/g;
  964:     $result =~ s/&(\#214|Ouml);/\\\"{O}/g;    
  965:     $result =~ s/&(\#215|times);/\$\\times\$/g;
  966:     $result =~ s/&(\#216|Oslash);/{\\O}/g;
  967:     $result =~ s/&(\#217|Ugrave);/\\\`{U}/g;    
  968:     $result =~ s/&(\#218|Uacute);/\\\'{U}/g;
  969:     $result =~ s/&(\#219|Ucirc);/\\^{U}/g;
  970:     $result =~ s/&(\#220|Uuml);/\\\"{U}/g;
  971:     $result =~ s/&(\#221|Yacute);/\\\'{Y}/g;
  972:     $result =~ s/&(\#223|szlig);/\\ss/g;
  973:     $result =~ s/&(\#224|agrave);/\\\`{a}/g;
  974:     $result =~ s/&(\#225|aacute);/\\\'{a}/g;
  975:     $result =~ s/&(\#226|acirc);/\\^{a}/g;
  976:     $result =~ s/&(\#227|atilde);/\\~{a}/g;
  977:     $result =~ s/&(\#228|auml);/\\\"{a}/g;
  978:     $result =~ s/&(\#229|aring);/{\\aa}/g;
  979:     $result =~ s/&(\#230|aelig);/{\\ae}/g;
  980:     $result =~ s/&(\#231|ccedil);/\\c{c}/g;
  981:     $result =~ s/&(\#232|egrave);/\\\`{e}/g;
  982:     $result =~ s/&(\#233|eacute);/\\\'{e}/g;
  983:     $result =~ s/&(\#234|ecirc);/\\^{e}/g;
  984:     $result =~ s/&(\#235|euml);/\\\"{e}/g;
  985:     $result =~ s/&(\#236|igrave);/\\\`{i}/g;
  986:     $result =~ s/&(\#237|iacute);/\\\'{i}/g;
  987:     $result =~ s/&(\#238|icirc);/\\^{i}/g;
  988:     $result =~ s/&(\#239|iuml);/\\\"{i}/g;
  989:     $result =~ s/&(\#240|eth);/\$\\partial\$/g;
  990:     $result =~ s/&(\#241|ntilde);/\\~{n}/g;
  991:     $result =~ s/&(\#242|ograve);/\\\`{o}/g;
  992:     $result =~ s/&(\#243|oacute);/\\\'{o}/g;
  993:     $result =~ s/&(\#244|ocirc);/\\^{o}/g;
  994:     $result =~ s/&(\#245|otilde);/\\~{o}/g;
  995:     $result =~ s/&(\#246|ouml);/\\\"{o}/g;
  996:     $result =~ s/&(\#247|divide);/\$\\div\$/g;
  997:     $result =~ s/&(\#248|oslash);/{\\o}/g;
  998:     $result =~ s/&(\#249|ugrave);/\\\`{u}/g; 
  999:     $result =~ s/&(\#250|uacute);/\\\'{u}/g;
 1000:     $result =~ s/&(\#251|ucirc);/\\^{u}/g;
 1001:     $result =~ s/&(\#252|uuml);/\\\"{u}/g;
 1002:     $result =~ s/&(\#253|yacute);/\\\'{y}/g;
 1003:     $result =~ s/&(\#255|yuml);/\\\"{y}/g;
 1004:     $result =~ s/&\#952;/\$\\theta\$/g;
 1005: #Greek Alphabet
 1006:     $result =~ s/&(alpha|\#945);/\$\\alpha \$/g;
 1007:     $result =~ s/&(beta|\#946);/\$\\beta \$/g;
 1008:     $result =~ s/&(gamma|\#947);/\$\\gamma \$/g;
 1009:     $result =~ s/&(delta|\#948);/\$\\delta \$/g;
 1010:     $result =~ s/&(epsilon|\#949);/\$\\epsilon \$/g;
 1011:     $result =~ s/&(zeta|\#950);/\$\\zeta \$/g;
 1012:     $result =~ s/&(eta|\#951);/\$\\eta \$/g;
 1013:     $result =~ s/&(theta|\#952);/\$\\theta \$/g;
 1014:     $result =~ s/&(iota|\#953);/\$\\iota \$/g;
 1015:     $result =~ s/&(kappa|\#954);/\$\\kappa \$/g;
 1016:     $result =~ s/&(lambda|\#955);/\$\\lambda \$/g;
 1017:     $result =~ s/&(mu|\#956);/\$\\mu \$/g;
 1018:     $result =~ s/&(nu|\#957);/\$\\nu \$/g;
 1019:     $result =~ s/&(xi|\#958);/\$\\xi \$/g;
 1020:     $result =~ s/&(pi|\#960);/\$\\pi \$/g;
 1021:     $result =~ s/&(rho|\#961);/\$\\rho \$/g;
 1022:     $result =~ s/&(sigma|\#963);/\$\\sigma \$/g;
 1023:     $result =~ s/&(tau|\#964);/\$\\tau \$/g;
 1024:     $result =~ s/&(upsilon|\#965);/\$\\upsilon \$/g;
 1025:     $result =~ s/&(phi|\#966);/\$\\phi \$/g;
 1026:     $result =~ s/&(chi|\#967);/\$\\chi \$/g;
 1027:     $result =~ s/&(psi|\#968);/\$\\psi \$/g;
 1028:     $result =~ s/&(omega|\#969);/\$\\omega \$/g;
 1029:     $result =~ s/&(Gamma|\#915);/\$\\Gamma \$/g;
 1030:     $result =~ s/&(Delta|\#916);/\$\\Delta \$/g;
 1031:     $result =~ s/&(Theta|\#920);/\$\\Theta \$/g;
 1032:     $result =~ s/&(Lambda|\#923);/\$\\Lambda \$/g;
 1033:     $result =~ s/&(Xi|\#926);/\$\\Xi \$/g;
 1034:     $result =~ s/&(Pi|\#928);/\$\\Pi \$/g;
 1035:     $result =~ s/&(Sigma|\#931);/\$\\Sigma \$/g;
 1036:     $result =~ s/&(Upsilon|\#933);/\$\\Upsilon \$/g;
 1037:     $result =~ s/&(Phi|\#934);/\$\\Phi \$/g;
 1038:     $result =~ s/&(Psi|\#936);/\$\\Psi \$/g;
 1039:     $result =~ s/&(Omega|\#937);/\$\\Omega \$/g;
 1040: 
 1041:     return $result;
 1042: }
 1043: 
 1044: 
 1045:     sub page_format {
 1046: 	my ($papersize,$layout,$numberofcolumns) = @_; 
 1047: 	my ($textwidth,$textheight,$oddoffset,$evenoffset) = (0,0,0,0);
 1048: 	if ($papersize=~/Letter/) {
 1049: 	    if ($layout eq 'CAPA') {
 1050: 		if ($numberofcolumns == 1) {
 1051: 		    $textwidth = '18 cm';
 1052: 		    $textheight = '25.9 cm';
 1053: 		    $oddoffset = '-0.57 in';
 1054: 		    $evenoffset = '-0.57 in';
 1055: 		} elsif ($numberofcolumns == 2) {
 1056: 		    $textwidth = '93 mm';
 1057: 		    $textheight = '25.9 cm';
 1058: 		    $oddoffset = '-0.57 in';
 1059: 		    $evenoffset = '-0.57 in';
 1060: 		}
 1061: 	    } elsif ($layout eq 'CBI') {
 1062: 		if ($numberofcolumns eq '1') {
 1063: 		    $textwidth = '8.8 in';
 1064: 		    $textheight = '6.8 in';
 1065: 		    $oddoffset = '-40 pt';
 1066: 		    $evenoffset = '-60 pt';
 1067: 		} elsif ($numberofcolumns == 2) {
 1068: 		    $textwidth = '4.2 in';
 1069: 		    $textheight = '6.8 in';
 1070: 		    $oddoffset = '-40 pt';
 1071: 		    $evenoffset = '-60 pt';
 1072: 		}
 1073: 	    }
 1074: #    } elsif($papersize=~/Legal/) {
 1075: #    } elsif($papersize=~/Ledger/) {
 1076: #    } elsif($papersize=~/Executive/) {
 1077:     } elsif($papersize=~/A4/) {
 1078: 	    if ($layout eq 'CAPA') {
 1079: 		if ($numberofcolumns == 1) {
 1080: 		    $textwidth = '18 cm';
 1081: 		    $textheight = '28 cm';
 1082: 		    $oddoffset = '-0.57 in';
 1083: 		    $evenoffset = '-0.57 in';
 1084: 		} elsif ($numberofcolumns == 2) {
 1085: 		    $textwidth = '96 mm';
 1086: 		    $textheight = '2 cm';
 1087: 		    $oddoffset = '-0.57 in';
 1088: 		    $evenoffset = '-0.57 in';
 1089: 		}
 1090: 	    } elsif ($layout eq 'CBI') {
 1091: 		if ($numberofcolumns eq '1') {
 1092: 		    $textwidth = '8.5 in';
 1093: 		    $textheight = '7.7 in';
 1094: 		    $oddoffset = '-40 pt';
 1095: 		    $evenoffset = '-60 pt';
 1096: 		} elsif ($numberofcolumns == 2) {
 1097: 		    $textwidth = '3.9 in';
 1098: 		    $textheight = '7.7 in';
 1099: 		    $oddoffset = '-40 pt';
 1100: 		    $evenoffset = '-60 pt';
 1101: 		}
 1102: 	    }
 1103: #    } elsif($papersize=~/A3/) {
 1104: #    } elsif($papersize=~/A2/) {
 1105: #    } elsif($papersize=~/A5/) {
 1106: #    } elsif($papersize=~/A6/) {
 1107: 	}
 1108: 	return $textwidth,$textheight,$oddoffset,$evenoffset;
 1109: }
 1110: 
 1111: 
 1112: sub page_format_new {
 1113: #
 1114: #Correspondence between $papersize variable and real paper format:
 1115: # 0 - "Letter [8 1/2x11 in]"
 1116: # 1 - "Legal [8 1/2x14 in]"
 1117: # 2 - "Ledger/Tabloid [11x17 in]"
 1118: # 3 - "Executive [7 1/2x10 in]"
 1119: # 4 - "A2 [420x594 mm]"
 1120: # 5 - "A3 [297x420 mm]"
 1121: # 6 - "A4 [210x297 mm]"
 1122: # 7 - "A5 [148x210 mm]"
 1123: # 8 - "A6 [105x148 mm]"
 1124: # 
 1125:     my ($papersize,$layout,$numberofcolumns) = @_; 
 1126:     my ($textwidth,$textheight,$oddoffset,$evenoffset) = (0,0,0,0);
 1127:     if ($papersize eq '0') {
 1128: 	if ($layout eq 'book') {
 1129: 	    if ($numberofcolumns == 1) {
 1130: 		$textwidth = '18 cm';
 1131: 		$textheight = '25.9 cm';
 1132: 		$oddoffset = '-0.57 in';
 1133: 		$evenoffset = '-0.57 in';
 1134: 	    } elsif ($numberofcolumns == 2) {
 1135: 		$textwidth = '93 mm';
 1136: 		$textheight = '25.9 cm';
 1137: 		$oddoffset = '-0.57 in';
 1138: 		$evenoffset = '-0.57 in';
 1139: 	    }
 1140: 	} elsif ($layout eq 'album') {
 1141: 	    if ($numberofcolumns eq '1') {
 1142: 		$textwidth = '8.8 in';
 1143: 		$textheight = '6.8 in';
 1144: 		$oddoffset = '-40 pt';
 1145: 		$evenoffset = '-60 pt';
 1146: 	    } elsif ($numberofcolumns == 2) {
 1147: 		$textwidth = '4.2 in';
 1148: 		$textheight = '6.8 in';
 1149: 		$oddoffset = '-40 pt';
 1150: 		$evenoffset = '-60 pt';
 1151: 	    }
 1152: 	}
 1153: #    } elsif($papersize eq '1') {
 1154: #    } elsif($papersize eq '2') {
 1155: #    } elsif($papersize eq '3'/) {
 1156:     } elsif($papersize eq '6') {
 1157: 	if ($layout eq 'book') {
 1158: 	    if ($numberofcolumns == 1) {
 1159: 		$textwidth = '18 cm';
 1160: 		$textheight = '28 cm';
 1161: 		$oddoffset = '-0.57 in';
 1162: 		$evenoffset = '-0.57 in';
 1163: 	    } elsif ($numberofcolumns == 2) {
 1164: 		$textwidth = '96 mm';
 1165: 		$textheight = '2 cm';
 1166: 		$oddoffset = '-0.57 in';
 1167: 		$evenoffset = '-0.57 in';
 1168: 	    }
 1169: 	} elsif ($layout eq 'album') {
 1170: 	    if ($numberofcolumns eq '1') {
 1171: 		$textwidth = '8.5 in';
 1172: 		$textheight = '7.7 in';
 1173: 		$oddoffset = '-40 pt';
 1174: 		$evenoffset = '-60 pt';
 1175: 	    } elsif ($numberofcolumns == 2) {
 1176: 		$textwidth = '3.9 in';
 1177: 		$textheight = '7.7 in';
 1178: 		$oddoffset = '-40 pt';
 1179: 		$evenoffset = '-60 pt';
 1180: 	    }
 1181: 	}
 1182: #    } elsif($papersize eq '5') {
 1183: #    } elsif($papersize eq '4') {
 1184: #    } elsif($papersize eq '7') {
 1185: #    } elsif($papersize eq '8') {
 1186:     }
 1187:     return $textwidth,$textheight,$oddoffset,$evenoffset;
 1188: }
 1189: 
 1190: 
 1191: sub get_name {
 1192:     my ($uname,$udom)=@_;
 1193:     if (!defined($uname)) { $uname=$ENV{'user.name'}; }
 1194:     if (!defined($udom)) { $uname=$ENV{'user.domain'}; }
 1195:     my $plainname=&Apache::loncommon::plainname($uname,$udom);
 1196:     if ($plainname=~/^\s*$/) { $plainname=$uname; }
 1197:     return &Apache::lonxml::latex_special_symbols($plainname,undef,undef,
 1198: 						  'header');
 1199: }
 1200: 
 1201: sub page_format_transformation {
 1202:     my ($papersize,$layout,$numberofcolumns,$choice,$text,$assignment) = @_; 
 1203:     my ($textwidth,$textheight,$oddoffset,$evenoffset);
 1204: 	$assignment=~s/_/ /g;
 1205:         if ($numberofcolumns != 1) {
 1206:           ($textwidth,$textheight,$oddoffset,$evenoffset) = &page_format($papersize,$layout,$numberofcolumns);
 1207: 	} else {
 1208: 	  ($textwidth,$textheight,$oddoffset,$evenoffset) = ($ENV{'form.width'},$ENV{'form.height'},$ENV{'form.leftmargin'},$ENV{'form.leftmargin'});
 1209: 	}
 1210:     my $name = &get_name();
 1211:     if ($name =~ /^\s*$/) {
 1212: 	$name=&Apache::lonxml::latex_special_symbols($ENV{'user.name'},undef,
 1213: 						     undef,'header');
 1214:     }
 1215:     my $courseidinfo = &Apache::lonxml::latex_special_symbols(&Apache::lonnet::unescape($ENV{'course.'.$ENV{'request.course.id'}.'.description'}),'','','header');
 1216:     if ($layout eq 'CBI') {
 1217: 	    $text =~ s/\\begin{document}/\\setlength{\\oddsidemargin}{$oddoffset}\\setlength{\\evensidemargin}{$evenoffset}\\setlength{\\topmargin}{200pt}\\setlength{\\textwidth}{$textwidth}\\setlength{\\textheight}{$textheight}\\setlength{\\parindent}{20pt}\\setlength{\\marginparwidth}{90pt}\\setlength{\\textfloatsep}{8pt plus 2\.0pt minus 4\.0pt}\\newlength{\\minipagewidth}\\setlength{\\minipagewidth}{\\textwidth\/\$number_of_columns-0\.2cm} \\begin{document}\\setcounter{page}{1}\\noindent\\parbox{\\minipagewidth}{\\noindent\\fbox{\\textbf{$name}} \\hfill  $courseidinfo} \\vskip 5 mm /;
 1218:     } elsif ($layout eq 'CAPA') {
 1219: 	if ($choice ne 'All class print') { 
 1220: 		$text =~ s/\\begin{document}/\\textheight $textheight\\oddsidemargin = $evenoffset\\evensidemargin = $evenoffset\\textwidth= $textwidth\\newlength{\\minipagewidth}\\setlength{\\minipagewidth}{\\textwidth\/\$number_of_columns-0\.2cm}\\renewcommand{\\ref}{\\keephidden\}\\usepackage{fancyhdr}\\pagestyle{fancy}\\rhead{}\\chead{}\\lhead{\\textbf{$name} - $courseidinfo \\hfill \\thepage \\\\ \\textit{$assignment}}\\begin{document}\\voffset=-0\.8 cm\\setcounter{page}{1}/;
 1221: 	} else {
 1222: 	    $text =~ s/\\pagestyle{fancy}\\rhead{}\\chead{}\s*\\begin{document}/\\textheight $textheight\\oddsidemargin = $evenoffset\\evensidemargin = $evenoffset\\textwidth= $textwidth\\newlength{\\minipagewidth}\\setlength{\\minipagewidth}{\\textwidth\/\$number_of_columns-0\.2cm}\\renewcommand{\\ref}{\\keephidden\}\\pagestyle{fancy}\\rhead{}\\chead{}\\begin{document}\\voffset=-0\.8cm\\setcounter{page}{1}  \\vskip 5 mm /;
 1223: 
 1224: 	}
 1225:     }
 1226:     return $text;
 1227: }
 1228: 
 1229: 
 1230: sub page_format_transformation_new {
 1231:     my ($papersize,$layout,$numberofcolumns,$choice,$text,$assignment) = @_; 
 1232:     my ($textwidth,$textheight,$oddoffset,$evenoffset);
 1233: 	$assignment=~s/_/ /g;
 1234:         if ($numberofcolumns != 1) {
 1235:           ($textwidth,$textheight,$oddoffset,$evenoffset) = &page_format_new($papersize,$layout,$numberofcolumns);
 1236: 	} else {
 1237: 	  ($textwidth,$textheight,$oddoffset,$evenoffset) = ($ENV{'form.width'},$ENV{'form.height'},$ENV{'form.leftmargin'},$ENV{'form.leftmargin'});
 1238: 	}
 1239:     my $name = &get_name();
 1240:     if ($name =~ /^\s*$/) {
 1241: 	$name=&Apache::lonxml::latex_special_symbols($ENV{'user.name'},undef,
 1242: 						     undef,'header');
 1243:     }
 1244:     my $courseidinfo = &Apache::lonxml::latex_special_symbols(&Apache::lonnet::unescape($ENV{'course.'.$ENV{'request.course.id'}.'.description'}),'','','header');
 1245:     if ($layout eq 'album') {
 1246: 	    $text =~ s/\\begin{document}/\\setlength{\\oddsidemargin}{$oddoffset}\\setlength{\\evensidemargin}{$evenoffset}\\setlength{\\topmargin}{200pt}\\setlength{\\textwidth}{$textwidth}\\setlength{\\textheight}{$textheight}\\setlength{\\parindent}{20pt}\\setlength{\\marginparwidth}{90pt}\\setlength{\\textfloatsep}{8pt plus 2\.0pt minus 4\.0pt}\\newlength{\\minipagewidth}\\setlength{\\minipagewidth}{\\textwidth\/\$number_of_columns-0\.2cm} \\begin{document}\\setcounter{page}{1}\\noindent\\parbox{\\minipagewidth}{\\noindent\\fbox{\\textbf{$name}} \\hfill  $courseidinfo} \\vskip 5 mm /;
 1247:     } elsif ($layout eq 'book') {
 1248: 	if ($choice ne 'All class print') { 
 1249: 		$text =~ s/\\begin{document}/\\textheight $textheight\\oddsidemargin = $evenoffset\\evensidemargin = $evenoffset\\textwidth= $textwidth\\newlength{\\minipagewidth}\\setlength{\\minipagewidth}{\\textwidth\/\$number_of_columns-0\.2cm}\\renewcommand{\\ref}{\\keephidden\}\\usepackage{fancyhdr}\\pagestyle{fancy}\\rhead{}\\chead{}\\lhead{\\textbf{$name} - $courseidinfo \\hfill \\thepage \\\\ \\textit{$assignment}}\\begin{document}\\voffset=-0\.8 cm\\setcounter{page}{1}/;
 1250: 	} else {
 1251: 	    $text =~ s/\\pagestyle{fancy}\\rhead{}\\chead{}\s*\\begin{document}/\\textheight $textheight\\oddsidemargin = $evenoffset\\evensidemargin = $evenoffset\\textwidth= $textwidth\\newlength{\\minipagewidth}\\setlength{\\minipagewidth}{\\textwidth\/\$number_of_columns-0\.2cm}\\renewcommand{\\ref}{\\keephidden\}\\pagestyle{fancy}\\rhead{}\\chead{}\\begin{document}\\voffset=-0\.8cm\\setcounter{page}{1}  \\vskip 5 mm /;
 1252: 
 1253: 	}
 1254:     }
 1255:     return $text;
 1256: }
 1257: 
 1258: 
 1259: 
 1260: sub page_cleanup {
 1261:     my $result = shift;	
 1262:  
 1263:     $result =~ m/\\end{document}(\d*)$/;
 1264:     my $number_of_columns = $1;
 1265:     my $insert = '{';
 1266:     for (my $id=1;$id<=$number_of_columns;$id++) { $insert .='l'; }
 1267:     $insert .= '}';
 1268:     $result =~ s/(\\begin{longtable})INSERTTHEHEADOFLONGTABLE\\endfirsthead\\endhead/$1$insert/g;
 1269:     $result =~ s/&\s*REMOVETHEHEADOFLONGTABLE\\\\/\\\\/g;
 1270:     return $result,$number_of_columns;
 1271: }
 1272: 
 1273: 
 1274: sub details_for_menu {
 1275: 
 1276:     my $name_of_resourse = $hash{'title_'.$hash{'ids_'.$ENV{'form.postdata'}}};
 1277:     my $symbolic = &Apache::lonnet::symbread($ENV{'form.postdata'});
 1278:     my ($map,$id,$resource)=split(/___/,$symbolic);
 1279:     $map=&Apache::lonnet::clutter($map);
 1280:     my $name_of_sequence;
 1281:     $name_of_sequence = $hash{'title_'.$hash{'ids_'.$map}};
 1282:     if ($name_of_sequence =~ /^\s*$/) {
 1283: 	$map =~ m|([^/]+)$|;
 1284: 	$name_of_sequence = $1;
 1285:     }
 1286:     my $name_of_map = $hash{'title_'.$hash{'ids_'.&Apache::lonnet::clutter($ENV{'request.course.uri'})}};
 1287:     if ($name_of_map =~ /^\s*$/) {
 1288: 	$ENV{'request.course.uri'} =~ m|([^/]+)$|;
 1289: 	$name_of_map = $1;
 1290:     }
 1291:     return ($name_of_resourse,$name_of_sequence,$name_of_map);
 1292: 
 1293: }
 1294: 
 1295: 
 1296: sub latex_corrections {
 1297: 
 1298:     my ($number_of_columns,$result) = @_;
 1299: 
 1300:     $result =~ s/\\includegraphics{/\\includegraphics\[width=\\minipagewidth\]{/g;
 1301:     $result =~ s/\$number_of_columns/$number_of_columns/g;
 1302:     $result =~ s/(\\end{document})/\\strut\\newline\\noindent\\makebox\[\\textwidth\/$number_of_columns\]\[b\]{\\hrulefill}\\newline\\noindent\\tiny Printed from LON-CAPA\\copyright MSU{\\hfill} Licensed under GNU General Public License  $1/;
 1303:     $result =~ s/(\\end{longtable}\s*)(\\strut\\newline\\noindent\\makebox\[\\textwidth\/$number_of_columns\]\[b\]{\\hrulefill})/$2$1/g;
 1304:     $result =~ s/(\\end{longtable}\s*)\\strut\\newline/$1/g;
 1305: #-- LaTeX corrections     
 1306:     my $first_comment = index($result,'<!--',0);
 1307:     while ($first_comment != -1) {
 1308: 	my $end_comment = index($result,'-->',$first_comment);
 1309: 	substr($result,$first_comment,$end_comment-$first_comment+3) = '';
 1310: 	$first_comment = index($result,'<!--',$first_comment);
 1311:     }
 1312:     $result =~ s/^\s+$//gm; #remove empty lines
 1313:     $result =~ s/(\s)(\s+)/$1/g; #removes more than one empty space
 1314:     $result =~ s/\\\\\s*\\vskip/\\vskip/gm;
 1315:     $result =~ s/\\\\\s*\\noindent\s*(\\\\)+/\\\\\\noindent /g;
 1316:     $result =~ s/{\\par }\s*\\\\/\\\\/gm;
 1317: 	$result =~ s/\\\\\s+\[/ \[/g;
 1318:     $result =~ s/\b__+\b/\\makebox\[1 cm\]\[b\]{\\hrulefill}/g;
 1319:     #conversion of html characters to LaTeX equivalents
 1320:     if ($result =~ m/&(\w+|#\d+);/) {
 1321: 	$result = &character_chart($result);
 1322:     }
 1323:     $result =~ s/(\\end{tabular})\s*\\vskip 0 mm/$1/g;
 1324:     $result =~ s/(\\begin{enumerate})\s*\\noindent/$1/g;
 1325: 
 1326:     return $result;
 1327: }
 1328: 
 1329: 
 1330: sub output_data_new {
 1331:     my ($r,$helper) = @_;
 1332:     $r->print(<<ENDPART);
 1333: <html>
 1334: <head>
 1335: <title>LON-CAPA output for printing</title>
 1336: </head>
 1337: <body bgcolor="FFFFFF">
 1338: <hr>
 1339: ENDPART
 1340: 
 1341:     my $format_from_helper = $helper->{'VARS'}->{'FORMAT'};
 1342:     my ($result,$selectionmade) = ('','');
 1343:     my $number_of_columns = 1; #used only for pages to determine the width of the cell
 1344:     my @temporary_array=split /\|/,$format_from_helper;
 1345:     my ($laystyle,$numberofcolumns,$papersize)=@temporary_array;
 1346:     if ($laystyle eq 'L') {
 1347: 	$laystyle='album';
 1348:     } else {
 1349: 	$laystyle='book';
 1350:     }
 1351:     my ($textwidth,$textheight,$oddoffset,$evenoffset) = &page_format_new($papersize,$laystyle,$numberofcolumns);
 1352: 
 1353: 
 1354: 
 1355:    
 1356: #    my $choice = $ENV{'form.choice'};
 1357: #    my $layout = $ENV{'form.layout'};
 1358: #    my $numberofcolumns = $ENV{'form.numberofcolumns'};  
 1359: #    my $papersize = $ENV{'form.papersize'};          
 1360: #    my $laystyle = 'book';
 1361:     my $assignment =  $ENV{'form.assignment'};
 1362: #    if ($choice eq 'Subdirectory print') {
 1363: #	if ($ENV{'form.layout'} eq '') {$layout = 'CAPA';}
 1364: #	if ($ENV{'form.papersize'} eq '') {$papersize = 'Letter [8 1/2x11 in]';}
 1365: #	if ($ENV{'form.numberofcolumns'} eq '') {$numberofcolumns = 2;}
 1366: #    }
 1367: #    if (($choice eq 'Standard LaTeX output for current document') &&
 1368: #	($ENV{'form.url'}=~m|^/~|)) {
 1369: #	if ($ENV{'form.layout'} eq '') {$layout = 'CAPA';}
 1370: #	if ($ENV{'form.papersize'} eq '') {$papersize = 'Letter [8 1/2x11 in]';}
 1371: #	if ($ENV{'form.numberofcolumns'} eq '') {$numberofcolumns = 2;}
 1372: #    }
 1373: 
 1374:     
 1375: #    my ($textwidth,$textheight,$oddoffset,$evenoffset) = &page_format($papersize,$layout,$numberofcolumns);
 1376: #    if ($numberofcolumns == 1) {
 1377: #	$textwidth=$ENV{'form.width'};
 1378: #	$textheight=$ENV{'form.height'};
 1379: #    }
 1380:     my $LaTeXwidth; 
 1381:     if ($textwidth=~/(\d+)\s*cm/) {
 1382: 	$LaTeXwidth = $1*10;
 1383:     } elsif ($textwidth=~/(\d+)\s*mm/) {
 1384: 	$LaTeXwidth = $1;
 1385:     } elsif ($textwidth=~/(\d+)\s*in/) {
 1386: 	$LaTeXwidth = $1*25.4;
 1387:     }
 1388:     $LaTeXwidth.=' mm';
 1389: 
 1390: 
 1391:     if ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'current_document') {
 1392:       #-- single document - problem, page, html, xml, ...  
 1393: 	$selectionmade = 1;
 1394: 	if ($helper->{'VARS'}->{'url'}=~/\.(problem|exam|quiz|assess|survey|form|library|page|xml|html|htm|xhtml|xhtm)$/) {
 1395:         my %moreenv;
 1396:         $moreenv{'form.grade_target'}='tex';
 1397:         if (&Apache::lonnet::allowed('bre',$helper->{'VARS'}->{'url'})) {
 1398: 	  $helper->{'VARS'}->{'url'}=~s/http:\/\/[^\/]+//;
 1399:         }
 1400:         $moreenv{'request.filename'}=$helper->{'VARS'}->{'url'};
 1401:         $moreenv{'form.textwidth'}=$LaTeXwidth;
 1402:         &Apache::lonnet::appenv(%moreenv);
 1403:         my $texversion=&Apache::lonnet::ssi($helper->{'VARS'}->{'url'});
 1404:         &Apache::lonnet::delenv('form.grade_target','form.textwidth','form.counter');
 1405:         $result .= $texversion;
 1406:         if ($helper->{'VARS'}->{'url'}=~m/\.page\s*$/) {
 1407: 	  ($result,$number_of_columns) = &page_cleanup($result);
 1408:         }
 1409:       } else {
 1410: #	  $result='\documentclass[letterpaper]{article}\usepackage{calc}\begin{document}Printout of this type of document is currently not supported: ';
 1411: #	  if ($ENV{'form.url'}=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) {
 1412: #	      $result.=$1;
 1413: #	  } else {
 1414: #	      $result.=$ENV{'form.url'};
 1415: #	  }
 1416: #	  $result.=' \end{document}'
 1417:       }
 1418: 
 1419: 
 1420:     } elsif (($helper->{'VARS'}->{'PRINT_TYPE'} eq 'map_problems') or
 1421:              ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'map_problems_pages') or
 1422:              ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'all_problems')) { 
 1423:         #-- produce an output string
 1424: 	my $flag_latex_header_remove = 'NO';
 1425: 	my $flag_page_in_sequence = 'NO';
 1426: 	my @master_seq=split /\|\|\|/, $helper->{'VARS'}->{'RESOURCES'};
 1427: 
 1428: #	foreach  my $key (@master_seq) {
 1429: #	    $r->print(' '.$key.'<br />');
 1430: #	}
 1431: #	return OK;
 1432: 
 1433: 	for (my $i=0;$i<=$#master_seq;$i++) {
 1434: 	    $master_seq[$i]=~/___\d+___(.*)$/;
 1435: 	    my $urlp='/res/'.$1;
 1436: 	    if ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'map_problems') {
 1437: 		$selectionmade = 2;
 1438: 	    } elsif ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'map_problems_pages') {
 1439: 		$selectionmade = 3;
 1440: 	    } elsif ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'all_problems') {
 1441: 		$selectionmade = 4;
 1442: 	    }
 1443: 	    my %moreenv;
 1444: 	    $moreenv{'form.grade_target'}='tex';
 1445: 	    $moreenv{'form.textwidth'}=$LaTeXwidth;
 1446: 	    &Apache::lonnet::appenv(%moreenv);
 1447: 	    #&Apache::lonnet::logthis("Trying to get $urlp with symb $master_seq[$i]");
 1448: 	    my $texversion=&Apache::lonnet::ssi($urlp,('symb'=>$master_seq[$i]));
 1449: 	    &Apache::lonnet::delenv('form.grade_target','form.textwidth');
 1450: 	    if ($urlp=~/\.page$/) {
 1451: 		($texversion,my $number_of_columns_page) = &page_cleanup($texversion);
 1452: 		if ($number_of_columns_page > $number_of_columns) {$number_of_columns=$number_of_columns_page;} 
 1453: 		$texversion =~ s/\\end{document}\d*/\\end{document}/;
 1454: 		$flag_page_in_sequence = 'YES';
 1455: 	    } 
 1456: 	    if ($flag_latex_header_remove ne 'NO') {
 1457: 		$texversion = &latex_header_footer_remove($texversion);
 1458: 	    } else {
 1459: 		$texversion =~ s/\\end{document}//;
 1460: 	    }
 1461: 	    $result .= $texversion;         
 1462: 	    $flag_latex_header_remove = 'YES';   
 1463: 	}
 1464: 	&Apache::lonnet::delenv('form.counter');
 1465: 	if ($flag_page_in_sequence eq 'YES') {$result =~ s/\\usepackage{calc}/\\usepackage{calc}\\usepackage{longtable}/;}	
 1466: 	$result .= '\end{document}';
 1467: #    }  elsif ($choice eq 'Standard LaTeX output for the top level sequence') {
 1468: #        # where is the main sequence of the course?
 1469: #	$selectionmade = 4;
 1470: #	my $main_seq=&Apache::lonnet::clutter($ENV{'request.course.uri'});
 1471: #	my @file_seq = &coming_from_hash($main_seq);
 1472: #	for (my $i=0;$i<=$#file_seq;$i++) {
 1473: #	    my ($path,$title,$symb) = split /&&/,$file_seq[$i];
 1474: #	    $file_seq[$i] = $path.'&&'.$symb;
 1475: #	}
 1476: #	my $flag_latex_header_remove = 'NO';
 1477: #        #-- produce an output string
 1478: #	for (my $i=0;$i<=$#file_seq;$i++) {
 1479: #	   if (not $file_seq[$i]=~/^&&/) {
 1480: #	     my ($urlp,$symb) = split /&&/, $file_seq[$i];
 1481: #	     $urlp=~s/\/home\/httpd\/html//;	    
 1482: #	     if ($urlp=~m/\.(problem|exam|quiz|assess|survey|form|library)/) {
 1483: #		my %moreenv;
 1484: #		$moreenv{'form.grade_target'}='tex';
 1485: #		$moreenv{'form.textwidth'}=$LaTeXwidth;
 1486: #		&Apache::lonnet::appenv(%moreenv);
 1487: #		my $texversion=&Apache::lonnet::ssi($urlp,('symb'=>$symb));
 1488: #		&Apache::lonnet::delenv('form.grade_target','form.textwidth');		    
 1489: #		if ($flag_latex_header_remove ne 'NO') {
 1490: #		    $texversion = &latex_header_footer_remove($texversion);
 1491: #		} else {
 1492: #		    $texversion =~ s/\\end{document}//;
 1493: #		}
 1494: #		$result .= $texversion;  
 1495: #		$flag_latex_header_remove = 'YES';
 1496: #	     }		
 1497: #	   }
 1498: #	}	    
 1499: #	$result .= '\end{document}';
 1500: #    } elsif ($choice eq 'All class print') { 
 1501: #    #-- prints assignments for whole class or for selected students  
 1502: #	$selectionmade = 5;
 1503: #        my @students = ();
 1504: #	for (my $i=0; $i<$ENV{'form.numberofstudents'};$i++) {
 1505: #	    if ($ENV{'form.whomtoprint'.$i}=~/:/) {
 1506: #		push @students,$ENV{'form.whomtoprint'.$i};
 1507: #	    }
 1508: #	}
 1509: #	#where is the primary sequence containing current resource (the same for all students)?
 1510: #	my $symbolic = &Apache::lonnet::symbread($ENV{'form.url'});
 1511: #	my @sequence = split('___',$symbolic);
 1512: #	my $primary_sequence;
 1513: #
 1514: #	$primary_sequence=&Apache::lonnet::clutter($sequence[0]);
 1515: #	my @master_seq = &coming_from_hash($primary_sequence); 
 1516: #	for (my $i=0;$i<=$#master_seq;$i++) {
 1517: #	    my ($path,$title,$symb) = split /&&/,$master_seq[$i];
 1518: #	    $master_seq[$i] = $path.'&&'.$symb;
 1519: #	}
 1520: #        #loop over students
 1521: #	my $flag_latex_header_remove = 'NO'; 
 1522: #	my %moreenv;
 1523: #	$moreenv{'form.textwidth'}=$LaTeXwidth;
 1524: #	&Apache::lonnet::appenv(%moreenv);
 1525: #	my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin($r,'Print Status','Class Print Status',$#students+1);
 1526: #	foreach my $person (@students) {
 1527: #	    my $current_output = ''; 
 1528: #	    my ($usersection,$username,$userdomain) = split /:/,$person;
 1529: #	    my $fullname = &get_name($username,$userdomain);
 1530: #            #goes through all resources, checks if they are available for current student, and produces output   
 1531: #	    foreach my $curresline (@master_seq)  {
 1532: #		my ($curres,$symb) = split /&&/, $curresline;
 1533: #                if ($curres=~ m/\.(problem|exam|quiz|assess|survey|form|library)/) {
 1534: #		    my ($map,$id,$res_url) = split(/___/,$symb);
 1535: #		    if (&Apache::lonnet::allowed('bre',$res_url)) {
 1536: #			my $rendered = &Apache::loncommon::get_student_view($symb,$username,$userdomain,
 1537: #                                                                        $ENV{'request.course.id'},'tex');
 1538: #			if ($flag_latex_header_remove eq 'YES') {
 1539: #			    $rendered = &latex_header_footer_remove($rendered);
 1540: #			} else {
 1541: #			    $rendered =~ s/\\end{document}//;
 1542: #			}
 1543: #			$current_output .= $rendered;
 1544: #		    }
 1545: #		    $flag_latex_header_remove = 'YES';
 1546: #		}
 1547: #	    }
 1548: #	    my $courseidinfo = $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
 1549: #	    if ($current_output=~/\\documentclass/) {
 1550: #		$current_output =~ s/\\begin{document}/\\setlength{\\topmargin}{1cm}\\usepackage{fancyhdr}\\pagestyle{fancy}\\rhead{}\\chead{} \\begin{document}\\noindent\\lhead{\\textit{\\textbf{$fullname}} - $courseidinfo \\hfill \\thepage \\\\ \\textit{$assignment}}\\vskip 3 mm /;
 1551: #	    } else {
 1552: #		my $blanspages = '';
 1553: #		for (my $j=0;$j<$ENV{'form.addedpages'};$j++) {$blanspages.='\clearpage\strut\clearpage';}
 1554: #		$current_output = '\strut\\newline\\noindent\\makebox[\\textwidth/$number_of_columns][b]{\\hrulefill}\\newline\\noindent{\\tiny Printed from LON-CAPA\\copyright MSU{\\hfill} Licensed under GNU General Public License }\\newpage \\vskip 3mm'.$blanspages.'\setcounter{page}{1}\noindent\parbox{\minipagewidth}{\noindent\\lhead{\\textit{\\textbf{'.$fullname.'}} - '.$courseidinfo.' \\hfill \\thepage \\\\ \\textit{'.$assignment.'}}\hskip 1.4in } \vskip 5 mm '.$current_output;
 1555: #	    }
 1556: #	    $result .= $current_output;
 1557: #	    &Apache::lonnet::delenv('form.counter');	    
 1558: #	    &Apache::lonxml::init_counter(); 
 1559: #	    &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
 1560: #						    'last student '.$fullname);
 1561: #	}
 1562: #	&Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
 1563: #	$result .= '\end{document}';
 1564: #	&Apache::lonnet::delenv('form.textwidth');
 1565: #    } elsif ($choice eq 'Subdirectory print') {      
 1566: #    #prints selected problems from the subdirectory 
 1567: #	$selectionmade = 6;
 1568: #        my @list_of_files = ();
 1569: #	for (my $i=0; $i<$ENV{'form.numberofproblems'};$i++) {
 1570: #	    if ($ENV{'form.whattoprint'.$i}=~/^\//) {
 1571: #		push @list_of_files,$ENV{'form.whattoprint'.$i};
 1572: #	    }
 1573: #	}	
 1574: #	my $flag_latex_header_remove = 'NO';  
 1575: #	for (my $i=0;$i<=$#list_of_files;$i++) {
 1576: #	    my $urlp = $list_of_files[$i];
 1577: #	    if ($urlp=~/\//) {
 1578: #		my %moreenv;
 1579: #		$moreenv{'form.grade_target'}='tex';
 1580: #		$moreenv{'form.textwidth'}=$LaTeXwidth;
 1581: #		&Apache::lonnet::appenv(%moreenv);
 1582: #		if ($urlp =~ m|/home/([^/]+)/public_html|) {
 1583: #		    $urlp =~ s|/home/([^/]*)/public_html|/~$1|;
 1584: #		} else {
 1585: #		    $urlp =~ s|^/home/httpd/html||;
 1586: #		}
 1587: #		my $texversion=&Apache::lonnet::ssi($urlp);
 1588: #		&Apache::lonnet::delenv('form.grade_target','form.textwidth');
 1589: #                #this chunck is responsible for printing the path to problem
 1590: #		my $newurlp = '';
 1591: #		my $HowMany = length($urlp)*2;
 1592: #		if ($HowMany > $LaTeXwidth) {
 1593: #		    my @temporrary = split '/',$urlp;
 1594: #		    my $HowManyNew = 0;
 1595: #		    for (my $ii=0;$ii<=$#temporrary;$ii++) {
 1596: #			if ($temporrary[$ii] ne '') {
 1597: #			    $HowManyNew += length($temporrary[$ii])*2;
 1598: #			    if ($HowManyNew < $LaTeXwidth ) {
 1599: #				$newurlp .=  '/'.$temporrary[$ii];
 1600: #			    } else {
 1601: #				$HowManyNew = 0;
 1602: #				$newurlp .=  '|\vskip -1 mm \noindent \verb|';
 1603: #				$ii--;
 1604: #			    }
 1605: #			}
 1606: #		    }
 1607: #		}
 1608: #		$texversion =~ s/(\\begin{minipage}{\\textwidth})/$1 {\\small\\noindent\\verb|$newurlp\|\\vskip 0 mm}/;
 1609: #		if ($flag_latex_header_remove ne 'NO') {
 1610: #		    $texversion = &latex_header_footer_remove($texversion);
 1611: #		} else {
 1612: #		    $texversion =~ s/\\end{document}//;
 1613: #		}
 1614: #		$result .= $texversion;
 1615: #	    }
 1616: #	    $flag_latex_header_remove = 'YES';  
 1617: #	}
 1618: #	$result .= '\end{document}';      	
 1619:     }
 1620: #-------------------------------------------------------- corrections for the different page formats
 1621:     $result = &page_format_transformation_new($papersize,$laystyle,$numberofcolumns,$helper->{'VARS'}->{'PRINT_TYPE'},$result,$helper->{VARS}->{'assignment'});
 1622: #    if ($layout eq 'CBI') {
 1623: #        $laystyle = 'album';
 1624: #    }
 1625:     $result = &latex_corrections($number_of_columns,$result);
 1626: #    #changes page's parameters for the one column output 
 1627: #    if ($ENV{'form.numberofcolumns'} == 1) {
 1628: #	$result =~ s/\\textwidth= 9cm/\\textwidth= $ENV{'form.width'}/;
 1629: #	$result =~ s/\\textheight 25\.9cm/\\textheight $ENV{'form.height'}/;
 1630: #	$result =~ s/\\evensidemargin = -0\.57in/\\evensidemargin= $ENV{'form.leftmargin'}/;
 1631: #	$result =~ s/\\oddsidemargin = -0\.57in/\\oddsidemargin= $ENV{'form.leftmargin'}/;
 1632: #    }
 1633: #-- writing .tex file in prtspool 
 1634:     my $temp_file;
 1635:     my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout_".time."_".rand(10000000).".tex";
 1636:     unless ($temp_file = Apache::File->new('>'.$filename)) {
 1637: 	$r->log_error("Couldn't open $filename for output $!");
 1638: 	return SERVER_ERROR; 
 1639:     } 
 1640:     print $temp_file $result;
 1641: 
 1642: 
 1643: $r->print(<<FINALEND);
 1644: <meta http-equiv="Refresh" content="0; url=/cgi-bin/printout.pl?$filename&$laystyle&$numberofcolumns&$selectionmade">
 1645: </body>
 1646: </html>
 1647: FINALEND
 1648: }
 1649: 
 1650: 
 1651: 
 1652: sub handler {
 1653: 
 1654:     my $r = shift;
 1655:     my $helper;
 1656: 
 1657:     # A hook for me to work without disturbing Alex.
 1658:     if (!$ENV{'form.jeremy'}) {
 1659:         my $result = printHelper($r);
 1660:         if (!ref($result)) {
 1661:             return $result;
 1662:         }
 1663:         $helper = $result;
 1664:     
 1665: 
 1666: #    my $key; 
 1667: #    foreach $key (keys %{$helper->{'VARS'}}) {
 1668: #	$r->print(' '.$key.'->'.$helper->{'VARS'}->{$key}.'<br />');
 1669: #    }
 1670: #	return OK;
 1671: 	
 1672: 	
 1673: 	&output_data_new($r,$helper);
 1674: 
 1675: 
 1676: 
 1677: 
 1678: 
 1679: 
 1680:     return OK;
 1681:     }
 1682:     my $loaderror=&Apache::lonnet::overloaderror($r);
 1683:     if ($loaderror) { return $loaderror; }
 1684:     $loaderror=
 1685:        &Apache::lonnet::overloaderror($r,
 1686:          $ENV{'course.'.$ENV{'request.course.id'}.'.home'});
 1687:     if ($loaderror) { return $loaderror; }
 1688: 
 1689:     $r->content_type('text/html');
 1690:     $r->send_http_header;
 1691:     $r->print(&Apache::loncommon::bodytag("Printing"));
 1692: 
 1693:     if ($ENV{'request.course.id'}) {
 1694: 	my $fn=$ENV{'request.course.fn'};
 1695: 	tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER(),0640);
 1696:     }
 1697: 
 1698: #-- start form
 1699:     &headerform($r);
 1700: #-- menu for output
 1701:     unless  ($ENV{'form.phase'}) {
 1702: 	&menu_for_output($r);
 1703:     }
 1704: #-- additional menu for class printing
 1705:      if ($ENV{'form.phase'} eq 'two') {
 1706: 	 if($ENV{'form.choice'} eq 'Standard LaTeX output for whole primary sequence') {
 1707: 	     &sequence_content_menu(0,$r);
 1708: 	 } elsif($ENV{'form.choice'} eq 'Standard LaTeX output for the primary sequence') {
 1709: 	     &sequence_content_menu(1,$r);
 1710: 	 } elsif($ENV{'form.choice'} eq 'All class print') {
 1711: 	     &additional_class_menu($r);
 1712: 	 } elsif($ENV{'form.choice'} eq 'Subdirectory print') {
 1713: 	     &problem_choice_menu($r);
 1714: 	 } else {
 1715: 	     $ENV{'form.phase'} = 'three';
 1716: 	 }
 1717:      }
 1718: #-- additional menu for page layout (one column case)
 1719:     if ($ENV{'form.phase'} eq 'three') {
 1720: 	if($ENV{'form.numberofcolumns'} == 1) {
 1721: 	    &additional_print_menu($r);
 1722: 	} else {
 1723: 	    $ENV{'form.phase'} = 'four';
 1724: 	}
 1725:     }
 1726: #-- core part 
 1727:     if ($ENV{'form.phase'} eq 'four') {
 1728: 	&output_data($r);
 1729:     }	
 1730:     untie %hash;
 1731:     return OK;
 1732:    
 1733: } 
 1734: 
 1735: use Apache::lonhelper;
 1736: 
 1737: sub printHelper {
 1738:     my $r = shift;
 1739: 
 1740:     if ($ENV{'request.course.id'}) {
 1741: 	my $fn=$ENV{'request.course.fn'};
 1742: 	tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER(),0640);
 1743:     }
 1744: 
 1745:     if ($r->header_only) {
 1746:         if ($ENV{'browser.mathml'}) {
 1747:             $r->content_type('text/xml');
 1748:         } else {
 1749:             $r->content_type('text/html');
 1750:         }
 1751:         $r->send_http_header;
 1752:         return OK;
 1753:     }
 1754: 
 1755:     # Send header, nocache
 1756:     if ($ENV{'browser.mathml'}) {
 1757:         $r->content_type('text/xml');
 1758:     } else {
 1759:         $r->content_type('text/html');
 1760:     }
 1761:     &Apache::loncommon::no_cache($r);
 1762:     $r->send_http_header;
 1763:     $r->rflush();
 1764: 
 1765:     # Unfortunately, this helper is so complicated we have to
 1766:     # write it by hand
 1767: 
 1768:     Apache::loncommon::get_unprocessed_cgi($ENV{QUERY_STRING});
 1769:     
 1770:     my $helper = Apache::lonhelper::helper->new("Printing Wizard");
 1771:     
 1772:     # This will persistently load in the data we want from the
 1773:     # very first screen.
 1774:     if (defined($ENV{'form.postdata'})) {
 1775:         $helper->{VARS}->{'postdata'} = $ENV{'form.postdata'};
 1776:     }
 1777:     if (defined($ENV{'form.symb'})) {
 1778:         $helper->{VARS}->{'symb'} = &Apache::lonnet::symbread($helper->{VARS}->{'postdata'});
 1779:     }
 1780:     if (defined($ENV{'form.url'})) {
 1781:         $helper->{VARS}->{'url'} = $helper->{VARS}->{'postdata'};
 1782:     }
 1783: 
 1784:     my ($resourceTitle,$sequenceTitle,$mapTitle) = &details_for_menu;
 1785:     $helper->{VARS}->{'assignment'} = $sequenceTitle;
 1786: 
 1787:     if (!$resourceTitle) { # if the resource doesn't have a title, use the filename
 1788:         my $url = $helper->{VARS}->{'postdata'};
 1789:         $resourceTitle = substr($url, rindex($url, '/') + 1);
 1790:     }
 1791: 
 1792:     Apache::lonhelper::registerHelperTags();
 1793: 
 1794:     # Extract map
 1795:     my $symb = $helper->{VARS}->{'symb'};
 1796:     my ($map, $id, $url) = split(/__/, $symb);
 1797: 
 1798:     my $subdir = &Apache::lonnet::filelocation("", $helper->{VARS}->{'postdata'});
 1799: 
 1800:     # "Delete everything after the last slash."
 1801:     $subdir =~ s|/[^/]+$||;
 1802: 
 1803:     # What can be printed is a very dynamic decision based on
 1804:     # lots of factors. So we need to dynamically build this list.
 1805:     # To prevent security leaks, states are only added to the wizard
 1806:     # if they can be reached, which ensures manipulating the form input
 1807:     # won't allow anyone to reach states they shouldn't have permission
 1808:     # to reach.
 1809: 
 1810:     # printChoices is tracking the kind of printing the user can
 1811:     # do, and will be used in a choices construction later.
 1812:     # In the meantime we will be adding states and elements to
 1813:     # the helper by hand.
 1814:     my $printChoices = [];
 1815:     my $paramHash;
 1816:     # We can always print the current screen.
 1817: 
 1818:     push @{$printChoices}, ["<b>$resourceTitle</b> (exactly what was on the screen)", 'current_document', 'FINAL'];
 1819: 
 1820:     # If we're in a sequence...
 1821:     if ($helper->{VARS}->{'postdata'} =~ /\/res\//) {
 1822:         # Allow problems from sequence
 1823:         push @{$printChoices}, ["Problems from <b>$sequenceTitle</b>", 'map_problems', 'CHOOSE_PROBLEMS'];
 1824:         # Allow all resources from sequence
 1825:         push @{$printChoices}, ["Problems and pages from <b>$sequenceTitle</b>", 'map_problems_pages', 'CHOOSE_PROBLEMS_HTML'];
 1826: 
 1827:         my $isProblem = '$res->is_problem();';
 1828:         my $isProblemOrMap = '$res->is_problem() || $res->is_map()';
 1829:         my $isNotMap = '!$res->is_map();';
 1830:         my $symb = '$res->symb();';
 1831:         my $helperFragment = <<HELPERFRAGMENT;
 1832:   <state name="CHOOSE_PROBLEMS" title="Select Problems">
 1833:     <message>Select problems to print:</message>
 1834:     <resource variable="RESOURCES" multichoice="1">
 1835:       <nextstate>FINAL</nextstate>
 1836:       <filterfunc>return $isProblemOrMap</filterfunc>
 1837:       <choicefunc>return $isProblem</choicefunc>
 1838:       <mapurl>$map</mapurl>
 1839:       <valuefunc>return $symb</valuefunc>
 1840:       </resource>
 1841:     </state>
 1842: 
 1843:   <state name="CHOOSE_PROBLEMS_HTML" title="Select Resources">
 1844:     <message>Select resources to print:</message>
 1845:     <resource variable="RESOURCES" multichoice="1">
 1846:       <nextstate>FINAL</nextstate>
 1847:       <choicefunc>return $isNotMap;</choicefunc>
 1848:       <mapurl>$map</mapurl>
 1849:       <valuefunc>return $symb</valuefunc>
 1850:       </resource>
 1851:     </state>
 1852: HELPERFRAGMENT
 1853: 
 1854:         &Apache::lonxml::xmlparse($r, 'helper', $helperFragment); 
 1855:     }
 1856: 
 1857:     # If the user is priviledged, allow them to print all 
 1858:     # problems in the course, optionally for selected students
 1859:     if (($ENV{'request.role'}=~m/^cc\./ or $ENV{'request.role'}=~m/^in\./ or $ENV{'request.role'}=~m/^ta\./) and ($helper->{VARS}->{'postdata'}=~/\/res\//)) { 
 1860:         push @{$printChoices}, ['<b>All problems</b> in course (may take a lot of time)', 'all_problems', 'ALL_PROBLEMS'];
 1861:         push @{$printChoices}, ["Problems from <b>$sequenceTitle</b> for selected students", 'problems_for_students', 'CHOOSE_STUDENTS'];
 1862: 
 1863:         my $isProblem = '$res->is_problem();';
 1864:         my $isProblemOrMap = '$res->is_problem() || $res->is_map()';
 1865:         my $symb = '$res->symb();';
 1866:         &Apache::lonxml::xmlparse($r, 'helper', <<CHOOSE_STUDENTS);
 1867:   <state name="ALL_PROBLEMS" title="Select Problems">
 1868:     <message>Select problems to print:</message>
 1869:     <resource variable="RESOURCES" multichoice="1">
 1870:       <nextstate>FINAL</nextstate>
 1871:       <filterfunc>return $isProblemOrMap</filterfunc>
 1872:       <choicefunc>return $isProblem</choicefunc>
 1873:       <valuefunc>return $symb</valuefunc>
 1874:       </resource>
 1875:     </state>
 1876: 
 1877:   <state name="CHOOSE_STUDENTS" title="Choose Students">
 1878:     <message>Select the students you wish to print the problems for:</message>
 1879:     <student multichoice='1' variable="STUDENTS" nextstate="FINAL" />
 1880:     </state>
 1881: CHOOSE_STUDENTS
 1882:     }
 1883: 
 1884:     # FIXME: That RE should come from a library somewhere.
 1885:     if ((&Apache::lonnet::allowed('bre',$subdir) eq 'F') and ($helper->{VARS}->{'postdata'}=~/\.(problem|exam|quiz|assess|survey|form|library|page|xml|html|htm|xhtml|xhtm)/)) {    
 1886:         push @{$printChoices}, ["Problems from <b>$subdir</b>", 'problems_from_directory', 'CHOOSE_FROM_SUBDIR'];
 1887:         
 1888:         my $f = '$filename';
 1889:         my $xmlfrag = <<CHOOSE_FROM_SUBDIR;
 1890:   <state name="CHOOSE_FROM_SUBDIR" title="Select Files">
 1891:     <message>Select problems you wish to print from <b>$subdir</b></message>
 1892:     <files variable="FILES" multichoice='1'>
 1893:       <nextstate>FINAL</nextstate>
 1894:       <filechoice>return '$subdir';</filechoice>
 1895: CHOOSE_FROM_SUBDIR
 1896:         
 1897:         $xmlfrag .= <<'CHOOSE_FROM_SUBDIR';
 1898:       <filefilter>return $filename =~ 
 1899:            m/^[^\.]+\.(problem|exam|quiz|assess|survey|form|library)$/;
 1900:       </filefilter>
 1901:       </files>
 1902:     </state>
 1903: CHOOSE_FROM_SUBDIR
 1904:         &Apache::lonxml::xmlparse($r, 'helper', $xmlfrag);
 1905:     }
 1906: 
 1907:     # Generate the first state, to select which resources get printed.
 1908:     Apache::lonhelper::state->new("START", "Selecting Resources to Print");
 1909:     $paramHash = Apache::lonhelper::getParamHash();
 1910:     $paramHash->{MESSAGE_TEXT} = "What do you want to print?";
 1911:     Apache::lonhelper::message->new();
 1912:     $paramHash = Apache::lonhelper::getParamHash();
 1913:     $paramHash->{'variable'} = 'PRINT_TYPE';
 1914:     $helper->declareVar('PRINT_TYPE');
 1915:     $paramHash->{CHOICES} = $printChoices;
 1916:     Apache::lonhelper::choices->new();
 1917:     Apache::lonprintout::page_format_state->new("FORMAT");
 1918: 
 1919:     $helper->process();
 1920: 
 1921:     # MANUAL BAILOUT CONDITION:
 1922:     # If we're in the "final" state, bailout and return to handler
 1923:     if ($helper->{STATE} eq 'FINAL') {
 1924:         return $helper;
 1925:     }    
 1926: 
 1927:     $r->print($helper->display());
 1928: 
 1929:     Apache::lonhelper::unregisterHelperTags();
 1930: 
 1931:     untie %hash;
 1932: 
 1933:     return OK;
 1934: }
 1935: 
 1936: 
 1937: 1;
 1938: 
 1939: package Apache::lonprintout::page_format_state;
 1940: 
 1941: =pod
 1942: 
 1943: =head1 Helper element: page_format_state
 1944: 
 1945: See lonhelper.pm documentation for discussion of the helper framework.
 1946: 
 1947: Apache::lonprintout::page_format_state is an element that gives the 
 1948: user an opportunity to select the page layout they wish to print 
 1949: with: Number of columns, portrait/landscape, and paper size. If you 
 1950: want to change the paper size choices, change the @paperSize array 
 1951: contents in this package.
 1952: 
 1953: page_format_state is always directly invoked in lonprintout.pm, so there
 1954: is no tag interface. You actually pass parameters to the constructor.
 1955: 
 1956: =over 4
 1957: 
 1958: =item * B<new>(varName): varName is where the print information will be stored in the format FIXME.
 1959: 
 1960: =back
 1961: 
 1962: =cut
 1963: 
 1964: use Apache::lonhelper;
 1965: 
 1966: no strict;
 1967: @ISA = ("Apache::lonhelper::element");
 1968: use strict;
 1969: 
 1970: my $maxColumns = 2;
 1971: my @paperSize = ("Letter [8 1/2x11 in]", "Legal [8 1/2x14 in]", 
 1972:                  "Ledger/Tabloid [11x17 in]", "Executive [7 1/2x10 in]",
 1973:                  "A2 [420x594 mm]", "A3 [297x420 mm]", "A4 [210x297 mm]", 
 1974:                  "A5 [148x210 mm]", "A6 [105x148 mm]" );
 1975: 
 1976: # Tentative format: Orientation (L = Landscape, P = portrait) | Colnum |
 1977: #                   Paper type
 1978: 
 1979: sub new { 
 1980:     my $self = Apache::lonhelper::element->new();
 1981: 
 1982:     shift;
 1983: 
 1984:     $self->{'variable'} = shift;
 1985:     my $helper = Apache::lonhelper::getHelper();
 1986:     $helper->declareVar($self->{'variable'});
 1987:     bless($self);
 1988:     return $self;
 1989: }
 1990: 
 1991: sub render {
 1992:     my $self = shift;
 1993:     my $helper = Apache::lonhelper::getHelper();
 1994:     my $result = '';
 1995:     my $var = $self->{'variable'};
 1996:     my $curVal = $helper->{VARS}->{$var};
 1997: 
 1998:     my ($layout, $cols, $paper) = split(/\|/, $curVal);
 1999: 
 2000:     $result .= <<STATEHTML;
 2001: 
 2002: <p>How should the printing be formatted?</p>
 2003: <table cellpadding="3">
 2004:   <tr>
 2005:     <td align="center"><b>Layout</b></td>
 2006:     <td align="center"><b>Columns</b></td>
 2007:     <td align="center"><b>Paper type</b></td>
 2008:   </tr>
 2009:   <tr>
 2010:     <td>
 2011:       <input type="radio" name="${var}.layout" value="L" checked='1' /> Landscape<br />
 2012:       <input type="radio" name="${var}.layout" value="P" /> Portrait
 2013:     </td>
 2014:     <td>
 2015:       <select name="${var}.cols">
 2016: STATEHTML
 2017: 
 2018:     my $i;
 2019:     for ($i = 1; $i <= $maxColumns; $i++) {
 2020:         if ($i == $cols) {
 2021:             $result .= "<option value='$i' selected>$i</option>\n";
 2022:         } else {
 2023:             $result .= "<option value='$i'>$i</option>\n";
 2024:         }
 2025:     }
 2026: 
 2027:     $result .= "</select></td><td>\n";
 2028:     $result .= "<select name='${var}.paper'>\n";
 2029: 
 2030:     $i = 0;
 2031:     foreach (@paperSize) {
 2032:         if ($paper == $i) {
 2033:             $result .= "<option selected value='$i'>" . $paperSize[$i] . "</option>\n";
 2034:         } else {
 2035:             $result .= "<option value='$i'>" . $paperSize[$i] . "</option>\n";
 2036:         }
 2037:         $i++;
 2038:     }
 2039: 
 2040:     $result .= "</select></td></tr></table>";
 2041:     return $result;
 2042: }
 2043: 
 2044: sub postprocess {
 2045:     my $self = shift;
 2046: 
 2047:     my $var = $self->{'variable'};
 2048:     my $helper = Apache::lonhelper->getHelper();
 2049:     $helper->{VARS}->{$var} = 
 2050:         $ENV{"form.$var.layout"} . '|' . $ENV{"form.$var.cols"} . '|' .
 2051:         $ENV{"form.$var.paper"};
 2052:     return 1;
 2053: }
 2054: 
 2055: 1;
 2056: 
 2057: __END__
 2058: 

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