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

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

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