--- loncom/homework/caparesponse/caparesponse.pm 2016/08/09 23:43:45 1.258 +++ loncom/homework/caparesponse/caparesponse.pm 2018/09/13 17:00:05 1.259 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # caparesponse definition # -# $Id: caparesponse.pm,v 1.258 2016/08/09 23:43:45 raeburn Exp $ +# $Id: caparesponse.pm,v 1.259 2018/09/13 17:00:05 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -664,7 +664,14 @@ sub end_numericalresponse { my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval); my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval); - + my ($needsdollar,$needscomma); + if ($unit =~ /\$/) { + $needsdollar = 1; + } + if ($unit =~ /\,/) { + $needscomma = 1; + } + if ($target eq 'answer') { $result.=&Apache::response::answer_header($tag,undef, scalar(keys(%answer))); @@ -760,7 +767,7 @@ sub end_numericalresponse { if ($unit=~/\,/) { $fmt="\,".$fmt; $unit=~s/\,//g; } $element = &format_number($element,$fmt,$target, $safeeval); - if ($fmt=~/\$/ && $unit!~/\$/) { $element=~s/\$//; } + if ($fmt=~/\$/ && !$needsdollar) { $element=~s/\$//; } } } push(@fmt_ans,join(',',@$ans)); @@ -773,7 +780,7 @@ sub end_numericalresponse { ! ($Apache::lonhomework::type eq 'exam' || lc($hideunit) eq "yes") ) { my $cleanunit=$unit; - $cleanunit=~s/\$\,//g; + $cleanunit=~s/[\$,]//g; foreach my $ans (@fmt_ans) { $ans.=" $cleanunit"; } @@ -790,7 +797,13 @@ sub end_numericalresponse { $tag,$parstack, $safeeval,1); $error=&mt("Computer's answer is incorrect ([_1]).",'"'.join(', ',@$response).'"').' '; - if ($ad eq 'UNIT_INVALID_STUDENT') { + if (($ad eq 'NO_UNIT') && $needsdollar) { + $error.=&mt('The unit attribute includes [_1] but the answer format does not.','$').' '. + &mt('Either remove the [_1] from the unit or prepend [_1] to the answer format.','$'); + } elsif (($ad eq 'COMMA_FAIL') && $needscomma) { + $error.=&mt('The unit attribute includes [_1] but the answer format does not.',',').' '. + &mt('Either remove the [_1] from the unit or prepend [_1] to the answer format.',','); + } elsif ($ad eq 'UNIT_INVALID_STUDENT') { $error.=&mt('Unable to interpret units. Computer reads units as "[_1]".',$msg).' '. &mt('The unit attribute in the numericalresponse item needs to be a supported physical unit.'); } elsif ($sigline ne '') {