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

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

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