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

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

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