'.&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()
@@ -795,12 +1048,14 @@ 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(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();
} 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') {
@@ -880,6 +1135,7 @@ sub compare_rel {
sub addlog {
my ($text)=@_;
+ $text=~s/\'/\\\'/g;
$Apache::functionplotresponse::ruleslog.=$text.' ';
}
@@ -887,15 +1143,59 @@ 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 ".('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");
+#
+# 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");
+ }
my $li=0;
my $lh=400;
@@ -952,14 +1252,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];
}
@@ -967,37 +1290,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') {
@@ -1005,7 +1350,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,7 +1381,7 @@ sub end_functionplotruleset {
$Apache::functionplotresponse::ruleslog='';
$Apache::functionplotresponse::functionplotrulelabels{'start'}=400;
$Apache::functionplotresponse::functionplotrulelabels{'end'}=0;
- if (&populate_arrays($internalid,$xmin,$xmax) eq 'no_func') {
+ if (&populate_arrays($internalid,$xmin,$xmax,$ymin,$ymax) eq 'no_func') {
$ad='NOT_FUNCTION';
} else {
&addlog("Start of function ".&actualval($Apache::functionplotresponse::functionplotrulelabels{'start'},$xmin,$xmax)." (index ".
@@ -1045,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;
}
@@ -1107,10 +1453,34 @@ 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 @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);
+ }
+ }
+ }
+ }
+ 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();
@@ -1166,7 +1536,8 @@ 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()
@@ -1187,7 +1558,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;
|