--- loncom/interface/lonprintout.pm 2012/10/12 13:48:08 1.620 +++ loncom/interface/lonprintout.pm 2014/09/21 16:32:26 1.627.2.13 @@ -1,7 +1,7 @@ # The LearningOnline Network # Printout # -# $Id: lonprintout.pm,v 1.620 2012/10/12 13:48:08 raeburn Exp $ +# $Id: lonprintout.pm,v 1.627.2.13 2014/09/21 16:32:26 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -27,7 +27,6 @@ # package Apache::lonprintout; use strict; -use POSIX; use Apache::Constants qw(:common :http); use Apache::lonxml; use Apache::lonnet; @@ -47,7 +46,6 @@ use File::Basename; use HTTP::Response; use LONCAPA::map(); -use POSIX qw(ctime); use Apache::lonlocal; use Carp; use LONCAPA; @@ -149,8 +147,7 @@ sub printable_sequence { # Return: # XML that can be parsed by the helper to drive the state machine. # -sub create_incomplete_folder_selstud_helper($helper) -{ +sub create_incomplete_folder_selstud_helper { my ($helper, $map) = @_; @@ -495,7 +492,7 @@ RESOURCE_SELECTOR # # @return ($open, $close) # -# @note If open/close dates are not defined they will be retunred as undef +# @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. @@ -525,7 +522,7 @@ sub compute_open_window { # If no overlap...both are -1 as promised. - if (defined($earliest_close) && defined($latest_open) + if (($earliest_close ne '') && ($latest_open ne '') && ($earliest_close < $latest_open)) { $latest_open = -1; $earliest_close = -1; @@ -573,11 +570,14 @@ sub printable { # # @return (opendate, closedate) # -# @note If open/close dates are not defined they will be retunred as undef +# @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; @@ -596,7 +596,18 @@ sub get_print_dates { 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); } @@ -604,13 +615,6 @@ sub get_print_dates { ($open_date, $close_date) = &compute_open_window(\@open_dates, \@close_dates); - if ($open_date) { - $open_date = POSIX::strftime('%D', localtime($open_date)); - } - if ($close_date) { - $close_date = POSIX::strftime('%D', localtime($close_date)); - } - return ($open_date, $close_date); } @@ -705,11 +709,14 @@ sub incomplete { # to only those that are in the original set selcted to be printed. # # Parameters: -# $helper - The helper we need $helper->{'VARS'}->{'symb'} -# to construct the navmap and the iteration. -# $seq - The original set of resources to print +# $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 # # Implicit inputs: # $ @@ -718,53 +725,58 @@ sub incomplete { # print_resources. # sub master_seq_to_person_seq { - my ($helper, $seq, $who) = @_; + 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 ($perm{'pav'} && $perm{'vgr'} && $nohidemap) { + $unhidden = &Apache::lonnet::clutter($map); + } - my ($map, $id, $url) = &Apache::lonnet::decode_symb($helper->{VARS}->{'symb'}); - my $navmap = Apache::lonnavmaps::navmap->new($username, $userdomain); - my $iterator = $navmap->getIterator($navmap->firstResource(), - $navmap->finishResource(), - {}, 1); - my %nonResourceItems = ( - $iterator->BEGIN_MAP => 1, - $iterator->BEGIN_BRANCH => 1, - $iterator->END_BRANCH => 1, - $iterator->END_MAP => 1, - $iterator->FORWARD => 1, - $iterator->BACKWARD => 1 + 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(); + } - ); # These items are not resources but appear in the midst of iteration. + my $iterator = $navmap->getIterator($start,$finish,{},1); # Iterate on the resource..select the items that are randomly selected # and that are in the seq_has. 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 (! exists $nonResourceItems{$curres} && ! $curres->randomout()) { - my $symb = $curres->symb(); - if (exists $seq_hash{$symb}) { - push(@output_seq, $symb); + + if (ref($curres) && ! $curres->randomout()) { + my $currsymb = $curres->symb(); + if (exists($seq_hash{$currsymb})) { + push(@output_seq, $currsymb); } } } - return \@output_seq; # for now. @@ -1044,12 +1056,12 @@ sub printf_style_subst { # %s - The section if it is supplied. # sub format_page_header { - my ($width, $format, $assignment, $course, $student, $section) = @_; + my ($width, $format, $assignment, $course, $student) = @_; $width = &recalcto_mm($width); # Get width in mm. - my $chars_per_line = int($width/1.6); # Character/textline. + my $chars_per_line = int($width/2); # Character/textline. # Default format? @@ -1065,40 +1077,44 @@ sub format_page_header { # - 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); + } - my $name_length = int($chars_per_line *3 /4); - my $sec_length = int($chars_per_line / 5); + $format = "\\textbf{$student} $course \\hfill \\thepage \\\\ \\textit{$assignment}"; - $format = "%$name_length".'n'; + } else { + # 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!!! - if ($section) { - $format .= ' - Sec: '."%$sec_length".'s'; - } + } - $format .= '\\\\%c \\\\ %a'; - + return $format; - } - # 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); - $format = &printf_style_subst("s", $format, $section); - - - # 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!!! # 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('Construction Space').' \\\\'.&mt('Test-Printout '); + my $testPrintout = '\\\\'.&mt('Authoring Space').' \\\\'.&mt('Test-Printout '); $format =~ s/\\\\\s\\\\\s/$testPrintout/; } # @@ -1220,6 +1236,7 @@ sub is_code_valid { my ($code_type, $code_length) = ('letter', 6); # defaults. my @lines = &Apache::grades::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; @@ -1828,8 +1845,6 @@ sub get_course { my $courseidinfo; if (defined($env{'request.course.id'})) { $courseidinfo = &Apache::lonxml::latex_special_symbols(&unescape($env{'course.'.$env{'request.course.id'}.'.description'}),'header'); - my $sec = $env{'request.course.sec'}; - } return $courseidinfo; } @@ -1852,6 +1867,7 @@ sub page_format_transformation { 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($textwidth, $header_text, $assignment, $courseidinfo, $name); @@ -2060,6 +2076,7 @@ sub unsupported { my $result.= &print_latex_header($mode); if ($currentURL=~m|^(/adm/wrapper/)?ext/|) { $currentURL=~s|^(/adm/wrapper/)?ext/|http://|; + $currentURL=~s|^http://https://|https://|; my $title=&Apache::lonnet::gettitle($symb); $title = &Apache::lonxml::latex_special_symbols($title); $result.=' \strut \\\\ '.$title.' \strut \\\\ '.$currentURL.' '; @@ -2139,6 +2156,8 @@ sub print_page_in_course { my @page_resources = $navmap->retrieveResources($resource_src); $result .= &print_page_in_course($helper, $rparmhash, $resource_src, \@page_resources); + } elsif ($resource->ext()) { + $result .= &unsupported($currentURL,$mode,$symb); } # these resources go through the XML transformer: @@ -2155,7 +2174,7 @@ sub print_page_in_course { $form{'grade_target'} = 'tex'; $form{'textwidth'} = &get_textwidth($helper, $LaTeXwidth); - $form{'pdfFormFields'} = $pdfFormFields; # + $form{'pdfFormFields'} = 'no'; # $form{'showallfoils'} = $helper->{'VARS'}->{'showallfoils'}; $form{'problem_split'}=$parmhash{'problem_stream_switch'}; @@ -2208,7 +2227,7 @@ sub print_page_in_course { $texversion.=&path_to_problem($urlp,$LaTeXwidth); } else { $texversion.='\vskip 0 mm \noindent\textbf{'. - &mt("Printing from Construction Space: No Title").'}\vskip 0 mm '; + &mt("Printing from Authoring Space: No Title").'}\vskip 0 mm '; $texversion.=&path_to_problem($urlp,$LaTeXwidth); } $texversion.='\vskip 1 mm '.$answer.'\end{document}'; @@ -2605,6 +2624,7 @@ sub print_construction_sequence { # For item 100, filtering was done at the helper level. sub output_data { + my ($r,$helper,$rparmhash) = @_; my %parmhash = %$rparmhash; $ssi_error = 0; # This will be set nonzero by failing ssi's. @@ -2690,7 +2710,7 @@ ENDPART my %form; $form{'grade_target'} = 'tex'; $form{'textwidth'} = &get_textwidth($helper, $LaTeXwidth); - $form{'pdfFormFields'} = $pdfFormFields; + $form{'pdfFormFields'} = 'no'; # If form.showallfoils is set, then request all foils be shown: # privilege will be enforced both by not allowing the @@ -2788,7 +2808,7 @@ ENDPART $texversion.=&path_to_problem($cleanURL,$LaTeXwidth); } else { $texversion.='\vskip 0 mm \noindent\textbf{'. - &mt("Printing from Construction Space: No Title").'}\vskip 0 mm '; + &mt("Printing from Authoring Space: No Title").'}\vskip 0 mm '; $texversion.=&path_to_problem($cleanURL,$LaTeXwidth); } @@ -2878,7 +2898,6 @@ ENDPART ($print_type eq 'select_sequences') or ($print_type eq 'map_incomplete_problems_seq') ) { - #-- produce an output string if (($print_type eq 'map_problems') or @@ -3015,6 +3034,7 @@ ENDPART if (($selectionmade == 4) and ($assignment ne $prevassignment)) { my $name = &get_name(); my $courseidinfo = &get_course(); + if (defined($courseidinfo)) { $courseidinfo=' - '.$courseidinfo } $prevassignment=$assignment; my $header_text = $parmhash{'print_header_format'}; $header_text = &format_page_header($textwidth, $header_text, @@ -3141,6 +3161,12 @@ ENDPART } my @master_seq=split /\|\|\|/, $helper->{'VARS'}->{'RESOURCES'}; + my $map; + if ($helper->{VARS}->{'symb'}) { + ($map, my $id, my $resource) = + &Apache::lonnet::decode_symb($helper->{VARS}->{'symb'}); + } + #loop over students my $flag_latex_header_remove = 'NO'; @@ -3175,7 +3201,8 @@ ENDPART } else { $i=int($student_counter/$helper->{'VARS'}{'NUMBER_TO_PRINT'}); } - my $actual_seq = master_seq_to_person_seq($helper, \@master_seq, $person); + my $actual_seq = master_seq_to_person_seq($map, \@master_seq, + $person, undef, 1); my ($output,$fullname, $printed)=&print_resources($r,$helper, $person,$type, \%moreenv, $actual_seq, @@ -3201,7 +3228,6 @@ ENDPART my $old_name=$helper->{'VARS'}->{'REUSE_OLD_CODES'}; my $single_code = $helper->{'VARS'}->{'SINGLE_CODE'}; my $selected_code = $helper->{'VARS'}->{'CODE_SELECTED_FROM_LIST'}; - my $code_option=$helper->{'VARS'}->{'CODE_OPTION'}; my @lines = &Apache::grades::get_scantronformat_file(); my ($code_type,$code_length,$bubbles_per_row)=('letter',6,10); @@ -3218,6 +3244,19 @@ ENDPART } } } + my ($randomorder,$randompick,$map); + if ($helper->{VARS}{'symb'}) { + ($map, my $id, my $resource) = + &Apache::lonnet::decode_symb($helper->{VARS}{'symb'}); + my $navmap = Apache::lonnavmaps::navmap->new(); + if (defined($navmap)) { + if ($map) { + my $mapres = $navmap->getResourceByUrl($map); + $randomorder = $mapres->randomorder(); + $randompick = $mapres->randompick(); + } + } + } my %moreenv = ('textwidth' => &get_textwidth($helper,$LaTeXwidth)); $moreenv{'problem_split'} = $parmhash{'problem_stream_switch'}; $moreenv{'instructor_comments'}='hide'; @@ -3279,9 +3318,17 @@ ENDPART } else { $moreenv{'CODE'}=&num_to_letters($code); } + my $actual_seq = \@master_seq; + if ($randomorder || $randompick) { + $env{'form.CODE'} = $moreenv{'CODE'}; + $actual_seq = master_seq_to_person_seq($map, \@master_seq, + undef, + $moreenv{'CODE'}, 1); + delete($env{'form.CODE'}); + } my ($output,$fullname, $printed)= &print_resources($r,$helper,'anonymous',$type,\%moreenv, - \@master_seq,$flag_latex_header_remove, + $actual_seq,$flag_latex_header_remove, $LaTeXwidth); $resources_printed .= ":"; $print_array[$file_num].=$output; @@ -3334,7 +3381,7 @@ ENDPART $texversion.=&path_to_problem ($urlp,$LaTeXwidth); } else { $texversion.='\vskip 0 mm \noindent\textbf{'. - &mt("Printing from Construction Space: No Title").'}\vskip 0 mm '; + &mt("Printing from Authoring Space: No Title").'}\vskip 0 mm '; $texversion.=&path_to_problem ($urlp,$LaTeXwidth); } $texversion.='\vskip 1 mm '.$answer.'\end{document}'; @@ -3533,7 +3580,6 @@ sub print_resources { my $namepostfix = "\\\\"; # Both anon and not anon should get the same vspace. - # # Figure out if we need to filter the output by # the incomplete problems for that person @@ -3575,6 +3621,9 @@ sub print_resources { my $current_assignment = ""; my $assignment; my $courseidinfo = &get_course(); + my $possprint = scalar(@{$master_seq}); + if (defined($courseidinfo)) { $courseidinfo=' - '.$courseidinfo } + if ($usersection ne '') {$courseidinfo.=' - Sec. '.$usersection} foreach my $curresline (@{$master_seq}) { if (defined $page_breaks{$curresline}) { @@ -3594,6 +3643,7 @@ sub print_resources { next; } $actually_printed++; # we're going to print one. + if (&Apache::lonnet::allowed('bre',$res_url)) { if ($res_url!~m|^ext/| && $res_url=~/\.(problem|exam|quiz|assess|survey|form|library|page|xml|html|htm|xhtml|xhtm)$/) { @@ -3698,7 +3748,7 @@ sub print_resources { if (($assignment ne $current_assignment) && ($assignment ne "")) { my $header_line = &format_page_header($LaTeXwidth, $parmhash{'print_header_format'}, $assignment, $courseidinfo, - $fullname, $usersection); + $fullname); my $header_start = ($columns_in_format == 1) ? '\lhead' : '\fancyhead[LO]'; $header_line = $header_start.'{'.$header_line.'}'; @@ -3715,7 +3765,24 @@ sub print_resources { # if ($actually_printed == 0) { - $current_output = &encapsulate_minipage("\\vskip -10mm \nNo incomplete resources\n \\vskip 100 mm { }\n"); + my $message = &mt('No resources to print'); + if (!$possprint) { + if ($perm{'pav'} || $perm{'pfo'}) { + $message = &mt('There are no unhidden resources to print.')."\n\n". + &mt('The most likely reason is one of the following: ')."\n". + '\begin{itemize}'."\n". + '\item '.&mt("The 'Resource hidden from students' parameter is set for the folder being printed.")."\n". + '\item '.&mt("'Hidden' is checked in the Course Editor individually for each resource in the folder being printed.")."\n". + '\end{itemize}'."\n\n". + &mt("Note: to print a bubblesheet exam which you want to hide from students, ". + "use the Course Editor to check the 'Hidden' checkbox for the exam folder itself.")."\n"; + } + } elsif ($print_incomplete) { + $message = &mt('No incomplete resources'); + } + if ($message) { + $current_output = &encapsulate_minipage("\\vskip -10mm \n$message\n \\vskip 100 mm { }\n"); + } if ($remove_latex_header eq "NO") { $current_output = &print_latex_header() . $current_output; } else { @@ -3726,32 +3793,23 @@ sub print_resources { if ($syllabus_first) { $current_output =~ s/\\\\ Last updated:/Last updated:/ } - if (0) { - my $currentassignment=&Apache::lonxml::latex_special_symbols($helper->{VARS}->{'assignment'},'header'); - my $header_line = - &format_page_header($LaTeXwidth, $parmhash{'print_header_format'}, - $currentassignment, $courseidinfo, $fullname, $usersection); - my $header_start = ($columns_in_format == 1) ? '\lhead' - : '\fancyhead[LO]'; - $header_line = $header_start.'{'.$header_line.'}'; - } - if ($current_output=~/\\documentclass/) { -# $current_output =~ s/\\begin{document}/\\setlength{\\topmargin}{1cm} \\begin{document}\\noindent\\parbox{\\minipagewidth}{\\noindent$header_line$namepostfix}\\vskip 5 mm /; - $current_output =~ s/\\begin{document}/\\setlength{\\topmargin}{1cm} \\begin{document}\\noindent\\parbox{\\minipagewidth}{\\noindent$namepostfix}\\vskip 5 mm /; + my $currentassignment=&Apache::lonxml::latex_special_symbols($helper->{VARS}->{'assignment'},'header'); + my $header_line = + &format_page_header($LaTeXwidth, $parmhash{'print_header_format'}, + $currentassignment, $courseidinfo, $fullname); + my $header_start = ($columns_in_format == 1) ? '\lhead' : '\fancyhead[LO]'; + my $newheader = $header_start.'{'.$header_line.'}'; + if ($current_output=~/\\documentclass/) { + $current_output =~ s/\\begin{document}/\\setlength{\\topmargin}{1cm} \\begin{document}\\noindent\\parbox{\\minipagewidth}{\\noindent$newheader$namepostfix}\\vskip 5 mm /; } else { my $blankpages = '\clearpage\strut\clearpage'x$helper->{'VARS'}->{'EMPTY_PAGES'}; -# $current_output = '\strut\vspace*{-6 mm}\\newline'. -# ©right_line().' \newpage '.$blankpages.$end_of_student. -# '\setcounter{page}{1}\noindent\parbox{\minipagewidth}{\noindent'. -# $header_line.$namepostfix. '} \vskip 5 mm '.$current_output; $current_output = '\strut\vspace*{-6 mm}\\newline'. ©right_line().' \newpage '.$blankpages.$end_of_student. - '\setcounter{page}{1}\noindent\parbox{\minipagewidth}{\noindent' - .$namepostfix. '} \vskip 5 mm '.$current_output; - + '\setcounter{page}{1}\noindent\parbox{\minipagewidth}{\noindent'. + $newheader.$namepostfix. '} \vskip 5 mm '.$current_output; } # # Close the student bracketing. @@ -3800,7 +3858,6 @@ sub handler { } &init_perm(); - my $helper = printHelper($r); if (!ref($helper)) { return $helper; @@ -4020,7 +4077,7 @@ sub printHelper { &Apache::lonenc::check_encrypt(&Apache::lonnet::clutter($url)); my $navmap = Apache::lonnavmaps::navmap->new(); my $res = $navmap->getBySymb($symb); - $res_printable = $res->resprintable() | $userCanPrint; #printability in course context + $res_printable = $res->resprintable() || $userCanPrint; #printability in course context ($res_printstartdate, $res_printenddate) = &get_print_dates($res); ($course_open, $course_close) = &course_print_dates($res); ($map_open, $map_close) = &map_print_dates($res); @@ -4100,13 +4157,13 @@ sub printHelper { my $start_new_option; if ($perm{'pav'}) { $start_new_option = - "