@@ -92,7 +92,7 @@ sub splitUnits {
92
92
: $aUnit . ' (?:\s*[/* ]\s*' . $aUnit . ' )*' ;
93
93
$unitPattern = $unitPattern . ' (?:\/' . $unitPattern . ' )*' if $parseMathQuill ;
94
94
my $unitSpace = " ($aUnit ) +($aUnit )" ;
95
- my ($num , $units ) = $string =~ m ! ^(.*?(?:[)}\] 0-9a-z]|\d\. ))\s *($unitPattern )\s *$ ! ;
95
+ my ($num , $units ) = $string =~ m ! ^(.*?(?:[)}\] 0-9a-z]|\d\. ))? \s *($unitPattern )\s *$ ! ;
96
96
if ($units ) {
97
97
while ($units =~ s / $unitSpace/ $1 *$2 / ) { }
98
98
$units =~ s / // g ;
@@ -238,20 +238,26 @@ sub unitsPreFilter {
238
238
$ans -> {correct_value }{context }
239
239
&& $ans -> {correct_value }-> context-> flag(' useMathQuill' )
240
240
&& (!defined $ans -> {mathQuillOpts } || $ans -> {mathQuillOpts } !~ / ^\s *disabled\s *$ /i ));
241
+ if (defined ($units ) && $units ne ' ' && $num eq ' ' ) {
242
+ $self -> cmp_Error($ans , " Units must follow a number" );
243
+ $ans -> {unit_error } = $ans -> {ans_message };
244
+ $ans -> {student_ans } = ' ' ;
245
+ return $ans ;
246
+ }
241
247
unless (defined ($num ) && defined ($units ) && $units ne ' ' ) {
242
248
$self -> cmp_Error($ans , " Your answer doesn't look like " . lc ($self -> cmp_class));
243
- $ans -> {error_flag } = ' UNITS_NONE ' ;
249
+ $ans -> {unit_error } = $ans -> { ans_message } ;
244
250
return $ans ;
245
251
}
246
252
if ($units =~ m ! /.*/! ) {
247
253
$self -> cmp_Error($ans , " Your units can only contain one division" );
248
- $ans -> {error_flag } = ' UNITS_DIVISION ' ;
254
+ $ans -> {unit_error } = $ans -> { ans_message } ;
249
255
return $ans ;
250
256
}
251
257
my $ref = { getUnits($units ) };
252
258
if ($ref -> {ERROR }) {
253
259
$self -> cmp_Error($ans , $ref -> {ERROR });
254
- $ans -> {error_flag } = ' UNITS_BAD ' ;
260
+ $ans -> {unit_error } = $ans -> { ans_message } ;
255
261
return $ans ;
256
262
}
257
263
$ans -> {units } = $units ;
@@ -263,6 +269,14 @@ sub cmp_preprocess {
263
269
my $self = shift ;
264
270
my $ans = shift ;
265
271
272
+ if ($ans -> {unit_error }) {
273
+ $ans -> {ans_message } = $ans -> {error_message } = $ans -> {unit_error };
274
+ if ($ans -> {student_ans } eq ' ' ) {
275
+ $ans -> {student_ans } = $ans -> {original_student_ans };
276
+ $ans -> {preview_latex_string } = TeXunits($ans -> {student_ans });
277
+ }
278
+ return ;
279
+ }
266
280
my $units = $ans -> {units };
267
281
return $ans unless $units ;
268
282
$ans -> {student_ans } .= " " . $units ;
@@ -272,8 +286,8 @@ sub cmp_preprocess {
272
286
if (!defined ($ans -> {student_value }) || $self -> checkStudentValue($ans -> {student_value })) {
273
287
$ans -> {student_value } = undef ;
274
288
$ans -> score(0);
275
- $ans -> {error_flag } = ' UNITS_NO_NUMBER' ;
276
289
$self -> cmp_Error($ans , " Units must follow a number" );
290
+ $ans -> {unit_error } = $ans -> {ans_message };
277
291
return ;
278
292
}
279
293
@@ -284,7 +298,7 @@ sub cmp_preprocess {
284
298
sub cmp_equal {
285
299
my $self = shift ;
286
300
my $ans = shift ;
287
- if (!$ans -> {error_flag }) {
301
+ if (!$ans -> {unit_error }) {
288
302
my $meth = @{ ref ($self ) . ' ::ISA' }[-1] . ' ::cmp_equal' ;
289
303
$meth = ' Value::cmp_equal' unless defined &$meth ;
290
304
&$meth ($self , $ans , @_ );
@@ -298,7 +312,6 @@ sub cmp_postprocess {
298
312
$self -> cmp_Error($ans , " The units for your answer are not correct" )
299
313
unless $ans -> {correct_value }-> uPowers eq $ans -> {student_value }-> uPowers;
300
314
}
301
- $ans -> {error_flag } = undef if $ans -> {error_flag } =~ m / ^UNITS_/ ;
302
315
return $ans ;
303
316
}
304
317
0 commit comments