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

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

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