File:  [LON-CAPA] / loncom / interface / lonprintout.pm
Revision 1.683: download - view: text, annotated - select for diffs
Sun Jan 15 01:19:50 2023 UTC (16 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Replace use of &mtn() with &mt() and use bracket notation for HTML tags.
- Correction to one transaltion (localized phrase was Problems not Resources).

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

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