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

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

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