File:  [LON-CAPA] / loncom / interface / lonprintout.pm
Revision 1.553: download - view: text, annotated - select for diffs
Wed May 6 16:19:34 2009 UTC (15 years ago) by bisitz
Branches: MAIN
CVS tags: HEAD, BZ5434-fox
XHTML conform selected/checked/multiple HTML attributes
and optimized spacing

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

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