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

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

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