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

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

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