File:  [LON-CAPA] / loncom / interface / lonprintout.pm
Revision 1.520: download - view: text, annotated - select for diffs
Wed Mar 12 02:45:07 2008 UTC (16 years, 2 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Change arguments accepted by lonnet::appenv() to:
1. reference to hash
2. (optional) reference to array.

Change all instances where lonnet::appenv() is called to replace first argument passed (originally a hash) to a reference to the hash.

- Modify lonnet.pm rev 1.35 code intended to prevent "dangerous" modifications of the environment, so it will actually do this.
- Allow these modifications to be made in certain instances:
   DC switching to adhoc role
   CC switching to a different course role in the current course.
   user self-enrolling as a student

    1: # The LearningOnline Network
    2: # Printout
    3: #
    4: # $Id: lonprintout.pm,v 1.520 2008/03/12 02:45:07 raeburn 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: # http://www.lon-capa.org/
   26: #
   27: #
   28: package Apache::lonprintout;
   29: 
   30: use strict;
   31: use Apache::Constants qw(:common :http);
   32: use Apache::lonxml;
   33: use Apache::lonnet;
   34: use Apache::loncommon;
   35: use Apache::inputtags;
   36: use Apache::grades;
   37: use Apache::edit;
   38: use Apache::File();
   39: use Apache::lonnavmaps;
   40: use Apache::admannotations;
   41: use HTTP::Response;
   42: 
   43: use LONCAPA::map();
   44: use POSIX qw(strftime);
   45: use Apache::lonlocal;
   46: use Carp;
   47: use LONCAPA;
   48: 
   49: my %perm;
   50: my %parmhash;
   51: my $resources_printed;
   52: 
   53: # Global variables that describe errors in ssi calls detected  by ssi_with_retries.
   54: #
   55: 
   56: my $ssi_error;			# True if there was an ssi error.
   57: my $ssi_last_error_resource;	# The resource URI that could not be fetched.
   58: my $ssi_last_error;		# The error text from the server. (e.g. 500 Server timed out).
   59: 
   60: #
   61: #  Our ssi max retry count.
   62: #
   63: 
   64: my $ssi_retry_count = 5;	# Some arbitrary value.
   65: 
   66: 
   67: 
   68: # Fetch the contents of a resource, uninterpreted.
   69: # This is used here to fetch a latex file to be included
   70: # verbatim into the printout<
   71: # NOTE: Ask Guy if there is a lonnet function similar to this?
   72: #
   73: # Parameters:
   74: #   URL of the file
   75: #
   76: sub fetch_raw_resource {
   77:     my ($url) = @_;
   78: 
   79:     my $filename  = &Apache::lonnet::filelocation("", $url);
   80:     my $contents  = &Apache::lonnet::getfile($filename);
   81: 
   82:     if ($contents == -1) {
   83: 	return "File open failed for $filename";      # This will bomb the print.
   84:     }
   85:     return $contents;
   86: 
   87:     
   88: }
   89: 
   90: #  Fetch the annotations associated with a URL and 
   91: #  put a centered 'annotations:' title.
   92: #  This is all suppressed if the annotations are empty.
   93: #
   94: sub annotate {
   95:     my ($symb) = @_;
   96: 
   97:     my $annotation_text = &Apache::admannotations::get_annotation($symb, 1);
   98: 
   99: 
  100:     my $result = "";
  101: 
  102:     if (length($annotation_text) > 0) {
  103: 	$result .= '\\hspace*{\\fill} \\\\[\\baselineskip] \textbf{Annotations:} \\\\ ';
  104: 	$result .= "\n";
  105: 	$result .= &Apache::lonxml::latex_special_symbols($annotation_text,"");	# Escape latex.
  106: 	$result .= "\n\n";
  107:     }
  108:     return $result;
  109: }
  110: 
  111: 
  112: #
  113: #   ssi_with_retries - Does the server side include of a resource.
  114: #                      if the ssi call returns an error we'll retry it up to
  115: #                      the number of times requested by the caller.
  116: #                      If we still have a proble, no text is appended to the
  117: #                      output and we set some global variables.
  118: #                      to indicate to the caller an SSI error occured.  
  119: #                      All of this is supposed to deal with the issues described
  120: #                      in LonCAPA BZ 5631 see:
  121: #                      http://bugs.lon-capa.org/show_bug.cgi?id=5631
  122: #                      by informing the user that this happened.
  123: #
  124: # Parameters:
  125: #   resource   - The resource to include.  This is passed directly, without
  126: #                interpretation to lonnet::ssi.
  127: #   form       - The form hash parameters that guide the interpretation of the resource
  128: #                
  129: #   retries    - Number of retries allowed before giving up completely.
  130: # Returns:
  131: #   On success, returns the rendered resource identified by the resource parameter.
  132: # Side Effects:
  133: #   The following global variables can be set:
  134: #    ssi_error                - If an unrecoverable error occured this becomes true.
  135: #                               It is up to the caller to initialize this to false
  136: #                               if desired.
  137: #    ssi_last_error_resource  - If an unrecoverable error occured, this is the value
  138: #                               of the resource that could not be rendered by the ssi
  139: #                               call.
  140: #    ssi_last_error           - The error string fetched from the ssi response
  141: #                               in the event of an error.
  142: #
  143: sub ssi_with_retries {
  144:     my ($resource, $retries, %form) = @_;
  145: 
  146: 
  147:     my ($content, $response) = &Apache::loncommon::ssi_with_retries($resource, $retries, %form);
  148:     if (!$response->is_success) {
  149: 	$ssi_error               = 1;
  150: 	$ssi_last_error_resource = $resource;
  151: 	$ssi_last_error          = $response->code . " " . $response->message;
  152: 	
  153: 	&Apache::lonnet::logthis("Error in SSI resource: $resource Error: $ssi_last_error");
  154:     }
  155: 
  156:     return $content;
  157: 
  158: }
  159: 
  160: #
  161: #   printf_style_subst  item format_string repl
  162: #  
  163: # Does printf style substitution for a format string that
  164: # can have %[n]item in it.. wherever, %[n]item occurs,
  165: # rep is substituted in format_string.  Note that
  166: # [n] is an optional integer length.  If provided,
  167: # repl is truncated to at most [n] characters prior to 
  168: # substitution.
  169: #
  170: sub printf_style_subst {
  171:     my ($item, $format_string, $repl) = @_;
  172:     my $result = "";
  173:     while ($format_string =~ /(%)(\d*)\Q$item\E/g ) {
  174: 	my $fmt = $1;
  175: 	my $size = $2;
  176: 	my $subst = $repl;
  177: 	if ($size ne "") {
  178: 	    $subst = substr($subst, 0, $size);
  179: 	    
  180: 	    #  Here's a nice edge case.. supose the end of the
  181: 	    #  substring is a \.  In that case may have  just
  182: 	    #  chopped off a TeX escape... in that case, we append
  183: 	    #   " " for the trailing character, and let the field 
  184: 	    #  spill over a bit (sigh).
  185: 	    #  We don't just chop off the last character in order to deal
  186: 	    #  with one last pathology, and that would be if substr had
  187: 	    #  trimmed us to e.g. \\\  
  188: 
  189: 
  190: 	    if ($subst =~ /\\$/) {
  191: 		$subst .= " ";
  192: 	    }
  193: 	}
  194: 	my $item_pos = pos($format_string);
  195: 	$result .= substr($format_string, 0, $item_pos - length($size) -2) . $subst;
  196:         $format_string = substr($format_string, pos($format_string));
  197:     }
  198: 
  199:     # Put the residual format string into the result:
  200: 
  201:     $result .= $format_string;
  202: 
  203:     return $result;
  204: }
  205: 
  206: 
  207: # Format a header according to a format.  
  208: # 
  209: 
  210: # Substitutions:
  211: #     %a    - Assignment name.
  212: #     %c    - Course name.
  213: #     %n    - Student name.
  214: #
  215: sub format_page_header {
  216:     my ($width, $format, $assignment, $course, $student) = @_;
  217:     
  218:     $width = &recalcto_mm($width); # Get width in mm.
  219:     #  Default format?
  220: 
  221:     if ($format eq '') {
  222: 	# For the default format, we may need to truncate
  223: 	# elements..  To do this we need to get the page width.
  224: 	# we assume that each character is about 2mm in width.
  225: 	# (correct for the header text size??).  We ignore
  226: 	# any formatting (e.g. boldfacing in this).
  227: 	# 
  228: 	# - Allow the student/course to be one line.
  229: 	#   but only truncate the course.
  230: 	# - Allow the assignment to be 2 lines (wrapped).
  231: 	#
  232: 	my $chars_per_line = $width/2; # Character/textline.
  233: 
  234: 
  235: 	my $firstline = "$student $course";
  236: 	if (length($firstline) > $chars_per_line) {
  237: 	    my $lastchar = $chars_per_line - length($student) - 1;
  238: 	    if ($lastchar > 0) {
  239: 		$course = substr($course, 0, $lastchar);
  240: 	    } else {		# Nothing left of course:
  241: 		$course = '';
  242: 	    }
  243: 	}
  244: 	if (length($assignment) > $chars_per_line) {
  245: 	    $assignment = substr($assignment, 0, $chars_per_line);
  246: 	}
  247: 	
  248: 	$format =  "\\textbf{$student} $course \\hfill \\thepage \\\\ \\textit{$assignment}";
  249: 	
  250:     } else {
  251: 	# An open question is how to handle long user formatted page headers...
  252: 	# A possible future is to support e.g. %na so that the user can control
  253: 	# the truncation of the elements that can appear in the header.
  254: 	#
  255: 	$format =  &printf_style_subst("a", $format, $assignment);
  256: 	$format =  &printf_style_subst("c", $format, $course);
  257: 	$format =  &printf_style_subst("n", $format, $student);
  258: 
  259: 	# If the user put %'s in the format string, they  must be escaped
  260: 	# to \% else LaTeX will think they are comments and terminate
  261: 	# the line.. which is bad!!!
  262: 
  263: 
  264:     }
  265:     
  266: 
  267:     return $format;
  268:     
  269: }
  270: 
  271: #
  272: #   Convert a numeric code to letters
  273: #
  274: sub num_to_letters {
  275:     my ($num) = @_;
  276:     my @nums= split('',$num);
  277:     my @num_to_let=('A'..'Z');
  278:     my $word;
  279:     foreach my $digit (@nums) { $word.=$num_to_let[$digit]; }
  280:     return $word;
  281: }
  282: #   Convert a letter code to numeric.
  283: #
  284: sub letters_to_num {
  285:     my ($letters) = @_;
  286:     my @letters = split('', uc($letters));
  287:    my %substitution;
  288:     my $digit = 0;
  289:     foreach my $letter ('A'..'J') {
  290: 	$substitution{$letter} = $digit;
  291: 	$digit++;
  292:     }
  293:     #  The substitution is done as below to preserve leading
  294:     #  zeroes which are needed to keep the code size exact
  295:     #
  296:     my $result ="";
  297:     foreach my $letter (@letters) {
  298: 	$result.=$substitution{$letter};
  299:     }
  300:     return $result;
  301: }
  302: 
  303: #  Determine if a code is a valid numeric code.  Valid
  304: #  numeric codes must be comprised entirely of digits and
  305: #  have a correct number of digits.
  306: #
  307: #  Parameters:
  308: #     value      - proposed code value.
  309: #     num_digits - Number of digits required.
  310: #
  311: sub is_valid_numeric_code {
  312:     my ($value, $num_digits) = @_;
  313:     #   Remove leading/trailing whitespace;
  314:     $value =~ s/^\s*//g;
  315:     $value =~ s/\s*$//g;
  316:     
  317:     #  All digits?
  318:     if ($value !~ /^[0-9]+$/) {
  319: 	return "Numeric code $value has invalid characters - must only be digits";
  320:     }
  321:     if (length($value) != $num_digits) {
  322: 	return "Numeric code $value incorrect number of digits (correct = $num_digits)";
  323:     }
  324:     return undef;
  325: }
  326: #   Determines if a code is a valid alhpa code.  Alpha codes
  327: #   are ciphers that map  [A-J,a-j] -> 0..9 0..9.
  328: #   They also have a correct digit count.
  329: # Parameters:
  330: #     value          - Proposed code value.
  331: #     num_letters    - correct number of letters.
  332: # Note:
  333: #    leading and trailing whitespace are ignored.
  334: #
  335: sub is_valid_alpha_code {
  336:     my ($value, $num_letters) = @_;
  337:     
  338:      # strip leading and trailing spaces.
  339: 
  340:     $value =~ s/^\s*//g;
  341:     $value =~ s/\s*$//g;
  342: 
  343:     #  All alphas in the right range?
  344:     if ($value !~ /^[A-J,a-j]+$/) {
  345: 	return "Invalid letter code $value must only contain A-J";
  346:     }
  347:     if (length($value) != $num_letters) {
  348: 	return "Letter code $value has incorrect number of letters (correct = $num_letters)";
  349:     }
  350:     return undef;
  351: }
  352: 
  353: #   Determine if a code entered by the user in a helper is valid.
  354: #   valid depends on the code type and the type of code selected.
  355: #   The type of code selected can either be numeric or 
  356: #   Alphabetic.  If alphabetic, the code, in fact is a simple
  357: #   substitution cipher for the actual numeric code: 0->A, 1->B ...
  358: #   We'll be nice and be case insensitive for alpha codes.
  359: # Parameters:
  360: #    code_value    - the value of the code the user typed in.
  361: #    code_option   - The code type selected from the set in the scantron format
  362: #                    table.
  363: # Returns:
  364: #    undef         - The code is valid.
  365: #    other         - An error message indicating what's wrong.
  366: #
  367: sub is_code_valid {
  368:     my ($code_value, $code_option) = @_;
  369:     my ($code_type, $code_length) = ('letter', 6);	# defaults.
  370:     open(FG, $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab');
  371:     foreach my $line (<FG>) {
  372: 	my ($name, $type, $length) = (split(/:/, $line))[0,2,4];
  373: 	if($name eq $code_option) {
  374: 	    $code_length = $length;
  375: 	    if($type eq 'number') {
  376: 		$code_type = 'number';
  377: 	    }
  378: 	}
  379:     }
  380:     my $valid;
  381:     if ($code_type eq 'number') {
  382: 	return &is_valid_numeric_code($code_value, $code_length);
  383:     } else {
  384: 	return &is_valid_alpha_code($code_value, $code_length);
  385:     }
  386: 
  387: }
  388: 
  389: #   Compare two students by name.  The students are in the form
  390: #   returned by the helper:
  391: #      user:domain:section:last,   first:status
  392: #   This is a helper function for the perl sort built-in  therefore:
  393: # Implicit Inputs:
  394: #    $a     - The first element to compare (global)
  395: #    $b     - The second element to compare (global)
  396: # Returns:
  397: #   -1   - $a < $b
  398: #    0   - $a == $b
  399: #   +1   - $a > $b
  400: #   Note that the initial comparison is done on the last names with the
  401: #   first names only used to break the tie.
  402: #
  403: #
  404: sub compare_names {
  405:     #  First split the names up into the primary fields.
  406: 
  407:     my ($u1, $d1, $s1, $n1, $stat1) = split(/:/, $a);
  408:     my ($u2, $d2, $s2, $n2, $stat2) = split(/:/, $b);
  409: 
  410:     # Now split the last name and first name of each n:
  411:     #
  412: 
  413:     my ($l1,$f1) = split(/,/, $n1);
  414:     my ($l2,$f2) = split(/,/, $n2);
  415: 
  416:     # We don't bother to remove the leading/trailing whitespace from the
  417:     # firstname, unless the last names compare identical.
  418: 
  419:     if($l1 lt $l2) {
  420: 	return -1;
  421:     }
  422:     if($l1 gt $l2) {
  423: 	return  1;
  424:     }
  425: 
  426:     # Break the tie on the first name, but there are leading (possibly trailing
  427:     # whitespaces to get rid of first 
  428:     #
  429:     $f1 =~ s/^\s+//;		# Remove leading...
  430:     $f1 =~ s/\s+$//;		# Trailing spaces from first 1...
  431:     
  432:     $f2 =~ s/^\s+//;
  433:     $f2 =~ s/\s+$//;		# And the same for first 2...
  434: 
  435:     if($f1 lt $f2) {
  436: 	return -1;
  437:     }
  438:     if($f1 gt $f2) {
  439: 	return 1;
  440:     }
  441:     
  442:     #  Must be the same name.
  443: 
  444:     return 0;
  445: }
  446: 
  447: sub latex_header_footer_remove {
  448:     my $text = shift;
  449:     $text =~ s/\\end{document}//;
  450:     $text =~ s/\\documentclass([^&]*)\\begin{document}//;
  451:     return $text;
  452: }
  453: #
  454: #  If necessary, encapsulate text inside 
  455: #  a minipage env.
  456: #  necessity is determined by the problem_split param.
  457: #
  458: sub encapsulate_minipage {
  459:     my ($text) = @_;
  460:     if (!($env{'form.problem.split'} =~ /yes/i)) {
  461: 	$text = '\begin{minipage}{\textwidth}'.$text.'\end{minipage}';
  462:     }
  463:     return $text;
  464: }
  465: #
  466: #  The NUMBER_TO_PRINT and SPLIT_PDFS
  467: #  variables interact, this sub looks at these two parameters
  468: #  and comes up with a final value for NUMBER_TO_PRINT which can be:
  469: #     all     - if SPLIT_PDFS eq 'all'.
  470: #     1       - if SPLIT_PDFS eq 'oneper'
  471: #     section - if SPLIT_PDFS eq 'sections'
  472: #     <unchanged> - if SPLIT_PDFS eq 'usenumber'
  473: #
  474: sub adjust_number_to_print {
  475:     my $helper = shift;
  476: 
  477:     my $split_pdf = $helper->{'VARS'}->{'SPLIT_PDFS'};
  478:     
  479:     if ($split_pdf eq 'all') {
  480: 	$helper->{'VARS'}->{'NUMBER_TO_PRINT'} = 'all';
  481:     } elsif ($split_pdf eq 'oneper') {
  482: 	$helper->{'VARS'}->{'NUMBER_TO_PRINT'} = 1;
  483:     } elsif ($split_pdf eq 'sections') {
  484: 	$helper->{'VARS'}->{'NUMBER_TO_PRINT'} = 'section';
  485:     } elsif ($split_pdf eq 'usenumber') {
  486: 	#  Unmodified.
  487:     } else {
  488: 	# Error!!!!
  489: 
  490: 	croak "bad SPLIT_PDFS: $split_pdf in lonprintout::adjust_number_to_print";
  491:     }
  492: }
  493: 
  494: sub character_chart {
  495:     my $result = shift;	
  496:     $result =~ s/&\#0?0?(7|9);//g;
  497:     $result =~ s/&\#0?(10|13);//g;
  498:     $result =~ s/&\#0?32;/ /g;
  499:     $result =~ s/&\#0?33;/!/g;
  500:     $result =~ s/&(\#0?34|quot);/\"/g;
  501:     $result =~ s/&\#0?35;/\\\#/g;
  502:     $result =~ s/&\#0?36;/\\\$/g;
  503:     $result =~ s/&\#0?37;/\\%/g; 
  504:     $result =~ s/&(\#0?38|amp);/\\&/g; 
  505:     $result =~ s/&\#(0?39|146);/\'/g;
  506:     $result =~ s/&\#0?40;/(/g;
  507:     $result =~ s/&\#0?41;/)/g;
  508:     $result =~ s/&\#0?42;/\*/g;
  509:     $result =~ s/&\#0?43;/\+/g;
  510:     $result =~ s/&\#(0?44|130);/,/g;
  511:     $result =~ s/&\#0?45;/-/g;
  512:     $result =~ s/&\#0?46;/\./g;
  513:     $result =~ s/&\#0?47;/\//g;
  514:     $result =~ s/&\#0?48;/0/g;
  515:     $result =~ s/&\#0?49;/1/g;
  516:     $result =~ s/&\#0?50;/2/g;
  517:     $result =~ s/&\#0?51;/3/g;
  518:     $result =~ s/&\#0?52;/4/g;
  519:     $result =~ s/&\#0?53;/5/g;
  520:     $result =~ s/&\#0?54;/6/g;
  521:     $result =~ s/&\#0?55;/7/g;
  522:     $result =~ s/&\#0?56;/8/g;
  523:     $result =~ s/&\#0?57;/9/g;
  524:     $result =~ s/&\#0?58;/:/g;
  525:     $result =~ s/&\#0?59;/;/g;
  526:     $result =~ s/&(\#0?60|lt|\#139);/\$<\$/g;
  527:     $result =~ s/&\#0?61;/\\ensuremath\{=\}/g;
  528:     $result =~ s/&(\#0?62|gt|\#155);/\\ensuremath\{>\}/g;
  529:     $result =~ s/&\#0?63;/\?/g;
  530:     $result =~ s/&\#0?65;/A/g;
  531:     $result =~ s/&\#0?66;/B/g;
  532:     $result =~ s/&\#0?67;/C/g;
  533:     $result =~ s/&\#0?68;/D/g;
  534:     $result =~ s/&\#0?69;/E/g;
  535:     $result =~ s/&\#0?70;/F/g;
  536:     $result =~ s/&\#0?71;/G/g;
  537:     $result =~ s/&\#0?72;/H/g;
  538:     $result =~ s/&\#0?73;/I/g;
  539:     $result =~ s/&\#0?74;/J/g;
  540:     $result =~ s/&\#0?75;/K/g;
  541:     $result =~ s/&\#0?76;/L/g;
  542:     $result =~ s/&\#0?77;/M/g;
  543:     $result =~ s/&\#0?78;/N/g;
  544:     $result =~ s/&\#0?79;/O/g;
  545:     $result =~ s/&\#0?80;/P/g;
  546:     $result =~ s/&\#0?81;/Q/g;
  547:     $result =~ s/&\#0?82;/R/g;
  548:     $result =~ s/&\#0?83;/S/g;
  549:     $result =~ s/&\#0?84;/T/g;
  550:     $result =~ s/&\#0?85;/U/g;
  551:     $result =~ s/&\#0?86;/V/g;
  552:     $result =~ s/&\#0?87;/W/g;
  553:     $result =~ s/&\#0?88;/X/g;
  554:     $result =~ s/&\#0?89;/Y/g;
  555:     $result =~ s/&\#0?90;/Z/g;
  556:     $result =~ s/&\#0?91;/[/g;
  557:     $result =~ s/&\#0?92;/\\ensuremath\{\\setminus\}/g;
  558:     $result =~ s/&\#0?93;/]/g;
  559:     $result =~ s/&\#(0?94|136);/\\ensuremath\{\\wedge\}/g;
  560:     $result =~ s/&\#(0?95|138|154);/\\underline{\\makebox[2mm]{\\strut}}/g;
  561:     $result =~ s/&\#(0?96|145);/\`/g;
  562:     $result =~ s/&\#0?97;/a/g;
  563:     $result =~ s/&\#0?98;/b/g;
  564:     $result =~ s/&\#0?99;/c/g;
  565:     $result =~ s/&\#100;/d/g;
  566:     $result =~ s/&\#101;/e/g;
  567:     $result =~ s/&\#102;/f/g;
  568:     $result =~ s/&\#103;/g/g;
  569:     $result =~ s/&\#104;/h/g;
  570:     $result =~ s/&\#105;/i/g;
  571:     $result =~ s/&\#106;/j/g;
  572:     $result =~ s/&\#107;/k/g;
  573:     $result =~ s/&\#108;/l/g;
  574:     $result =~ s/&\#109;/m/g;
  575:     $result =~ s/&\#110;/n/g;
  576:     $result =~ s/&\#111;/o/g;
  577:     $result =~ s/&\#112;/p/g;
  578:     $result =~ s/&\#113;/q/g;
  579:     $result =~ s/&\#114;/r/g;
  580:     $result =~ s/&\#115;/s/g;
  581:     $result =~ s/&\#116;/t/g;
  582:     $result =~ s/&\#117;/u/g;
  583:     $result =~ s/&\#118;/v/g;
  584:     $result =~ s/&\#119;/w/g;
  585:     $result =~ s/&\#120;/x/g;
  586:     $result =~ s/&\#121;/y/g;
  587:     $result =~ s/&\#122;/z/g;
  588:     $result =~ s/&\#123;/\\{/g;
  589:     $result =~ s/&\#124;/\|/g;
  590:     $result =~ s/&\#125;/\\}/g;
  591:     $result =~ s/&\#126;/\~/g;
  592:     $result =~ s/&\#131;/\\textflorin /g;
  593:     $result =~ s/&\#132;/\"/g;
  594:     $result =~ s/&\#133;/\\ensuremath\{\\ldots\}/g;
  595:     $result =~ s/&\#134;/\\ensuremath\{\\dagger\}/g;
  596:     $result =~ s/&\#135;/\\ensuremath\{\\ddagger\}/g;
  597:     $result =~ s/&\#137;/\\textperthousand /g;
  598:     $result =~ s/&\#140;/{\\OE}/g;
  599:     $result =~ s/&\#147;/\`\`/g;
  600:     $result =~ s/&\#148;/\'\'/g;
  601:     $result =~ s/&\#149;/\\ensuremath\{\\bullet\}/g;
  602:     $result =~ s/&(\#150|\#8211);/--/g;
  603:     $result =~ s/&\#151;/---/g;
  604:     $result =~ s/&\#152;/\\ensuremath\{\\sim\}/g;
  605:     $result =~ s/&\#153;/\\texttrademark /g;
  606:     $result =~ s/&\#156;/\\oe/g;
  607:     $result =~ s/&\#159;/\\\"Y/g;
  608:     $result =~ s/&(\#160|nbsp);/~/g;
  609:     $result =~ s/&(\#161|iexcl);/!\`/g;
  610:     $result =~ s/&(\#162|cent);/\\textcent /g;
  611:     $result =~ s/&(\#163|pound);/\\pounds /g; 
  612:     $result =~ s/&(\#164|curren);/\\textcurrency /g;
  613:     $result =~ s/&(\#165|yen);/\\textyen /g;
  614:     $result =~ s/&(\#166|brvbar);/\\textbrokenbar /g;
  615:     $result =~ s/&(\#167|sect);/\\textsection /g;
  616:     $result =~ s/&(\#168|uml);/\\texthighdieresis /g;
  617:     $result =~ s/&(\#169|copy);/\\copyright /g;
  618:     $result =~ s/&(\#170|ordf);/\\textordfeminine /g;
  619:     $result =~ s/&(\#172|not);/\\ensuremath\{\\neg\}/g;
  620:     $result =~ s/&(\#173|shy);/ - /g;
  621:     $result =~ s/&(\#174|reg);/\\textregistered /g;
  622:     $result =~ s/&(\#175|macr);/\\ensuremath\{^{-}\}/g;
  623:     $result =~ s/&(\#176|deg);/\\ensuremath\{^{\\circ}\}/g;
  624:     $result =~ s/&(\#177|plusmn);/\\ensuremath\{\\pm\}/g;
  625:     $result =~ s/&(\#178|sup2);/\\ensuremath\{^2\}/g;
  626:     $result =~ s/&(\#179|sup3);/\\ensuremath\{^3\}/g;
  627:     $result =~ s/&(\#180|acute);/\\textacute /g;
  628:     $result =~ s/&(\#181|micro);/\\ensuremath\{\\mu\}/g;
  629:     $result =~ s/&(\#182|para);/\\P/g;
  630:     $result =~ s/&(\#183|middot);/\\ensuremath\{\\cdot\}/g;
  631:     $result =~ s/&(\#184|cedil);/\\c{\\strut}/g;
  632:     $result =~ s/&(\#185|sup1);/\\ensuremath\{^1\}/g;
  633:     $result =~ s/&(\#186|ordm);/\\textordmasculine /g;
  634:     $result =~ s/&(\#188|frac14);/\\textonequarter /g;
  635:     $result =~ s/&(\#189|frac12);/\\textonehalf /g;
  636:     $result =~ s/&(\#190|frac34);/\\textthreequarters /g;
  637:     $result =~ s/&(\#191|iquest);/?\`/g;   
  638:     $result =~ s/&(\#192|Agrave);/\\\`{A}/g;  
  639:     $result =~ s/&(\#193|Aacute);/\\\'{A}/g; 
  640:     $result =~ s/&(\#194|Acirc);/\\^{A}/g;
  641:     $result =~ s/&(\#195|Atilde);/\\~{A}/g;
  642:     $result =~ s/&(\#196|Auml);/\\\"{A}/g; 
  643:     $result =~ s/&(\#197|Aring);/{\\AA}/g;
  644:     $result =~ s/&(\#198|AElig);/{\\AE}/g;
  645:     $result =~ s/&(\#199|Ccedil);/\\c{c}/g;
  646:     $result =~ s/&(\#200|Egrave);/\\\`{E}/g;  
  647:     $result =~ s/&(\#201|Eacute);/\\\'{E}/g;    
  648:     $result =~ s/&(\#202|Ecirc);/\\^{E}/g;
  649:     $result =~ s/&(\#203|Euml);/\\\"{E}/g;
  650:     $result =~ s/&(\#204|Igrave);/\\\`{I}/g;
  651:     $result =~ s/&(\#205|Iacute);/\\\'{I}/g;    
  652:     $result =~ s/&(\#206|Icirc);/\\^{I}/g;
  653:     $result =~ s/&(\#207|Iuml);/\\\"{I}/g;    
  654:     $result =~ s/&(\#209|Ntilde);/\\~{N}/g;
  655:     $result =~ s/&(\#210|Ograve);/\\\`{O}/g;
  656:     $result =~ s/&(\#211|Oacute);/\\\'{O}/g;
  657:     $result =~ s/&(\#212|Ocirc);/\\^{O}/g;
  658:     $result =~ s/&(\#213|Otilde);/\\~{O}/g;
  659:     $result =~ s/&(\#214|Ouml);/\\\"{O}/g;    
  660:     $result =~ s/&(\#215|times);/\\ensuremath\{\\times\}/g;
  661:     $result =~ s/&(\#216|Oslash);/{\\O}/g;
  662:     $result =~ s/&(\#217|Ugrave);/\\\`{U}/g;    
  663:     $result =~ s/&(\#218|Uacute);/\\\'{U}/g;
  664:     $result =~ s/&(\#219|Ucirc);/\\^{U}/g;
  665:     $result =~ s/&(\#220|Uuml);/\\\"{U}/g;
  666:     $result =~ s/&(\#221|Yacute);/\\\'{Y}/g;
  667:     $result =~ s/&(\#223|szlig);/{\\ss}/g;
  668:     $result =~ s/&(\#224|agrave);/\\\`{a}/g;
  669:     $result =~ s/&(\#225|aacute);/\\\'{a}/g;
  670:     $result =~ s/&(\#226|acirc);/\\^{a}/g;
  671:     $result =~ s/&(\#227|atilde);/\\~{a}/g;
  672:     $result =~ s/&(\#228|auml);/\\\"{a}/g;
  673:     $result =~ s/&(\#229|aring);/{\\aa}/g;
  674:     $result =~ s/&(\#230|aelig);/{\\ae}/g;
  675:     $result =~ s/&(\#231|ccedil);/\\c{c}/g;
  676:     $result =~ s/&(\#232|egrave);/\\\`{e}/g;
  677:     $result =~ s/&(\#233|eacute);/\\\'{e}/g;
  678:     $result =~ s/&(\#234|ecirc);/\\^{e}/g;
  679:     $result =~ s/&(\#235|euml);/\\\"{e}/g;
  680:     $result =~ s/&(\#236|igrave);/\\\`{i}/g;
  681:     $result =~ s/&(\#237|iacute);/\\\'{i}/g;
  682:     $result =~ s/&(\#238|icirc);/\\^{i}/g;
  683:     $result =~ s/&(\#239|iuml);/\\\"{i}/g;
  684:     $result =~ s/&(\#240|eth);/\\ensuremath\{\\partial\}/g;
  685:     $result =~ s/&(\#241|ntilde);/\\~{n}/g;
  686:     $result =~ s/&(\#242|ograve);/\\\`{o}/g;
  687:     $result =~ s/&(\#243|oacute);/\\\'{o}/g;
  688:     $result =~ s/&(\#244|ocirc);/\\^{o}/g;
  689:     $result =~ s/&(\#245|otilde);/\\~{o}/g;
  690:     $result =~ s/&(\#246|ouml);/\\\"{o}/g;
  691:     $result =~ s/&(\#247|divide);/\\ensuremath\{\\div\}/g;
  692:     $result =~ s/&(\#248|oslash);/{\\o}/g;
  693:     $result =~ s/&(\#249|ugrave);/\\\`{u}/g; 
  694:     $result =~ s/&(\#250|uacute);/\\\'{u}/g;
  695:     $result =~ s/&(\#251|ucirc);/\\^{u}/g;
  696:     $result =~ s/&(\#252|uuml);/\\\"{u}/g;
  697:     $result =~ s/&(\#253|yacute);/\\\'{y}/g;
  698:     $result =~ s/&(\#255|yuml);/\\\"{y}/g;
  699:     $result =~ s/&\#295;/\\ensuremath\{\\hbar\}/g;
  700:     $result =~ s/&\#952;/\\ensuremath\{\\theta\}/g;
  701: #Greek Alphabet
  702:     $result =~ s/&(alpha|\#945);/\\ensuremath\{\\alpha\}/g;
  703:     $result =~ s/&(beta|\#946);/\\ensuremath\{\\beta\}/g;
  704:     $result =~ s/&(gamma|\#947);/\\ensuremath\{\\gamma\}/g;
  705:     $result =~ s/&(delta|\#948);/\\ensuremath\{\\delta\}/g;
  706:     $result =~ s/&(epsilon|\#949);/\\ensuremath\{\\epsilon\}/g;
  707:     $result =~ s/&(zeta|\#950);/\\ensuremath\{\\zeta\}/g;
  708:     $result =~ s/&(eta|\#951);/\\ensuremath\{\\eta\}/g;
  709:     $result =~ s/&(theta|\#952);/\\ensuremath\{\\theta\}/g;
  710:     $result =~ s/&(iota|\#953);/\\ensuremath\{\\iota\}/g;
  711:     $result =~ s/&(kappa|\#954);/\\ensuremath\{\\kappa\}/g;
  712:     $result =~ s/&(lambda|\#955);/\\ensuremath\{\\lambda\}/g;
  713:     $result =~ s/&(mu|\#956);/\\ensuremath\{\\mu\}/g;
  714:     $result =~ s/&(nu|\#957);/\\ensuremath\{\\nu\}/g;
  715:     $result =~ s/&(xi|\#958);/\\ensuremath\{\\xi\}/g;
  716:     $result =~ s/&(omicron|\#959);/o/g;
  717:     $result =~ s/&(pi|\#960);/\\ensuremath\{\\pi\}/g;
  718:     $result =~ s/&(rho|\#961);/\\ensuremath\{\\rho\}/g;
  719:     $result =~ s/&(sigma|\#963);/\\ensuremath\{\\sigma\}/g;
  720:     $result =~ s/&(tau|\#964);/\\ensuremath\{\\tau\}/g;
  721:     $result =~ s/&(upsilon|\#965);/\\ensuremath\{\\upsilon\}/g;
  722:     $result =~ s/&(phi|\#966);/\\ensuremath\{\\phi\}/g;
  723:     $result =~ s/&(chi|\#967);/\\ensuremath\{\\chi\}/g;
  724:     $result =~ s/&(psi|\#968);/\\ensuremath\{\\psi\}/g;
  725:     $result =~ s/&(omega|\#969);/\\ensuremath\{\\omega\}/g;
  726:     $result =~ s/&(thetasym|\#977);/\\ensuremath\{\\vartheta\}/g;
  727:     $result =~ s/&(piv|\#982);/\\ensuremath\{\\varpi\}/g;
  728:     $result =~ s/&(Alpha|\#913);/A/g;
  729:     $result =~ s/&(Beta|\#914);/B/g;
  730:     $result =~ s/&(Gamma|\#915);/\\ensuremath\{\\Gamma\}/g;
  731:     $result =~ s/&(Delta|\#916);/\\ensuremath\{\\Delta\}/g;
  732:     $result =~ s/&(Epsilon|\#917);/E/g;
  733:     $result =~ s/&(Zeta|\#918);/Z/g;
  734:     $result =~ s/&(Eta|\#919);/H/g;
  735:     $result =~ s/&(Theta|\#920);/\\ensuremath\{\\Theta\}/g;
  736:     $result =~ s/&(Iota|\#921);/I/g;
  737:     $result =~ s/&(Kappa|\#922);/K/g;
  738:     $result =~ s/&(Lambda|\#923);/\\ensuremath\{\\Lambda\}/g;
  739:     $result =~ s/&(Mu|\#924);/M/g;
  740:     $result =~ s/&(Nu|\#925);/N/g;
  741:     $result =~ s/&(Xi|\#926);/\\ensuremath\{\\Xi\}/g;
  742:     $result =~ s/&(Omicron|\#927);/O/g;
  743:     $result =~ s/&(Pi|\#928);/\\ensuremath\{\\Pi\}/g;
  744:     $result =~ s/&(Rho|\#929);/P/g;
  745:     $result =~ s/&(Sigma|\#931);/\\ensuremath\{\\Sigma\}/g;
  746:     $result =~ s/&(Tau|\#932);/T/g;
  747:     $result =~ s/&(Upsilon|\#933);/\\ensuremath\{\\Upsilon\}/g;
  748:     $result =~ s/&(Phi|\#934);/\\ensuremath\{\\Phi\}/g;
  749:     $result =~ s/&(Chi|\#935);/X/g;
  750:     $result =~ s/&(Psi|\#936);/\\ensuremath\{\\Psi\}/g;
  751:     $result =~ s/&(Omega|\#937);/\\ensuremath\{\\Omega\}/g;
  752: #Arrows (extended HTML 4.01)
  753:     $result =~ s/&(larr|\#8592);/\\ensuremath\{\\leftarrow\}/g;
  754:     $result =~ s/&(uarr|\#8593);/\\ensuremath\{\\uparrow\}/g;
  755:     $result =~ s/&(rarr|\#8594);/\\ensuremath\{\\rightarrow\}/g;
  756:     $result =~ s/&(darr|\#8595);/\\ensuremath\{\\downarrow\}/g;
  757:     $result =~ s/&(harr|\#8596);/\\ensuremath\{\\leftrightarrow\}/g;
  758:     $result =~ s/&(lArr|\#8656);/\\ensuremath\{\\Leftarrow\}/g;
  759:     $result =~ s/&(uArr|\#8657);/\\ensuremath\{\\Uparrow\}/g;
  760:     $result =~ s/&(rArr|\#8658);/\\ensuremath\{\\Rightarrow\}/g;
  761:     $result =~ s/&(dArr|\#8659);/\\ensuremath\{\\Downarrow\}/g;
  762:     $result =~ s/&(hArr|\#8660);/\\ensuremath\{\\Leftrightarrow\}/g;
  763: #Mathematical Operators (extended HTML 4.01)
  764:     $result =~ s/&(forall|\#8704);/\\ensuremath\{\\forall\}/g;
  765:     $result =~ s/&(part|\#8706);/\\ensuremath\{\\partial\}/g;
  766:     $result =~ s/&(exist|\#8707);/\\ensuremath\{\\exists\}/g;
  767:     $result =~ s/&(empty|\#8709);/\\ensuremath\{\\emptyset\}/g;
  768:     $result =~ s/&(nabla|\#8711);/\\ensuremath\{\\nabla\}/g;
  769:     $result =~ s/&(isin|\#8712);/\\ensuremath\{\\in\}/g;
  770:     $result =~ s/&(notin|\#8713);/\\ensuremath\{\\notin\}/g;
  771:     $result =~ s/&(ni|\#8715);/\\ensuremath\{\\ni\}/g;
  772:     $result =~ s/&(prod|\#8719);/\\ensuremath\{\\prod\}/g;
  773:     $result =~ s/&(sum|\#8721);/\\ensuremath\{\\sum\}/g;
  774:     $result =~ s/&(minus|\#8722);/\\ensuremath\{-\}/g;
  775:     $result =~ s/–/\\ensuremath\{-\}/g;
  776:     $result =~ s/&(lowast|\#8727);/\\ensuremath\{*\}/g;
  777:     $result =~ s/&(radic|\#8730);/\\ensuremath\{\\surd\}/g;
  778:     $result =~ s/&(prop|\#8733);/\\ensuremath\{\\propto\}/g;
  779:     $result =~ s/&(infin|\#8734);/\\ensuremath\{\\infty\}/g;
  780:     $result =~ s/&(ang|\#8736);/\\ensuremath\{\\angle\}/g;
  781:     $result =~ s/&(and|\#8743);/\\ensuremath\{\\wedge\}/g;
  782:     $result =~ s/&(or|\#8744);/\\ensuremath\{\\vee\}/g;
  783:     $result =~ s/&(cap|\#8745);/\\ensuremath\{\\cap\}/g;
  784:     $result =~ s/&(cup|\#8746);/\\ensuremath\{\\cup\}/g;
  785:     $result =~ s/&(int|\#8747);/\\ensuremath\{\\int\}/g;
  786:     $result =~ s/&(sim|\#8764);/\\ensuremath\{\\sim\}/g;
  787:     $result =~ s/&(cong|\#8773);/\\ensuremath\{\\cong\}/g;
  788:     $result =~ s/&(asymp|\#8776);/\\ensuremath\{\\approx\}/g;
  789:     $result =~ s/&(ne|\#8800);/\\ensuremath\{\\not=\}/g;
  790:     $result =~ s/&(equiv|\#8801);/\\ensuremath\{\\equiv\}/g;
  791:     $result =~ s/&(le|\#8804);/\\ensuremath\{\\leq\}/g;
  792:     $result =~ s/&(ge|\#8805);/\\ensuremath\{\\geq\}/g;
  793:     $result =~ s/&(sub|\#8834);/\\ensuremath\{\\subset\}/g;
  794:     $result =~ s/&(sup|\#8835);/\\ensuremath\{\\supset\}/g;
  795:     $result =~ s/&(nsub|\#8836);/\\ensuremath\{\\not\\subset\}/g;
  796:     $result =~ s/&(sube|\#8838);/\\ensuremath\{\\subseteq\}/g;
  797:     $result =~ s/&(supe|\#8839);/\\ensuremath\{\\supseteq\}/g;
  798:     $result =~ s/&(oplus|\#8853);/\\ensuremath\{\\oplus\}/g;
  799:     $result =~ s/&(otimes|\#8855);/\\ensuremath\{\\otimes\}/g;
  800:     $result =~ s/&(perp|\#8869);/\\ensuremath\{\\perp\}/g;
  801:     $result =~ s/&(sdot|\#8901);/\\ensuremath\{\\cdot\}/g;
  802: #Geometric Shapes (extended HTML 4.01)
  803:     $result =~ s/&(loz|\#9674);/\\ensuremath\{\\Diamond\}/g;
  804: #Miscellaneous Symbols (extended HTML 4.01)
  805:     $result =~ s/&(spades|\#9824);/\\ensuremath\{\\spadesuit\}/g;
  806:     $result =~ s/&(clubs|\#9827);/\\ensuremath\{\\clubsuit\}/g;
  807:     $result =~ s/&(hearts|\#9829);/\\ensuremath\{\\heartsuit\}/g;
  808:     $result =~ s/&(diams|\#9830);/\\ensuremath\{\\diamondsuit\}/g;
  809: #   Chemically useful 'things' contributed by Hon Kie (bug 4652).
  810: 
  811:     $result =~ s/&\#8636;/\\ensuremath\{\\leftharpoonup\}/g;
  812:     $result =~ s/&\#8637;/\\ensuremath\{\\leftharpoondown\}/g;
  813:     $result =~ s/&\#8640;/\\ensuremath\{\\rightharpoonup\}/g;
  814:     $result =~ s/&\#8641;/\\ensuremath\{\\rightharpoondown\}/g;
  815:     $result =~ s/&\#8652;/\\ensuremath\{\\rightleftharpoons\}/g;
  816:     $result =~ s/&\#8605;/\\ensuremath\{\\leadsto\}/g;
  817:     $result =~ s/&\#8617;/\\ensuremath\{\\hookleftarrow\}/g;
  818:     $result =~ s/&\#8618;/\\ensuremath\{\\hookrightarrow\}/g;
  819:     $result =~ s/&\#8614;/\\ensuremath\{\\mapsto\}/g;
  820:     $result =~ s/&\#8599;/\\ensuremath\{\\nearrow\}/g;
  821:     $result =~ s/&\#8600;/\\ensuremath\{\\searrow\}/g;
  822:     $result =~ s/&\#8601;/\\ensuremath\{\\swarrow\}/g;
  823:     $result =~ s/&\#8598;/\\ensuremath\{\\nwarrow\}/g;
  824: 
  825:     # Left/right quotations:
  826: 
  827:     $result =~ s/&(ldquo|#8220);/\`\`/g;
  828:     $result =~ s/&(rdquo|#8221);/\'\'/g;
  829: 
  830: 
  831:     return $result;
  832: }
  833: 
  834: 
  835:                   #width, height, oddsidemargin, evensidemargin, topmargin
  836: my %page_formats=
  837:     ('letter' => {
  838: 	 'book' => {
  839: 	     '1' => [ '7.1 in','9.8 in', '-0.57 in','-0.57 in','0.275 in'],
  840: 	     '2' => ['3.66 in','9.8 in', '-0.57 in','-0.57 in','0.275 in']
  841: 	 },
  842: 	 'album' => {
  843: 	     '1' => [ '8.8 in', '6.8 in','-0.55 in',  '-0.55 in','0.394 in'],
  844: 	     '2' => [ '4.8 in', '6.8 in','-0.5 in', '-1.0 in','3.5 in']
  845: 	 },
  846:      },
  847:      'legal' => {
  848: 	 'book' => {
  849: 	     '1' => ['7.1 in','13 in',,'-0.57 in','-0.57 in','-0.5 in'],
  850: 	     '2' => ['3.66 in','13 in','-0.57 in','-0.57 in','-0.5 in']
  851: 	 },
  852: 	 'album' => {
  853: 	     '1' => ['12 in','7.1 in',,'-0.57 in','-0.57 in','-0.5 in'],
  854:              '2' => ['6.0 in','7.1 in','-1 in','-1 in','5 in']
  855:           },
  856:      },
  857:      'tabloid' => {
  858: 	 'book' => {
  859: 	     '1' => ['9.8 in','16 in','-0.57 in','-0.57 in','-0.5 in'],
  860: 	     '2' => ['4.9 in','16 in','-0.57 in','-0.57 in','-0.5 in']
  861: 	 },
  862: 	 'album' => {
  863: 	     '1' => ['16 in','9.8 in','-0.57 in','-0.57 in','-0.5 in'],
  864: 	     '2' => ['16 in','4.9 in','-0.57 in','-0.57 in','-0.5 in']
  865:           },
  866:      },
  867:      'executive' => {
  868: 	 'book' => {
  869: 	     '1' => ['6.8 in','9 in','-0.57 in','-0.57 in','1.2 in'],
  870: 	     '2' => ['3.1 in','9 in','-0.57 in','-0.57 in','1.2 in']
  871: 	 },
  872: 	 'album' => {
  873: 	     '1' => [],
  874: 	     '2' => []
  875:           },
  876:      },
  877:      'a2' => {
  878: 	 'book' => {
  879: 	     '1' => [],
  880: 	     '2' => []
  881: 	 },
  882: 	 'album' => {
  883: 	     '1' => [],
  884: 	     '2' => []
  885:           },
  886:      },
  887:      'a3' => {
  888: 	 'book' => {
  889: 	     '1' => [],
  890: 	     '2' => []
  891: 	 },
  892: 	 'album' => {
  893: 	     '1' => [],
  894: 	     '2' => []
  895:           },
  896:      },
  897:      'a4' => {
  898: 	 'book' => {
  899: 	     '1' => ['17.6 cm','27.2 cm','-1.397 cm','-2.11 cm','-1.27 cm'],
  900: 	     '2' => [ '9.1 cm','27.2 cm','-1.397 cm','-2.11 cm','-1.27 cm']
  901: 	 },
  902: 	 'album' => {
  903: 	     '1' => ['21.59 cm','19.558 cm','-1.397cm','-2.11 cm','0 cm'],
  904: 	     '2' => ['9.91 cm','19.558 cm','-1.397 cm','-2.11 cm','0 cm']
  905: 	 },
  906:      },
  907:      'a5' => {
  908: 	 'book' => {
  909: 	     '1' => [],
  910: 	     '2' => []
  911: 	 },
  912: 	 'album' => {
  913: 	     '1' => [],
  914: 	     '2' => []
  915:           },
  916:      },
  917:      'a6' => {
  918: 	 'book' => {
  919: 	     '1' => [],
  920: 	     '2' => []
  921: 	 },
  922: 	 'album' => {
  923: 	     '1' => [],
  924: 	     '2' => []
  925:           },
  926:      },
  927:      );
  928: 
  929: sub page_format {
  930: #
  931: #Supported paper format: "Letter [8 1/2x11 in]",      "Legal [8 1/2x14 in]",
  932: #                        "Ledger/Tabloid [11x17 in]", "Executive [7 1/2x10 in]",
  933: #                        "A2 [420x594 mm]",           "A3 [297x420 mm]",
  934: #                        "A4 [210x297 mm]",           "A5 [148x210 mm]",
  935: #                        "A6 [105x148 mm]"
  936: # 
  937:     my ($papersize,$layout,$numberofcolumns) = @_; 
  938:     return @{$page_formats{$papersize}->{$layout}->{$numberofcolumns}};
  939: }
  940: 
  941: 
  942: sub get_name {
  943:     my ($uname,$udom)=@_;
  944:     if (!defined($uname)) { $uname=$env{'user.name'}; }
  945:     if (!defined($udom)) { $udom=$env{'user.domain'}; }
  946:     my $plainname=&Apache::loncommon::plainname($uname,$udom);
  947:     if ($plainname=~/^\s*$/) { $plainname=$uname.'@'.$udom; }
  948:    $plainname=&Apache::lonxml::latex_special_symbols($plainname,'header');
  949:     return $plainname;
  950: }
  951: 
  952: sub get_course {
  953:     my $courseidinfo;
  954:     if (defined($env{'request.course.id'})) {
  955: 	$courseidinfo = &Apache::lonxml::latex_special_symbols(&unescape($env{'course.'.$env{'request.course.id'}.'.description'}),'header');
  956:     }
  957:     return $courseidinfo;
  958: }
  959: 
  960: sub page_format_transformation {
  961:     my ($papersize,$layout,$numberofcolumns,$choice,$text,$assignment,$tableofcontents,$indexlist,$selectionmade) = @_; 
  962:     my ($textwidth,$textheight,$oddoffset,$evenoffset,$topmargin);
  963: 
  964:     if ($selectionmade eq '4') {
  965: 	if ($choice eq 'all_problems') {
  966: 	    $assignment='Problems from the Whole Course';
  967: 	} else {
  968: 	    $assignment='Resources from the Whole Course';
  969: 	}
  970:     } else {
  971: 	$assignment=&Apache::lonxml::latex_special_symbols($assignment,'header');
  972:     }
  973:     ($textwidth,$textheight,$oddoffset,$evenoffset,$topmargin) = &page_format($papersize,$layout,$numberofcolumns,$topmargin);
  974: 
  975: 
  976:     my $name = &get_name();
  977:     my $courseidinfo = &get_course();
  978:     if (defined($courseidinfo)) { $courseidinfo=' - '.$courseidinfo }
  979:     my $header_text  = $parmhash{'print_header_format'};
  980:     $header_text     = &format_page_header($textwidth, $header_text, $assignment,
  981: 					   $courseidinfo, $name);
  982:     my $topmargintoinsert = '';
  983:     if ($topmargin ne '0') {$topmargintoinsert='\setlength{\topmargin}{'.$topmargin.'}';}
  984:     my $fancypagestatement='';
  985:     if ($numberofcolumns eq '2') {
  986: 	$fancypagestatement="\\fancyhead{}\\fancyhead[LO]{$header_text}";
  987:     } else {
  988: 	$fancypagestatement="\\rhead{}\\chead{}\\lhead{$header_text}";
  989:     }
  990:     if ($layout eq 'album') {
  991: 	    $text =~ s/\\begin{document}/\\setlength{\\oddsidemargin}{$oddoffset}\\setlength{\\evensidemargin}{$evenoffset}$topmargintoinsert\n\\setlength{\\textwidth}{$textwidth}\\setlength{\\textheight}{$textheight}\\setlength{\\textfloatsep}{8pt plus 2\.0pt minus 4\.0pt}\n\\newlength{\\minipagewidth}\\setlength{\\minipagewidth}{\\textwidth\/\$number_of_columns-0\.2cm}\\usepackage{fancyhdr}\\addtolength{\\headheight}{\\baselineskip}\n\\pagestyle{fancy}$fancypagestatement\\begin{document}\\voffset=-0\.8 cm\\setcounter{page}{1}\n /;
  992:     } elsif ($layout eq 'book') {
  993: 	if ($choice ne 'All class print') { 
  994: 	    $text =~ s/\\begin{document}/\\textheight $textheight\\oddsidemargin = $evenoffset\\evensidemargin = $evenoffset $topmargintoinsert\n\\textwidth= $textwidth\\newlength{\\minipagewidth}\\setlength{\\minipagewidth}{\\textwidth\/\$number_of_columns-0\.2cm}\n\\renewcommand{\\ref}{\\keephidden\}\\usepackage{fancyhdr}\\addtolength{\\headheight}{\\baselineskip}\\pagestyle{fancy}$fancypagestatement\\begin{document}\n\\voffset=-0\.8 cm\\setcounter{page}{1}\n/;
  995: 	} else {
  996: 	    $text =~ s/\\pagestyle{fancy}\\rhead{}\\chead{}\s*\\begin{document}/\\textheight = $textheight\\oddsidemargin = $evenoffset\n\\evensidemargin = $evenoffset $topmargintoinsert\\textwidth= $textwidth\\newlength{\\minipagewidth}\n\\setlength{\\minipagewidth}{\\textwidth\/\$number_of_columns-0\.2cm}\\renewcommand{\\ref}{\\keephidden\}\\pagestyle{fancy}\\rhead{}\\chead{}\\begin{document}\\voffset=-0\.8cm\n\\setcounter{page}{1}  \\vskip 5 mm\n /;
  997: 	}
  998: 	if ($papersize eq 'a4') {
  999: 	    $text =~ s/(\\begin{document})/$1\\special{papersize=210mm,297mm}/;
 1000: 	}
 1001:     }
 1002:     if ($tableofcontents eq 'yes') {$text=~s/(\\setcounter\{page\}\{1\})/$1 \\tableofcontents\\newpage /;}
 1003:     if ($indexlist eq 'yes') {
 1004: 	$text=~s/(\\begin{document})/\\makeindex $1/;
 1005: 	$text=~s/(\\end{document})/\\strut\\\\\\strut\\printindex $1/;
 1006:     }
 1007:     return $text;
 1008: }
 1009: 
 1010: 
 1011: sub page_cleanup {
 1012:     my $result = shift;	
 1013:  
 1014:     $result =~ m/\\end{document}(\d*)$/;
 1015:     my $number_of_columns = $1;
 1016:     my $insert = '{';
 1017:     for (my $id=1;$id<=$number_of_columns;$id++) { $insert .='l'; }
 1018:     $insert .= '}';
 1019:     $result =~ s/(\\begin{longtable})INSERTTHEHEADOFLONGTABLE\\endfirsthead\\endhead/$1$insert/g;
 1020:     $result =~ s/&\s*REMOVETHEHEADOFLONGTABLE\\\\/\\\\/g;
 1021:     return $result,$number_of_columns;
 1022: }
 1023: 
 1024: 
 1025: sub details_for_menu {
 1026:     my ($helper)=@_;
 1027:     my $postdata=$env{'form.postdata'};
 1028:     if (!$postdata) { $postdata=$helper->{VARS}{'postdata'}; }
 1029:     my $name_of_resource = &Apache::lonnet::gettitle($postdata);
 1030:     my $symbolic = &Apache::lonnet::symbread($postdata);
 1031:     return if ( $symbolic eq '');
 1032: 
 1033:     my ($map,$id,$resource)=&Apache::lonnet::decode_symb($symbolic);
 1034:     $map=&Apache::lonnet::clutter($map);
 1035:     my $name_of_sequence = &Apache::lonnet::gettitle($map);
 1036:     if ($name_of_sequence =~ /^\s*$/) {
 1037: 	$map =~ m|([^/]+)$|;
 1038: 	$name_of_sequence = $1;
 1039:     }
 1040:     my $name_of_map = &Apache::lonnet::gettitle($env{'request.course.uri'});
 1041:     if ($name_of_map =~ /^\s*$/) {
 1042: 	$env{'request.course.uri'} =~ m|([^/]+)$|;
 1043: 	$name_of_map = $1;
 1044:     }
 1045:     return ($name_of_resource,$name_of_sequence,$name_of_map);
 1046: }
 1047: 
 1048: sub copyright_line {
 1049:     return '\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\vspace*{-2 mm}\newline\noindent{\tiny Printed from LON-CAPA\copyright MSU{\hfill} Licensed under GNU General Public License } ';
 1050: }
 1051: my $end_of_student = "\n".'\special{ps:ENDOFSTUDENTSTAMP}'."\n";
 1052: 
 1053: sub latex_corrections {
 1054:     my ($number_of_columns,$result,$selectionmade,$answer_mode) = @_;
 1055: #    $result =~ s/\\includegraphics{/\\includegraphics\[width=\\minipagewidth\]{/g;
 1056:     my $copyright = &copyright_line();
 1057:     if ($selectionmade eq '1' || $answer_mode eq 'only') {
 1058: 	$result =~ s/(\\end{document})/\\strut\\vskip 0 mm $copyright $end_of_student $1/;
 1059:     } else {
 1060: 	$result =~ s/(\\end{document})/\\strut\\vspace\*{-4 mm}\\newline $copyright $end_of_student $1/;
 1061:     }
 1062:     $result =~ s/\$number_of_columns/$number_of_columns/g;
 1063:     $result =~ s/(\\end{longtable}\s*)(\\strut\\newline\\noindent\\makebox\[\\textwidth\/$number_of_columns\]\[b\]{\\hrulefill})/$2$1/g;
 1064:     $result =~ s/(\\end{longtable}\s*)\\strut\\newline/$1/g;
 1065: #-- LaTeX corrections     
 1066:     my $first_comment = index($result,'<!--',0);
 1067:     while ($first_comment != -1) {
 1068: 	my $end_comment = index($result,'-->',$first_comment);
 1069: 	substr($result,$first_comment,$end_comment-$first_comment+3) = '';
 1070: 	$first_comment = index($result,'<!--',$first_comment);
 1071:     }
 1072:     $result =~ s/^\s+$//gm; #remove empty lines
 1073:     #removes more than one empty space
 1074:     $result =~ s|(\s\s+)|($1=~/[\n\r]/)?"\n":" "|ge;
 1075:     $result =~ s/\\\\\s*\\vskip/\\vskip/gm;
 1076:     $result =~ s/\\\\\s*\\noindent\s*(\\\\)+/\\\\\\noindent /g;
 1077:     $result =~ s/{\\par }\s*\\\\/\\\\/gm;
 1078:     $result =~ s/\\\\\s+\[/ \[/g;
 1079:     #conversion of html characters to LaTeX equivalents
 1080:     if ($result =~ m/&(\w+|#\d+);/) {
 1081: 	$result = &character_chart($result);
 1082:     }
 1083:     $result =~ s/(\\end{tabular})\s*\\vskip 0 mm/$1/g;
 1084:     $result =~ s/(\\begin{enumerate})\s*\\noindent/$1/g;
 1085:     return $result;
 1086: }
 1087: 
 1088: 
 1089: sub index_table {
 1090:     my $currentURL = shift;
 1091:     my $insex_string='';
 1092:     $currentURL=~s/\.([^\/+])$/\.$1\.meta/;
 1093:     $insex_string=&Apache::lonnet::metadata($currentURL,'keywords');
 1094:     return $insex_string;
 1095: }
 1096: 
 1097: 
 1098: sub IndexCreation {
 1099:     my ($texversion,$currentURL)=@_;
 1100:     my @key_words=split(/,/,&index_table($currentURL));
 1101:     my $chunk='';
 1102:     my $st=index $texversion,'\addcontentsline{toc}{subsection}{';
 1103:     if ($st>0) {
 1104: 	for (my $i=0;$i<3;$i++) {$st=(index $texversion,'}',$st+1);}
 1105: 	$chunk=substr($texversion,0,$st+1);
 1106: 	substr($texversion,0,$st+1)=' ';
 1107:     }
 1108:     foreach my $key_word (@key_words) {
 1109: 	if ($key_word=~/\S+/) {
 1110: 	    $texversion=~s/\b($key_word)\b/$1 \\index{$key_word} /i;
 1111: 	}
 1112:     }			
 1113:     if ($st>0) {substr($texversion,0,1)=$chunk;}
 1114:     return $texversion;
 1115: }
 1116: 
 1117: sub print_latex_header {
 1118:     my $mode=shift;
 1119:     my $output='\documentclass[letterpaper,twoside]{article}\raggedbottom';
 1120:     if (($mode eq 'batchmode') || (!$perm{'pav'})) {
 1121: 	$output.='\batchmode';
 1122:     }
 1123:     $output.='\newcommand{\keephidden}[1]{}\renewcommand{\deg}{$^{\circ}$}'."\n".
 1124:    	    '\usepackage{multirow}'."\n".
 1125: 	     '\usepackage{longtable}\usepackage{textcomp}\usepackage{makeidx}'."\n".
 1126: 	     '\usepackage[dvips]{graphicx}\usepackage{epsfig}'."\n".
 1127: 	     '\usepackage{wrapfig}'.
 1128: 	     '\usepackage{picins}\usepackage{calc}'."\n".
 1129: 	     '\usepackage[utf8]{inputenc}'."\n".
 1130: 	     '\newenvironment{choicelist}{\begin{list}{}{\setlength{\rightmargin}{0in}'."\n".
 1131: 	     '\setlength{\leftmargin}{0.13in}\setlength{\topsep}{0.05in}'."\n".
 1132: 	     '\setlength{\itemsep}{0.022in}\setlength{\parsep}{0in}'."\n".
 1133: 	     '\setlength{\belowdisplayskip}{0.04in}\setlength{\abovedisplayskip}{0.05in}'."\n".
 1134: 	     '\setlength{\abovedisplayshortskip}{-0.04in}'."\n".
 1135: 	     '\setlength{\belowdisplayshortskip}{0.04in}}}{\end{list}}'."\n".
 1136: 	     '\renewenvironment{theindex}{\begin{list}{}{{\vskip 1mm \noindent \large'."\n".
 1137: 	     '\textbf{Index}} \newline \setlength{\rightmargin}{0in}'."\n".
 1138: 	     '\setlength{\leftmargin}{0.13in}\setlength{\topsep}{0.01in}'."\n".
 1139: 	     '\setlength{\itemsep}{0.1in}\setlength{\parsep}{-0.02in}'."\n".
 1140: 	     '\setlength{\belowdisplayskip}{0.01in}\setlength{\abovedisplayskip}{0.01in}'."\n".
 1141: 	     '\setlength{\abovedisplayshortskip}{-0.04in}'."\n".
 1142: 	     '\setlength{\belowdisplayshortskip}{0.01in}}}{\end{list}}\begin{document}'."\n";
 1143:     return $output;	     
 1144: }
 1145: 
 1146: sub path_to_problem {
 1147:     my ($urlp,$colwidth)=@_;
 1148:     $urlp=&Apache::lonnet::clutter($urlp);
 1149: 
 1150:     my $newurlp = '';
 1151:     $colwidth=~s/\s*mm\s*$//;
 1152: #characters average about 2 mm in width
 1153:     if (length($urlp)*2 > $colwidth) {
 1154: 	my @elements = split('/',$urlp);
 1155: 	my $curlength=0;
 1156: 	foreach my $element (@elements) {
 1157: 	    if ($element eq '') { next; }
 1158: 	    if ($curlength+(length($element)*2) > $colwidth) {
 1159: 		$newurlp .=  '|\vskip -1 mm \verb|';
 1160: 		$curlength=length($element)*2;
 1161: 	    } else {
 1162: 		$curlength+=length($element)*2;
 1163: 	    }
 1164: 	    $newurlp.='/'.$element;
 1165: 	}
 1166:     } else {
 1167: 	$newurlp=$urlp;
 1168:     }
 1169:     return '{\small\noindent\verb|'.$newurlp.'|\vskip 0 mm}';
 1170: }
 1171: 
 1172: sub recalcto_mm {
 1173:     my $textwidth=shift;
 1174:     my $LaTeXwidth;
 1175:     if ($textwidth=~/(-?\d+\.?\d*)\s*cm/) {
 1176: 	$LaTeXwidth = $1*10;
 1177:     } elsif ($textwidth=~/(-?\d+\.?\d*)\s*mm/) {
 1178: 	$LaTeXwidth = $1;
 1179:     } elsif ($textwidth=~/(-?\d+\.?\d*)\s*in/) {
 1180: 	$LaTeXwidth = $1*25.4;
 1181:     }
 1182:     $LaTeXwidth.=' mm';
 1183:     return $LaTeXwidth;
 1184: }
 1185: 
 1186: sub get_textwidth {
 1187:     my ($helper,$LaTeXwidth)=@_;
 1188:     my $textwidth=$LaTeXwidth;
 1189:     if ($helper->{'VARS'}->{'pagesize.width'}=~/\d+/ &&
 1190: 	$helper->{'VARS'}->{'pagesize.widthunit'}=~/\w+/) {
 1191: 	$textwidth=&recalcto_mm($helper->{'VARS'}->{'pagesize.width'}.' '.
 1192: 				$helper->{'VARS'}->{'pagesize.widthunit'});
 1193:     }
 1194:     return $textwidth;
 1195: }
 1196: 
 1197: 
 1198: sub unsupported {
 1199:     my ($currentURL,$mode,$symb)=@_;
 1200:     if ($mode ne '') {$mode='\\'.$mode}
 1201:     my $result.= &print_latex_header($mode);
 1202:     if ($currentURL=~m|^(/adm/wrapper/)?ext/|) {
 1203: 	$currentURL=~s|^(/adm/wrapper/)?ext/|http://|;
 1204: 	my $title=&Apache::lonnet::gettitle($symb);
 1205: 	$title = &Apache::lonxml::latex_special_symbols($title);
 1206: 	$result.=' \strut \\\\ '.$title.' \strut \\\\ '.$currentURL.' ';
 1207:     } else {
 1208: 	$result.=$currentURL;
 1209:     }
 1210:     $result.= '\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill} \end{document}';
 1211:     return $result;
 1212: }
 1213: 
 1214: 
 1215: #
 1216: # List of recently generated print files
 1217: #
 1218: sub recently_generated {
 1219:     my $r=shift;
 1220:     my $prtspool=$r->dir_config('lonPrtDir');
 1221:     my $zip_result;
 1222:     my $pdf_result;
 1223:     opendir(DIR,$prtspool);
 1224: 
 1225:     my @files = 
 1226: 	grep(/^$env{'user.name'}_$env{'user.domain'}_printout_(\d+)_.*\.(pdf|zip)$/,readdir(DIR));
 1227:     closedir(DIR);
 1228: 
 1229:     @files = sort {
 1230: 	my ($actime) = (stat($prtspool.'/'.$a))[10];
 1231: 	my ($bctime) = (stat($prtspool.'/'.$b))[10];
 1232: 	return $bctime <=> $actime;
 1233:     } (@files);
 1234: 
 1235:     foreach my $filename (@files) {
 1236: 	my ($ext) = ($filename =~ m/(pdf|zip)$/);
 1237: 	my ($cdev,$cino,$cmode,$cnlink,
 1238: 	    $cuid,$cgid,$crdev,$csize,
 1239: 	    $catime,$cmtime,$cctime,
 1240: 	    $cblksize,$cblocks)=stat($prtspool.'/'.$filename);
 1241: 	my $result="<a href='/prtspool/$filename'>".
 1242: 	    &mt('Generated [_1] ([_2] bytes)',
 1243: 		&Apache::lonlocal::locallocaltime($cctime),$csize).
 1244: 		'</a><br />';
 1245: 	if ($ext eq 'pdf') { $pdf_result .= $result; }
 1246: 	if ($ext eq 'zip') { $zip_result .= $result; }
 1247:     }
 1248:     if ($zip_result) {
 1249: 	$r->print('<h4>'.&mt('Recently generated printout zip files')."</h4>\n"
 1250: 		  .$zip_result);
 1251:     }
 1252:     if ($pdf_result) {
 1253: 	$r->print('<h4>'.&mt('Recently generated printouts')."</h4>\n"
 1254: 		  .$pdf_result);
 1255:     }
 1256: }
 1257: 
 1258: #
 1259: #   Retrieve the hash of page breaks.
 1260: #
 1261: #  Inputs:
 1262: #    helper   - reference to helper object.
 1263: #  Outputs
 1264: #    A reference to a page break hash.
 1265: #
 1266: #
 1267: #use Data::Dumper;
 1268: #sub dump_helper_vars {
 1269: #    my ($helper) = @_;
 1270: #    my $helpervars = Dumper($helper->{'VARS'});
 1271: #    &Apache::lonnet::logthis("Dump of helper vars:\n $helpervars");
 1272: #}
 1273: 
 1274: sub get_page_breaks  {
 1275:     my ($helper) = @_;
 1276:     my %page_breaks;
 1277: 
 1278:     foreach my $break (split /\|\|\|/, $helper->{'VARS'}->{'FINISHPAGE'}) {
 1279: 	$page_breaks{$break} = 1;
 1280:     }
 1281:     return %page_breaks;
 1282: }
 1283: 
 1284: #  Output a sequence (recursively if neeed)
 1285: #  from construction space.
 1286: # Parameters:
 1287: #    url     = URL of the sequence to print.
 1288: #    helper  - Reference to the helper hash.
 1289: #    form    - Copy of the format hash.
 1290: #    LaTeXWidth
 1291: # Returns:
 1292: #   Text to add to the printout.
 1293: #   NOTE if the first element of the outermost sequence
 1294: #   is itself a sequence, the outermost caller may need to
 1295: #   prefix the latex with the page headers stuff.
 1296: #
 1297: sub print_construction_sequence {
 1298:     my ($currentURL, $helper, %form, $LaTeXwidth) = @_;
 1299:     my $result;
 1300:     my $rndseed=time;
 1301:     if ($helper->{'VARS'}->{'curseed'}) {
 1302: 	$rndseed=$helper->{'VARS'}->{'curseed'};
 1303:     }
 1304:     my $errtext=&LONCAPA::map::mapread($currentURL);
 1305:     # 
 1306:     #  These make this all support recursing for subsequences.
 1307:     #
 1308:     my @order    = @LONCAPA::map::order;
 1309:     my @resources = @LONCAPA::map::resources; 
 1310:     for (my $member=0;$member<=$#order;$member++) {
 1311: 	$resources[$order[$member]]=~/^([^:]*):([^:]*):/;
 1312: 	my $urlp=$2;
 1313: 	if ($urlp=~/\.(problem|exam|quiz|assess|survey|form|library|xml|html|htm|xhtml|xhtm)$/) {
 1314: 	    my $texversion='';
 1315: 	    if ($helper->{'VARS'}->{'ANSWER_TYPE'} ne 'only') {
 1316: 		$form{'problem_split'}=$parmhash{'problem_stream_switch'};
 1317: 		$form{'suppress_tries'}=$parmhash{'suppress_tries'};
 1318: 		$form{'latex_type'}=$helper->{'VARS'}->{'LATEX_TYPE'};
 1319: 		$form{'rndseed'}=$rndseed;
 1320: 		$resources_printed .=$urlp.':';
 1321: 		$texversion=&ssi_with_retries($urlp, $ssi_retry_count, %form);
 1322: 	    }
 1323: 	    if((($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') ||
 1324: 		($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'only')) && 
 1325: 	       ($urlp=~/\.(problem|exam|quiz|assess|survey|form|library|page)$/)) {
 1326: 		#  Don't permanently modify %$form...
 1327: 		my %answerform = %form;
 1328: 		$answerform{'grade_target'}='answer';
 1329: 		$answerform{'answer_output_mode'}='tex';
 1330: 		$answerform{'rndseed'}=$rndseed;
 1331: 		$answerform{'problem_split'}=$parmhash{'problem_stream_switch'};
 1332: 		if ($urlp=~/\/res\//) {$env{'request.state'}='published';}
 1333: 		$resources_printed .= $urlp.':';
 1334: 		my $answer=&ssi_with_retries($urlp, $ssi_retry_count, %answerform);
 1335: 		if ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') {
 1336: 		    $texversion=~s/(\\keephidden{ENDOFPROBLEM})/$answer$1/;
 1337: 		} else {
 1338: 		    # If necessary, encapsulate answer in minipage:
 1339: 		    
 1340: 		    $texversion=&print_latex_header($helper->{'VARS'}->{'LATEX_TYPE'});
 1341: 		    my $title = &Apache::lonnet::gettitle($helper->{'VARS'}->{'symb'});
 1342: 		    $title = &Apache::lonxml::latex_special_symbols($title);
 1343: 		    my $body ='\vskip 0 mm \noindent\textbf{'.$title.'}\vskip 0 mm ';
 1344: 		    $body.=&path_to_problem($urlp,$LaTeXwidth);
 1345: 		    $body.='\vskip 1 mm '.$answer.'\end{document}';
 1346: 		    $body = &encapsulate_minipage($body);
 1347: 		    $texversion.=$body;
 1348: 		}
 1349: 	    }
 1350: 	    $texversion = &latex_header_footer_remove($texversion);
 1351: 
 1352: 	    if ($helper->{'VARS'}->{'TABLE_INDEX'} eq 'yes') {
 1353: 		$texversion=&IndexCreation($texversion,$urlp);
 1354: 	    }
 1355: 	    if ($helper->{'VARS'}->{'CONSTR_RESOURSE_URL'} eq 'yes') {
 1356: 		$texversion=~s/(\\addcontentsline\{toc\}\{subsection\}\{[^\}]*\})/$1 URL: \\verb|$urlp| \\strut\\\\\\strut /;
 1357: 	    }
 1358: 	    $result.=$texversion;
 1359: 
 1360: 	} elsif ($urlp=~/\.(sequence|page)$/) {
 1361: 	    
 1362: 	    # header:
 1363: 
 1364: 	    $result.='\strut\newline\noindent Sequence/page '.$urlp.'\strut\newline\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\newline\noindent ';
 1365: 
 1366: 	    # IF sequence, recurse:
 1367: 	    
 1368: 	    if ($urlp =~ /\.sequence$/) {
 1369: 		my $sequence_url = $urlp;
 1370: 		my $domain       = $env{'user.domain'};	# Constr. space only on local
 1371: 		my $user         = $env{'user.name'};
 1372: 
 1373: 		$sequence_url    =~ s/^\/res\/$domain/\/home/;
 1374: 		$sequence_url    =~ s/^(\/home\/$user)/$1\/public_html/;
 1375: #		$sequence_url    =~ s|\/~([^\/]+)\/|\/home\/$1\/public_html\/|;
 1376: 		$result .= &print_construction_sequence($sequence_url, 
 1377: 							$helper, %form, 
 1378: 							$LaTeXwidth);
 1379: 	    }
 1380: 	}  
 1381:     }
 1382:     if ($helper->{VARS}->{'construction'} eq '1') {$result=~s/(\\begin{document})/$1 \\fbox\{RANDOM SEED IS $rndseed\} /;}
 1383:     return $result;
 1384: }
 1385: 
 1386: sub output_data {
 1387:     my ($r,$helper,$rparmhash) = @_;
 1388:     my %parmhash = %$rparmhash;
 1389:     $ssi_error = 0;		# This will be set nonzero by failing ssi's.
 1390:     $resources_printed = '';
 1391:     my $do_postprocessing = 1;
 1392:     my $js = <<ENDPART;
 1393: <script type="text/javascript">
 1394:     var editbrowser;
 1395:     function openbrowser(formname,elementname,only,omit) {
 1396:         var url = '/res/?';
 1397:         if (editbrowser == null) {
 1398:             url += 'launch=1&';
 1399:         }
 1400:         url += 'catalogmode=interactive&';
 1401:         url += 'mode=parmset&';
 1402:         url += 'form=' + formname + '&';
 1403:         if (only != null) {
 1404:             url += 'only=' + only + '&';
 1405:         } 
 1406:         if (omit != null) {
 1407:             url += 'omit=' + omit + '&';
 1408:         }
 1409:         url += 'element=' + elementname + '';
 1410:         var title = 'Browser';
 1411:         var options = 'scrollbars=1,resizable=1,menubar=0';
 1412:         options += ',width=700,height=600';
 1413:         editbrowser = open(url,title,options,'1');
 1414:         editbrowser.focus();
 1415:     }
 1416: </script>
 1417: ENDPART
 1418: 
 1419: 
 1420: 
 1421:     my $start_page  = &Apache::loncommon::start_page('Preparing Printout',$js);
 1422:     my $msg = &mt('Please stand by while processing your print request, this may take some time ...');
 1423: 
 1424:     $r->print($start_page."\n<p>\n$msg\n</p>\n");
 1425: 
 1426:     # fetch the pagebreaks and store them in the course environment
 1427:     # The page breaks will be pulled into the hash %page_breaks which is
 1428:     # indexed by symb and contains 1's for each break.
 1429: 
 1430:     $env{'form.pagebreaks'}  = $helper->{'VARS'}->{'FINISHPAGE'};
 1431:     $env{'form.lastprinttype'} = $helper->{'VARS'}->{'PRINT_TYPE'}; 
 1432:     &Apache::loncommon::store_course_settings('print',
 1433: 					      {'pagebreaks'    => 'scalar',
 1434: 					       'lastprinttype' => 'scalar'});
 1435: 
 1436:     my %page_breaks  = &get_page_breaks($helper);
 1437: 
 1438:     my $format_from_helper = $helper->{'VARS'}->{'FORMAT'};
 1439:     my ($result,$selectionmade) = ('','');
 1440:     my $number_of_columns = 1; #used only for pages to determine the width of the cell
 1441:     my @temporary_array=split /\|/,$format_from_helper;
 1442:     my ($laystyle,$numberofcolumns,$papersize)=@temporary_array;
 1443:     if ($laystyle eq 'L') {
 1444: 	$laystyle='album';
 1445:     } else {
 1446: 	$laystyle='book';
 1447:     }
 1448:     my ($textwidth,$textheight,$oddoffset,$evenoffset) = &page_format($papersize,$laystyle,$numberofcolumns);
 1449:     my $assignment =  $env{'form.assignment'};
 1450:     my $LaTeXwidth=&recalcto_mm($textwidth); 
 1451:     my @print_array=();
 1452:     my @student_names=();
 1453: 
 1454:      
 1455:     #  Common settings for the %form has:
 1456:     # In some cases these settings get overriddent by specific cases, but the
 1457:     # settings are common enough to make it worthwhile factoring them out
 1458:     # here.
 1459:     #
 1460:     my %form;
 1461:     $form{'grade_target'} = 'tex';
 1462:     $form{'textwidth'}    = &get_textwidth($helper, $LaTeXwidth);
 1463: 
 1464:     # If form.showallfoils is set, then request all foils be shown:
 1465:     # privilege will be enforced both by not allowing the 
 1466:     # check box selecting this option to be presnt unless it's ok,
 1467:     # and by lonresponse's priv. check.
 1468:     # The if is here because lonresponse.pm only cares that
 1469:     # showallfoils is defined, not what the value is.
 1470: 
 1471:     if ($helper->{'VARS'}->{'showallfoils'} eq "1") { 
 1472: 	$form{'showallfoils'} = $helper->{'VARS'}->{'showallfoils'};
 1473:     }
 1474:     
 1475:     if ($helper->{'VARS'}->{'style_file'}=~/\w/) {
 1476: 	&Apache::lonnet::appenv({'construct.style' =>
 1477: 				$helper->{'VARS'}->{'style_file'}});
 1478:     } elsif ($env{'construct.style'}) {
 1479: 	&Apache::lonnet::delenv('construct\\.style');
 1480:     }
 1481: 
 1482: 
 1483:     if ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'current_document') {
 1484:       #-- single document - problem, page, html, xml, ...
 1485: 	my ($currentURL,$cleanURL);
 1486: 
 1487: 	if ($helper->{'VARS'}->{'construction'} ne '1') {
 1488:             #prints published resource
 1489: 	    $currentURL=$helper->{'VARS'}->{'postdata'};
 1490: 	    $cleanURL=&Apache::lonenc::check_decrypt($currentURL);
 1491: 	} else {
 1492: 
 1493:             #prints resource from the construction space
 1494: 	    $currentURL='/'.$helper->{'VARS'}->{'filename'};
 1495: 	    if ($currentURL=~/([^?]+)/) {$currentURL=$1;}
 1496: 	    $cleanURL=$currentURL;
 1497: 	}
 1498: 	$selectionmade = 1;
 1499: 	if ($cleanURL!~m|^/adm/|
 1500: 	    && $cleanURL=~/\.(problem|exam|quiz|assess|survey|form|library|page|xml|html|htm|xhtml|xhtm)$/) {
 1501: 	    my $rndseed=time;
 1502: 	    my $texversion='';
 1503: 	    if ($helper->{'VARS'}->{'ANSWER_TYPE'} ne 'only') {
 1504: 		my %moreenv;
 1505: 		$moreenv{'request.filename'}=$cleanURL;
 1506:                 if ($helper->{'VARS'}->{'probstatus'} eq 'exam') {$form{'problemtype'}='exam';}
 1507: 		$form{'problem_split'}=$parmhash{'problem_stream_switch'};
 1508: 		$form{'suppress_tries'}=$parmhash{'suppress_tries'};
 1509: 		$form{'latex_type'}=$helper->{'VARS'}->{'LATEX_TYPE'};
 1510: 		$form{'print_discussions'}=$helper->{'VARS'}->{'PRINT_DISCUSSIONS'};
 1511: 		$form{'print_annotations'}=$helper->{'VARS'}->{'PRINT_ANNOTATIONS'};
 1512: 		if (($helper->{'VARS'}->{'PRINT_DISCUSSIONS'} eq 'yes') ||
 1513: 		    ($helper->{'VARS'}->{'PRINT_ANNOTATIONS'} eq 'yes')) {
 1514: 		    $form{'problem_split'}='yes';
 1515: 		}
 1516: 		if ($helper->{'VARS'}->{'curseed'}) {
 1517: 		    $rndseed=$helper->{'VARS'}->{'curseed'};
 1518: 		}
 1519: 		$form{'rndseed'}=$rndseed;
 1520: 		&Apache::lonnet::appenv(\%moreenv);
 1521: 
 1522: 		&Apache::lonxml::clear_problem_counter();
 1523: 
 1524: 		$resources_printed .= $currentURL.':';
 1525: 		$texversion.=&ssi_with_retries($currentURL,$ssi_retry_count, %form);
 1526: 
 1527: 		#  Add annotations if required:
 1528: 	    
 1529: 		&Apache::lonxml::clear_problem_counter();
 1530: 
 1531: 		&Apache::lonnet::delenv('request.filename');
 1532: 	    }
 1533: 	    # current document with answers.. no need to encap in minipage
 1534: 	    #  since there's only one answer.
 1535: 
 1536: 	    if(($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') ||
 1537: 	       ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'only')) {
 1538: 		$form{'problem_split'}=$parmhash{'problem_stream_switch'};
 1539: 		$form{'grade_target'}='answer';
 1540: 		$form{'answer_output_mode'}='tex';
 1541: 		$form{'rndseed'}=$rndseed;
 1542:                 if ($helper->{'VARS'}->{'probstatus'} eq 'exam') {
 1543: 		    $form{'problemtype'}='exam';
 1544: 		}
 1545: 		$resources_printed .= $currentURL.':';
 1546: 		my $answer=&ssi_with_retries($currentURL,$ssi_retry_count, %form);
 1547: 		
 1548: 
 1549: 		if ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') {
 1550: 		    $texversion=~s/(\\keephidden{ENDOFPROBLEM})/$answer$1/;
 1551: 		} else {
 1552: 		    $texversion=&print_latex_header($helper->{'VARS'}->{'LATEX_TYPE'});
 1553: 		    if ($helper->{'VARS'}->{'construction'} ne '1') {
 1554: 			my $title = &Apache::lonnet::gettitle($helper->{'VARS'}->{'symb'});
 1555: 			$title = &Apache::lonxml::latex_special_symbols($title);
 1556: 			$texversion.='\vskip 0 mm \noindent\textbf{'.$title.'}\vskip 0 mm ';
 1557: 			$texversion.=&path_to_problem($cleanURL,$LaTeXwidth);
 1558: 		    } else {
 1559: 			$texversion.='\vskip 0 mm \noindent\textbf{Prints from construction space - there is no title.}\vskip 0 mm ';
 1560: 			my $URLpath=$cleanURL;
 1561: 			$URLpath=~s/~([^\/]+)/public_html\/$1\/$1/;
 1562: 			$texversion.=&path_to_problem($URLpath,$LaTeXwidth);
 1563: 		    }
 1564: 		    $texversion.='\vskip 1 mm '.$answer.'\end{document}';
 1565: 		}
 1566: 
 1567: 
 1568: 		
 1569: 
 1570: 	    }
 1571: 	    # Print annotations.
 1572: 
 1573: 
 1574: 	    if ($helper->{'VARS'}->{'PRINT_ANNOTATIONS'} eq 'yes') {
 1575: 		my $annotation .= &annotate($currentURL);
 1576: 		$texversion =~ s/(\\keephidden{ENDOFPROBLEM})/$annotation$1/;
 1577: 	    }
 1578: 
 1579: 
 1580: 	    if ($helper->{'VARS'}->{'TABLE_INDEX'} eq 'yes') {
 1581: 		$texversion=&IndexCreation($texversion,$currentURL);
 1582: 	    }
 1583: 	    if ($helper->{'VARS'}->{'CONSTR_RESOURSE_URL'} eq 'yes') {
 1584: 		$texversion=~s/(\\addcontentsline\{toc\}\{subsection\}\{[^\}]*\})/$1 URL: \\verb|$currentURL| \\strut\\\\\\strut /;
 1585: 
 1586: 	    }
 1587: 	    $result .= $texversion;
 1588: 	    if ($currentURL=~m/\.page\s*$/) {
 1589: 		($result,$number_of_columns) = &page_cleanup($result);
 1590: 	    }
 1591:         } elsif ($cleanURL!~m|^/adm/|
 1592: 		 && $currentURL=~/\.sequence$/ && $helper->{'VARS'}->{'construction'} eq '1') {
 1593:             #printing content of sequence from the construction space	
 1594: 	    $currentURL=~s|\/~([^\/]+)\/|\/home\/$1\/public_html\/|;
 1595: 	    $result .= &print_latex_header($helper->{'VARS'}->{'LATEX_TYPE'});
 1596: 	    $result .= &print_construction_sequence($currentURL, $helper, %form,
 1597: 						    $LaTeXwidth);
 1598: 	    $result .= '\end{document}';  
 1599: 	    if (!($result =~ /\\begin\{document\}/)) {
 1600: 		$result = &print_latex_header() . $result;
 1601: 	    }
 1602: 	    # End construction space sequence.
 1603: 	} elsif ($cleanURL=~/\/(smppg|syllabus|aboutme|bulletinboard)$/) { 
 1604: 		$form{'latex_type'}=$helper->{'VARS'}->{'LATEX_TYPE'};
 1605: 		if ($currentURL=~/\/syllabus$/) {$currentURL=~s/\/res//;}
 1606: 		$resources_printed .= $currentURL.':';
 1607: 		my $texversion=&ssi_with_retries($currentURL, $ssi_retry_count, %form);
 1608: 		if ($helper->{'VARS'}->{'PRINT_ANNOTATIONS'} eq 'yes') {
 1609: 		    my $annotation = &annotate($currentURL);
 1610: 		    $texversion    =~ s/(\\end{document})/$annotation$1/;
 1611: 		}
 1612: 		$result .= $texversion;
 1613: 	} elsif ($cleanURL =~/.tex$/) {
 1614: 	    # For this sort of print of a single LaTeX file,
 1615: 	    # We can just print the LaTeX file as it is uninterpreted in any way:
 1616: 	    #
 1617: 
 1618: 	    $result = &fetch_raw_resource($currentURL);
 1619: 	    if ($helper->{'VARS'}->{'PRINT_ANNOTATIONS'} eq 'yes') {
 1620: 		my $annotation = &annotate($currentURL);
 1621: 		$result =~ s/(\\end{document})/$annotation$1/;
 1622: 	    }
 1623: 
 1624: 	    $do_postprocessing = 0; # Don't massage the result.
 1625: 
 1626: 	} else {
 1627: 	    $result.=&unsupported($currentURL,$helper->{'VARS'}->{'LATEX_TYPE'},
 1628: 				  $helper->{'VARS'}->{'symb'});
 1629: 	}
 1630:     } elsif (($helper->{'VARS'}->{'PRINT_TYPE'} eq 'map_problems')       or
 1631:              ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'map_problems_pages') or
 1632:              ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'all_problems')       or
 1633: 	     ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'all_resources')      or # BUGBUG
 1634: 	     ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'select_sequences')) { 
 1635: 
 1636: 
 1637:         #-- produce an output string
 1638: 	if ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'map_problems') {
 1639: 	    $selectionmade = 2;
 1640: 	} elsif ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'map_problems_pages') {
 1641: 	    $selectionmade = 3;
 1642: 	} elsif ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'all_problems') {
 1643: 	    $selectionmade = 4;
 1644: 	} elsif ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'all_resources') {  #BUGBUG
 1645: 	    $selectionmade = 4;
 1646: 	} elsif ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'select_sequences') {
 1647: 	    $selectionmade = 7;
 1648: 	}
 1649: 	$form{'problem_split'}=$parmhash{'problem_stream_switch'};
 1650: 	$form{'suppress_tries'}=$parmhash{'suppress_tries'};
 1651: 	$form{'latex_type'}=$helper->{'VARS'}->{'LATEX_TYPE'};
 1652: 	$form{'print_discussions'}=$helper->{'VARS'}->{'PRINT_DISCUSSIONS'};
 1653: 	$form{'print_annotations'} = $helper->{'VARS'}->{'PRINT_ANNOTATIONS'};
 1654: 	if (($helper->{'VARS'}->{'PRINT_DISCUSSIONS'} eq 'yes')   ||
 1655: 	    ($helper->{'VARS'}->{'PRINT_ANNOTATIONS'} eq 'yes') ) {
 1656: 	    $form{'problem_split'}='yes';
 1657: 	}
 1658: 	my $flag_latex_header_remove = 'NO';
 1659: 	my $flag_page_in_sequence = 'NO';
 1660: 	my @master_seq=split /\|\|\|/, $helper->{'VARS'}->{'RESOURCES'};
 1661: 	my $prevassignment='';
 1662: 
 1663: 	&Apache::lonxml::clear_problem_counter();
 1664: 
 1665: 	my $pbreakresources = keys %page_breaks;
 1666: 	for (my $i=0;$i<=$#master_seq;$i++) {
 1667: 
 1668: 	    # Note due to document structure, not allowed to put \newpage
 1669: 	    # prior to the first resource
 1670: 
 1671: 	    if (defined $page_breaks{$master_seq[$i]}) {
 1672: 		if($i != 0) {
 1673: 		    $result.="\\newpage\n";
 1674: 		}
 1675: 	    }
 1676: 	    my ($sequence,undef,$urlp)=&Apache::lonnet::decode_symb($master_seq[$i]);
 1677: 	    $urlp=&Apache::lonnet::clutter($urlp);
 1678: 	    $form{'symb'}=$master_seq[$i];
 1679: 
 1680: 	    my $assignment=&Apache::lonxml::latex_special_symbols(&Apache::lonnet::gettitle($sequence),'header'); #title of the assignment which contains this problem
 1681: 	    if ($selectionmade==7) {$helper->{VARS}->{'assignment'}=$assignment;}
 1682: 	    if ($i==0) {$prevassignment=$assignment;}
 1683: 	    my $texversion='';
 1684: 	    if ($urlp!~m|^/adm/|
 1685: 		&& $urlp=~/\.(problem|exam|quiz|assess|survey|form|library|page|xml|html|htm|xhtml|xhtm)$/) {
 1686: 		$resources_printed .= $urlp.':';
 1687: 		&Apache::lonxml::remember_problem_counter();
 1688: 		$texversion.=&ssi_with_retries($urlp, $ssi_retry_count, %form);
 1689: 		if ($urlp=~/\.page$/) {
 1690: 		    ($texversion,my $number_of_columns_page) = &page_cleanup($texversion);
 1691: 		    if ($number_of_columns_page > $number_of_columns) {$number_of_columns=$number_of_columns_page;} 
 1692: 		    $texversion =~ s/\\end{document}\d*/\\end{document}/;
 1693: 		    $flag_page_in_sequence = 'YES';
 1694: 		} 
 1695: 
 1696: 		if(($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') ||
 1697: 		   ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'only')) {
 1698: 		    #  Don't permanently pervert the %form hash
 1699: 		    my %answerform = %form;
 1700: 		    $answerform{'grade_target'}='answer';
 1701: 		    $answerform{'answer_output_mode'}='tex';
 1702: 		    $resources_printed .= $urlp.':';
 1703: 
 1704: 		    &Apache::lonxml::restore_problem_counter();
 1705: 		    my $answer=&ssi_with_retries($urlp, $ssi_retry_count, %answerform);
 1706: 
 1707: 		    if ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') {
 1708: 			$texversion=~s/(\\keephidden{ENDOFPROBLEM})/$answer$1/;
 1709: 		    } else {
 1710: 			if ($urlp=~/\.(problem|exam|quiz|assess|survey|form|library)$/) {
 1711: 			    $texversion=&print_latex_header($helper->{'VARS'}->{'LATEX_TYPE'});
 1712: 			    my $title = &Apache::lonnet::gettitle($master_seq[$i]);
 1713: 			    $title = &Apache::lonxml::latex_special_symbols($title);
 1714: 			    my $body ='\vskip 0 mm \noindent\textbf{'.$title.'}\vskip 0 mm ';
 1715: 			    $body   .= &path_to_problem ($urlp,$LaTeXwidth);
 1716: 			    $body   .='\vskip 1 mm '.$answer;
 1717: 			    $body    = &encapsulate_minipage($body);
 1718: 			    $texversion .= $body;
 1719: 			} else {
 1720: 			    $texversion='';
 1721: 			}
 1722: 		    }
 1723: 
 1724: 		}
 1725: 		if ($helper->{'VARS'}->{'PRINT_ANNOTATIONS'} eq 'yes') {
 1726: 		    my $annotation .= &annotate($urlp);
 1727: 		    $texversion =~ s/(\\keephidden{ENDOFPROBLEM})/$annotation$1/;
 1728: 		}
 1729: 
 1730: 		if ($flag_latex_header_remove ne 'NO') {
 1731: 		    $texversion = &latex_header_footer_remove($texversion);
 1732: 		} else {
 1733: 		    $texversion =~ s/\\end{document}//;
 1734: 		}
 1735: 		if ($helper->{'VARS'}->{'TABLE_INDEX'} eq 'yes') {
 1736: 		    $texversion=&IndexCreation($texversion,$urlp);
 1737: 		}
 1738: 		if (($selectionmade == 4) and ($assignment ne $prevassignment)) {
 1739: 		    my $name = &get_name();
 1740: 		    my $courseidinfo = &get_course();
 1741: 		    if (defined($courseidinfo)) { $courseidinfo=' - '.$courseidinfo }
 1742: 		    $prevassignment=$assignment;
 1743: 		    my $header_text = $parmhash{'print_header_format'};
 1744: 		    $header_text    = &format_page_header($textwidth, $header_text,
 1745: 							  $assignment, 
 1746: 							  $courseidinfo, 
 1747: 							  $name);
 1748: 		    if ($numberofcolumns eq '1') {
 1749: 			$result .='\newpage \noindent\parbox{\minipagewidth}{\noindent\\lhead{'.$header_text.'}} \vskip 5 mm ';
 1750: 		    } else {
 1751: 			$result .='\newpage \noindent\parbox{\minipagewidth}{\noindent\\fancyhead[LO]{'.$header_text.'}} \vskip 5 mm ';
 1752: 		    }			
 1753: 		}
 1754: 		$result .= $texversion;
 1755: 		$flag_latex_header_remove = 'YES';   
 1756: 	    } elsif ($urlp=~/\/(smppg|syllabus|aboutme|bulletinboard)$/) { 
 1757: 		$form{'latex_type'}=$helper->{'VARS'}->{'LATEX_TYPE'};
 1758: 		if ($urlp=~/\/syllabus$/) {$urlp=~s/\/res//;}
 1759: 		$resources_printed .= $urlp.':';
 1760: 		my $texversion=&ssi_with_retries($urlp, $ssi_retry_count, %form);
 1761: 		if ($helper->{'VARS'}->{'PRINT_ANNOTATIONS'} eq 'yes') {
 1762: 		    my $annotation = &annotate($urlp);
 1763: 		    $texversion =~ s/(\\end{document)/$annotation$1/;
 1764: 		}
 1765: 
 1766: 		if ($flag_latex_header_remove ne 'NO') {
 1767: 		    $texversion = &latex_header_footer_remove($texversion);
 1768: 		} else {
 1769: 		    $texversion =~ s/\\end{document}/\\vskip 0\.5mm\\noindent\\makebox\[\\textwidth\/\$number_of_columns\]\[b\]\{\\hrulefill\}/;
 1770: 		}
 1771: 		$result .= $texversion;
 1772: 		$flag_latex_header_remove = 'YES'; 
 1773: 	    } else {
 1774: 		$texversion=&unsupported($urlp,$helper->{'VARS'}->{'LATEX_TYPE'},
 1775: 					 $master_seq[$i]);
 1776: 		if ($flag_latex_header_remove ne 'NO') {
 1777: 		    $texversion = &latex_header_footer_remove($texversion);
 1778: 		} else {
 1779: 		    $texversion =~ s/\\end{document}//;
 1780: 		}
 1781: 		$result .= $texversion;
 1782: 		$flag_latex_header_remove = 'YES';   
 1783: 	    }	    
 1784: 	    if (&Apache::loncommon::connection_aborted($r)) { last; }
 1785: 	}
 1786: 	&Apache::lonxml::clear_problem_counter();
 1787: 	if ($flag_page_in_sequence eq 'YES') {
 1788: 	    $result =~ s/\\usepackage{calc}/\\usepackage{calc}\\usepackage{longtable}/;
 1789: 	}	
 1790: 	$result .= '\end{document}';
 1791:      } elsif (($helper->{'VARS'}->{'PRINT_TYPE'} eq 'problems_for_students') ||
 1792: 	      ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'resources_for_students')){
 1793: 
 1794: 
 1795:      #-- prints assignments for whole class or for selected students  
 1796: 	 my $type;
 1797: 	 if ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'problems_for_students') {
 1798: 	     $selectionmade=5;
 1799: 	     $type='problems';
 1800: 	 } elsif ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'resources_for_students') {
 1801: 	     $selectionmade=8;
 1802: 	     $type='resources';
 1803: 	 }
 1804: 	 my @students=split /\|\|\|/, $helper->{'VARS'}->{'STUDENTS'};
 1805: 	 #   The normal sort order is by section then by students within the
 1806: 	 #   section. If the helper var student_sort is 1, then the user has elected
 1807: 	 #   to override this and output the students by name.
 1808: 	 #    Each element of the students array is of the form:
 1809: 	 #       username:domain:section:last, first:status
 1810: 	 #    
 1811: 	 #  Note that student sort is not compatible with printing 
 1812: 	 #  1 section per pdf...so that setting overrides.
 1813: 	 #   
 1814: 	 if (($helper->{'VARS'}->{'student_sort'}    eq 1)  && 
 1815: 	     ($helper->{'VARS'}->{'SPLIT_PDFS'} ne "sections")) {
 1816: 	     @students = sort compare_names  @students;
 1817: 	 }
 1818: 	 &adjust_number_to_print($helper);
 1819: 
 1820:          if ($helper->{'VARS'}->{'NUMBER_TO_PRINT'} eq '0' ||
 1821: 	     $helper->{'VARS'}->{'NUMBER_TO_PRINT'} eq 'all' ) {
 1822: 	     $helper->{'VARS'}->{'NUMBER_TO_PRINT'}=$#students+1;
 1823: 	 }
 1824: 	 # If we are splitting on section boundaries, we need 
 1825: 	 # to remember that in split_on_sections and 
 1826: 	 # print all of the students in the list.
 1827: 	 #
 1828: 	 my $split_on_sections = 0;
 1829: 	 if ($helper->{'VARS'}->{'NUMBER_TO_PRINT'} eq 'section') {
 1830: 	     $split_on_sections = 1;
 1831: 	     $helper->{'VARS'}->{'NUMBER_TO_PRINT'} = $#students+1;
 1832: 	 }
 1833: 	 my @master_seq=split /\|\|\|/, $helper->{'VARS'}->{'RESOURCES'};
 1834: 
 1835: 	 #loop over students
 1836: 	 my $flag_latex_header_remove = 'NO'; 
 1837: 	 my %moreenv;
 1838:          $moreenv{'instructor_comments'}='hide';
 1839: 	 $moreenv{'textwidth'}=&get_textwidth($helper,$LaTeXwidth);
 1840: 	 $moreenv{'print_discussions'}=$helper->{'VARS'}->{'PRINT_DISCUSSIONS'};
 1841: 	 $moreenv{'print_annotations'} = $helper->{'VARS'}->{'PRINT_ANNOTATIONS'};
 1842: 	 $moreenv{'problem_split'}    = $parmhash{'problem_stream_switch'};
 1843: 	 $moreenv{'suppress_tries'}   = $parmhash{'suppress_tries'};
 1844: 	 if (($helper->{'VARS'}->{'PRINT_DISCUSSIONS'} eq 'yes')  ||
 1845: 	     ($helper->{'VARS'}->{'PRINT_ANNOTATIONS'} eq 'yes')) {
 1846: 	     $moreenv{'problem_split'}='yes';
 1847: 	 }
 1848: 	 my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin($r,'Print Status','Class Print Status',$#students+1,'inline','75');
 1849: 	 my $student_counter=-1;
 1850: 	 my $i = 0;
 1851: 	 my $last_section = (split(/:/,$students[0]))[2];
 1852: 	 foreach my $person (@students) {
 1853: 
 1854:              my $duefile="/home/httpd/prtspool/$env{'user.name'}_$env{'user.domain'}_printout.due";
 1855: 	     if (-e $duefile) {
 1856: 		 my $temp_file = Apache::File->new('>>'.$duefile);
 1857: 		 print $temp_file "1969\n";
 1858: 	     }
 1859: 	     $student_counter++;
 1860: 	     if ($split_on_sections) {
 1861: 		 my $this_section = (split(/:/,$person))[2];
 1862: 		 if ($this_section ne $last_section) {
 1863: 		     $i++;
 1864: 		     $last_section = $this_section;
 1865: 		 }
 1866: 	     } else {
 1867: 		 $i=int($student_counter/$helper->{'VARS'}{'NUMBER_TO_PRINT'});
 1868: 	     }
 1869: 	     my ($output,$fullname, $printed)=&print_resources($r,$helper,
 1870: 						     $person,$type,
 1871: 						     \%moreenv,\@master_seq,
 1872: 						     $flag_latex_header_remove,
 1873: 						     $LaTeXwidth);
 1874: 	     $resources_printed .= ":";
 1875: 	     $print_array[$i].=$output;
 1876: 	     $student_names[$i].=$person.':'.$fullname.'_END_';
 1877: 	     &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,&mt('last student').' '.$fullname);
 1878: 	     $flag_latex_header_remove = 'YES';
 1879: 	     if (&Apache::loncommon::connection_aborted($r)) { last; }
 1880: 	 }
 1881: 	 &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
 1882: 	 $result .= $print_array[0].'  \end{document}';
 1883:      } elsif (($helper->{'VARS'}->{'PRINT_TYPE'} eq 'problems_for_anon')     ||
 1884: 	      ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'resources_for_anon')  ) { 
 1885: 	 my $cdom =$env{'course.'.$env{'request.course.id'}.'.domain'};
 1886: 	 my $cnum =$env{'course.'.$env{'request.course.id'}.'.num'};
 1887: 	 my $num_todo=$helper->{'VARS'}->{'NUMBER_TO_PRINT_TOTAL'};
 1888: 	 my $code_name=$helper->{'VARS'}->{'ANON_CODE_STORAGE_NAME'};
 1889: 	 my $old_name=$helper->{'VARS'}->{'REUSE_OLD_CODES'};
 1890: 	 my $single_code = $helper->{'VARS'}->{'SINGLE_CODE'};
 1891: 	 my $selected_code = $helper->{'VARS'}->{'CODE_SELECTED_FROM_LIST'};
 1892: 
 1893: 	 my $code_option=$helper->{'VARS'}->{'CODE_OPTION'};
 1894: 	 open(FH,$Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab');
 1895: 	 my ($code_type,$code_length)=('letter',6);
 1896: 	 foreach my $line (<FH>) {
 1897: 	     my ($name,$type,$length) = (split(/:/,$line))[0,2,4];
 1898: 	     if ($name eq $code_option) {
 1899: 		 $code_length=$length;
 1900: 		 if ($type eq 'number') { $code_type = 'number'; }
 1901: 	     }
 1902: 	 }
 1903: 	 my %moreenv = ('textwidth' => &get_textwidth($helper,$LaTeXwidth));
 1904: 	 $moreenv{'problem_split'}    = $parmhash{'problem_stream_switch'};
 1905:          $moreenv{'instructor_comments'}='hide';
 1906: 	 my $seed=time+($$<<16)+($$);
 1907: 	 my @allcodes;
 1908: 	 if ($old_name) {
 1909: 	     my %result=&Apache::lonnet::get('CODEs',
 1910: 					     [$old_name,"type\0$old_name"],
 1911: 					     $cdom,$cnum);
 1912: 	     $code_type=$result{"type\0$old_name"};
 1913: 	     @allcodes=split(',',$result{$old_name});
 1914: 	     $num_todo=scalar(@allcodes);
 1915: 	 } elsif ($selected_code) { # Selection value is always numeric.
 1916: 	     $num_todo = 1;
 1917: 	     @allcodes = ($selected_code);
 1918: 	 } elsif ($single_code) {
 1919: 
 1920: 	     $num_todo    = 1;	# Unconditionally one code to do.
 1921: 	     # If an alpha code have to convert to numbers so it can be
 1922: 	     # converted back to letters again :-)
 1923: 	     #
 1924: 	     if ($code_type ne 'number') {
 1925: 		 $single_code = &letters_to_num($single_code);
 1926: 	     }
 1927: 	     @allcodes = ($single_code);
 1928: 	 } else {
 1929: 	     my %allcodes;
 1930: 	     srand($seed);
 1931: 	     for (my $i=0;$i<$num_todo;$i++) {
 1932: 		 $moreenv{'CODE'}=&get_CODE(\%allcodes,$i,$seed,$code_length,
 1933: 					    $code_type);
 1934: 	     }
 1935: 	     if ($code_name) {
 1936: 		 &Apache::lonnet::put('CODEs',
 1937: 				      {
 1938: 					$code_name =>join(',',keys(%allcodes)),
 1939: 					"type\0$code_name" => $code_type
 1940: 				      },
 1941: 				      $cdom,$cnum);
 1942: 	     }
 1943: 	     @allcodes=keys(%allcodes);
 1944: 	 }
 1945: 	 my @master_seq=split /\|\|\|/, $helper->{'VARS'}->{'RESOURCES'};
 1946: 	 my ($type) = split(/_/,$helper->{'VARS'}->{'PRINT_TYPE'});
 1947: 	 &adjust_number_to_print($helper);
 1948: 	 my $number_per_page=$helper->{'VARS'}->{'NUMBER_TO_PRINT'};
 1949: 	 if ($number_per_page eq '0' || $number_per_page eq 'all') {
 1950: 	     $number_per_page=$num_todo;
 1951: 	 }
 1952: 	 my $flag_latex_header_remove = 'NO'; 
 1953: 	 my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin($r,'Print Status','Class Print Status',$num_todo,'inline','75');
 1954: 	 my $count=0;
 1955: 	 foreach my $code (sort(@allcodes)) {
 1956: 	     my $file_num=int($count/$number_per_page);
 1957: 	     if ($code_type eq 'number') { 
 1958: 		 $moreenv{'CODE'}=$code;
 1959: 	     } else {
 1960: 		 $moreenv{'CODE'}=&num_to_letters($code);
 1961: 	     }
 1962: 	     my ($output,$fullname, $printed)=
 1963: 		 &print_resources($r,$helper,'anonymous',$type,\%moreenv,
 1964: 				  \@master_seq,$flag_latex_header_remove,
 1965: 				  $LaTeXwidth);
 1966: 	     $resources_printed .= ":";
 1967: 	     $print_array[$file_num].=$output;
 1968: 	     &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
 1969: 				       &mt('last assignment').' '.$fullname);
 1970: 	     $flag_latex_header_remove = 'YES';
 1971: 	     $count++;
 1972: 	     if (&Apache::loncommon::connection_aborted($r)) { last; }
 1973: 	 }
 1974: 	 &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
 1975: 	 $result .= $print_array[0].'  \end{document}';
 1976:      } elsif ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'problems_from_directory') {      
 1977:     #prints selected problems from the subdirectory 
 1978: 	$selectionmade = 6;
 1979:         my @list_of_files=split /\|\|\|/, $helper->{'VARS'}->{'FILES'};
 1980: 	@list_of_files=sort @list_of_files;
 1981: 	my $flag_latex_header_remove = 'NO'; 
 1982: 	my $rndseed=time;
 1983: 	if ($helper->{'VARS'}->{'curseed'}) {
 1984: 	    $rndseed=$helper->{'VARS'}->{'curseed'};
 1985: 	}
 1986: 	for (my $i=0;$i<=$#list_of_files;$i++) {
 1987: 	    my $urlp = $list_of_files[$i];
 1988: 	    $urlp=~s|//|/|;
 1989: 	    if ($urlp=~/\//) {
 1990: 		$form{'problem_split'}=$parmhash{'problem_stream_switch'};
 1991: 		$form{'rndseed'}=$rndseed;
 1992: 		if ($urlp =~ m|/home/([^/]+)/public_html|) {
 1993: 		    $urlp =~ s|/home/([^/]*)/public_html|/~$1|;
 1994: 		} else {
 1995: 		    $urlp =~ s|^$Apache::lonnet::perlvar{'lonDocRoot'}||;
 1996: 		}
 1997: 		$resources_printed .= $urlp.':';
 1998: 		my $texversion=&ssi_with_retries($urlp, $ssi_retry_count, %form);
 1999: 		if(($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') ||
 2000: 		   ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'only')) {
 2001: 		    #  Don't permanently pervert %form:
 2002: 		    my %answerform = %form;
 2003: 		    $answerform{'grade_target'}='answer';
 2004: 		    $answerform{'answer_output_mode'}='tex';
 2005: 		    $answerform{'latex_type'}=$helper->{'VARS'}->{'LATEX_TYPE'};
 2006: 		    $answerform{'rndseed'}=$rndseed;
 2007: 		    $resources_printed .= $urlp.':';
 2008: 		    my $answer=&ssi_with_retries($urlp, $ssi_retry_count, %answerform);
 2009: 		    if ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') {
 2010: 			$texversion=~s/(\\keephidden{ENDOFPROBLEM})/$answer$1/;
 2011: 		    } else {
 2012: 			$texversion=&print_latex_header($helper->{'VARS'}->{'LATEX_TYPE'});
 2013: 			if ($helper->{'VARS'}->{'construction'} ne '1') {
 2014: 			    $texversion.='\vskip 0 mm \noindent ';
 2015: 			    $texversion.=&path_to_problem ($urlp,$LaTeXwidth);
 2016: 			} else {
 2017: 			    $texversion.='\vskip 0 mm \noindent\textbf{Prints from construction space - there is no title.}\vskip 0 mm ';
 2018: 			    my $URLpath=$urlp;
 2019: 			    $URLpath=~s/~([^\/]+)/public_html\/$1\/$1/;
 2020: 			    $texversion.=&path_to_problem ($URLpath,$LaTeXwidth);
 2021: 			}
 2022: 			$texversion.='\vskip 1 mm '.$answer.'\end{document}';
 2023: 		    }
 2024: 		}
 2025:                 #this chunk is responsible for printing the path to problem
 2026: 
 2027: 		my $newurlp=$urlp;
 2028: 		if ($newurlp=~/~/) {$newurlp=~s|\/~([^\/]+)\/|\/home\/$1\/public_html\/|;}
 2029: 		$newurlp=&path_to_problem($newurlp,$LaTeXwidth);
 2030: 		$texversion =~ s/(\\begin{minipage}{\\textwidth})/$1 $newurlp/;
 2031: 		if ($flag_latex_header_remove ne 'NO') {
 2032: 		    $texversion = &latex_header_footer_remove($texversion);
 2033: 		} else {
 2034: 		    $texversion =~ s/\\end{document}//;
 2035: 		}
 2036: 		if ($helper->{'VARS'}->{'TABLE_INDEX'} eq 'yes') {
 2037: 		    $texversion=&IndexCreation($texversion,$urlp);
 2038: 		}
 2039: 		if ($helper->{'VARS'}->{'CONSTR_RESOURSE_URL'} eq 'yes') {
 2040: 		    $texversion=~s/(\\addcontentsline\{toc\}\{subsection\}\{[^\}]*\})/$1 URL: \\verb|$urlp| \\strut\\\\\\strut /;
 2041: 		    
 2042: 		}
 2043: 		$result .= $texversion;
 2044: 	    }
 2045: 	    $flag_latex_header_remove = 'YES';  
 2046: 	}
 2047: 	if ($helper->{VARS}->{'construction'} eq '1') {$result=~s/(\\typeout)/ RANDOM SEED IS $rndseed $1/;}
 2048: 	$result .= '\end{document}';      	
 2049:     }
 2050: #-------------------------------------------------------- corrections for the different page formats
 2051: 
 2052:     # Only post process if that has not been turned off e.g. by a raw latex resource.
 2053: 
 2054:     if ($do_postprocessing) {
 2055: 	$result = &page_format_transformation($papersize,$laystyle,$numberofcolumns,$helper->{'VARS'}->{'PRINT_TYPE'},$result,$helper->{VARS}->{'assignment'},$helper->{'VARS'}->{'TABLE_CONTENTS'},$helper->{'VARS'}->{'TABLE_INDEX'},$selectionmade);
 2056: 	$result = &latex_corrections($number_of_columns,$result,$selectionmade,
 2057: 				     $helper->{'VARS'}->{'ANSWER_TYPE'});
 2058: 	#if ($numberofcolumns == 1) {
 2059: 	$result =~ s/\\textwidth\s*=\s*-?\d*\.?\d*\s*(cm|mm|in)/\\textwidth= $helper->{'VARS'}->{'pagesize.width'} $helper->{'VARS'}->{'pagesize.widthunit'} /;
 2060: 	$result =~ s/\\textheight\s*=?\s*-?\d*\.?\d*\s*(cm|mm|in)/\\textheight $helper->{'VARS'}->{'pagesize.height'} $helper->{'VARS'}->{'pagesize.heightunit'} /;
 2061: 	$result =~ s/\\evensidemargin\s*=\s*-?\d*\.?\d*\s*(cm|mm|in)/\\evensidemargin= $helper->{'VARS'}->{'pagesize.lmargin'} $helper->{'VARS'}->{'pagesize.lmarginunit'} /;
 2062: 	$result =~ s/\\oddsidemargin\s*=\s*-?\d*\.?\d*\s*(cm|mm|in)/\\oddsidemargin= $helper->{'VARS'}->{'pagesize.lmargin'} $helper->{'VARS'}->{'pagesize.lmarginunit'} /;
 2063: 	#}
 2064:     }
 2065: 
 2066:     # Set URLback if this is a construction space print so we can provide
 2067:     # a link to the resource being edited.
 2068:     #
 2069: 
 2070:     my $URLback=''; #link to original document
 2071:     if ($helper->{'VARS'}->{'construction'} eq '1') {
 2072: 	#prints resource from the construction space
 2073: 	$URLback='/'.$helper->{'VARS'}->{'filename'};
 2074: 	if ($URLback=~/([^?]+)/) {
 2075: 	    $URLback=$1;
 2076: 	    $URLback=~s|^/~|/priv/|;
 2077: 	}
 2078:     }
 2079: 
 2080: 
 2081:     # If there's been an unrecoverable SSI error, report it to the user
 2082:     # otherwise, we can write the tex file.
 2083:     #
 2084: 
 2085:     if ($ssi_error) {
 2086: 	my $end_page = &Apache::loncommon::end_page();
 2087: 	$r->print(<<ERROR_END);
 2088: <br />
 2089: <h2>An unrecoverable error occured:</h2>
 2090: <p>
 2091:   I was not able to render one of the print resources ($ssi_last_error_resource) 
 2092: due to an unrecoverable error communicating with a server:
 2093: <br />
 2094: $ssi_last_error;
 2095: <br />
 2096: </p>
 2097: <p>
 2098: I recommend that you try printing again later as this may mean the server was just 
 2099: temporarily unavailable, or is down for maintenance.  If this error persists, then
 2100: please contact your LonCAPA support folks for assistance and diagnosis. 
 2101: <br />
 2102: <br />
 2103: We apologize for the inconvenience.
 2104: </p>
 2105: $end_page
 2106: ERROR_END
 2107:     } else {
 2108: 
 2109: #-- writing .tex file in prtspool 
 2110: 	my $temp_file;
 2111: 	my $identifier = &Apache::loncommon::get_cgi_id();
 2112: 	my $filename = "/home/httpd/prtspool/$env{'user.name'}_$env{'user.domain'}_printout_$identifier.tex";
 2113: 	if (!($#print_array>0)) { 
 2114: 	    unless ($temp_file = Apache::File->new('>'.$filename)) {
 2115: 		$r->log_error("Couldn't open $filename for output $!");
 2116: 		return SERVER_ERROR; 
 2117: 	    }
 2118: 	    print $temp_file $result;
 2119: 	    my $begin=index($result,'\begin{document}',0);
 2120: 	    my $inc=substr($result,0,$begin+16);
 2121: 	} else {
 2122: 	    my $begin=index($result,'\begin{document}',0);
 2123: 	    my $inc=substr($result,0,$begin+16);
 2124: 	    for (my $i=0;$i<=$#print_array;$i++) {
 2125: 		if ($i==0) {
 2126: 		    $print_array[$i]=$result;
 2127: 		} else {
 2128: 		    $print_array[$i].='\end{document}';
 2129: 		    $print_array[$i] = 
 2130: 			&latex_corrections($number_of_columns,$print_array[$i],
 2131: 					   $selectionmade, 
 2132: 					   $helper->{'VARS'}->{'ANSWER_TYPE'});
 2133: 		    
 2134: 		    my $anobegin=index($print_array[$i],'\setcounter{page}',0);
 2135: 		    substr($print_array[$i],0,$anobegin)='';
 2136: 		    $print_array[$i]=$inc.$print_array[$i];
 2137: 		}
 2138: 		my $temp_file;
 2139: 		my $newfilename=$filename;
 2140: 		my $num=$i+1;
 2141: 		$newfilename =~s/\.tex$//; 
 2142: 		$newfilename=sprintf("%s_%03d.tex",$newfilename, $num);
 2143: 		unless ($temp_file = Apache::File->new('>'.$newfilename)) {
 2144: 		    $r->log_error("Couldn't open $newfilename for output $!");
 2145: 		    return SERVER_ERROR; 
 2146: 		}
 2147: 		print $temp_file $print_array[$i];
 2148: 	    }
 2149: 	    
 2150: 	}
 2151: 	my $student_names='';
 2152: 	if ($#print_array>0) {
 2153: 	    for (my $i=0;$i<=$#print_array;$i++) {
 2154: 		$student_names.=$student_names[$i].'_ENDPERSON_';
 2155: 	    }
 2156: 	} else {
 2157: 	    if ($#student_names>-1) {
 2158: 		$student_names=$student_names[0].'_ENDPERSON_';
 2159: 	    } else {
 2160: 		my $fullname = &get_name($env{'user.name'},$env{'user.domain'});
 2161: 		$student_names=join(':',$env{'user.name'},$env{'user.domain'},
 2162: 				    $env{'request.course.sec'},$fullname).
 2163: 					'_ENDPERSON_'.'_END_';
 2164: 	    }
 2165: 	}
 2166: 	
 2167: 	# logic for now is too complex to trace if this has been defined
 2168: 	#  yet.
 2169: 	my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 2170: 	my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 2171: 	&Apache::lonnet::appenv({'cgi.'.$identifier.'.file'   => $filename,
 2172: 				'cgi.'.$identifier.'.layout'  => $laystyle,
 2173: 				'cgi.'.$identifier.'.numcol'  => $numberofcolumns,
 2174: 				'cgi.'.$identifier.'.paper'  => $papersize,
 2175: 				'cgi.'.$identifier.'.selection' => $selectionmade,
 2176: 				'cgi.'.$identifier.'.tableofcontents' => $helper->{'VARS'}->{'TABLE_CONTENTS'},
 2177: 				'cgi.'.$identifier.'.tableofindex' => $helper->{'VARS'}->{'TABLE_INDEX'},
 2178: 				'cgi.'.$identifier.'.role' => $perm{'pav'},
 2179: 				'cgi.'.$identifier.'.numberoffiles' => $#print_array,
 2180: 				'cgi.'.$identifier.'.studentnames' => $student_names,
 2181: 				'cgi.'.$identifier.'.backref' => $URLback,});
 2182: 	&Apache::lonnet::appenv({"cgi.$identifier.user"    => $env{'user.name'},
 2183: 				"cgi.$identifier.domain"  => $env{'user.domain'},
 2184: 				"cgi.$identifier.courseid" => $cnum, 
 2185: 				"cgi.$identifier.coursedom" => $cdom, 
 2186: 				"cgi.$identifier.resources" => $resources_printed});
 2187: 	
 2188: 	my $end_page = &Apache::loncommon::end_page();
 2189: 	$r->print(<<FINALEND);
 2190: <br />
 2191: <meta http-equiv="Refresh" content="0; url=/cgi-bin/printout.pl?$identifier" />
 2192: <a href="/cgi-bin/printout.pl?$identifier">Continue</a>
 2193: $end_page
 2194: FINALEND
 2195:   }                                       # endif ssi errors.
 2196: }
 2197: 
 2198: 
 2199: sub get_CODE {
 2200:     my ($all_codes,$num,$seed,$size,$type)=@_;
 2201:     my $max='1'.'0'x$size;
 2202:     my $newcode;
 2203:     while(1) {
 2204: 	$newcode=sprintf("%0".$size."d",int(rand($max)));
 2205: 	if (!exists($$all_codes{$newcode})) {
 2206: 	    $$all_codes{$newcode}=1;
 2207: 	    if ($type eq 'number' ) {
 2208: 		return $newcode;
 2209: 	    } else {
 2210: 		return &num_to_letters($newcode);
 2211: 	    }
 2212: 	}
 2213:     }
 2214: }
 2215: 
 2216: sub print_resources {
 2217:     my ($r,$helper,$person,$type,$moreenv,$master_seq,$remove_latex_header,
 2218: 	$LaTeXwidth)=@_;
 2219:     my $current_output = ''; 
 2220:     my $printed = '';
 2221:     my ($username,$userdomain,$usersection) = split /:/,$person;
 2222:     my $fullname = &get_name($username,$userdomain);
 2223:     my $namepostfix = "\\\\";	# Both anon and not anon should get the same vspace.
 2224:     if ($person =~ 'anon') {
 2225: 	$namepostfix .="Name: ";
 2226: 	$fullname = "CODE - ".$moreenv->{'CODE'};
 2227:     }
 2228:     #  Fullname may have special latex characters that need \ prefixing:
 2229:     #
 2230: 
 2231:     my $i           = 0;
 2232:     #goes through all resources, checks if they are available for 
 2233:     #current student, and produces output   
 2234: 
 2235:     &Apache::lonxml::clear_problem_counter();
 2236:     my %page_breaks  = &get_page_breaks($helper);
 2237:     my $columns_in_format = (split(/\|/,$helper->{'VARS'}->{'FORMAT'}))[1];
 2238:     #
 2239:     #   end each student with a 
 2240:     #   Special that allows the post processor to even out the page
 2241:     #   counts later.  Nasty problem this... it would be really
 2242:     #   nice to put the special in as a postscript comment
 2243:     #   e.g. \special{ps:\ENDOFSTUDENTSTAMP}  unfortunately,
 2244:     #   The special gets passed the \ and dvips puts it in the output file
 2245:     #   so we will just rely on prntout.pl to strip  ENDOFSTUDENTSTAMP from the
 2246:     #   postscript.  Each ENDOFSTUDENTSTAMP will go on a line by itself.
 2247:     #
 2248: 
 2249: 
 2250:     foreach my $curresline (@{$master_seq})  {
 2251: 	if (defined $page_breaks{$curresline}) {
 2252: 	    if($i != 0) {
 2253: 		$current_output.= "\\newpage\n";
 2254: 	    }
 2255: 	}
 2256: 	$i++;
 2257: 
 2258: 	if ( !($type eq 'problems' && 
 2259: 	       ($curresline!~ m/\.(problem|exam|quiz|assess|survey|form|library)$/)) ) {
 2260: 	    my ($map,$id,$res_url) = &Apache::lonnet::decode_symb($curresline);
 2261: 	    if (&Apache::lonnet::allowed('bre',$res_url)) {
 2262: 		if ($res_url!~m|^ext/|
 2263: 		    && $res_url=~/\.(problem|exam|quiz|assess|survey|form|library|page|xml|html|htm|xhtml|xhtm)$/) {
 2264: 		    $printed .= $curresline.':';
 2265: 
 2266: 		    &Apache::lonxml::remember_problem_counter();    
 2267: 
 2268: 		    my $rendered = &Apache::loncommon::get_student_view($curresline,$username,$userdomain,$env{'request.course.id'},'tex',$moreenv);
 2269: 
 2270: 		    if(($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') ||
 2271: 		       ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'only')) {
 2272: 			#   Use a copy of the hash so we don't pervert it on future loop passes.
 2273: 			my %answerenv = %{$moreenv};
 2274: 			$answerenv{'answer_output_mode'}='tex';
 2275: 			$answerenv{'latex_type'}=$helper->{'VARS'}->{'LATEX_TYPE'};
 2276: 			
 2277: 			&Apache::lonxml::restore_problem_counter();
 2278: 
 2279: 			my $ansrendered = &Apache::loncommon::get_student_answers($curresline,$username,$userdomain,$env{'request.course.id'},%answerenv);
 2280: 
 2281: 			if ($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') {
 2282: 			    $rendered=~s/(\\keephidden{ENDOFPROBLEM})/$ansrendered$1/;
 2283: 			} else {
 2284: 
 2285: 			    
 2286: 			    my $header =&print_latex_header($helper->{'VARS'}->{'LATEX_TYPE'});
 2287: 			    my $title = &Apache::lonnet::gettitle($curresline);
 2288: 			    $title = &Apache::lonxml::latex_special_symbols($title);
 2289: 			    my $body   ='\vskip 0 mm \noindent\textbf{'.$title.'}\vskip 0 mm ';
 2290: 			    $body     .=&path_to_problem($res_url,$LaTeXwidth);
 2291: 			    $body     .='\vskip 1 mm '.$ansrendered;
 2292: 			    $body     = &encapsulate_minipage($body);
 2293: 			    $rendered = $header.$body;
 2294: 			}
 2295: 		    }
 2296: 
 2297: 		    if ($helper->{'VARS'}->{'PRINT_ANNOTATIONS'} eq 'yes') {
 2298: 			my $url = &Apache::lonnet::clutter($res_url);
 2299: 			my $annotation = &annotate($url);
 2300: 			$rendered =~  s/(\\keephidden{ENDOFPROBLEM})/$annotation$1/;
 2301: 		    }
 2302: 		    if ($remove_latex_header eq 'YES') {
 2303: 			$rendered = &latex_header_footer_remove($rendered);
 2304: 		    } else {
 2305: 			$rendered =~ s/\\end{document}//;
 2306: 		    }
 2307: 		    $current_output .= $rendered;		    
 2308: 		} elsif ($res_url=~/\/(smppg|syllabus|aboutme|bulletinboard)$/) {
 2309: 		    $printed .= $curresline.':';
 2310: 		    my $rendered = &Apache::loncommon::get_student_view($curresline,$username,$userdomain,$env{'request.course.id'},'tex',$moreenv);
 2311: 		    if ($helper->{'VARS'}->{'PRINT_ANNOTATIONS'} eq 'yes') {
 2312: 			my $url = &Apache::lonnet::clutter($res_url);
 2313: 			my $annotation = &annotate($url);
 2314: 			$annotation    =~ s/(\\end{document})/$annotation$1/;
 2315: 		    }
 2316: 		    if ($remove_latex_header eq 'YES') {
 2317: 			$rendered = &latex_header_footer_remove($rendered);
 2318: 		    } else {
 2319: 			$rendered =~ s/\\end{document}//;
 2320: 		    }
 2321: 		    $current_output .= $rendered.'\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\strut \vskip 0 mm \strut ';
 2322: 
 2323: 		} else {
 2324: 		    my $rendered = &unsupported($res_url,$helper->{'VARS'}->{'LATEX_TYPE'},$curresline);
 2325: 		    if ($remove_latex_header ne 'NO') {
 2326: 			$rendered = &latex_header_footer_remove($rendered);
 2327: 		    } else {
 2328: 			$rendered =~ s/\\end{document}//;
 2329: 		    }
 2330: 		    $current_output .= $rendered;
 2331: 		}
 2332: 	    }
 2333: 	    $remove_latex_header = 'YES';
 2334: 	}
 2335: 	if (&Apache::loncommon::connection_aborted($r)) { last; }
 2336:     }
 2337:     my $courseidinfo = &get_course();
 2338:     if (defined($courseidinfo)) { $courseidinfo=' - '.$courseidinfo }
 2339:     if ($usersection ne '') {$courseidinfo.=' - Sec. '.$usersection}
 2340:     my $currentassignment=&Apache::lonxml::latex_special_symbols($helper->{VARS}->{'assignment'},'header');
 2341:     my $header_line =
 2342: 	&format_page_header($LaTeXwidth, $parmhash{'print_header_format'},
 2343: 			    $currentassignment, $courseidinfo, $fullname);
 2344:     my $header_start = ($columns_in_format == 1) ? '\lhead'
 2345: 	                                         : '\fancyhead[LO]';
 2346:     $header_line = $header_start.'{'.$header_line.'}';
 2347: 
 2348:     if ($current_output=~/\\documentclass/) {
 2349: 	$current_output =~ s/\\begin{document}/\\setlength{\\topmargin}{1cm} \\begin{document}\\noindent\\parbox{\\minipagewidth}{\\noindent$header_line$namepostfix}\\vskip 5 mm /;
 2350:     } else {
 2351: 	my $blankpages = 
 2352: 	    '\clearpage\strut\clearpage'x$helper->{'VARS'}->{'EMPTY_PAGES'};
 2353: 	    
 2354: 	$current_output = '\strut\vspace*{-6 mm}\\newline'.
 2355: 	    &copyright_line().' \newpage '.$blankpages.$end_of_student.
 2356: 	    '\setcounter{page}{1}\noindent\parbox{\minipagewidth}{\noindent'.
 2357: 	    $header_line.$namepostfix.'} \vskip 5 mm '.$current_output;
 2358:     }
 2359:     #
 2360:     #  Close the student bracketing.
 2361:     #
 2362:     return ($current_output,$fullname, $printed);
 2363: 
 2364: }
 2365: 
 2366: sub handler {
 2367: 
 2368:     my $r = shift;
 2369:     
 2370:     &init_perm();
 2371: 
 2372: 
 2373: 
 2374:     my $helper = printHelper($r);
 2375:     if (!ref($helper)) {
 2376: 	return $helper;
 2377:     }
 2378:    
 2379: 
 2380:     %parmhash=&Apache::lonnet::coursedescription($env{'request.course.id'});
 2381:  
 2382: 
 2383: 
 2384: 
 2385:     #  If a figure conversion queue file exists for this user.domain
 2386:     # we delete it since it can only be bad (if it were good, printout.pl
 2387:     # would have deleted it the last time around.
 2388: 
 2389:     my $conversion_queuefile = "/home/httpd/prtspool/$env{'user.name'}_$env{'user.domain'}_printout.dat";
 2390:     if(-e $conversion_queuefile) {
 2391: 	unlink $conversion_queuefile;
 2392:     }
 2393:     
 2394: 
 2395:     &output_data($r,$helper,\%parmhash);
 2396:     return OK;
 2397: } 
 2398: 
 2399: use Apache::lonhelper;
 2400: 
 2401: sub addMessage {
 2402:     my $text = shift;
 2403:     my $paramHash = Apache::lonhelper::getParamHash();
 2404:     $paramHash->{MESSAGE_TEXT} = $text;
 2405:     Apache::lonhelper::message->new();
 2406: }
 2407: 
 2408: 
 2409: 
 2410: sub init_perm {
 2411:     undef(%perm);
 2412:     $perm{'pav'}=&Apache::lonnet::allowed('pav',$env{'request.course.id'});
 2413:     if (!$perm{'pav'}) {
 2414: 	$perm{'pav'}=&Apache::lonnet::allowed('pav',
 2415: 		  $env{'request.course.id'}.'/'.$env{'request.course.sec'});
 2416:     }
 2417:     $perm{'pfo'}=&Apache::lonnet::allowed('pfo',$env{'request.course.id'});
 2418:     if (!$perm{'pfo'}) {
 2419: 	$perm{'pfo'}=&Apache::lonnet::allowed('pfo',
 2420: 		  $env{'request.course.id'}.'/'.$env{'request.course.sec'});
 2421:     }
 2422: }
 2423: 
 2424: sub get_randomly_ordered_warning {
 2425:     my ($helper,$map) = @_;
 2426: 
 2427:     my $message;
 2428: 
 2429:     my $postdata = $env{'form.postdata'} || $helper->{VARS}{'postdata'};
 2430:     my $navmap = Apache::lonnavmaps::navmap->new();
 2431:     my $res = $navmap->getResourceByUrl($map);
 2432:     if ($res) {
 2433: 	my $func = 
 2434: 	    sub { return ($_[0]->is_map() && $_[0]->randomorder); };
 2435: 	my @matches = $navmap->retrieveResources($res, $func,1,1,1);
 2436: 	if (@matches) {
 2437: 	    $message = "Some of the items below are in folders set to be randomly ordered. However, when printing the contents of these folders, they will be printed in the original order for all students, not the randomized order.";
 2438: 	}
 2439:     }
 2440:     if ($message) {
 2441: 	return '<message type="warning">'.$message.'</message>';
 2442:     }
 2443:     return;
 2444: }
 2445: 
 2446: sub printHelper {
 2447:     my $r = shift;
 2448: 
 2449:     if ($r->header_only) {
 2450:         if ($env{'browser.mathml'}) {
 2451:             &Apache::loncommon::content_type($r,'text/xml');
 2452:         } else {
 2453:             &Apache::loncommon::content_type($r,'text/html');
 2454:         }
 2455:         $r->send_http_header;
 2456:         return OK;
 2457:     }
 2458: 
 2459:     # Send header, nocache
 2460:     if ($env{'browser.mathml'}) {
 2461:         &Apache::loncommon::content_type($r,'text/xml');
 2462:     } else {
 2463:         &Apache::loncommon::content_type($r,'text/html');
 2464:     }
 2465:     &Apache::loncommon::no_cache($r);
 2466:     $r->send_http_header;
 2467:     $r->rflush();
 2468: 
 2469:     # Unfortunately, this helper is so complicated we have to
 2470:     # write it by hand
 2471: 
 2472:     Apache::loncommon::get_unprocessed_cgi($ENV{QUERY_STRING});
 2473:     
 2474:     my $helper = Apache::lonhelper::helper->new("Printing Helper");
 2475:     $helper->declareVar('symb');
 2476:     $helper->declareVar('postdata');    
 2477:     $helper->declareVar('curseed'); 
 2478:     $helper->declareVar('probstatus');   
 2479:     $helper->declareVar('filename');
 2480:     $helper->declareVar('construction');
 2481:     $helper->declareVar('assignment');
 2482:     $helper->declareVar('style_file');
 2483:     $helper->declareVar('student_sort');
 2484:     $helper->declareVar('FINISHPAGE');
 2485:     $helper->declareVar('PRINT_TYPE');
 2486:     $helper->declareVar("showallfoils");
 2487:     $helper->declareVar("STUDENTS");
 2488: 
 2489: 
 2490:    
 2491: 
 2492: 
 2493:     #  The page breaks can get loaded initially from the course environment:
 2494:     # But we only do this in the initial state so that they are allowed to change.
 2495:     #
 2496: 
 2497:     # $helper->{VARS}->{FINISHPAGE} = '';
 2498:     
 2499:     &Apache::loncommon::restore_course_settings('print',
 2500: 						{'pagebreaks'  => 'scalar',
 2501: 					         'lastprinttype' => 'scalar'});
 2502:     
 2503:     # This will persistently load in the data we want from the
 2504:     # very first screen.
 2505:     
 2506:     if($helper->{VARS}->{PRINT_TYPE} eq $env{'form.lastprinttype'}) {
 2507: 	if (!defined ($env{"form.CURRENT_STATE"})) {
 2508: 	    
 2509: 	    $helper->{VARS}->{FINISHPAGE} = $env{'form.pagebreaks'};
 2510: 	} else {
 2511: 	    my $state = $env{"form.CURRENT_STATE"};
 2512: 	    if ($state eq "START") {
 2513: 		$helper->{VARS}->{FINISHPAGE} = $env{'form.pagebreaks'};
 2514: 	    }
 2515: 	}
 2516: 	
 2517:     }
 2518: 
 2519: 
 2520:     # Detect whether we're coming from construction space
 2521:     if ($env{'form.postdata'}=~/^(?:http:\/\/[^\/]+\/|\/|)\~([^\/]+)\/(.*)$/) {
 2522:         $helper->{VARS}->{'filename'} = "~$1/$2";
 2523:         $helper->{VARS}->{'construction'} = 1;
 2524:     } else {
 2525:         if ($env{'form.postdata'}) {
 2526:             $helper->{VARS}->{'symb'} = &Apache::lonnet::symbread($env{'form.postdata'});
 2527: 	    if ( $helper->{VARS}->{'symb'} eq '') {
 2528: 		$helper->{VARS}->{'postdata'} = $env{'form.postdata'};
 2529: 	    }
 2530:         }
 2531:         if ($env{'form.symb'}) {
 2532:             $helper->{VARS}->{'symb'} = $env{'form.symb'};
 2533:         }
 2534:         if ($env{'form.url'}) {
 2535:             $helper->{VARS}->{'symb'} = &Apache::lonnet::symbread($helper->{VARS}->{'postdata'});
 2536:         }
 2537: 
 2538:     }
 2539: 
 2540:     if ($env{'form.symb'}) {
 2541:         $helper->{VARS}->{'symb'} = $env{'form.symb'};
 2542:     }
 2543:     if ($env{'form.url'}) {
 2544:         $helper->{VARS}->{'symb'} = &Apache::lonnet::symbread($helper->{VARS}->{'postdata'});
 2545: 
 2546:     }
 2547:     $helper->{VARS}->{'symb'}=
 2548: 	&Apache::lonenc::check_encrypt($helper->{VARS}->{'symb'});
 2549:     my ($resourceTitle,$sequenceTitle,$mapTitle) = &details_for_menu($helper);
 2550:     if ($sequenceTitle ne '') {$helper->{VARS}->{'assignment'}=$sequenceTitle;}
 2551: 
 2552:     
 2553:     # Extract map
 2554:     my $symb = $helper->{VARS}->{'symb'};
 2555:     my ($map, $id, $url);
 2556:     my $subdir;
 2557:     my $is_published=0;		# True when printing from resource space.
 2558: 
 2559:     # Get the resource name from construction space
 2560:     if ($helper->{VARS}->{'construction'}) {
 2561:         $resourceTitle = substr($helper->{VARS}->{'filename'}, 
 2562:                                 rindex($helper->{VARS}->{'filename'}, '/')+1);
 2563:         $subdir = substr($helper->{VARS}->{'filename'},
 2564:                          0, rindex($helper->{VARS}->{'filename'}, '/') + 1);
 2565:     } else {
 2566: 	if ($symb ne '') {
 2567: 	    ($map, $id, $url) = &Apache::lonnet::decode_symb($symb);
 2568: 	    $helper->{VARS}->{'postdata'} = 
 2569: 		&Apache::lonenc::check_encrypt(&Apache::lonnet::clutter($url));
 2570: 	} else {
 2571: 	    $url = $helper->{VARS}->{'postdata'};
 2572: 	    $is_published=1;	# From resource space.
 2573: 	}
 2574: 	$url = &Apache::lonnet::clutter($url);
 2575: 
 2576:         if (!$resourceTitle) { # if the resource doesn't have a title, use the filename
 2577:             my $postdata = $helper->{VARS}->{'postdata'};
 2578:             $resourceTitle = substr($postdata, rindex($postdata, '/') + 1);
 2579:         }
 2580:         $subdir = &Apache::lonnet::filelocation("", $url);
 2581:     }
 2582:     if (!$helper->{VARS}->{'curseed'} && $env{'form.curseed'}) {
 2583: 	$helper->{VARS}->{'curseed'}=$env{'form.curseed'};
 2584:     }
 2585:     if (!$helper->{VARS}->{'probstatus'} && $env{'form.problemtype'}) {
 2586: 	$helper->{VARS}->{'probstatus'}=$env{'form.problemstatus'};
 2587:     }
 2588: 
 2589:     my $userCanSeeHidden = Apache::lonnavmaps::advancedUser();
 2590: 
 2591:     Apache::lonhelper::registerHelperTags();
 2592: 
 2593:     # "Delete everything after the last slash."
 2594:     $subdir =~ s|/[^/]+$||;
 2595: 
 2596:     # What can be printed is a very dynamic decision based on
 2597:     # lots of factors. So we need to dynamically build this list.
 2598:     # To prevent security leaks, states are only added to the wizard
 2599:     # if they can be reached, which ensures manipulating the form input
 2600:     # won't allow anyone to reach states they shouldn't have permission
 2601:     # to reach.
 2602: 
 2603:     # printChoices is tracking the kind of printing the user can
 2604:     # do, and will be used in a choices construction later.
 2605:     # In the meantime we will be adding states and elements to
 2606:     # the helper by hand.
 2607:     my $printChoices = [];
 2608:     my $paramHash;
 2609: 
 2610:     if ($resourceTitle) {
 2611:         push @{$printChoices}, ["<b><i>$resourceTitle</i></b> (".&mt('the resource you just saw on the screen').")", 'current_document', 'PAGESIZE'];
 2612:     }
 2613: 
 2614:     # Useful filter strings
 2615:     my $isProblem = '($res->is_problem()||$res->contains_problem) ';
 2616:     $isProblem .= ' && !$res->randomout()' if !$userCanSeeHidden;
 2617:     my $isProblemOrMap = '$res->is_problem() || $res->contains_problem() || $res->is_sequence()';
 2618:     my $isNotMap = '!$res->is_sequence()';
 2619:     $isNotMap .= ' && !$res->randomout()' if !$userCanSeeHidden;
 2620:     my $isMap = '$res->is_map()';
 2621:     my $symbFilter = '$res->shown_symb()';
 2622:     my $urlValue = '$res->link()';
 2623: 
 2624:     $helper->declareVar('SEQUENCE');
 2625: 
 2626:     # If we're in a sequence...
 2627: 
 2628:     my $start_new_option;
 2629:     if ($perm{'pav'}) {
 2630: 	$start_new_option = 
 2631: 	    "<option text='".&mt('Start new page<br />before selected').
 2632: 	    "' variable='FINISHPAGE' />";
 2633:     }
 2634: 
 2635:     if (($helper->{'VARS'}->{'construction'} ne '1' ) &&
 2636: 
 2637: 	$helper->{VARS}->{'postdata'} &&
 2638: 	$helper->{VARS}->{'assignment'}) {
 2639:         # Allow problems from sequence
 2640:         push @{$printChoices}, [&mt('Selected <b>Problems</b> in folder <b><i>[_1]</i></b>',$sequenceTitle), 'map_problems', 'CHOOSE_PROBLEMS'];
 2641:         # Allow all resources from sequence
 2642:         push @{$printChoices}, [&mt('Selected <b>Resources</b> in folder <b><i>[_1]</i></b>',$sequenceTitle), 'map_problems_pages', 'CHOOSE_PROBLEMS_HTML'];
 2643: 
 2644:         my $helperFragment = <<HELPERFRAGMENT;
 2645:   <state name="CHOOSE_PROBLEMS" title="Select Problem(s) to print">
 2646:     <resource variable="RESOURCES" multichoice="1" toponly='1' addstatus="1"
 2647:               closeallpages="1">
 2648:       <nextstate>PAGESIZE</nextstate>
 2649:       <filterfunc>return $isProblem;</filterfunc>
 2650:       <mapurl>$map</mapurl>
 2651:       <valuefunc>return $symbFilter;</valuefunc>
 2652:       $start_new_option
 2653:       </resource>
 2654:     </state>
 2655: 
 2656:   <state name="CHOOSE_PROBLEMS_HTML" title="Select Resource(s) to print">
 2657:     <resource variable="RESOURCES" multichoice="1" toponly='1' addstatus="1"
 2658:               closeallpages="1">
 2659:       <nextstate>PAGESIZE</nextstate>
 2660:       <filterfunc>return $isNotMap;</filterfunc>
 2661:       <mapurl>$map</mapurl>
 2662:       <valuefunc>return $symbFilter;</valuefunc>
 2663:       $start_new_option
 2664:       </resource>
 2665:     </state>
 2666: HELPERFRAGMENT
 2667: 
 2668: 	&Apache::lonxml::xmlparse($r, 'helper', $helperFragment);
 2669:     }
 2670: 
 2671:     # If the user has pfo (print for otheres) allow them to print all 
 2672:     # problems and resources  in the entier course, optionally for selected students
 2673:     if ($perm{'pfo'} &&  !$is_published  &&
 2674:         ($helper->{VARS}->{'postdata'}=~/\/res\// || $helper->{VARS}->{'postdata'}=~/\/(syllabus|smppg|aboutme|bulletinboard)$/)) { 
 2675: 
 2676:         push @{$printChoices}, [&mtn('Selected <b>Problems</b> from <b>entire course</b>'), 'all_problems', 'ALL_PROBLEMS'];
 2677: 	push @{$printChoices}, [&mtn('Selected <b>Resources</b> from <b>entire course</b>'), 'all_resources', 'ALL_RESOURCES'];
 2678:          &Apache::lonxml::xmlparse($r, 'helper', <<ALL_PROBLEMS);
 2679:   <state name="ALL_PROBLEMS" title="Select Problem(s) to print">
 2680:     <resource variable="RESOURCES" toponly='0' multichoice="1"
 2681: 	suppressEmptySequences='0' addstatus="1" closeallpages="1">
 2682:       <nextstate>PAGESIZE</nextstate>
 2683:       <filterfunc>return $isProblemOrMap;</filterfunc>
 2684:       <choicefunc>return $isNotMap;</choicefunc>
 2685:       <valuefunc>return $symbFilter;</valuefunc>
 2686:       $start_new_option
 2687:     </resource>
 2688:   </state>
 2689:   <state name="ALL_RESOURCES" title="Select Resource(s) to print">
 2690:     <resource variable="RESOURCES" toponly='0' multichoice='1'
 2691:               suppressEmptySequences='0' addstatus='1' closeallpages='1'>
 2692:       <nextstate>PAGESIZE</nextstate>
 2693:       <filterfunc>return $isNotMap; </filterfunc>
 2694:       <valuefunc>return $symbFilter;</valuefunc>
 2695:       $start_new_option
 2696:     </resource>
 2697:   </state>
 2698: ALL_PROBLEMS
 2699: 
 2700: 	if ($helper->{VARS}->{'assignment'}) {
 2701: 	    push @{$printChoices}, [&mt("Selected <b>Problems</b> from folder <b><i>[_1]</i></b> for <b>selected people</b>",$sequenceTitle), 'problems_for_students', 'CHOOSE_STUDENTS'];
 2702: 	    push @{$printChoices}, [&mt("Selected <b>Problems</b> from folder <b><i>[_1]</i></b> for <b>CODEd assignments</b>",$sequenceTitle), 'problems_for_anon', 'CHOOSE_ANON1'];
 2703: 	}
 2704: 
 2705: 	my $randomly_ordered_warning = 
 2706: 	    &get_randomly_ordered_warning($helper,$map);
 2707: 
 2708: 	# resource_selector will hold a few states that:
 2709: 	#   - Allow resources to be selected for printing.
 2710: 	#   - Determine pagination between assignments.
 2711: 	#   - Determine how many assignments should be bundled into a single PDF.
 2712:         # TODO:
 2713: 	#    Probably good to do things like separate this up into several vars, each
 2714: 	#    with one state, and use REGEXPs at inclusion time to set state names
 2715: 	#    and next states for better mix and match capability
 2716: 	#
 2717: 	my $resource_selector=<<RESOURCE_SELECTOR;
 2718:     <state name="SELECT_PROBLEMS" title="Select resources to print">
 2719:     $randomly_ordered_warning
 2720: 
 2721:    <nextstate>PRINT_FORMATTING</nextstate> 
 2722:    <message><br /><big><i><b>Select resources for the assignment</b></i></big><br /></message>
 2723:     <resource variable="RESOURCES" multichoice="1" addstatus="1" 
 2724:               closeallpages="1">
 2725:       <filterfunc>return $isProblem;</filterfunc>
 2726:       <mapurl>$map</mapurl>
 2727:       <valuefunc>return $symbFilter;</valuefunc>
 2728:       $start_new_option
 2729:       </resource>
 2730:     </state>
 2731:     <state name="PRINT_FORMATTING" title="How should results be printed?">
 2732:     <message><br /><big><i><b>How should the results be printed?</b></i></big><br /></message>
 2733:     <choices variable="EMPTY_PAGES">
 2734:       <choice computer='0'>Start each student\'s assignment on a new page/column (add a pagefeed after each assignment)</choice>
 2735:       <choice computer='1'>Add one empty page/column after each student\'s assignment</choice>
 2736:       <choice computer='2'>Add two empty pages/column after each student\'s assignment</choice>
 2737:       <choice computer='3'>Add three empty pages/column after each student\'s assignment</choice>
 2738:     </choices>
 2739:     <nextstate>PAGESIZE</nextstate>
 2740:     <message><hr width='33%' /><b>How do you want assignments split into PDF files? </b></message>
 2741:     <choices variable="SPLIT_PDFS">
 2742:        <choice computer="all">All assignments in a single PDF file</choice>
 2743:        <choice computer="sections">Each PDF contains exactly one section</choice>
 2744:        <choice computer="oneper">Each PDF contains exactly one assignment</choice>
 2745:        <choice computer="usenumber" relatedvalue="NUMBER_TO_PRINT">
 2746:             Specify the number of assignments per PDF:</choice>
 2747:     </choices>
 2748:     </state>
 2749: RESOURCE_SELECTOR
 2750: 
 2751:         &Apache::lonxml::xmlparse($r, 'helper', <<CHOOSE_STUDENTS);
 2752:   <state name="CHOOSE_STUDENTS" title="Select Students and Resources">
 2753:       <message><b>Select sorting order of printout</b> </message>
 2754:     <choices variable='student_sort'>
 2755:       <choice computer='0'>Sort by section then student</choice>
 2756:       <choice computer='1'>Sort by students across sections.</choice>
 2757:     </choices>
 2758:       <message><br /><hr /><br /> </message>
 2759:       <student multichoice='1' variable="STUDENTS" nextstate="SELECT_PROBLEMS" coursepersonnel="1"/>
 2760:   </state>
 2761:     $resource_selector
 2762: CHOOSE_STUDENTS
 2763: 
 2764: 	my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 2765: 	my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 2766:         my @names=&Apache::lonnet::getkeys('CODEs',$cdom,$cnum);
 2767: 	my $namechoice='<choice></choice>';
 2768: 	foreach my $name (sort {uc($a) cmp uc($b)} @names) {
 2769: 	    if ($name =~ /^error: 2 /) { next; }
 2770: 	    if ($name =~ /^type\0/) { next; }
 2771: 	    $namechoice.='<choice computer="'.$name.'">'.$name.'</choice>';
 2772: 	}
 2773: 
 2774: 
 2775: 	my %code_values;
 2776: 	my %codes_to_print;
 2777: 	foreach my $key (@names) {
 2778: 	    %code_values = &Apache::grades::get_codes($key, $cdom, $cnum);
 2779: 	    foreach my $key (keys(%code_values)) {
 2780: 		$codes_to_print{$key} = 1;
 2781: 	    }
 2782: 	}
 2783: 
 2784: 	my $code_selection;
 2785: 	foreach my $code (sort {uc($a) cmp uc($b)} (keys(%codes_to_print))) {
 2786: 	    my $choice  = $code;
 2787: 	    if ($code =~ /^[A-Z]+$/) { # Alpha code
 2788: 		$choice = &letters_to_num($code);
 2789: 	    }
 2790: 	    push(@{$helper->{DATA}{ALL_CODE_CHOICES}},[$code,$choice]);
 2791: 	}
 2792: 	if (%codes_to_print) {
 2793: 	    $code_selection .='   
 2794: 	    <message><b>Choose single CODE from list:</b></message>
 2795: 		<message></td><td></message>
 2796: 		<dropdown variable="CODE_SELECTED_FROM_LIST" multichoice="0" allowempty="0">
 2797:                   <choice></choice>
 2798:                   <exec>
 2799:                      push(@{$state->{CHOICES}},@{$helper->{DATA}{ALL_CODE_CHOICES}});
 2800:                   </exec>
 2801: 		</dropdown>
 2802: 	    <message></td></tr><tr><td></message>
 2803:             '.$/;
 2804: 
 2805: 	}
 2806: 
 2807: 	
 2808: 	open(FH,$Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab');
 2809: 	my $codechoice='';
 2810: 	foreach my $line (<FH>) {
 2811: 	    my ($name,$description,$code_type,$code_length)=
 2812: 		(split(/:/,$line))[0,1,2,4];
 2813: 	    if ($code_length > 0 && 
 2814: 		$code_type =~/^(letter|number|-1)/) {
 2815: 		$codechoice.='<choice computer="'.$name.'">'.$description.'</choice>';
 2816: 	    }
 2817: 	}
 2818: 	if ($codechoice eq '') {
 2819: 	    $codechoice='<choice computer="default">Default</choice>';
 2820: 	}
 2821:         &Apache::lonxml::xmlparse($r, 'helper', <<CHOOSE_ANON1);
 2822:   <state name="CHOOSE_ANON1" title="Specify CODEd Assignments">
 2823:     <nextstate>SELECT_PROBLEMS</nextstate>
 2824:     <message><h4>Fill out one of the forms below</h4></message>
 2825:     <message><br /><hr /> <br /></message>
 2826:     <message><h3>Generate new CODEd Assignments</h3></message>
 2827:     <message><table><tr><td><b>Number of CODEd assignments to print:</b></td><td></message>
 2828:     <string variable="NUMBER_TO_PRINT_TOTAL" maxlength="5" size="5">
 2829:        <validator>
 2830: 	if (((\$helper->{'VARS'}{'NUMBER_TO_PRINT_TOTAL'}+0) < 1) &&
 2831: 	    !\$helper->{'VARS'}{'REUSE_OLD_CODES'}                &&
 2832:             !\$helper->{'VARS'}{'SINGLE_CODE'}                    &&
 2833: 	    !\$helper->{'VARS'}{'CODE_SELECTED_FROM_LIST'}) {
 2834: 	    return "You need to specify the number of assignments to print";
 2835: 	}
 2836: 	return undef;
 2837:        </validator>
 2838:     </string>
 2839:     <message></td></tr><tr><td></message>
 2840:     <message><b>Names to save the CODEs under for later:</b></message>
 2841:     <message></td><td></message>
 2842:     <string variable="ANON_CODE_STORAGE_NAME" maxlength="50" size="20" />
 2843:     <message></td></tr><tr><td></message>
 2844:     <message><b>Bubble sheet type:</b></message>
 2845:     <message></td><td></message>
 2846:     <dropdown variable="CODE_OPTION" multichoice="0" allowempty="0">
 2847:     $codechoice
 2848:     </dropdown>
 2849:     <message></td></tr><tr><td colspan="2"></td></tr><tr><td></message>
 2850:     <message></td></tr><tr><td></table></message>
 2851:     <message><br /><hr /><h3>Print a Specific CODE </h3><br /><table></message>
 2852:     <message><tr><td><b>Enter a CODE to print:</b></td><td></message>
 2853:     <string variable="SINGLE_CODE" size="10">
 2854:         <validator>
 2855: 	   if(!\$helper->{'VARS'}{'NUMBER_TO_PRINT_TOTAL'}           &&
 2856: 	      !\$helper->{'VARS'}{'REUSE_OLD_CODES'}                 &&
 2857: 	      !\$helper->{'VARS'}{'CODE_SELECTED_FROM_LIST'}) {
 2858: 	      return &Apache::lonprintout::is_code_valid(\$helper->{'VARS'}{'SINGLE_CODE'},
 2859: 						      \$helper->{'VARS'}{'CODE_OPTION'});
 2860: 	   } else {
 2861: 	       return undef;	# Other forces control us.
 2862: 	   }
 2863:         </validator>
 2864:     </string>
 2865:     <message></td></tr><tr><td></message>
 2866:         $code_selection
 2867:     <message></td></tr></table></message>
 2868:     <message><hr /><h3>Reprint a Set of Saved CODEs</h3><table><tr><td></message>
 2869:     <message><b>Select saved CODEs:</b></message>
 2870:     <message></td><td></message>
 2871:     <dropdown variable="REUSE_OLD_CODES">
 2872:         $namechoice
 2873:     </dropdown>
 2874:     <message></td></tr></table></message>
 2875:   </state>
 2876:   $resource_selector
 2877: CHOOSE_ANON1
 2878: 
 2879: 
 2880: 	if ($helper->{VARS}->{'assignment'}) {
 2881: 	    push @{$printChoices}, [&mt("Selected <b>Resources</b> from folder <b><i>[_1]</i></b> for <b>selected people</b>",$sequenceTitle), 'resources_for_students', 'CHOOSE_STUDENTS1'];
 2882: 	    push @{$printChoices}, [&mt("Selected <b>Resources</b> from folder <b><i>[_1]</i></b> for <b>CODEd assignments</b>",$sequenceTitle), 'resources_for_anon', 'CHOOSE_ANON2'];
 2883: 	}
 2884: 	    
 2885: 
 2886: 	$resource_selector=<<RESOURCE_SELECTOR;
 2887:     <state name="SELECT_RESOURCES" title="Select Resources">
 2888:     $randomly_ordered_warning
 2889: 
 2890:     <nextstate>PRINT_FORMATTING</nextstate>
 2891:     <message><br /><big><i><b>Select resources for the assignment</b></i></big><br /></message>
 2892:     <resource variable="RESOURCES" multichoice="1" addstatus="1" 
 2893:               closeallpages="1">
 2894:       <filterfunc>return $isNotMap;</filterfunc>
 2895:       <mapurl>$map</mapurl>
 2896:       <valuefunc>return $symbFilter;</valuefunc>
 2897:       $start_new_option
 2898:       </resource>
 2899:     </state>
 2900:     <state name="PRINT_FORMATTING" title="Format of the print job">
 2901:     <nextstate>NUMBER_PER_PDF</nextstate>
 2902:     <message><br /><big><i><b>How should the results be printed?</b></i></big><br /></message>
 2903:     <choices variable="EMPTY_PAGES">
 2904:       <choice computer='0'>Start each student\'s assignment on a new page/column (add a pagefeed after each assignment)</choice>
 2905:       <choice computer='1'>Add one empty page/column after each student\'s assignment</choice>
 2906:       <choice computer='2'>Add two empty pages/column after each student\'s assignment</choice>
 2907:       <choice computer='3'>Add three empty pages/column after each student\'s assignment</choice>
 2908:     </choices>
 2909:     <nextstate>PAGESIZE</nextstate>
 2910:     <message><hr width='33%' /><b>How do you want assignments split into PDF files? </b></message>
 2911:     <choices variable="SPLIT_PDFS">
 2912:        <choice computer="all">All assignments in a single PDF file</choice>
 2913:        <choice computer="sections">Each PDF contains exactly one section</choice>
 2914:        <choice computer="oneper">Each PDF contains exactly one assignment</choice>
 2915:        <choice computer="usenumber" relatedvalue="NUMBER_TO_PRINT">
 2916:            Specify the number of assignments per PDF:</choice>
 2917:     </choices>
 2918:     </state>
 2919: RESOURCE_SELECTOR
 2920: 
 2921: 	&Apache::lonxml::xmlparse($r, 'helper', <<CHOOSE_STUDENTS1);
 2922:   <state name="CHOOSE_STUDENTS1" title="Select Students and Resources">
 2923:     <choices variable='student_sort'>
 2924:       <choice computer='0'>Sort by section then student</choice>
 2925:       <choice computer='1'>Sort by students across sections.</choice>
 2926:     </choices>
 2927:     <message><br /><hr /><br /></message>
 2928:     <student multichoice='1' variable="STUDENTS" nextstate="SELECT_RESOURCES" coursepersonnel="1" />
 2929: 
 2930:     </state>
 2931:     $resource_selector
 2932: CHOOSE_STUDENTS1
 2933: 
 2934: 	&Apache::lonxml::xmlparse($r, 'helper', <<CHOOSE_ANON2);
 2935:   <state name="CHOOSE_ANON2" title="Select CODEd Assignments">
 2936:     <nextstate>SELECT_RESOURCES</nextstate>
 2937:     <message><h4>Fill out one of the forms below</h4></message>
 2938:     <message><br /><hr /> <br /></message>
 2939:     <message><h3>Generate new CODEd Assignments</h3></message>
 2940:     <message><table><tr><td><b>Number of CODEd assignments to print:</b></td><td></message>
 2941:     <string variable="NUMBER_TO_PRINT_TOTAL" maxlength="5" size="5">
 2942:        <validator>
 2943: 	if (((\$helper->{'VARS'}{'NUMBER_TO_PRINT_TOTAL'}+0) < 1) &&
 2944: 	    !\$helper->{'VARS'}{'REUSE_OLD_CODES'}                &&
 2945: 	    !\$helper->{'VARS'}{'SINGLE_CODE'}                   &&
 2946: 	    !\$helper->{'VARS'}{'CODE_SELECTED_FROM_LIST'}) {
 2947: 	    return "You need to specify the number of assignments to print";
 2948: 	}
 2949: 	return undef;
 2950:        </validator>
 2951:     </string>
 2952:     <message></td></tr><tr><td></message>
 2953:     <message><b>Names to save the CODEs under for later:</b></message>
 2954:     <message></td><td></message>
 2955:     <string variable="ANON_CODE_STORAGE_NAME" maxlength="50" size="20" />
 2956:     <message></td></tr><tr><td></message>
 2957:     <message><b>Bubble sheet type:</b></message>
 2958:     <message></td><td></message>
 2959:     <dropdown variable="CODE_OPTION" multichoice="0" allowempty="0">
 2960:     $codechoice
 2961:     </dropdown>
 2962:     <message></td></tr><tr><td></table></message>
 2963:     <message><br /><hr /><h3>Print a Specific CODE </h3><br /><table></message>
 2964:     <message><tr><td><b>Enter a CODE to print:</b></td><td></message>
 2965:     <string variable="SINGLE_CODE" size="10">
 2966:         <validator>
 2967: 	   if(!\$helper->{'VARS'}{'NUMBER_TO_PRINT_TOTAL'}           &&
 2968: 	      !\$helper->{'VARS'}{'REUSE_OLD_CODES'}                 &&
 2969: 	      !\$helper->{'VARS'}{'CODE_SELECTED_FROM_LIST'}) {
 2970: 	      return &Apache::lonprintout::is_code_valid(\$helper->{'VARS'}{'SINGLE_CODE'},
 2971: 						      \$helper->{'VARS'}{'CODE_OPTION'});
 2972: 	   } else {
 2973: 	       return undef;	# Other forces control us.
 2974: 	   }
 2975:         </validator>
 2976:     </string>
 2977:     <message></td></tr><tr><td></message>
 2978:         $code_selection
 2979:     <message></td></tr></table></message>
 2980:     <message><hr /><h3>Reprint a Set of Saved CODEs</h3><table><tr><td></message>
 2981:     <message><b>Select saved CODEs:</b></message>
 2982:     <message></td><td></message>
 2983:     <dropdown variable="REUSE_OLD_CODES">
 2984:         $namechoice
 2985:     </dropdown>
 2986:     <message></td></tr></table></message>
 2987:   </state>
 2988:     $resource_selector
 2989: CHOOSE_ANON2
 2990:     }
 2991: 
 2992:     # FIXME: That RE should come from a library somewhere.
 2993:     if (($perm{'pav'} 
 2994: 	&& $subdir ne $Apache::lonnet::perlvar{'lonDocRoot'}.'/res/'
 2995: 	&& (defined($helper->{'VARS'}->{'construction'})
 2996: 	    ||
 2997: 	    (&Apache::lonnet::allowed('bre',$subdir) eq 'F'
 2998: 	     && 
 2999: 	     $helper->{VARS}->{'postdata'}=~/\.(problem|exam|quiz|assess|survey|form|library|page|xml|html|htm|xhtml|xhtm)/)
 3000: 	    )) 
 3001: 	&& $helper->{VARS}->{'assignment'} eq ""
 3002: 	) {
 3003: 
 3004: 	my $pretty_dir = &Apache::lonnet::hreflocation($subdir);
 3005:         push @{$printChoices}, [&mt("Selected <b>Problems</b> from current subdirectory <b><i>[_1]</i></b>",$pretty_dir), 'problems_from_directory', 'CHOOSE_FROM_SUBDIR'];
 3006:         my $xmlfrag = <<CHOOSE_FROM_SUBDIR;
 3007:   <state name="CHOOSE_FROM_SUBDIR" title="Select File(s) from <b><small>$pretty_dir</small></b> to print">
 3008: 
 3009:     <files variable="FILES" multichoice='1'>
 3010:       <nextstate>PAGESIZE</nextstate>
 3011:       <filechoice>return '$subdir';</filechoice>
 3012: CHOOSE_FROM_SUBDIR
 3013:         
 3014:         # this is broken up because I really want interpolation above,
 3015:         # and I really DON'T want it below
 3016:         $xmlfrag .= <<'CHOOSE_FROM_SUBDIR';
 3017:       <filefilter>return Apache::lonhelper::files::not_old_version($filename) &&
 3018: 	  $filename =~ m/\.(problem|exam|quiz|assess|survey|form|library)$/;
 3019:       </filefilter>
 3020:       </files>
 3021:     </state>
 3022: CHOOSE_FROM_SUBDIR
 3023:         &Apache::lonxml::xmlparse($r, 'helper', $xmlfrag);
 3024:     }
 3025: 
 3026:     # Allow the user to select any sequence in the course, feed it to
 3027:     # another resource selector for that sequence
 3028:     if (!$helper->{VARS}->{'construction'} && !$is_published) {
 3029: 	push @$printChoices, [&mtn("Selected <b>Resources</b> from <b>selected folder</b> in course"),
 3030: 			      'select_sequences', 'CHOOSE_SEQUENCE'];
 3031: 	my $escapedSequenceName = $helper->{VARS}->{'SEQUENCE'};
 3032: 	#Escape apostrophes and backslashes for Perl
 3033: 	$escapedSequenceName =~ s/\\/\\\\/g;
 3034: 	$escapedSequenceName =~ s/'/\\'/g;
 3035: 	&Apache::lonxml::xmlparse($r, 'helper', <<CHOOSE_FROM_ANY_SEQUENCE);
 3036:   <state name="CHOOSE_SEQUENCE" title="Select Sequence To Print From">
 3037:     <message>Select the sequence to print resources from:</message>
 3038:     <resource variable="SEQUENCE">
 3039:       <nextstate>CHOOSE_FROM_ANY_SEQUENCE</nextstate>
 3040:       <filterfunc>return \$res->is_sequence;</filterfunc>
 3041:       <valuefunc>return $urlValue;</valuefunc>
 3042:       <choicefunc>return \$res->hasResource(\$res,sub { return !\$_[0]->is_sequence() },0,0);
 3043: 	</choicefunc>
 3044:       </resource>
 3045:     </state>
 3046:   <state name="CHOOSE_FROM_ANY_SEQUENCE" title="Select Resources To Print">
 3047:     <message>(mark desired resources then click "next" button) <br /></message>
 3048:     <resource variable="RESOURCES" multichoice="1" toponly='1' addstatus="1"
 3049:               closeallpages="1">
 3050:       <nextstate>PAGESIZE</nextstate>
 3051:       <filterfunc>return $isNotMap</filterfunc>
 3052:       <mapurl evaluate='1'>return '$escapedSequenceName';</mapurl>
 3053:       <valuefunc>return $symbFilter;</valuefunc>
 3054:       $start_new_option
 3055:       </resource>
 3056:     </state>
 3057: CHOOSE_FROM_ANY_SEQUENCE
 3058: }
 3059: 
 3060:     # Generate the first state, to select which resources get printed.
 3061:     Apache::lonhelper::state->new("START", "Select Printing Options:");
 3062:     $paramHash = Apache::lonhelper::getParamHash();
 3063:     $paramHash->{MESSAGE_TEXT} = "";
 3064:     Apache::lonhelper::message->new();
 3065:     $paramHash = Apache::lonhelper::getParamHash();
 3066:     $paramHash->{'variable'} = 'PRINT_TYPE';
 3067:     $paramHash->{CHOICES} = $printChoices;
 3068:     Apache::lonhelper::choices->new();
 3069: 
 3070:     my $startedTable = 0; # have we started an HTML table yet? (need
 3071:                           # to close it later)
 3072: 
 3073:     if (($perm{'pav'} and &Apache::lonnet::allowed('vgr',$env{'request.course.id'})) or 
 3074: 	($helper->{VARS}->{'construction'} eq '1')) {
 3075: 	addMessage("<hr width='33%' /><table><tr><td align='right'>".
 3076:                    '<label for="ANSWER_TYPE_forminput">'.
 3077:                    &mt('Print').
 3078:                    "</label>: </td><td>");
 3079:         $paramHash = Apache::lonhelper::getParamHash();
 3080: 	$paramHash->{'variable'} = 'ANSWER_TYPE';   
 3081: 	$helper->declareVar('ANSWER_TYPE');         
 3082:         $paramHash->{CHOICES} = [
 3083:                                    ['Without Answers', 'yes'],
 3084:                                    ['With Answers', 'no'],
 3085:                                    ['Only Answers', 'only']
 3086:                                 ];
 3087:         Apache::lonhelper::dropdown->new();
 3088: 	addMessage("</td></tr>");
 3089: 	$startedTable = 1;
 3090:     }
 3091: 
 3092:     if ($perm{'pav'}) {
 3093: 	if (!$startedTable) {
 3094: 	    addMessage("<hr width='33%' /><table><tr><td align='right'>".
 3095:                        '<label for="LATEX_TYPE_forminput">'.
 3096:                        &mt('LaTeX mode').
 3097:                        "</label>: </td><td>");
 3098: 	    $startedTable = 1;
 3099: 	} else {
 3100: 	    addMessage("<tr><td align='right'>".
 3101:                        '<label for="LATEX_TYPE_forminput">'.
 3102:                         &mt('LaTeX mode').
 3103:                        "</label>: </td><td>");
 3104: 	}
 3105:         $paramHash = Apache::lonhelper::getParamHash();
 3106: 	$paramHash->{'variable'} = 'LATEX_TYPE';   
 3107: 	$helper->declareVar('LATEX_TYPE');  
 3108: 	if ($helper->{VARS}->{'construction'} eq '1') {       
 3109: 	    $paramHash->{CHOICES} = [
 3110: 				     ['standard LaTeX mode', 'standard'], 
 3111: 				     ['LaTeX batchmode', 'batchmode'], ];
 3112: 	} else {
 3113: 	    $paramHash->{CHOICES} = [
 3114: 				     ['LaTeX batchmode', 'batchmode'],
 3115: 				     ['standard LaTeX mode', 'standard'] ];
 3116: 	}
 3117:         Apache::lonhelper::dropdown->new();
 3118:  
 3119: 	addMessage("</td></tr><tr><td align='right'>".
 3120:                    '<label for="TABLE_CONTENTS_forminput">'.
 3121:                    &mt('Print Table of Contents').
 3122:                    "</label>: </td><td>");
 3123:         $paramHash = Apache::lonhelper::getParamHash();
 3124: 	$paramHash->{'variable'} = 'TABLE_CONTENTS';   
 3125: 	$helper->declareVar('TABLE_CONTENTS');         
 3126:         $paramHash->{CHOICES} = [
 3127:                                    ['No', 'no'],
 3128:                                    ['Yes', 'yes'] ];
 3129:         Apache::lonhelper::dropdown->new();
 3130: 	addMessage("</td></tr>");
 3131:         
 3132: 	if (not $helper->{VARS}->{'construction'}) {
 3133: 	    addMessage("<tr><td align='right'>".
 3134:                        '<label for="TABLE_INDEX_forminput">'.
 3135:                        &mt('Print Index').
 3136:                        "</label>: </td><td>");
 3137: 	    $paramHash = Apache::lonhelper::getParamHash();
 3138: 	    $paramHash->{'variable'} = 'TABLE_INDEX';   
 3139: 	    $helper->declareVar('TABLE_INDEX');         
 3140: 	    $paramHash->{CHOICES} = [
 3141: 				     ['No', 'no'],
 3142: 				     ['Yes', 'yes'] ];
 3143: 	    Apache::lonhelper::dropdown->new();
 3144: 	    addMessage("</td></tr>");
 3145: 	    addMessage("<tr><td align='right'>".
 3146:                        '<label for="PRINT_DISCUSSIONS_forminput">'.
 3147:                        &mt('Print Discussions').
 3148:                        "</label>: </td><td>");
 3149: 	    $paramHash = Apache::lonhelper::getParamHash();
 3150: 	    $paramHash->{'variable'} = 'PRINT_DISCUSSIONS';   
 3151: 	    $helper->declareVar('PRINT_DISCUSSIONS');         
 3152: 	    $paramHash->{CHOICES} = [
 3153: 				     ['No', 'no'],
 3154: 				     ['Yes', 'yes'] ];
 3155: 	    Apache::lonhelper::dropdown->new();
 3156: 	    addMessage("</td></tr>");
 3157: 
 3158: 	    # Prompt for printing annotations too.
 3159: 		
 3160: 	    addMessage("<tr><td align='right'>".
 3161: 		       '<label for="PRINT_ANNOTATIONS_forminput">'.
 3162: 		       &mt('Print Annotations').
 3163: 		       "</label>:</td><td>");
 3164: 	    $paramHash = Apache::lonhelper::getParamHash();
 3165: 	    $paramHash->{'variable'} = "PRINT_ANNOTATIONS";
 3166: 	    $helper->declareVar("PRINT_ANNOTATIONS");
 3167: 	    $paramHash->{CHOICES} = [
 3168: 				     ['No', 'no'],
 3169: 				     ['Yes', 'yes']];
 3170: 	    Apache::lonhelper::dropdown->new();
 3171: 	    addMessage("</td></tr>");
 3172: 
 3173: 	    addMessage("<tr><td align = 'right'>  </td><td>");
 3174: 	    $paramHash = Apache::lonhelper::getParamHash();
 3175: 	    $paramHash->{'multichoice'} = "true";
 3176: 	    $paramHash->{'allowempty'}  = "true";
 3177: 	    $paramHash->{'variable'}   = "showallfoils";
 3178: 	    $paramHash->{'CHOICES'} = [ ["Show all foils", "1"] ];
 3179: 	    Apache::lonhelper::choices->new();
 3180: 	    addMessage("</td></tr>");
 3181: 	}
 3182: 
 3183: 	if ($helper->{'VARS'}->{'construction'}) { 
 3184: 	    my $stylevalue='$Apache::lonnet::env{"construct.style"}';
 3185:             my $randseedtext=&mt("Use random seed");
 3186:             my $stylefiletext=&mt("Use style file");
 3187:             my $selectfiletext=&mt("Select style file");
 3188: 
 3189: 	    my $xmlfrag .= <<"RNDSEED";
 3190: 	    <message><tr><td align='right'>
 3191:             <label for="curseed_forminput">$randseedtext</label>:
 3192:             </td><td></message>
 3193: 	    <string variable="curseed" size="15" maxlength="15">
 3194: 		<defaultvalue>
 3195: 	            return $helper->{VARS}->{'curseed'};
 3196: 	        </defaultvalue>
 3197: 	    </string>
 3198: 	     <message></td></tr><tr><td align="right">
 3199:              <label for="style_file">$stylefiletext</label>:
 3200:              </td><td></message>
 3201:              <string variable="style_file" size="40">
 3202: 		<defaultvalue>
 3203: 	            return $stylevalue;
 3204: 	        </defaultvalue>
 3205:              </string><message>&nbsp; <a href="javascript:openbrowser('helpform','style_file_forminput','sty')">$selectfiletext</a> </td></tr><tr><td></td><td align="left"></message>
 3206: 	     <choices allowempty="1" multichoice="true" variable="showallfoils">
 3207:                 <choice computer="1">Show all foils</choice>
 3208:              </choices>
 3209: 	     <message></td></tr></message>
 3210: RNDSEED
 3211:             &Apache::lonxml::xmlparse($r, 'helper', $xmlfrag);
 3212: 
 3213: 
 3214: 	    addMessage("<tr><td>Problem Type:</td><td>");
 3215: 	    #
 3216: 	    # Initial value from construction space:
 3217: 	    #
 3218: 	    if (!$helper->{VARS}->{'probstatus'} && $env{'form.problemtype'}) {
 3219: 		$helper->{VARS}->{'probstatus'} = $env{'form.problemtype'};	# initial value
 3220: 	    }
 3221: 	    $xmlfrag = << "PROBTYPE";
 3222: 		<dropdown variable="probstatus" multichoice="0" allowempty="0">
 3223: 		   <defaultvalue>
 3224: 		      return "$helper->{VARS}->{'probstatus'}";
 3225:                    </defaultvalue>
 3226: 		   <choice computer="problem">Homework Problem</choice>
 3227: 		   <choice computer="exam">Exam Problem</choice>
 3228: 		   <choice computer="survey">Survey question</choice>
 3229: 		</dropdown>
 3230: PROBTYPE
 3231:             &Apache::lonxml::xmlparse($r, 'helper', $xmlfrag);
 3232: 	    
 3233: 	    addMessage("</td></tr>"); 
 3234: 
 3235: 	} 
 3236:     }
 3237: 
 3238: 
 3239: 
 3240: 
 3241:     if ($startedTable) {
 3242: 	addMessage("</table>");
 3243:     }
 3244: 
 3245:     Apache::lonprintout::page_format_state->new("FORMAT");
 3246: 
 3247:     # Generate the PAGESIZE state which will offer the user the margin
 3248:     # choices if they select one column
 3249:     Apache::lonhelper::state->new("PAGESIZE", "Set Margins");
 3250:     Apache::lonprintout::page_size_state->new('pagesize', 'FORMAT', 'FINAL');
 3251: 
 3252: 
 3253:     $helper->process();
 3254: 
 3255: 
 3256:     # MANUAL BAILOUT CONDITION:
 3257:     # If we're in the "final" state, bailout and return to handler
 3258:     if ($helper->{STATE} eq 'FINAL') {
 3259:         return $helper;
 3260:     }    
 3261: 
 3262:     $r->print($helper->display());
 3263:     if ($helper->{STATE} eq 'START') {
 3264: 	&recently_generated($r);
 3265:     }
 3266:     &Apache::lonhelper::unregisterHelperTags();
 3267: 
 3268:     return OK;
 3269: }
 3270: 
 3271: 
 3272: 1;
 3273: 
 3274: package Apache::lonprintout::page_format_state;
 3275: 
 3276: =pod
 3277: 
 3278: =head1 Helper element: page_format_state
 3279: 
 3280: See lonhelper.pm documentation for discussion of the helper framework.
 3281: 
 3282: Apache::lonprintout::page_format_state is an element that gives the 
 3283: user an opportunity to select the page layout they wish to print 
 3284: with: Number of columns, portrait/landscape, and paper size. If you 
 3285: want to change the paper size choices, change the @paperSize array 
 3286: contents in this package.
 3287: 
 3288: page_format_state is always directly invoked in lonprintout.pm, so there
 3289: is no tag interface. You actually pass parameters to the constructor.
 3290: 
 3291: =over 4
 3292: 
 3293: =item * B<new>(varName): varName is where the print information will be stored in the format FIXME.
 3294: 
 3295: =back
 3296: 
 3297: =cut
 3298: 
 3299: use Apache::lonhelper;
 3300: 
 3301: no strict;
 3302: @ISA = ("Apache::lonhelper::element");
 3303: use strict;
 3304: use Apache::lonlocal;
 3305: use Apache::lonnet;
 3306: 
 3307: my $maxColumns = 2;
 3308: # it'd be nice if these all worked
 3309: #my @paperSize = ("letter [8 1/2x11 in]", "legal [8 1/2x14 in]", 
 3310: #                 "tabloid (ledger) [11x17 in]", "executive [7 1/2x10 in]",
 3311: #                 "a2 [420x594 mm]", "a3 [297x420 mm]", "a4 [210x297 mm]", 
 3312: #                 "a5 [148x210 mm]", "a6 [105x148 mm]" );
 3313: my @paperSize = ("letter [8 1/2x11 in]", "legal [8 1/2x14 in]", 
 3314: 		 "a4 [210x297 mm]");
 3315: 
 3316: # Tentative format: Orientation (L = Landscape, P = portrait) | Colnum |
 3317: #                   Paper type
 3318: 
 3319: sub new { 
 3320:     my $self = Apache::lonhelper::element->new();
 3321: 
 3322:     shift;
 3323: 
 3324:     $self->{'variable'} = shift;
 3325:     my $helper = Apache::lonhelper::getHelper();
 3326:     $helper->declareVar($self->{'variable'});
 3327:     bless($self);
 3328:     return $self;
 3329: }
 3330: 
 3331: sub render {
 3332:     my $self = shift;
 3333:     my $helper = Apache::lonhelper::getHelper();
 3334:     my $result = '';
 3335:     my $var = $self->{'variable'};
 3336:     my $PageLayout=&mt('Page layout');
 3337:     my $NumberOfColumns=&mt('Number of columns');
 3338:     my $PaperType=&mt('Paper type');
 3339:     my $landscape=&mt('Landscape');
 3340:     my $portrait=&mt('Portrait');
 3341:     $result .= <<STATEHTML;
 3342: 
 3343: <hr width="33%" />
 3344: <table cellpadding="3">
 3345:   <tr>
 3346:     <td align="center"><b>$PageLayout</b></td>
 3347:     <td align="center"><b>$NumberOfColumns</b></td>
 3348:     <td align="center"><b>$PaperType</b></td>
 3349:   </tr>
 3350:   <tr>
 3351:     <td>
 3352:       <label><input type="radio" name="${var}.layout" value="L" /> $landscape </label><br />
 3353:       <label><input type="radio" name="${var}.layout" value="P" checked='1'  /> $portrait </label>
 3354:     </td>
 3355:     <td align="center">
 3356:       <select name="${var}.cols">
 3357: STATEHTML
 3358: 
 3359:     my $i;
 3360:     for ($i = 1; $i <= $maxColumns; $i++) {
 3361:         if ($i == 2) {
 3362:             $result .= "<option value='$i' selected>$i</option>\n";
 3363:         } else {
 3364:             $result .= "<option value='$i'>$i</option>\n";
 3365:         }
 3366:     }
 3367: 
 3368:     $result .= "</select></td><td>\n";
 3369:     $result .= "<select name='${var}.paper'>\n";
 3370: 
 3371:     my %parmhash=&Apache::lonnet::coursedescription($env{'request.course.id'});
 3372:     my $DefaultPaperSize=lc($parmhash{'default_paper_size'});
 3373:     $DefaultPaperSize=~s/\s//g;
 3374:     if ($DefaultPaperSize eq '') {$DefaultPaperSize='letter';}
 3375:     $i = 0;
 3376:     foreach (@paperSize) {
 3377: 	$_=~/(\w+)/;
 3378: 	my $papersize=$1;
 3379:         if ($paperSize[$i]=~/$DefaultPaperSize/) {
 3380:             $result .= "<option selected value='$papersize'>" . $paperSize[$i] . "</option>\n";
 3381:         } else {
 3382:             $result .= "<option value='$papersize'>" . $paperSize[$i] . "</option>\n";
 3383:         }
 3384:         $i++;
 3385:     }
 3386:     $result .= "</select></td></tr></table>";
 3387:     return $result;
 3388: }
 3389: 
 3390: sub postprocess {
 3391:     my $self = shift;
 3392: 
 3393:     my $var = $self->{'variable'};
 3394:     my $helper = Apache::lonhelper->getHelper();
 3395:     $helper->{VARS}->{$var} = 
 3396:         $env{"form.$var.layout"} . '|' . $env{"form.$var.cols"} . '|' .
 3397:         $env{"form.$var.paper"};
 3398:     return 1;
 3399: }
 3400: 
 3401: 1;
 3402: 
 3403: package Apache::lonprintout::page_size_state;
 3404: 
 3405: =pod
 3406: 
 3407: =head1 Helper element: page_size_state
 3408: 
 3409: See lonhelper.pm documentation for discussion of the helper framework.
 3410: 
 3411: Apache::lonprintout::page_size_state is an element that gives the 
 3412: user the opportunity to further refine the page settings if they
 3413: select a single-column page.
 3414: 
 3415: page_size_state is always directly invoked in lonprintout.pm, so there
 3416: is no tag interface. You actually pass parameters to the constructor.
 3417: 
 3418: =over 4
 3419: 
 3420: =item * B<new>(varName): varName is where the print information will be stored in the format FIXME.
 3421: 
 3422: =back
 3423: 
 3424: =cut
 3425: 
 3426: use Apache::lonhelper;
 3427: use Apache::lonnet;
 3428: no strict;
 3429: @ISA = ("Apache::lonhelper::element");
 3430: use strict;
 3431: 
 3432: 
 3433: 
 3434: sub new { 
 3435:     my $self = Apache::lonhelper::element->new();
 3436: 
 3437:     shift; # disturbs me (probably prevents subclassing) but works (drops
 3438:            # package descriptor)... - Jeremy
 3439: 
 3440:     $self->{'variable'} = shift;
 3441:     my $helper = Apache::lonhelper::getHelper();
 3442:     $helper->declareVar($self->{'variable'});
 3443: 
 3444:     # The variable name of the format element, so we can look into 
 3445:     # $helper->{VARS} to figure out whether the columns are one or two
 3446:     $self->{'formatvar'} = shift;
 3447: 
 3448: 
 3449:     $self->{NEXTSTATE} = shift;
 3450:     bless($self);
 3451: 
 3452:     return $self;
 3453: }
 3454: 
 3455: sub render {
 3456:     my $self = shift;
 3457:     my $helper = Apache::lonhelper::getHelper();
 3458:     my $result = '';
 3459:     my $var = $self->{'variable'};
 3460: 
 3461: 
 3462: 
 3463:     if (defined $self->{ERROR_MSG}) {
 3464:         $result .= '<br /><span class="LC_error">' . $self->{ERROR_MSG} . '</span><br />';
 3465:     }
 3466: 
 3467:     my $format = $helper->{VARS}->{$self->{'formatvar'}};
 3468: 
 3469:     # Use format to get sensible defaults for the margins:
 3470: 
 3471: 
 3472:     my ($laystyle, $cols, $papersize) = split(/\|/, $format);
 3473:     ($papersize)                      = split(/ /, $papersize);
 3474: 
 3475: 
 3476:     if ($laystyle eq 'L') {
 3477: 	$laystyle = 'album';
 3478:     } else {
 3479: 	$laystyle = 'book';
 3480:     }
 3481: 
 3482: 
 3483:     my %size;
 3484:     ($size{'width_and_units'},
 3485:      $size{'height_and_units'},
 3486:      $size{'margin_and_units'})=
 3487: 	 &Apache::lonprintout::page_format($papersize, $laystyle, $cols);
 3488:     
 3489:     foreach my $dimension ('width','height','margin') {
 3490: 	($size{$dimension},$size{$dimension.'_unit'}) =
 3491: 	    split(/ +/, $size{$dimension.'_and_units'},2);
 3492:        	
 3493: 	foreach my $unit ('cm','in') {
 3494: 	    $size{$dimension.'_options'} .= '<option ';
 3495: 	    if ($size{$dimension.'_unit'} eq $unit) {
 3496: 		$size{$dimension.'_options'} .= 'selected="selected" ';
 3497: 	    }
 3498: 	    $size{$dimension.'_options'} .= '>'.$unit.'</option>';
 3499: 	}
 3500:     }
 3501: 
 3502:     # Adjust margin for LaTeX margin: .. requires units == cm or in.
 3503: 
 3504:     if ($size{'margin_unit'} eq 'in') {
 3505: 	$size{'margin'} += 1;
 3506:     }  else {
 3507: 	$size{'margin'} += 2.54;
 3508:     }
 3509:     my %text = ('format' => 'How should each column be formatted?',
 3510: 		'width'  => 'Width:',
 3511: 		'height' => 'Height:',
 3512: 		'margin' => 'Left Margin:',);
 3513:     %text = &Apache::lonlocal::texthash(%text);
 3514: 
 3515:     $result .= <<ELEMENTHTML;
 3516: 
 3517: <p>$text{'format'}</p>
 3518: 
 3519: <table cellpadding='3'>
 3520:   <tr>
 3521:     <td align='right'><b>$text{'width'}</b></td>
 3522:     <td align='left'><input type='text' name='$var.width' value="$size{'width'}" size='4' /></td>
 3523:     <td align='left'>
 3524:       <select name='$var.widthunit'>
 3525:       $size{'width_options'}
 3526:       </select>
 3527:     </td>
 3528:   </tr>
 3529:   <tr>
 3530:     <td align='right'><b>$text{'height'}</b></td>
 3531:     <td align='left'><input type='text' name="$var.height" value="$size{'height'}" size='4' /></td>
 3532:     <td align='left'>
 3533:       <select name='$var.heightunit'>
 3534:       $size{'height_options'}
 3535:       </select>
 3536:     </td>
 3537:   </tr>
 3538:   <tr>
 3539:     <td align='right'><b>$text{'margin'}</b></td>
 3540:     <td align='left'><input type='text' name='$var.lmargin' value="$size{'margin'}" size='4' /></td>
 3541:     <td align='left'>
 3542:       <select name='$var.lmarginunit'>
 3543:       $size{'margin_options'}
 3544:       </select>
 3545:     </td>
 3546:   </tr>
 3547: </table>
 3548: 
 3549: <!--<p>Hint: Some instructors like to leave scratch space for the student by
 3550: making the width much smaller than the width of the page.</p>-->
 3551: 
 3552: ELEMENTHTML
 3553: 
 3554:     return $result;
 3555: }
 3556: 
 3557: 
 3558: sub preprocess {
 3559:     my $self = shift;
 3560:     my $helper = Apache::lonhelper::getHelper();
 3561: 
 3562:     my $format = $helper->{VARS}->{$self->{'formatvar'}};
 3563: 
 3564:     #  If the user does not have 'pav' privilege, set default widths and
 3565:     #  on to the next state right away.
 3566:     #
 3567:     if (!$perm{'pav'}) {
 3568: 	my $var = $self->{'variable'};
 3569: 	my $format = $helper->{VARS}->{$self->{'formatvar'}};
 3570: 	
 3571: 	my ($laystyle, $cols, $papersize) = split(/\|/, $format);
 3572: 	($papersize)                      = split(/ /, $papersize);
 3573: 	
 3574: 	
 3575: 	if ($laystyle eq 'L') {
 3576: 	    $laystyle = 'album';
 3577: 	} else {
 3578: 	    $laystyle = 'book';
 3579: 	}
 3580: 	#  Figure out some good defaults for the print out and set them:
 3581: 	
 3582: 	my %size;
 3583: 	($size{'width'},
 3584: 	 $size{'height'},
 3585: 	 $size{'lmargin'})=
 3586: 	     &Apache::lonprintout::page_format($papersize, $laystyle, $cols);
 3587: 	
 3588: 	foreach my $dim ('width', 'height', 'lmargin') {
 3589: 	    my ($value, $units) = split(/ /, $size{$dim});
 3590: 	    	    
 3591: 	    $helper->{VARS}->{"$var.".$dim}      = $value;
 3592: 	    $helper->{VARS}->{"$var.".$dim.'unit'} = $units;
 3593: 	    
 3594: 	}
 3595: 	
 3596: 
 3597: 	# Transition to the next state
 3598: 
 3599: 	$helper->changeState($self->{NEXTSTATE});
 3600:     }
 3601:    
 3602:     return 1;
 3603: }
 3604: 
 3605: sub postprocess {
 3606:     my $self = shift;
 3607: 
 3608:     my $var = $self->{'variable'};
 3609:     my $helper = Apache::lonhelper->getHelper();
 3610:     my $width = $helper->{VARS}->{$var .'.width'} = $env{"form.${var}.width"}; 
 3611:     my $height = $helper->{VARS}->{$var .'.height'} = $env{"form.${var}.height"}; 
 3612:     my $lmargin = $helper->{VARS}->{$var .'.lmargin'} = $env{"form.${var}.lmargin"}; 
 3613:     $helper->{VARS}->{$var .'.widthunit'} = $env{"form.${var}.widthunit"}; 
 3614:     $helper->{VARS}->{$var .'.heightunit'} = $env{"form.${var}.heightunit"}; 
 3615:     $helper->{VARS}->{$var .'.lmarginunit'} = $env{"form.${var}.lmarginunit"}; 
 3616: 
 3617:     my $error = '';
 3618: 
 3619:     # /^-?[0-9]+(\.[0-9]*)?$/ -> optional minus, at least on digit, followed 
 3620:     # by an optional period, followed by digits, ending the string
 3621: 
 3622:     if ($width !~  /^-?[0-9]*(\.[0-9]*)?$/) {
 3623:         $error .= "Invalid width; please type only a number.<br />\n";
 3624:     }
 3625:     if ($height !~  /^-?[0-9]*(\.[0-9]*)?$/) {
 3626:         $error .= "Invalid height; please type only a number.<br />\n";
 3627:     }
 3628:     if ($lmargin !~  /^-?[0-9]*(\.[0-9]*)?$/) {
 3629:         $error .= "Invalid left margin; please type only a number.<br />\n";
 3630:     } else {
 3631: 	# Adjust for LaTeX 1.0 inch margin:
 3632: 
 3633: 	if ($env{"form.${var}.lmarginunit"} eq "in") {
 3634: 	    $helper->{VARS}->{$var.'.lmargin'} = $lmargin - 1;
 3635: 	} else {
 3636: 	    $helper->{VARS}->{$var.'.lmargin'} = $lmargin - 2.54;
 3637: 	}
 3638:     }
 3639: 
 3640:     if (!$error) {
 3641:         Apache::lonhelper::getHelper()->changeState($self->{NEXTSTATE});
 3642:         return 1;
 3643:     } else {
 3644:         $self->{ERROR_MSG} = $error;
 3645:         return 0;
 3646:     }
 3647: }
 3648: 
 3649: 
 3650: 
 3651: __END__
 3652: 

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