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

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

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