[LON-CAPA-cvs] cvs: loncom /homework default_homework.lcpm /homework/caparesponse caparesponse.pm /xml lonxml.pm

albertel lon-capa-cvs@mail.lon-capa.org
Tue, 05 Oct 2004 20:18:29 -0000


albertel		Tue Oct  5 16:18:29 2004 EDT

  Modified files:              
    /loncom/homework	default_homework.lcpm 
    /loncom/homework/caparesponse	caparesponse.pm 
    /loncom/xml	lonxml.pm 
  Log:
  - BUG#1363, support $ and , format specifiers in <numericalresponse> if $ is required, require the answer to have a $ at the beginning
  
  
Index: loncom/homework/default_homework.lcpm
diff -u loncom/homework/default_homework.lcpm:1.87 loncom/homework/default_homework.lcpm:1.88
--- loncom/homework/default_homework.lcpm:1.87	Wed Jun  9 17:16:43 2004
+++ loncom/homework/default_homework.lcpm	Tue Oct  5 16:18:29 2004
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA 
 # used by lonxml::xmlparse() as input variable $safeinit to Apache::run::run()
 #
-# $Id: default_homework.lcpm,v 1.87 2004/06/09 21:16:43 albertel Exp $
+# $Id: default_homework.lcpm,v 1.88 2004/10/05 20:18:29 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -41,7 +41,7 @@
     my $type=$LONCAPA::CAPAresponse_args{'type'};
     my $tol=$LONCAPA::CAPAresponse_args{'tol'};
     my $sig=$LONCAPA::CAPAresponse_args{'sig'};
-    my $ans_fmt=$LONCAPA::CAPAresponse_args{'ans_fmt'};
+    my $ans_fmt=$LONCAPA::CAPAresponse_args{'format'};
     my $unit=$LONCAPA::CAPAresponse_args{'unit'};
     my $calc=$LONCAPA::CAPAresponse_args{'calc'};
     my $samples=$LONCAPA::CAPAresponse_args{'samples'};
@@ -70,6 +70,13 @@
 	$response=~s/ +/ /g;
     }
     if ($type eq 'float') { $response=~s/,//g; }
+    &LONCAPA_INTERNAL_DEBUG("fmt $ans_fmt, res is $response");
+    if ($type eq 'float' && $ans_fmt=~/\$/) {
+	if ($response!~/^\$/)  { return "NO_UNIT: Missing \$ "; }
+	$response=~s/\$//g;
+    }
+    $ans_fmt=~s/\W//g;
+
     if (length($response) > 500) { return "TOO_LONG: Answer too long"; }
 
     if ($type eq '' ) {
@@ -530,6 +537,7 @@
 
 sub prettyprint {
     my ($value,$fmt,$target)=@_;
+    &LONCAPA_INTERNAL_DEBUG("format -$fmt-");
     my $result;
     if (!$target) { $target = $external::target; }
     if ($fmt =~ /chem/i) { return(&chemparse($value)); }
Index: loncom/homework/caparesponse/caparesponse.pm
diff -u loncom/homework/caparesponse/caparesponse.pm:1.152 loncom/homework/caparesponse/caparesponse.pm:1.153
--- loncom/homework/caparesponse/caparesponse.pm:1.152	Mon Aug 23 15:34:52 2004
+++ loncom/homework/caparesponse/caparesponse.pm	Tue Oct  5 16:18:29 2004
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # caparesponse definition
 #
-# $Id: caparesponse.pm,v 1.152 2004/08/23 19:34:52 albertel Exp $
+# $Id: caparesponse.pm,v 1.153 2004/10/05 20:18:29 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -103,16 +103,9 @@
 		} else {
 		    $format=$formats[0];
 		}
-		my $formatted;
-		if ((defined($format)) && ($format ne '')) {
-		    $format=~s/e/E/g;
-		    &Apache::lonxml::debug("formatting with :$format: answer :$answer:");
-		    $formatted=sprintf('%.'.$format,$answer).',';
-		} else {
-		    &Apache::lonxml::debug("no format answer :$answer:");
-		    $formatted="$answer,";
-		}
-		$answertxt.=$formatted;
+		my $formatted=&format_number($answer,$format,$target,
+					     $safeeval);
+		$answertxt.=$formatted.',';
 	    }
 	    chop $answertxt;
 	    if ($target eq 'web') {
@@ -155,7 +148,7 @@
 		    my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
 		    my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
 		    my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
-		    my @values=&make_numerical_bubbles($number_of_bubbles,$target,$answers[0],$formats[0],\@incorrect);
+		    my @values=&make_numerical_bubbles($number_of_bubbles,$target,$answers[0],$formats[0],\@incorrect,$safeeval);
 		    $response=$values[$response];
 		}
 		$Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
@@ -163,7 +156,7 @@
 		$$args_ref{'response'}=$response;
 		my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
 
-		foreach my $arg ('type','tol','sig','ans_fmt','unit','calc',
+		foreach my $arg ('type','tol','sig','format','unit','calc',
 				 'samples') {
 		    $$args_ref{$arg}=
 			&Apache::lonxml::get_param($arg,$parstack,$safeeval);
@@ -235,7 +228,7 @@
 	    my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
 	    my @bubble_values=&make_numerical_bubbles($number_of_bubbles,
 						      $target,$answers[0],
-						      $formats[0],\@incorrect);
+						      $formats[0],\@incorrect,$safeeval);
 	    my @alphabet=('A'..'Z');
 	    if ($target eq 'web') {
 		if ($tag eq 'numericalresponse') {
@@ -331,10 +324,10 @@
 	    }
 	    if ($fmt && $tag eq 'numericalresponse') {
 		$fmt=~s/e/E/g;
-		$ans = sprintf('%.'.$fmt,$ans);
+		$ans = &format_number($ans,$fmt,$target,$safeeval);
 		if ($high) {
-		    $high=sprintf('%.'.$fmt,$high);
-		    $low =sprintf('%.'.$fmt,$low);
+		    $high=&format_number($high,$fmt,$target,$safeeval);
+		    $low =&format_number($low,$fmt,$target,$safeeval);
 		}
 	    }
 	    if ($target eq 'answer') {
@@ -428,13 +421,9 @@
 }
 
 sub format_number {
-    my ($number,$format,$target)=@_;
+    my ($number,$format,$target,$safeeval)=@_;
     my $ans;
-    if ($format ne '') {
-	$format=~s/e/E/g;
-	$ans = sprintf('%.'.$format,$number);
-    } else {
-	my $format = '';
+    if ($format eq '') {
 	#What is the number? (integer,decimal,floating point)
 	if ($number=~/^(\d*\.?\d*)(E|e)(\d*)$/) {
 	    $format = '3e';
@@ -443,22 +432,13 @@
 	} elsif ($number=~/^(\d*)$/) {
 	    $format = 'd';
 	}
-	$ans = sprintf('%.'.$format,$number);
-    }
-    if ($target eq 'tex') {
-	if ($ans =~ m/([0-9\.\-\+]+)E([0-9\-\+]+)/ ) {
-	    my $number = $1;
-	    my $power = $2;
-	    $power=~s/^\+//;
-	    $power=~s/^(-?)0+(\d+)/$1$2/;
-	    $ans=$number.'$\times 10^{'.$power.'}$'; #'stupidemacs
-	}
     }
+    $ans=&Apache::run::run("&prettyprint(q\0$number\0,q\0$format\0,q\0$target\0)",$safeeval);
     return $ans;
 }
 
 sub make_numerical_bubbles {
-    my ($number_of_bubbles,$target,$answer,$format,$incorrect) =@_;
+    my ($number_of_bubbles,$target,$answer,$format,$incorrect,$safeeval) =@_;
     my @bubble_values = ();
     &Apache::lonxml::debug("answer is $answer incorrect is $incorrect");
     my @oldseed=&Math::Random::random_get_seed();
@@ -475,7 +455,7 @@
 	    &Math::Random::random_set_seed(@oldseed);
 	    if (defined($format) && $format ne '') {
 		foreach my $value (@bubble_values) {
-		    $value=&format_number($value,$format,$target);
+		    $value=&format_number($value,$format,$target,$safeeval);
 		}
 	    }
 	    return @bubble_values;
@@ -495,7 +475,7 @@
     for ($ind=0;$ind<$number_of_bubbles;$ind++) {
 	$bubble_values[$ind] = $answer*($factor**($power-$powers[$#powers-$ind]));
 	$bubble_values[$ind] = &format_number($bubble_values[$ind],
-					       $format,$target);
+					       $format,$target,$safeeval);
 
     }
     &Math::Random::random_set_seed(@oldseed);
Index: loncom/xml/lonxml.pm
diff -u loncom/xml/lonxml.pm:1.340 loncom/xml/lonxml.pm:1.341
--- loncom/xml/lonxml.pm:1.340	Tue Oct  5 14:54:49 2004
+++ loncom/xml/lonxml.pm	Tue Oct  5 16:18:29 2004
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # XML Parser Module 
 #
-# $Id: lonxml.pm,v 1.340 2004/10/05 18:54:49 albertel Exp $
+# $Id: lonxml.pm,v 1.341 2004/10/05 20:18:29 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1028,6 +1028,7 @@
     unless ($_=~/\W/) {
       my $val=$token->[2]->{$_};
       $val =~ s/([\%\@\\\"\'])/\\$1/g;
+      $val =~ s/^(\$\d)/\\$1/;
       #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
       $temp .= "my \$$_=\"$val\";";
     }
@@ -1477,7 +1478,7 @@
   } elsif ( $args !~ /my \$\Q$param\E=\"/ ) { return undef; }
   my $value=&Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
   &Apache::lonxml::debug("first run is $value");
-  if ($value =~ /^[\$\@\%]\w+$/) {
+  if ($value =~ /^[\$\@\%][a-zA-Z_]\w*$/) {
       &Apache::lonxml::debug("doing second");
       my @result=&Apache::run::run("return $value",$safeeval,1);
       if (!defined($result[0])) {