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

1.3       albertel    1: # The LearningOnline Network with CAPA
                      2: # caparesponse definition
1.47      albertel    3: #
1.195   ! albertel    4: # $Id: caparesponse.pm,v 1.194 2006/09/29 20:55:36 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.193     albertel  327: #	if ($$args_ref{'samples'}) {
1.191     www       328: 	    $$args_ref{'type'}='fml';
1.193     albertel  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') {
                    434: 	my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
                    435: 	my $status = $Apache::inputtags::status['-1'];
                    436: 	if ($Apache::lonhomework::type eq 'exam') {
1.194     albertel  437: 	    # FIXME support multi dimensional numerical problems
                    438:             #       in exam bubbles
1.184     albertel  439: 	    my ($bubble_values,$bubble_display)=
                    440: 		&make_numerical_bubbles($partid,$id,$target,$parstack,
                    441: 					$safeeval);
                    442: 	    my $number_of_bubbles = scalar(@{ $bubble_values });
1.89      albertel  443: 	    my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
                    444: 						    $safeeval);
                    445: 	    my @alphabet=('A'..'Z');
                    446: 	    if ($target eq 'web') {
1.125     albertel  447: 		if ($tag eq 'numericalresponse') {
1.89      albertel  448: 		    if ($unit=~/\S/) {$result.=' (in '.$unit.')<br /><br />';}
                    449: 		    $result.= '<table border="1"><tr>';
1.116     albertel  450: 		    my $previous=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.$id.submission"};
1.90      albertel  451: 		    for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
1.116     albertel  452: 			my $checked='';
1.158     albertel  453: 			if ($previous eq $bubble_values->[$ind]) {
1.116     albertel  454: 			    $checked=" checked='on' ";
                    455: 			}
1.90      albertel  456: 			$result.='<td><input type="radio" name="HWVAL_'.$id.
1.158     albertel  457: 			    '" value="'.$bubble_values->[$ind].'" '.$checked
1.116     albertel  458: 			    .' /><b>'.$alphabet[$ind].'</b>: '.
1.158     albertel  459: 			    $bubble_display->[$ind].'</td>';
1.89      albertel  460: 		    }
                    461: 		    $result.='</tr></table>';
                    462: 		}
                    463: 	    } elsif ($target eq 'tex') {
1.107     sakharuk  464: 		if ((defined $unit) and ($unit=~/\S/) and ($Apache::lonhomework::type eq 'exam')) {
1.89      albertel  465: 		    $result.=' \textit{(in} \verb|'.$unit.'|\textit{)} ';
                    466: 		}
1.125     albertel  467: 		if ($tag eq 'numericalresponse') {
1.90      albertel  468: 		    my ($celllength,$number_of_tables,@table_range)=
1.159     albertel  469: 			&get_table_sizes($number_of_bubbles,$bubble_display);
1.89      albertel  470: 		    my $j=0;
                    471: 		    my $cou=0;
                    472: 		    $result.='\vskip -1 mm \noindent \begin{enumerate}\item[\textbf{'.$Apache::lonxml::counter.'}.]';
                    473: 		    for (my $i=0;$i<$number_of_tables;$i++) {
1.144     sakharuk  474: 			$result.='\vskip -1 mm \noindent \setlength{\tabcolsep}{2 mm}\begin{tabular}{';
1.90      albertel  475: 			for (my $ind=0;$ind<$table_range[$j];$ind++) {
1.128     sakharuk  476: 			    $result.='p{3 mm}p{'.$celllength.' mm}';
1.89      albertel  477: 			}
                    478: 			$result.='}';
1.90      albertel  479: 			for (my $ind=$cou;$ind<$cou+$table_range[$j];$ind++) {
1.159     albertel  480: 			    $result.='\hskip -4 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$ & \hskip -3 mm {\small '.$bubble_display->[$ind].'} ';
1.89      albertel  481: 			    if ($ind != $cou+$table_range[$j]-1) {$result.=' & ';}
                    482: 			}
                    483: 			$cou += $table_range[$j];
                    484: 			$j++;
                    485: 			$result.='\\\\\end{tabular}\vskip 0 mm ';
                    486: 		    }
                    487: 		    $result.='\end{enumerate}';
                    488: 		} else {
1.188     albertel  489: 		    $increment = &Apache::response::repetition();
1.89      albertel  490: 		}
                    491: 	    }
                    492: 	}
                    493:     } elsif ($target eq 'edit') {
                    494: 	$result.='</td></tr>'.&Apache::edit::end_table;
                    495:     } elsif ($target eq 'answer' || $target eq 'analyze') {
1.167     albertel  496: 	my $part_id="$partid.$id";
1.89      albertel  497: 	if ($target eq 'analyze') {
                    498: 	    push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
1.125     albertel  499: 	    $Apache::lonhomework::analyze{"$part_id.type"} = $tag;
1.117     albertel  500: 	    my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
1.161     albertel  501: 	    if ($#incorrect eq 0) { @incorrect=(split(/,/,$incorrect[0])); }
1.117     albertel  502: 	    push (@{ $Apache::lonhomework::analyze{"$part_id.incorrect"} }, @incorrect);
1.154     albertel  503: 	    &Apache::response::check_if_computed($token,$parstack,
                    504: 						 $safeeval,'answer');
1.89      albertel  505: 	}
1.125     albertel  506: 	if (scalar(@$tagstack)) {
1.140     albertel  507: 	    &Apache::response::setup_params($tag,$safeeval);
1.125     albertel  508: 	}
1.194     albertel  509: 	&add_in_tag_answer($parstack,$safeeval);
1.58      sakharuk  510: 	my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
1.194     albertel  511: 
1.58      sakharuk  512: 	my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval);
1.89      albertel  513: 	
                    514: 	if ($target eq 'answer') {
1.195   ! albertel  515: 	    $result.=&Apache::response::answer_header($tag,undef,
        !           516: 						      scalar(keys(%answer)));
1.185     albertel  517: 	    if ($tag eq 'numericalresponse'
                    518: 		&& $Apache::lonhomework::type eq 'exam') {
1.184     albertel  519: 		my ($bubble_values,undef,$correct) = &make_numerical_bubbles($partid,
                    520: 					     $id,$target,$parstack,$safeeval);
                    521: 		$result.=&Apache::response::answer_part($tag,$correct);
                    522: 	    }
1.89      albertel  523: 	}
1.194     albertel  524: 	foreach my $name (sort(keys(%answer))) {
                    525: 	    my @answers = @{ $answer{$name}{'answers'} };
                    526: 	    my ($sigline,$tolline);
1.195   ! albertel  527: 	    if ($name ne $tag_internal_answer_name 
        !           528: 		|| scalar(keys(%answer)) > 1) {
        !           529: 		$result.=&Apache::response::answer_part($tag,$name);
        !           530: 	    }
1.194     albertel  531: 	    for(my $i=0;$i<=$#answers;$i++) {
                    532: 		my $ans=$answers[$i];
                    533: 		my $fmt=$formats[0];
                    534: 		if (@formats && $#formats) {$fmt=$formats[$i];}
                    535: 		my ($sighigh,$siglow);
                    536: 		if ($Apache::inputtags::params{'sig'}) {
                    537: 		    ($sighigh,$siglow)=&get_sigrange($Apache::inputtags::params{'sig'});
                    538: 		}
1.195   ! albertel  539: 		my @vector;
        !           540: 		if (ref($ans)) {
        !           541: 		    @vector = @{ $ans };
        !           542: 		} else {
        !           543: 		    @vector = ($ans);
1.194     albertel  544: 		}
                    545: 		if ($target eq 'answer') {
1.195   ! albertel  546: 		    my @all_answer_info;
        !           547: 		    foreach my $element (@vector) {
        !           548: 			my ($high,$low);
        !           549: 			if ($Apache::inputtags::params{'tol'}) {
        !           550: 			    ($high,$low)=&get_tolrange($element,$Apache::inputtags::params{'tol'});
        !           551: 			}
        !           552: 			if ($fmt && $tag eq 'numericalresponse') {
        !           553: 			    $fmt=~s/e/E/g;
        !           554: 			    if ($unit=~/\$/) { $fmt="\$".$fmt; $unit=~s/\$//g; }
        !           555: 			    if ($unit=~/\,/) { $fmt="\,".$fmt; $unit=~s/\,//g; }
        !           556: 			    $element = &format_number($element,$fmt,$target,$safeeval);
        !           557: 			    #if ($high) {
        !           558: 			    #    $high=&format_number($high,$fmt,$target,$safeeval);
        !           559: 			    #    $low =&format_number($low,$fmt,$target,$safeeval);
        !           560: 			    #}
        !           561: 			}
        !           562: 			if ($high && $tag eq 'numericalresponse') {
        !           563: 			    $element.=' ['.$low.','.$high.']';
        !           564: 			    $tolline .= "[$low, $high]";
        !           565: 			}
        !           566: 			if (defined($sighigh) && $tag eq 'numericalresponse') {
        !           567: 			    if ($env{'form.answer_output_mode'} eq 'tex') {
        !           568: 				$element.= " Sig $siglow - $sighigh";
        !           569: 			    } else {
        !           570: 				$element.= " Sig <i>$siglow - $sighigh</i>";
        !           571: 				$sigline .= "[$siglow, $sighigh]";
        !           572: 			    }
1.194     albertel  573: 			}
1.195   ! albertel  574: 			push(@all_answer_info,$element);
1.194     albertel  575: 		    }
1.195   ! albertel  576: 		    $result.= &Apache::response::answer_part($tag,join(', ',@all_answer_info));
1.194     albertel  577: 		} elsif ($target eq 'analyze') {
1.195   ! albertel  578: 		    my ($high,$low);
        !           579: 		    if ($Apache::inputtags::params{'tol'}) {
        !           580: 			($high,$low)=&get_tolrange($ans,$Apache::inputtags::params{'tol'});
        !           581: 		    }
1.194     albertel  582: 		    push (@{ $Apache::lonhomework::analyze{"$part_id.answer"} }, $ans);
                    583: 		    if ($high) {
                    584: 			push (@{ $Apache::lonhomework::analyze{"$part_id.ans_high"} }, $high);
                    585: 			push (@{ $Apache::lonhomework::analyze{"$part_id.ans_low"} }, $low);
                    586: 		    }
                    587: 		    if ($fmt) {
                    588: 			push (@{ $Apache::lonhomework::analyze{"$part_id.format"} }, $fmt);
                    589: 		    }
                    590: 		}
                    591: 	    }
                    592: 
                    593: 	    my @fmt_ans;
                    594: 	    for(my $i=0;$i<=$#answers;$i++) {
                    595: 		my $ans=$answers[$i];
                    596: 		my $fmt=$formats[0];
                    597: 		if (@formats && $#formats) {$fmt=$formats[$i];}
1.195   ! albertel  598: 		foreach my $element (@$ans) {		    
        !           599: 		    if ($fmt && $tag eq 'numericalresponse') {
        !           600: 			$fmt=~s/e/E/g;
        !           601: 			if ($unit=~/\$/) { $fmt="\$".$fmt; $unit=~s/\$//g; }
        !           602: 			if ($unit=~/\,/) { $fmt="\,".$fmt; $unit=~s/\,//g; }
        !           603: 			$element = &format_number($element,$fmt,$target,
        !           604: 						  $safeeval);
        !           605: 			if ($fmt=~/\$/ && $unit!~/\$/) { $element=~s/\$//; }
        !           606: 		    }
1.194     albertel  607: 		}
1.195   ! albertel  608: 		push(@fmt_ans,join(',',@$ans));
1.194     albertel  609: 	    }
1.195   ! albertel  610: 	    my $response=\@fmt_ans;
        !           611: 
1.194     albertel  612: 	    my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.
                    613: 					      $id.'.turnoffunit');
                    614: 	    if ($unit ne ''  && 
                    615: 		! ($Apache::lonhomework::type eq 'exam' ||
                    616: 		   lc($hideunit) eq "yes") )  {
                    617: 		my $cleanunit=$unit;
                    618: 		$cleanunit=~s/\$\,//g;
1.195   ! albertel  619: 		foreach my $ans (@fmt_ans) {
        !           620: 		    $ans.=" $cleanunit";
        !           621: 		}
1.60      sakharuk  622: 	    }
1.194     albertel  623: 	    my ($ad,$msg)=&check_submission($response,$partid,$id,$tag,
                    624: 					    $parstack,$safeeval);
                    625: 	    if ($ad ne 'EXACT_ANS' && $ad ne 'APPROX_ANS') {
                    626: 		my $error;
                    627: 		if ($tag eq 'formularesponse') {
1.195   ! albertel  628: 		    $error=&mt('Computer\'s answer is incorrect ("[_1]").',join(', ',@$response));
1.194     albertel  629: 		} else {
                    630: 		    # answer failed check if it is sig figs that is failing
                    631: 		    my ($ad,$msg)=&check_submission($response,$partid,$id,
                    632: 						    $tag,$parstack,
                    633: 						    $safeeval,1);
                    634: 		    if ($sigline ne '') {
1.195   ! albertel  635: 			$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  636: 		    } else {
1.195   ! albertel  637: 			$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  638: 		    }
                    639: 		}
1.194     albertel  640: 		if ($ad ne 'EXACT_ANS' && $ad ne 'APPROX_ANS') {
                    641: 		    &Apache::lonxml::error($error);
                    642: 		} else {
                    643: 		    &Apache::lonxml::warning($error);
1.165     albertel  644: 		}
1.79      sakharuk  645: 	    }
1.176     albertel  646: 
1.194     albertel  647: 	    if (defined($unit) and ($unit ne '') and
                    648: 		$tag eq 'numericalresponse') {
                    649: 		if ($target eq 'answer') {
                    650: 		    if ($env{'form.answer_output_mode'} eq 'tex') {
                    651: 			$result.=&Apache::response::answer_part($tag,
                    652: 								" Unit: $unit ");
                    653: 		    } else {
                    654: 			$result.=&Apache::response::answer_part($tag,
                    655: 								"Unit: <b>$unit</b>");
                    656: 		    }
                    657: 		} elsif ($target eq 'analyze') {
                    658: 		    push (@{ $Apache::lonhomework::analyze{"$part_id.unit"} }, $unit);
1.171     albertel  659: 		}
1.167     albertel  660: 	    }
1.194     albertel  661: 	    if ($tag eq 'formularesponse' && $target eq 'answer') {
                    662: 		my $samples=&Apache::lonxml::get_param('samples',$parstack,$safeeval);
                    663: 		$result.=&Apache::response::answer_part($tag,$samples);
1.89      albertel  664: 	    }
1.195   ! albertel  665: 	    $result.=&Apache::response::next_answer($tag,$name);
1.89      albertel  666: 	}
                    667: 	if ($target eq 'answer') {
1.125     albertel  668: 	    $result.=&Apache::response::answer_footer($tag);
1.89      albertel  669: 	}
1.69      sakharuk  670:     }
1.121     sakharuk  671:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
1.90      albertel  672: 	$target eq 'tex' || $target eq 'analyze') {
                    673: 	&Apache::lonxml::increment_counter($increment);
                    674:     }
1.89      albertel  675:     &Apache::response::end_response;
                    676:     return $result;
1.90      albertel  677: }
                    678: 
                    679: sub get_table_sizes {
1.128     sakharuk  680:     my ($number_of_bubbles,$rbubble_values)=@_;
                    681:     my $scale=2; #mm for one digit
                    682:     my $cell_width=0;
                    683:     foreach my $member (@$rbubble_values) {
                    684: 	my $cell_width_real=0;
1.160     albertel  685: 	if ($member=~/(\+|-)?(\d*)\.?(\d*)\s*\$?\\times\s*10\^{(\+|-)?(\d+)}\$?/) {
1.138     sakharuk  686: 	    $cell_width_real=(length($2)+length($3)+length($5)+7)*$scale;
                    687: 	} elsif ($member=~/(\d*)\.?(\d*)(E|e)(\+|-)?(\d*)/) {
1.128     sakharuk  688: 	    $cell_width_real=(length($1)+length($2)+length($5)+9)*$scale;
1.138     sakharuk  689:         } elsif ($member=~/(\d*)\.(\d*)/) {
1.132     sakharuk  690: 	    $cell_width_real=(length($1)+length($2)+3)*$scale;
1.128     sakharuk  691: 	} else {
1.138     sakharuk  692: 	    $cell_width_real=(length($member)+1)*$scale*0.9;
1.128     sakharuk  693: 	}
                    694: 	if ($cell_width_real>$cell_width) {$cell_width=$cell_width_real;}
1.90      albertel  695:     }
1.132     sakharuk  696:     $cell_width+=8; 
1.130     sakharuk  697:     my $textwidth;
1.166     albertel  698:     if ($env{'form.textwidth'} ne '') {
                    699: 	$env{'form.textwidth'}=~/(\d*)\.?(\d*)/;
1.132     sakharuk  700: 	$textwidth=$1.'.'.$2;
1.130     sakharuk  701:     } else {
1.166     albertel  702: 	$env{'form.textwidth'}=~/(\d+)\.?(\d*)/;
1.132     sakharuk  703: 	$textwidth=$1.'.'.$2;
1.130     sakharuk  704:     }
1.128     sakharuk  705:     my $bubbles_per_line=int($textwidth/$cell_width);
1.151     sakharuk  706:     if ($bubbles_per_line > $number_of_bubbles) {
                    707: 	$bubbles_per_line=$number_of_bubbles;
                    708:     }elsif (($bubbles_per_line > $number_of_bubbles/2) && ($number_of_bubbles % 2==0)) {$bubbles_per_line=$number_of_bubbles/2;}
1.128     sakharuk  709:     my $number_of_tables = int($number_of_bubbles/$bubbles_per_line);
1.90      albertel  710:     my @table_range = ();
1.128     sakharuk  711:     for (my $i=0;$i<$number_of_tables;$i++) {push @table_range,$bubbles_per_line;}
                    712:     if ($number_of_bubbles % $bubbles_per_line) {
1.90      albertel  713: 	$number_of_tables++;
1.128     sakharuk  714: 	push @table_range,($number_of_bubbles % $bubbles_per_line);
1.90      albertel  715:     }
1.128     sakharuk  716:     $cell_width-=8;
1.136     sakharuk  717:     $cell_width=$cell_width*3/4;
1.128     sakharuk  718:     return ($cell_width,$number_of_tables,@table_range);
1.90      albertel  719: }
                    720: 
                    721: sub format_number {
1.153     albertel  722:     my ($number,$format,$target,$safeeval)=@_;
1.90      albertel  723:     my $ans;
1.153     albertel  724:     if ($format eq '') {
1.90      albertel  725: 	#What is the number? (integer,decimal,floating point)
1.187     albertel  726: 	if ($number=~/^(\d*\.?\d*)(E|e)[+\-]?(\d*)$/) {
1.113     sakharuk  727: 	    $format = '3e';
1.90      albertel  728: 	} elsif ($number=~/^(\d*)\.(\d*)$/) {
                    729: 	    $format = '4f';
                    730: 	} elsif ($number=~/^(\d*)$/) {
                    731: 	    $format = 'd';
                    732: 	}
                    733:     }
1.156     albertel  734:     if (!$Apache::lonxml::default_homework_loaded) {
                    735: 	&Apache::lonxml::default_homework_load($safeeval);
                    736:     }
1.153     albertel  737:     $ans=&Apache::run::run("&prettyprint(q\0$number\0,q\0$format\0,q\0$target\0)",$safeeval);
1.90      albertel  738:     return $ans;
                    739: }
                    740: 
                    741: sub make_numerical_bubbles {
1.184     albertel  742:     my ($part,$id,$target,$parstack,$safeeval) =@_;
                    743:     
                    744:     my $number_of_bubbles = 
                    745: 	&Apache::response::get_response_param($part.'_'.$id,'numbubbles',8);
                    746: 
                    747:     my ($format)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
                    748:     my ($answer)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
                    749:     my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,
                    750: 						   $safeeval);
                    751:     if ($#incorrect eq 0) { @incorrect=(split(/,/,$incorrect[0])); }
                    752:     
1.158     albertel  753:     my @bubble_values=();
1.184     albertel  754:     my @alphabet=('A'..'Z');
                    755: 
                    756:     &Apache::lonxml::debug("answer is $answer incorrect is @incorrect");
1.119     albertel  757:     my @oldseed=&Math::Random::random_get_seed();
1.184     albertel  758:     if (@incorrect) {
                    759: 	&Apache::lonxml::debug("inside ".(scalar(@incorrect)+1 gt $number_of_bubbles));
                    760: 	if (defined($incorrect[0]) &&
                    761: 	    scalar(@incorrect)+1 >= $number_of_bubbles) {
                    762: 	    &Apache::lonxml::debug("inside ".(scalar(@incorrect)+1).":$number_of_bubbles");
1.117     albertel  763: 	    &Apache::response::setrandomnumber();
1.184     albertel  764: 	    my @rand_inc=&Math::Random::random_permutation(@incorrect);
1.117     albertel  765: 	    @bubble_values=@rand_inc[0..($number_of_bubbles-2)];
                    766: 	    @bubble_values=sort {$a <=> $b} (@bubble_values,$answer);
1.184     albertel  767: 	    &Apache::lonxml::debug("Answer was :$answer: returning :".$#bubble_values.": which are :".join(':',@bubble_values));
1.119     albertel  768: 	    &Math::Random::random_set_seed(@oldseed);
1.184     albertel  769: 
                    770: 	    my $correct;
                    771: 	    for(my $i=0; $i<=$#bubble_values;$i++) {
                    772: 		if ($bubble_values[$i] eq $answer) {
                    773: 		    $correct = $alphabet[$i];
                    774: 		    last;
                    775: 		}
                    776: 	    }
                    777: 
1.134     albertel  778: 	    if (defined($format) && $format ne '') {
1.158     albertel  779: 		my @bubble_display;
1.137     albertel  780: 		foreach my $value (@bubble_values) {
1.158     albertel  781: 		    push(@bubble_display,
                    782: 			 &format_number($value,$format,$target,$safeeval));
1.134     albertel  783: 		}
1.184     albertel  784: 		return (\@bubble_values,\@bubble_display,$correct);
1.158     albertel  785: 	    } else {
1.184     albertel  786: 		return (\@bubble_values,\@bubble_values,$correct);
1.134     albertel  787: 	    }
1.117     albertel  788: 	}
1.184     albertel  789: 	if (defined($incorrect[0]) &&
                    790: 	    scalar(@incorrect)+1 < $number_of_bubbles) {
                    791: 	    &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  792: 	}
1.117     albertel  793:     }
1.90      albertel  794:     my @factors = (1.13,1.17,1.25,1.33,1.45); #default values of factors
1.126     albertel  795:     my @powers = (1..$number_of_bubbles);
1.90      albertel  796:     &Apache::response::setrandomnumber();
                    797:     my $ind=&Math::Random::random_uniform_integer(1,0,$#powers);
                    798:     my $power = $powers[$ind];
                    799:     $ind=&Math::Random::random_uniform_integer(1,0,$#factors);
                    800:     my $factor = $factors[$ind];
1.158     albertel  801:     my @bubble_display;
1.90      albertel  802:     for ($ind=0;$ind<$number_of_bubbles;$ind++) {
1.91      albertel  803: 	$bubble_values[$ind] = $answer*($factor**($power-$powers[$#powers-$ind]));
1.158     albertel  804: 	$bubble_display[$ind] = &format_number($bubble_values[$ind],
1.153     albertel  805: 					       $format,$target,$safeeval);
1.91      albertel  806: 
1.90      albertel  807:     }
1.184     albertel  808:     my $correct = $alphabet[$number_of_bubbles-$power];
1.119     albertel  809:     &Math::Random::random_set_seed(@oldseed);
1.184     albertel  810:     return (\@bubble_values,\@bubble_display,$correct);
1.51      albertel  811: }
                    812: 
                    813: sub get_tolrange {
1.89      albertel  814:     my ($ans,$tol)=@_;
                    815:     my ($high,$low);
                    816:     if ($tol =~ /%$/) {
                    817: 	chop($tol);
                    818: 	my $change=$ans*($tol/100.0);
                    819: 	$high=$ans+$change;
                    820: 	$low=$ans-$change;
                    821:     } else {
                    822: 	$high=$ans+$tol;
                    823: 	$low=$ans-$tol;
                    824:     }
                    825:     return ($high,$low);
1.52      albertel  826: }
                    827: 
                    828: sub get_sigrange {
1.89      albertel  829:     my ($sig)=@_;
1.195   ! albertel  830:     #&Apache::lonxml::debug("Got a sig of :$sig:");
1.166     albertel  831:     my $courseid=$env{'request.course.id'};
                    832:     if (lc($env{"course.$courseid.disablesigfigs"}) eq 'yes') {
1.147     albertel  833: 	return (15,0);
                    834:     }
1.89      albertel  835:     my $sig_lbound;
                    836:     my $sig_ubound;
                    837:     if ($sig eq '') {
                    838: 	$sig_lbound = 0; #SIG_LB_DEFAULT
                    839: 	$sig_ubound =15; #SIG_UB_DEFAULT
                    840:     } else {
                    841: 	($sig_lbound,$sig_ubound) = split(/,/,$sig);
1.145     albertel  842: 	if (!defined($sig_lbound)) {
1.89      albertel  843: 	    $sig_lbound = 0; #SIG_LB_DEFAULT
                    844: 	    $sig_ubound =15; #SIG_UB_DEFAULT
                    845: 	}
1.145     albertel  846: 	if (!defined($sig_ubound)) { $sig_ubound=$sig_lbound; }
1.133     albertel  847:     }
                    848:     if (($sig_ubound<$sig_lbound) ||
                    849: 	($sig_lbound > 15) ||
                    850: 	($sig =~/(\+|-)/ ) ) {
                    851: 	my $errormsg=&mt("Invalid Significant figures detected")." ($sig)";
1.166     albertel  852: 	if ($env{'request.state'} eq 'construct') {
1.133     albertel  853: 	    $errormsg.=
                    854: 		&Apache::loncommon::help_open_topic('Significant_Figures');
                    855: 	}
                    856: 	&Apache::lonxml::error($errormsg);
1.52      albertel  857:     }
1.89      albertel  858:     return ($sig_ubound,$sig_lbound);
1.34      albertel  859: }
                    860: 
                    861: sub start_stringresponse {
1.89      albertel  862:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    863:     my $result;
1.122     albertel  864:     my $id = &Apache::response::start_response($parstack,$safeeval);
1.89      albertel  865:     if ($target eq 'meta') {
                    866: 	$result=&Apache::response::meta_package_write('stringresponse');
1.122     albertel  867:     } elsif ($target eq 'edit') {
                    868: 	$result.=&Apache::edit::tag_start($target,$token);
                    869: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
                    870: 	$result.=&Apache::edit::select_arg('Type:','type',
                    871: 			 [['cs','Case Sensitive'],['ci','Case Insensitive'],
                    872: 			  ['mc','Case Insensitive, Any Order'],
                    873: 			  ['re','Regular Expression']],$token);
1.152     albertel  874: 	$result.=&Apache::edit::text_arg('String to display for answer:',
                    875: 					 'answerdisplay',$token);
1.122     albertel  876: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    877:     } elsif ($target eq 'modified') {
1.146     albertel  878: 	my $constructtag;
                    879: 	$constructtag=&Apache::edit::get_new_args($token,$parstack,
                    880: 						  $safeeval,'answer',
                    881: 						  'type','answerdisplay');
                    882: 	if ($constructtag) {
                    883: 	    $result = &Apache::edit::rebuild_tag($token);
                    884: 	    $result.=&Apache::edit::handle_insert();
                    885: 	}
                    886:     } elsif ($target eq 'web') {
                    887: 	if (  &Apache::response::show_answer() ) {
1.152     albertel  888: 	    my $answer=
                    889: 	       &Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval);
                    890: 	    if (!defined $answer || $answer eq '') {
                    891: 		$answer=
                    892: 		    &Apache::lonxml::get_param('answer',$parstack,$safeeval);
                    893: 	    }
1.195   ! albertel  894: 	    $Apache::inputtags::answertxt{$id}=[$answer];
1.146     albertel  895: 	} 
1.122     albertel  896:     } elsif ($target eq 'answer' || $target eq 'grade') {
                    897: 	&Apache::response::reset_params();
1.89      albertel  898:     }
                    899:     return $result;
1.34      albertel  900: }
                    901: 
                    902: sub end_stringresponse {
1.122     albertel  903:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.189     albertel  904: 
1.122     albertel  905:     my $result = '';
                    906:     my $part=$Apache::inputtags::part;
                    907:     my $id=$Apache::inputtags::response[-1];
                    908:     my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
                    909:     my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
1.129     www       910:     my $answerdisplay=&Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval);
1.122     albertel  911:     &Apache::lonxml::debug("current $answer ".$token->[2]);
                    912:     if (!$Apache::lonxml::default_homework_loaded) {
                    913: 	&Apache::lonxml::default_homework_load($safeeval);
                    914:     }
1.162     albertel  915:     if ( $target eq 'grade' && &Apache::response::submitted() ) {
1.140     albertel  916: 	&Apache::response::setup_params('stringresponse',$safeeval);
1.122     albertel  917: 	$safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
                    918: 	if ($Apache::lonhomework::type eq 'exam' ||
1.162     albertel  919: 	    &Apache::response::submitted('scantron')) {
1.189     albertel  920: 	    &Apache::response::scored_response($part,$id);
                    921: 
1.122     albertel  922: 	} else {
                    923: 	    my $response = &Apache::response::getresponse();
                    924: 	    if ( $response =~ /[^\s]/) {
                    925: 		my %previous = &Apache::response::check_for_previous($response,
                    926: 								    $part,$id);
                    927: 		&Apache::lonxml::debug("submitted a $response<br>\n");
                    928: 		&Apache::lonxml::debug($$parstack[-1] . "\n<br>");
                    929: 		$Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    930: 		    $response;
1.142     albertel  931: 		my ($ad,$msg);
1.122     albertel  932: 		if ($type eq 're' ) { 
                    933: 		    # if the RE wasn't in a var it likely got munged,
                    934:                     # thus grab it from the var directly
                    935: #		    my $testans=$token->[2]->{'answer'};
                    936: #		    if ($testans !~ m/^\s*\$/) {
                    937: #			$answer=$token->[2]->{'answer'};
                    938: #		    }
1.143     albertel  939: 		    ${$safeeval->varglob('LONCAPA::response')}=$response;
1.179     albertel  940: 		    $result = &Apache::run::run('if ($LONCAPA::response=~m'.$answer.') { return 1; } else { return 0; }',$safeeval);
1.122     albertel  941: 		    &Apache::lonxml::debug("current $response");
                    942: 		    &Apache::lonxml::debug("current $answer");
                    943: 		    $ad = ($result) ? 'APPROX_ANS' : 'INCORRECT';
                    944: 		} else {
1.195   ! albertel  945: 		    my @args = ('type');
        !           946: 		    my $args_ref = &setup_capa_args($safeeval,$parstack,
        !           947: 						    \@args,$response);
        !           948: 
        !           949: 		    &add_in_tag_answer($parstack,$safeeval);
        !           950: 		    my (@final_awards,@final_msgs,@names);
        !           951: 		    foreach my $name (keys(%answer)) {
        !           952: 			&Apache::lonxml::debug(" doing $name with ".join(':',@{ $answer{$name}{'answers'} }));
        !           953: 			${$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=dclone($answer{$name});
        !           954: 			my ($result, @msgs)=&Apache::run::run("&caparesponse_check_list()",$safeeval);
        !           955: 			&Apache::lonxml::debug('msgs are'.join(':',@msgs));
        !           956: 			my ($awards)=split(/:/,$result);
        !           957: 			my (@awards) = split(/,/,$awards);
        !           958: 			($ad,$msg) = 
        !           959: 			    &Apache::inputtags::finalizeawards(\@awards,\@msgs);
        !           960: 			push(@final_awards,$ad);
        !           961: 			push(@final_msgs,$msg);
        !           962: 			push(@names,$name);
        !           963: 			&Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
1.122     albertel  964: 		    }
1.195   ! albertel  965: 		    my ($ad, $msg, $name) = 
        !           966: 			&Apache::inputtags::finalizeawards(\@final_awards,
        !           967: 							   \@final_msgs,
        !           968: 							   \@names,1);
1.122     albertel  969: 		}
1.163     albertel  970: 		if ($Apache::lonhomework::type eq 'survey' &&
                    971: 		    ($ad eq 'INCORRECT' || $ad eq 'APPROX_ANS' ||
                    972: 		     $ad eq 'EXACT_ANS')) {
                    973: 		    $ad='SUBMITTED';
                    974: 		}
1.122     albertel  975: 		&Apache::response::handle_previous(\%previous,$ad);
                    976: 		$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
1.142     albertel  977: 		$Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=$msg;
1.122     albertel  978: 	    }
                    979: 	}
                    980:     } elsif ($target eq 'answer' || $target eq 'analyze') {
                    981: 	if ($target eq 'analyze') {
                    982: 	    push (@{ $Apache::lonhomework::analyze{"parts"} },"$part.$id");
1.125     albertel  983: 	    $Apache::lonhomework::analyze{"$part.$id.type"} = 'stringresponse';
1.154     albertel  984: 	    &Apache::response::check_if_computed($token,$parstack,$safeeval,
                    985: 						 'answer');
1.122     albertel  986: 	}
1.140     albertel  987: 	&Apache::response::setup_params('stringresponse',$safeeval);
1.122     albertel  988: 	if ($target eq 'answer') {
1.125     albertel  989: 	    $result.=&Apache::response::answer_header('stringresponse');
1.122     albertel  990: 	}
                    991: #	foreach my $ans (@answers) {
                    992: 	    if ($target eq 'answer') {
1.125     albertel  993: 		$result.=&Apache::response::answer_part('stringresponse',$answer);
1.178     albertel  994: 		if ($type eq 're') {
                    995: 		    $result.=&Apache::response::answer_part('stringresponse',
                    996: 							    $answerdisplay);
                    997: 		}
1.122     albertel  998: 	    } elsif ($target eq 'analyze') {
                    999: 		push (@{ $Apache::lonhomework::analyze{"$part.$id.answer"} },
                   1000: 		      $answer);
                   1001: 	    }
                   1002: #	}
                   1003: 	my $string='Case Insensitive';
                   1004: 	if ($type eq 'mc') {
                   1005: 	    $string='Multiple Choice';
                   1006: 	} elsif ($type eq 'cs') {
                   1007: 	    $string='Case Sensitive';
                   1008: 	} elsif ($type eq 'ci') {
                   1009: 	    $string='Case Insensitive';
                   1010: 	} elsif ($type eq 're') {
                   1011: 	    $string='Regular Expression';
                   1012: 	}
                   1013: 	if ($target eq 'answer') {
1.166     albertel 1014: 	    if ($env{'form.answer_output_mode'} eq 'tex') {
1.125     albertel 1015: 		$result.=&Apache::response::answer_part('stringresponse',
1.122     albertel 1016: 							"$string");
                   1017: 	    } else {
1.125     albertel 1018: 		$result.=&Apache::response::answer_part('stringresponse',
1.122     albertel 1019: 							"<b>$string</b>");
                   1020: 	    }
                   1021: 	} elsif ($target eq 'analyze') {
                   1022: 	    push (@{$Apache::lonhomework::analyze{"$part.$id.str_type"}},
                   1023: 		  $type);
                   1024: 	}
                   1025: 	if ($target eq 'answer') {
1.125     albertel 1026: 	    $result.=&Apache::response::answer_footer('stringresponse');
1.122     albertel 1027: 	}
                   1028:     } elsif ($target eq 'edit') {
                   1029: 	$result.='</td></tr>'.&Apache::edit::end_table;
                   1030:     }
                   1031:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
                   1032: 	$target eq 'tex' || $target eq 'analyze') {
1.189     albertel 1033: 	&Apache::lonxml::increment_counter(&Apache::response::repetition());
1.122     albertel 1034:     }
                   1035:     &Apache::response::end_response;
                   1036:     return $result;
1.44      albertel 1037: }
                   1038: 
                   1039: sub start_formularesponse {
1.89      albertel 1040:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1041:     my $result;
                   1042:     if ($target eq 'meta') {
1.104     bowersj2 1043: 	&Apache::response::start_response($parstack,$safeeval);
1.89      albertel 1044: 	$result=&Apache::response::meta_package_write('formularesponse');
1.104     bowersj2 1045: 	&Apache::response::end_response();
1.89      albertel 1046:     } else {
                   1047: 	$result.=&start_numericalresponse(@_);
                   1048:     }
                   1049:     return $result;
1.44      albertel 1050: }
                   1051: 
                   1052: sub end_formularesponse {
1.89      albertel 1053:     return end_numericalresponse(@_);
1.3       albertel 1054: }
                   1055: 
1.1       albertel 1056: 1;
1.3       albertel 1057: __END__
1.89      albertel 1058: 

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