[LON-CAPA-cvs] cvs: loncom /homework default_homework.lcpm

damieng damieng at source.lon-capa.org
Tue Feb 7 16:24:30 EST 2017


damieng		Tue Feb  7 21:24:30 2017 EDT

  Modified files:              
    /loncom/homework	default_homework.lcpm 
  Log:
  fixed bug 6844
  
Index: loncom/homework/default_homework.lcpm
diff -u loncom/homework/default_homework.lcpm:1.172 loncom/homework/default_homework.lcpm:1.173
--- loncom/homework/default_homework.lcpm:1.172	Wed Jan  4 19:54:47 2017
+++ loncom/homework/default_homework.lcpm	Tue Feb  7 21:24:29 2017
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA 
 # used by lonxml::xmlparse() as input variable $safeinit to Apache::run::run()
 #
-# $Id: default_homework.lcpm,v 1.172 2017/01/04 19:54:47 damieng Exp $
+# $Id: default_homework.lcpm,v 1.173 2017/02/07 21:24:29 damieng Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -309,10 +309,13 @@
 	#for numerical problems split off the unit
         my $part1;
         my $part2;
+        my $match_algebra = qr{^(.*[^\s])\s+([^\s]+)$};
+        # FIXME: with the above regexp, algebra with spaces will not be evaluated correctly
+        my $match_numerical_units = qr{^([\d\.\,\s\$]*(?:(?:[xX\*]10[\^\*]*|[eE]*)[\+\-]*\d*)*(?:^|\S)\d+)([\$\s\w\^\*\/\(\)\+\-]*[^\d\.\s\,][\$\s\w\^\*\/\(\)\+\-]*)$};
         if ($allowalgebra) {
-           ($part1,$part2)=($responses->[0][-1]=~ /^(.*[^\s])\s+([^\s]+)$/); 
+           ($part1,$part2) = ($responses->[0][-1] =~ /$match_algebra/); 
         } else {
-           ($part1,$part2)=($responses->[0][-1]=~ /^([\d\.\,\s\$]*(?:(?:[xX\*]10[\^\*]*|[eE]*)[\+\-]*\d*)*(?:^|\S)\d+)([\$\s\w\^\*\/\(\)\+\-]*[^\d\.\s\,][\$\s\w\^\*\/\(\)\+\-]*)$/);
+           ($part1,$part2) = ($responses->[0][-1] =~ /$match_numerical_units/);
         }
         if (defined($part1) && defined($part2)) {
 	    $responses->[0][-1]=$part1;
@@ -327,6 +330,27 @@
                     }
                 }
             }
+            if (scalar(@$responses) > 0 && defined $answerunit) {
+                # there are several response values, units should be the same for all
+                $part2 =~ s/^\s+|\s+$//g;
+                for (my $i=1; $i<scalar(@$responses); $i++) {
+                    my $element = $responses->[$i][-1];
+                    my ($part1i, $part2i);
+                    if ($allowalgebra) {
+                        ($part1i, $part2i) = ($element =~ /$match_algebra/); 
+                    } else {
+                        ($part1i, $part2i) = ($element =~ /$match_numerical_units/);
+                    }
+                    $part2i =~ s/^\s+|\s+$//g;
+                    if (!defined $part2i) {
+                        return 'NO_UNIT';
+                    } elsif ($part2i ne $part2) {
+                        return ('UNIT_FAIL', "$part2 $part2i");
+                    } else {
+                        $responses->[$i][-1] = $part1i;
+                    }
+                }
+            }
 	}
     }
     $unit=~s/\s//;




More information about the LON-CAPA-cvs mailing list