--- loncom/interface/lonprintout.pm 2005/04/07 07:03:23 1.374 +++ loncom/interface/lonprintout.pm 2006/08/21 22:53:19 1.480 @@ -1,7 +1,8 @@ -# The LearningOnline Network +# +# The LearningOnline Network # Printout # -# $Id: lonprintout.pm,v 1.374 2005/04/07 07:03:23 albertel Exp $ +# $Id: lonprintout.pm,v 1.480 2006/08/21 22:53:19 foxr Exp $ # # Copyright Michigan State University Board of Trustees # @@ -41,6 +42,159 @@ use Apache::lonnavmaps; use Apache::lonratedt; use POSIX qw(strftime); use Apache::lonlocal; +use Carp; +use lib '/home/httpd/lib/perl/'; +use LONCAPA; + +my %perm; +my %parmhash; +my $resources_printed; + + +# Format a header according to a format. +# + +# Substitutions: +# %a - Assignment name. +# %c - Course name. +# %n - Student name. +# +sub format_page_header { + my ($format, $assignment, $course, $student) = @_; + + # Default format? + + if ($format eq '') { + $format = "\\textbf{$student} $course \\hfill \\thepage \\\\ \\textit{$assignment}"; + + } else { + $format =~ s/%a/$assignment/g; + $format =~ s/%c/$course/g; + $format =~ s/%n/$student/g; + } + + + return $format; + +} + +# +# Convert a numeric code to letters +# +sub num_to_letters { + my ($num) = @_; + my @nums= split('',$num); + my @num_to_let=('A'..'Z'); + my $word; + foreach my $digit (@nums) { $word.=$num_to_let[$digit]; } + return $word; +} +# Convert a letter code to numeric. +# +sub letters_to_num { + my ($letters) = @_; + my @letters = split('', uc($letters)); + my %substitution; + my $digit = 0; + foreach my $letter ('A'..'J') { + $substitution{$letter} = $digit; + $digit++; + } + # The substitution is done as below to preserve leading + # zeroes which are needed to keep the code size exact + # + my $result =""; + foreach my $letter (@letters) { + $result.=$substitution{$letter}; + } + return $result; +} + +# Determine if a code is a valid numeric code. Valid +# numeric codes must be comprised entirely of digits and +# have a correct number of digits. +# +# Parameters: +# value - proposed code value. +# num_digits - Number of digits required. +# +sub is_valid_numeric_code { + my ($value, $num_digits) = @_; + # Remove leading/trailing whitespace; + $value =~ s/^\s*//g; + $value =~ s/\s*$//g; + + # All digits? + if ($value !~ /^[0-9]+$/) { + return "Numeric code $value has invalid characters - must only be digits"; + } + if (length($value) != $num_digits) { + return "Numeric code $value incorrect number of digits (correct = $num_digits)"; + } + return undef; +} +# Determines if a code is a valid alhpa code. Alpha codes +# are ciphers that map [A-J,a-j] -> 0..9 0..9. +# They also have a correct digit count. +# Parameters: +# value - Proposed code value. +# num_letters - correct number of letters. +# Note: +# leading and trailing whitespace are ignored. +# +sub is_valid_alpha_code { + my ($value, $num_letters) = @_; + + # strip leading and trailing spaces. + + $value =~ s/^\s*//g; + $value =~ s/\s*$//g; + + # All alphas in the right range? + if ($value !~ /^[A-J,a-j]+$/) { + return "Invalid letter code $value must only contain A-J"; + } + if (length($value) != $num_letters) { + return "Letter code $value has incorrect number of letters (correct = $num_letters)"; + } + return undef; +} + +# Determine if a code entered by the user in a helper is valid. +# valid depends on the code type and the type of code selected. +# The type of code selected can either be numeric or +# Alphabetic. If alphabetic, the code, in fact is a simple +# substitution cipher for the actual numeric code: 0->A, 1->B ... +# We'll be nice and be case insensitive for alpha codes. +# Parameters: +# code_value - the value of the code the user typed in. +# code_option - The code type selected from the set in the scantron format +# table. +# Returns: +# undef - The code is valid. +# other - An error message indicating what's wrong. +# +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 ($name, $type, $length) = (split(/:/, $line))[0,2,4]; + if($name eq $code_option) { + $code_length = $length; + if($type eq 'number') { + $code_type = 'number'; + } + } + } + my $valid; + if ($code_type eq 'number') { + return &is_valid_numeric_code($code_value, $code_length); + } else { + return &is_valid_alpha_code($code_value, $code_length); + } + +} # Compare two students by name. The students are in the form # returned by the helper: @@ -106,7 +260,46 @@ sub latex_header_footer_remove { $text =~ s/\\documentclass([^&]*)\\begin{document}//; return $text; } +# +# If necessary, encapsulate text inside +# a minipage env. +# necessity is determined by the problem_split param. +# +sub encapsulate_minipage { + my ($text) = @_; + if (!($env{'form.problem.split'} =~ /yes/i)) { + $text = '\begin{minipage}{\textwidth}'.$text.'\end{minipage}'; + } + return $text; +} +# +# The NUMBER_TO_PRINT and SPLIT_PDFS +# variables interact, this sub looks at these two parameters +# and comes up with a final value for NUMBER_TO_PRINT which can be: +# all - if SPLIT_PDFS eq 'all'. +# 1 - if SPLIT_PDFS eq 'oneper' +# section - if SPLIT_PDFS eq 'sections' +# - if SPLIT_PDFS eq 'usenumber' +# +sub adjust_number_to_print { + my $helper = shift; + my $split_pdf = $helper->{'VARS'}->{'SPLIT_PDFS'}; + + if ($split_pdf eq 'all') { + $helper->{'VARS'}->{'NUMBER_TO_PRINT'} = 'all'; + } elsif ($split_pdf eq 'oneper') { + $helper->{'VARS'}->{'NUMBER_TO_PRINT'} = 1; + } elsif ($split_pdf eq 'sections') { + $helper->{'VARS'}->{'NUMBER_TO_PRINT'} = 'section'; + } elsif ($split_pdf eq 'usenumber') { + # Unmodified. + } else { + # Error!!!! + + croak "bad SPLIT_PDFS: $split_pdf in lonprintout::adjust_number_to_print"; + } +} sub character_chart { my $result = shift; @@ -313,6 +506,7 @@ sub character_chart { $result =~ s/&(\#252|uuml);/\\\"{u}/g; $result =~ s/&(\#253|yacute);/\\\'{y}/g; $result =~ s/&(\#255|yuml);/\\\"{y}/g; + $result =~ s/&\#295;/\\ensuremath\{\\hbar\}/g; $result =~ s/&\#952;/\\ensuremath\{\\theta\}/g; #Greek Alphabet $result =~ s/&(alpha|\#945);/\\ensuremath\{\\alpha\}/g; @@ -388,6 +582,7 @@ sub character_chart { $result =~ s/&(prod|\#8719);/\\ensuremath\{\\prod\}/g; $result =~ s/&(sum|\#8721);/\\ensuremath\{\\sum\}/g; $result =~ s/&(minus|\#8722);/\\ensuremath\{-\}/g; + $result =~ s/–/\\ensuremath\{-\}/g; $result =~ s/&(lowast|\#8727);/\\ensuremath\{*\}/g; $result =~ s/&(radic|\#8730);/\\ensuremath\{\\surd\}/g; $result =~ s/&(prop|\#8733);/\\ensuremath\{\\propto\}/g; @@ -433,7 +628,7 @@ my %page_formats= '2' => ['3.66 in','9.8 in', '-0.57 in','-0.57 in','0.7 cm'] }, 'album' => { - '1' => [ '8.8 in', '6.8 in','-40 pt in', '-60 pt','1 cm'], + '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'] }, }, @@ -443,8 +638,8 @@ my %page_formats= '2' => ['3.16 in','13 in','-0.57 in','-0.57 in','-0.5 in'] }, 'album' => { - '1' => [], - '2' => [] + '1' => ['12 in','7.1 in',,'-0.57 in','-0.57 in','-0.5 in'], + '2' => ['6.0 in','7.1 in','-1 in','-1 in','5 in'] }, }, 'tabloid' => { @@ -453,8 +648,8 @@ my %page_formats= '2' => ['4.9 in','16 in','-0.57 in','-0.57 in','-0.5 in'] }, 'album' => { - '1' => [], - '2' => [] + '1' => ['16 in','9.8 in','-0.57 in','-0.57 in','-0.5 in'], + '2' => ['16 in','4.9 in','-0.57 in','-0.57 in','-0.5 in'] }, }, 'executive' => { @@ -489,12 +684,12 @@ my %page_formats= }, 'a4' => { 'book' => { - '1' => ['176 mm','272 mm','-40 pt in','-60 pt','-0.5 in'], - '2' => [ '91 mm','272 mm','-40 pt in','-60 pt','-0.5 in'] + '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'] }, 'album' => { - '1' => ['8.5 in','7.7 in','-40 pt in','-60 pt','0 in'], - '2' => ['3.9 in','7.7 in','-40 pt in','-60 pt','0 in'] + '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'] }, }, 'a5' => { @@ -538,14 +733,14 @@ sub get_name { if (!defined($udom)) { $udom=$env{'user.domain'}; } my $plainname=&Apache::loncommon::plainname($uname,$udom); if ($plainname=~/^\s*$/) { $plainname=$uname.'@'.$udom; } - $plainname=&Apache::lonxml::latex_special_symbols($plainname,'header'); + $plainname=&Apache::lonxml::latex_special_symbols($plainname,'header'); return $plainname; } sub get_course { my $courseidinfo; if (defined($env{'request.course.id'})) { - $courseidinfo = &Apache::lonxml::latex_special_symbols(&Apache::lonnet::unescape($env{'course.'.$env{'request.course.id'}.'.description'}),'header'); + $courseidinfo = &Apache::lonxml::latex_special_symbols(&unescape($env{'course.'.$env{'request.course.id'}.'.description'}),'header'); } return $courseidinfo; } @@ -553,22 +748,28 @@ sub get_course { sub page_format_transformation { my ($papersize,$layout,$numberofcolumns,$choice,$text,$assignment,$tableofcontents,$indexlist,$selectionmade) = @_; my ($textwidth,$textheight,$oddoffset,$evenoffset,$topmargin); + if ($selectionmade eq '4') { $assignment='Problems from the Whole Course'; } else { $assignment=&Apache::lonxml::latex_special_symbols($assignment,'header'); } ($textwidth,$textheight,$oddoffset,$evenoffset,$topmargin) = &page_format($papersize,$layout,$numberofcolumns,$topmargin); + + my $name = &get_name(); my $courseidinfo = &get_course(); if (defined($courseidinfo)) { $courseidinfo=' - '.$courseidinfo } + my $header_text = $parmhash{'print_header_format'}; + $header_text = &format_page_header($header_text, $assignment, + $courseidinfo, $name); my $topmargintoinsert = ''; if ($topmargin ne '0') {$topmargintoinsert='\setlength{\topmargin}{'.$topmargin.'}';} my $fancypagestatement=''; if ($numberofcolumns eq '2') { - $fancypagestatement="\\fancyhead{}\\fancyhead[LO]{\\textbf{$name} $courseidinfo \\hfill \\thepage \\\\ \\textit{$assignment}}"; + $fancypagestatement="\\fancyhead{}\\fancyhead[LO]{$header_text}"; } else { - $fancypagestatement="\\rhead{}\\chead{}\\lhead{\\textbf{$name} $courseidinfo \\hfill \\thepage \\\\ \\textit{$assignment}}"; + $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 /; @@ -626,18 +827,21 @@ sub details_for_menu { return ($name_of_resource,$name_of_sequence,$name_of_map); } +sub copyright_line { + 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 } '; +} +my $end_of_student = "\n".'\special{ps:ENDOFSTUDENTSTAMP}'."\n"; sub latex_corrections { - - my ($number_of_columns,$result,$selectionmade) = @_; - + my ($number_of_columns,$result,$selectionmade,$answer_mode) = @_; # $result =~ s/\\includegraphics{/\\includegraphics\[width=\\minipagewidth\]{/g; - $result =~ s/\$number_of_columns/$number_of_columns/g; - if ($selectionmade ne '1') { - $result =~ s/(\\end{document})/\\strut\\vspace\*{-4 mm}\\newline\\noindent\\makebox\[\\textwidth\/$number_of_columns\]\[b\]{\\hrulefill}\\newline\\noindent\\tiny Printed from LON-CAPA\\copyright MSU{\\hfill} Licensed under GNU General Public License $1/; + 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/; } else { - $result =~ s/(\\end{document})/\\strut\\newline\\noindent\\makebox\[\\textwidth\/$number_of_columns\]\[b\]{\\hrulefill}\\newline\\noindent\\tiny Printed from LON-CAPA\\copyright MSU{\\hfill} Licensed under GNU General Public License $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; #-- LaTeX corrections @@ -648,7 +852,8 @@ sub latex_corrections { $first_comment = index($result,' ELEMENTHTML return $result; } -# If the user didn't select 1 column, skip this state. + sub preprocess { my $self = shift; my $helper = Apache::lonhelper::getHelper(); my $format = $helper->{VARS}->{$self->{'formatvar'}}; - if (substr($format, 2, 1) ne '1') { - $helper->changeState($self->{NEXTSTATE}); + + # If the user does not have 'pav' privilege, set default widths and + # on to the next state right away. + # + if (!$perm{'pav'}) { + my $var = $self->{'variable'}; + my $format = $helper->{VARS}->{$self->{'formatvar'}}; + + my ($laystyle, $cols, $papersize) = split(/\|/, $format); + ($papersize) = split(/ /, $papersize); + + + if ($laystyle eq 'L') { + $laystyle = 'album'; + } else { + $laystyle = 'book'; + } + # Figure out some good defaults for the print out and set them: + + my %size; + ($size{'width'}, + $size{'height'}, + $size{'lmargin'})= + &Apache::lonprintout::page_format($papersize, $laystyle, $cols); + + foreach my $dim ('width', 'height', 'lmargin') { + my ($value, $units) = split(/ /, $size{$dim}); + + $helper->{VARS}->{"$var.".$dim} = $value; + $helper->{VARS}->{"$var.".$dim.'unit'} = $units; + + } + + + # Transition to the next state + + $helper->changeState($self->{NEXTSTATE}); } return 1; @@ -2468,14 +3177,22 @@ sub postprocess { # /^-?[0-9]+(\.[0-9]*)?$/ -> optional minus, at least on digit, followed # by an optional period, followed by digits, ending the string - if ($width !~ /^-?[0-9]+(\.[0-9]*)?$/) { + if ($width !~ /^-?[0-9]*(\.[0-9]*)?$/) { $error .= "Invalid width; please type only a number.
\n"; } - if ($height !~ /^-?[0-9]+(\.[0-9]*)?$/) { + if ($height !~ /^-?[0-9]*(\.[0-9]*)?$/) { $error .= "Invalid height; please type only a number.
\n"; } - if ($lmargin !~ /^-?[0-9]+(\.[0-9]*)?$/) { + if ($lmargin !~ /^-?[0-9]*(\.[0-9]*)?$/) { $error .= "Invalid left margin; please type only a number.
\n"; + } else { + # Adjust for LaTeX 1.0 inch margin: + + if ($env{"form.${var}.lmarginunit"} eq "in") { + $helper->{VARS}->{$var.'.lmargin'} = $lmargin - 1; + } else { + $helper->{VARS}->{$var.'.lmargin'} = $lmargin - 2.54; + } } if (!$error) {