--- loncom/homework/functionplotresponse.pm	2010/11/23 23:14:20	1.45
+++ loncom/homework/functionplotresponse.pm	2011/01/28 16:06:49	1.53
@@ -1,7 +1,7 @@
 # LearningOnline Network with CAPA
 # option list style responses
 #
-# $Id: functionplotresponse.pm,v 1.45 2010/11/23 23:14:20 www Exp $
+# $Id: functionplotresponse.pm,v 1.53 2011/01/28 16:06:49 www Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -108,7 +108,7 @@ sub init_script {
           $script.="if (param=='applet_$id') { loaded_$id=true; }\n";
       }
       $script.="if (".join(' && ',map { "loaded_$_" } (@Apache::functionplotresponse::callscripts)).
-               ") { setTimeout('ggbInitAll()',20) }";
+               ") { setTimeout('ggbInitAll()',200) }";
       my $calls=join("\n",map { "ggbInit_$_();" } (@Apache::functionplotresponse::callscripts)); 
       return (<<ENDGGBINIT);
 <script type="text/javascript">
@@ -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("$xvariable=Slider[0,$domain,$xinterval]");
+document.ggbApplet_$id.evalCommand("a=1");
+document.ggbApplet_$id.evalCommand("$xvariable=Slider[$xinterval,$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);
 }
 
 #
@@ -330,7 +334,7 @@ ENDYOFFAXISLABEL
 }
 
 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;
@@ -342,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";
    }
 }
 
@@ -355,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};
@@ -364,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);
    }
@@ -383,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;
@@ -508,6 +536,12 @@ sub start_functionplotrule {
                                      $token,'8').
              &Apache::edit::end_row();
   } elsif ($target eq 'modified') {
+    if (($env{'form.'.&Apache::edit::html_element_name('xinitial')} ne '') & ($env{'form.'.&Apache::edit::html_element_name('xinitiallabel')} eq 'start')) {
+       $env{'form.'.&Apache::edit::html_element_name('xinitiallabel')}='';
+    }
+    if (($env{'form.'.&Apache::edit::html_element_name('xfinal')} ne '') & ($env{'form.'.&Apache::edit::html_element_name('xfinallabel')} eq 'end')) {
+       $env{'form.'.&Apache::edit::html_element_name('xfinallabel')}='';
+    }
     my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                                                  $safeeval,'index','derivativeorder',
                                                            'xinitial','xinitiallabel','xfinal','xfinallabel',
@@ -788,7 +822,8 @@ sub start_functionplotresponse {
        .'<tr><td><span class="LC_nobreak">'.&mt('Function Plot Question').'</span></td>'
        .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
        .&Apache::edit::deletelist($target,$token).'&nbsp;&nbsp;&nbsp;'
-       .&Apache::edit::insertlist($target,$token)
+       .&Apache::edit::insertlist($target,$token).'&nbsp;&nbsp;&nbsp;'
+       .&Apache::loncommon::help_open_topic('Function_Plot_Response_Question','Function Plot Responses')
        .'</span></td>'
        ."<td>&nbsp;"
        .&Apache::edit::end_row()
@@ -811,12 +846,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') {
@@ -912,6 +950,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;
 
@@ -1006,38 +1047,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') {
@@ -1045,7 +1107,8 @@ sub start_functionplotruleset {
         '<tr><td><span class="LC_nobreak">'.&mt('Function Plot Rule Set').'</span></td>'
        .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
        .&Apache::edit::deletelist($target,$token).'&nbsp;&nbsp;&nbsp;'.
-        &Apache::edit::insertlist($target,$token)
+        &Apache::edit::insertlist($target,$token).'&nbsp;&nbsp;&nbsp;'
+       .&Apache::loncommon::help_open_topic('Function_Plot_Response_Rule_Set','Function Plot Rules')
        .'</span></td>'
        ."<td>&nbsp;"
        .&Apache::edit::end_row()
@@ -1147,10 +1210,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();
@@ -1206,7 +1286,8 @@ sub start_functionplotelements {
         '<tr><td><span class="LC_nobreak">'.&mt('Function Plot Elements').'</span></td>'
        .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
        .&Apache::edit::deletelist($target,$token).'&nbsp;&nbsp;&nbsp;'.
-        &Apache::edit::insertlist($target,$token)
+        &Apache::edit::insertlist($target,$token).'&nbsp;&nbsp;&nbsp;'
+       .&Apache::loncommon::help_open_topic('Function_Plot_Response_Elements','Function Plot Elements')
        .'</span></td>'
        ."<td>&nbsp;"
        .&Apache::edit::end_row()