--- loncom/imspackages/imsprocessor.pm	2005/07/12 16:05:59	1.22
+++ loncom/imspackages/imsprocessor.pm	2005/07/13 21:21:01	1.26
@@ -2095,7 +2095,7 @@ sub parse_webct4_questionDB {
             $$settings{$id}{$numid}{toltype} = $attr->{type};
         }
         if ("@state" eq "questestinterop section item resprocessing itemproc_extension webct:x_webct_v01_autocalculate webct:x_webct_v01_unit") {
-            my $unitid = $attr->{ident};
+            $unitid = $attr->{ident};
             %{$$settings{$id}{$numid}{$unitid}} = ();
             push(@{$$settings{$id}{$numid}{units}},$unitid);
             $$settings{$id}{$numid}{$unitid}{value} = $attr->{value}; 
@@ -3060,7 +3060,7 @@ sub write_webct4_questions {
                     } else {
                         $output .= qq|
 <optionresponse max="10" randomize="yes">
-    <foilgroup options=$allmatchlist>
+    <foilgroup options="$allmatchlist">
 |;
                     }
                 } else {
@@ -3251,11 +3251,34 @@ sub write_webct4_questions {
                     }
                 }
             } elsif ($$settings{$id}{class} eq 'numerical') {
+                my %mathfns = (
+                    'abs' => 'abs',
+                    'acos' => 'acos',
+                    'asin' => 'asin',
+                    'atan' => 'atan',
+                    'ceil' => 'ceil',
+                    'cos' => 'cos',
+                    'exp' => 'exp',
+                    'fact' => 'factorial',
+                    'floor' => 'floor',
+                    'int' => 'int',
+                    'ln' => 'log',
+                    'log' => 'log',
+                    'max' => 'max',
+                    'min' => 'min',
+                    'round' => 'roundto',
+                    'sin' => 'sin',
+                    'sqrt' => 'sqrt',
+                    'tan' => 'tan',
+                );
+
                 my $scriptblock = qq|
 <script type="loncapa/perl">
 |;
                 foreach my $numid (@{$$settings{$id}{numids}}) {
                     my $formula = $$settings{$id}{$numid}{formula};
+                    my $pattern = join('|',(sort (keys (%mathfns))));
+                    $formula =~ s/($pattern)/\&$mathfns{$1}/g;
                     foreach my $var (keys %{$$settings{$id}{$numid}{vars}}) {
                         my $decnum = $$settings{$id}{$numid}{vars}{$var}{dec};
                         my $increment = '0.';
@@ -3269,9 +3292,7 @@ sub write_webct4_questions {
                             }
                             $increment .= '1';
                         } 
-                        $formula =~ s/{($var)}/\$$1/g;
-                        $formula =~ s/ln\(?([^\)])\)?/ &log($1) /g;
-                        $formula =~ s/sqrt/\&sqrt/g;
+                        $formula =~ s/{($var)}/(\$$1)/g;
                         $scriptblock .= qq|
 \$$var=&random($$settings{$id}{$numid}{vars}{$var}{min},$$settings{$id}{$numid}{vars}{$var}{max},$increment);
 |;
@@ -3302,7 +3323,7 @@ sub write_webct4_questions {
                         }
                         my $unitentry = '';
                         if ($unit ne '') {
-                            $unitentry =  'unit='.$unit;
+                            $unitentry =  'unit="'.$unit.'"';
                         }
                         $output .= qq|
 <numericalresponse $unitentry $ansformat  answer="\$answervar">