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

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

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