--- loncom/interface/lonprintout.pm 2006/08/22 14:44:39 1.482 +++ loncom/interface/lonprintout.pm 2023/04/18 18:48:14 1.627.2.32.2.7 @@ -1,8 +1,7 @@ -# # The LearningOnline Network # Printout # -# $Id: lonprintout.pm,v 1.482 2006/08/22 14:44:39 albertel Exp $ +# $Id: lonprintout.pm,v 1.627.2.32.2.7 2023/04/18 18:48:14 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -23,12 +22,10 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # /home/httpd/html/adm/gpl.txt -# # http://www.lon-capa.org/ # # package Apache::lonprintout; - use strict; use Apache::Constants qw(:common :http); use Apache::lonxml; @@ -39,17 +36,1151 @@ use Apache::grades; use Apache::edit; use Apache::File(); use Apache::lonnavmaps; -use Apache::lonratedt; -use POSIX qw(strftime); +use Apache::admannotations; +use Apache::lonenc; +use Apache::entities; +use Apache::londefdef; +# use Apache::structurelags; # for language management. + +use File::Basename; + +use HTTP::Response; +use LONCAPA::map(); use Apache::lonlocal; use Carp; -use lib '/home/httpd/lib/perl/'; use LONCAPA; + my %perm; my %parmhash; my $resources_printed; +# Global variables that describe errors in ssi calls detected by ssi_with_retries. +# + +my $ssi_error; # True if there was an ssi error. +my $ssi_last_error_resource; # The resource URI that could not be fetched. +my $ssi_last_error; # The error text from the server. (e.g. 500 Server timed out). + +# +# Our ssi max retry count. +# + +my $ssi_retry_count = 5; # Some arbitrary value. + + +# Font size: + +my $font_size = 'normalsize'; # Default is normalsize... + +#---------------------------- Helper helpers. ------------------------- + +## +# Filter function to determine if a resource is a printable sequence. +# +# @param $res -Resource to check. +# +# @return 1 - printable and a resource +# 0 - either notm a sequence or not printable. +# +sub printable_sequence { + my $res = shift; + + # Non-sequences are not listed: + + if (!$res->is_sequence()) { + return 0; + } + + # Person with pav or pfo can always print: + + if ($perm{'pav'} || $perm{'pfo'}) { + return 1; + } + + if ($res->is_sequence()) { + my $symb = $res->symb(); + my $navmap = $res->{NAV_MAP}; + + # Find the first resource in the map: + + my $iterator = $navmap->getIterator($res, undef, undef, 1, 1); + my $first = $iterator->next(); + + while (1) { + if ($first == $iterator->END_ITERATOR) { last; } + if (ref($first) && ! $first->is_sequence()) {last; } + $first = $iterator->next(); + } + + + # Might be an empty map: + + if (!ref($first)) { + return 0; + } + my $partsref = $first->parts(); + my @parts = @$partsref; + my ($open, $close) = $navmap->map_printdates($first, $parts[0]); + return &printable($open, $close); + } + return 0; +} + +# BZ5209: +# Create the states needed to run the helper for incomplete problems from +# the current folder for selected students. +# This includes: +# - A resource selector limited to problems (incompleteness must be +# calculated on a student per student basis. +# - A student selector. +# - Tie in to the FORMAT of the print job. +# +# States: +# CHOOSE_INCOMPLETE_PEOPLE_SEQ - Resource selection. +# CHOOSE_STUDENTS_INCOMPLETE - Student selection. +# CHOOSE_STUDENTS_INCOMPLETE_FORMAT - Format selection +# Parameters: +# helper - the helper which already contains info about the current folder we can +# purloin. +# map - the map for which incomplete problems are to be printed +# nocurrloc - True if printout called from icon/link in Tools in /adm/navmaps +# Return: +# XML that can be parsed by the helper to drive the state machine. +# +sub create_incomplete_folder_selstud_helper { + my ($helper, $map, $nocurrloc) = @_; + + + my $symbFilter = '$res->shown_symb()'; + my $selFilter = '$res->is_problem()'; + + + my $resource_chooser = &generate_resource_chooser('CHOOSE_INCOMPLETE_PEOPLE_SEQ', + 'Select problem(s) to print', + 'multichoice="1" toponly="1" addstatus="1" closeallpages="1" modallink="1" nocurrloc="'.$nocurrloc.'"', + 'RESOURCES', + 'CHOOSE_STUDENTS_INCOMPLETE', + $map, + $selFilter, + '', + $symbFilter, + ''); + + my $student_chooser = &generate_student_chooser('CHOOSE_STUDENTS_INCOMPLETE', + 'student_sort', + 'STUDENTS', + 'CHOOSE_STUDENTS_INCOMPLETE_FORMAT'); + + my $format_chooser = &generate_format_selector($helper, + 'Format of the print job', + 'CHOOSE_STUDENTS_INCOMPLETE_FORMAT'); # end state. + + return $resource_chooser . $student_chooser . $format_chooser; +} + + +# BZ 5209 +# Create the states needed to run the helper for incomplete problems from +# the current folder for selected students. +# This includes: +# - A resource selector limited to problems. (incompleteness must be calculated +# on a student per student basis. +# - A student selector. +# - Tie in to format for the print job. +# States: +# INCOMPLETE_PROBLEMS_COURSE_RESOURCES - Resource selector. +# INCOMPLETE_PROBLEMS_COURSE_STUDENTS - Student selector. +# INCOMPLETE_PROBLEMS_COURSE_FORMAT - Format selection. +# +# Parameters: +# helper - Helper we are creating states for. +# Returns: +# Text that can be parsed by the helper. +# + +sub create_incomplete_course_helper { + my $helper = shift; + + my $filter = '$res->is_problem() || $res->contains_problem() || $res->is_sequence() || $res->is_practice())'; + my $symbfilter = '$res->shown_symb()'; + + my $resource_chooser = &generate_resource_chooser('INCOMPLETE_PROBLEMS_COURSE_RESOURCES', + 'Select problem(s) to print', + 'multichoice = "1" suppressEmptySequences="0" addstatus="1" closeallpagtes="1" modallink="1"', + 'RESOURCES', + 'INCOMPLETE_PROBLEMS_COURSE_STUDENTS', + '', + $filter, + '', + $symbfilter, + ''); + + my $people_chooser = &generate_student_chooser('INCOMPLETE_PROBLEMS_COURSE_STUDENTS', + 'student_sort', + 'STUDENTS', + 'INCOMPLETE_PROBLEMS_COURSE_FORMAT'); + + my $format = &generate_format_selector($helper, + 'Format of the print job', + 'INCOMPLETE_PROBLEMS_COURSE_FORMAT'); # end state. + + return $resource_chooser . $people_chooser . $format; + + +} + +# BZ5209 +# Creates the states needed to run the print helper for a student +# that wants to print his incomplete problems from the current folder. +# Parameters: +# $helper - helper we are generating states for. +# $map - The map for which the student wants incomplete problems. +# $nocurrloc - True if printout called from icon/link in Tools in /adm/navmaps +# Returns: +# XML that defines the helper states being created. +# +# States: +# CHOOSE_INCOMPLETE_SEQ - Resource selector. +# +sub create_incomplete_folder_helper { + my ($helper, $map, $nocurrloc) = @_; + + my $filter = '$res->is_problem()'; + $filter .= ' && $res->resprintable() '; + $filter .= ' && $res->is_incomplete() '; + + my $symfilter = '$res->shown_symb()'; + + my $resource_chooser = &generate_resource_chooser('CHOOSE_INCOMPLETE_SEQ', + 'Select problem(s) to print', + 'multichoice="1", toponly ="1", addstatus="1", closeallpages="1" modallink="1" nocurrloc="'.$nocurrloc.'"', + 'RESOURCES', + 'PAGESIZE', + $map, + $filter, '', + $symfilter, + ''); + + return $resource_chooser; +} + + +# Returns the text neded for a student chooser. +# that text must still be parsed by the helper xml parser. +# Parameters: +# this_state - State name of the chooser. +# sort_choice - variable to hold the sorting choice. +# variable - Name of variable to hold students. +# next_state - State after chooser. + + +sub generate_student_chooser { + my ($this_state, + $sort_choice, + $variable, + $next_state) = @_; + my $result = < + Select sorting order of printout + + + Sort by section then student + Sort by students across sections. + + +


+ + + +CHOOSE_STUDENTS + + return $result; +} + +# Generate the text needed for a resource chooser given the top level of +# the sequence/page +# +# Parameters: +# this_state - State name of the chooser. +# prompt_text - Text to use to prompt user. +# resource_options - Resource tag options e.g. +# "multichoice='1', toponly='1', addstatus='1', +# modallink='1'" +# that control the selection and appearance of the +# resource selector. +# variable - Name of the variable to hold the choice +# next_state - Name of the next state the helper should transition +# to +# top_url - Top level URL within which to make the selector. +# If empty the top level sequence is shown. +# filter - How to filter the resources. +# value_func - function. +# choice_func - If not empty generates a with this function. +# start_new_option +# - Fragment appended after valuefunc. +# +# +sub generate_resource_chooser { + my ($this_state, + $prompt_text, + $resource_options, + $variable, + $next_state, + $top_url, + $filter, + $choice_func, + $value_func, + $start_new_option) = @_; + + my $result = < + + $next_state + return $filter; +CHOOSE_RESOURCES + if ($choice_func ne '') { + $result .= "return $choice_func;"; + } + if ($top_url ne '') { + $result .= "$top_url"; + } + $result .= <return $value_func; + $start_new_option + + +CHOOSE_RESOURCES + return $result; +} +# +# Generate the helper XML for a code choice helper dialog: +# +# Paramters: +# $helper - Reference to the helper. +# $state - Name of the state for the chooser. +# $next_state - Name fo the state to follow the chooser. +# $bubble_types - Populates the bubble sheet type dropt down. +# $code_selections - Provides set of code choices that have been used +# $saved_codes - Provides the list of saved codes. +# +# Returns; +# The Xml of the code chooser. +# +sub generate_code_selector { + my ($helper, + $state, + $next_state, + $bubble_types, + $code_selections, + $saved_codes) = @_; # Unpack the parameters. + + my $result = < + $next_state +

Fill out one of the forms below

+


+

Generate new CODEd Assignments

+
Number of CODEd assignments to print: + + + if (((\$helper->{'VARS'}{'NUMBER_TO_PRINT_TOTAL'}+0) < 1) && + !\$helper->{'VARS'}{'REUSE_OLD_CODES'} && + !\$helper->{'VARS'}{'SINGLE_CODE'} && + !\$helper->{'VARS'}{'CODE_SELECTED_FROM_LIST'} ) { + + return "You need to specify the number of assignments to print"; + } + if (((\$helper->{'VARS'}{'NUMBER_TO_PRINT_TOTAL'}+0) >= 1) && + (\$helper->{'VARS'}{'SINGLE_CODE'} ne '') ) { + return 'Specifying number of codes to print and a specific code is not compatible'; + } + return undef; + + +
+ Names to save the CODEs under for later: + + +
+ Bubblesheet type: + + + $bubble_types + +
+
+

Print a Specific CODE


+
Enter a CODE to print: + + + if(!\$helper->{'VARS'}{'NUMBER_TO_PRINT_TOTAL'} && + !\$helper->{'VARS'}{'REUSE_OLD_CODES'} && + !\$helper->{'VARS'}{'CODE_SELECTED_FROM_LIST'}) { + return &Apache::lonprintout::is_code_valid(\$helper->{'VARS'}{'SINGLE_CODE'}, + \$helper->{'VARS'}{'CODE_OPTION'}); + } elsif (\$helper->{'VARS'}{'SINGLE_CODE'} ne ''){ + return 'Specifying a code name is incompatible with specifying number of codes.'; + } else { + return undef; # Other forces control us. + } + + +
+ $code_selections +
+

Reprint a Set of Saved CODEs

+ Select saved CODEs: + + + $saved_codes + +
+ +CHOOSE_ANON1 + + return $result; +} + +sub generate_common_choosers { + my ($r,$helper,$map,$url,$isProblem,$symbFilter,$start_new_option) = @_; + + my $randomly_ordered_warning = + &get_randomly_ordered_warning($helper, $map); + + # code for a few states used for printout launched from both + # /adm/navmaps and from a resource by a privileged user: + # - To allow resources to be selected for printing. + # - To determine pagination between assignments. + # - To determine how many assignments should be bundled into a single PDF. + + my $resource_selector= &generate_resource_chooser('SELECT_PROBLEMS', + 'Select resources to print', + 'multichoice="1" addstatus="1" closeallpages="1" modallink="1" suppressNavmap="1"', + 'RESOURCES', + 'PRINT_FORMATTING', + $map, + $isProblem, '', $symbFilter, + $start_new_option); + $resource_selector .= &generate_format_selector($helper, + 'How should results be printed?', + 'PRINT_FORMATTING'). + &generate_resource_chooser('CHOOSE_STUDENTS_PAGE', + 'Select Problem(s) to print', + "multichoice='1' addstatus='1' closeallpages ='1' modallink='1'", + 'RESOURCES', + 'PRINT_FORMATTING', + $url, + $isProblem, '', $symbFilter, + $start_new_option); + +# Generate student choosers. + + &Apache::lonxml::xmlparse($r, 'helper', + &generate_student_chooser('CHOOSE_TGT_STUDENTS_PAGE', + 'student_sort', + 'STUDENTS', + 'CHOOSE_STUDENTS_PAGE')); + &Apache::lonxml::xmlparse($r, 'helper', + &generate_student_chooser('CHOOSE_STUDENTS', + 'student_sort', + 'STUDENTS', + 'SELECT_PROBLEMS')); + &Apache::lonxml::xmlparse($r, 'helper', $resource_selector); + + my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'}; + my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'}; + my @names=&Apache::lonnet::getkeys('CODEs',$cdom,$cnum); + my $namechoice=''; + foreach my $name (sort {uc($a) cmp uc($b)} @names) { + if ($name =~ /^error: 2 /) { next; } + if ($name =~ /^type\0/) { next; } + $namechoice.=''.$name.''; + } + + my %code_values; + my %codes_to_print; + foreach my $key (@names) { + %code_values = &Apache::grades::get_codes($key, $cdom, $cnum); + foreach my $key (keys(%code_values)) { + $codes_to_print{$key} = 1; + } + } + + my $code_selection; + foreach my $code (sort {uc($a) cmp uc($b)} (keys(%codes_to_print))) { + my $choice = $code; + if ($code =~ /^[A-Z]+$/) { # Alpha code + $choice = &letters_to_num($code); + } + push(@{$helper->{DATA}{ALL_CODE_CHOICES}},[$code,$choice]); + } + if (%codes_to_print) { + $code_selection .=' + Choose single CODE from list: + + + + + push(@{$state->{CHOICES}},@{$helper->{DATA}{ALL_CODE_CHOICES}}); + + + + '.$/; + } + + my @lines = &Apache::lonnet::get_scantronformat_file(); + my $codechoice=''; + foreach my $line (@lines) { + next if (($line =~ /^\#/) || ($line eq '')); + my ($name,$description,$code_type,$code_length)= + (split(/:/,$line))[0,1,2,4]; + if ($code_length > 0 && + $code_type =~/^(letter|number|-1)/) { + $codechoice.=''.$description.''; + } + } + if ($codechoice eq '') { + $codechoice='Default'; + } + my $anon1 = &generate_code_selector($helper, + 'CHOOSE_ANON1', + 'SELECT_PROBLEMS', + $codechoice, + $code_selection, + $namechoice) . $resource_selector; + + &Apache::lonxml::xmlparse($r, 'helper',$anon1); + + my $anon_page = &generate_code_selector($helper, + 'CHOOSE_ANON1_PAGE', + 'SELECT_PROBLEMS_PAGE', + $codechoice, + $code_selection, + $namechoice) . + &generate_resource_chooser('SELECT_PROBLEMS_PAGE', + 'Select Problem(s) to print', + "multichoice='1' addstatus='1' closeallpages ='1' modallink='1'", + 'RESOURCES', + 'PRINT_FORMATTING', + $url, + $isProblem, '', $symbFilter, + $start_new_option); + &Apache::lonxml::xmlparse($r, 'helper', $anon_page); + return ($randomly_ordered_warning,$codechoice,$code_selection,$namechoice); +} + +# Returns the XML for choosing how assignments are to be formatted +# that text must still be parsed by the helper xml parser. +# Parameters: 3 (required) + +# helper - The helper; $helper->{'VARS'}->{'PRINT_TYPE'} used +# to check if splitting PDFs by section can be offered. +# title - Title for the current state. +# this_state - State name of the chooser. + +sub generate_format_selector { + my ($helper,$title,$this_state) = @_; + my $secpdfoption; + unless (($helper->{'VARS'}->{'PRINT_TYPE'} eq 'problems_for_anon') || + ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'problems_for_anon_page') || + ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'resources_for_anon') || + ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'select_sequences_problems_for_anon') || + ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'select_sequences_resources_for_anon')) { + $secpdfoption = 'Each PDF contains exactly one section'; + } + return < +
How should the results be printed?
+ + Start each student\'s assignment on a new page/column (add a pagefeed after each assignment) + Add one empty page/column after each student\'s assignment + Add two empty pages/column after each student\'s assignment + Add three empty pages/column after each student\'s assignment + + PAGESIZE +
How do you want assignments split into PDF files?
+ + All assignments in a single PDF file + $secpdfoption + Each PDF contains exactly one assignment + + Specify the number of assignments per PDF: + + +RESOURCE_SELECTOR +} + +#----------------------------------------------------------------------- + +# Computes an open and close date from a list of open/close dates for a resource's +# parts. +# +# @param \@opens - reference to an array of open dates. +# @param \@closes - reference to an array of close dates. +# +# @return ($open, $close) +# +# @note If open/close dates are not defined they will be returned as undef +# @note It is possible for there to be no overlap in which case -1,-1 +# will be returned. +# @note The algorithm used is to take the latest open date and the earliest end date. +# +sub compute_open_window { + my ($opensref, $closesref) = @_; + + my @opens = @$opensref; + my @closes = @$closesref; + + # latest open date: + my $latest_open; + + foreach my $open (@opens) { + if (!defined($latest_open) || ($open > $latest_open)) { + $latest_open = $open; + } + } + # Earliest close: + + my $earliest_close; + foreach my $close (@closes) { + if (!defined($earliest_close) || ($close < $earliest_close)) { + $earliest_close = $close; + } + } + + # If no overlap...both are -1 as promised. + + if (($earliest_close ne '') && ($latest_open ne '') + && ($earliest_close < $latest_open)) { + $latest_open = -1; + $earliest_close = -1; + } + + return ($latest_open, $earliest_close); + +} + +## +# Determines if 'now' is within the set of printable dates. +# +# @param $open_date - Starting date/timestamp. +# @param $close_date - Ending date/timestamp. +# +# @return 0 - Not open. +# @return 1 - open. +# +sub printable { + my ($open_date, $close_date) = @_; + + + my $now = time(); + + # Have to do a bit of fancy footwork around undefined open/close dates: + + if ($open_date && ($open_date > $now)) { + return 0; + } + + if ($close_date && ($close_date < $now)) { + return 0; + } + + return 1; + +} + +## +# Returns the innermost print start/print end dates for a resource. +# This is done by looking at the start/end dates for its parts and choosing +# the intersection of those dates. +# +# @param res - lonnvamaps::resource object that represents the resource. +# +# @return (opendate, closedate) +# +# @note If open/close dates are not defined they will be returned as undef +# @note It is possible for there to be no overlap in which case -1,-1 +# will be returned. +# @note The algorithm used is to take the latest open date and the earliest end date. +# For consistency with &printable() in lonnavmaps.pm determination of start +# date for printing checks printstartdate param first, then, if not set, +# opendate param, then, if not set, contentopen param. + + +sub get_print_dates { + my $res = shift; + my $partsref = $res->parts(); + my @parts; + if (ref($partsref) eq 'ARRAY') { + @parts = @{$partsref}; + } + my $open_date; + my $close_date; + my @open_dates; + my @close_dates; + + + if (@parts) { + foreach my $part (@parts) { + my $partopen = $res->parmval('printstartdate', $part); + my $partclose = $res->parmval('printenddate', $part); + if (!$partopen) { + $partopen = $res->parmval('opendate',$part); + } + if (!$partopen) { + $partopen = $res->parmval('contentopen',$part); + } + if ($partopen) { + push(@open_dates, $partopen); + } + if ($partclose) { + push(@close_dates, $partclose); + } + push(@open_dates, $partopen); + push(@close_dates, $partclose); + } + } + + ($open_date, $close_date) = &compute_open_window(\@open_dates, \@close_dates); + + return ($open_date, $close_date); +} + +## +# Get the dates for which a course says a resource can be printed. This is like +# get_print_dates but namvaps::course_print_dates are gotten...and not converted +# to times either. +# +# @param $res - Reference to a resource hash from lonnavmaps::resource. +# +# @return (opendate, closedate) +# +sub course_print_dates { + my $res = shift; + my $partsref = $res->parts(); + my @parts = @$partsref; + my $open_date; + my $close_date; + my @open_dates; + my @close_dates; + my $navmap = $res->{NAV_MAP}; # Slightly OO dirty. + + # Don't bother looping over undefined or empty parts array; + + if (@parts) { + foreach my $part (@parts) { + my ($partopen, $partclose) = $navmap->course_printdates($res, $part); + push(@open_dates, $partopen); + push(@close_dates, $partclose); + } + ($open_date, $close_date) = &compute_open_window(\@open_dates, \@close_dates); + } + return ($open_date, $close_date); +} +## +# Same as above but for the enclosing map: +# +sub map_print_dates { + my $res = shift; + my $partsref = $res->parts(); + my @parts = @$partsref; + my $open_date; + my $close_date; + my @open_dates; + my @close_dates; + my $navmap = $res->{NAV_MAP}; # slightly OO dirty. + + + # Don't bother looping over undefined or empty parts array; + + if (@parts) { + foreach my $part (@parts) { + my ($partopen, $partclose) = $navmap->map_printdates($res, $part); + push(@open_dates, $partopen); + push(@close_dates, $partclose); + } + ($open_date, $close_date) = &compute_open_window(\@open_dates, \@close_dates); + } + return ($open_date, $close_date); +} + +# Determine if a resource is incomplete given the map: +# Parameters: +# $username - Name of user for whom we are checking. +# $domain - Domain of user we are checking. +# $map - map name. +# Returns: +# 0 - map is not incomplete. +# 1 - map is incomplete. +# +sub incomplete { + my ($username, $domain, $map) = @_; + + + my $navmap = Apache::lonnavmaps::navmap->new($username, $domain); + + + if (defined($navmap)) { + my $res = $navmap->getResourceByUrl($map); + my $result = $res->is_incomplete(); + return $result; + } else { + return 1; + } +} +# +# When printing for students, the resources and order of the +# resources may need to be altered if there are folders with +# random selectiopn or random ordering (or both) enabled. +# This sub computes the set of resources to print for a student +# modified both by random ordering and selection and filtered +# to only those that are in the original set selected to be printed. +# +# Parameters: +# $map - The URL of the folder being printed. +# Used to determine which startResource and finishResource +# to use when using the navmap's getIterator method. +# $seq - The original set of resources to print. +# (really an array of resource names (array of symb's). +# $who - Student/domain for whome the sequence will be generated. +# $code - CODE being printed when printing Problems/Resources +# from folder for CODEd assignments +# $nohidemap - If true, parameter in map for hiddenresource will be +# ignored. The user calling the routine should have +# both the pav and vgr privileges if this is set to true). +# +# Implicit inputs: +# $ +# Returns: +# reference to an array of resources that can be passed to +# print_resources. +# +sub master_seq_to_person_seq { + my ($map, $seq, $who, $code, $nohidemap) = @_; + + + my ($username, $userdomain, $usersection) = split(/:/, $who); + + # Toss the sequence up into a hash so that we have O(1) lookup time. + # on the items that come out of the user's list of resources. + # + + my %seq_hash = map {$_ => 1} @$seq; + my @output_seq; + + my $unhidden; + if ($nohidemap) { + $unhidden = &Apache::lonnet::clutter($map); + } + + my $navmap = Apache::lonnavmaps::navmap->new($username, $userdomain, + $code,$unhidden); + my ($start,$finish); + + if ($map) { + my $mapres = $navmap->getResourceByUrl($map); + if ($mapres->is_map()) { + $start = $mapres->map_start(); + $finish = $mapres->map_finish(); + } + } + unless ($start && $finish) { + $start = $navmap->firstResource(); + $finish = $navmap->finishResource(); + } + + my $iterator = $navmap->getIterator($start,$finish,{},1); + + # Iterate on the resource..select the items that are randomly selected + # and that are in the seq_hash. Presumably the iterator will take care + # of the random ordering part of the deal. + my $curres; + while ($curres = $iterator->next()) { + # + # Only process resources..that are not removed by randomout... + # and are selected for printint as well. + # + + if (ref($curres) && ! $curres->randomout()) { + my $currsymb = $curres->symb(); + if (exists($seq_hash{$currsymb})) { + push(@output_seq, $currsymb); + } + } + } + + return \@output_seq; # for now. + +} + + +# Fetch the contents of a resource, uninterpreted. +# This is used here to fetch a latex file to be included +# verbatim into the printout< +# NOTE: Ask Guy if there is a lonnet function similar to this? +# +# Parameters: +# URL of the file +# +sub fetch_raw_resource { + my ($url) = @_; + + my $filename = &Apache::lonnet::filelocation("", $url); + my $contents = &Apache::lonnet::getfile($filename); + + if ($contents == -1) { + return "File open failed for $filename"; # This will bomb the print. + } + return $contents; + + +} + +# Fetch the annotations associated with a URL and +# put a centered 'annotations:' title. +# This is all suppressed if the annotations are empty. +# +sub annotate { + my ($symb) = @_; + + my $annotation_text = &Apache::loncommon::get_annotation($symb, 1); + + + my $result = ""; + + if (length($annotation_text) > 0) { + $result .= '\\hspace*{\\fill} \\\\[\\baselineskip] \textbf{Annotations:} \\\\ '; + $result .= "\n"; + $result .= &Apache::lonxml::latex_special_symbols($annotation_text,""); # Escape latex. + $result .= "\n\n"; + } + return $result; +} + +# +# Set a global document font size: +# This is done by replacing \begin{document} +# with \begin{document}{\some-font-directive +# and \end{document} with +# }\end{document +# +sub set_font_size { + + my ($text) = @_; + + # There appear to be cases where the font directive is empty.. in which + # case the first substitution would insert a spurious \ oh happy day. + # as this has been the cause of much mystery and hair pulling _sigh_ + + if ($font_size ne '') { + + $text =~ s/\\begin\{document}/\\begin{document}{\\$font_size/; + $text =~ s/\\end\{document}/}\\end{document}/; + } + return $text; + + +} + +# include_pdf - PDF files are included into the +# output as follows: +# - The PDF, if necessary, is replicated. +# - The PDF is added to the list of files to convert to postscript (along with the images). +# - The LaTeX is added to include the final converted postscript in the file as an included +# job. The assumption is that the includepsheader.ps header will be included. +# +# Parameters: +# pdf_uri - URI of the PDF file to include. +# +# Returns: +# The LaTeX to include. +# +# Assumptions: +# The uri is actually a PDF file +# The postscript will have the includepsheader.ps included. +# +# +sub include_pdf { + my ($pdf_uri) = @_; + + # Where is the file? If not local we'll need to repcopy it:' + + my $file = &Apache::lonnet::filelocation('', $pdf_uri); + if (! -e $file) { + &Apache::lonnet::repcopy($file); + $file = &Apache::lonnet::filelocation('',$pdf_uri); + } + + # The file is now replicated locally ... or it did not exist in the first place + # (unlikely). If it did exist, add the pdf to the set of files/images that + # need to be converted for this print job: + + my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'}; + $file =~ s{(.*)/res/}{$londocroot/res/}; + + open(FILE,">>","$Apache::lonnet::perlvar{'lonPrtDir'}/$env{'user.name'}_$env{'user.domain'}_printout.dat"); + print FILE ("$file\n"); + close (FILE); + + # Construct the special to put out. To do this we need to get the + # resulting filename after conversion. The file will have the same name + # but will be in the user's spool directory with converted images. + + my $dirname = "/home/httpd/prtspool/$env{'user.name'}/"; + my ( $base, $path, $ext) = &fileparse($file, '.pdf'); +# my $destname = $dirname.'/'.$base.'.eps'; # Not really an eps but easier in printout.pl + $base =~ s/ /\_/g; + + + my $output = &print_latex_header(); + $output .= '\special{ps: _begin_job_ (' + .$base.'.pdf.eps'. + ')run _end_job_}'; + + return $output; + + +} +## +# Collect the various \select_language{language_name} +# latex tags to build a \usepackage[lang-list]{babel} which will +# appear just prior to the \begin{document} at the front of the concatenated +# set of resources: +# @param doc - The string of latex to search/replace. +# @return string +# @retval - the modified document stringt. +# +sub collect_languages { + my $doc = shift; + my %languages; + while ($doc =~ /\\selectlanguage\{(\w+)}/mg) { + $languages{$1} = 1; # allows us to request each language exactly once. + } + my @lang_list = (keys(%languages)); # List of unique languages + if (scalar @lang_list) { + my $babel_header = '\usepackage[' . join(',', @lang_list) .']{babel}'. "\n"; + $doc =~ s/\\begin\{document}/$babel_header\\begin{document}/; + } + return $doc; +} +#------------------------------------------------------------------- + +# +# ssi_with_retries- Does the server side include of a resource. +# if the ssi call returns an error we'll retry it up to +# the number of times requested by the caller. +# If we still have a proble, no text is appended to the +# output and we set some global variables. +# to indicate to the caller an SSI error occurred. +# All of this is supposed to deal with the issues described +# in LonCAPA BZ 5631 see: +# http://bugs.lon-capa.org/show_bug.cgi?id=5631 +# by informing the user that this happened. +# +# Parameters: +# resource - The resource to include. This is passed directly, without +# interpretation to lonnet::ssi. +# form - The form hash parameters that guide the interpretation of the resource +# +# retries - Number of retries allowed before giving up completely. +# Returns: +# On success, returns the rendered resource identified by the resource parameter. +# Side Effects: +# The following global variables can be set: +# ssi_error - If an unrecoverable error occurred this becomes true. +# It is up to the caller to initialize this to false +# if desired. +# ssi_last_error_resource - If an unrecoverable error occurred, this is the value +# of the resource that could not be rendered by the ssi +# call. +# ssi_last_error - The error string fetched from the ssi response +# in the event of an error. +# +sub ssi_with_retries { + my ($resource, $retries, %form) = @_; + + my $target = $form{'grade_target'}; + my $aom = $form{'answer_output_mode'}; + + + + my ($content, $response) = &Apache::loncommon::ssi_with_retries($resource, $retries, %form); + if (!$response->is_success) { + $ssi_error = 1; + $ssi_last_error_resource = $resource; + $ssi_last_error = $response->code . " " . $response->message; + $content='\section*{!!! An error occurred !!!}'; + } + + return $content; + +} + +sub get_student_view_with_retries { + my ($curresline,$retries,$username,$userdomain,$courseid,$target,$moreenv)=@_; + + my ($content, $response) = &Apache::loncommon::get_student_view_with_retries($curresline,$retries,$username,$userdomain,$courseid,$target,$moreenv); + if (!$response->is_success) { + $ssi_error = 1; + $ssi_last_error_resource = $curresline.' for user '.$username.':'.$userdomain; + $ssi_last_error = $response->code . " " . $response->message; + $content='\section*{!!! An error occurred !!!}'; + } + return $content; + +} + +# +# printf_style_subst item format_string repl +# +# Does printf style substitution for a format string that +# can have %[n]item in it.. wherever, %[n]item occurs, +# rep is substituted in format_string. Note that +# [n] is an optional integer length. If provided, +# repl is truncated to at most [n] characters prior to +# substitution. +# +sub printf_style_subst { + my ($item, $format_string, $repl) = @_; + my $result = ""; + while ($format_string =~ /(%)(\d*)\Q$item\E/g ) { + my $fmt = $1; + my $size = $2; + my $subst = $repl; + if ($size ne "") { + $subst = substr($subst, 0, $size); + + # Here's a nice edge case ... suppose the end of the + # substring is a \. In that case may have just + # chopped off a TeX escape... in that case, we append + # " " for the trailing character, and let the field + # spill over a bit (sigh). + # We don't just chop off the last character in order to deal + # with one last pathology, and that would be if substr had + # trimmed us to e.g. \\\ + + + if ($subst =~ /\\$/) { + $subst .= " "; + } + } + my $item_pos = pos($format_string); + $result .= substr($format_string, 0, $item_pos - length($size) -2) . $subst; + $format_string = substr($format_string, pos($format_string)); + } + + # Put the residual format string into the result: + + $result .= $format_string; + + return $result; +} + # Format a header according to a format. # @@ -58,21 +1189,83 @@ my $resources_printed; # %a - Assignment name. # %c - Course name. # %n - Student name. +# %s - The section if it is supplied. # sub format_page_header { - my ($format, $assignment, $course, $student) = @_; - + my ($width, $format, $assignment, $course, $student) = @_; + + + + $width = &recalcto_mm($width); # Get width in mm. + my $chars_per_line = int($width/2); # Character/textline. + # Default format? if ($format eq '') { - $format = "\\textbf{$student} $course \\hfill \\thepage \\\\ \\textit{$assignment}"; - + # For the default format, we may need to truncate + # elements.. To do this we need to get the page width. + # we assume that each character is about 2mm in width. + # (correct for the header text size??). We ignore + # any formatting (e.g. boldfacing in this). + # + # - Allow the student/course to be one line. + # but only truncate the course. + # - Allow the assignment to be 2 lines (wrapped). + # + + my $firstline = "$student $course"; + if (length($firstline) > $chars_per_line) { + my $lastchar = $chars_per_line - length($student) - 1; + if ($lastchar > 0) { + $course = substr($course, 0, $lastchar); + } else { # Nothing left of course: + $course = ''; + } + } + if (length($assignment) > $chars_per_line) { + $assignment = substr($assignment, 0, $chars_per_line); + } + + $format = "\\textbf{$student} $course \\hfill \\thepage \\\\ \\textit{$assignment}"; + } else { - $format =~ s/%a/$assignment/g; - $format =~ s/%c/$course/g; - $format =~ s/%n/$student/g; + # An open question is how to handle long user formatted page headers... + # A possible future is to support e.g. %na so that the user can control + # the truncation of the elements that can appear in the header. + # + $format = &printf_style_subst("a", $format, $assignment); + $format = &printf_style_subst("c", $format, $course); + $format = &printf_style_subst("n", $format, $student); + + # If the user put %'s in the format string, they must be escaped + # to \% else LaTeX will think they are comments and terminate + # the line.. which is bad!!! + } + + return $format; + + # If the user has role author, $course and $assignment are empty so + # there is '\\ \\ ' in the page header. That's cause a error in LaTeX + if($format =~ /\\\\\s\\\\\s/) { + #TODO find sensible caption for page header + my $testPrintout = '\\\\'.&mt('Authoring Space').' \\\\'.&mt('Test-Printout '); + $format =~ s/\\\\\s\\\\\s/$testPrintout/; + } + # + # We're going to trust LaTeX to break lines appropriately, but + # we'll truncate anything that's more than 3 lines worth of + # text. This is also assuming (which will probably end badly) + # nobody's going to embed LaTeX control sequences in the title + # header or rather that those control sequences won't get broken + # by the stuff below. + # + my $total_length = 3*$chars_per_line; + if (length($format) > $total_length) { + $format = substr($format, 0, $total_length); + } + return $format; @@ -94,7 +1287,7 @@ sub num_to_letters { sub letters_to_num { my ($letters) = @_; my @letters = split('', uc($letters)); - my %substitution; + my %substitution; my $digit = 0; foreach my $letter ('A'..'J') { $substitution{$letter} = $digit; @@ -177,8 +1370,9 @@ sub is_valid_alpha_code { sub is_code_valid { my ($code_value, $code_option) = @_; my ($code_type, $code_length) = ('letter', 6); # defaults. - open(FG, $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab'); - foreach my $line () { + my @lines = &Apache::lonnet::get_scantronformat_file(); + foreach my $line (@lines) { + next if (($line =~ /^\#/) || ($line eq '')); my ($name, $type, $length) = (split(/:/, $line))[0,2,4]; if($name eq $code_option) { $code_length = $length; @@ -195,6 +1389,22 @@ sub is_code_valid { } } +# +# Compare two students by section (Used to sort by section). +# +# Implicit inputs, +# $a - The first one +# $b - The second one. +# +# Returns: +# a-section cmp b-section +# +sub compare_sections { + my ($u1, $d1, $s1, $n1, $stat1) = split(/:/, $a); + my ($u2, $d2, $s2, $n2, $stat2) = split(/:/, $b); + + return $s1 cmp $s2; +} # Compare two students by name. The students are in the form # returned by the helper: @@ -234,7 +1444,7 @@ sub compare_names { } # Break the tie on the first name, but there are leading (possibly trailing - # whitespaces to get rid of first + # whitespaces to get rid of first) # $f1 =~ s/^\s+//; # Remove leading... $f1 =~ s/\s+$//; # Trailing spaces from first 1... @@ -256,8 +1466,8 @@ sub compare_names { sub latex_header_footer_remove { my $text = shift; - $text =~ s/\\end{document}//; - $text =~ s/\\documentclass([^&]*)\\begin{document}//; + $text =~ s/\\end\{document}//; + $text =~ s/\\documentclass([^&]*)\\begin\{document}//; return $text; } # @@ -266,8 +1476,8 @@ sub latex_header_footer_remove { # necessity is determined by the problem_split param. # sub encapsulate_minipage { - my ($text) = @_; - if (!($env{'form.problem.split'} =~ /yes/i)) { + my ($text,$problem_split) = @_; + if (!($problem_split =~ /yes/i)) { $text = '\begin{minipage}{\textwidth}'.$text.'\end{minipage}'; } return $text; @@ -296,12 +1506,19 @@ sub adjust_number_to_print { # Unmodified. } else { # Error!!!! - + croak "bad SPLIT_PDFS: $split_pdf in lonprintout::adjust_number_to_print"; + } } + sub character_chart { + my $result = shift; + return &Apache::entities::replace_entities($result); +} + +sub old_character_chart { my $result = shift; $result =~ s/&\#0?0?(7|9);//g; $result =~ s/&\#0?(10|13);//g; @@ -409,7 +1626,7 @@ sub character_chart { $result =~ s/&\#147;/\`\`/g; $result =~ s/&\#148;/\'\'/g; $result =~ s/&\#149;/\\ensuremath\{\\bullet\}/g; - $result =~ s/&\#150;/--/g; + $result =~ s/&(\#150|\#8211);/--/g; $result =~ s/&\#151;/---/g; $result =~ s/&\#152;/\\ensuremath\{\\sim\}/g; $result =~ s/&\#153;/\\texttrademark /g; @@ -423,7 +1640,7 @@ sub character_chart { $result =~ s/&(\#165|yen);/\\textyen /g; $result =~ s/&(\#166|brvbar);/\\textbrokenbar /g; $result =~ s/&(\#167|sect);/\\textsection /g; - $result =~ s/&(\#168|uml);/\\texthighdieresis /g; + $result =~ s/&(\#168|uml);/\\"\{\} /g; $result =~ s/&(\#169|copy);/\\copyright /g; $result =~ s/&(\#170|ordf);/\\textordfeminine /g; $result =~ s/&(\#172|not);/\\ensuremath\{\\neg\}/g; @@ -434,7 +1651,7 @@ sub character_chart { $result =~ s/&(\#177|plusmn);/\\ensuremath\{\\pm\}/g; $result =~ s/&(\#178|sup2);/\\ensuremath\{^2\}/g; $result =~ s/&(\#179|sup3);/\\ensuremath\{^3\}/g; - $result =~ s/&(\#180|acute);/\\textacute /g; + $result =~ s/&(\#180|acute);/\\'\{\} /g; $result =~ s/&(\#181|micro);/\\ensuremath\{\\mu\}/g; $result =~ s/&(\#182|para);/\\P/g; $result =~ s/&(\#183|middot);/\\ensuremath\{\\cdot\}/g; @@ -616,6 +1833,29 @@ sub character_chart { $result =~ s/&(clubs|\#9827);/\\ensuremath\{\\clubsuit\}/g; $result =~ s/&(hearts|\#9829);/\\ensuremath\{\\heartsuit\}/g; $result =~ s/&(diams|\#9830);/\\ensuremath\{\\diamondsuit\}/g; +# Chemically useful 'things' contributed by Hon Kie (bug 4652). + + $result =~ s/&\#8636;/\\ensuremath\{\\leftharpoonup\}/g; + $result =~ s/&\#8637;/\\ensuremath\{\\leftharpoondown\}/g; + $result =~ s/&\#8640;/\\ensuremath\{\\rightharpoonup\}/g; + $result =~ s/&\#8641;/\\ensuremath\{\\rightharpoondown\}/g; + $result =~ s/&\#8652;/\\ensuremath\{\\rightleftharpoons\}/g; + $result =~ s/&\#8605;/\\ensuremath\{\\leadsto\}/g; + $result =~ s/&\#8617;/\\ensuremath\{\\hookleftarrow\}/g; + $result =~ s/&\#8618;/\\ensuremath\{\\hookrightarrow\}/g; + $result =~ s/&\#8614;/\\ensuremath\{\\mapsto\}/g; + $result =~ s/&\#8599;/\\ensuremath\{\\nearrow\}/g; + $result =~ s/&\#8600;/\\ensuremath\{\\searrow\}/g; + $result =~ s/&\#8601;/\\ensuremath\{\\swarrow\}/g; + $result =~ s/&\#8598;/\\ensuremath\{\\nwarrow\}/g; + + # Left/right quotations: + + $result =~ s/&(ldquo|#8220);/\`\`/g; + $result =~ s/&(rdquo|#8221);/\'\'/g; + + + return $result; } @@ -624,18 +1864,18 @@ sub character_chart { my %page_formats= ('letter' => { 'book' => { - '1' => [ '7.1 in','9.8 in', '-0.57 in','-0.57 in','0.7 cm'], - '2' => ['3.66 in','9.8 in', '-0.57 in','-0.57 in','0.7 cm'] + '1' => [ '7.1 in','9.8 in', '-0.57 in','-0.57 in','0.275 in'], + '2' => ['3.66 in','9.8 in', '-0.57 in','-0.57 in','0.275 in'] }, 'album' => { - '1' => [ '8.8 in', '6.8 in','-0.55 in', '-0.83 in','1 cm'], - '2' => [ '4.4 in', '6.8 in','-0.5 in', '-1.5 in','3.5 in'] + '1' => [ '8.8 in', '6.8 in','-0.55 in', '-0.55 in','0.394 in'], + '2' => [ '4.8 in', '6.8 in','-0.5 in', '-1.0 in','3.5 in'] }, }, 'legal' => { 'book' => { '1' => ['7.1 in','13 in',,'-0.57 in','-0.57 in','-0.5 in'], - '2' => ['3.16 in','13 in','-0.57 in','-0.57 in','-0.5 in'] + '2' => ['3.66 in','13 in','-0.57 in','-0.57 in','-0.5 in'] }, 'album' => { '1' => ['12 in','7.1 in',,'-0.57 in','-0.57 in','-0.5 in'], @@ -684,12 +1924,12 @@ my %page_formats= }, 'a4' => { 'book' => { - '1' => ['17.6 cm','27.2 cm','-0.55 in','-0.83 in','-0.5 in'], - '2' => [ '9.1 cm','27.2 cm','-0.55 in','-0.83 in','-0.5 in'] + '1' => ['17.6 cm','27.2 cm','-1.397 cm','-2.11 cm','-1.27 cm'], + '2' => [ '9.1 cm','27.2 cm','-1.397 cm','-2.11 cm','-1.27 cm'] }, 'album' => { - '1' => ['8.5 in','7.7 in','-0.55 in','-0.83 in','0 in'], - '2' => ['3.9 in','7.7 in','-0.55 in','-0.83 in','0 in'] + '1' => ['21.59 cm','19.558 cm','-1.397cm','-2.11 cm','0 cm'], + '2' => ['9.91 cm','19.558 cm','-1.397 cm','-2.11 cm','0 cm'] }, }, 'a5' => { @@ -750,7 +1990,11 @@ sub page_format_transformation { my ($textwidth,$textheight,$oddoffset,$evenoffset,$topmargin); if ($selectionmade eq '4') { - $assignment='Problems from the Whole Course'; + if ($choice eq 'all_problems') { + $assignment=&mt('Problems from the Whole Course'); + } else { + $assignment=&mt('Resources from the Whole Course'); + } } else { $assignment=&Apache::lonxml::latex_special_symbols($assignment,'header'); } @@ -761,7 +2005,7 @@ sub page_format_transformation { my $courseidinfo = &get_course(); if (defined($courseidinfo)) { $courseidinfo=' - '.$courseidinfo } my $header_text = $parmhash{'print_header_format'}; - $header_text = &format_page_header($header_text, $assignment, + $header_text = &format_page_header($textwidth, $header_text, $assignment, $courseidinfo, $name); my $topmargintoinsert = ''; if ($topmargin ne '0') {$topmargintoinsert='\setlength{\topmargin}{'.$topmargin.'}';} @@ -772,21 +2016,27 @@ sub page_format_transformation { $fancypagestatement="\\rhead{}\\chead{}\\lhead{$header_text}"; } if ($layout eq 'album') { - $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\\begin{document}\\voffset=-0\.8 cm\\setcounter{page}{1}\n /; + $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 /; } elsif ($layout eq 'book') { if ($choice ne 'All class print') { - $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\\begin{document}\n\\voffset=-0\.8 cm\\setcounter{page}{1}\n/; + $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/; } else { - $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{}\\begin{document}\\voffset=-0\.8cm\n\\setcounter{page}{1} \\vskip 5 mm\n /; + $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 /; } if ($papersize eq 'a4') { - $text =~ s/(\\begin{document})/$1\\special{papersize=210mm,297mm}/; + my $papersize_text; + if ($perm{'pav'}) { + $papersize_text = '\\special{papersize=210mm,297mm}'; + } else { + $papersize_text = '\special{papersize=210mm,297mm}'; + } + $text =~ s/(\\begin\{document})/$1$papersize_text/; } } if ($tableofcontents eq 'yes') {$text=~s/(\\setcounter\{page\}\{1\})/$1 \\tableofcontents\\newpage /;} if ($indexlist eq 'yes') { - $text=~s/(\\begin{document})/\\makeindex $1/; - $text=~s/(\\end{document})/\\strut\\\\\\strut\\printindex $1/; + $text=~s/(\\begin\{document})/\\makeindex $1/; + $text=~s/(\\end\{document})/\\strut\\\\\\strut\\printindex $1/; } return $text; } @@ -795,12 +2045,12 @@ sub page_format_transformation { sub page_cleanup { my $result = shift; - $result =~ m/\\end{document}(\d*)$/; + $result =~ m/\\end\{document}(\d*)$/; my $number_of_columns = $1; my $insert = '{'; for (my $id=1;$id<=$number_of_columns;$id++) { $insert .='l'; } $insert .= '}'; - $result =~ s/(\\begin{longtable})INSERTTHEHEADOFLONGTABLE\\endfirsthead\\endhead/$1$insert/g; + $result =~ s/(\\begin\{longtable})INSERTTHEHEADOFLONGTABLE\\endfirsthead\\endhead/$1$insert/g; $result =~ s/&\s*REMOVETHEHEADOFLONGTABLE\\\\/\\\\/g; return $result,$number_of_columns; } @@ -836,16 +2086,16 @@ my $end_of_student = "\n".'\special{ps:E sub latex_corrections { my ($number_of_columns,$result,$selectionmade,$answer_mode) = @_; -# $result =~ s/\\includegraphics{/\\includegraphics\[width=\\minipagewidth\]{/g; +# $result =~ s/\\includegraphics\{/\\includegraphics\[width=\\minipagewidth\]{/g; my $copyright = ©right_line(); if ($selectionmade eq '1' || $answer_mode eq 'only') { - $result =~ s/(\\end{document})/\\strut\\vskip 0 mm $copyright $end_of_student $1/; + $result =~ s/(\\end\{document})/\\strut\\vskip 0 mm $copyright $end_of_student $1/; } else { - $result =~ s/(\\end{document})/\\strut\\vspace\*{-4 mm}\\newline $copyright $end_of_student $1/; + $result =~ s/(\\end\{document})/\\strut\\vspace\*{-4 mm}\\newline $copyright $end_of_student $1/; } $result =~ s/\$number_of_columns/$number_of_columns/g; - $result =~ s/(\\end{longtable}\s*)(\\strut\\newline\\noindent\\makebox\[\\textwidth\/$number_of_columns\]\[b\]{\\hrulefill})/$2$1/g; - $result =~ s/(\\end{longtable}\s*)\\strut\\newline/$1/g; + $result =~ s/(\\end\{longtable}\s*)(\\strut\\newline\\noindent\\makebox\[\\textwidth\/$number_of_columns\]\[b\]\{\\hrulefill})/$2$1/g; + $result =~ s/(\\end\{longtable}\s*)\\strut\\newline/$1/g; #-- LaTeX corrections my $first_comment = index($result,' - -ELEMENTHTML + my %lt = &Apache::lonlocal::texthash( + 'format' => 'How should each column be formatted?', + 'width' => 'Width', + 'height' => 'Height', + 'margin' => 'Left Margin' + ); + + $result .= '

'.$lt{'format'}.'

' + .&Apache::lonhtmlcommon::start_pick_box() + .&Apache::lonhtmlcommon::row_title($lt{'width'}) + .'' + .'' + .&Apache::lonhtmlcommon::row_closure() + .&Apache::lonhtmlcommon::row_title($lt{'height'}) + .'' + .'' + .&Apache::lonhtmlcommon::row_closure() + .&Apache::lonhtmlcommon::row_title($lt{'margin'}) + .'' + .'' + .&Apache::lonhtmlcommon::row_closure(1) + .&Apache::lonhtmlcommon::end_pick_box(); + #

Hint: Some instructors like to leave scratch space for the student by + # making the width much smaller than the width of the page.

return $result; } @@ -3127,11 +5569,8 @@ sub preprocess { ($papersize) = split(/ /, $papersize); - if ($laystyle eq 'L') { - $laystyle = 'album'; - } else { - $laystyle = 'book'; - } + $laystyle = &Apache::lonprintout::map_laystyle($laystyle); + # Figure out some good defaults for the print out and set them: my %size; @@ -3201,7 +5640,5 @@ sub postprocess { } } - - __END__