File:  [LON-CAPA] / loncom / interface / lonprintout.pm
Revision 1.627.2.32.2.4: download - view: text, annotated - select for diffs
Tue Jan 17 23:02:49 2023 UTC (16 months, 1 week ago) by raeburn
Branches: version_2_11_4_msu
Diff to branchpoint 1.627.2.32: preferred, unified
- For 2.11.4 (modified)
  Include changes in 1.679, 1.680, 1.681, 1.682

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

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