'.&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()
@@ -836,9 +1245,8 @@ 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,...):',
- 'answerdisplay',$token,'50');
-
+ &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,
@@ -923,6 +1331,7 @@ sub compare_rel {
sub addlog {
my ($text)=@_;
+ $text=~s/\'/\\\'/g;
$Apache::functionplotresponse::ruleslog.=$text.' ';
}
@@ -930,15 +1339,167 @@ 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 vectorcoords {
+ my ($id,$label)=@_;
+ return ($env{'form.HWVAL_'.$id.'_'.$label.'Start_x'},
+ $env{'form.HWVAL_'.$id.'_'.$label.'End_x'},
+ $env{'form.HWVAL_'.$id.'_'.$label.'Start_y'},
+ $env{'form.HWVAL_'.$id.'_'.$label.'End_y'});
+}
+
+sub objectcoords {
+ my ($id,$label)=@_;
+ return ($env{'form.HWVAL_'.$id.'_'.$label.'_x'},
+ $env{'form.HWVAL_'.$id.'_'.$label.'_y'});
+}
+
+sub vectorangle {
+ my ($x,$y)=@_;
+ my $angle=57.2957795*atan2($y,$x);
+ if ($angle<0) { $angle=360+$angle; }
+ return $angle;
+}
+
+sub vectorlength {
+ my ($x,$y)=@_;
+ return sqrt($x*$x+$y*$y);
+}
+
+sub relvector {
+ my ($xs,$xe,$ys,$ye)=@_;
+ return ($xe-$xs,$ye-$ys);
+}
+
+sub plotvectorlength {
+ return &vectorlength(&relvector(&vectorcoords(@_)));
+}
+
+sub plotvectorangle {
+ return &vectorangle(&relvector(&vectorcoords(@_)));
+}
+
+
+#
+# Evaluate a functionplotvectorrule
+#
+
+sub functionplotvectorrulecheck {
+ my ($rule,$xmin,$xmax,$ymin,$ymax,$safeeval)=@_;
+ &addlog("=================");
+ my ($label,$type)=split(/\:/,$rule);
+ if ($type eq 'vector') {
+ return &vectorcheck($rule,$xmin,$xmax,$ymin,$ymax,$safeeval);
+ } elsif ($type eq 'sum') {
+ return &sumcheck($rule,$xmin,$xmax,$ymin,$ymax,$safeeval);
+ }
+}
+
+sub vectorcheck {
+ my ($rule,$xmin,$xmax,$ymin,$ymax,$safeeval)=@_;
+ my ($label,$type,$id,$vector,
+ $attachpoint,$notattachpoint,
+ $tailpoint,$tippoint,$nottailpoint,$nottippoint,
+ $length,$angle,$lengtherror,$angleerror)=split(/\:/,$rule);
+ &addlog("Vector Rule $label for vector ".$vector);
+ if ($length ne '') {
+ &addlog("Checking for length $length with error $lengtherror");
+ $length=&Apache::run::run($length,$safeeval);
+ &addlog("Length evaluated to $length");
+ my $thislength=&plotvectorlength($id,$vector);
+ &addlog("Found length $thislength");
+ if (abs($thislength-$length)>$lengtherror) {
+ &setfailed($label);
+ return 0;
+ }
+ }
+ if ($angle ne '') {
+ &addlog("Checking for angle $angle with error $angleerror");
+ $angle=&Apache::run::run($angle,$safeeval);
+ &addlog("Angle evaluated to $angle");
+ my $thisangle=&plotvectorangle($id,$vector);
+ &addlog("Found angle $thisangle");
+ my $anglediff=abs($thisangle-$angle);
+ &addlog("Angle difference: $anglediff");
+ if ($anglediff>360.-$anglediff) {
+ $anglediff=360.-$anglediff;
+ }
+ &addlog("Smallest angle difference: $anglediff");
+ if ($anglediff>$angleerror) {
+ &setfailed($label);
+ return 0;
+ }
+ }
+ &addlog("Rule $label passed.");
+ return 1;
+}
+
+sub sumcheck {
+ my ($rule,$xmin,$xmax,$ymin,$ymax,$safeeval)=@_;
+ my ($label,$type,$id,$object,$length,$angle,$lengtherror,$angleerror)=split(/\:/,$rule);
+ &addlog("Vector Sum Rule $label for vectors attached to ".$object);
+
+ &addlog("Rule $label passed.");
+ return 1;
+}
+
+#
+# Evaluate a functionplotrule
+#
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 ".($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;
@@ -1010,10 +1571,7 @@ sub functionplotrulecheck {
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");
+ &setfailed($label);
return 0;
}
} else {
@@ -1033,38 +1591,58 @@ 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 ($hintlabel)=@_;
+ $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') {
@@ -1072,7 +1650,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()
@@ -1102,7 +1681,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 ".
@@ -1112,7 +1691,14 @@ 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;
+ }
+ }
+# And now go through the vector rules
+ foreach my $rule (@Apache::functionplotresponse::functionplotvectorrules) {
+ unless (&functionplotvectorrulecheck($rule,$xmin,$xmax,$ymin,$ymax,$safeeval)) {
$ad='INCORRECT';
last;
}
@@ -1183,17 +1769,25 @@ 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);
+ }
}
}
}
-
+ 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();
@@ -1249,7 +1843,8 @@ sub start_functionplotelements {
' |