--- loncom/homework/functionplotresponse.pm	2011/09/28 23:33:52	1.64
+++ loncom/homework/functionplotresponse.pm	2011/11/18 18:54:21	1.73
@@ -1,7 +1,7 @@
 # LearningOnline Network with CAPA
-# option list style responses
+# Functionplot responses
 #
-# $Id: functionplotresponse.pm,v 1.64 2011/09/28 23:33:52 www Exp $
+# $Id: functionplotresponse.pm,v 1.73 2011/11/18 18:54:21 www Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -31,9 +31,11 @@ use strict;
 use Apache::response();
 use Apache::lonlocal;
 use Apache::lonnet;
+use Apache::run;
  
 BEGIN {
   &Apache::lonxml::register('Apache::functionplotresponse',('functionplotresponse','backgroundplot','spline',
+                                                            'plotobject','plotvector',
                                                             'functionplotrule','functionplotruleset',
                                                             'functionplotelements'));
 }
@@ -75,7 +77,7 @@ sub geogebra_default_parameters {
         <param name="boxborder" value="false"  />
         <param name="centerimage" value="true"  />
 	<param name="cache_archive" value="geogebra.jar, geogebra_main.jar, geogebra_gui.jar, geogebra_cas.jar, geogebra_export.jar, geogebra_algos.jar, geogebra_javascript.jar, geogebra_properties.jar, jlatexmath.jar, jlm_cyrillic.jar, jlm_greek.jar" />
-	<param name="cache_version" value="3.9.219.0, 3.9.219.0, 3.9.219.0, 3.9.219.0, 3.9.219.0, 3.9.219.0, 3.9.219.0, 3.9.219.0, 3.9.219.0, 3.9.219.0, 3.9.219.0" />
+	<param name="cache_version" value="4.0.1.0,4.0.1.0,4.0.1.0,4.0.1.0,4.0.1.0,4.0.1.0,4.0.1.0,4.0.1.0,4.0.1.0,4.0.1.0,4.0.1.0" />
         <param name="framePossible" value="false" />
 
         <param name="showResetIcon" value="false" />
@@ -163,6 +165,14 @@ sub update_register {
 sub set_point_coordinate {
    my ($id,$variable,$x,$y,$fixed)=@_;
    my $mult=($fixed?'a*':'');
+# Get rid of wild exponents, make sure it's a number
+   $x=1.*$x;
+   $y=1.*$y;
+# GeoGebra does not understand "E"
+   $x=~s/[e|E]/\*10\^/;
+   $x=~s/\+//;
+   $y=~s/[e|E]/\*10\^/;
+   $y=~s/\+//;
    return (<<ENDSETVARIABLE);
 document.ggbApplet_$id.evalCommand("a=1");
 document.ggbApplet_$id.evalCommand("$variable=$mult($x,$y)");
@@ -370,6 +380,58 @@ sub plot_script {
 }
 
 #
+# Subroutine to produce objects
+#
+
+sub plotobject_script {
+   my ($id,$label,$x,$y)=@_;
+   unless ($label) {
+      $Apache::functionplotresponse::counter++;
+      $label='O'.$Apache::functionplotresponse::counter;
+   }
+   return "document.ggbApplet_$id.evalCommand('a=1');\n".
+          "document.ggbApplet_$id.setVisible('a', false);\n".
+          "document.ggbApplet_$id.setLabelVisible('a', false);\n".
+          "document.ggbApplet_$id.evalCommand('$label=a*($x,$y)');\n".
+          "document.ggbApplet_$id.setVisible('$label', true);\n".
+          "document.ggbApplet_$id.setLabelVisible('$label', true);\n";
+}
+
+#
+# Subroutine to produce vectors
+#
+
+sub plotvector_script {
+   my ($id,$label,$xs,$ys,$xe,$ye,$xmin,$xmax)=@_;
+   unless ($label) {
+      $Apache::functionplotresponse::counter++;
+      $label='V'.$Apache::functionplotresponse::counter;
+   }
+   my $startlabel=$label.'Start';
+   my $endlabel=$label.'End';
+   my $pointlabel=$label.'Point';
+   my $pointx=2.*($xmax-$xmin)+$xmax;
+   my $anglelabel=$label.'Angle';
+   return(<<ENDVECTOR);
+document.ggbApplet_$id.evalCommand("$startlabel=($xs,$ys)");
+document.ggbApplet_$id.setVisible("$startlabel",false);
+document.ggbApplet_$id.setLabelVisible("$startlabel",false);
+document.ggbApplet_$id.evalCommand("$endlabel=($xe,$ye)");
+document.ggbApplet_$id.setLabelVisible("$endlabel",false);
+document.ggbApplet_$id.evalCommand("$label=Vector[$startlabel,$endlabel]");
+document.ggbApplet_$id.setLabelVisible("$label",true);
+document.ggbApplet_$id.setLineThickness("$label",8);
+document.ggbApplet_$id.evalCommand("$pointlabel=($pointx,y($startlabel))");
+document.ggbApplet_$id.evalCommand("$anglelabel=Angle[$pointlabel,$startlabel,$endlabel]");
+document.ggbApplet_$id.setLabelVisible("$anglelabel",true);
+document.ggbApplet_$id.setLabelStyle("$anglelabel",VALUE=2);
+document.ggbApplet_$id.registerObjectUpdateListener('$startlabel','updatePointCoordinates_$id');
+document.ggbApplet_$id.registerObjectUpdateListener('$endlabel','updatePointCoordinates_$id');
+document.ggbApplet_$id.registerObjectUpdateListener('$anglelabel','updatePointCoordinates_$id');
+ENDVECTOR
+}
+
+#
 # Answer spline display
 # 
 # points: x,y,slope_x,slope_y
@@ -422,6 +484,107 @@ sub generate_spline {
    $result.='document.ggbApplet_'.$id.'.evalCommand("Spline'.$order.'['.join(',',@coords).']");'."\n";
    return $result;
 }
+
+#
+# Object
+#
+
+sub start_plotobject {
+   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
+   my $result='';
+   my $internalid = $Apache::inputtags::part.'_'.$Apache::inputtags::response[-1];
+   my $x=&Apache::lonxml::get_param('x',$parstack,$safeeval);
+   my $y=&Apache::lonxml::get_param('y',$parstack,$safeeval);
+   my $label=&Apache::lonxml::get_param('label',$parstack,$safeeval);
+   $label=~s/\W//gs;
+   $label=ucfirst($label);
+   unless ($label) { $label="NewObject"; }
+   if ($target eq 'web') {
+      my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-3);
+      unless (defined($x)) { $x=$xmin; }
+      unless (defined($y)) { $y=$ymin; }
+      $result.=&plotobject_script($internalid,$label,$x,$y);
+   } elsif ($target eq 'edit') {
+        $result=&Apache::edit::tag_start($target,$token,'Plot Object').
+             &Apache::edit::text_arg('Label on Plot:','label',
+                                     $token,'16').
+             &Apache::edit::text_arg('x:','x',
+                                     $token,'8').
+             &Apache::edit::text_arg('y:','y',
+                                     $token,'8').
+             &Apache::edit::end_row();
+  } elsif ($target eq 'modified') {
+    my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'label','x','y');
+    if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
+  }
+  return $result;
+}
+
+sub end_plotobject {
+   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
+   my $result='';
+   if ($target eq 'edit') {
+       $result=&Apache::edit::end_table();
+   }
+   return $result;
+}
+
+#
+# Vector
+#
+
+sub start_plotvector {
+   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
+   my $result='';
+   my $internalid = $Apache::inputtags::part.'_'.$Apache::inputtags::response[-1];
+   my $tailx=&Apache::lonxml::get_param('tailx',$parstack,$safeeval);
+   my $taily=&Apache::lonxml::get_param('taily',$parstack,$safeeval);
+   my $tipx=&Apache::lonxml::get_param('tipx',$parstack,$safeeval);
+   my $tipy=&Apache::lonxml::get_param('tipy',$parstack,$safeeval);
+
+   my $label=&Apache::lonxml::get_param('label',$parstack,$safeeval);
+   $label=~s/\W//gs;
+   $label=ucfirst($label);
+   unless ($label) { $label="NewVector"; }
+   if ($target eq 'web') {
+      my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-3);
+      unless (defined($tailx)) { $tailx=$xmin; }
+      unless (defined($taily)) { $taily=$ymin; }
+      unless (defined($tipx)) { $tipx=$xmin; }
+      unless (defined($tipy)) { $tipy=$ymin; }
+      $result.=&plotvector_script($internalid,$label,$tailx,$taily,$tipx,$tipy,$xmin,$xmax);
+   } elsif ($target eq 'edit') {
+        $result=&Apache::edit::tag_start($target,$token,'Plot Vector').
+             &Apache::edit::text_arg('Label on Plot:','label',
+                                     $token,'16').
+             &Apache::edit::text_arg('Tail x:','tailx',
+                                     $token,'8').
+             &Apache::edit::text_arg('Tail y:','taily',
+                                     $token,'8').
+             &Apache::edit::text_arg('Tip x:','tipx',
+                                     $token,'8').
+             &Apache::edit::text_arg('Tip y:','tipy',
+                                     $token,'8').
+
+             &Apache::edit::end_row();
+  } elsif ($target eq 'modified') {
+    my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'label','tailx','taily','tipx','tipy');
+    if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
+  }
+  return $result;
+}
+
+sub end_plotvector {
+   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
+   my $result='';
+   if ($target eq 'edit') {
+       $result=&Apache::edit::end_table();
+   }
+   return $result;
+}
+
+
+
 #
 # <backgroundplot function="..." fixed="yes/no" />
 #
@@ -563,7 +726,7 @@ sub start_functionplotrule {
                                    ['lt','less than'],
                                    ['le','less than or equal']],$token).
              $result.= &Apache::edit::select_or_text_arg('Value:','value',
-                                               [['undef','not defined']],$token,'8').
+                                               [['undef','not defined']],$token,'30').
              &Apache::edit::text_arg('Percent error:','percenterror',
                                      $token,'8').
              &Apache::edit::end_row();
@@ -972,6 +1135,7 @@ sub compare_rel {
 
 sub addlog {
    my ($text)=@_;
+   $text=~s/\'/\\\'/g;
    $Apache::functionplotresponse::ruleslog.=$text.'<br />';
 }
 
@@ -979,15 +1143,56 @@ sub actualval {
    my ($i,$xmin,$xmax)=@_;
    return $xmin+$i/400.*($xmax-$xmin);
 }
+
+sub fpr_val {
+   my ($arg)=@_;
+   return &actualval($Apache::functionplotresponse::functionplotrulelabels{$arg},
+                     $Apache::functionplotresponse::fpr_xmin,
+                     $Apache::functionplotresponse::fpr_xmax);
+}
+
+sub fpr_f {
+   my ($arg)=@_;
+   return $Apache::functionplotresponse::func[&array_index($Apache::functionplotresponse::fpr_xmin,
+                                                           $Apache::functionplotresponse::fpr_xmax,
+                                                           $arg)];
+}
+
+sub fpr_dfdx {
+   my ($arg)=@_;
+   return $Apache::functionplotresponse::dfuncdx[&array_index($Apache::functionplotresponse::fpr_xmin,
+                                                              $Apache::functionplotresponse::fpr_xmax,
+                                                              $arg)];
+}
+
+sub fpr_d2fdx2 {
+   my ($arg)=@_;
+   return $Apache::functionplotresponse::d2funcdx2[&array_index($Apache::functionplotresponse::fpr_xmin,
+                                                                $Apache::functionplotresponse::fpr_xmax,
+                                                                $arg)];
+}
  
 sub functionplotrulecheck {
-   my ($rule,$xmin,$xmax,$ymin,$ymax)=@_;
+   my ($rule,$xmin,$xmax,$ymin,$ymax,$safeeval)=@_;
 
    my ($label,$derivative,$xinitial,$xinitiallabel,$xfinal,$xfinallabel,$minimumlength,$maximumlength,$relationship,$value,$percent)
       =split(/\:/,$rule);
    $percent=($percent>0?$percent:5);
    &addlog("=================");
    &addlog("Rule $label for ".($derivative<0?'integral':('function itself','first derivative','second derivative')[$derivative])." $relationship $value");
+#
+# Evaluate the value
+#
+   if ($value=~/\D/) {
+      $Apache::functionplotresponse::fpr_xmin=$xmin;
+      $Apache::functionplotresponse::fpr_xmax=$xmax;
+      $value=&Apache::run::run($value,$safeeval);
+      &addlog("Value evaluated to $value");
+   }
+
+#
+# Minimum and maximum lengths of the interval
+#
    if ((defined($minimumlength)) || (defined($maximumlength))) {
       &addlog("Minimumlength $minimumlength Maximumlength $maximumlength");
    }
@@ -1186,7 +1391,7 @@ sub end_functionplotruleset {
 
 # We have a function that we can actually grade, go through the spline rules.
            foreach my $rule (@Apache::functionplotresponse::functionplotrules) {
-              unless (&functionplotrulecheck($rule,$xmin,$xmax,$ymin,$ymax)) {
+              unless (&functionplotrulecheck($rule,$xmin,$xmax,$ymin,$ymax,$safeeval)) {
                  $ad='INCORRECT';
                  last;
               }