--- loncom/homework/functionplotresponse.pm	2010/11/20 21:35:53	1.43
+++ loncom/homework/functionplotresponse.pm	2010/12/03 00:52:59	1.49
@@ -1,7 +1,7 @@
 # LearningOnline Network with CAPA
 # option list style responses
 #
-# $Id: functionplotresponse.pm,v 1.43 2010/11/20 21:35:53 www Exp $
+# $Id: functionplotresponse.pm,v 1.49 2010/12/03 00:52:59 www Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -161,9 +161,11 @@ sub update_register {
 # Set a point coordinate variable
 #
 sub set_point_coordinate {
-   my ($id,$variable,$x,$y)=@_;
+   my ($id,$variable,$x,$y,$fixed)=@_;
+   my $mult=($fixed?'a*':'');
    return (<<ENDSETVARIABLE);
-document.ggbApplet_$id.evalCommand("$variable=($x,$y)");
+document.ggbApplet_$id.evalCommand("a=1");
+document.ggbApplet_$id.evalCommand("$variable=$mult($x,$y)");
 document.ggbApplet_$id.setLabelVisible("$variable",false);
 ENDSETVARIABLE
 }
@@ -172,21 +174,23 @@ ENDSETVARIABLE
 # Set a slope coordinate variable
 #
 sub set_slope_coordinate {
-   my ($id,$variable,$xrel,$yrel,$xmin,$xmax,$ymin,$ymax,$pointname)=@_;
+   my ($id,$variable,$xrel,$yrel,$xmin,$xmax,$ymin,$ymax,$pointname,$fixed)=@_;
    my $xvariable=$variable.'x';
    my $yvariable=$variable.'y';
    my $domain=$xmax-$xmin;
    my $range=$ymax-$ymin;
    my $xinterval=$domain/100.;
    my $yinterval=$range/200.;
+   my $mult=($fixed?'a*':'');
    return (<<ENDSETSVARIABLE);
+document.ggbApplet_$id.evalCommand("a=1");
 document.ggbApplet_$id.evalCommand("$xvariable=Slider[0,$domain,$xinterval]");
 document.ggbApplet_$id.setVisible("$xvariable", false);
 document.ggbApplet_$id.evalCommand("$xvariable=$xrel");
 document.ggbApplet_$id.evalCommand("$yvariable=Slider[-$range,$range,$yinterval]");
 document.ggbApplet_$id.setVisible("$yvariable", false);
 document.ggbApplet_$id.evalCommand("$yvariable=$yrel");
-document.ggbApplet_$id.evalCommand("$variable=($xvariable+x($pointname),$yvariable+y($pointname))");
+document.ggbApplet_$id.evalCommand("$variable=$mult($xvariable+x($pointname),$yvariable+y($pointname))");
 document.ggbApplet_$id.setLabelVisible("$variable", false);
 ENDSETSVARIABLE
 }
@@ -215,7 +219,7 @@ sub generate_input_field {
 # Initialize a new point coordinate variable at set a listener on it
 #
 sub new_point_coordinate {
-    my ($id,$variable,$x,$y)=@_;
+    my ($id,$variable,$x,$y,$fixed)=@_;
     if (defined($Apache::functionplotresponse::previous{&field_name($id,$variable,'x')})) {
        $x=$Apache::functionplotresponse::previous{&field_name($id,$variable,'x')};
     }
@@ -223,14 +227,14 @@ sub new_point_coordinate {
        $y=$Apache::functionplotresponse::previous{&field_name($id,$variable,'y')};
     }
     &generate_input_field($id,$variable,$x,$y);
-    return &set_point_coordinate($id,$variable,$x,$y).&update_register($id,$variable);
+    return &set_point_coordinate($id,$variable,$x,$y,$fixed).&update_register($id,$variable);
 }
 
 #
 # Initialize a new slope coordinate variable at set a listener on it
 #
 sub new_slope_coordinate {
-    my ($id,$variable,$x,$y,$pointname,$xp,$yp,$xmin,$xmax,$ymin,$ymax)=@_;
+    my ($id,$variable,$x,$y,$pointname,$xp,$yp,$xmin,$xmax,$ymin,$ymax,$fixed)=@_;
 #
 # $variable: name of the slope point
 # $x, $y: coordinates of the slope point
@@ -253,7 +257,7 @@ sub new_slope_coordinate {
     &generate_input_field($id,$variable,$x,$y);
     my $xrel=$x-$xp;
     my $yrel=$y-$yp;
-    return &set_slope_coordinate($id,$variable,$xrel,$yrel,$xmin,$xmax,$ymin,$ymax,$pointname).&update_register($id,$variable);
+    return &set_slope_coordinate($id,$variable,$xrel,$yrel,$xmin,$xmax,$ymin,$ymax,$pointname,$fixed).&update_register($id,$variable);
 }
 
 #
@@ -295,28 +299,42 @@ ENDAXESSCRIPT
 }
 
 sub axes_label {
-    my ($id,$xlabel,$ylabel)=@_;
+    my ($id,$xmin,$xmax,$ymin,$ymax,$xlabel,$ylabel)=@_;
     unless ($xlabel || $ylabel) { return ''; }
     my $return='document.ggbApplet_'.$id.'.evalCommand("topRight=Corner[3]");';
     if ($xlabel) {
+      if (($ymin<0) && ($ymax>0)) {
        $return.=(<<ENDXAXISLABELSCRIPT);
 document.ggbApplet_$id.evalCommand("Xlabel=(x(topRight)-AxisStepX[],AxisStepY[]/6)");
 document.ggbApplet_$id.setVisible("Xlabel",false);
 document.ggbApplet_$id.evalCommand("Text[\\"$xlabel\\", Xlabel]");
 ENDXAXISLABELSCRIPT
+      } else {
+       $return.=(<<ENDXOFFAXISLABEL);
+document.ggbApplet_$id.evalCommand("LowerRight=Corner[2]");
+document.ggbApplet_$id.evalCommand("Text[\\"$xlabel\\", (x(LowerRight) - AxisStepX[], y(LowerRight) + AxisStepY[] / 2)]");
+ENDXOFFAXISLABEL
+      }
     }
     if ($ylabel) {
+      if (($xmin<0) && ($xmax>0)) {
        $return.=(<<ENDYAXISLABELSCRIPT);
 document.ggbApplet_$id.evalCommand("Ylabel=(AxisStepX[]/6,y(topRight)-AxisStepY[]/3)");
 document.ggbApplet_$id.setVisible("Ylabel",false);
 document.ggbApplet_$id.evalCommand("Text[\\"$ylabel\\", Ylabel]");
 ENDYAXISLABELSCRIPT
+      } else {
+       $return.=(<<ENDYOFFAXISLABEL);
+document.ggbApplet_$id.evalCommand("UpperLeft=Corner[4]");
+document.ggbApplet_$id.evalCommand("Text[\\"$ylabel\\", (x(UpperLeft) + AxisStepX[] / 5, y(UpperLeft) - AxisStepY[] / 1.8)]");
+ENDYOFFAXISLABEL
+      }
     }
     return $return;
 }
 
 sub plot_script {
-   my ($id,$function,$fixed,$label,$xmin,$xmax)=@_;
+   my ($id,$function,$fixed,$label,$color,$xmin,$xmax,$thickness)=@_;
    $label=~s/\W//g;
    if (($label) && ($label!~/^[A-Za-z]/)) {
       $label='C'.$label;
@@ -328,11 +346,22 @@ sub plot_script {
       $Apache::functionplotresponse::counter++;
       $label='C'.$Apache::functionplotresponse::counter;
    }
+   my $rc=0;
+   my $gc=0;
+   my $bc=0;
+   if ($color) {
+      my ($rh,$gh,$bh)=($color=~/(..)(..)(..)/);
+      $rc=hex($rh);
+      $gc=hex($gh);
+      $bc=hex($bh);
+   }
    if ($fixed) {
       return "document.ggbApplet_$id.evalCommand('$label=Function[$function,$xmin,$xmax]');\n".
-             ($visible?'':"document.ggbApplet_$id.setLabelVisible('$label', false);\n");
+             ($visible?'':"document.ggbApplet_$id.setLabelVisible('$label', false);\n").
+             ($color?"document.ggbApplet_$id.setColor('$label',$rc,$gc,$bc);\n":'').
+             ($thickness?"document.ggbApplet_$id.setLineThickness('$label',$thickness);\n":'');
    } else {
-       return "document.ggbApplet_$id.evalCommand('y=$function')";
+       return "document.ggbApplet_$id.evalCommand('y=$function');\n";
    }
 }
 
@@ -341,7 +370,7 @@ sub plot_script {
 #
 
 sub generate_spline {
-   my ($id,$label,$xmin,$xmax,$ymin,$ymax)=@_;
+   my ($id,$label,$xmin,$xmax,$ymin,$ymax,$fixed)=@_;
    my $result='';
    my $order=$Apache::functionplotresponse::splineorder{$label};
    my $x=$Apache::functionplotresponse::splineinitx{$label};
@@ -350,11 +379,11 @@ sub generate_spline {
    my $sy=$Apache::functionplotresponse::splinescaley{$label};
    my @coords=();
    foreach my $i (1..$order) {
-       $result.=&new_point_coordinate($id,$label.'P'.$i,$x,$y);
+       $result.=&new_point_coordinate($id,$label.'P'.$i,$x,$y,$fixed);
        my $xp=$x;
        $x+=$sx/(2.*($order-1));
        push(@coords,$label.'P'.$i);
-       $result.=&new_slope_coordinate($id,$label.'S'.$i,$x,$y+$sy,$label.'P'.$i,$xp,$y,$xmin,$xmax,$ymin,$ymax);
+       $result.=&new_slope_coordinate($id,$label.'S'.$i,$x,$y+$sy,$label.'P'.$i,$xp,$y,$xmin,$xmax,$ymin,$ymax,$fixed);
        $x+=$sx/(2.*($order-1));
        push(@coords,$label.'S'.$i);
    }
@@ -369,25 +398,38 @@ sub start_backgroundplot {
    my $result='';
    my $internalid = $Apache::inputtags::part.'_'.$Apache::inputtags::response[-1];
    my $function=&Apache::lonxml::get_param('function',$parstack,$safeeval);
+   my $xinitial=&Apache::lonxml::get_param('xinitial',$parstack,$safeeval);
+   my $xfinal=&Apache::lonxml::get_param('xfinal',$parstack,$safeeval);
    my $label=&Apache::lonxml::get_param('label',$parstack,$safeeval);
+   my $color=&Apache::lonxml::get_param('color',$parstack,$safeeval);
+   $color=~s/[^a-fA-F0-9]//gs;
+   unless (length($color)==6) { $color=''; }
    my $fixed=(&Apache::lonxml::get_param('fixed',$parstack,$safeeval)=~/on|true|yes|1/i?1:0);
  
    unless ($function) { $function="0"; }
    if ($target eq 'web') {
       my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-3);
-      $result.=&plot_script($internalid,$function,$fixed,$label,$xmin,$xmax);
+      unless (defined($xinitial)) { $xinitial=$xmin; }
+      unless (defined($xfinal)) { $xfinal=$xmax; }
+      $result.=&plot_script($internalid,$function,$fixed,$label,$color,$xinitial,$xfinal);
    } elsif ($target eq 'edit') {
         $result=&Apache::edit::tag_start($target,$token,'Background Function Plot').
              &Apache::edit::text_arg('Function:','function',
                                      $token,'16').
+             &Apache::edit::text_arg('Initial x-value (optional):','xinitial',
+                                     $token,'8').
+             &Apache::edit::text_arg('Final x-value (optional):','xfinal',
+                                     $token,'8').
              &Apache::edit::text_arg('Label on Plot:','label',
                                      $token,'8').
+             &Apache::edit::text_arg('Color (hex code):','color',
+                                     $token,'8').
              &Apache::edit::select_arg('Fixed location:','fixed',
                                   ['yes','no'],$token).
              &Apache::edit::end_row();
   } elsif ($target eq 'modified') {
     my $constructtag=&Apache::edit::get_new_args($token,$parstack,
-                                                 $safeeval,'function','label','fixed');
+                                                 $safeeval,'function','label','xinitial','xfinal','color','fixed');
     if ($constructtag) { $result=&Apache::edit::rebuild_tag($token); }
   }
   return $result;
@@ -467,18 +509,19 @@ sub start_functionplotrule {
                                    ['1','First derivative'],
                                    ['2','Second derivative'],
                                    ['-1','Integral']],$token).'<br />'.
-             &Apache::edit::text_arg('(Initial) x-value:','xinitial',
+             &Apache::edit::text_arg('Initial x-value:','xinitial',
                                       $token,'8').
-             &Apache::edit::select_or_text_arg('(Initial) x-value label:','xinitiallabel',
-                                               [['start','Start of Plot']],$token,'8').'<br />'.
+             &Apache::edit::select_or_text_arg('Initial x-value label:','xinitiallabel',
+                                               [['start','Start of Plot'],
+                                                ['end','End of Plot']],$token,'8').'<br />'.
 
-             &Apache::edit::text_arg('Optional final x-value for ranges:','xfinal',
+             &Apache::edit::text_arg('Final x-value (optional):','xfinal',
                                       $token,'8').
-             &Apache::edit::select_or_text_arg('Optional final x-value label:','xfinallabel',
+             &Apache::edit::select_or_text_arg('Final x-value label (optional):','xfinallabel',
                                                [['end','End of Plot']],$token,'8').'<br />'.
-             &Apache::edit::text_arg('Optional minimum length for range:','minimumlength',
+             &Apache::edit::text_arg('Minimum length for range (optional):','minimumlength',
                                      $token,'8').
-             &Apache::edit::text_arg('Optional maximum length for range:','maximumlength',
+             &Apache::edit::text_arg('Maximum length for range (optional):','maximumlength',
                                      $token,'8').'<br />'.
              &Apache::edit::select_or_text_arg(&mt('Relationship:'),'relationship',
                                   [['eq','equal'],
@@ -562,7 +605,7 @@ sub start_spline {
              &Apache::edit::text_arg('Index:','index',
                                      $token,'4').'&nbsp;'.
              &Apache::edit::select_arg('Order:','order',
-                                  ['2','3','4','5','6','7','8','9'],$token).'&nbsp;'.
+                                  ['2','3','4','5','6','7','8'],$token).'&nbsp;'.
              &Apache::edit::text_arg('Initial x-value:','initx',
                                      $token,'4').'&nbsp;'.
              &Apache::edit::text_arg('Initial y-value:','inity',
@@ -796,12 +839,15 @@ sub start_functionplotresponse {
              &Apache::edit::select_arg('y-axis visible:','yaxisvisible',
                                   ['yes','no'],$token).'<br />'.
              &Apache::edit::select_arg('Grid visible:','gridvisible',
-                                  ['yes','no'],$token).
+                                  ['yes','no'],$token).'<br />'.
+             &Apache::edit::text_arg('Background plot(s) for answer (function:xmin:xmax,function:xmin:xmax,...):',
+                                         'answerdisplay',$token,'50');
+
              &Apache::edit::end_row().&Apache::edit::start_spanning_row();
   } elsif ($target eq 'modified') {
     my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                                                  $safeeval,'xlabel','xmin','xmax','ylabel','ymin','ymax',
-                                                           'xaxisvisible','yaxisvisible','gridvisible');
+                                                           'xaxisvisible','yaxisvisible','gridvisible','answerdisplay');
     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
 
   } elsif ($target eq 'meta') {
@@ -897,6 +943,9 @@ sub functionplotrulecheck {
    $percent=($percent>0?$percent:5);
    &addlog("=================");
    &addlog("Rule $label for ".($derivative<0?'integral':('function itself','first derivative','second derivative')[$derivative])." $relationship $value");
+   if ((defined($minimumlength)) || (defined($maximumlength))) {
+      &addlog("Minimumlength $minimumlength Maximumlength $maximumlength");
+   }
    my $li=0;
    my $lh=400;
 
@@ -991,38 +1040,59 @@ sub functionplotrulecheck {
            &addlog("Actual value ".(defined($val)?$val:'undef').", expected $value, tolerance $tol");
            &addlog("Condition not fulfilled at x=".&actualval($i,$xmin,$xmax)." (".$Apache::functionplotresponse::actualxval[$i]."; index $i)");
            if (($findupper) && ($i>$li)) {
-# check for minimum and maximum lengths
-              my $length=&actualval($i,$xmin,$xmax)-&actualval($li,$xmin,$xmax);
-              if ($minimumlength) {
-                 if ($length<$minimumlength) {
-                    &addlog("Rule $label failed, actual length $length, minimum length $minimumlength");
-                    return 0;
-                 }
-              }
-              if ($maximumlength) {
-                 if ($length>$maximumlength) {
-                    &addlog("Rule $label failed, actual length $length, maximum length $maximumlength");
-                    return 0;
-                 }
-              }
+# Check lengths
+              unless (&checklength($i,$li,$minimumlength,$maximumlength,$xmin,$xmax,$label)) { return 0; }
+# Successfully found a new label, set it
               $Apache::functionplotresponse::functionplotrulelabels{$xfinallabel}=$i;
               &addlog("Rule $label passed, setting label $xfinallabel");
               return 1;
            } else {
               &addlog("Rule $label failed.");
-              my $hintlabel=$label;
-              $hintlabel=~s/^R//;
-              push(@Apache::functionplotresponse::failedrules,$hintlabel);
-              &addlog("Set hint condition $hintlabel");
+              &setfailed($label);
               return 0; 
            }
         }
      }
    }
+# Corner case where this makes sense: using start or stop as defined labels
+   unless (&checklength($lh,$li,$minimumlength,$maximumlength,$xmin,$xmax,$label)) { return 0; }
    &addlog("Rule $label passed.");
    return 1;
 }
 
+#
+# check for minimum and maximum lengths
+#
+
+sub checklength {
+    my ($i,$li,$minimumlength,$maximumlength,$xmin,$xmax,$label)=@_;
+    unless (($minimumlength) || ($maximumlength)) { return 1; }
+    my $length=&actualval($i,$xmin,$xmax)-&actualval($li,$xmin,$xmax);
+    if ($minimumlength) {
+       if ($length<$minimumlength) {
+          &addlog("Rule $label failed, actual length $length, minimum length $minimumlength");
+          &setfailed($label);
+          return 0;
+       }
+    }
+    if ($maximumlength) {
+       if ($length>$maximumlength) {
+          &addlog("Rule $label failed, actual length $length, maximum length $maximumlength");
+          &setfailed($label);
+          return 0;
+       }
+    }
+    return 1;
+}
+
+sub setfailed {
+   my ($label)=@_;
+   my $hintlabel=$label;
+   $hintlabel=~s/^R//;
+   push(@Apache::functionplotresponse::failedrules,$hintlabel);
+   &addlog("Set hint condition $hintlabel");
+}
+
 sub start_functionplotruleset {
    my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
    if ($target eq 'edit') {
@@ -1132,10 +1202,27 @@ sub end_functionplotelements {
   if ($target eq 'edit' ) {
      $result=&Apache::edit::end_table();
   } elsif ($target eq 'web') {
-# Now is the time to render all of the stored splines
      my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-2);
+
+# Are we in show answer mode?
+     my $showanswer=&Apache::response::show_answer();
+     if ($showanswer) {
+# Render answerdisplay
+        my $answerdisplay=&Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval,-2);
+        if ($answerdisplay=~/\S/s) {
+           foreach my $plot (split(/\s*\,\s*/,$answerdisplay)) {
+              my ($func,$xl,$xh)=split(/\s*\:\s*/,$plot);
+              if ((!defined($xl)) || ($xl eq '')) { $xl=$xmin; }
+              if ((!defined($xh)) || ($xh eq '')) { $xh=$xmax; }
+              $result.=&plot_script($internalid,$func,1,'','00aa00',$xl,$xh,6);
+           }
+        }
+     }
+     my $fixed=0;
+     if (($showanswer) || (&Apache::response::check_status()>=2)) { $fixed=1; }
+# Now is the time to render all of the stored splines
      foreach my $label (keys(%Apache::functionplotresponse::splineorder)) {
-        $result.=&generate_spline($internalid,$label,$xmin,$xmax,$ymin,$ymax);
+        $result.=&generate_spline($internalid,$label,$xmin,$xmax,$ymin,$ymax,$fixed);
      }
 # close the init script
      $result.=&end_init_script();
@@ -1212,7 +1299,7 @@ sub start_functionplotelements {
      $result.=&start_init_script($internalid);
 # put the axis commands inside
      $result.=&axes_script($internalid,$xmin,$xmax,$ymin,$ymax,$xaxisvisible,$yaxisvisible,$gridvisible);
-     $result.=&axes_label($internalid,$xlabel,$ylabel);
+     $result.=&axes_label($internalid,$xmin,$xmax,$ymin,$ymax,$xlabel,$ylabel);
 # init script is left open
   }
   return $result;