--- loncom/homework/caparesponse/caparesponse.pm 2003/06/24 20:41:56 1.105 +++ loncom/homework/caparesponse/caparesponse.pm 2003/10/23 07:31:52 1.117 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # caparesponse definition # -# $Id: caparesponse.pm,v 1.105 2003/06/24 20:41:56 albertel Exp $ +# $Id: caparesponse.pm,v 1.117 2003/10/23 07:31:52 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -41,6 +41,8 @@ sub start_numericalresponse { if ($target eq 'edit') { $result.=&Apache::edit::tag_start($target,$token); $result.=&Apache::edit::text_arg('Answer:','answer',$token); + $result.=&Apache::edit::text_arg('Incorrect Answers:','incorrect', + $token); if ($token->[1] eq 'numericalresponse') { $result.=&Apache::edit::text_arg('Unit:','unit',$token,5). &Apache::loncommon::help_open_topic('Physical_Units'); @@ -61,7 +63,8 @@ sub start_numericalresponse { if ($token->[1] eq 'numericalresponse') { $constructtag=&Apache::edit::get_new_args($token,$parstack, $safeeval,'answer', - 'unit','format'); + 'incorrect','unit', + 'format'); } elsif ($token->[1] eq 'stringresponse') { $constructtag=&Apache::edit::get_new_args($token,$parstack, $safeeval,'answer', @@ -114,23 +117,22 @@ sub end_numericalresponse { if ( $response =~ /[^\s]/) { my $ad; my %previous = &Apache::response::check_for_previous($response,$partid,$id); - $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response; &Apache::lonxml::debug("submitted a $response
\n"); &Apache::lonxml::debug($$parstack[-1] . "\n
"); if ($ENV{'form.submitted'} eq 'scantron') { - my $number_of_bubbles = 8;#default values for number of bubbles - my (@formats)=&Apache::lonxml::get_param_var('format', - $parstack,$safeeval); - my (@answers)=&Apache::lonxml::get_param_var('answer', - $parstack,$safeeval); - my @values=&make_numerical_bubbles($number_of_bubbles,$target, - $answers[0],$formats[0]); + my $number_of_bubbles = &Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.numbubbles'); + if (!$number_of_bubbles) { $number_of_bubbles=8; } + my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval); + my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval); + my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval); + my @values=&make_numerical_bubbles($number_of_bubbles,$target,$answers[0],$formats[0],\@incorrect); $response=$values[$response]; } else { $response =~ s/\\/\\\\/g; $response =~ s/\'/\\\'/g; } + $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response; &Apache::lonxml::debug("current $response"); my $expression="&caparesponse_check_list('".$response."','". $$parstack[-1]; @@ -169,20 +171,13 @@ sub end_numericalresponse { $safeeval); my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"}; my $status = $Apache::inputtags::status['-1']; - if ( ($award =~ /^correct/ - && lc($Apache::lonhomework::problemstatus) ne 'no') - || $status eq "SHOW_ANSWER" - || $ENV{'form.texaward'} eq 'SHOW_ANSWER') { + if ( &Apache::response::show_answer() ) { my (@formats)=&Apache::lonxml::get_param_var('format',$parstack, $safeeval); my $unit=&Apache::lonxml::get_param_var('unit',$parstack, $safeeval); if ($target eq 'web') { $result="
The correct answer is "; - } elsif ($target eq 'tex') { - if ($ENV{'form.texaward'} eq 'SHOW_ANSWER') { - $result='\vskip 0 mm The correct answer is \\texttt{ '; - } } for (my $i=0; $i <= $#answers; $i++) { my $answer=$answers[$i]; @@ -194,15 +189,13 @@ sub end_numericalresponse { } my $formatted; if ((defined($format)) && ($format ne '')) { + $format=~s/e/E/g; &Apache::lonxml::debug("formatting with :$format: answer :$answer:"); $formatted=sprintf('%.'.$format,$answer).','; } else { &Apache::lonxml::debug("no format answer :$answer:"); $formatted="$answer,"; } - if ($ENV{'form.texaward'} eq 'SHOW_ANSWER') { - $result.=$formatted; - } if ($target eq 'tex') { $formatted=''; #$formatted=&Apache::lonxml::latex_special_symbols($formatted); @@ -212,33 +205,35 @@ sub end_numericalresponse { chop $result; if ($target eq 'web') { $result.=" $unit.
"; - } elsif ($target eq 'tex') { - if ($ENV{'form.texaward'} eq 'SHOW_ANSWER') { - $result.=&Apache::lonxml::latex_special_symbols($unit); - $result.="}. \\vskip 0 mm "; - } } } if ($Apache::lonhomework::type eq 'exam') { - my $number_of_bubbles = 8; #default values for number of bubbles + my $number_of_bubbles = &Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.numbubbles'); + if (!$number_of_bubbles) { $number_of_bubbles=8; } my (@formats)=&Apache::lonxml::get_param_var('format',$parstack, $safeeval); my $unit=&Apache::lonxml::get_param_var('unit',$parstack, $safeeval); + my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval); my @bubble_values=&make_numerical_bubbles($number_of_bubbles, $target,$answers[0], - $formats[0]); + $formats[0],\@incorrect); my @alphabet=('A'..'Z'); my $id=$Apache::inputtags::response[-1]; if ($target eq 'web') { if ($$tagstack[-1] eq 'numericalresponse') { if ($unit=~/\S/) {$result.=' (in '.$unit.')

';} $result.= ''; + my $previous=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.$id.submission"}; for (my $ind=0;$ind<$number_of_bubbles;$ind++) { + my $checked=''; + if ($previous eq $bubble_values[$ind]) { + $checked=" checked='on' "; + } $result.=''; + '" value="'.$bubble_values[$ind].'" '.$checked + .' />'.$alphabet[$ind].': '. + $bubble_values[$ind].''; } $result.='
'. - $alphabet[$ind].': '. - $bubble_values[$ind].'
'; } elsif ($$tagstack[-1] eq 'formularesponse') { @@ -247,7 +242,7 @@ sub end_numericalresponse {

'; } } elsif ($target eq 'tex') { - if (defined $unit and $Apache::lonhomework::type eq 'exam') { + if ((defined $unit) and ($unit=~/\S/) and ($Apache::lonhomework::type eq 'exam')) { $result.=' \textit{(in} \verb|'.$unit.'|\textit{)} '; } if ($$tagstack[-1] eq 'numericalresponse') { @@ -291,6 +286,8 @@ sub end_numericalresponse { if ($target eq 'analyze') { push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id); $Apache::lonhomework::analyze{"$part_id.type"} = $$tagstack[-1]; + my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval); + push (@{ $Apache::lonhomework::analyze{"$part_id.incorrect"} }, @incorrect); } &Apache::response::setup_params($$tagstack[-1]); my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval); @@ -314,6 +311,7 @@ sub end_numericalresponse { ($sighigh,$siglow)=&get_sigrange($Apache::inputtags::params{'sig'}); } if ($fmt && $$tagstack[-1] eq 'numericalresponse') { + $fmt=~s/e/E/g; $ans = sprintf('%.'.$fmt,$ans); if ($high) { $high=sprintf('%.'.$fmt,$high); @@ -324,7 +322,7 @@ sub end_numericalresponse { if ($high && $$tagstack[-1] eq 'numericalresponse') { $ans.=' ['.$low.','.$high.']'; } if ($sighigh && $$tagstack[-1] eq 'numericalresponse') { if ($ENV{'form.answer_output_mode'} eq 'tex') { - $ans.= " Sig \\textit{$siglow - $sighigh}"; + $ans.= " Sig $siglow - $sighigh"; } else { $ans.= " Sig $siglow - $sighigh"; } @@ -343,7 +341,7 @@ sub end_numericalresponse { if ($target eq 'answer') { if ($ENV{'form.answer_output_mode'} eq 'tex') { $result.=&Apache::response::answer_part($$tagstack[-1], - "Unit: \\verb|$unit|"); + " Unit: $unit "); } else { $result.=&Apache::response::answer_part($$tagstack[-1], "Unit: $unit"); @@ -366,7 +364,7 @@ sub end_numericalresponse { if ($target eq 'answer') { if ($ENV{'form.answer_output_mode'} eq 'tex') { $result.=&Apache::response::answer_part($$tagstack[-1], - "\\textbf{$string}"); + "$string"); } else { $result.=&Apache::response::answer_part($$tagstack[-1], "$string"); @@ -416,12 +414,13 @@ sub format_number { my ($number,$format,$target)=@_; my $ans; if ($format ne '') { + $format=~s/e/E/g; $ans = sprintf('%.'.$format,$number); } else { my $format = ''; #What is the number? (integer,decimal,floating point) if ($number=~/^(\d*\.?\d*)(E|e)(\d*)$/) { - $format = 'e'.$2; + $format = '3e'; } elsif ($number=~/^(\d*)\.(\d*)$/) { $format = '4f'; } elsif ($number=~/^(\d*)$/) { @@ -442,8 +441,22 @@ sub format_number { } sub make_numerical_bubbles { - my ($number_of_bubbles,$target,$answer,$format) =@_; + my ($number_of_bubbles,$target,$answer,$format,$incorrect) =@_; my @bubble_values = (); + &Apache::lonxml::debug("incorrect is $incorrect"); + if (defined($incorrect) && ref($incorrect)) { + &Apache::lonxml::debug("inside ".(scalar(@$incorrect)+1 gt $number_of_bubbles)); + if (scalar(@$incorrect)+1 >= $number_of_bubbles) { + &Apache::lonxml::debug("inside ".(scalar(@$incorrect)+1).":$number_of_bubbles"); + &Apache::response::setrandomnumber(); + my @rand_inc=&Math::Random::random_permutation(@$incorrect); + @bubble_values=@rand_inc[0..($number_of_bubbles-2)]; + @bubble_values=sort {$a <=> $b} (@bubble_values,$answer); + &Apache::lonxml::debug("Answer was :$answer: returning :".$#bubble_values.": whih are :".join(':',@bubble_values)); + return @bubble_values; + } + #FIXME what to do when not enough incorrects specified? + } my @factors = (1.13,1.17,1.25,1.33,1.45); #default values of factors my @powers = (1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0); #default values for powers &Apache::response::setrandomnumber();