File:  [LON-CAPA] / loncom / interface / lonprintout.pm
Revision 1.530: download - view: text, annotated - select for diffs
Tue Apr 22 10:24:24 2008 UTC (16 years, 1 month ago) by foxr
Branches: MAIN
CVS tags: HEAD
BZ5691  - correct the latex produced for
¨ and ´ so these entities don't cause print failures.
Note that entities.pm table will replace this RSN in any event, but
we don't like print failures in the meantime.

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

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