Annotation of loncom/homework/caparesponse/caparesponse.pm, revision 1.205

1.3       albertel    1: # The LearningOnline Network with CAPA
                      2: # caparesponse definition
1.47      albertel    3: #
1.205   ! www         4: # $Id: caparesponse.pm,v 1.204 2006/12/19 00:49:53 albertel Exp $
1.47      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.3       albertel   28: 
                     29: package Apache::caparesponse;
                     30: use strict;
1.4       albertel   31: use capa;
1.190     albertel   32: use Safe::Hole;
                     33: use Apache::lonmaxima();
1.129     www        34: use Apache::lonlocal;
1.166     albertel   35: use Apache::lonnet;
1.202     www        36: use Apache::response();
1.194     albertel   37: use Storable qw(dclone);
1.3       albertel   38: 
1.50      harris41   39: BEGIN {
1.194     albertel   40:     &Apache::lonxml::register('Apache::caparesponse',('numericalresponse','stringresponse','formularesponse'));
1.3       albertel   41: }
                     42: 
1.181     albertel   43: my %answer;
1.204     albertel   44: my @answers;
1.203     albertel   45: sub get_answer { return %answer; };
1.204     albertel   46: sub push_answer{ push(@answers,dclone(\%answer)); undef(%answer) }
                     47: sub pop_answer { %answer = %{pop(@answers)}; };
1.203     albertel   48: 
1.181     albertel   49: my $cur_name;
1.195     albertel   50: my $tag_internal_answer_name = 'INTERNAL';
                     51: 
1.181     albertel   52: sub start_answer {
                     53:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                     54:     my $result;
                     55:     $cur_name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                     56:     if ($cur_name =~ /^\s*$/) { $cur_name = $Apache::lonxml::curdepth; }
                     57:     my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
                     58:     if (!defined($type) && $tagstack->[-2] eq 'answergroup') {
                     59: 	$type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
                     60:     }
                     61:     if (!defined($type)) { $type = 'ordered' };
                     62:     $answer{$cur_name}= { 'type' => $type,
                     63: 			  'answers' => [] };
                     64:     return $result;
                     65: }
                     66: 
                     67: sub end_answer {
                     68:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                     69:     my $result;
                     70:     undef($cur_name);
                     71:     return $result;
                     72: }
                     73: 
                     74: sub start_answergroup {
                     75:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                     76:     my $result;
                     77:     return $result;
                     78: }
                     79: 
                     80: sub end_answergroup {
                     81:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                     82:     my $result;
1.194     albertel   83:     if ($target eq 'web') {
                     84:     	if (  &Apache::response::show_answer() ) {  
                     85: 	    my $partid = $Apache::inputtags::part;
                     86: 	    my $id = $Apache::inputtags::response[-1];
                     87: 	    &set_answertext($Apache::lonhomework::history{"resource.$partid.$id.answername"},
                     88: 			    $target,$token,$tagstack,$parstack,$parser,
                     89: 			    $safeeval,-2);
                     90: 	}
                     91:     }
1.181     albertel   92:     return $result;
                     93: }
                     94: 
                     95: sub start_value {
1.182     albertel   96:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.181     albertel   97:     my $result;
                     98:     if ( $target eq 'web' || $target eq 'tex' ||
                     99: 	 $target eq 'grade' || $target eq 'webgrade' ||
                    100: 	 $target eq 'answer' || $target eq 'analyze' ) {
1.182     albertel  101: 	my $bodytext = &Apache::lonxml::get_all_text("/value",$parser,$style);
1.181     albertel  102: 	$bodytext = &Apache::run::evaluate($bodytext,$safeeval,
                    103: 					   $$parstack[-1]);
1.195     albertel  104: 	
                    105: 	push(@{ $answer{$cur_name}{'answers'} },[$bodytext]);
                    106: 	
1.181     albertel  107:     }
                    108:     return $result;
                    109: }
                    110: 
                    111: sub end_value {
                    112:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    113:     my $result;
                    114:     return $result;
                    115: }
                    116: 
1.195     albertel  117: sub start_vector {
                    118:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    119:     my $result;
                    120:     if ( $target eq 'web' || $target eq 'tex' ||
                    121: 	 $target eq 'grade' || $target eq 'webgrade' ||
                    122: 	 $target eq 'answer' || $target eq 'analyze' ) {
                    123: 	my $bodytext = &Apache::lonxml::get_all_text("/vector",$parser,$style);
                    124: 	my @values = &Apache::run::run($bodytext,$safeeval,$$parstack[-1]);
                    125: 	if (@values == 1) {
                    126: 	    @values = split(',',$values[0]);
                    127: 	}
                    128: 	push(@{ $answer{$cur_name}{'answers'} },\@values);
                    129:     }
                    130:     return $result;
                    131: }
                    132: 
                    133: sub end_vector {
                    134:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    135:     my $result;
                    136:     return $result;
                    137: }
                    138: 
1.181     albertel  139: sub start_array {
1.182     albertel  140:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.181     albertel  141:     my $result;
                    142:     if ( $target eq 'web' || $target eq 'tex' ||
                    143: 	 $target eq 'grade' || $target eq 'webgrade' ||
                    144: 	 $target eq 'answer' || $target eq 'analyze' ) {
1.182     albertel  145: 	my $bodytext = &Apache::lonxml::get_all_text("/array",$parser,$style);
1.181     albertel  146: 	my @values = &Apache::run::evaluate($bodytext,$safeeval,
                    147: 					    $$parstack[-1]);
                    148: 	push(@{ $answer{$cur_name}{'answers'} },@values);
                    149:     }
                    150:     return $result;
                    151: }
                    152: 
                    153: sub end_array {
                    154:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    155:     my $result;
                    156:     return $result;
                    157: }
                    158: 
                    159: sub start_unit {
                    160:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    161:     my $result;
                    162:     return $result;
                    163: }
                    164: 
                    165: sub end_unit {
                    166:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    167:     my $result;
                    168:     return $result;
                    169: }
                    170: 
1.89      albertel  171: sub start_numericalresponse {
                    172:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.195     albertel  173:     &Apache::lonxml::register('Apache::caparesponse',
                    174: 			      ('answer','answergroup','value','array','unit',
                    175: 			       'vector'));
1.89      albertel  176:     my $id = &Apache::response::start_response($parstack,$safeeval);
                    177:     my $result;
1.181     albertel  178:     undef(%answer);
                    179:     undef(%{$safeeval->varglob('LONCAPA::CAPAresponse_args')});
1.89      albertel  180:     if ($target eq 'edit') {
                    181: 	$result.=&Apache::edit::tag_start($target,$token);
                    182: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
                    183: 	if ($token->[1] eq 'numericalresponse') {
1.122     albertel  184: 	    $result.=&Apache::edit::text_arg('Incorrect Answers:','incorrect',
1.149     www       185: 					     $token).
                    186: 		&Apache::loncommon::help_open_topic('numerical_wrong_answers');
1.89      albertel  187: 	    $result.=&Apache::edit::text_arg('Unit:','unit',$token,5).
                    188: 		&Apache::loncommon::help_open_topic('Physical_Units');
                    189: 	    $result.=&Apache::edit::text_arg('Format:','format',$token,4).
                    190: 		&Apache::loncommon::help_open_topic('Numerical_Response_Format');
                    191: 	} elsif ($token->[1] eq 'formularesponse') {
                    192: 	    $result.=&Apache::edit::text_arg('Sample Points:','samples',
                    193: 					     $token,40).
                    194: 	      &Apache::loncommon::help_open_topic('Formula_Response_Sampling');
                    195: 	}
                    196: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    197:     } elsif ($target eq 'modified') {
                    198: 	my $constructtag;
                    199: 	if ($token->[1] eq 'numericalresponse') {
                    200: 	    $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    201: 						      $safeeval,'answer',
1.156     albertel  202:  						      'incorrect','unit',
1.117     albertel  203: 						      'format');
1.89      albertel  204: 	} elsif ($token->[1] eq 'formularesponse') {
                    205: 	    $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    206: 						      $safeeval,'answer',
                    207: 						      'samples');
                    208: 	}
                    209: 	if ($constructtag) {
                    210: 	    $result = &Apache::edit::rebuild_tag($token);
                    211: 	    $result.=&Apache::edit::handle_insert();
1.22      albertel  212: 	}
1.89      albertel  213:     } elsif ($target eq 'meta') {
                    214: 	$result=&Apache::response::meta_package_write('numericalresponse');
                    215:     } elsif ($target eq 'answer' || $target eq 'grade') {
                    216: 	&Apache::response::reset_params();
1.96      albertel  217:     } elsif ($target eq 'web') {
                    218: 	my $partid = $Apache::inputtags::part;
                    219: 	my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
                    220: 	&Apache::lonxml::debug("Got unit $hideunit for $partid $id");
                    221: 	#no way to enter units, with radio buttons
                    222: 	if (lc($hideunit) eq "yes") {
                    223: 	    my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
                    224: 						    $safeeval);
                    225: 	    if ($unit =~ /\S/) { $result.=" (in $unit) "; }
                    226: 	}
1.146     albertel  227: 	if (  &Apache::response::show_answer() ) {
1.195     albertel  228: 	    &set_answertext($tag_internal_answer_name,$target,$token,$tagstack,
                    229: 			    $parstack,$parser,$safeeval,-1);
1.194     albertel  230: 	}
                    231:     }
                    232:     return $result;
                    233: }
                    234: 
                    235: sub set_answertext {
                    236:     my ($name,$target,$token,$tagstack,$parstack,$parser,$safeeval,
                    237: 	$response_level) = @_;
                    238:     &add_in_tag_answer($parstack,$safeeval,$response_level);
                    239: 
                    240:     return if ($name eq '' || !ref($answer{$name}));
                    241: 
                    242:     my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,
                    243: 						 $safeeval,$response_level);
                    244:     my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval,
                    245: 					    $response_level);
                    246: 
                    247:     &Apache::lonxml::debug("answer looks to be $name");
1.195     albertel  248:     my @answertxt;
1.194     albertel  249:     for (my $i=0; $i < scalar(@{$answer{$name}{'answers'}}); $i++) {
1.195     albertel  250: 	my $answertxt;
1.194     albertel  251: 	my $answer=$answer{$name}{'answers'}[$i];
1.195     albertel  252: 	foreach my $element (@$answer) {
                    253: 	    if ( scalar(@$tagstack)
                    254: 		 && $tagstack->[$response_level] ne 'numericalresponse') {
                    255: 		$answertxt.=$element.',';
1.194     albertel  256: 	    } else {
1.195     albertel  257: 		my $format;
                    258: 		if ($#formats > 0) {
                    259: 		    $format=$formats[$i];
                    260: 		} else {
                    261: 		    $format=$formats[0];
                    262: 		}
                    263: 		if ($unit=~/\$/) { $format="\$".$format; $unit=~s/\$//g; }
                    264: 		if ($unit=~/\,/) { $format="\,".$format; $unit=~s/\,//g; }
                    265: 		my $formatted=&format_number($element,$format,$target,
                    266: 					     $safeeval);
                    267: 		$answertxt.=' '.$formatted.',';
1.146     albertel  268: 	    }
1.195     albertel  269: 	    
                    270: 	}
                    271: 	chop($answertxt);
                    272: 	if ($target eq 'web') {
                    273: 	    $answertxt.=" $unit ";
1.146     albertel  274: 	}
1.195     albertel  275: 
                    276: 	push(@answertxt,$answertxt)
1.16      albertel  277:     }
1.194     albertel  278: 	
                    279:     my $id = $Apache::inputtags::response[-1];
1.195     albertel  280:     $Apache::inputtags::answertxt{$id}=\@answertxt;
1.21      albertel  281: }
                    282: 
1.195     albertel  283: sub setup_capa_args {
                    284:     my ($safeeval,$parstack,$args,$response) = @_;
1.167     albertel  285:     my $args_ref= \%{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
1.195     albertel  286:     undef(%{ $args_ref });
                    287:  
                    288:     foreach my $arg (@{$args}) {
1.167     albertel  289: 	$$args_ref{$arg}=
                    290: 	    &Apache::lonxml::get_param($arg,$parstack,$safeeval);
                    291:     }
                    292:     foreach my $key (keys(%Apache::inputtags::params)) {
                    293: 	$$args_ref{$key}=$Apache::inputtags::params{$key};
                    294:     }
1.195     albertel  295:     &setup_capa_response($args_ref,$response);
                    296:     return $args_ref;
                    297: }
                    298: 
                    299: sub setup_capa_response {
                    300:     my ($args_ref,$response) = @_;   
                    301: 
                    302:     use Data::Dumper;
                    303:     &Apache::lonxml::debug("response dump is ".&Dumper($response));
1.167     albertel  304:     
1.195     albertel  305:     if (ref($response)) {
                    306: 	$$args_ref{'response'}=dclone($response);
                    307:     } else {
                    308: 	$$args_ref{'response'}=dclone([$response]);
                    309:     }
                    310: }
                    311: 
                    312: sub check_submission {
                    313:     my ($response,$partid,$id,$tag,$parstack,$safeeval,$ignore_sig)=@_;
                    314:     my @args = ('type','tol','sig','format','unit','calc','samples');
                    315:     my $args_ref = &setup_capa_args($safeeval,$parstack,\@args,$response);
                    316: 
                    317:     my $hideunit=
                    318: 	&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
                    319:         #no way to enter units, with radio buttons
1.167     albertel  320:     if ($Apache::lonhomework::type eq 'exam' ||
                    321: 	lc($hideunit) eq "yes") {
                    322: 	delete($$args_ref{'unit'});
                    323:     }
                    324:     #sig fig don't make much sense either
                    325:     if (($Apache::lonhomework::type eq 'exam' ||
1.171     albertel  326: 	 &Apache::response::submitted('scantron') ||
                    327: 	 $ignore_sig) &&
1.167     albertel  328: 	$tag eq 'numericalresponse') {
                    329: 	delete($$args_ref{'sig'});
                    330:     }
                    331:     
                    332:     if ($tag eq 'formularesponse') {
1.199     www       333: 	if ($$args_ref{'samples'}) {
1.191     www       334: 	    $$args_ref{'type'}='fml';
1.199     www       335: 	} else {
                    336: 	    $$args_ref{'type'}='math';
                    337: 	}
1.167     albertel  338:     } elsif ($tag eq 'numericalresponse') {
                    339: 	$$args_ref{'type'}='float';
                    340:     }
1.194     albertel  341:     
                    342:     &add_in_tag_answer($parstack,$safeeval);
                    343: 
1.195     albertel  344:     my (@final_awards,@final_msgs,@names);
1.181     albertel  345:     foreach my $name (keys(%answer)) {
                    346: 	&Apache::lonxml::debug(" doing $name with ".join(':',@{ $answer{$name}{'answers'} }));
1.194     albertel  347: 	
                    348: 	${$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=dclone($answer{$name});
1.195     albertel  349: 	&setup_capa_response($args_ref,$response);
                    350: 	use Time::HiRes;
                    351: 	my $t0 = [Time::HiRes::gettimeofday()];
1.181     albertel  352: 	my ($result,@msgs) = 
                    353: 	    &Apache::run::run("&caparesponse_check_list()",$safeeval);
1.195     albertel  354: 	&Apache::lonxml::debug("checking $name $result with $response took ".&Time::HiRes::tv_interval($t0));
1.181     albertel  355: 	&Apache::lonxml::debug('msgs are '.join(':',@msgs));
                    356: 	my ($awards)=split(/:/,$result);
                    357: 	my @awards= split(/,/,$awards);
                    358: 	my ($ad, $msg) = &Apache::inputtags::finalizeawards(\@awards,\@msgs);
                    359: 	push(@final_awards,$ad);
                    360: 	push(@final_msgs,$msg);
                    361: 	push(@names,$name);
                    362:     }
                    363:     my ($ad, $msg, $name) = &Apache::inputtags::finalizeawards(\@final_awards,
                    364: 							       \@final_msgs,
                    365: 							       \@names,1);
1.194     albertel  366:     &Apache::lonxml::debug(" name of picked award is $name from ".join(', ',@names));
                    367:     return($ad,$msg, $name);
                    368: }
                    369: 
                    370: sub add_in_tag_answer {
                    371:     my ($parstack,$safeeval,$response_level) = @_;
                    372:     my @answer=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval,
                    373: 					      $response_level);
                    374:     &Apache::lonxml::debug('answer is'.join(':',@answer));
                    375:     if (@answer && defined($answer[0])) {
1.195     albertel  376: 	$answer{$tag_internal_answer_name}= {'type' => 'ordered',
                    377: 					     'answers' => [\@answer] };
1.194     albertel  378:     }
1.167     albertel  379: }
                    380: 
1.202     www       381: sub capa_formula_fix {
                    382:    my ($expression)=@_;
                    383:    return &Apache::response::implicit_multiplication($expression);
                    384: }
                    385: 
1.89      albertel  386: sub end_numericalresponse {
                    387:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.90      albertel  388:     my $increment=1;
1.89      albertel  389:     my $result = '';
                    390:     if (!$Apache::lonxml::default_homework_loaded) {
                    391: 	&Apache::lonxml::default_homework_load($safeeval);
1.34      albertel  392:     }
1.167     albertel  393:     my $partid = $Apache::inputtags::part;
                    394:     my $id = $Apache::inputtags::response[-1];
1.125     albertel  395:     my $tag;
1.190     albertel  396:     my $safehole = new Safe::Hole;
1.168     albertel  397:     $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
1.190     albertel  398: 
1.125     albertel  399:     if (scalar(@$tagstack)) { $tag=$$tagstack[-1]; }
1.162     albertel  400:     if ( $target eq 'grade' && &Apache::response::submitted() ) {
1.140     albertel  401: 	&Apache::response::setup_params($tag,$safeeval);
1.95      albertel  402: 	if ($Apache::lonhomework::type eq 'exam' && 
1.190     albertel  403: 	    (($tag eq 'formularesponse') || ($tag eq 'mathresponse'))) {
1.95      albertel  404: 	    $increment=&Apache::response::scored_response($partid,$id);
                    405: 	} else {
                    406: 	    my $response = &Apache::response::getresponse();
                    407: 	    if ( $response =~ /[^\s]/) {
                    408: 		my %previous = &Apache::response::check_for_previous($response,$partid,$id);
                    409: 		&Apache::lonxml::debug("submitted a $response<br>\n");
                    410: 		&Apache::lonxml::debug($$parstack[-1] . "\n<br>");
                    411: 		
1.162     albertel  412: 		if ( &Apache::response::submitted('scantron')) {
1.184     albertel  413: 		    my ($values,$display)=&make_numerical_bubbles($partid,$id,
                    414: 						  $target,$parstack,$safeeval);
1.158     albertel  415: 		    $response=$values->[$response];
1.95      albertel  416: 		}
1.115     albertel  417: 		$Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
1.194     albertel  418: 		my ($ad,$msg,$name)=&check_submission($response,$partid,$id,
                    419: 						      $tag,$parstack,
                    420: 						      $safeeval);
1.167     albertel  421: 
1.142     albertel  422: 		&Apache::lonxml::debug('ad is'.$ad);
                    423: 		if ($ad eq 'SIG_FAIL') {
                    424: 		    my ($sig_u,$sig_l)=
                    425: 			&get_sigrange($Apache::inputtags::params{'sig'});
                    426: 		    $msg=join(':',$msg,$sig_l,$sig_u);
                    427: 		    &Apache::lonxml::debug("sigs bad $sig_u $sig_l ".
                    428: 					   $Apache::inputtags::params{'sig'});
                    429: 		}
1.95      albertel  430: 		&Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
1.163     albertel  431: 		if ($Apache::lonhomework::type eq 'survey' &&
                    432: 		    ($ad eq 'INCORRECT' || $ad eq 'APPROX_ANS' ||
                    433: 		     $ad eq 'EXACT_ANS')) {
                    434: 		    $ad='SUBMITTED';
                    435: 		}
1.95      albertel  436: 		&Apache::response::handle_previous(\%previous,$ad);
                    437: 		$Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
1.142     albertel  438: 		$Apache::lonhomework::results{"resource.$partid.$id.awardmsg"}=$msg;
1.194     albertel  439: 		$Apache::lonhomework::results{"resource.$partid.$id.answername"}=$name;
1.95      albertel  440: 		$result='';
1.90      albertel  441: 	    }
1.89      albertel  442: 	}
                    443:     } elsif ($target eq 'web' || $target eq 'tex') {
1.196     albertel  444: 	&check_for_answer_errors($parstack,$safeeval);
1.89      albertel  445: 	my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
                    446: 	my $status = $Apache::inputtags::status['-1'];
                    447: 	if ($Apache::lonhomework::type eq 'exam') {
1.194     albertel  448: 	    # FIXME support multi dimensional numerical problems
                    449:             #       in exam bubbles
1.184     albertel  450: 	    my ($bubble_values,$bubble_display)=
                    451: 		&make_numerical_bubbles($partid,$id,$target,$parstack,
                    452: 					$safeeval);
                    453: 	    my $number_of_bubbles = scalar(@{ $bubble_values });
1.89      albertel  454: 	    my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
                    455: 						    $safeeval);
                    456: 	    my @alphabet=('A'..'Z');
                    457: 	    if ($target eq 'web') {
1.125     albertel  458: 		if ($tag eq 'numericalresponse') {
1.89      albertel  459: 		    if ($unit=~/\S/) {$result.=' (in '.$unit.')<br /><br />';}
                    460: 		    $result.= '<table border="1"><tr>';
1.116     albertel  461: 		    my $previous=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.$id.submission"};
1.90      albertel  462: 		    for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
1.116     albertel  463: 			my $checked='';
1.158     albertel  464: 			if ($previous eq $bubble_values->[$ind]) {
1.116     albertel  465: 			    $checked=" checked='on' ";
                    466: 			}
1.90      albertel  467: 			$result.='<td><input type="radio" name="HWVAL_'.$id.
1.158     albertel  468: 			    '" value="'.$bubble_values->[$ind].'" '.$checked
1.116     albertel  469: 			    .' /><b>'.$alphabet[$ind].'</b>: '.
1.158     albertel  470: 			    $bubble_display->[$ind].'</td>';
1.89      albertel  471: 		    }
                    472: 		    $result.='</tr></table>';
                    473: 		}
                    474: 	    } elsif ($target eq 'tex') {
1.107     sakharuk  475: 		if ((defined $unit) and ($unit=~/\S/) and ($Apache::lonhomework::type eq 'exam')) {
1.89      albertel  476: 		    $result.=' \textit{(in} \verb|'.$unit.'|\textit{)} ';
                    477: 		}
1.125     albertel  478: 		if ($tag eq 'numericalresponse') {
1.90      albertel  479: 		    my ($celllength,$number_of_tables,@table_range)=
1.159     albertel  480: 			&get_table_sizes($number_of_bubbles,$bubble_display);
1.89      albertel  481: 		    my $j=0;
                    482: 		    my $cou=0;
                    483: 		    $result.='\vskip -1 mm \noindent \begin{enumerate}\item[\textbf{'.$Apache::lonxml::counter.'}.]';
                    484: 		    for (my $i=0;$i<$number_of_tables;$i++) {
1.144     sakharuk  485: 			$result.='\vskip -1 mm \noindent \setlength{\tabcolsep}{2 mm}\begin{tabular}{';
1.90      albertel  486: 			for (my $ind=0;$ind<$table_range[$j];$ind++) {
1.128     sakharuk  487: 			    $result.='p{3 mm}p{'.$celllength.' mm}';
1.89      albertel  488: 			}
                    489: 			$result.='}';
1.90      albertel  490: 			for (my $ind=$cou;$ind<$cou+$table_range[$j];$ind++) {
1.159     albertel  491: 			    $result.='\hskip -4 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$ & \hskip -3 mm {\small '.$bubble_display->[$ind].'} ';
1.89      albertel  492: 			    if ($ind != $cou+$table_range[$j]-1) {$result.=' & ';}
                    493: 			}
                    494: 			$cou += $table_range[$j];
                    495: 			$j++;
                    496: 			$result.='\\\\\end{tabular}\vskip 0 mm ';
                    497: 		    }
                    498: 		    $result.='\end{enumerate}';
                    499: 		} else {
1.188     albertel  500: 		    $increment = &Apache::response::repetition();
1.89      albertel  501: 		}
                    502: 	    }
                    503: 	}
                    504:     } elsif ($target eq 'edit') {
                    505: 	$result.='</td></tr>'.&Apache::edit::end_table;
                    506:     } elsif ($target eq 'answer' || $target eq 'analyze') {
1.167     albertel  507: 	my $part_id="$partid.$id";
1.89      albertel  508: 	if ($target eq 'analyze') {
                    509: 	    push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
1.125     albertel  510: 	    $Apache::lonhomework::analyze{"$part_id.type"} = $tag;
1.117     albertel  511: 	    my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
1.161     albertel  512: 	    if ($#incorrect eq 0) { @incorrect=(split(/,/,$incorrect[0])); }
1.117     albertel  513: 	    push (@{ $Apache::lonhomework::analyze{"$part_id.incorrect"} }, @incorrect);
1.154     albertel  514: 	    &Apache::response::check_if_computed($token,$parstack,
                    515: 						 $safeeval,'answer');
1.89      albertel  516: 	}
1.125     albertel  517: 	if (scalar(@$tagstack)) {
1.140     albertel  518: 	    &Apache::response::setup_params($tag,$safeeval);
1.125     albertel  519: 	}
1.194     albertel  520: 	&add_in_tag_answer($parstack,$safeeval);
1.58      sakharuk  521: 	my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
1.194     albertel  522: 
1.58      sakharuk  523: 	my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval);
1.89      albertel  524: 	
                    525: 	if ($target eq 'answer') {
1.195     albertel  526: 	    $result.=&Apache::response::answer_header($tag,undef,
                    527: 						      scalar(keys(%answer)));
1.185     albertel  528: 	    if ($tag eq 'numericalresponse'
                    529: 		&& $Apache::lonhomework::type eq 'exam') {
1.184     albertel  530: 		my ($bubble_values,undef,$correct) = &make_numerical_bubbles($partid,
                    531: 					     $id,$target,$parstack,$safeeval);
                    532: 		$result.=&Apache::response::answer_part($tag,$correct);
                    533: 	    }
1.89      albertel  534: 	}
1.194     albertel  535: 	foreach my $name (sort(keys(%answer))) {
                    536: 	    my @answers = @{ $answer{$name}{'answers'} };
1.200     albertel  537: 	    if ($target eq 'analyze') {
                    538: 		foreach my $info ('answer','ans_high','ans_low','format') {
                    539: 		    $Apache::lonhomework::analyze{"$part_id.$info"}{$name}=[];
                    540: 		}
                    541: 	    }
1.194     albertel  542: 	    my ($sigline,$tolline);
1.195     albertel  543: 	    if ($name ne $tag_internal_answer_name 
                    544: 		|| scalar(keys(%answer)) > 1) {
                    545: 		$result.=&Apache::response::answer_part($tag,$name);
                    546: 	    }
1.194     albertel  547: 	    for(my $i=0;$i<=$#answers;$i++) {
                    548: 		my $ans=$answers[$i];
                    549: 		my $fmt=$formats[0];
                    550: 		if (@formats && $#formats) {$fmt=$formats[$i];}
                    551: 		my ($sighigh,$siglow);
                    552: 		if ($Apache::inputtags::params{'sig'}) {
                    553: 		    ($sighigh,$siglow)=&get_sigrange($Apache::inputtags::params{'sig'});
                    554: 		}
1.195     albertel  555: 		my @vector;
                    556: 		if (ref($ans)) {
                    557: 		    @vector = @{ $ans };
                    558: 		} else {
                    559: 		    @vector = ($ans);
1.194     albertel  560: 		}
1.200     albertel  561: 		my @all_answer_info;
                    562: 		foreach my $element (@vector) {
                    563: 		    my ($high,$low);
                    564: 		    if ($Apache::inputtags::params{'tol'}) {
                    565: 			($high,$low)=&get_tolrange($element,$Apache::inputtags::params{'tol'});
                    566: 		    }
                    567: 		    if ($target eq 'answer') {
1.195     albertel  568: 			if ($fmt && $tag eq 'numericalresponse') {
                    569: 			    $fmt=~s/e/E/g;
                    570: 			    if ($unit=~/\$/) { $fmt="\$".$fmt; $unit=~s/\$//g; }
                    571: 			    if ($unit=~/\,/) { $fmt="\,".$fmt; $unit=~s/\,//g; }
                    572: 			    $element = &format_number($element,$fmt,$target,$safeeval);
                    573: 			    #if ($high) {
                    574: 			    #    $high=&format_number($high,$fmt,$target,$safeeval);
                    575: 			    #    $low =&format_number($low,$fmt,$target,$safeeval);
                    576: 			    #}
                    577: 			}
                    578: 			if ($high && $tag eq 'numericalresponse') {
                    579: 			    $element.=' ['.$low.','.$high.']';
                    580: 			    $tolline .= "[$low, $high]";
                    581: 			}
                    582: 			if (defined($sighigh) && $tag eq 'numericalresponse') {
                    583: 			    if ($env{'form.answer_output_mode'} eq 'tex') {
                    584: 				$element.= " Sig $siglow - $sighigh";
                    585: 			    } else {
                    586: 				$element.= " Sig <i>$siglow - $sighigh</i>";
                    587: 				$sigline .= "[$siglow, $sighigh]";
                    588: 			    }
1.194     albertel  589: 			}
1.195     albertel  590: 			push(@all_answer_info,$element);
1.200     albertel  591: 			
                    592: 		    } elsif ($target eq 'analyze') {
                    593: 			push (@{ $Apache::lonhomework::analyze{"$part_id.answer"}{$name}[$i] }, $element);
                    594: 			if ($high) {
                    595: 			    push (@{ $Apache::lonhomework::analyze{"$part_id.ans_high"}{$name}[$i] }, $high);
                    596: 			    push (@{ $Apache::lonhomework::analyze{"$part_id.ans_low"}{$name}[$i] }, $low);
                    597: 			}
                    598: 			if ($fmt) {
                    599: 			    push (@{ $Apache::lonhomework::analyze{"$part_id.format"}{$name}[$i] }, $fmt);
                    600: 			}
1.194     albertel  601: 		    }
1.200     albertel  602: 		}
                    603: 		if ($target eq 'answer') {
1.195     albertel  604: 		    $result.= &Apache::response::answer_part($tag,join(', ',@all_answer_info));
1.194     albertel  605: 		}
                    606: 	    }
                    607: 
                    608: 	    my @fmt_ans;
                    609: 	    for(my $i=0;$i<=$#answers;$i++) {
                    610: 		my $ans=$answers[$i];
                    611: 		my $fmt=$formats[0];
                    612: 		if (@formats && $#formats) {$fmt=$formats[$i];}
1.195     albertel  613: 		foreach my $element (@$ans) {		    
                    614: 		    if ($fmt && $tag eq 'numericalresponse') {
                    615: 			$fmt=~s/e/E/g;
                    616: 			if ($unit=~/\$/) { $fmt="\$".$fmt; $unit=~s/\$//g; }
                    617: 			if ($unit=~/\,/) { $fmt="\,".$fmt; $unit=~s/\,//g; }
                    618: 			$element = &format_number($element,$fmt,$target,
                    619: 						  $safeeval);
                    620: 			if ($fmt=~/\$/ && $unit!~/\$/) { $element=~s/\$//; }
                    621: 		    }
1.194     albertel  622: 		}
1.195     albertel  623: 		push(@fmt_ans,join(',',@$ans));
1.194     albertel  624: 	    }
1.195     albertel  625: 	    my $response=\@fmt_ans;
                    626: 
1.194     albertel  627: 	    my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.
                    628: 					      $id.'.turnoffunit');
                    629: 	    if ($unit ne ''  && 
                    630: 		! ($Apache::lonhomework::type eq 'exam' ||
                    631: 		   lc($hideunit) eq "yes") )  {
                    632: 		my $cleanunit=$unit;
                    633: 		$cleanunit=~s/\$\,//g;
1.195     albertel  634: 		foreach my $ans (@fmt_ans) {
                    635: 		    $ans.=" $cleanunit";
                    636: 		}
1.60      sakharuk  637: 	    }
1.194     albertel  638: 	    my ($ad,$msg)=&check_submission($response,$partid,$id,$tag,
                    639: 					    $parstack,$safeeval);
                    640: 	    if ($ad ne 'EXACT_ANS' && $ad ne 'APPROX_ANS') {
                    641: 		my $error;
                    642: 		if ($tag eq 'formularesponse') {
1.195     albertel  643: 		    $error=&mt('Computer\'s answer is incorrect ("[_1]").',join(', ',@$response));
1.194     albertel  644: 		} else {
                    645: 		    # answer failed check if it is sig figs that is failing
                    646: 		    my ($ad,$msg)=&check_submission($response,$partid,$id,
                    647: 						    $tag,$parstack,
                    648: 						    $safeeval,1);
                    649: 		    if ($sigline ne '') {
1.195     albertel  650: 			$error=&mt('Computer\'s answer is incorrect ("[_1]"). It is likely that the tolerance range [_2] or significant figures [_3] need to be adjusted.',join(', ',@$response),$tolline,$sigline);
1.98      sakharuk  651: 		    } else {
1.195     albertel  652: 			$error=&mt('Computer\'s answer is incorrect ("[_1]"). It is likely that the tolerance range [_2] needs to be adjusted.',join(', ',@$response),$tolline);
1.98      sakharuk  653: 		    }
                    654: 		}
1.194     albertel  655: 		if ($ad ne 'EXACT_ANS' && $ad ne 'APPROX_ANS') {
                    656: 		    &Apache::lonxml::error($error);
                    657: 		} else {
                    658: 		    &Apache::lonxml::warning($error);
1.165     albertel  659: 		}
1.79      sakharuk  660: 	    }
1.176     albertel  661: 
1.194     albertel  662: 	    if (defined($unit) and ($unit ne '') and
                    663: 		$tag eq 'numericalresponse') {
                    664: 		if ($target eq 'answer') {
                    665: 		    if ($env{'form.answer_output_mode'} eq 'tex') {
                    666: 			$result.=&Apache::response::answer_part($tag,
                    667: 								" Unit: $unit ");
                    668: 		    } else {
                    669: 			$result.=&Apache::response::answer_part($tag,
                    670: 								"Unit: <b>$unit</b>");
                    671: 		    }
                    672: 		} elsif ($target eq 'analyze') {
                    673: 		    push (@{ $Apache::lonhomework::analyze{"$part_id.unit"} }, $unit);
1.171     albertel  674: 		}
1.167     albertel  675: 	    }
1.194     albertel  676: 	    if ($tag eq 'formularesponse' && $target eq 'answer') {
                    677: 		my $samples=&Apache::lonxml::get_param('samples',$parstack,$safeeval);
                    678: 		$result.=&Apache::response::answer_part($tag,$samples);
1.89      albertel  679: 	    }
1.195     albertel  680: 	    $result.=&Apache::response::next_answer($tag,$name);
1.89      albertel  681: 	}
                    682: 	if ($target eq 'answer') {
1.125     albertel  683: 	    $result.=&Apache::response::answer_footer($tag);
1.89      albertel  684: 	}
1.69      sakharuk  685:     }
1.121     sakharuk  686:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
1.90      albertel  687: 	$target eq 'tex' || $target eq 'analyze') {
                    688: 	&Apache::lonxml::increment_counter($increment);
                    689:     }
1.196     albertel  690:     &Apache::response::end_response();
1.89      albertel  691:     return $result;
1.90      albertel  692: }
                    693: 
1.196     albertel  694: sub check_for_answer_errors {
                    695:     my ($parstack,$safeeval) = @_;
                    696:     &add_in_tag_answer($parstack,$safeeval);
                    697:     my %counts;
                    698:     foreach my $name (keys(%answer)) {
                    699: 	push(@{$counts{scalar(@{$answer{$name}{'answers'}})}},$name);
                    700:     }
                    701:     if (scalar(keys(%counts)) > 1) {
                    702: 	my $counts = join(' ',map {
                    703: 	    my $count = $_;
                    704: 	    &mt("Answers [_1] had [_2] components.",
                    705: 		'<tt>'.join(', ',@{$counts{$count}}).'</tt>',
                    706: 		$count);
                    707: 	} (sort(keys(%counts))));
                    708: 	&Apache::lonxml::error(&mt("All answers must have the same number of components. Varying numbers of answers were seen. ").$counts);
                    709:     }
                    710:     use Data::Dumper;
                    711:     &Apache::lonxml::debug("count dump is ".&Dumper(\%counts));
                    712:     my $expected_number_of_inputs = (keys(%counts))[0];
                    713:     if ( $expected_number_of_inputs != scalar(@Apache::inputtags::inputlist)) {
                    714: 	&Apache::lonxml::error(&mt("Expected [_1] input fields, but there were only [_2] seen.", 
                    715: 				   $expected_number_of_inputs,
                    716: 				   scalar(@Apache::inputtags::inputlist)));
                    717:     }
                    718: }
                    719: 
1.90      albertel  720: sub get_table_sizes {
1.128     sakharuk  721:     my ($number_of_bubbles,$rbubble_values)=@_;
                    722:     my $scale=2; #mm for one digit
                    723:     my $cell_width=0;
                    724:     foreach my $member (@$rbubble_values) {
                    725: 	my $cell_width_real=0;
1.160     albertel  726: 	if ($member=~/(\+|-)?(\d*)\.?(\d*)\s*\$?\\times\s*10\^{(\+|-)?(\d+)}\$?/) {
1.138     sakharuk  727: 	    $cell_width_real=(length($2)+length($3)+length($5)+7)*$scale;
                    728: 	} elsif ($member=~/(\d*)\.?(\d*)(E|e)(\+|-)?(\d*)/) {
1.128     sakharuk  729: 	    $cell_width_real=(length($1)+length($2)+length($5)+9)*$scale;
1.138     sakharuk  730:         } elsif ($member=~/(\d*)\.(\d*)/) {
1.132     sakharuk  731: 	    $cell_width_real=(length($1)+length($2)+3)*$scale;
1.128     sakharuk  732: 	} else {
1.138     sakharuk  733: 	    $cell_width_real=(length($member)+1)*$scale*0.9;
1.128     sakharuk  734: 	}
                    735: 	if ($cell_width_real>$cell_width) {$cell_width=$cell_width_real;}
1.90      albertel  736:     }
1.132     sakharuk  737:     $cell_width+=8; 
1.130     sakharuk  738:     my $textwidth;
1.166     albertel  739:     if ($env{'form.textwidth'} ne '') {
                    740: 	$env{'form.textwidth'}=~/(\d*)\.?(\d*)/;
1.132     sakharuk  741: 	$textwidth=$1.'.'.$2;
1.130     sakharuk  742:     } else {
1.166     albertel  743: 	$env{'form.textwidth'}=~/(\d+)\.?(\d*)/;
1.132     sakharuk  744: 	$textwidth=$1.'.'.$2;
1.130     sakharuk  745:     }
1.128     sakharuk  746:     my $bubbles_per_line=int($textwidth/$cell_width);
1.151     sakharuk  747:     if ($bubbles_per_line > $number_of_bubbles) {
                    748: 	$bubbles_per_line=$number_of_bubbles;
                    749:     }elsif (($bubbles_per_line > $number_of_bubbles/2) && ($number_of_bubbles % 2==0)) {$bubbles_per_line=$number_of_bubbles/2;}
1.128     sakharuk  750:     my $number_of_tables = int($number_of_bubbles/$bubbles_per_line);
1.90      albertel  751:     my @table_range = ();
1.128     sakharuk  752:     for (my $i=0;$i<$number_of_tables;$i++) {push @table_range,$bubbles_per_line;}
                    753:     if ($number_of_bubbles % $bubbles_per_line) {
1.90      albertel  754: 	$number_of_tables++;
1.128     sakharuk  755: 	push @table_range,($number_of_bubbles % $bubbles_per_line);
1.90      albertel  756:     }
1.128     sakharuk  757:     $cell_width-=8;
1.136     sakharuk  758:     $cell_width=$cell_width*3/4;
1.128     sakharuk  759:     return ($cell_width,$number_of_tables,@table_range);
1.90      albertel  760: }
                    761: 
                    762: sub format_number {
1.153     albertel  763:     my ($number,$format,$target,$safeeval)=@_;
1.90      albertel  764:     my $ans;
1.153     albertel  765:     if ($format eq '') {
1.90      albertel  766: 	#What is the number? (integer,decimal,floating point)
1.187     albertel  767: 	if ($number=~/^(\d*\.?\d*)(E|e)[+\-]?(\d*)$/) {
1.113     sakharuk  768: 	    $format = '3e';
1.90      albertel  769: 	} elsif ($number=~/^(\d*)\.(\d*)$/) {
                    770: 	    $format = '4f';
                    771: 	} elsif ($number=~/^(\d*)$/) {
                    772: 	    $format = 'd';
                    773: 	}
                    774:     }
1.156     albertel  775:     if (!$Apache::lonxml::default_homework_loaded) {
                    776: 	&Apache::lonxml::default_homework_load($safeeval);
                    777:     }
1.153     albertel  778:     $ans=&Apache::run::run("&prettyprint(q\0$number\0,q\0$format\0,q\0$target\0)",$safeeval);
1.90      albertel  779:     return $ans;
                    780: }
                    781: 
                    782: sub make_numerical_bubbles {
1.184     albertel  783:     my ($part,$id,$target,$parstack,$safeeval) =@_;
                    784:     
                    785:     my $number_of_bubbles = 
                    786: 	&Apache::response::get_response_param($part.'_'.$id,'numbubbles',8);
                    787: 
                    788:     my ($format)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
1.201     albertel  789:     my $name = (exists($answer{$tag_internal_answer_name}) 
                    790: 		? $tag_internal_answer_name
                    791: 		: (sort(keys(%answer)))[0]);
                    792: 
                    793:     if ( scalar(@{$answer{$name}{'answers'}}) > 1) {
                    794: 	&Apache::lonxml::error("Only answers with 1 component are supported in exam mode");
                    795:     }
                    796:     if (scalar(@{$answer{$name}{'answers'}[0]}) > 1) {
                    797: 	&Apache::lonxml::error("Vector answers are unsupported in exam mode.");
                    798:     }
                    799: 
                    800:     my $answer = $answer{$name}{'answers'}[0][0];
1.184     albertel  801:     my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,
                    802: 						   $safeeval);
                    803:     if ($#incorrect eq 0) { @incorrect=(split(/,/,$incorrect[0])); }
                    804:     
1.158     albertel  805:     my @bubble_values=();
1.184     albertel  806:     my @alphabet=('A'..'Z');
                    807: 
                    808:     &Apache::lonxml::debug("answer is $answer incorrect is @incorrect");
1.119     albertel  809:     my @oldseed=&Math::Random::random_get_seed();
1.184     albertel  810:     if (@incorrect) {
                    811: 	&Apache::lonxml::debug("inside ".(scalar(@incorrect)+1 gt $number_of_bubbles));
                    812: 	if (defined($incorrect[0]) &&
                    813: 	    scalar(@incorrect)+1 >= $number_of_bubbles) {
                    814: 	    &Apache::lonxml::debug("inside ".(scalar(@incorrect)+1).":$number_of_bubbles");
1.117     albertel  815: 	    &Apache::response::setrandomnumber();
1.184     albertel  816: 	    my @rand_inc=&Math::Random::random_permutation(@incorrect);
1.117     albertel  817: 	    @bubble_values=@rand_inc[0..($number_of_bubbles-2)];
                    818: 	    @bubble_values=sort {$a <=> $b} (@bubble_values,$answer);
1.184     albertel  819: 	    &Apache::lonxml::debug("Answer was :$answer: returning :".$#bubble_values.": which are :".join(':',@bubble_values));
1.119     albertel  820: 	    &Math::Random::random_set_seed(@oldseed);
1.184     albertel  821: 
                    822: 	    my $correct;
                    823: 	    for(my $i=0; $i<=$#bubble_values;$i++) {
                    824: 		if ($bubble_values[$i] eq $answer) {
                    825: 		    $correct = $alphabet[$i];
                    826: 		    last;
                    827: 		}
                    828: 	    }
                    829: 
1.134     albertel  830: 	    if (defined($format) && $format ne '') {
1.158     albertel  831: 		my @bubble_display;
1.137     albertel  832: 		foreach my $value (@bubble_values) {
1.158     albertel  833: 		    push(@bubble_display,
                    834: 			 &format_number($value,$format,$target,$safeeval));
1.134     albertel  835: 		}
1.184     albertel  836: 		return (\@bubble_values,\@bubble_display,$correct);
1.158     albertel  837: 	    } else {
1.184     albertel  838: 		return (\@bubble_values,\@bubble_values,$correct);
1.134     albertel  839: 	    }
1.117     albertel  840: 	}
1.184     albertel  841: 	if (defined($incorrect[0]) &&
                    842: 	    scalar(@incorrect)+1 < $number_of_bubbles) {
                    843: 	    &Apache::lonxml::warning("Not enough incorrect answers were specified in the incorrect array, ignoring the specified incorrect answers and instead generating them (".join(',',@incorrect).").");
1.127     albertel  844: 	}
1.117     albertel  845:     }
1.90      albertel  846:     my @factors = (1.13,1.17,1.25,1.33,1.45); #default values of factors
1.126     albertel  847:     my @powers = (1..$number_of_bubbles);
1.90      albertel  848:     &Apache::response::setrandomnumber();
                    849:     my $ind=&Math::Random::random_uniform_integer(1,0,$#powers);
                    850:     my $power = $powers[$ind];
                    851:     $ind=&Math::Random::random_uniform_integer(1,0,$#factors);
                    852:     my $factor = $factors[$ind];
1.158     albertel  853:     my @bubble_display;
1.90      albertel  854:     for ($ind=0;$ind<$number_of_bubbles;$ind++) {
1.91      albertel  855: 	$bubble_values[$ind] = $answer*($factor**($power-$powers[$#powers-$ind]));
1.158     albertel  856: 	$bubble_display[$ind] = &format_number($bubble_values[$ind],
1.153     albertel  857: 					       $format,$target,$safeeval);
1.91      albertel  858: 
1.90      albertel  859:     }
1.184     albertel  860:     my $correct = $alphabet[$number_of_bubbles-$power];
1.119     albertel  861:     &Math::Random::random_set_seed(@oldseed);
1.184     albertel  862:     return (\@bubble_values,\@bubble_display,$correct);
1.51      albertel  863: }
                    864: 
                    865: sub get_tolrange {
1.89      albertel  866:     my ($ans,$tol)=@_;
                    867:     my ($high,$low);
                    868:     if ($tol =~ /%$/) {
                    869: 	chop($tol);
                    870: 	my $change=$ans*($tol/100.0);
                    871: 	$high=$ans+$change;
                    872: 	$low=$ans-$change;
                    873:     } else {
                    874: 	$high=$ans+$tol;
                    875: 	$low=$ans-$tol;
                    876:     }
                    877:     return ($high,$low);
1.52      albertel  878: }
                    879: 
                    880: sub get_sigrange {
1.89      albertel  881:     my ($sig)=@_;
1.195     albertel  882:     #&Apache::lonxml::debug("Got a sig of :$sig:");
1.166     albertel  883:     my $courseid=$env{'request.course.id'};
                    884:     if (lc($env{"course.$courseid.disablesigfigs"}) eq 'yes') {
1.147     albertel  885: 	return (15,0);
                    886:     }
1.89      albertel  887:     my $sig_lbound;
                    888:     my $sig_ubound;
                    889:     if ($sig eq '') {
                    890: 	$sig_lbound = 0; #SIG_LB_DEFAULT
                    891: 	$sig_ubound =15; #SIG_UB_DEFAULT
                    892:     } else {
                    893: 	($sig_lbound,$sig_ubound) = split(/,/,$sig);
1.145     albertel  894: 	if (!defined($sig_lbound)) {
1.89      albertel  895: 	    $sig_lbound = 0; #SIG_LB_DEFAULT
                    896: 	    $sig_ubound =15; #SIG_UB_DEFAULT
                    897: 	}
1.145     albertel  898: 	if (!defined($sig_ubound)) { $sig_ubound=$sig_lbound; }
1.133     albertel  899:     }
                    900:     if (($sig_ubound<$sig_lbound) ||
                    901: 	($sig_lbound > 15) ||
                    902: 	($sig =~/(\+|-)/ ) ) {
                    903: 	my $errormsg=&mt("Invalid Significant figures detected")." ($sig)";
1.166     albertel  904: 	if ($env{'request.state'} eq 'construct') {
1.133     albertel  905: 	    $errormsg.=
                    906: 		&Apache::loncommon::help_open_topic('Significant_Figures');
                    907: 	}
                    908: 	&Apache::lonxml::error($errormsg);
1.52      albertel  909:     }
1.89      albertel  910:     return ($sig_ubound,$sig_lbound);
1.34      albertel  911: }
                    912: 
                    913: sub start_stringresponse {
1.89      albertel  914:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    915:     my $result;
1.122     albertel  916:     my $id = &Apache::response::start_response($parstack,$safeeval);
1.89      albertel  917:     if ($target eq 'meta') {
                    918: 	$result=&Apache::response::meta_package_write('stringresponse');
1.122     albertel  919:     } elsif ($target eq 'edit') {
                    920: 	$result.=&Apache::edit::tag_start($target,$token);
                    921: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
                    922: 	$result.=&Apache::edit::select_arg('Type:','type',
                    923: 			 [['cs','Case Sensitive'],['ci','Case Insensitive'],
                    924: 			  ['mc','Case Insensitive, Any Order'],
                    925: 			  ['re','Regular Expression']],$token);
1.152     albertel  926: 	$result.=&Apache::edit::text_arg('String to display for answer:',
                    927: 					 'answerdisplay',$token);
1.122     albertel  928: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    929:     } elsif ($target eq 'modified') {
1.146     albertel  930: 	my $constructtag;
                    931: 	$constructtag=&Apache::edit::get_new_args($token,$parstack,
                    932: 						  $safeeval,'answer',
                    933: 						  'type','answerdisplay');
                    934: 	if ($constructtag) {
                    935: 	    $result = &Apache::edit::rebuild_tag($token);
                    936: 	    $result.=&Apache::edit::handle_insert();
                    937: 	}
                    938:     } elsif ($target eq 'web') {
                    939: 	if (  &Apache::response::show_answer() ) {
1.152     albertel  940: 	    my $answer=
                    941: 	       &Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval);
                    942: 	    if (!defined $answer || $answer eq '') {
                    943: 		$answer=
                    944: 		    &Apache::lonxml::get_param('answer',$parstack,$safeeval);
                    945: 	    }
1.195     albertel  946: 	    $Apache::inputtags::answertxt{$id}=[$answer];
1.146     albertel  947: 	} 
1.122     albertel  948:     } elsif ($target eq 'answer' || $target eq 'grade') {
                    949: 	&Apache::response::reset_params();
1.89      albertel  950:     }
                    951:     return $result;
1.34      albertel  952: }
                    953: 
                    954: sub end_stringresponse {
1.122     albertel  955:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.189     albertel  956: 
1.122     albertel  957:     my $result = '';
                    958:     my $part=$Apache::inputtags::part;
                    959:     my $id=$Apache::inputtags::response[-1];
                    960:     my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
                    961:     my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
1.129     www       962:     my $answerdisplay=&Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval);
1.122     albertel  963:     &Apache::lonxml::debug("current $answer ".$token->[2]);
                    964:     if (!$Apache::lonxml::default_homework_loaded) {
                    965: 	&Apache::lonxml::default_homework_load($safeeval);
                    966:     }
1.162     albertel  967:     if ( $target eq 'grade' && &Apache::response::submitted() ) {
1.140     albertel  968: 	&Apache::response::setup_params('stringresponse',$safeeval);
1.122     albertel  969: 	$safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
                    970: 	if ($Apache::lonhomework::type eq 'exam' ||
1.162     albertel  971: 	    &Apache::response::submitted('scantron')) {
1.189     albertel  972: 	    &Apache::response::scored_response($part,$id);
                    973: 
1.122     albertel  974: 	} else {
                    975: 	    my $response = &Apache::response::getresponse();
                    976: 	    if ( $response =~ /[^\s]/) {
                    977: 		my %previous = &Apache::response::check_for_previous($response,
                    978: 								    $part,$id);
                    979: 		&Apache::lonxml::debug("submitted a $response<br>\n");
                    980: 		&Apache::lonxml::debug($$parstack[-1] . "\n<br>");
                    981: 		$Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    982: 		    $response;
1.142     albertel  983: 		my ($ad,$msg);
1.122     albertel  984: 		if ($type eq 're' ) { 
                    985: 		    # if the RE wasn't in a var it likely got munged,
                    986:                     # thus grab it from the var directly
                    987: #		    my $testans=$token->[2]->{'answer'};
                    988: #		    if ($testans !~ m/^\s*\$/) {
                    989: #			$answer=$token->[2]->{'answer'};
                    990: #		    }
1.143     albertel  991: 		    ${$safeeval->varglob('LONCAPA::response')}=$response;
1.179     albertel  992: 		    $result = &Apache::run::run('if ($LONCAPA::response=~m'.$answer.') { return 1; } else { return 0; }',$safeeval);
1.122     albertel  993: 		    &Apache::lonxml::debug("current $response");
                    994: 		    &Apache::lonxml::debug("current $answer");
                    995: 		    $ad = ($result) ? 'APPROX_ANS' : 'INCORRECT';
                    996: 		} else {
1.195     albertel  997: 		    my @args = ('type');
                    998: 		    my $args_ref = &setup_capa_args($safeeval,$parstack,
                    999: 						    \@args,$response);
                   1000: 
                   1001: 		    &add_in_tag_answer($parstack,$safeeval);
                   1002: 		    my (@final_awards,@final_msgs,@names);
                   1003: 		    foreach my $name (keys(%answer)) {
                   1004: 			&Apache::lonxml::debug(" doing $name with ".join(':',@{ $answer{$name}{'answers'} }));
                   1005: 			${$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=dclone($answer{$name});
                   1006: 			my ($result, @msgs)=&Apache::run::run("&caparesponse_check_list()",$safeeval);
                   1007: 			&Apache::lonxml::debug('msgs are'.join(':',@msgs));
                   1008: 			my ($awards)=split(/:/,$result);
                   1009: 			my (@awards) = split(/,/,$awards);
                   1010: 			($ad,$msg) = 
                   1011: 			    &Apache::inputtags::finalizeawards(\@awards,\@msgs);
                   1012: 			push(@final_awards,$ad);
                   1013: 			push(@final_msgs,$msg);
                   1014: 			push(@names,$name);
                   1015: 			&Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
1.122     albertel 1016: 		    }
1.195     albertel 1017: 		    my ($ad, $msg, $name) = 
                   1018: 			&Apache::inputtags::finalizeawards(\@final_awards,
                   1019: 							   \@final_msgs,
                   1020: 							   \@names,1);
1.122     albertel 1021: 		}
1.163     albertel 1022: 		if ($Apache::lonhomework::type eq 'survey' &&
                   1023: 		    ($ad eq 'INCORRECT' || $ad eq 'APPROX_ANS' ||
                   1024: 		     $ad eq 'EXACT_ANS')) {
                   1025: 		    $ad='SUBMITTED';
                   1026: 		}
1.122     albertel 1027: 		&Apache::response::handle_previous(\%previous,$ad);
                   1028: 		$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
1.142     albertel 1029: 		$Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=$msg;
1.122     albertel 1030: 	    }
                   1031: 	}
                   1032:     } elsif ($target eq 'answer' || $target eq 'analyze') {
1.200     albertel 1033: 	&add_in_tag_answer($parstack,$safeeval);
1.122     albertel 1034: 	if ($target eq 'analyze') {
                   1035: 	    push (@{ $Apache::lonhomework::analyze{"parts"} },"$part.$id");
1.125     albertel 1036: 	    $Apache::lonhomework::analyze{"$part.$id.type"} = 'stringresponse';
1.154     albertel 1037: 	    &Apache::response::check_if_computed($token,$parstack,$safeeval,
                   1038: 						 'answer');
1.122     albertel 1039: 	}
1.140     albertel 1040: 	&Apache::response::setup_params('stringresponse',$safeeval);
1.122     albertel 1041: 	if ($target eq 'answer') {
1.125     albertel 1042: 	    $result.=&Apache::response::answer_header('stringresponse');
1.122     albertel 1043: 	}
1.200     albertel 1044: 	foreach my $name (keys(%answer)) {
                   1045: 	    my @answers = @{ $answer{$name}{'answers'} };
                   1046: 	    for (my $i=0;$i<=$#answers;$i++) {
                   1047: 		my $answer_part = $answers[$i];
                   1048: 		foreach my $element (@{$answer_part}) {
                   1049: 		    if ($target eq 'answer') {
                   1050: 			$result.=&Apache::response::answer_part('stringresponse',
                   1051: 								$element);
                   1052: 		    } elsif ($target eq 'analyze') {
                   1053: 			push (@{ $Apache::lonhomework::analyze{"$part.$id.answer"}{$name}[$i] },
                   1054: 			      $element);
                   1055: 		    }
                   1056: 		}
                   1057: 		if ($target eq 'answer' && $type eq 're') {
1.178     albertel 1058: 		    $result.=&Apache::response::answer_part('stringresponse',
                   1059: 							    $answerdisplay);
                   1060: 		}
1.122     albertel 1061: 	    }
1.200     albertel 1062: 	}
1.122     albertel 1063: 	my $string='Case Insensitive';
                   1064: 	if ($type eq 'mc') {
                   1065: 	    $string='Multiple Choice';
                   1066: 	} elsif ($type eq 'cs') {
                   1067: 	    $string='Case Sensitive';
                   1068: 	} elsif ($type eq 'ci') {
                   1069: 	    $string='Case Insensitive';
                   1070: 	} elsif ($type eq 're') {
                   1071: 	    $string='Regular Expression';
                   1072: 	}
                   1073: 	if ($target eq 'answer') {
1.166     albertel 1074: 	    if ($env{'form.answer_output_mode'} eq 'tex') {
1.125     albertel 1075: 		$result.=&Apache::response::answer_part('stringresponse',
1.122     albertel 1076: 							"$string");
                   1077: 	    } else {
1.125     albertel 1078: 		$result.=&Apache::response::answer_part('stringresponse',
1.122     albertel 1079: 							"<b>$string</b>");
                   1080: 	    }
                   1081: 	} elsif ($target eq 'analyze') {
                   1082: 	    push (@{$Apache::lonhomework::analyze{"$part.$id.str_type"}},
                   1083: 		  $type);
                   1084: 	}
                   1085: 	if ($target eq 'answer') {
1.125     albertel 1086: 	    $result.=&Apache::response::answer_footer('stringresponse');
1.122     albertel 1087: 	}
                   1088:     } elsif ($target eq 'edit') {
                   1089: 	$result.='</td></tr>'.&Apache::edit::end_table;
                   1090:     }
                   1091:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
                   1092: 	$target eq 'tex' || $target eq 'analyze') {
1.189     albertel 1093: 	&Apache::lonxml::increment_counter(&Apache::response::repetition());
1.122     albertel 1094:     }
                   1095:     &Apache::response::end_response;
                   1096:     return $result;
1.44      albertel 1097: }
                   1098: 
                   1099: sub start_formularesponse {
1.89      albertel 1100:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1101:     my $result;
                   1102:     if ($target eq 'meta') {
1.104     bowersj2 1103: 	&Apache::response::start_response($parstack,$safeeval);
1.89      albertel 1104: 	$result=&Apache::response::meta_package_write('formularesponse');
1.104     bowersj2 1105: 	&Apache::response::end_response();
1.89      albertel 1106:     } else {
                   1107: 	$result.=&start_numericalresponse(@_);
                   1108:     }
                   1109:     return $result;
1.44      albertel 1110: }
                   1111: 
                   1112: sub end_formularesponse {
1.89      albertel 1113:     return end_numericalresponse(@_);
1.3       albertel 1114: }
                   1115: 
1.1       albertel 1116: 1;
1.3       albertel 1117: __END__
1.89      albertel 1118: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>