Skip to content

Commit 3982803

Browse files
committed
Fix issues with the legacy NumberWithUnits.
See issue #1301 for details of the changes and examples to test with.
1 parent 0c7f42d commit 3982803

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

lib/Parser/Legacy/NumberWithUnits.pm

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ sub splitUnits {
9292
: $aUnit . '(?:\s*[/* ]\s*' . $aUnit . ')*';
9393
$unitPattern = $unitPattern . '(?:\/' . $unitPattern . ')*' if $parseMathQuill;
9494
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*$!;
9696
if ($units) {
9797
while ($units =~ s/$unitSpace/$1*$2/) { }
9898
$units =~ s/ //g;
@@ -238,20 +238,26 @@ sub unitsPreFilter {
238238
$ans->{correct_value}{context}
239239
&& $ans->{correct_value}->context->flag('useMathQuill')
240240
&& (!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+
}
241247
unless (defined($num) && defined($units) && $units ne '') {
242248
$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};
244250
return $ans;
245251
}
246252
if ($units =~ m!/.*/!) {
247253
$self->cmp_Error($ans, "Your units can only contain one division");
248-
$ans->{error_flag} = 'UNITS_DIVISION';
254+
$ans->{unit_error} = $ans->{ans_message};
249255
return $ans;
250256
}
251257
my $ref = { getUnits($units) };
252258
if ($ref->{ERROR}) {
253259
$self->cmp_Error($ans, $ref->{ERROR});
254-
$ans->{error_flag} = 'UNITS_BAD';
260+
$ans->{unit_error} = $ans->{ans_message};
255261
return $ans;
256262
}
257263
$ans->{units} = $units;
@@ -263,6 +269,14 @@ sub cmp_preprocess {
263269
my $self = shift;
264270
my $ans = shift;
265271

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+
}
266280
my $units = $ans->{units};
267281
return $ans unless $units;
268282
$ans->{student_ans} .= " " . $units;
@@ -272,8 +286,8 @@ sub cmp_preprocess {
272286
if (!defined($ans->{student_value}) || $self->checkStudentValue($ans->{student_value})) {
273287
$ans->{student_value} = undef;
274288
$ans->score(0);
275-
$ans->{error_flag} = 'UNITS_NO_NUMBER';
276289
$self->cmp_Error($ans, "Units must follow a number");
290+
$ans->{unit_error} = $ans->{ans_message};
277291
return;
278292
}
279293

@@ -284,7 +298,7 @@ sub cmp_preprocess {
284298
sub cmp_equal {
285299
my $self = shift;
286300
my $ans = shift;
287-
if (!$ans->{error_flag}) {
301+
if (!$ans->{unit_error}) {
288302
my $meth = @{ ref($self) . '::ISA' }[-1] . '::cmp_equal';
289303
$meth = 'Value::cmp_equal' unless defined &$meth;
290304
&$meth($self, $ans, @_);
@@ -298,7 +312,6 @@ sub cmp_postprocess {
298312
$self->cmp_Error($ans, "The units for your answer are not correct")
299313
unless $ans->{correct_value}->uPowers eq $ans->{student_value}->uPowers;
300314
}
301-
$ans->{error_flag} = undef if $ans->{error_flag} =~ m/^UNITS_/;
302315
return $ans;
303316
}
304317

macros/parsers/parserMultiAnswer.pl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@ sub cmp {
104104
}
105105

106106
if ($self->{allowBlankAnswers}) {
107+
my $blankCheck = AnswerEvaluator->new()->{pre_filters}[0][0];
107108
foreach my $cmp (@{ $self->{cmp} }) {
108-
$cmp->install_pre_filter('erase');
109+
$cmp->{pre_filters} = [ grep { $_->[0] != $blankCheck } @{ $cmp->{pre_filters} } ];
109110
$cmp->install_pre_filter(sub {
110111
my $ans = shift;
111112
$ans->{student_ans} =~ s/^\s+//g;

macros/parsers/parserRadioMultiAnswer.pl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,10 @@ sub cmp {
354354
}
355355

356356
if ($self->{allowBlankAnswers}) {
357+
my $blankCheck = AnswerEvaluator->new()->{pre_filters}[0][0];
357358
for (@{ $self->{cmp} }) {
358359
for my $cmp (@$_) {
359-
$cmp->install_pre_filter('erase');
360+
$cmp->{pre_filters} = [ grep { $_->[0] != $blankCheck } @{ $cmp->{pre_filters} } ];
360361
$cmp->install_pre_filter(sub {
361362
my $ans = shift;
362363
$ans->{student_ans} =~ s/^\s+//g;

0 commit comments

Comments
 (0)