--- loncom/interface/lonprintout.pm	2003/05/07 19:09:10	1.157
+++ loncom/interface/lonprintout.pm	2003/05/15 13:32:02	1.173
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Printout
 #
-# $Id: lonprintout.pm,v 1.157 2003/05/07 19:09:10 bowersj2 Exp $
+# $Id: lonprintout.pm,v 1.173 2003/05/15 13:32:02 sakharuk Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -481,15 +481,16 @@ ENDPART
       $selectionmade = 1;
       if ($ENV{'form.url'}=~/\.(problem|exam|quiz|assess|survey|form|library|page|xml|html|htm|xhtml|xhtm)$/) {
         my %moreenv;
-        $moreenv{'form.grade_target'}='tex';
         if (&Apache::lonnet::allowed('bre',$ENV{'form.url'})) {
 	  $ENV{'form.url'}=~s/http:\/\/[^\/]+//;
         }
         $moreenv{'request.filename'}=$ENV{'form.url'};
-        $moreenv{'form.textwidth'}=$LaTeXwidth;
+	my %form;
+        $form{'textwidth'}=$LaTeXwidth;
+        $form{'grade_target'}='tex';
         &Apache::lonnet::appenv(%moreenv);
-        my $texversion=&Apache::lonnet::ssi($ENV{'form.url'});
-        &Apache::lonnet::delenv('form.grade_target','form.textwidth','form.counter');
+        my $texversion=&Apache::lonnet::ssi($ENV{'form.url'},%form);
+        &Apache::lonnet::delenv('form.counter');
         $result .= $texversion;
         if ($ENV{'form.url'}=~m/\.page\s*$/) {
 	  ($result,$number_of_columns) = &page_cleanup($result);
@@ -523,13 +524,12 @@ ENDPART
 	    } elsif ($choice eq 'Standard LaTeX output for whole primary sequence') {
               $selectionmade = 3;
 	    }
-	   my %moreenv;
-	   $moreenv{'form.grade_target'}='tex';
-	   $moreenv{'form.textwidth'}=$LaTeXwidth;
-	   &Apache::lonnet::appenv(%moreenv);
+	   my %form;
+	   $form{'textwidth'}=$LaTeXwidth;
+	   $form{'grade_target'}='tex';
+	   $form{'symb'}=$symb;
 	   #&Apache::lonnet::logthis("Trying to get $urlp with symb $symb");
-	   my $texversion=&Apache::lonnet::ssi($urlp,('symb'=>$symb));
-	   &Apache::lonnet::delenv('form.grade_target','form.textwidth');
+	   my $texversion=&Apache::lonnet::ssi($urlp,%form);
 	   if ($urlp =~ m/\.page/) {
 	     ($texversion,my $number_of_columns_page) = &page_cleanup($texversion);
 	     if ($number_of_columns_page > $number_of_columns) {$number_of_columns=$number_of_columns_page;} 
@@ -563,12 +563,11 @@ ENDPART
 	     my ($urlp,$symb) = split /&&/, $file_seq[$i];
 	     $urlp=~s/\/home\/httpd\/html//;	    
 	     if ($urlp=~m/\.(problem|exam|quiz|assess|survey|form|library)/) {
-		my %moreenv;
-		$moreenv{'form.grade_target'}='tex';
-		$moreenv{'form.textwidth'}=$LaTeXwidth;
-		&Apache::lonnet::appenv(%moreenv);
-		my $texversion=&Apache::lonnet::ssi($urlp,('symb'=>$symb));
-		&Apache::lonnet::delenv('form.grade_target','form.textwidth');		    
+		my %form;
+		$form{'grade_target'}='tex';
+		$form{'textwidth'}=$LaTeXwidth;
+		$form{'symb'}=$symb;
+		my $texversion=&Apache::lonnet::ssi($urlp,%form);
 		if ($flag_latex_header_remove ne 'NO') {
 		    $texversion = &latex_header_footer_remove($texversion);
 		} else {
@@ -658,17 +657,15 @@ ENDPART
 	for (my $i=0;$i<=$#list_of_files;$i++) {
 	    my $urlp = $list_of_files[$i];
 	    if ($urlp=~/\//) {
-		my %moreenv;
-		$moreenv{'form.grade_target'}='tex';
-		$moreenv{'form.textwidth'}=$LaTeXwidth;
-		&Apache::lonnet::appenv(%moreenv);
+		my %form;
+		$form{'grade_target'}='tex';
+		$form{'textwidth'}=$LaTeXwidth;
 		if ($urlp =~ m|/home/([^/]+)/public_html|) {
 		    $urlp =~ s|/home/([^/]*)/public_html|/~$1|;
 		} else {
 		    $urlp =~ s|^/home/httpd/html||;
 		}
-		my $texversion=&Apache::lonnet::ssi($urlp);
-		&Apache::lonnet::delenv('form.grade_target','form.textwidth');
+		my $texversion=&Apache::lonnet::ssi($urlp,%form);
                 #this chunck is responsible for printing the path to problem
 		my $newurlp = '';
 		my $HowMany = length($urlp)*2;
@@ -1037,7 +1034,6 @@ sub character_chart {
     $result =~ s/&(Phi|\#934);/\$\\Phi \$/g;
     $result =~ s/&(Psi|\#936);/\$\\Psi \$/g;
     $result =~ s/&(Omega|\#937);/\$\\Omega \$/g;
-
     return $result;
 }
 
@@ -1363,28 +1359,41 @@ ENDPART
     if ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'current_document') {
       #-- single document - problem, page, html, xml, ...
 	my $currentURL;
-	if (not defined $helper->{'VARS'}->{'construction'}) {
+	if ($helper->{'VARS'}->{'construction'} ne '1') {
+            #prins published resource
 	    $currentURL=$helper->{'VARS'}->{'postdata'};
 	} else {
-	    $currentURL=$helper->{'VARS'}->{'construction'};
+            #prins resource from the construction space
+	    $currentURL=$helper->{'VARS'}->{'filename'};
+	    $currentURL=~s/\/home\//\/~/;
+	    $currentURL=~s/public_html\///;
 	}
 	$selectionmade = 1;
 	if ($currentURL=~/\.(problem|exam|quiz|assess|survey|form|library|page|xml|html|htm|xhtml|xhtm)$/) {
-        my %moreenv;
-        $moreenv{'form.grade_target'}='tex';
-	if (&Apache::lonnet::allowed('bre',$currentURL)) {
-	    $currentURL=~s/http:\/\/[^\/]+//;
-	}
-        $moreenv{'request.filename'}=$currentURL;
-        $moreenv{'form.textwidth'}=$LaTeXwidth;
-        &Apache::lonnet::appenv(%moreenv);
-        my $texversion=&Apache::lonnet::ssi($currentURL);
-        &Apache::lonnet::delenv('form.grade_target','form.textwidth','form.counter');
-        $result .= $texversion;
-        if ($currentURL=~m/\.page\s*$/) {
-	  ($result,$number_of_columns) = &page_cleanup($result);
-        }
-      } else {
+	    my %moreenv;
+	    $moreenv{'request.filename'}=$currentURL;
+	    my %form;
+	    $form{'grade_target'}='tex';
+	    $form{'textwidth'}=$LaTeXwidth;
+	    my $rndseed=time;
+	    $form{'rndseed'}=$rndseed;
+	    &Apache::lonnet::appenv(%moreenv);
+	    my $texversion=&Apache::lonnet::ssi($currentURL,%form);
+	    &Apache::lonnet::delenv('form.counter');
+	    &Apache::lonnet::delenv('request.filename');
+	    if($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') {
+		my %form;
+		$form{'grade_target'}='answer';
+		$form{'answer_output_mode'}='tex';
+		$form{'rndseed'}=$rndseed;
+		my $answer=&Apache::lonnet::ssi($currentURL,%form);
+		$texversion=~s/(\\keephidden{ENDOFPROBLEM})/$answer$1/;
+	    }
+	    $result .= $texversion;
+	    if ($currentURL=~m/\.page\s*$/) {
+		($result,$number_of_columns) = &page_cleanup($result);
+	    }
+	} else {
 #	  $result='\documentclass[letterpaper]{article}\usepackage{calc}\begin{document}Printout of this type of document is currently not supported: ';
 #	  if ($ENV{'form.url'}=~/\/(aboutme|syllabus|bulletinboard|smppg)$/) {
 #	      $result.=$1;
@@ -1392,9 +1401,7 @@ ENDPART
 #	      $result.=$ENV{'form.url'};
 #	  }
 #	  $result.=' \end{document}'
-      }
-
-
+	}
     } elsif (($helper->{'VARS'}->{'PRINT_TYPE'} eq 'map_problems') or
              ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'map_problems_pages') or
              ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'all_problems')) { 
@@ -1412,13 +1419,12 @@ ENDPART
 	    } elsif ($helper->{'VARS'}->{'PRINT_TYPE'} eq 'all_problems') {
 		$selectionmade = 4;
 	    }
-	    my %moreenv;
-	    $moreenv{'form.grade_target'}='tex';
-	    $moreenv{'form.textwidth'}=$LaTeXwidth;
-	    &Apache::lonnet::appenv(%moreenv);
+	    my %form;	   
+	    $form{'grade_target'}='tex';
+	    $form{'textwidth'}=$LaTeXwidth;
+	    $form{'symb'}=$master_seq[$i];
 	    #&Apache::lonnet::logthis("Trying to get $urlp with symb $master_seq[$i]");
-	    my $texversion=&Apache::lonnet::ssi($urlp,('symb'=>$master_seq[$i]));
-	    &Apache::lonnet::delenv('form.grade_target','form.textwidth');
+	    my $texversion=&Apache::lonnet::ssi($urlp,%form);
 	    if ($urlp=~/\.page$/) {
 		($texversion,my $number_of_columns_page) = &page_cleanup($texversion);
 		if ($number_of_columns_page > $number_of_columns) {$number_of_columns=$number_of_columns_page;} 
@@ -1430,6 +1436,13 @@ ENDPART
 	    } else {
 		$texversion =~ s/\\end{document}//;
 	    }
+	    if($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') {
+		my %form;
+		$form{'grade_target'}='answer';
+		$form{'answer_output_mode'}='tex';
+		my $answer=&Apache::lonnet::ssi($urlp,%form);
+		$texversion=~s/(\\keephidden{ENDOFPROBLEM})/$answer$1/;
+	    }
 	    $result .= $texversion;         
 	    $flag_latex_header_remove = 'YES';   
 	}
@@ -1464,6 +1477,13 @@ ENDPART
 			} else {
 			    $rendered =~ s/\\end{document}//;
 			}
+			if($helper->{'VARS'}->{'ANSWER_TYPE'} eq 'no') {
+			    my %form;
+			    $form{'answer_output_mode'}='tex';
+			    my $ansrendered = &Apache::loncommon::get_student_answers($curresline,$username,$userdomain,
+										      $ENV{'request.course.id'},%form);
+			    $rendered=~s/(\\keephidden{ENDOFPROBLEM})/$ansrendered$1/;
+			}
 			$current_output .= $rendered;
 		    }
 		    $flag_latex_header_remove = 'YES';
@@ -1495,17 +1515,15 @@ ENDPART
 	for (my $i=0;$i<=$#list_of_files;$i++) {
 	    my $urlp = $list_of_files[$i];
 	    if ($urlp=~/\//) {
-		my %moreenv;
-		$moreenv{'form.grade_target'}='tex';
-		$moreenv{'form.textwidth'}=$LaTeXwidth;
-		&Apache::lonnet::appenv(%moreenv);
+		my %form;
+		$form{'grade_target'}='tex';
+		$form{'textwidth'}=$LaTeXwidth;
 		if ($urlp =~ m|/home/([^/]+)/public_html|) {
 		    $urlp =~ s|/home/([^/]*)/public_html|/~$1|;
 		} else {
 		    $urlp =~ s|^/home/httpd/html||;
 		}
-		my $texversion=&Apache::lonnet::ssi($urlp);
-		&Apache::lonnet::delenv('form.grade_target','form.textwidth');
+		my $texversion=&Apache::lonnet::ssi($urlp,%form);
                 #this chunck is responsible for printing the path to problem
 		my $newurlp = '';
 		my $HowMany = length($urlp)*2;
@@ -1583,10 +1601,11 @@ sub handler {
         $helper = $result;
 	
 	
-#	my $key; 
-#	foreach $key (keys %{$helper->{'VARS'}}) {
-#	    $r->print(' '.$key.'->'.$helper->{'VARS'}->{$key}.'<br />');
-#	}
+
+	my $key; 
+	foreach $key (keys %{$helper->{'VARS'}}) {
+	    $r->print(' '.$key.'->'.$helper->{'VARS'}->{$key}.'<-<br />');
+	}
 #	return OK;
 	
 	
@@ -1750,16 +1769,9 @@ sub printHelper {
 
     # "Delete everything after the last slash."
     $subdir =~ s|/[^/]+$||;
-    if (not defined $helper->{VARS}->{'construction'}) {
-	$subdir =  '/home/httpd/html/res'.$subdir;   
-    } else {
-	$helper->{VARS}->{'construction'}=~/^([^\/]+)\/\/([^\/]+)(.*)\/[^\/]*$/;
-	$subdir = $3;
-	$subdir=~/\/~([^\/]+)\/(.*)$/;
-	$subdir = '/home/'.$1.'/public_html/'.$2;
+    if (not $helper->{VARS}->{'construction'}) {
+	$subdir='/home/httpd/html/res/'.$subdir;
     }
-####    $r->print('Dir:  '.$subdir.'  ');
-####    return OK;
 
 
     # What can be printed is a very dynamic decision based on
@@ -1783,7 +1795,7 @@ sub printHelper {
     $r->print($helper->{VARS}->{'postdata'});
 
     # If we're in a sequence...
-    if (!defined($helper->{'VARS'}->{'construction'})) {
+    if ($helper->{'VARS'}->{'construction'} ne '1') {
         # Allow problems from sequence
         push @{$printChoices}, ["Problem(s) from <b>$sequenceTitle</b>", 'map_problems', 'CHOOSE_PROBLEMS'];
         # Allow all resources from sequence
@@ -1830,7 +1842,7 @@ HELPERFRAGMENT
         &Apache::lonxml::xmlparse($r, 'helper', <<CHOOSE_STUDENTS);
   <state name="ALL_PROBLEMS" title="Select Problem(s) to print">
     <message>(mark them then click "next" button) <br /></message>
-    <resource variable="RESOURCES" multichoice="1">
+    <resource variable="RESOURCES" multichoice="1" suppressEmptySequences='1'>
       <nextstate>PAGESIZE</nextstate>
       <filterfunc>return $isProblemOrMap</filterfunc>
       <choicefunc>return $isProblem</choicefunc>
@@ -1891,6 +1903,21 @@ CHOOSE_FROM_SUBDIR
     $helper->declareVar('PRINT_TYPE');
     $paramHash->{CHOICES} = $printChoices;
     Apache::lonhelper::choices->new();
+
+    if (($ENV{'request.role.adv'} and &Apache::lonnet::allowed('vgr',$ENV{'request.course.id'})) or 
+	($helper->{VARS}->{'construction'} eq '1')) {
+        $paramHash = Apache::lonhelper::getParamHash();
+        $paramHash->{MESSAGE_TEXT} = "<br /><big><b><i>Next option is available only for advanced users:</i></b></big><br />";
+        Apache::lonhelper::message->new();
+        $paramHash = Apache::lonhelper::getParamHash();
+	$paramHash->{'variable'} = 'ANSWER_TYPE';   
+	$helper->declareVar('ANSWER_TYPE');         
+        $paramHash->{CHOICES} = [
+                                   ['Print without answer', 'yes'],
+                                   ['Print with answers', 'no'] ];
+        Apache::lonhelper::choices->new();
+    }
+
     Apache::lonprintout::page_format_state->new("FORMAT");
 
     # Generate the PAGESIZE state which will offer the user the margin