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

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

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