'.&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 +1266,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 +1352,7 @@ sub compare_rel {
sub addlog {
my ($text)=@_;
+ $text=~s/\'/\\\'/g;
$Apache::functionplotresponse::ruleslog.=$text.' ';
}
@@ -930,15 +1360,331 @@ 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 fpr_vectorcoords {
+ my ($arg)=@_;
+ $arg=~s/\W//gs;
+ $arg=ucfirst($arg);
+ my $id=$Apache::inputtags::response[-1];
+ my $partid=$Apache::inputtags::part;
+ my $internalid = $partid.'_'.$id;
+ return ($env{'form.HWVAL_'.$internalid.'_'.$arg.'Start_x'},
+ $env{'form.HWVAL_'.$internalid.'_'.$arg.'End_x'},
+ $env{'form.HWVAL_'.$internalid.'_'.$arg.'Start_y'},
+ $env{'form.HWVAL_'.$internalid.'_'.$arg.'End_y'});
+}
+
+sub fpr_objectcoords {
+ my ($arg)=@_;
+ $arg=~s/\W//gs;
+ $arg=ucfirst($arg);
+ my $id=$Apache::inputtags::response[-1];
+ my $partid=$Apache::inputtags::part;
+ my $internalid = $partid.'_'.$id;
+ return ($env{'form.HWVAL_'.$internalid.'_'.$arg.'_x'},
+ $env{'form.HWVAL_'.$internalid.'_'.$arg.'_y'});
+}
+
+sub fpr_vectorlength {
+ my ($arg)=@_;
+ my ($xs,$xe,$ys,$ye)=&fpr_vectorcoords($arg);
+ return sqrt(($xe-$xs)*($xe-$xs)+($ye-$ys)*($ye-$ys));
+}
+
+sub fpr_vectorangle {
+ my ($arg)=@_;
+ my ($xs,$xe,$ys,$ye)=&fpr_vectorcoords($arg);
+ my $angle=57.2957795*atan2(($ye-$ys),($xe-$xs));
+ if ($angle<0) { $angle=360+$angle; }
+ return $angle;
+}
+
+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 attached {
+ my ($id,$vector,$objects,$xmin,$xmax,$ymin,$ymax)=@_;
+ my ($xs,$xe,$ys,$ye)=&vectorcoords($id,$vector);
+ my $tolx=($xmax-$xmin)/100.;
+ my $toly=($ymax-$ymin)/100.;
+ my $tail=0;
+ my $tip=0;
+ foreach my $obj (split(/\s*\,\s*/,$objects)) {
+ $obj=~s/\W//g;
+ unless ($obj) { next; }
+ $obj=ucfirst($obj);
+ my ($xo,$yo)=&objectcoords($id,$obj);
+ &addlog("Proximity $vector ($xs,$ys)-($xe,$ye) to $obj ($xo,$yo)");
+ if ((abs($xs-$xo)<$tolx) && (abs($ys-$yo)<$toly)) {
+ $tail=1;
+ &addlog("Attached tail: $obj");
+ }
+ if ((abs($xe-$xo)<$tolx) && (abs($ye-$yo)<$toly)) {
+ $tip=1;
+ &addlog("Attached tip: $obj");
+ }
+ }
+ &addlog("Result tail:$tail tip:$tip");
+ return($tail,$tip);
+}
+
+
+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);
+ } elsif ($type eq 'custom') {
+ return &customcheck($rule,$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;
+ }
+ }
+ if ($attachpoint ne '') {
+ &addlog("Checking attached: ".$attachpoint);
+ my ($tail,$tip)=&attached($id,$vector,$attachpoint,$xmin,$xmax,$ymin,$ymax);
+ unless ($tail || $tip) {
+ &setfailed($label);
+ return 0;
+ }
+ }
+ if ($notattachpoint ne '') {
+ &addlog("Checking not attached: ".$notattachpoint);
+ my ($tail,$tip)=&attached($id,$vector,$notattachpoint,$xmin,$xmax,$ymin,$ymax);
+ if ($tail || $tip) {
+ &setfailed($label);
+ return 0;
+ }
+ }
+ if ($tailpoint ne '') {
+ &addlog("Checking tail: ".$tailpoint);
+ my ($tail,$tip)=&attached($id,$vector,$tailpoint,$xmin,$xmax,$ymin,$ymax);
+ unless ($tail) {
+ &setfailed($label);
+ return 0;
+ }
+ }
+ if ($nottailpoint ne '') {
+ &addlog("Checking not tail: ".$nottailpoint);
+ my ($tail,$tip)=&attached($id,$vector,$nottailpoint,$xmin,$xmax,$ymin,$ymax);
+ if ($tail) {
+ &setfailed($label);
+ return 0;
+ }
+ }
+ if ($tippoint ne '') {
+ &addlog("Checking tip: ".$tippoint);
+ my ($tail,$tip)=&attached($id,$vector,$tippoint,$xmin,$xmax,$ymin,$ymax);
+ unless ($tip) {
+ &setfailed($label);
+ return 0;
+ }
+ }
+ if ($nottippoint ne '') {
+ &addlog("Checking not tip: ".$nottippoint);
+ my ($tail,$tip)=&attached($id,$vector,$nottippoint,$xmin,$xmax,$ymin,$ymax);
+ if ($tip) {
+ &setfailed($label);
+ return 0;
+ }
+ }
+
+ &addlog("Rule $label passed.");
+ return 1;
+}
+
+sub sumcheck {
+ my ($rule,$xmin,$xmax,$ymin,$ymax,$safeeval)=@_;
+ my ($label,$type,$id,$vectors,$length,$angle,$lengtherror,$angleerror)=split(/\:/,$rule);
+ &addlog("Vector Sum Rule $label for vectors ".$vectors);
+ my $sumx=0;
+ my $sumy=0;
+ foreach my $sv (split(/\s*\,\s*/,$vectors)) {
+ my ($rx,$ry)=&relvector(&vectorcoords($id,$sv));
+ $sumx+=$rx;
+ $sumy+=$ry;
+ }
+ &addlog("Sum vector ($sumx,$sumy)");
+ if ($length ne '') {
+ &addlog("Checking length $length with error $lengtherror");
+ $length=&Apache::run::run($length,$safeeval);
+ &addlog("Evaluated to $length");
+ my $thislength=&vectorlength($sumx,$sumy);
+ &addlog("Actual length $thislength");
+ if (abs($length-$thislength)>$lengtherror) {
+ &setfailed($label);
+ return 0;
+ }
+ }
+ if ($angle ne '') {
+ &addlog("Checking angle $angle with error $angleerror");
+ $angle=&Apache::run::run($angle,$safeeval);
+ &addlog("Evaluated to $angle");
+ my $thisangle=&vectorangle($sumx,$sumy);
+ &addlog("Actual 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 customcheck {
+ my ($rule,$safeeval)=@_;
+ my ($label,$type,$prg)=split(/\:/,$rule);
+ &addlog("Custom Rule ".$label);
+ my $result=&Apache::run::run(&unescape($prg),$safeeval);
+ &addlog("Algorithm returned $result");
+ unless ($result) {
+ &setfailed($label);
+ return 0;
+ }
+ &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 +1756,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 +1776,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 +1835,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 +1866,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 +1876,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 +1954,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 +2028,8 @@ sub start_functionplotelements {
' |