--- loncom/homework/functionplotresponse.pm 2011/01/25 20:52:08 1.51
+++ loncom/homework/functionplotresponse.pm 2011/04/04 21:27:30 1.60
@@ -1,7 +1,7 @@
# LearningOnline Network with CAPA
# option list style responses
#
-# $Id: functionplotresponse.pm,v 1.51 2011/01/25 20:52:08 www Exp $
+# $Id: functionplotresponse.pm,v 1.60 2011/04/04 21:27:30 www Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -61,7 +61,7 @@ sub geogebra_endcode {
#
sub geogebra_spline_program {
return (<
+
ENDSPLINEPROGRAM
}
@@ -75,7 +75,7 @@ sub geogebra_default_parameters {
-
+
@@ -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 (<
@@ -333,6 +333,10 @@ ENDYOFFAXISLABEL
return $return;
}
+#
+# Subroutine to produce background and answer plots
+#
+
sub plot_script {
my ($id,$function,$fixed,$label,$color,$xmin,$xmax,$thickness)=@_;
$label=~s/\W//g;
@@ -366,6 +370,34 @@ sub plot_script {
}
#
+# Answer spline display
+#
+# points: x,y,slope_x,slope_y
+
+sub answer_spline_script {
+ my ($id,@points)=@_;
+ my $order=int(($#points+1)/4);
+ if ($order<2) { $order=2; }
+ if ($order>8) { $order=8; }
+ $Apache::functionplotresponse::counter++;
+ my $label='CSpline'.$Apache::functionplotresponse::counter;
+ my $output='document.ggbApplet_'.$id.'.evalCommand("'.$label.'=Spline'.$order.'[';
+ for (my $i=0;$i<=$#points;$i+=4) {
+ $output.="($points[$i],$points[$i+1]),($points[$i+2],$points[$i+3]),";
+ }
+ $output=~s/\,$//;
+ $output.=']");'."\n";
+ for (my $i=2; $i<2*$order; $i+=2) {
+ $output.='document.ggbApplet_'.$id.'.setColor("'.$label.'_'.$i.'",0,170,0);'."\n";
+ }
+ for (my $i=1; $i<2*$order; $i+=2) {
+ $output.='document.ggbApplet_'.$id.'.setVisible("'.$label.'_'.$i.'",false);'."\n";
+ }
+
+ return $output;
+}
+
+#
# Subroutine that generates code for spline $label based on stored information
#
@@ -536,6 +568,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',
@@ -841,7 +879,7 @@ sub start_functionplotresponse {
['yes','no'],$token).'
'.
&Apache::edit::select_arg('Grid visible:','gridvisible',
['yes','no'],$token).'
'.
- &Apache::edit::text_arg('Background plot(s) for answer (function:xmin:xmax,function:xmin:xmax,...):',
+ &Apache::edit::text_arg('Background plot(s) for answer (function(x):xmin:xmax,function(x):xmin:xmax,x1:y1:sx1:sy1:x2:y2:sx2:sy2,...):',
'answerdisplay',$token,'50');
&Apache::edit::end_row().&Apache::edit::start_spanning_row();
@@ -1213,10 +1251,17 @@ sub end_functionplotelements {
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 @components=split(/\s*\:\s*/,$plot);
+ if ($#components<3) {
+# Just a simple plot
+ my ($func,$xl,$xh)=@components;
+ if ((!defined($xl)) || ($xl eq '')) { $xl=$xmin; }
+ if ((!defined($xh)) || ($xh eq '')) { $xh=$xmax; }
+ $result.=&plot_script($internalid,$func,1,'','00aa00',$xl,$xh,6);
+ } else {
+# This is a spline
+ $result.=&answer_spline_script($internalid,@components);
+ }
}
}
}