File:  [LON-CAPA] / loncom / interface / lonprintout.pm
Revision 1.492: download - view: text, annotated - select for diffs
Mon Nov 20 11:01:50 2006 UTC (17 years, 6 months ago) by foxr
Branches: MAIN
CVS tags: version_2_3_X, version_2_3_2, version_2_3_1, version_2_3_0, version_2_2_99_1, version_2_2_99_0, HEAD
Bug 4446 - really ensure that the first problem in anon and
nonanon mode will have the same amount of vertical space
prior to the problem text itself... on the principle that
it is best to be consistent.

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

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