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

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

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