[LON-CAPA-cvs] cvs: loncom /homework functionplotresponse.pm

www www@source.lon-capa.org
Fri, 03 Dec 2010 00:52:59 -0000


www		Fri Dec  3 00:52:59 2010 EDT

  Modified files:              
    /loncom/homework	functionplotresponse.pm 
  Log:
  Bug #6401: Check for minimum and maximum length in corner case of borders being start and end
  
  
Index: loncom/homework/functionplotresponse.pm
diff -u loncom/homework/functionplotresponse.pm:1.48 loncom/homework/functionplotresponse.pm:1.49
--- loncom/homework/functionplotresponse.pm:1.48	Wed Nov 24 15:30:45 2010
+++ loncom/homework/functionplotresponse.pm	Fri Dec  3 00:52:59 2010
@@ -1,7 +1,7 @@
 # LearningOnline Network with CAPA
 # option list style responses
 #
-# $Id: functionplotresponse.pm,v 1.48 2010/11/24 15:30:45 www Exp $
+# $Id: functionplotresponse.pm,v 1.49 2010/12/03 00:52:59 www Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -943,6 +943,9 @@
    $percent=($percent>0?$percent:5);
    &addlog("=================");
    &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;
 
@@ -1037,38 +1040,59 @@
            &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') {