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

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

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