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

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

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