--- loncom/homework/caparesponse/caparesponse.pm 2003/04/08 03:54:22 1.90 +++ loncom/homework/caparesponse/caparesponse.pm 2003/09/23 01:52:57 1.107.2.1 @@ -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.107.2.1 2003/09/23 01:52:57 albertel 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,86 @@ 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.="');"; + my @answer=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval); + &Apache::lonxml::debug('answer is'.join(':',@answer)); + @{$safeeval->varglob('CAPARESPONSE_CHECK_LIST_answer')}=@answer; + + $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 ( &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') { - #$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 +197,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 +209,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 +223,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 +232,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') { @@ -208,7 +244,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') { @@ -224,9 +260,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 +270,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}'; @@ -279,7 +310,7 @@ sub end_numericalresponse { if ($Apache::inputtags::params{'sig'}) { ($sighigh,$siglow)=&get_sigrange($Apache::inputtags::params{'sig'}); } - if ($fmt) { + if ($fmt && $$tagstack[-1] eq 'numericalresponse') { $ans = sprintf('%.'.$fmt,$ans); if ($high) { $high=sprintf('%.'.$fmt,$high); @@ -287,8 +318,14 @@ sub end_numericalresponse { } } if ($target eq 'answer') { - if ($high) { $ans.=' ['.$low.','.$high.']'; } - if ($sighigh) { $ans.= " Sig $siglow - $sighigh"; } + 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 $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); @@ -298,16 +335,21 @@ sub end_numericalresponse { } } } - if ($unit) { + if (defined($unit) and ($unit ne '') and + $$tagstack[-1] eq 'numericalresponse') { if ($target eq 'answer') { - $result.=&Apache::response::answer_part($$tagstack[-1], - "Unit: $unit"); + if ($ENV{'form.answer_output_mode'} eq 'tex') { + $result.=&Apache::response::answer_part($$tagstack[-1], + " Unit: $unit "); + } else { + $result.=&Apache::response::answer_part($$tagstack[-1], + "Unit: $unit"); + } } elsif ($target eq 'analyze') { - push (@{ $Apache::lonhomework::analyze{"$part_id.unit"} }, - $unit); + push (@{ $Apache::lonhomework::analyze{"$part_id.unit"} }, $unit); } } - if ($type || $token->[1] eq 'stringresponse') { + if ($type || $$tagstack[-1] eq 'stringresponse') { my $string='Case Insensitive'; if ($type eq 'mc') { $string='Multiple Choice'; @@ -319,10 +361,15 @@ sub end_numericalresponse { $string='Formula'; } if ($target eq 'answer') { - $result.=&Apache::response::answer_part($$tagstack[-1], - ''.$string.''); + if ($ENV{'form.answer_output_mode'} eq 'tex') { + $result.=&Apache::response::answer_part($$tagstack[-1], + "\\textbf{$string}"); + } else { + $result.=&Apache::response::answer_part($$tagstack[-1], + "$string"); + } } elsif ($target eq 'analyze') { - push (@{ $Apache::lonhomework::analyze{"$part_id.type"} }, + push (@{ $Apache::lonhomework::analyze{"$part_id.str_type"} }, $type); } } @@ -392,8 +439,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 +449,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 { @@ -445,7 +495,9 @@ sub start_stringresponse { my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_; my $result; if ($target eq 'meta') { + &Apache::response::start_response($parstack,$safeeval); $result=&Apache::response::meta_package_write('stringresponse'); + &Apache::response::end_response(); } else { $result.=&start_numericalresponse(@_); } @@ -460,7 +512,9 @@ sub start_formularesponse { my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_; my $result; if ($target eq 'meta') { + &Apache::response::start_response($parstack,$safeeval); $result=&Apache::response::meta_package_write('formularesponse'); + &Apache::response::end_response(); } else { $result.=&start_numericalresponse(@_); }