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

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

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