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

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

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