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

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

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