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

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

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