--- loncom/homework/caparesponse/caparesponse.pm 2003/04/08 03:54:22 1.90 +++ loncom/homework/caparesponse/caparesponse.pm 2003/05/13 20:39:58 1.100 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # caparesponse definition # -# $Id: caparesponse.pm,v 1.90 2003/04/08 03:54:22 albertel Exp $ +# $Id: caparesponse.pm,v 1.100 2003/05/13 20:39:58 sakharuk Exp $ # # Copyright Michigan State University Board of Trustees # @@ -79,6 +79,16 @@ sub start_numericalresponse { $result=&Apache::response::meta_package_write('numericalresponse'); } elsif ($target eq 'answer' || $target eq 'grade') { &Apache::response::reset_params(); + } elsif ($target eq 'web') { + my $partid = $Apache::inputtags::part; + my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit'); + &Apache::lonxml::debug("Got unit $hideunit for $partid $id"); + #no way to enter units, with radio buttons + if (lc($hideunit) eq "yes") { + my $unit=&Apache::lonxml::get_param_var('unit',$parstack, + $safeeval); + if ($unit =~ /\S/) { $result.=" (in $unit) "; } + } } return $result; } @@ -90,64 +100,85 @@ sub end_numericalresponse { if (!$Apache::lonxml::default_homework_loaded) { &Apache::lonxml::default_homework_load($safeeval); } - if ( $target eq 'grade' && defined $ENV{'form.submitted'}) { + if ( $target eq 'grade' && defined($ENV{'form.submitted'})) { &Apache::response::setup_params($$tagstack[-1]); $safeeval->share_from('capa',['&caparesponse_capa_check_answer']); my $partid = $Apache::inputtags::part; my $id = $Apache::inputtags::response['-1']; - my $response = &Apache::response::getresponse(); - 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[$#$parstack] . "\n
"); - - if ($ENV{'form.submitted'} eq 'scantron') { - &Apache::response::setrandomnumber(); - #FIXME the 8 here is based off of number of powers need a parameter - my $ind=&Math::Random::random_uniform_integer(1,0,8); - if ($ind eq $response) { $ad='CORRECT'; } else { $ad='INCORRECT'; } - } - $response =~ s/\\/\\\\/g; - $response =~ s/\'/\\\'/g; - &Apache::lonxml::debug("current $response"); - my $expression="&caparesponse_check_list('".$response."','". - $$parstack[-1]; - foreach my $key (keys(%Apache::inputtags::params)) { - $expression.= ';my $'. #' - $key.'="'.$Apache::inputtags::params{$key}.'"'; - } - if ($$tagstack[-1] eq 'formularesponse') { - $expression.=';my $type="fml";'; - } elsif ($$tagstack[-1] eq 'numericalresponse') { - $expression.=';my $type="float";'; - } - $expression.="');"; - $result = &Apache::run::run($expression,$safeeval); - my ($awards) = split /:/ , $result; - ($ad) = &Apache::inputtags::finalizeawards(split /,/ , $awards); - &Apache::lonxml::debug("$expression"); - &Apache::lonxml::debug("\n
result:$result:$Apache::lonxml::curdepth
\n"); - &Apache::response::handle_previous(\%previous,$ad); - $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad; - $result=''; + if ($Apache::lonhomework::type eq 'exam' && + ($$tagstack[-1] eq 'formularesponse' || + $$tagstack[-1] eq 'stringresponse')) { + $increment=&Apache::response::scored_response($partid,$id); + } else { + my $response = &Apache::response::getresponse(); + 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]); + $response=$values[$response]; + } else { + $response =~ s/\\/\\\\/g; + $response =~ s/\'/\\\'/g; + } + &Apache::lonxml::debug("current $response"); + my $expression="&caparesponse_check_list('".$response."','". + $$parstack[-1]; + my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit'); + #no way to enter units, with radio buttons + if ($Apache::lonhomework::type eq 'exam' || + lc($hideunit) eq "yes") { + $expression.=';my $unit=undef;'; + } + foreach my $key (keys(%Apache::inputtags::params)) { + $expression.= ';my $'. #' + $key.'="'.$Apache::inputtags::params{$key}.'"'; + } + if ($$tagstack[-1] eq 'formularesponse') { + $expression.=';my $type="fml";'; + } elsif ($$tagstack[-1] eq 'numericalresponse') { + $expression.=';my $type="float";'; + } + $expression.="');"; + $result = &Apache::run::run($expression,$safeeval); + my ($awards) = split /:/ , $result; + ($ad) = &Apache::inputtags::finalizeawards(split /,/ , $awards); + &Apache::lonxml::debug("$expression"); + &Apache::lonxml::debug("\n
result:$result:$Apache::lonxml::curdepth
\n"); + &Apache::response::handle_previous(\%previous,$ad); + $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad; + $result=''; + } } } elsif ($target eq 'web' || $target eq 'tex') { my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack, $safeeval); my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"}; my $status = $Apache::inputtags::status['-1']; - if ($award =~ /^correct/ || $status eq "SHOW_ANSWER" ) { + if ( ($award =~ /^correct/ + && lc($Apache::lonhomework::problemstatus) ne 'no') + || $status eq "SHOW_ANSWER" + || $ENV{'form.texaward'} eq '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') { - #$result='\vskip 0 mm The correct answer is \\texttt{'; + 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]; @@ -165,7 +196,9 @@ sub end_numericalresponse { &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); @@ -175,9 +208,11 @@ sub end_numericalresponse { chop $result; if ($target eq 'web') { $result.=" $unit.
"; - } elsif ($target eq 'tex') { - #$result.=&Apache::lonxml::latex_special_symbols($unit); - #$result.="}. \\vskip 0 mm "; + } 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') { @@ -187,7 +222,8 @@ sub end_numericalresponse { my $unit=&Apache::lonxml::get_param_var('unit',$parstack, $safeeval); my @bubble_values=&make_numerical_bubbles($number_of_bubbles, - @answers); + $target,$answers[0], + $formats[0]); my @alphabet=('A'..'Z'); my $id=$Apache::inputtags::response[-1]; if ($target eq 'web') { @@ -195,11 +231,10 @@ sub end_numericalresponse { if ($unit=~/\S/) {$result.=' (in '.$unit.')

';} $result.= ''; for (my $ind=0;$ind<$number_of_bubbles;$ind++) { - my $ans=&format_number($bubble_values[$ind], - $formats[0]); $result.=''; + '" value="'.$bubble_values[$ind].'">'. + $alphabet[$ind].': '. + $bubble_values[$ind].''; } $result.='
'.$alphabet[$ind]. - ': '.$ans.'
'; } elsif ($$tagstack[-1] eq 'formularesponse') { @@ -224,9 +259,7 @@ sub end_numericalresponse { } $result.='}'; for (my $ind=$cou;$ind<$cou+$table_range[$j];$ind++) { - my $ans=&format_number($bubble_values[$ind], - $formats[0],$target); - $result.='\hskip -3 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -2 mm & {\small '.$ans.'} '; + $result.='\hskip -3 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -2 mm & {\small '.$bubble_values[$ind].'} '; if ($ind != $cou+$table_range[$j]-1) {$result.=' & ';} } $cou += $table_range[$j]; @@ -236,13 +269,10 @@ sub end_numericalresponse { $result.='\end{enumerate}'; } else { $result.='\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}'; - my $id = $Apache::inputtags::part; - my $weight = &Apache::lonnet::EXT("resource.$id.weight"); - my $repetition = int $weight/9; - if ($weight % 9 != 0) {$repetition++;} + my $repetition = &Apache::response::repetition(); $result.='\begin{enumerate}'; for (my $i=0;$i<$repetition;$i++) { - $result.='\item[\textbf{'.$Apache::lonxml::counter.'}.]\textit{Leave blank on scoring form}\vskip 0 mm'; + $result.='\item[\textbf{'.($Apache::lonxml::counter+$i).'}.]\textit{Leave blank on scoring form}\vskip 0 mm'; } $increment=$repetition; $result.= '\end{enumerate}'; @@ -288,7 +318,13 @@ sub end_numericalresponse { } if ($target eq 'answer') { if ($high) { $ans.=' ['.$low.','.$high.']'; } - if ($sighigh) { $ans.= " Sig $siglow - $sighigh"; } + if ($sighigh) { + if ($ENV{'form.print_answer'} eq 'yes') { + $ans.= " Sig \\textit{$siglow - $sighigh}"; + } else { + $ans.= " Sig $siglow - $sighigh"; + } + } $result.=&Apache::response::answer_part($$tagstack[-1],$ans); } elsif ($target eq 'analyze') { push (@{ $Apache::lonhomework::analyze{"$part_id.answer"} }, $ans); @@ -300,8 +336,13 @@ sub end_numericalresponse { } if ($unit) { if ($target eq 'answer') { - $result.=&Apache::response::answer_part($$tagstack[-1], - "Unit: $unit"); + if ($ENV{'form.print_answer'} eq 'yes') { + $result.=&Apache::response::answer_part($$tagstack[-1], + "Unit: \\verb|$unit|"); + } else { + $result.=&Apache::response::answer_part($$tagstack[-1], + "Unit: $unit"); + } } elsif ($target eq 'analyze') { push (@{ $Apache::lonhomework::analyze{"$part_id.unit"} }, $unit); @@ -392,8 +433,8 @@ sub format_number { } sub make_numerical_bubbles { - my ($number_of_bubbles,@answers) =@_; - my @bubbles_values = (); + my ($number_of_bubbles,$target,$answer,$format) =@_; + my @bubble_values = (); 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(); @@ -402,9 +443,12 @@ sub make_numerical_bubbles { $ind=&Math::Random::random_uniform_integer(1,0,$#factors); my $factor = $factors[$ind]; for ($ind=0;$ind<$number_of_bubbles;$ind++) { - $bubbles_values[$ind] = $answers[0]*($factor**($power-$powers[$#powers-$ind])); + $bubble_values[$ind] = $answer*($factor**($power-$powers[$#powers-$ind])); + $bubble_values[$ind] = &format_number($bubble_values[$ind], + $format,$target); + } - return @bubbles_values; + return @bubble_values; } sub get_tolrange {