--- loncom/homework/caparesponse/caparesponse.pm 2014/12/30 20:03:15 1.255 +++ loncom/homework/caparesponse/caparesponse.pm 2020/12/04 22:19:19 1.261 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # caparesponse definition # -# $Id: caparesponse.pm,v 1.255 2014/12/30 20:03:15 raeburn Exp $ +# $Id: caparesponse.pm,v 1.261 2020/12/04 22:19:19 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -642,12 +642,7 @@ sub end_numericalresponse { } } } - if (($target eq 'web') && ($tag eq 'formularesponse') - && ($Apache::lonhomework::type ne 'exam') && ($Apache::inputtags::status['-1'] eq 'CAN_ANSWER') - && (&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffeditor') ne 'yes')) { - $result.=&Apache::response::edit_mathresponse_button($partid,$id); - } - + &Apache::response::setup_prior_tries_hash(\&format_prior_response_numerical); } elsif ($target eq 'edit') { $result.=''.&Apache::edit::end_table; @@ -669,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))); @@ -758,17 +760,21 @@ sub end_numericalresponse { my $ans=$answers[$i]; my $fmt=$formats[0]; if (@formats && $#formats) {$fmt=$formats[$i];} - foreach my $element (@$ans) { + my @answers; + if (ref($ans) eq 'ARRAY') { + @answers = (@{$ans}); + } + foreach my $element (@answers) { if ($fmt && $tag eq 'numericalresponse') { $fmt=~s/e/E/g; if ($unit=~/\$/) { $fmt="\$".$fmt; $unit=~s/\$//g; } 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)); + push(@fmt_ans,join(',',@answers)); } my $response=\@fmt_ans; @@ -778,7 +784,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"; } @@ -795,7 +801,16 @@ sub end_numericalresponse { $tag,$parstack, $safeeval,1); $error=&mt("Computer's answer is incorrect ([_1]).",'"'.join(', ',@$response).'"').' '; - if ($sigline ne '') { + 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 '') { $error.=&mt('It is likely that the tolerance range [_1] or significant figures [_2] need to be adjusted.',$tolline,$sigline); } else { $error.=&mt('It is likely that the tolerance range [_1] needs to be adjusted.',$tolline); @@ -896,7 +911,7 @@ sub get_table_sizes { my $cell_width=0; foreach my $member (@$rbubble_values) { my $cell_width_real=0; - if ($member=~/(\+|-)?(\d*)\.?(\d*)\s*\$?\\times\s*10\^{(\+|-)?(\d+)}\$?/) { + if ($member=~/(\+|-)?(\d*)\.?(\d*)\s*\$?\\times\s*10\^\{(\+|-)?(\d+)}\$?/) { $cell_width_real=(length($2)+length($3)+length($5)+7)*$scale; } elsif ($member=~/(\d*)\.?(\d*)(E|e)(\+|-)?(\d*)/) { $cell_width_real=(length($1)+length($2)+length($5)+9)*$scale; @@ -1050,7 +1065,12 @@ sub make_numerical_bubbles { &Math::Random::random_uniform_integer(1,1,10); } for ($ind=0;$ind<$number_of_bubbles;$ind++) { - $bubble_values[$ind] = $answerfactor*($factor**($power-$powers[$#powers-$ind])); + my $exponent = $power-$powers[$#powers-$ind]; + if ($exponent == 0) { + $bubble_values[$ind] = $answerfactor; + } else { + $bubble_values[$ind] = $answerfactor*($factor**$exponent); + } $bubble_display[$ind] = &format_number($bubble_values[$ind], $format,$target,$safeeval); }