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

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

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