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

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

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