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

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

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