File:  [LON-CAPA] / loncom / interface / lonprintout.pm
Revision 1.611: download - view: text, annotated - select for diffs
Wed Dec 21 21:25:40 2011 UTC (12 years, 5 months ago) by www
Branches: MAIN
CVS tags: HEAD
Bug 6455 - Progress Indicator now jQuery. Still need to test across browsers.

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

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