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

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

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