- my $xmin=&Apache::lonxml::get_param('xmin',$parstack,$safeeval);
- $xmin=(defined($xmin)?$xmin:-10);
- my $xmax=&Apache::lonxml::get_param('xmax',$parstack,$safeeval);
- $xmax=(defined($xmax)?$xmax:10);
- my $ymin=&Apache::lonxml::get_param('ymin',$parstack,$safeeval);
- $ymin=(defined($ymin)?$ymin:-10);
- my $ymax=&Apache::lonxml::get_param('ymax',$parstack,$safeeval);
- $ymax=(defined($ymax)?$ymax:10);
- if ($xmax<=$xmin) {
- &Apache::lonxml::warning('Maximum x-value needs to be larger than minimum value.');
- $xmax=$xmin+20;
- }
- if ($ymax<=$ymin) {
- &Apache::lonxml::warning('Maximum y-value needs to be larger than minimum value.');
- $ymax=$ymin+20;
- }
+ my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval);
my $xaxisvisible=(&Apache::lonxml::get_param('xaxisvisible',$parstack,$safeeval)=~/on|true|yes|1/i?'true':'false');
my $yaxisvisible=(&Apache::lonxml::get_param('yaxisvisible',$parstack,$safeeval)=~/on|true|yes|1/i?'true':'false');
my $gridvisible=(&Apache::lonxml::get_param('gridvisible',$parstack,$safeeval)=~/on|true|yes|1/i?'true':'false');
@@ -779,7 +816,8 @@ sub start_functionplotresponse {
.''.&mt('Function Plot Question').' | '
.''.&mt('Delete?').' '
.&Apache::edit::deletelist($target,$token).' '
- .&Apache::edit::insertlist($target,$token)
+ .&Apache::edit::insertlist($target,$token).' '
+ .&Apache::loncommon::help_open_topic('Function_Plot_Response_Question','Function Plot Responses')
.' | '
." "
.&Apache::edit::end_row()
@@ -802,12 +840,15 @@ sub start_functionplotresponse {
&Apache::edit::select_arg('y-axis visible:','yaxisvisible',
['yes','no'],$token).' '.
&Apache::edit::select_arg('Grid visible:','gridvisible',
- ['yes','no'],$token).
+ ['yes','no'],$token).' '.
+ &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') {
@@ -902,7 +943,10 @@ sub functionplotrulecheck {
=split(/\:/,$rule);
$percent=($percent>0?$percent:5);
&addlog("=================");
- &addlog("Rule $label for ".('function itself','first derivative','second derivative')[$derivative]." $relationship $value");
+ &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;
@@ -959,14 +1003,37 @@ sub functionplotrulecheck {
$tol=4.*$tol/($xmax-$xmin);
} elsif ($derivative==1) {
$tol=2.*$tol/($xmax-$xmin);
+ } elsif ($derivative==-1) {
+ $tol=$tol*($xmax-$xmin)/2.;
}
}
- for (my $i=$li; $i<=$lh; $i++) {
+ my $integral=0;
+ my $binwidth=($xmax-$xmin)/400.;
+ if (($derivative<0) && (!$findupper)) {
+# definite integral, calculate over whole length
+ &addlog("Calculating definite integral");
+ for (my $i=$li; $i<=$lh; $i++) {
+ $integral+=$Apache::functionplotresponse::func[$i]*$binwidth;
+ }
+ unless (&compare_rel($relationship,$value,$integral,$tol)) {
+ &addlog("Actual integral ".(defined($integral)?$integral:'undef').", expected $value, tolerance $tol");
+ &addlog("Rule $label failed.");
+ my $hintlabel=$label;
+ $hintlabel=~s/^R//;
+ push(@Apache::functionplotresponse::failedrules,$hintlabel);
+ &addlog("Set hint condition $hintlabel");
+ return 0;
+ }
+ } else {
+ for (my $i=$li; $i<=$lh; $i++) {
my $val;
if ($derivative==2) {
$val=$Apache::functionplotresponse::d2funcdx2[$i];
} elsif ($derivative==1) {
$val=$Apache::functionplotresponse::dfuncdx[$i];
+ } elsif ($derivative==-1) {
+ $integral+=$Apache::functionplotresponse::func[$i]*$binwidth;
+ $val=$integral;
} else {
$val=$Apache::functionplotresponse::func[$i];
}
@@ -974,37 +1041,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') {
@@ -1012,7 +1101,8 @@ sub start_functionplotruleset {
' |
'.&mt('Function Plot Rule Set').' | '
.''.&mt('Delete?').' '
.&Apache::edit::deletelist($target,$token).' '.
- &Apache::edit::insertlist($target,$token)
+ &Apache::edit::insertlist($target,$token).' '
+ .&Apache::loncommon::help_open_topic('Function_Plot_Response_Rule_Set','Function Plot Rules')
.' | '
." "
.&Apache::edit::end_row()
@@ -1035,14 +1125,7 @@ sub end_functionplotruleset {
#
# Actually grade
#
- my $xmin=&Apache::lonxml::get_param('xmin',$parstack,$safeeval,-2);
- $xmin=(defined($xmin)?$xmin:-10);
- my $xmax=&Apache::lonxml::get_param('xmax',$parstack,$safeeval,-2);
- $xmax=(defined($xmax)?$xmax:10);
- my $ymin=&Apache::lonxml::get_param('ymin',$parstack,$safeeval,-2);
- $ymin=(defined($ymin)?$ymin:-10);
- my $ymax=&Apache::lonxml::get_param('ymax',$parstack,$safeeval,-2);
- $ymax=(defined($ymax)?$ymax:10);
+ my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-2);
my $ad='';
undef %Apache::functionplotresponse::functionplotrulelabels;
@@ -1121,23 +1204,27 @@ sub end_functionplotelements {
if ($target eq 'edit' ) {
$result=&Apache::edit::end_table();
} elsif ($target eq 'web') {
+ 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
- my $xmin=&Apache::lonxml::get_param('xmin',$parstack,$safeeval,-2);
- $xmin=(defined($xmin)?$xmin:-10);
- my $xmax=&Apache::lonxml::get_param('xmax',$parstack,$safeeval,-2);
- $xmax=(defined($xmax)?$xmax:10);
- my $ymin=&Apache::lonxml::get_param('ymin',$parstack,$safeeval,-2);
- $ymin=(defined($ymin)?$ymin:-10);
- my $ymax=&Apache::lonxml::get_param('ymax',$parstack,$safeeval,-2);
- $ymax=(defined($ymax)?$ymax:10);
- if ($xmax<=$xmin) {
- $xmax=$xmin+20;
- }
- if ($ymax<=$ymin) {
- $ymax=$ymin+20;
- }
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();
@@ -1162,6 +1249,25 @@ sub end_functionplotelements {
return $result;
}
+sub boundaries {
+ my ($parstack,$safeeval,$level)=@_;
+ my $xmin=&Apache::lonxml::get_param('xmin',$parstack,$safeeval,$level);
+ $xmin=(defined($xmin)?$xmin:-10);
+ my $xmax=&Apache::lonxml::get_param('xmax',$parstack,$safeeval,$level);
+ $xmax=(defined($xmax)?$xmax:10);
+ my $ymin=&Apache::lonxml::get_param('ymin',$parstack,$safeeval,$level);
+ $ymin=(defined($ymin)?$ymin:-10);
+ my $ymax=&Apache::lonxml::get_param('ymax',$parstack,$safeeval,$level);
+ $ymax=(defined($ymax)?$ymax:10);
+ if ($xmax<=$xmin) {
+ $xmax=$xmin+20;
+ }
+ if ($ymax<=$ymin) {
+ $ymax=$ymin+20;
+ }
+ return ($xmin,$xmax,$ymin,$ymax);
+}
+
sub start_functionplotelements {
my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
my $result='';
@@ -1174,27 +1280,15 @@ sub start_functionplotelements {
' |
'.&mt('Function Plot Elements').' | '
.''.&mt('Delete?').' '
.&Apache::edit::deletelist($target,$token).' '.
- &Apache::edit::insertlist($target,$token)
+ &Apache::edit::insertlist($target,$token).' '
+ .&Apache::loncommon::help_open_topic('Function_Plot_Response_Elements','Function Plot Elements')
.' | '
." "
.&Apache::edit::end_row()
.&Apache::edit::start_spanning_row()
."\n";
} elsif ($target eq 'web') {
- my $xmin=&Apache::lonxml::get_param('xmin',$parstack,$safeeval,-2);
- $xmin=(defined($xmin)?$xmin:-10);
- my $xmax=&Apache::lonxml::get_param('xmax',$parstack,$safeeval,-2);
- $xmax=(defined($xmax)?$xmax:10);
- my $ymin=&Apache::lonxml::get_param('ymin',$parstack,$safeeval,-2);
- $ymin=(defined($ymin)?$ymin:-10);
- my $ymax=&Apache::lonxml::get_param('ymax',$parstack,$safeeval,-2);
- $ymax=(defined($ymax)?$ymax:10);
- if ($xmax<=$xmin) {
- $xmax=$xmin+20;
- }
- if ($ymax<=$ymin) {
- $ymax=$ymin+20;
- }
+ my ($xmin,$xmax,$ymin,$ymax)=&boundaries($parstack,$safeeval,-2);
my $xaxisvisible=(&Apache::lonxml::get_param('xaxisvisible',$parstack,$safeeval,-2)=~/on|true|yes|1/i?'true':'false');
my $yaxisvisible=(&Apache::lonxml::get_param('yaxisvisible',$parstack,$safeeval,-2)=~/on|true|yes|1/i?'true':'false');
my $gridvisible=(&Apache::lonxml::get_param('gridvisible',$parstack,$safeeval,-2)=~/on|true|yes|1/i?'true':'false');
@@ -1208,7 +1302,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;
|