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

1.3       albertel    1: # The LearningOnline Network with CAPA
                      2: # caparesponse definition
1.47      albertel    3: #
1.246   ! raeburn     4: # $Id: caparesponse.pm,v 1.245 2011/02/02 18:48:06 raeburn 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.241     raeburn    36: use Apache::lonmsg();
1.202     www        37: use Apache::response();
1.194     albertel   38: use Storable qw(dclone);
1.3       albertel   39: 
1.50      harris41   40: BEGIN {
1.194     albertel   41:     &Apache::lonxml::register('Apache::caparesponse',('numericalresponse','stringresponse','formularesponse'));
1.3       albertel   42: }
                     43: 
1.181     albertel   44: my %answer;
1.204     albertel   45: my @answers;
1.203     albertel   46: sub get_answer { return %answer; };
1.204     albertel   47: sub push_answer{ push(@answers,dclone(\%answer)); undef(%answer) }
                     48: sub pop_answer { %answer = %{pop(@answers)}; };
1.203     albertel   49: 
1.181     albertel   50: my $cur_name;
1.195     albertel   51: my $tag_internal_answer_name = 'INTERNAL';
                     52: 
1.181     albertel   53: sub start_answer {
                     54:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                     55:     my $result;
                     56:     $cur_name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                     57:     if ($cur_name =~ /^\s*$/) { $cur_name = $Apache::lonxml::curdepth; }
                     58:     my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
                     59:     if (!defined($type) && $tagstack->[-2] eq 'answergroup') {
                     60: 	$type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
                     61:     }
                     62:     if (!defined($type)) { $type = 'ordered' };
                     63:     $answer{$cur_name}= { 'type' => $type,
                     64: 			  'answers' => [] };
1.208     albertel   65:     if ($target eq 'edit') {
                     66:   	$result.=&Apache::edit::tag_start($target,$token);
                     67: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
                     68: 	$result.=&Apache::edit::select_arg('Type:','type',
                     69: 					   [['ordered',  'Ordered'  ],
                     70: 					    ['unordered','Unordered'],],
                     71: 					   $token);
                     72: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                     73:     } elsif ($target eq 'modified') {
                     74: 	my $constructtag = &Apache::edit::get_new_args($token,$parstack,
                     75: 						       $safeeval,'name',
                     76: 						       'type');
                     77: 	if ($constructtag) {
                     78: 	    $result = &Apache::edit::rebuild_tag($token);
                     79: 	    $result.= &Apache::edit::handle_insert();
                     80: 	}
                     81:     }
1.181     albertel   82:     return $result;
                     83: }
                     84: 
                     85: sub end_answer {
                     86:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                     87:     my $result;
1.208     albertel   88:     if ($target eq 'edit') {
                     89: 	$result .= &Apache::edit::tag_end();
                     90:     }
                     91: 
1.181     albertel   92:     undef($cur_name);
                     93:     return $result;
                     94: }
                     95: 
1.208     albertel   96: sub insert_answer {
                     97:     return '
                     98:         <answer>
                     99:             <value></value>
                    100:         </answer>';
                    101: }
                    102: 
1.181     albertel  103: sub start_answergroup {
                    104:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    105:     my $result;
1.208     albertel  106:     if ($target eq 'edit') {
                    107:   	$result.=&Apache::edit::tag_start($target,$token);
                    108: 	$result.=&Apache::edit::select_arg('Type:','type',
                    109: 					   [['ordered',  'Ordered'  ],
                    110: 					    ['unordered','Unordered'],],
                    111: 					   $token);
                    112: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    113:     } elsif ($target eq 'modified') {
                    114: 	my $constructtag = &Apache::edit::get_new_args($token,$parstack,
                    115: 						       $safeeval,'name',
                    116: 						       'type');
                    117: 	if ($constructtag) {
                    118: 	    $result = &Apache::edit::rebuild_tag($token);
                    119: 	    $result.= &Apache::edit::handle_insert();
                    120: 	}
                    121:     }
1.181     albertel  122:     return $result;
                    123: }
                    124: 
                    125: sub end_answergroup {
                    126:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    127:     my $result;
1.194     albertel  128:     if ($target eq 'web') {
                    129:     	if (  &Apache::response::show_answer() ) {  
                    130: 	    my $partid = $Apache::inputtags::part;
                    131: 	    my $id = $Apache::inputtags::response[-1];
                    132: 	    &set_answertext($Apache::lonhomework::history{"resource.$partid.$id.answername"},
                    133: 			    $target,$token,$tagstack,$parstack,$parser,
                    134: 			    $safeeval,-2);
                    135: 	}
1.208     albertel  136:     } elsif ($target eq 'edit') {
                    137: 	$result .= &Apache::edit::tag_end();
1.194     albertel  138:     }
1.181     albertel  139:     return $result;
                    140: }
                    141: 
1.208     albertel  142: sub insert_answergroup {
                    143:     return '
                    144:     <answergroup>
                    145:         <answer>
                    146:             <value></value>
                    147:         </answer>
                    148:     </answergroup>';
                    149: }
                    150: 
1.181     albertel  151: sub start_value {
1.182     albertel  152:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.181     albertel  153:     my $result;
                    154:     if ( $target eq 'web' || $target eq 'tex' ||
                    155: 	 $target eq 'grade' || $target eq 'webgrade' ||
                    156: 	 $target eq 'answer' || $target eq 'analyze' ) {
1.182     albertel  157: 	my $bodytext = &Apache::lonxml::get_all_text("/value",$parser,$style);
1.181     albertel  158: 	$bodytext = &Apache::run::evaluate($bodytext,$safeeval,
                    159: 					   $$parstack[-1]);
1.195     albertel  160: 	
                    161: 	push(@{ $answer{$cur_name}{'answers'} },[$bodytext]);
                    162: 	
1.208     albertel  163:     } elsif ($target eq 'edit') {
                    164:   	$result.=&Apache::edit::tag_start($target,$token);
                    165: 	my $bodytext = &Apache::lonxml::get_all_text("/value",$parser,$style);
                    166: 	$result.=&Apache::edit::editline($token->[1],$bodytext,undef,40).
                    167: 	    &Apache::edit::end_row();
                    168:     } elsif ($target eq 'modified') {
                    169: 	$result=$token->[4].&Apache::edit::modifiedfield('/value',$parser);
1.181     albertel  170:     }
                    171:     return $result;
                    172: }
                    173: 
                    174: sub end_value {
                    175:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    176:     my $result;
1.208     albertel  177:     if ($target eq 'edit') {
                    178: 	$result = &Apache::edit::end_table();
                    179:     }
1.181     albertel  180:     return $result;
                    181: }
                    182: 
1.208     albertel  183: sub insert_value {
                    184:     return '
                    185:             <value></value>';
                    186: }
                    187: 
1.195     albertel  188: sub start_vector {
                    189:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    190:     my $result;
                    191:     if ( $target eq 'web' || $target eq 'tex' ||
                    192: 	 $target eq 'grade' || $target eq 'webgrade' ||
                    193: 	 $target eq 'answer' || $target eq 'analyze' ) {
                    194: 	my $bodytext = &Apache::lonxml::get_all_text("/vector",$parser,$style);
                    195: 	my @values = &Apache::run::run($bodytext,$safeeval,$$parstack[-1]);
                    196: 	if (@values == 1) {
                    197: 	    @values = split(',',$values[0]);
                    198: 	}
                    199: 	push(@{ $answer{$cur_name}{'answers'} },\@values);
1.208     albertel  200:     } elsif ($target eq 'edit') {
                    201:   	$result.=&Apache::edit::tag_start($target,$token);
                    202: 	my $bodytext = &Apache::lonxml::get_all_text("/vector",$parser,$style);
                    203: 	$result.=&Apache::edit::editline($token->[1],$bodytext,undef,40).
                    204: 	    &Apache::edit::end_row();
                    205:     } elsif ($target eq 'modified') {
                    206: 	$result=$token->[4].&Apache::edit::modifiedfield('/vector',$parser);
1.195     albertel  207:     }
                    208:     return $result;
                    209: }
                    210: 
                    211: sub end_vector {
                    212:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    213:     my $result;
1.208     albertel  214:     if ($target eq 'edit') {
                    215: 	$result = &Apache::edit::end_table();
                    216:     }
1.195     albertel  217:     return $result;
                    218: }
                    219: 
1.208     albertel  220: sub insert_vector {
                    221:     return '
                    222:             <value></value>';
                    223: }
                    224: 
1.181     albertel  225: sub start_array {
1.182     albertel  226:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.181     albertel  227:     my $result;
                    228:     if ( $target eq 'web' || $target eq 'tex' ||
                    229: 	 $target eq 'grade' || $target eq 'webgrade' ||
                    230: 	 $target eq 'answer' || $target eq 'analyze' ) {
1.182     albertel  231: 	my $bodytext = &Apache::lonxml::get_all_text("/array",$parser,$style);
1.181     albertel  232: 	my @values = &Apache::run::evaluate($bodytext,$safeeval,
                    233: 					    $$parstack[-1]);
                    234: 	push(@{ $answer{$cur_name}{'answers'} },@values);
                    235:     }
                    236:     return $result;
                    237: }
                    238: 
                    239: sub end_array {
                    240:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    241:     my $result;
                    242:     return $result;
                    243: }
                    244: 
                    245: sub start_unit {
                    246:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    247:     my $result;
                    248:     return $result;
                    249: }
                    250: 
                    251: sub end_unit {
                    252:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    253:     my $result;
                    254:     return $result;
                    255: }
                    256: 
1.89      albertel  257: sub start_numericalresponse {
                    258:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.195     albertel  259:     &Apache::lonxml::register('Apache::caparesponse',
                    260: 			      ('answer','answergroup','value','array','unit',
                    261: 			       'vector'));
1.208     albertel  262:     push(@Apache::lonxml::namespace,'caparesponse');
1.89      albertel  263:     my $id = &Apache::response::start_response($parstack,$safeeval);
                    264:     my $result;
1.181     albertel  265:     undef(%answer);
                    266:     undef(%{$safeeval->varglob('LONCAPA::CAPAresponse_args')});
1.89      albertel  267:     if ($target eq 'edit') {
                    268: 	$result.=&Apache::edit::tag_start($target,$token);
                    269: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
                    270: 	if ($token->[1] eq 'numericalresponse') {
1.122     albertel  271: 	    $result.=&Apache::edit::text_arg('Incorrect Answers:','incorrect',
1.149     www       272: 					     $token).
                    273: 		&Apache::loncommon::help_open_topic('numerical_wrong_answers');
1.89      albertel  274: 	    $result.=&Apache::edit::text_arg('Unit:','unit',$token,5).
                    275: 		&Apache::loncommon::help_open_topic('Physical_Units');
                    276: 	    $result.=&Apache::edit::text_arg('Format:','format',$token,4).
                    277: 		&Apache::loncommon::help_open_topic('Numerical_Response_Format');
                    278: 	} elsif ($token->[1] eq 'formularesponse') {
                    279: 	    $result.=&Apache::edit::text_arg('Sample Points:','samples',
                    280: 					     $token,40).
                    281: 	      &Apache::loncommon::help_open_topic('Formula_Response_Sampling');
                    282: 	}
                    283: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    284:     } elsif ($target eq 'modified') {
                    285: 	my $constructtag;
                    286: 	if ($token->[1] eq 'numericalresponse') {
                    287: 	    $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    288: 						      $safeeval,'answer',
1.156     albertel  289:  						      'incorrect','unit',
1.117     albertel  290: 						      'format');
1.89      albertel  291: 	} elsif ($token->[1] eq 'formularesponse') {
                    292: 	    $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    293: 						      $safeeval,'answer',
                    294: 						      'samples');
                    295: 	}
                    296: 	if ($constructtag) {
                    297: 	    $result = &Apache::edit::rebuild_tag($token);
                    298: 	    $result.=&Apache::edit::handle_insert();
1.22      albertel  299: 	}
1.89      albertel  300:     } elsif ($target eq 'meta') {
                    301: 	$result=&Apache::response::meta_package_write('numericalresponse');
                    302:     } elsif ($target eq 'answer' || $target eq 'grade') {
                    303: 	&Apache::response::reset_params();
1.96      albertel  304:     } elsif ($target eq 'web') {
                    305: 	my $partid = $Apache::inputtags::part;
                    306: 	my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
                    307: 	&Apache::lonxml::debug("Got unit $hideunit for $partid $id");
                    308: 	#no way to enter units, with radio buttons
1.238     www       309: 	if ((lc($hideunit) eq "yes") && ($Apache::lonhomework::type ne 'exam')) {
1.96      albertel  310: 	    my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
                    311: 						    $safeeval);
                    312: 	    if ($unit =~ /\S/) { $result.=" (in $unit) "; }
                    313: 	}
1.146     albertel  314: 	if (  &Apache::response::show_answer() ) {
1.195     albertel  315: 	    &set_answertext($tag_internal_answer_name,$target,$token,$tagstack,
                    316: 			    $parstack,$parser,$safeeval,-1);
1.194     albertel  317: 	}
                    318:     }
                    319:     return $result;
                    320: }
                    321: 
                    322: sub set_answertext {
                    323:     my ($name,$target,$token,$tagstack,$parstack,$parser,$safeeval,
                    324: 	$response_level) = @_;
                    325:     &add_in_tag_answer($parstack,$safeeval,$response_level);
                    326: 
1.206     albertel  327:     if ($name eq '' || !ref($answer{$name})) {
                    328: 	if (ref($answer{$tag_internal_answer_name})) {
                    329: 	    $name = $tag_internal_answer_name;
                    330: 	} else {
                    331: 	    $name = (sort(keys(%answer)))[0];
                    332: 	}
                    333:     }
1.194     albertel  334:     return if ($name eq '' || !ref($answer{$name}));
                    335: 
                    336:     my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,
                    337: 						 $safeeval,$response_level);
                    338:     my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval,
                    339: 					    $response_level);
                    340: 
                    341:     &Apache::lonxml::debug("answer looks to be $name");
1.195     albertel  342:     my @answertxt;
1.194     albertel  343:     for (my $i=0; $i < scalar(@{$answer{$name}{'answers'}}); $i++) {
1.195     albertel  344: 	my $answertxt;
1.194     albertel  345: 	my $answer=$answer{$name}{'answers'}[$i];
1.195     albertel  346: 	foreach my $element (@$answer) {
                    347: 	    if ( scalar(@$tagstack)
                    348: 		 && $tagstack->[$response_level] ne 'numericalresponse') {
                    349: 		$answertxt.=$element.',';
1.194     albertel  350: 	    } else {
1.195     albertel  351: 		my $format;
                    352: 		if ($#formats > 0) {
                    353: 		    $format=$formats[$i];
                    354: 		} else {
                    355: 		    $format=$formats[0];
                    356: 		}
                    357: 		if ($unit=~/\$/) { $format="\$".$format; $unit=~s/\$//g; }
                    358: 		if ($unit=~/\,/) { $format="\,".$format; $unit=~s/\,//g; }
                    359: 		my $formatted=&format_number($element,$format,$target,
                    360: 					     $safeeval);
                    361: 		$answertxt.=' '.$formatted.',';
1.146     albertel  362: 	    }
1.195     albertel  363: 	    
                    364: 	}
                    365: 	chop($answertxt);
                    366: 	if ($target eq 'web') {
                    367: 	    $answertxt.=" $unit ";
1.146     albertel  368: 	}
1.195     albertel  369: 
                    370: 	push(@answertxt,$answertxt)
1.16      albertel  371:     }
1.194     albertel  372: 	
                    373:     my $id = $Apache::inputtags::response[-1];
1.195     albertel  374:     $Apache::inputtags::answertxt{$id}=\@answertxt;
1.21      albertel  375: }
                    376: 
1.195     albertel  377: sub setup_capa_args {
                    378:     my ($safeeval,$parstack,$args,$response) = @_;
1.167     albertel  379:     my $args_ref= \%{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
1.195     albertel  380:     undef(%{ $args_ref });
                    381:  
                    382:     foreach my $arg (@{$args}) {
1.167     albertel  383: 	$$args_ref{$arg}=
                    384: 	    &Apache::lonxml::get_param($arg,$parstack,$safeeval);
                    385:     }
                    386:     foreach my $key (keys(%Apache::inputtags::params)) {
                    387: 	$$args_ref{$key}=$Apache::inputtags::params{$key};
                    388:     }
1.195     albertel  389:     &setup_capa_response($args_ref,$response);
                    390:     return $args_ref;
                    391: }
                    392: 
                    393: sub setup_capa_response {
                    394:     my ($args_ref,$response) = @_;   
                    395: 
                    396:     if (ref($response)) {
                    397: 	$$args_ref{'response'}=dclone($response);
                    398:     } else {
                    399: 	$$args_ref{'response'}=dclone([$response]);
                    400:     }
                    401: }
                    402: 
                    403: sub check_submission {
                    404:     my ($response,$partid,$id,$tag,$parstack,$safeeval,$ignore_sig)=@_;
                    405:     my @args = ('type','tol','sig','format','unit','calc','samples');
                    406:     my $args_ref = &setup_capa_args($safeeval,$parstack,\@args,$response);
                    407: 
                    408:     my $hideunit=
                    409: 	&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
                    410:         #no way to enter units, with radio buttons
1.167     albertel  411:     if ($Apache::lonhomework::type eq 'exam' ||
                    412: 	lc($hideunit) eq "yes") {
                    413: 	delete($$args_ref{'unit'});
                    414:     }
                    415:     #sig fig don't make much sense either
                    416:     if (($Apache::lonhomework::type eq 'exam' ||
1.171     albertel  417: 	 &Apache::response::submitted('scantron') ||
                    418: 	 $ignore_sig) &&
1.167     albertel  419: 	$tag eq 'numericalresponse') {
                    420: 	delete($$args_ref{'sig'});
                    421:     }
                    422:     
                    423:     if ($tag eq 'formularesponse') {
1.199     www       424: 	if ($$args_ref{'samples'}) {
1.191     www       425: 	    $$args_ref{'type'}='fml';
1.199     www       426: 	} else {
                    427: 	    $$args_ref{'type'}='math';
                    428: 	}
1.167     albertel  429:     } elsif ($tag eq 'numericalresponse') {
                    430: 	$$args_ref{'type'}='float';
1.233     raeburn   431:     } elsif ($tag eq 'stringresponse') {
                    432:         if ($$args_ref{'type'} eq '') {
                    433:             $$args_ref{'type'} = 'ci';
                    434:         }
1.167     albertel  435:     }
1.233     raeburn   436: 
1.194     albertel  437:     &add_in_tag_answer($parstack,$safeeval);
                    438: 
1.218     albertel  439:     if (!%answer) {
                    440: 	&Apache::lonxml::error("No answers are defined");
                    441:     }
                    442: 
1.195     albertel  443:     my (@final_awards,@final_msgs,@names);
1.181     albertel  444:     foreach my $name (keys(%answer)) {
                    445: 	&Apache::lonxml::debug(" doing $name with ".join(':',@{ $answer{$name}{'answers'} }));
1.194     albertel  446: 	
                    447: 	${$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=dclone($answer{$name});
1.195     albertel  448: 	&setup_capa_response($args_ref,$response);
                    449: 	use Time::HiRes;
                    450: 	my $t0 = [Time::HiRes::gettimeofday()];
1.181     albertel  451: 	my ($result,@msgs) = 
1.233     raeburn   452: 	    &Apache::run::run("&caparesponse_check_list()",$safeeval);
1.195     albertel  453: 	&Apache::lonxml::debug("checking $name $result with $response took ".&Time::HiRes::tv_interval($t0));
1.243     raeburn   454:         
1.181     albertel  455: 	&Apache::lonxml::debug('msgs are '.join(':',@msgs));
                    456: 	my ($awards)=split(/:/,$result);
                    457: 	my @awards= split(/,/,$awards);
                    458: 	my ($ad, $msg) = &Apache::inputtags::finalizeawards(\@awards,\@msgs);
                    459: 	push(@final_awards,$ad);
                    460: 	push(@final_msgs,$msg);
                    461: 	push(@names,$name);
                    462:     }
                    463:     my ($ad, $msg, $name) = &Apache::inputtags::finalizeawards(\@final_awards,
                    464: 							       \@final_msgs,
                    465: 							       \@names,1);
1.194     albertel  466:     &Apache::lonxml::debug(" name of picked award is $name from ".join(', ',@names));
                    467:     return($ad,$msg, $name);
                    468: }
                    469: 
1.241     raeburn   470: sub stringresponse_gradechange {
                    471:     my ($part,$id,$previous,$caller,$response,$ad,$type) = @_;
                    472:     return unless (ref($previous) eq 'HASH');
                    473:     my ($prevarray,$prevaward);
                    474:     my %typenames = (
                    475:                      cs => 'Case sensitive',
                    476:                      ci => 'Case insensitive',
                    477:                     );
                    478:     if ($caller eq 'cs') {
                    479:         return unless (ref($previous->{'version'}) eq 'ARRAY');
                    480:         $prevarray = $previous->{'version'};
                    481:         $prevaward = $previous->{'award'};
                    482:     } elsif ($caller eq 'ci') {
                    483:         return unless (ref($previous->{'versionci'}) eq 'ARRAY');
                    484:         $prevarray = $previous->{'versionci'};
                    485:         $prevaward = $previous->{'awardci'};
                    486:     } else {
                    487:         return;
                    488:     }
                    489:     my $count=0;
                    490:     my %count_lookup;
                    491:     foreach my $i (1..$Apache::lonhomework::history{'version'}) {
                    492:         my $prefix = $i.":resource.$part";
                    493:         next if (!exists($Apache::lonhomework::history{"$prefix.award"}));
                    494:         $count++;
                    495:         $count_lookup{$i} = $count;
                    496:     }
                    497:     my ($symb,$courseid,$domain,$name) = &Apache::lonnet::whichuser();
                    498:     my %coursedesc = &Apache::lonnet::coursedescription($courseid);
                    499:     my $cdom = $coursedesc{'domain'};
                    500:     my $versions = ' (submissions: '.join(', ',map {$count_lookup{$_} } @{$prevarray}).')';
                    501:     my $warning = "String Response ($typenames{$type}) grading discrepancy: award for response of $response changed from $prevaward".$versions." to $ad; user: $name:$domain in course: $courseid for part: $part response: $id for symb: $symb";
                    502:     &Apache::lonnet::logthis($warning);
                    503:     my $origmail = $Apache::lonnet::perlvar{'lonAdmEMail'};
                    504:     my $recipients = &Apache::loncommon::build_recipient_list(undef,'errormail',
                    505:                                                               $cdom,$origmail);
                    506:     if ($recipients ne '') {
                    507:         &Apache::lonmsg::sendemail($recipients,'Stringresponse Grading Discrepancy',$warning);
                    508:     }
                    509:     return;
                    510: }
                    511: 
1.194     albertel  512: sub add_in_tag_answer {
                    513:     my ($parstack,$safeeval,$response_level) = @_;
                    514:     my @answer=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval,
                    515: 					      $response_level);
                    516:     &Apache::lonxml::debug('answer is'.join(':',@answer));
1.213     albertel  517:     if (@answer && $answer[0] =~ /\S/) {
1.195     albertel  518: 	$answer{$tag_internal_answer_name}= {'type' => 'ordered',
                    519: 					     'answers' => [\@answer] };
1.194     albertel  520:     }
1.167     albertel  521: }
                    522: 
1.202     www       523: sub capa_formula_fix {
                    524:    my ($expression)=@_;
                    525:    return &Apache::response::implicit_multiplication($expression);
                    526: }
                    527: 
1.89      albertel  528: sub end_numericalresponse {
                    529:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.208     albertel  530: 
                    531:     &Apache::lonxml::deregister('Apache::caparesponse',
                    532: 			      ('answer','answergroup','value','array','unit',
                    533: 			       'vector'));
                    534:     pop(@Apache::lonxml::namespace);
                    535: 
1.90      albertel  536:     my $increment=1;
1.89      albertel  537:     my $result = '';
                    538:     if (!$Apache::lonxml::default_homework_loaded) {
                    539: 	&Apache::lonxml::default_homework_load($safeeval);
1.34      albertel  540:     }
1.167     albertel  541:     my $partid = $Apache::inputtags::part;
                    542:     my $id = $Apache::inputtags::response[-1];
1.125     albertel  543:     my $tag;
1.190     albertel  544:     my $safehole = new Safe::Hole;
1.168     albertel  545:     $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
1.190     albertel  546: 
1.125     albertel  547:     if (scalar(@$tagstack)) { $tag=$$tagstack[-1]; }
1.162     albertel  548:     if ( $target eq 'grade' && &Apache::response::submitted() ) {
1.140     albertel  549: 	&Apache::response::setup_params($tag,$safeeval);
1.95      albertel  550: 	if ($Apache::lonhomework::type eq 'exam' && 
1.190     albertel  551: 	    (($tag eq 'formularesponse') || ($tag eq 'mathresponse'))) {
1.95      albertel  552: 	    $increment=&Apache::response::scored_response($partid,$id);
                    553: 	} else {
                    554: 	    my $response = &Apache::response::getresponse();
                    555: 	    if ( $response =~ /[^\s]/) {
                    556: 		my %previous = &Apache::response::check_for_previous($response,$partid,$id);
                    557: 		&Apache::lonxml::debug("submitted a $response<br>\n");
                    558: 		&Apache::lonxml::debug($$parstack[-1] . "\n<br>");
                    559: 		
1.162     albertel  560: 		if ( &Apache::response::submitted('scantron')) {
1.228     riegler   561: 	    &add_in_tag_answer($parstack,$safeeval);
                    562: 	    my ($values,$display)=&make_numerical_bubbles($partid,$id,
                    563: 					  $target,$parstack,$safeeval);
                    564: 	    $response=$values->[$response];
                    565: 	}
                    566: 	$Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
1.194     albertel  567: 		my ($ad,$msg,$name)=&check_submission($response,$partid,$id,
                    568: 						      $tag,$parstack,
                    569: 						      $safeeval);
1.167     albertel  570: 
1.142     albertel  571: 		&Apache::lonxml::debug('ad is'.$ad);
                    572: 		if ($ad eq 'SIG_FAIL') {
                    573: 		    my ($sig_u,$sig_l)=
                    574: 			&get_sigrange($Apache::inputtags::params{'sig'});
                    575: 		    $msg=join(':',$msg,$sig_l,$sig_u);
                    576: 		    &Apache::lonxml::debug("sigs bad $sig_u $sig_l ".
                    577: 					   $Apache::inputtags::params{'sig'});
                    578: 		}
1.95      albertel  579: 		&Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
1.237     raeburn   580:                 if (($ad eq 'INCORRECT' || $ad eq 'APPROX_ANS' ||
                    581:                      $ad eq 'EXACT_ANS')) {
                    582: 		    if ($Apache::lonhomework::type eq 'survey') {
                    583: 		        $ad='SUBMITTED';
                    584: 		    } elsif ($Apache::lonhomework::type eq 'surveycred') {
                    585:                         $ad='SUBMITTED_CREDIT';
                    586:                     } elsif ($Apache::lonhomework::type eq 'anonsurvey') {
                    587:                         $ad='ANONYMOUS';
                    588:                     } elsif ($Apache::lonhomework::type eq 'anonsurveycred') {
                    589:                         $ad='ANONYMOUS_CREDIT';                     
                    590:                     }
                    591:                 }
1.95      albertel  592: 		&Apache::response::handle_previous(\%previous,$ad);
                    593: 		$Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
1.142     albertel  594: 		$Apache::lonhomework::results{"resource.$partid.$id.awardmsg"}=$msg;
1.194     albertel  595: 		$Apache::lonhomework::results{"resource.$partid.$id.answername"}=$name;
1.95      albertel  596: 		$result='';
1.90      albertel  597: 	    }
1.89      albertel  598: 	}
                    599:     } elsif ($target eq 'web' || $target eq 'tex') {
1.196     albertel  600: 	&check_for_answer_errors($parstack,$safeeval);
1.89      albertel  601: 	my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
                    602: 	my $status = $Apache::inputtags::status['-1'];
                    603: 	if ($Apache::lonhomework::type eq 'exam') {
1.194     albertel  604: 	    # FIXME support multi dimensional numerical problems
                    605:             #       in exam bubbles
1.184     albertel  606: 	    my ($bubble_values,$bubble_display)=
                    607: 		&make_numerical_bubbles($partid,$id,$target,$parstack,
                    608: 					$safeeval);
                    609: 	    my $number_of_bubbles = scalar(@{ $bubble_values });
1.89      albertel  610: 	    my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
                    611: 						    $safeeval);
                    612: 	    my @alphabet=('A'..'Z');
                    613: 	    if ($target eq 'web') {
1.125     albertel  614: 		if ($tag eq 'numericalresponse') {
1.89      albertel  615: 		    if ($unit=~/\S/) {$result.=' (in '.$unit.')<br /><br />';}
                    616: 		    $result.= '<table border="1"><tr>';
1.116     albertel  617: 		    my $previous=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.$id.submission"};
1.90      albertel  618: 		    for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
1.116     albertel  619: 			my $checked='';
1.158     albertel  620: 			if ($previous eq $bubble_values->[$ind]) {
1.116     albertel  621: 			    $checked=" checked='on' ";
                    622: 			}
1.90      albertel  623: 			$result.='<td><input type="radio" name="HWVAL_'.$id.
1.158     albertel  624: 			    '" value="'.$bubble_values->[$ind].'" '.$checked
1.116     albertel  625: 			    .' /><b>'.$alphabet[$ind].'</b>: '.
1.158     albertel  626: 			    $bubble_display->[$ind].'</td>';
1.89      albertel  627: 		    }
                    628: 		    $result.='</tr></table>';
                    629: 		}
                    630: 	    } elsif ($target eq 'tex') {
1.107     sakharuk  631: 		if ((defined $unit) and ($unit=~/\S/) and ($Apache::lonhomework::type eq 'exam')) {
1.89      albertel  632: 		    $result.=' \textit{(in} \verb|'.$unit.'|\textit{)} ';
                    633: 		}
1.125     albertel  634: 		if ($tag eq 'numericalresponse') {
1.90      albertel  635: 		    my ($celllength,$number_of_tables,@table_range)=
1.159     albertel  636: 			&get_table_sizes($number_of_bubbles,$bubble_display);
1.89      albertel  637: 		    my $j=0;
                    638: 		    my $cou=0;
1.239     foxr      639: 		    $result.='\vskip 2mm \noindent ';
                    640: 		    $result .= '\textbf{'.$Apache::lonxml::counter.'.} \vskip -3mm ';
                    641: 
1.89      albertel  642: 		    for (my $i=0;$i<$number_of_tables;$i++) {
1.239     foxr      643: 			if ($i == 0) {
                    644: 			    $result .= '\vskip -1mm ';
                    645: 			} else {
                    646: 			    $result .= '\vskip 1mm ';
                    647: 			}
                    648: 			$result.='\noindent \setlength{\tabcolsep}{2 mm}\hskip 2pc\begin{tabular}{';
1.90      albertel  649: 			for (my $ind=0;$ind<$table_range[$j];$ind++) {
1.128     sakharuk  650: 			    $result.='p{3 mm}p{'.$celllength.' mm}';
1.89      albertel  651: 			}
                    652: 			$result.='}';
1.90      albertel  653: 			for (my $ind=$cou;$ind<$cou+$table_range[$j];$ind++) {
1.159     albertel  654: 			    $result.='\hskip -4 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$ & \hskip -3 mm {\small '.$bubble_display->[$ind].'} ';
1.89      albertel  655: 			    if ($ind != $cou+$table_range[$j]-1) {$result.=' & ';}
                    656: 			}
                    657: 			$cou += $table_range[$j];
                    658: 			$j++;
                    659: 			$result.='\\\\\end{tabular}\vskip 0 mm ';
                    660: 		    }
                    661: 		} else {
1.188     albertel  662: 		    $increment = &Apache::response::repetition();
1.89      albertel  663: 		}
                    664: 	    }
                    665: 	}
1.228     riegler   666:         if (($target eq 'web') && ($tag eq 'formularesponse')
1.231     riegler   667:             && ($Apache::lonhomework::type ne 'exam') && ($Apache::inputtags::status['-1'] eq 'CAN_ANSWER')
1.235     raeburn   668: 	    && (&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffeditor') ne 'yes')) {
                    669:             $result.=&Apache::response::edit_mathresponse_button($id,"HWVAL_$id");
1.228     riegler   670:         }
                    671:             
1.212     albertel  672: 	&Apache::response::setup_prior_tries_hash(\&format_prior_response_numerical);
1.89      albertel  673:     } elsif ($target eq 'edit') {
                    674: 	$result.='</td></tr>'.&Apache::edit::end_table;
                    675:     } elsif ($target eq 'answer' || $target eq 'analyze') {
1.167     albertel  676: 	my $part_id="$partid.$id";
1.89      albertel  677: 	if ($target eq 'analyze') {
                    678: 	    push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
1.125     albertel  679: 	    $Apache::lonhomework::analyze{"$part_id.type"} = $tag;
1.117     albertel  680: 	    my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
1.161     albertel  681: 	    if ($#incorrect eq 0) { @incorrect=(split(/,/,$incorrect[0])); }
1.117     albertel  682: 	    push (@{ $Apache::lonhomework::analyze{"$part_id.incorrect"} }, @incorrect);
1.154     albertel  683: 	    &Apache::response::check_if_computed($token,$parstack,
                    684: 						 $safeeval,'answer');
1.89      albertel  685: 	}
1.125     albertel  686: 	if (scalar(@$tagstack)) {
1.140     albertel  687: 	    &Apache::response::setup_params($tag,$safeeval);
1.125     albertel  688: 	}
1.194     albertel  689: 	&add_in_tag_answer($parstack,$safeeval);
1.58      sakharuk  690: 	my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
1.194     albertel  691: 
1.58      sakharuk  692: 	my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval);
1.89      albertel  693: 	
                    694: 	if ($target eq 'answer') {
1.195     albertel  695: 	    $result.=&Apache::response::answer_header($tag,undef,
                    696: 						      scalar(keys(%answer)));
1.185     albertel  697: 	    if ($tag eq 'numericalresponse'
                    698: 		&& $Apache::lonhomework::type eq 'exam') {
1.184     albertel  699: 		my ($bubble_values,undef,$correct) = &make_numerical_bubbles($partid,
                    700: 					     $id,$target,$parstack,$safeeval);
                    701: 		$result.=&Apache::response::answer_part($tag,$correct);
                    702: 	    }
1.89      albertel  703: 	}
1.194     albertel  704: 	foreach my $name (sort(keys(%answer))) {
                    705: 	    my @answers = @{ $answer{$name}{'answers'} };
1.200     albertel  706: 	    if ($target eq 'analyze') {
                    707: 		foreach my $info ('answer','ans_high','ans_low','format') {
                    708: 		    $Apache::lonhomework::analyze{"$part_id.$info"}{$name}=[];
                    709: 		}
                    710: 	    }
1.194     albertel  711: 	    my ($sigline,$tolline);
1.195     albertel  712: 	    if ($name ne $tag_internal_answer_name 
                    713: 		|| scalar(keys(%answer)) > 1) {
                    714: 		$result.=&Apache::response::answer_part($tag,$name);
                    715: 	    }
1.194     albertel  716: 	    for(my $i=0;$i<=$#answers;$i++) {
                    717: 		my $ans=$answers[$i];
                    718: 		my $fmt=$formats[0];
                    719: 		if (@formats && $#formats) {$fmt=$formats[$i];}
                    720: 		my ($sighigh,$siglow);
                    721: 		if ($Apache::inputtags::params{'sig'}) {
                    722: 		    ($sighigh,$siglow)=&get_sigrange($Apache::inputtags::params{'sig'});
                    723: 		}
1.195     albertel  724: 		my @vector;
                    725: 		if (ref($ans)) {
                    726: 		    @vector = @{ $ans };
                    727: 		} else {
                    728: 		    @vector = ($ans);
1.194     albertel  729: 		}
1.200     albertel  730: 		my @all_answer_info;
                    731: 		foreach my $element (@vector) {
                    732: 		    my ($high,$low);
                    733: 		    if ($Apache::inputtags::params{'tol'}) {
                    734: 			($high,$low)=&get_tolrange($element,$Apache::inputtags::params{'tol'});
                    735: 		    }
                    736: 		    if ($target eq 'answer') {
1.195     albertel  737: 			if ($fmt && $tag eq 'numericalresponse') {
                    738: 			    $fmt=~s/e/E/g;
                    739: 			    if ($unit=~/\$/) { $fmt="\$".$fmt; $unit=~s/\$//g; }
                    740: 			    if ($unit=~/\,/) { $fmt="\,".$fmt; $unit=~s/\,//g; }
                    741: 			    $element = &format_number($element,$fmt,$target,$safeeval);
                    742: 			    #if ($high) {
                    743: 			    #    $high=&format_number($high,$fmt,$target,$safeeval);
                    744: 			    #    $low =&format_number($low,$fmt,$target,$safeeval);
                    745: 			    #}
                    746: 			}
                    747: 			if ($high && $tag eq 'numericalresponse') {
1.224     www       748: 			    $element.='; ['.$low.'; '.$high.']';
1.195     albertel  749: 			    $tolline .= "[$low, $high]";
                    750: 			}
                    751: 			if (defined($sighigh) && $tag eq 'numericalresponse') {
                    752: 			    if ($env{'form.answer_output_mode'} eq 'tex') {
1.224     www       753: 				$element.= "; Sig $siglow - $sighigh";
1.195     albertel  754: 			    } else {
                    755: 				$element.= " Sig <i>$siglow - $sighigh</i>";
                    756: 				$sigline .= "[$siglow, $sighigh]";
                    757: 			    }
1.194     albertel  758: 			}
1.195     albertel  759: 			push(@all_answer_info,$element);
1.200     albertel  760: 			
                    761: 		    } elsif ($target eq 'analyze') {
                    762: 			push (@{ $Apache::lonhomework::analyze{"$part_id.answer"}{$name}[$i] }, $element);
                    763: 			if ($high) {
                    764: 			    push (@{ $Apache::lonhomework::analyze{"$part_id.ans_high"}{$name}[$i] }, $high);
                    765: 			    push (@{ $Apache::lonhomework::analyze{"$part_id.ans_low"}{$name}[$i] }, $low);
                    766: 			}
                    767: 			if ($fmt) {
                    768: 			    push (@{ $Apache::lonhomework::analyze{"$part_id.format"}{$name}[$i] }, $fmt);
                    769: 			}
1.194     albertel  770: 		    }
1.200     albertel  771: 		}
                    772: 		if ($target eq 'answer') {
1.223     www       773: 		    $result.= &Apache::response::answer_part($tag,join('; ',@all_answer_info));
1.194     albertel  774: 		}
                    775: 	    }
                    776: 
                    777: 	    my @fmt_ans;
                    778: 	    for(my $i=0;$i<=$#answers;$i++) {
                    779: 		my $ans=$answers[$i];
                    780: 		my $fmt=$formats[0];
                    781: 		if (@formats && $#formats) {$fmt=$formats[$i];}
1.195     albertel  782: 		foreach my $element (@$ans) {		    
                    783: 		    if ($fmt && $tag eq 'numericalresponse') {
                    784: 			$fmt=~s/e/E/g;
                    785: 			if ($unit=~/\$/) { $fmt="\$".$fmt; $unit=~s/\$//g; }
                    786: 			if ($unit=~/\,/) { $fmt="\,".$fmt; $unit=~s/\,//g; }
                    787: 			$element = &format_number($element,$fmt,$target,
                    788: 						  $safeeval);
                    789: 			if ($fmt=~/\$/ && $unit!~/\$/) { $element=~s/\$//; }
                    790: 		    }
1.194     albertel  791: 		}
1.195     albertel  792: 		push(@fmt_ans,join(',',@$ans));
1.194     albertel  793: 	    }
1.195     albertel  794: 	    my $response=\@fmt_ans;
                    795: 
1.194     albertel  796: 	    my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.
                    797: 					      $id.'.turnoffunit');
                    798: 	    if ($unit ne ''  && 
                    799: 		! ($Apache::lonhomework::type eq 'exam' ||
                    800: 		   lc($hideunit) eq "yes") )  {
                    801: 		my $cleanunit=$unit;
                    802: 		$cleanunit=~s/\$\,//g;
1.195     albertel  803: 		foreach my $ans (@fmt_ans) {
                    804: 		    $ans.=" $cleanunit";
                    805: 		}
1.60      sakharuk  806: 	    }
1.194     albertel  807: 	    my ($ad,$msg)=&check_submission($response,$partid,$id,$tag,
                    808: 					    $parstack,$safeeval);
                    809: 	    if ($ad ne 'EXACT_ANS' && $ad ne 'APPROX_ANS') {
                    810: 		my $error;
                    811: 		if ($tag eq 'formularesponse') {
1.236     bisitz    812: 		    $error=&mt("Computer's answer is incorrect ([_1]).",'"'.join(', ',@$response).'"');
1.194     albertel  813: 		} else {
                    814: 		    # answer failed check if it is sig figs that is failing
                    815: 		    my ($ad,$msg)=&check_submission($response,$partid,$id,
                    816: 						    $tag,$parstack,
                    817: 						    $safeeval,1);
1.236     bisitz    818: 		    $error=&mt("Computer's answer is incorrect ([_1]).",'"'.join(', ',@$response).'"').' ';
1.194     albertel  819: 		    if ($sigline ne '') {
1.236     bisitz    820: 			$error.=&mt('It is likely that the tolerance range [_1] or significant figures [_2] need to be adjusted.',$tolline,$sigline);
1.98      sakharuk  821: 		    } else {
1.236     bisitz    822: 			$error.=&mt('It is likely that the tolerance range [_1] needs to be adjusted.',$tolline);
1.98      sakharuk  823: 		    }
                    824: 		}
1.194     albertel  825: 		if ($ad ne 'EXACT_ANS' && $ad ne 'APPROX_ANS') {
                    826: 		    &Apache::lonxml::error($error);
                    827: 		} else {
                    828: 		    &Apache::lonxml::warning($error);
1.165     albertel  829: 		}
1.79      sakharuk  830: 	    }
1.176     albertel  831: 
1.194     albertel  832: 	    if (defined($unit) and ($unit ne '') and
                    833: 		$tag eq 'numericalresponse') {
                    834: 		if ($target eq 'answer') {
                    835: 		    if ($env{'form.answer_output_mode'} eq 'tex') {
                    836: 			$result.=&Apache::response::answer_part($tag,
                    837: 								" Unit: $unit ");
                    838: 		    } else {
                    839: 			$result.=&Apache::response::answer_part($tag,
                    840: 								"Unit: <b>$unit</b>");
                    841: 		    }
                    842: 		} elsif ($target eq 'analyze') {
                    843: 		    push (@{ $Apache::lonhomework::analyze{"$part_id.unit"} }, $unit);
1.171     albertel  844: 		}
1.167     albertel  845: 	    }
1.194     albertel  846: 	    if ($tag eq 'formularesponse' && $target eq 'answer') {
                    847: 		my $samples=&Apache::lonxml::get_param('samples',$parstack,$safeeval);
                    848: 		$result.=&Apache::response::answer_part($tag,$samples);
1.89      albertel  849: 	    }
1.195     albertel  850: 	    $result.=&Apache::response::next_answer($tag,$name);
1.89      albertel  851: 	}
                    852: 	if ($target eq 'answer') {
1.125     albertel  853: 	    $result.=&Apache::response::answer_footer($tag);
1.89      albertel  854: 	}
1.69      sakharuk  855:     }
1.121     sakharuk  856:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
1.90      albertel  857: 	$target eq 'tex' || $target eq 'analyze') {
1.221     raeburn   858:         if (($tag eq 'formularesponse') && ($target eq 'analyze')) {
                    859:             my $type = &Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.type');
                    860:             if ($type eq 'exam') {
                    861:                 $increment = &Apache::response::repetition();
                    862:             }
                    863:         }
1.220     albertel  864: 	&Apache::lonxml::increment_counter($increment,"$partid.$id");
                    865: 	if ($target eq 'analyze') {
                    866: 	    &Apache::lonhomework::set_bubble_lines();
                    867: 	}
1.90      albertel  868:     }
1.196     albertel  869:     &Apache::response::end_response();
1.89      albertel  870:     return $result;
1.90      albertel  871: }
                    872: 
1.212     albertel  873: sub format_prior_response_numerical {
                    874:     my ($mode,$answer) = @_;
1.213     albertel  875:     if (ref($answer)) {
                    876: 	my $result = '<table class="LC_prior_numerical"><tr>';
                    877: 	foreach my $element (@{ $answer }) {
                    878: 	    $result.= '<td><span class="LC_prior_numerical">'.
                    879: 		&HTML::Entities::encode($element,'"<>&').'</span></td>';
                    880: 	}
                    881: 	$result.='</tr></table>';
                    882: 	return $result;
                    883:     }
1.212     albertel  884:     return '<span class="LC_prior_numerical">'.
                    885: 	    &HTML::Entities::encode($answer,'"<>&').'</span>';
                    886: 
1.209     albertel  887: }
                    888: 
1.196     albertel  889: sub check_for_answer_errors {
                    890:     my ($parstack,$safeeval) = @_;
                    891:     &add_in_tag_answer($parstack,$safeeval);
                    892:     my %counts;
                    893:     foreach my $name (keys(%answer)) {
                    894: 	push(@{$counts{scalar(@{$answer{$name}{'answers'}})}},$name);
                    895:     }
                    896:     if (scalar(keys(%counts)) > 1) {
                    897: 	my $counts = join(' ',map {
                    898: 	    my $count = $_;
                    899: 	    &mt("Answers [_1] had [_2] components.",
                    900: 		'<tt>'.join(', ',@{$counts{$count}}).'</tt>',
                    901: 		$count);
                    902: 	} (sort(keys(%counts))));
                    903: 	&Apache::lonxml::error(&mt("All answers must have the same number of components. Varying numbers of answers were seen. ").$counts);
                    904:     }
                    905:     my $expected_number_of_inputs = (keys(%counts))[0];
1.214     albertel  906:     if ( $expected_number_of_inputs > 0 
                    907: 	 && $expected_number_of_inputs != scalar(@Apache::inputtags::inputlist)) {
1.196     albertel  908: 	&Apache::lonxml::error(&mt("Expected [_1] input fields, but there were only [_2] seen.", 
                    909: 				   $expected_number_of_inputs,
                    910: 				   scalar(@Apache::inputtags::inputlist)));
                    911:     }
                    912: }
                    913: 
1.90      albertel  914: sub get_table_sizes {
1.128     sakharuk  915:     my ($number_of_bubbles,$rbubble_values)=@_;
                    916:     my $scale=2; #mm for one digit
                    917:     my $cell_width=0;
                    918:     foreach my $member (@$rbubble_values) {
                    919: 	my $cell_width_real=0;
1.160     albertel  920: 	if ($member=~/(\+|-)?(\d*)\.?(\d*)\s*\$?\\times\s*10\^{(\+|-)?(\d+)}\$?/) {
1.138     sakharuk  921: 	    $cell_width_real=(length($2)+length($3)+length($5)+7)*$scale;
                    922: 	} elsif ($member=~/(\d*)\.?(\d*)(E|e)(\+|-)?(\d*)/) {
1.128     sakharuk  923: 	    $cell_width_real=(length($1)+length($2)+length($5)+9)*$scale;
1.138     sakharuk  924:         } elsif ($member=~/(\d*)\.(\d*)/) {
1.132     sakharuk  925: 	    $cell_width_real=(length($1)+length($2)+3)*$scale;
1.128     sakharuk  926: 	} else {
1.138     sakharuk  927: 	    $cell_width_real=(length($member)+1)*$scale*0.9;
1.128     sakharuk  928: 	}
                    929: 	if ($cell_width_real>$cell_width) {$cell_width=$cell_width_real;}
1.90      albertel  930:     }
1.132     sakharuk  931:     $cell_width+=8; 
1.130     sakharuk  932:     my $textwidth;
1.166     albertel  933:     if ($env{'form.textwidth'} ne '') {
                    934: 	$env{'form.textwidth'}=~/(\d*)\.?(\d*)/;
1.132     sakharuk  935: 	$textwidth=$1.'.'.$2;
1.130     sakharuk  936:     } else {
1.166     albertel  937: 	$env{'form.textwidth'}=~/(\d+)\.?(\d*)/;
1.132     sakharuk  938: 	$textwidth=$1.'.'.$2;
1.130     sakharuk  939:     }
1.128     sakharuk  940:     my $bubbles_per_line=int($textwidth/$cell_width);
1.151     sakharuk  941:     if ($bubbles_per_line > $number_of_bubbles) {
                    942: 	$bubbles_per_line=$number_of_bubbles;
1.216     albertel  943:     } elsif (($bubbles_per_line > $number_of_bubbles/2) 
                    944: 	     && ($number_of_bubbles % 2==0)) {
                    945: 	$bubbles_per_line=$number_of_bubbles/2;
                    946:     }
                    947:     if ($bubbles_per_line < 1) {
                    948: 	$bubbles_per_line=1;
                    949:     }
1.128     sakharuk  950:     my $number_of_tables = int($number_of_bubbles/$bubbles_per_line);
1.90      albertel  951:     my @table_range = ();
1.128     sakharuk  952:     for (my $i=0;$i<$number_of_tables;$i++) {push @table_range,$bubbles_per_line;}
                    953:     if ($number_of_bubbles % $bubbles_per_line) {
1.90      albertel  954: 	$number_of_tables++;
1.128     sakharuk  955: 	push @table_range,($number_of_bubbles % $bubbles_per_line);
1.90      albertel  956:     }
1.128     sakharuk  957:     $cell_width-=8;
1.136     sakharuk  958:     $cell_width=$cell_width*3/4;
1.128     sakharuk  959:     return ($cell_width,$number_of_tables,@table_range);
1.90      albertel  960: }
                    961: 
                    962: sub format_number {
1.153     albertel  963:     my ($number,$format,$target,$safeeval)=@_;
1.90      albertel  964:     my $ans;
1.153     albertel  965:     if ($format eq '') {
1.90      albertel  966: 	#What is the number? (integer,decimal,floating point)
1.187     albertel  967: 	if ($number=~/^(\d*\.?\d*)(E|e)[+\-]?(\d*)$/) {
1.113     sakharuk  968: 	    $format = '3e';
1.90      albertel  969: 	} elsif ($number=~/^(\d*)\.(\d*)$/) {
                    970: 	    $format = '4f';
                    971: 	} elsif ($number=~/^(\d*)$/) {
                    972: 	    $format = 'd';
                    973: 	}
                    974:     }
1.156     albertel  975:     if (!$Apache::lonxml::default_homework_loaded) {
                    976: 	&Apache::lonxml::default_homework_load($safeeval);
                    977:     }
1.153     albertel  978:     $ans=&Apache::run::run("&prettyprint(q\0$number\0,q\0$format\0,q\0$target\0)",$safeeval);
1.90      albertel  979:     return $ans;
                    980: }
                    981: 
                    982: sub make_numerical_bubbles {
1.184     albertel  983:     my ($part,$id,$target,$parstack,$safeeval) =@_;
1.215     albertel  984: 
                    985:     if (!%answer) {
                    986: 	&Apache::lonxml::error(&mt("No answers defined for response [_1] in part [_2] to make bubbles for.",$id,$part));
                    987: 	return ([],[],undef);
                    988:     }
1.184     albertel  989:     
                    990:     my $number_of_bubbles = 
                    991: 	&Apache::response::get_response_param($part.'_'.$id,'numbubbles',8);
                    992: 
                    993:     my ($format)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
1.201     albertel  994:     my $name = (exists($answer{$tag_internal_answer_name}) 
                    995: 		? $tag_internal_answer_name
                    996: 		: (sort(keys(%answer)))[0]);
                    997: 
                    998:     if ( scalar(@{$answer{$name}{'answers'}}) > 1) {
                    999: 	&Apache::lonxml::error("Only answers with 1 component are supported in exam mode");
                   1000:     }
                   1001:     if (scalar(@{$answer{$name}{'answers'}[0]}) > 1) {
                   1002: 	&Apache::lonxml::error("Vector answers are unsupported in exam mode.");
                   1003:     }
                   1004: 
                   1005:     my $answer = $answer{$name}{'answers'}[0][0];
1.184     albertel 1006:     my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,
                   1007: 						   $safeeval);
                   1008:     if ($#incorrect eq 0) { @incorrect=(split(/,/,$incorrect[0])); }
                   1009:     
1.158     albertel 1010:     my @bubble_values=();
1.184     albertel 1011:     my @alphabet=('A'..'Z');
                   1012: 
                   1013:     &Apache::lonxml::debug("answer is $answer incorrect is @incorrect");
1.119     albertel 1014:     my @oldseed=&Math::Random::random_get_seed();
1.184     albertel 1015:     if (@incorrect) {
                   1016: 	&Apache::lonxml::debug("inside ".(scalar(@incorrect)+1 gt $number_of_bubbles));
                   1017: 	if (defined($incorrect[0]) &&
                   1018: 	    scalar(@incorrect)+1 >= $number_of_bubbles) {
                   1019: 	    &Apache::lonxml::debug("inside ".(scalar(@incorrect)+1).":$number_of_bubbles");
1.117     albertel 1020: 	    &Apache::response::setrandomnumber();
1.184     albertel 1021: 	    my @rand_inc=&Math::Random::random_permutation(@incorrect);
1.117     albertel 1022: 	    @bubble_values=@rand_inc[0..($number_of_bubbles-2)];
                   1023: 	    @bubble_values=sort {$a <=> $b} (@bubble_values,$answer);
1.184     albertel 1024: 	    &Apache::lonxml::debug("Answer was :$answer: returning :".$#bubble_values.": which are :".join(':',@bubble_values));
1.119     albertel 1025: 	    &Math::Random::random_set_seed(@oldseed);
1.184     albertel 1026: 
                   1027: 	    my $correct;
                   1028: 	    for(my $i=0; $i<=$#bubble_values;$i++) {
                   1029: 		if ($bubble_values[$i] eq $answer) {
                   1030: 		    $correct = $alphabet[$i];
                   1031: 		    last;
                   1032: 		}
                   1033: 	    }
                   1034: 
1.134     albertel 1035: 	    if (defined($format) && $format ne '') {
1.158     albertel 1036: 		my @bubble_display;
1.137     albertel 1037: 		foreach my $value (@bubble_values) {
1.158     albertel 1038: 		    push(@bubble_display,
                   1039: 			 &format_number($value,$format,$target,$safeeval));
1.134     albertel 1040: 		}
1.184     albertel 1041: 		return (\@bubble_values,\@bubble_display,$correct);
1.158     albertel 1042: 	    } else {
1.184     albertel 1043: 		return (\@bubble_values,\@bubble_values,$correct);
1.134     albertel 1044: 	    }
1.117     albertel 1045: 	}
1.184     albertel 1046: 	if (defined($incorrect[0]) &&
                   1047: 	    scalar(@incorrect)+1 < $number_of_bubbles) {
                   1048: 	    &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 1049: 	}
1.117     albertel 1050:     }
1.90      albertel 1051:     my @factors = (1.13,1.17,1.25,1.33,1.45); #default values of factors
1.126     albertel 1052:     my @powers = (1..$number_of_bubbles);
1.90      albertel 1053:     &Apache::response::setrandomnumber();
                   1054:     my $ind=&Math::Random::random_uniform_integer(1,0,$#powers);
                   1055:     my $power = $powers[$ind];
                   1056:     $ind=&Math::Random::random_uniform_integer(1,0,$#factors);
                   1057:     my $factor = $factors[$ind];
1.158     albertel 1058:     my @bubble_display;
1.222     www      1059:     my $answerfactor=$answer;
                   1060:     if ($answer==0) { 
                   1061:        $answerfactor=&Math::Random::random_uniform_integer(1,1,100)/
                   1062:                      &Math::Random::random_uniform_integer(1,1,10);
                   1063:     }
1.90      albertel 1064:     for ($ind=0;$ind<$number_of_bubbles;$ind++) {
1.222     www      1065: 	$bubble_values[$ind] = $answerfactor*($factor**($power-$powers[$#powers-$ind]));
1.158     albertel 1066: 	$bubble_display[$ind] = &format_number($bubble_values[$ind],
1.153     albertel 1067: 					       $format,$target,$safeeval);
1.90      albertel 1068:     }
1.184     albertel 1069:     my $correct = $alphabet[$number_of_bubbles-$power];
1.222     www      1070:     if ($answer==0) {
                   1071:        $correct='A';
                   1072:        $bubble_values[0]=0;
                   1073:        $bubble_display[0] = &format_number($bubble_values[0],
                   1074:                                            $format,$target,$safeeval);
                   1075:     }
1.119     albertel 1076:     &Math::Random::random_set_seed(@oldseed);
1.184     albertel 1077:     return (\@bubble_values,\@bubble_display,$correct);
1.51      albertel 1078: }
                   1079: 
                   1080: sub get_tolrange {
1.89      albertel 1081:     my ($ans,$tol)=@_;
                   1082:     my ($high,$low);
                   1083:     if ($tol =~ /%$/) {
                   1084: 	chop($tol);
                   1085: 	my $change=$ans*($tol/100.0);
                   1086: 	$high=$ans+$change;
                   1087: 	$low=$ans-$change;
                   1088:     } else {
                   1089: 	$high=$ans+$tol;
                   1090: 	$low=$ans-$tol;
                   1091:     }
                   1092:     return ($high,$low);
1.52      albertel 1093: }
                   1094: 
                   1095: sub get_sigrange {
1.89      albertel 1096:     my ($sig)=@_;
1.195     albertel 1097:     #&Apache::lonxml::debug("Got a sig of :$sig:");
1.166     albertel 1098:     my $courseid=$env{'request.course.id'};
1.219     albertel 1099:     if ($env{'request.state'} ne 'construct'
                   1100: 	&& lc($env{"course.$courseid.disablesigfigs"}) eq 'yes') {
1.147     albertel 1101: 	return (15,0);
                   1102:     }
1.89      albertel 1103:     my $sig_lbound;
                   1104:     my $sig_ubound;
                   1105:     if ($sig eq '') {
                   1106: 	$sig_lbound = 0; #SIG_LB_DEFAULT
                   1107: 	$sig_ubound =15; #SIG_UB_DEFAULT
                   1108:     } else {
                   1109: 	($sig_lbound,$sig_ubound) = split(/,/,$sig);
1.145     albertel 1110: 	if (!defined($sig_lbound)) {
1.89      albertel 1111: 	    $sig_lbound = 0; #SIG_LB_DEFAULT
                   1112: 	    $sig_ubound =15; #SIG_UB_DEFAULT
                   1113: 	}
1.145     albertel 1114: 	if (!defined($sig_ubound)) { $sig_ubound=$sig_lbound; }
1.133     albertel 1115:     }
                   1116:     if (($sig_ubound<$sig_lbound) ||
                   1117: 	($sig_lbound > 15) ||
                   1118: 	($sig =~/(\+|-)/ ) ) {
                   1119: 	my $errormsg=&mt("Invalid Significant figures detected")." ($sig)";
1.166     albertel 1120: 	if ($env{'request.state'} eq 'construct') {
1.133     albertel 1121: 	    $errormsg.=
                   1122: 		&Apache::loncommon::help_open_topic('Significant_Figures');
                   1123: 	}
                   1124: 	&Apache::lonxml::error($errormsg);
1.52      albertel 1125:     }
1.89      albertel 1126:     return ($sig_ubound,$sig_lbound);
1.34      albertel 1127: }
                   1128: 
1.212     albertel 1129: sub format_prior_response_string {
                   1130:     my ($mode,$answer) =@_;
                   1131:     return '<span class="LC_prior_string">'.
                   1132: 	    &HTML::Entities::encode($answer,'"<>&').'</span>';
1.211     albertel 1133: }
                   1134: 
1.34      albertel 1135: sub start_stringresponse {
1.89      albertel 1136:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1137:     my $result;
1.122     albertel 1138:     my $id = &Apache::response::start_response($parstack,$safeeval);
1.246   ! raeburn  1139:     undef(%answer);
1.89      albertel 1140:     if ($target eq 'meta') {
                   1141: 	$result=&Apache::response::meta_package_write('stringresponse');
1.122     albertel 1142:     } elsif ($target eq 'edit') {
                   1143: 	$result.=&Apache::edit::tag_start($target,$token);
                   1144: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
                   1145: 	$result.=&Apache::edit::select_arg('Type:','type',
                   1146: 			 [['cs','Case Sensitive'],['ci','Case Insensitive'],
                   1147: 			  ['mc','Case Insensitive, Any Order'],
                   1148: 			  ['re','Regular Expression']],$token);
1.152     albertel 1149: 	$result.=&Apache::edit::text_arg('String to display for answer:',
                   1150: 					 'answerdisplay',$token);
1.122     albertel 1151: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   1152:     } elsif ($target eq 'modified') {
1.146     albertel 1153: 	my $constructtag;
                   1154: 	$constructtag=&Apache::edit::get_new_args($token,$parstack,
                   1155: 						  $safeeval,'answer',
                   1156: 						  'type','answerdisplay');
                   1157: 	if ($constructtag) {
                   1158: 	    $result = &Apache::edit::rebuild_tag($token);
                   1159: 	    $result.=&Apache::edit::handle_insert();
                   1160: 	}
                   1161:     } elsif ($target eq 'web') {
                   1162: 	if (  &Apache::response::show_answer() ) {
1.152     albertel 1163: 	    my $answer=
                   1164: 	       &Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval);
                   1165: 	    if (!defined $answer || $answer eq '') {
                   1166: 		$answer=
                   1167: 		    &Apache::lonxml::get_param('answer',$parstack,$safeeval);
                   1168: 	    }
1.195     albertel 1169: 	    $Apache::inputtags::answertxt{$id}=[$answer];
1.146     albertel 1170: 	} 
1.122     albertel 1171:     } elsif ($target eq 'answer' || $target eq 'grade') {
                   1172: 	&Apache::response::reset_params();
1.89      albertel 1173:     }
                   1174:     return $result;
1.34      albertel 1175: }
                   1176: 
                   1177: sub end_stringresponse {
1.122     albertel 1178:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.189     albertel 1179: 
1.122     albertel 1180:     my $result = '';
                   1181:     my $part=$Apache::inputtags::part;
                   1182:     my $id=$Apache::inputtags::response[-1];
                   1183:     my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
                   1184:     my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
1.129     www      1185:     my $answerdisplay=&Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval);
1.122     albertel 1186:     &Apache::lonxml::debug("current $answer ".$token->[2]);
                   1187:     if (!$Apache::lonxml::default_homework_loaded) {
                   1188: 	&Apache::lonxml::default_homework_load($safeeval);
                   1189:     }
1.162     albertel 1190:     if ( $target eq 'grade' && &Apache::response::submitted() ) {
1.140     albertel 1191: 	&Apache::response::setup_params('stringresponse',$safeeval);
1.122     albertel 1192: 	$safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
                   1193: 	if ($Apache::lonhomework::type eq 'exam' ||
1.162     albertel 1194: 	    &Apache::response::submitted('scantron')) {
1.189     albertel 1195: 	    &Apache::response::scored_response($part,$id);
                   1196: 
1.122     albertel 1197: 	} else {
                   1198: 	    my $response = &Apache::response::getresponse();
                   1199: 	    if ( $response =~ /[^\s]/) {
                   1200: 		my %previous = &Apache::response::check_for_previous($response,
1.241     raeburn  1201: 								    $part,$id,
                   1202:                                                                     undef,$type);
1.122     albertel 1203: 		&Apache::lonxml::debug("submitted a $response<br>\n");
                   1204: 		&Apache::lonxml::debug($$parstack[-1] . "\n<br>");
                   1205: 		$Apache::lonhomework::results{"resource.$part.$id.submission"}=
                   1206: 		    $response;
1.142     albertel 1207: 		my ($ad,$msg);
1.122     albertel 1208: 		if ($type eq 're' ) { 
                   1209: 		    # if the RE wasn't in a var it likely got munged,
                   1210:                     # thus grab it from the var directly
                   1211: #		    my $testans=$token->[2]->{'answer'};
                   1212: #		    if ($testans !~ m/^\s*\$/) {
                   1213: #			$answer=$token->[2]->{'answer'};
                   1214: #		    }
1.143     albertel 1215: 		    ${$safeeval->varglob('LONCAPA::response')}=$response;
1.179     albertel 1216: 		    $result = &Apache::run::run('if ($LONCAPA::response=~m'.$answer.') { return 1; } else { return 0; }',$safeeval);
1.122     albertel 1217: 		    &Apache::lonxml::debug("current $response");
                   1218: 		    &Apache::lonxml::debug("current $answer");
                   1219: 		    $ad = ($result) ? 'APPROX_ANS' : 'INCORRECT';
                   1220: 		} else {
1.195     albertel 1221: 		    my @args = ('type');
                   1222: 		    my $args_ref = &setup_capa_args($safeeval,$parstack,
                   1223: 						    \@args,$response);
1.233     raeburn  1224:                     if ($$args_ref{'type'} eq '') {
                   1225:                         $$args_ref{'type'} = 'ci';
                   1226:                     }
1.195     albertel 1227: 		    &add_in_tag_answer($parstack,$safeeval);
1.243     raeburn  1228: 		    my (@final_awards,@final_msgs,@names,%ansstring);
1.195     albertel 1229: 		    foreach my $name (keys(%answer)) {
                   1230: 			&Apache::lonxml::debug(" doing $name with ".join(':',@{ $answer{$name}{'answers'} }));
                   1231: 			${$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=dclone($answer{$name});
1.233     raeburn  1232: 			my ($result, @msgs)=&Apache::run::run("&caparesponse_check_list()",$safeeval);
1.241     raeburn  1233:                         if ($$args_ref{'type'} =~ /^c[si]$/) {
1.243     raeburn  1234:                             $ansstring{$name} = pop(@msgs);
1.242     raeburn  1235:                             my $control_chars_removed = pop(@msgs);
1.241     raeburn  1236:                             my $error = pop(@msgs);
1.242     raeburn  1237:                             if (($error ne '') || 
1.245     raeburn  1238:                                 ($control_chars_removed)) {
1.243     raeburn  1239:                                 my ($symb,$courseid,$sdomain,$sname) =
1.241     raeburn  1240:                                     &Apache::lonnet::whichuser();
1.245     raeburn  1241:                                 if ($control_chars_removed) {
1.242     raeburn  1242:                                     my $showresponse = $response;
                   1243:                                     if ($response =~ /[\000-\037]/) {
                   1244:                                         $response =~ s/[\000-\037]//g;
                   1245:                                     }
                   1246:                                     if ($showresponse  =~ /[\r\n\f]/) {
                   1247:                                         my @lines = split(/[\r\n\f]+/,$showresponse);
                   1248:                                         $showresponse = join('\\n',@lines);
                   1249:                                     }
1.243     raeburn  1250:                                     &Apache::lonnet::logthis("Stringresponse grading: control characters stripped from submission ".$showresponse." for $sname:$sdomain in $courseid for part: $part response: $id and symb: $symb");
1.242     raeburn  1251:                                     $Apache::lonhomework::results{"resource.$part.$id.submission"} = $response;
                   1252:                                 }
                   1253:                                 if ($error ne '') {
1.243     raeburn  1254:                                     &Apache::lonnet::logthis("Stringresponse grading error: $error for $sname:$sdomain in $courseid for part: $part response: $id and symb: $symb");
1.242     raeburn  1255:                                 }
1.241     raeburn  1256:                             }
                   1257:                         }
1.195     albertel 1258: 			&Apache::lonxml::debug('msgs are'.join(':',@msgs));
                   1259: 			my ($awards)=split(/:/,$result);
                   1260: 			my (@awards) = split(/,/,$awards);
                   1261: 			($ad,$msg) = 
                   1262: 			    &Apache::inputtags::finalizeawards(\@awards,\@msgs);
                   1263: 			push(@final_awards,$ad);
                   1264: 			push(@final_msgs,$msg);
                   1265: 			push(@names,$name);
                   1266: 			&Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
1.122     albertel 1267: 		    }
1.240     raeburn  1268: 		    ($ad, $msg, my $name) = 
1.195     albertel 1269: 			&Apache::inputtags::finalizeawards(\@final_awards,
                   1270: 							   \@final_msgs,
                   1271: 							   \@names,1);
1.243     raeburn  1272:                     if (keys(%ansstring) > 0) {
                   1273:                         $Apache::lonhomework::results{"resource.$part.$id.answerstring"} = &Apache::lonnet::hash2str(%ansstring);
                   1274:                     }
1.122     albertel 1275: 		}
1.237     raeburn  1276:                 if (($ad eq 'INCORRECT' || $ad eq 'APPROX_ANS' ||
                   1277:                      $ad eq 'EXACT_ANS')) {
                   1278: 		    if ($Apache::lonhomework::type eq 'survey') {
                   1279: 		        $ad='SUBMITTED';
                   1280: 		    } elsif ($Apache::lonhomework::type eq 'surveycred') {
                   1281:                         $ad='SUBMITTED_CREDIT';
                   1282:                     } elsif ($Apache::lonhomework::type eq 'anonsurvey') {
                   1283:                         $ad='ANONYMOUS';
                   1284:                     } elsif ($Apache::lonhomework::type eq 'anonsurveycred') {
                   1285:                         $ad='ANONYMOUS_CREDIT';
                   1286:                     }
                   1287:                 }
1.244     raeburn  1288:                 unless (($env{'request.state'} eq 'construct') || 
                   1289:                         ($Apache::lonhomework::type eq 'randomizetry')) {
                   1290:                     if (($ad eq 'INCORRECT' || $ad eq 'APPROX_ANS' || $ad eq 'EXACT_ANS')) {
                   1291:                         if ($previous{'used'}) {
                   1292:                             if ($ad ne $previous{'award'}) {
                   1293:                                 if (($previous{'award'} eq 'INCORRECT' || 
                   1294:                                      $previous{'award'} eq 'APPROX_ANS' ||
                   1295:                                      $previous{'award'} eq 'EXACT_ANS')) {
                   1296:                                     &stringresponse_gradechange($part,$id,\%previous,
                   1297:                                                                 'cs',$response,$ad,$type);
                   1298:                                 }
                   1299:                             }
                   1300:                         } elsif ($previous{'usedci'}) {
                   1301:                             if ($ad ne $previous{'awardci'}) {
                   1302:                                 if (($previous{'awardci'} eq 'INCORRECT' || 
                   1303:                                      $previous{'awardci'} eq 'APPROX_ANS' ||
                   1304:                                      $previous{'awardci'} eq 'EXACT_ANS')) {
                   1305:                                     &stringresponse_gradechange($part,$id,\%previous,
                   1306:                                                                 'ci',$response,$ad,$type);
                   1307:                                 }
                   1308:                             }
1.241     raeburn  1309:                         }
                   1310:                     }
                   1311:                 }
1.122     albertel 1312: 		&Apache::response::handle_previous(\%previous,$ad);
                   1313: 		$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
1.142     albertel 1314: 		$Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=$msg;
1.122     albertel 1315: 	    }
                   1316: 	}
                   1317:     } elsif ($target eq 'answer' || $target eq 'analyze') {
1.200     albertel 1318: 	&add_in_tag_answer($parstack,$safeeval);
1.122     albertel 1319: 	if ($target eq 'analyze') {
                   1320: 	    push (@{ $Apache::lonhomework::analyze{"parts"} },"$part.$id");
1.125     albertel 1321: 	    $Apache::lonhomework::analyze{"$part.$id.type"} = 'stringresponse';
1.154     albertel 1322: 	    &Apache::response::check_if_computed($token,$parstack,$safeeval,
                   1323: 						 'answer');
1.122     albertel 1324: 	}
1.140     albertel 1325: 	&Apache::response::setup_params('stringresponse',$safeeval);
1.122     albertel 1326: 	if ($target eq 'answer') {
1.125     albertel 1327: 	    $result.=&Apache::response::answer_header('stringresponse');
1.122     albertel 1328: 	}
1.200     albertel 1329: 	foreach my $name (keys(%answer)) {
                   1330: 	    my @answers = @{ $answer{$name}{'answers'} };
                   1331: 	    for (my $i=0;$i<=$#answers;$i++) {
                   1332: 		my $answer_part = $answers[$i];
                   1333: 		foreach my $element (@{$answer_part}) {
                   1334: 		    if ($target eq 'answer') {
                   1335: 			$result.=&Apache::response::answer_part('stringresponse',
                   1336: 								$element);
1.243     raeburn  1337:                         if ($env{'form.grade_retrieveanswers'}) {
                   1338:                             $env{'form.grade_answers.resource.'.$part.'.'.$id} = $element;
                   1339:                         }
1.200     albertel 1340: 		    } elsif ($target eq 'analyze') {
                   1341: 			push (@{ $Apache::lonhomework::analyze{"$part.$id.answer"}{$name}[$i] },
                   1342: 			      $element);
                   1343: 		    }
                   1344: 		}
                   1345: 		if ($target eq 'answer' && $type eq 're') {
1.178     albertel 1346: 		    $result.=&Apache::response::answer_part('stringresponse',
                   1347: 							    $answerdisplay);
                   1348: 		}
1.122     albertel 1349: 	    }
1.200     albertel 1350: 	}
1.122     albertel 1351: 	my $string='Case Insensitive';
                   1352: 	if ($type eq 'mc') {
                   1353: 	    $string='Multiple Choice';
                   1354: 	} elsif ($type eq 'cs') {
                   1355: 	    $string='Case Sensitive';
                   1356: 	} elsif ($type eq 'ci') {
                   1357: 	    $string='Case Insensitive';
                   1358: 	} elsif ($type eq 're') {
                   1359: 	    $string='Regular Expression';
                   1360: 	}
                   1361: 	if ($target eq 'answer') {
1.166     albertel 1362: 	    if ($env{'form.answer_output_mode'} eq 'tex') {
1.125     albertel 1363: 		$result.=&Apache::response::answer_part('stringresponse',
1.122     albertel 1364: 							"$string");
                   1365: 	    } else {
1.125     albertel 1366: 		$result.=&Apache::response::answer_part('stringresponse',
1.122     albertel 1367: 							"<b>$string</b>");
                   1368: 	    }
                   1369: 	} elsif ($target eq 'analyze') {
                   1370: 	    push (@{$Apache::lonhomework::analyze{"$part.$id.str_type"}},
                   1371: 		  $type);
                   1372: 	}
                   1373: 	if ($target eq 'answer') {
1.125     albertel 1374: 	    $result.=&Apache::response::answer_footer('stringresponse');
1.122     albertel 1375: 	}
                   1376:     } elsif ($target eq 'edit') {
                   1377: 	$result.='</td></tr>'.&Apache::edit::end_table;
1.211     albertel 1378:     } elsif ($target eq 'web' || $target eq 'tex') {
1.212     albertel 1379: 	&Apache::response::setup_prior_tries_hash(\&format_prior_response_string);
1.122     albertel 1380:     }
                   1381:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
                   1382: 	$target eq 'tex' || $target eq 'analyze') {
1.220     albertel 1383: 	&Apache::lonxml::increment_counter(&Apache::response::repetition(),
                   1384: 					   "$part.$id");
                   1385: 	if ($target eq 'analyze') {
                   1386: 	    &Apache::lonhomework::set_bubble_lines();
                   1387: 	}
1.122     albertel 1388:     }
                   1389:     &Apache::response::end_response;
                   1390:     return $result;
1.44      albertel 1391: }
                   1392: 
                   1393: sub start_formularesponse {
1.89      albertel 1394:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1395:     my $result;
                   1396:     if ($target eq 'meta') {
1.104     bowersj2 1397: 	&Apache::response::start_response($parstack,$safeeval);
1.89      albertel 1398: 	$result=&Apache::response::meta_package_write('formularesponse');
1.104     bowersj2 1399: 	&Apache::response::end_response();
1.89      albertel 1400:     } else {
                   1401: 	$result.=&start_numericalresponse(@_);
                   1402:     }
                   1403:     return $result;
1.44      albertel 1404: }
                   1405: 
                   1406: sub end_formularesponse {
1.89      albertel 1407:     return end_numericalresponse(@_);
1.3       albertel 1408: }
                   1409: 
1.1       albertel 1410: 1;
1.3       albertel 1411: __END__
1.89      albertel 1412: 

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