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

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

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