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

1.3       albertel    1: # The LearningOnline Network with CAPA
                      2: # caparesponse definition
1.47      albertel    3: #
1.185   ! albertel    4: # $Id: caparesponse.pm,v 1.184 2005/12/06 10:01:57 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.184     albertel  303: 		    my ($values,$display)=&make_numerical_bubbles($partid,$id,
                    304: 						  $target,$parstack,$safeeval);
1.158     albertel  305: 		    $response=$values->[$response];
1.95      albertel  306: 		}
1.115     albertel  307: 		$Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
1.181     albertel  308: 		my ($ad,$msg)=&check_submission($response,$partid,$id,
                    309: 						$tag,$parstack,$safeeval);
1.167     albertel  310: 
1.142     albertel  311: 		&Apache::lonxml::debug('ad is'.$ad);
                    312: 		if ($ad eq 'SIG_FAIL') {
                    313: 		    my ($sig_u,$sig_l)=
                    314: 			&get_sigrange($Apache::inputtags::params{'sig'});
                    315: 		    $msg=join(':',$msg,$sig_l,$sig_u);
                    316: 		    &Apache::lonxml::debug("sigs bad $sig_u $sig_l ".
                    317: 					   $Apache::inputtags::params{'sig'});
                    318: 		}
1.95      albertel  319: 		&Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
1.163     albertel  320: 		if ($Apache::lonhomework::type eq 'survey' &&
                    321: 		    ($ad eq 'INCORRECT' || $ad eq 'APPROX_ANS' ||
                    322: 		     $ad eq 'EXACT_ANS')) {
                    323: 		    $ad='SUBMITTED';
                    324: 		}
1.95      albertel  325: 		&Apache::response::handle_previous(\%previous,$ad);
                    326: 		$Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
1.142     albertel  327: 		$Apache::lonhomework::results{"resource.$partid.$id.awardmsg"}=$msg;
1.95      albertel  328: 		$result='';
1.90      albertel  329: 	    }
1.89      albertel  330: 	}
                    331:     } elsif ($target eq 'web' || $target eq 'tex') {
1.90      albertel  332: 	my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
                    333: 						     $safeeval);
1.89      albertel  334: 	my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
                    335: 	my $status = $Apache::inputtags::status['-1'];
                    336: 	if ($Apache::lonhomework::type eq 'exam') {
1.184     albertel  337: 	    my ($bubble_values,$bubble_display)=
                    338: 		&make_numerical_bubbles($partid,$id,$target,$parstack,
                    339: 					$safeeval);
                    340: 	    my $number_of_bubbles = scalar(@{ $bubble_values });
1.89      albertel  341: 	    my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
                    342: 						    $safeeval);
                    343: 	    my @alphabet=('A'..'Z');
                    344: 	    if ($target eq 'web') {
1.125     albertel  345: 		if ($tag eq 'numericalresponse') {
1.89      albertel  346: 		    if ($unit=~/\S/) {$result.=' (in '.$unit.')<br /><br />';}
                    347: 		    $result.= '<table border="1"><tr>';
1.116     albertel  348: 		    my $previous=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.$id.submission"};
1.90      albertel  349: 		    for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
1.116     albertel  350: 			my $checked='';
1.158     albertel  351: 			if ($previous eq $bubble_values->[$ind]) {
1.116     albertel  352: 			    $checked=" checked='on' ";
                    353: 			}
1.90      albertel  354: 			$result.='<td><input type="radio" name="HWVAL_'.$id.
1.158     albertel  355: 			    '" value="'.$bubble_values->[$ind].'" '.$checked
1.116     albertel  356: 			    .' /><b>'.$alphabet[$ind].'</b>: '.
1.158     albertel  357: 			    $bubble_display->[$ind].'</td>';
1.89      albertel  358: 		    }
                    359: 		    $result.='</tr></table>';
1.125     albertel  360: 		} elsif ($tag eq 'formularesponse') {
1.90      albertel  361: 		    $result.= '<br /><br /><font color="red">
                    362:                            <textarea name="HWVAL_'.$id.'" rows="4" cols="50">
                    363:                            </textarea></font> <br /><br />';
1.89      albertel  364: 		}
                    365: 	    } elsif ($target eq 'tex') {
1.107     sakharuk  366: 		if ((defined $unit) and ($unit=~/\S/) and ($Apache::lonhomework::type eq 'exam')) {
1.89      albertel  367: 		    $result.=' \textit{(in} \verb|'.$unit.'|\textit{)} ';
                    368: 		}
1.125     albertel  369: 		if ($tag eq 'numericalresponse') {
1.90      albertel  370: 		    my ($celllength,$number_of_tables,@table_range)=
1.159     albertel  371: 			&get_table_sizes($number_of_bubbles,$bubble_display);
1.89      albertel  372: 		    my $j=0;
                    373: 		    my $cou=0;
                    374: 		    $result.='\vskip -1 mm \noindent \begin{enumerate}\item[\textbf{'.$Apache::lonxml::counter.'}.]';
                    375: 		    for (my $i=0;$i<$number_of_tables;$i++) {
1.144     sakharuk  376: 			$result.='\vskip -1 mm \noindent \setlength{\tabcolsep}{2 mm}\begin{tabular}{';
1.90      albertel  377: 			for (my $ind=0;$ind<$table_range[$j];$ind++) {
1.128     sakharuk  378: 			    $result.='p{3 mm}p{'.$celllength.' mm}';
1.89      albertel  379: 			}
                    380: 			$result.='}';
1.90      albertel  381: 			for (my $ind=$cou;$ind<$cou+$table_range[$j];$ind++) {
1.159     albertel  382: 			    $result.='\hskip -4 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$ & \hskip -3 mm {\small '.$bubble_display->[$ind].'} ';
1.89      albertel  383: 			    if ($ind != $cou+$table_range[$j]-1) {$result.=' & ';}
                    384: 			}
                    385: 			$cou += $table_range[$j];
                    386: 			$j++;
                    387: 			$result.='\\\\\end{tabular}\vskip 0 mm ';
                    388: 		    }
                    389: 		    $result.='\end{enumerate}';
                    390: 		} else {
                    391: 		    $result.='\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
1.93      albertel  392: 		    my $repetition = &Apache::response::repetition();
1.89      albertel  393: 		    $result.='\begin{enumerate}';
                    394: 		    for (my $i=0;$i<$repetition;$i++) {
1.93      albertel  395: 			$result.='\item[\textbf{'.($Apache::lonxml::counter+$i).'}.]\textit{Leave blank on scoring form}\vskip 0 mm';
1.89      albertel  396: 		    }
1.90      albertel  397: 		    $increment=$repetition;
1.89      albertel  398: 		    $result.= '\end{enumerate}';
                    399: 		}
                    400: 	    }
                    401: 	}
                    402:     } elsif ($target eq 'edit') {
                    403: 	$result.='</td></tr>'.&Apache::edit::end_table;
                    404:     } elsif ($target eq 'answer' || $target eq 'analyze') {
1.167     albertel  405: 	my $part_id="$partid.$id";
1.89      albertel  406: 	if ($target eq 'analyze') {
                    407: 	    push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
1.125     albertel  408: 	    $Apache::lonhomework::analyze{"$part_id.type"} = $tag;
1.117     albertel  409: 	    my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
1.161     albertel  410: 	    if ($#incorrect eq 0) { @incorrect=(split(/,/,$incorrect[0])); }
1.117     albertel  411: 	    push (@{ $Apache::lonhomework::analyze{"$part_id.incorrect"} }, @incorrect);
1.154     albertel  412: 	    &Apache::response::check_if_computed($token,$parstack,
                    413: 						 $safeeval,'answer');
1.89      albertel  414: 	}
1.125     albertel  415: 	if (scalar(@$tagstack)) {
1.140     albertel  416: 	    &Apache::response::setup_params($tag,$safeeval);
1.125     albertel  417: 	}
1.89      albertel  418: 	my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
1.58      sakharuk  419: 	my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
                    420: 	my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval);
1.89      albertel  421: 	my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
                    422: 	
                    423: 	if ($target eq 'answer') {
1.125     albertel  424: 	    $result.=&Apache::response::answer_header($tag);
1.185   ! albertel  425: 	    if ($tag eq 'numericalresponse'
        !           426: 		&& $Apache::lonhomework::type eq 'exam') {
1.184     albertel  427: 		my ($bubble_values,undef,$correct) = &make_numerical_bubbles($partid,
                    428: 					     $id,$target,$parstack,$safeeval);
                    429: 		$result.=&Apache::response::answer_part($tag,$correct);
                    430: 	    }
1.89      albertel  431: 	}
1.176     albertel  432: 	my ($sigline,$tolline);
1.89      albertel  433: 	for(my $i=0;$i<=$#answers;$i++) {
                    434: 	    my $ans=$answers[$i];
1.90      albertel  435: 	    my $fmt=$formats[0];
                    436: 	    if (@formats && $#formats) {$fmt=$formats[$i];}
1.89      albertel  437: 	    my ($high,$low);
                    438: 	    if ($Apache::inputtags::params{'tol'}) {
                    439: 		($high,$low)=&get_tolrange($ans,$Apache::inputtags::params{'tol'});
                    440: 	    }
                    441: 	    my ($sighigh,$siglow);
                    442: 	    if ($Apache::inputtags::params{'sig'}) {
                    443: 		($sighigh,$siglow)=&get_sigrange($Apache::inputtags::params{'sig'});
                    444: 	    }
1.125     albertel  445: 	    if ($fmt && $tag eq 'numericalresponse') {
1.112     albertel  446: 		$fmt=~s/e/E/g;
1.156     albertel  447: 		if ($unit=~/\$/) { $fmt="\$".$fmt; $unit=~s/\$//g; }
                    448: 		if ($unit=~/\,/) { $fmt="\,".$fmt; $unit=~s/\,//g; }
1.153     albertel  449: 		$ans = &format_number($ans,$fmt,$target,$safeeval);
1.155     albertel  450: 		#if ($high) {
                    451: 		#    $high=&format_number($high,$fmt,$target,$safeeval);
                    452: 		#    $low =&format_number($low,$fmt,$target,$safeeval);
                    453: 		#}
1.60      sakharuk  454: 	    }
1.89      albertel  455: 	    if ($target eq 'answer') {
1.176     albertel  456: 		if ($high && $tag eq 'numericalresponse') {
                    457: 		    $ans.=' ['.$low.','.$high.']';
                    458: 		    $tolline .= "[$low, $high]";
                    459: 		}
1.145     albertel  460: 		if (defined($sighigh) && $tag eq 'numericalresponse') {
1.166     albertel  461: 		    if ($env{'form.answer_output_mode'} eq 'tex') {
1.106     sakharuk  462: 			$ans.= " Sig $siglow - $sighigh";
1.98      sakharuk  463: 		    } else {
1.101     albertel  464: 			$ans.= " Sig <i>$siglow - $sighigh</i>";
1.176     albertel  465: 			$sigline .= "[$siglow, $sighigh]";
1.98      sakharuk  466: 		    }
                    467: 		}
1.125     albertel  468: 		$result.=&Apache::response::answer_part($tag,$ans);
1.89      albertel  469: 	    } elsif ($target eq 'analyze') {
                    470: 		push (@{ $Apache::lonhomework::analyze{"$part_id.answer"} }, $ans);
                    471: 		if ($high) {
                    472: 		    push (@{ $Apache::lonhomework::analyze{"$part_id.ans_high"} }, $high);
                    473: 		    push (@{ $Apache::lonhomework::analyze{"$part_id.ans_low"} }, $low);
1.58      sakharuk  474: 		}
1.165     albertel  475: 		if ($fmt) {
                    476: 		    push (@{ $Apache::lonhomework::analyze{"$part_id.format"} }, $fmt);
                    477: 		}
1.79      sakharuk  478: 	    }
1.176     albertel  479: 	}
                    480: 
                    481: 	my @fmt_ans;
                    482: 	for(my $i=0;$i<=$#answers;$i++) {
                    483: 	    my $ans=$answers[$i];
                    484: 	    my $fmt=$formats[0];
                    485: 	    if (@formats && $#formats) {$fmt=$formats[$i];}
                    486: 	    if ($fmt && $tag eq 'numericalresponse') {
                    487: 		$fmt=~s/e/E/g;
                    488: 		if ($unit=~/\$/) { $fmt="\$".$fmt; $unit=~s/\$//g; }
                    489: 		if ($unit=~/\,/) { $fmt="\,".$fmt; $unit=~s/\,//g; }
                    490: 		$ans = &format_number($ans,$fmt,$target,$safeeval);
1.177     albertel  491: 		if ($fmt=~/\$/ && $unit!~/\$/) { $ans=~s/\$//; }
1.176     albertel  492: 	    }
                    493: 	    push(@fmt_ans,$ans);
                    494: 	}
                    495: 	my $response=join(', ',@fmt_ans);
                    496: 	my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.
                    497: 					  $id.'.turnoffunit');
                    498: 	if ($unit ne ''  && 
                    499: 	    ! ($Apache::lonhomework::type eq 'exam' ||
                    500: 	       lc($hideunit) eq "yes") )  {
                    501: 	    my $cleanunit=$unit;
                    502: 	    $cleanunit=~s/\$\,//g;
                    503: 	    $response.=" $cleanunit";
                    504: 	}
                    505: 
1.181     albertel  506: 	my ($ad,$msg)=&check_submission($response,$partid,$id,$tag,
                    507: 					$parstack,$safeeval);
1.176     albertel  508: 	if ($ad ne 'EXACT_ANS' && $ad ne 'APPROX_ANS') {
                    509: 	    my $error;
                    510: 	    if ($tag eq 'formularesponse') {
1.183     albertel  511: 		$error=&mt('Computer\'s answer is incorrect ("[_1]").',$response);
1.176     albertel  512: 	    } else {
                    513: 		# answer failed check if it is sig figs that is failing
1.181     albertel  514: 		my ($ad,$msg)=&check_submission($response,$partid,$id,
                    515: 						$tag,$parstack,
                    516: 						$safeeval,1);
1.176     albertel  517: 		if ($sigline ne '') {
                    518: 		    $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  519: 		} else {
1.180     albertel  520: 		    $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  521: 		}
1.167     albertel  522: 	    }
1.176     albertel  523: 	    if ($ad ne 'EXACT_ANS' && $ad ne 'APPROX_ANS') {
                    524: 		&Apache::lonxml::error($error);
                    525: 	    } else {
                    526: 		&Apache::lonxml::warning($error);
                    527: 	    }
1.36      albertel  528: 	}
1.176     albertel  529: 
1.102     albertel  530: 	if (defined($unit) and ($unit ne '') and
1.125     albertel  531: 	    $tag eq 'numericalresponse') {
1.89      albertel  532: 	    if ($target eq 'answer') {
1.166     albertel  533: 		if ($env{'form.answer_output_mode'} eq 'tex') {
1.125     albertel  534: 		    $result.=&Apache::response::answer_part($tag,
1.106     sakharuk  535: 							    " Unit: $unit ");
1.98      sakharuk  536: 		} else {
1.125     albertel  537: 		    $result.=&Apache::response::answer_part($tag,
1.98      sakharuk  538: 							    "Unit: <b>$unit</b>");
                    539: 		}
1.89      albertel  540: 	    } elsif ($target eq 'analyze') {
1.103     albertel  541: 		push (@{ $Apache::lonhomework::analyze{"$part_id.unit"} }, $unit);
1.89      albertel  542: 	    }
                    543: 	}
1.125     albertel  544: 	if ($tag eq 'formularesponse' && $target eq 'answer') {
1.89      albertel  545: 	    my $samples=&Apache::lonxml::get_param('samples',$parstack,$safeeval);
1.125     albertel  546: 	    $result.=&Apache::response::answer_part($tag,$samples);
1.89      albertel  547: 	}
                    548: 	if ($target eq 'answer') {
1.125     albertel  549: 	    $result.=&Apache::response::answer_footer($tag);
1.89      albertel  550: 	}
1.69      sakharuk  551:     }
1.121     sakharuk  552:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
1.90      albertel  553: 	$target eq 'tex' || $target eq 'analyze') {
                    554: 	&Apache::lonxml::increment_counter($increment);
                    555:     }
1.89      albertel  556:     &Apache::response::end_response;
                    557:     return $result;
1.90      albertel  558: }
                    559: 
                    560: sub get_table_sizes {
1.128     sakharuk  561:     my ($number_of_bubbles,$rbubble_values)=@_;
                    562:     my $scale=2; #mm for one digit
                    563:     my $cell_width=0;
                    564:     foreach my $member (@$rbubble_values) {
                    565: 	my $cell_width_real=0;
1.160     albertel  566: 	if ($member=~/(\+|-)?(\d*)\.?(\d*)\s*\$?\\times\s*10\^{(\+|-)?(\d+)}\$?/) {
1.138     sakharuk  567: 	    $cell_width_real=(length($2)+length($3)+length($5)+7)*$scale;
                    568: 	} elsif ($member=~/(\d*)\.?(\d*)(E|e)(\+|-)?(\d*)/) {
1.128     sakharuk  569: 	    $cell_width_real=(length($1)+length($2)+length($5)+9)*$scale;
1.138     sakharuk  570:         } elsif ($member=~/(\d*)\.(\d*)/) {
1.132     sakharuk  571: 	    $cell_width_real=(length($1)+length($2)+3)*$scale;
1.128     sakharuk  572: 	} else {
1.138     sakharuk  573: 	    $cell_width_real=(length($member)+1)*$scale*0.9;
1.128     sakharuk  574: 	}
                    575: 	if ($cell_width_real>$cell_width) {$cell_width=$cell_width_real;}
1.90      albertel  576:     }
1.132     sakharuk  577:     $cell_width+=8; 
1.130     sakharuk  578:     my $textwidth;
1.166     albertel  579:     if ($env{'form.textwidth'} ne '') {
                    580: 	$env{'form.textwidth'}=~/(\d*)\.?(\d*)/;
1.132     sakharuk  581: 	$textwidth=$1.'.'.$2;
1.130     sakharuk  582:     } else {
1.166     albertel  583: 	$env{'form.textwidth'}=~/(\d+)\.?(\d*)/;
1.132     sakharuk  584: 	$textwidth=$1.'.'.$2;
1.130     sakharuk  585:     }
1.128     sakharuk  586:     my $bubbles_per_line=int($textwidth/$cell_width);
1.151     sakharuk  587:     if ($bubbles_per_line > $number_of_bubbles) {
                    588: 	$bubbles_per_line=$number_of_bubbles;
                    589:     }elsif (($bubbles_per_line > $number_of_bubbles/2) && ($number_of_bubbles % 2==0)) {$bubbles_per_line=$number_of_bubbles/2;}
1.128     sakharuk  590:     my $number_of_tables = int($number_of_bubbles/$bubbles_per_line);
1.90      albertel  591:     my @table_range = ();
1.128     sakharuk  592:     for (my $i=0;$i<$number_of_tables;$i++) {push @table_range,$bubbles_per_line;}
                    593:     if ($number_of_bubbles % $bubbles_per_line) {
1.90      albertel  594: 	$number_of_tables++;
1.128     sakharuk  595: 	push @table_range,($number_of_bubbles % $bubbles_per_line);
1.90      albertel  596:     }
1.128     sakharuk  597:     $cell_width-=8;
1.136     sakharuk  598:     $cell_width=$cell_width*3/4;
1.128     sakharuk  599:     return ($cell_width,$number_of_tables,@table_range);
1.90      albertel  600: }
                    601: 
                    602: sub format_number {
1.153     albertel  603:     my ($number,$format,$target,$safeeval)=@_;
1.90      albertel  604:     my $ans;
1.153     albertel  605:     if ($format eq '') {
1.90      albertel  606: 	#What is the number? (integer,decimal,floating point)
                    607: 	if ($number=~/^(\d*\.?\d*)(E|e)(\d*)$/) {
1.113     sakharuk  608: 	    $format = '3e';
1.90      albertel  609: 	} elsif ($number=~/^(\d*)\.(\d*)$/) {
                    610: 	    $format = '4f';
                    611: 	} elsif ($number=~/^(\d*)$/) {
                    612: 	    $format = 'd';
                    613: 	}
                    614:     }
1.156     albertel  615:     if (!$Apache::lonxml::default_homework_loaded) {
                    616: 	&Apache::lonxml::default_homework_load($safeeval);
                    617:     }
1.153     albertel  618:     $ans=&Apache::run::run("&prettyprint(q\0$number\0,q\0$format\0,q\0$target\0)",$safeeval);
1.90      albertel  619:     return $ans;
                    620: }
                    621: 
                    622: sub make_numerical_bubbles {
1.184     albertel  623:     my ($part,$id,$target,$parstack,$safeeval) =@_;
                    624:     
                    625:     my $number_of_bubbles = 
                    626: 	&Apache::response::get_response_param($part.'_'.$id,'numbubbles',8);
                    627: 
                    628:     my ($format)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
                    629:     my ($answer)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
                    630:     my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,
                    631: 						   $safeeval);
                    632:     if ($#incorrect eq 0) { @incorrect=(split(/,/,$incorrect[0])); }
                    633:     
1.158     albertel  634:     my @bubble_values=();
1.184     albertel  635:     my @alphabet=('A'..'Z');
                    636: 
                    637:     &Apache::lonxml::debug("answer is $answer incorrect is @incorrect");
1.119     albertel  638:     my @oldseed=&Math::Random::random_get_seed();
1.184     albertel  639:     if (@incorrect) {
                    640: 	&Apache::lonxml::debug("inside ".(scalar(@incorrect)+1 gt $number_of_bubbles));
                    641: 	if (defined($incorrect[0]) &&
                    642: 	    scalar(@incorrect)+1 >= $number_of_bubbles) {
                    643: 	    &Apache::lonxml::debug("inside ".(scalar(@incorrect)+1).":$number_of_bubbles");
1.117     albertel  644: 	    &Apache::response::setrandomnumber();
1.184     albertel  645: 	    my @rand_inc=&Math::Random::random_permutation(@incorrect);
1.117     albertel  646: 	    @bubble_values=@rand_inc[0..($number_of_bubbles-2)];
                    647: 	    @bubble_values=sort {$a <=> $b} (@bubble_values,$answer);
1.184     albertel  648: 	    &Apache::lonxml::debug("Answer was :$answer: returning :".$#bubble_values.": which are :".join(':',@bubble_values));
1.119     albertel  649: 	    &Math::Random::random_set_seed(@oldseed);
1.184     albertel  650: 
                    651: 	    my $correct;
                    652: 	    for(my $i=0; $i<=$#bubble_values;$i++) {
                    653: 		if ($bubble_values[$i] eq $answer) {
                    654: 		    $correct = $alphabet[$i];
                    655: 		    last;
                    656: 		}
                    657: 	    }
                    658: 
1.134     albertel  659: 	    if (defined($format) && $format ne '') {
1.158     albertel  660: 		my @bubble_display;
1.137     albertel  661: 		foreach my $value (@bubble_values) {
1.158     albertel  662: 		    push(@bubble_display,
                    663: 			 &format_number($value,$format,$target,$safeeval));
1.134     albertel  664: 		}
1.184     albertel  665: 		return (\@bubble_values,\@bubble_display,$correct);
1.158     albertel  666: 	    } else {
1.184     albertel  667: 		return (\@bubble_values,\@bubble_values,$correct);
1.134     albertel  668: 	    }
1.117     albertel  669: 	}
1.184     albertel  670: 	if (defined($incorrect[0]) &&
                    671: 	    scalar(@incorrect)+1 < $number_of_bubbles) {
                    672: 	    &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  673: 	}
1.117     albertel  674:     }
1.90      albertel  675:     my @factors = (1.13,1.17,1.25,1.33,1.45); #default values of factors
1.126     albertel  676:     my @powers = (1..$number_of_bubbles);
1.90      albertel  677:     &Apache::response::setrandomnumber();
                    678:     my $ind=&Math::Random::random_uniform_integer(1,0,$#powers);
                    679:     my $power = $powers[$ind];
                    680:     $ind=&Math::Random::random_uniform_integer(1,0,$#factors);
                    681:     my $factor = $factors[$ind];
1.158     albertel  682:     my @bubble_display;
1.90      albertel  683:     for ($ind=0;$ind<$number_of_bubbles;$ind++) {
1.91      albertel  684: 	$bubble_values[$ind] = $answer*($factor**($power-$powers[$#powers-$ind]));
1.158     albertel  685: 	$bubble_display[$ind] = &format_number($bubble_values[$ind],
1.153     albertel  686: 					       $format,$target,$safeeval);
1.91      albertel  687: 
1.90      albertel  688:     }
1.184     albertel  689:     my $correct = $alphabet[$number_of_bubbles-$power];
1.119     albertel  690:     &Math::Random::random_set_seed(@oldseed);
1.184     albertel  691:     return (\@bubble_values,\@bubble_display,$correct);
1.51      albertel  692: }
                    693: 
                    694: sub get_tolrange {
1.89      albertel  695:     my ($ans,$tol)=@_;
                    696:     my ($high,$low);
                    697:     if ($tol =~ /%$/) {
                    698: 	chop($tol);
                    699: 	my $change=$ans*($tol/100.0);
                    700: 	$high=$ans+$change;
                    701: 	$low=$ans-$change;
                    702:     } else {
                    703: 	$high=$ans+$tol;
                    704: 	$low=$ans-$tol;
                    705:     }
                    706:     return ($high,$low);
1.52      albertel  707: }
                    708: 
                    709: sub get_sigrange {
1.89      albertel  710:     my ($sig)=@_;
                    711:     &Apache::lonxml::debug("Got a sig of :$sig:");
1.166     albertel  712:     my $courseid=$env{'request.course.id'};
                    713:     if (lc($env{"course.$courseid.disablesigfigs"}) eq 'yes') {
1.147     albertel  714: 	return (15,0);
                    715:     }
1.89      albertel  716:     my $sig_lbound;
                    717:     my $sig_ubound;
                    718:     if ($sig eq '') {
                    719: 	$sig_lbound = 0; #SIG_LB_DEFAULT
                    720: 	$sig_ubound =15; #SIG_UB_DEFAULT
                    721:     } else {
                    722: 	($sig_lbound,$sig_ubound) = split(/,/,$sig);
1.145     albertel  723: 	if (!defined($sig_lbound)) {
1.89      albertel  724: 	    $sig_lbound = 0; #SIG_LB_DEFAULT
                    725: 	    $sig_ubound =15; #SIG_UB_DEFAULT
                    726: 	}
1.145     albertel  727: 	if (!defined($sig_ubound)) { $sig_ubound=$sig_lbound; }
1.133     albertel  728:     }
                    729:     if (($sig_ubound<$sig_lbound) ||
                    730: 	($sig_lbound > 15) ||
                    731: 	($sig =~/(\+|-)/ ) ) {
                    732: 	my $errormsg=&mt("Invalid Significant figures detected")." ($sig)";
1.166     albertel  733: 	if ($env{'request.state'} eq 'construct') {
1.133     albertel  734: 	    $errormsg.=
                    735: 		&Apache::loncommon::help_open_topic('Significant_Figures');
                    736: 	}
                    737: 	&Apache::lonxml::error($errormsg);
1.52      albertel  738:     }
1.89      albertel  739:     return ($sig_ubound,$sig_lbound);
1.34      albertel  740: }
                    741: 
                    742: sub start_stringresponse {
1.89      albertel  743:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    744:     my $result;
1.122     albertel  745:     my $id = &Apache::response::start_response($parstack,$safeeval);
1.89      albertel  746:     if ($target eq 'meta') {
                    747: 	$result=&Apache::response::meta_package_write('stringresponse');
1.122     albertel  748:     } elsif ($target eq 'edit') {
                    749: 	$result.=&Apache::edit::tag_start($target,$token);
                    750: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
                    751: 	$result.=&Apache::edit::select_arg('Type:','type',
                    752: 			 [['cs','Case Sensitive'],['ci','Case Insensitive'],
                    753: 			  ['mc','Case Insensitive, Any Order'],
                    754: 			  ['re','Regular Expression']],$token);
1.152     albertel  755: 	$result.=&Apache::edit::text_arg('String to display for answer:',
                    756: 					 'answerdisplay',$token);
1.122     albertel  757: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                    758:     } elsif ($target eq 'modified') {
1.146     albertel  759: 	my $constructtag;
                    760: 	$constructtag=&Apache::edit::get_new_args($token,$parstack,
                    761: 						  $safeeval,'answer',
                    762: 						  'type','answerdisplay');
                    763: 	if ($constructtag) {
                    764: 	    $result = &Apache::edit::rebuild_tag($token);
                    765: 	    $result.=&Apache::edit::handle_insert();
                    766: 	}
                    767:     } elsif ($target eq 'web') {
                    768: 	if (  &Apache::response::show_answer() ) {
1.152     albertel  769: 	    my $answer=
                    770: 	       &Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval);
                    771: 	    if (!defined $answer || $answer eq '') {
                    772: 		$answer=
                    773: 		    &Apache::lonxml::get_param('answer',$parstack,$safeeval);
                    774: 	    }
                    775: 	    $Apache::inputtags::answertxt{$id}=$answer;
1.146     albertel  776: 	} 
1.122     albertel  777:     } elsif ($target eq 'answer' || $target eq 'grade') {
                    778: 	&Apache::response::reset_params();
1.89      albertel  779:     }
                    780:     return $result;
1.34      albertel  781: }
                    782: 
                    783: sub end_stringresponse {
1.122     albertel  784:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    785:     my $increment=1;
                    786:     my $result = '';
                    787:     my $part=$Apache::inputtags::part;
                    788:     my $id=$Apache::inputtags::response[-1];
                    789:     my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
                    790:     my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
1.129     www       791:     my $answerdisplay=&Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval);
1.122     albertel  792:     &Apache::lonxml::debug("current $answer ".$token->[2]);
                    793:     if (!$Apache::lonxml::default_homework_loaded) {
                    794: 	&Apache::lonxml::default_homework_load($safeeval);
                    795:     }
1.162     albertel  796:     if ( $target eq 'grade' && &Apache::response::submitted() ) {
1.140     albertel  797: 	&Apache::response::setup_params('stringresponse',$safeeval);
1.122     albertel  798: 	$safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
                    799: 	if ($Apache::lonhomework::type eq 'exam' ||
1.162     albertel  800: 	    &Apache::response::submitted('scantron')) {
1.122     albertel  801: 	    $increment=&Apache::response::scored_response($part,$id);
                    802: 	} else {
                    803: 	    my $response = &Apache::response::getresponse();
                    804: 	    if ( $response =~ /[^\s]/) {
                    805: 		my %previous = &Apache::response::check_for_previous($response,
                    806: 								    $part,$id);
                    807: 		&Apache::lonxml::debug("submitted a $response<br>\n");
                    808: 		&Apache::lonxml::debug($$parstack[-1] . "\n<br>");
                    809: 		$Apache::lonhomework::results{"resource.$part.$id.submission"}=
                    810: 		    $response;
1.142     albertel  811: 		my ($ad,$msg);
1.122     albertel  812: 		if ($type eq 're' ) { 
                    813: 		    # if the RE wasn't in a var it likely got munged,
                    814:                     # thus grab it from the var directly
                    815: #		    my $testans=$token->[2]->{'answer'};
                    816: #		    if ($testans !~ m/^\s*\$/) {
                    817: #			$answer=$token->[2]->{'answer'};
                    818: #		    }
1.143     albertel  819: 		    ${$safeeval->varglob('LONCAPA::response')}=$response;
1.179     albertel  820: 		    $result = &Apache::run::run('if ($LONCAPA::response=~m'.$answer.') { return 1; } else { return 0; }',$safeeval);
1.122     albertel  821: 		    &Apache::lonxml::debug("current $response");
                    822: 		    &Apache::lonxml::debug("current $answer");
                    823: 		    $ad = ($result) ? 'APPROX_ANS' : 'INCORRECT';
                    824: 		} else {
1.143     albertel  825: 		    my $args_ref= 
                    826: 			\%{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
                    827: 		
                    828: 		    $$args_ref{'response'}=$response;
1.122     albertel  829: 		    &Apache::lonxml::debug("current $response");
1.143     albertel  830: 		    $$args_ref{'type'}=
                    831: 			&Apache::lonxml::get_param('type',$parstack,$safeeval);
1.122     albertel  832: 		    foreach my $key (keys(%Apache::inputtags::params)) {
1.143     albertel  833: 			$$args_ref{$key}=$Apache::inputtags::params{$key};
1.122     albertel  834: 		    }
                    835: 		    &Apache::lonxml::debug('answer is'.join(':',$answer));
1.143     albertel  836: 		    @{$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=($answer);
                    837: 		    ($result, my @msgs)=&Apache::run::run("&caparesponse_check_list()",$safeeval);
1.142     albertel  838: 		    &Apache::lonxml::debug('msgs are'.join(':',@msgs));
                    839: 		    my ($awards)=split(/:/,$result);
                    840: 		    my (@awards) = split(/,/,$awards);
                    841: 		    ($ad,$msg) = &Apache::inputtags::finalizeawards(\@awards,\@msgs);
1.122     albertel  842: 		    &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
                    843: 		}
1.163     albertel  844: 		if ($Apache::lonhomework::type eq 'survey' &&
                    845: 		    ($ad eq 'INCORRECT' || $ad eq 'APPROX_ANS' ||
                    846: 		     $ad eq 'EXACT_ANS')) {
                    847: 		    $ad='SUBMITTED';
                    848: 		}
1.122     albertel  849: 		&Apache::response::handle_previous(\%previous,$ad);
                    850: 		$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
1.142     albertel  851: 		$Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=$msg;
1.122     albertel  852: 	    }
                    853: 	}
                    854:     } elsif ($target eq 'web' || $target eq 'tex') {
                    855: 	my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
                    856: 	my $status = $Apache::inputtags::status['-1'];
                    857: 	if ($Apache::lonhomework::type eq 'exam' && $target eq 'tex') {
                    858: 	    $result.='\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
                    859: 	    $increment = &Apache::response::repetition();
                    860: 	    $result.='\begin{enumerate}';
                    861: 	    for (my $i=0;$i<$increment;$i++) {
                    862: 		$result.='\item[\textbf{'.($Apache::lonxml::counter+$i).
                    863: 		    '}.]\textit{Leave blank on scoring form}\vskip 0 mm';
                    864: 	    }
                    865: 	    $result.= '\end{enumerate}';
                    866: 	}
                    867:     } elsif ($target eq 'answer' || $target eq 'analyze') {
                    868: 	if ($target eq 'analyze') {
                    869: 	    push (@{ $Apache::lonhomework::analyze{"parts"} },"$part.$id");
1.125     albertel  870: 	    $Apache::lonhomework::analyze{"$part.$id.type"} = 'stringresponse';
1.154     albertel  871: 	    &Apache::response::check_if_computed($token,$parstack,$safeeval,
                    872: 						 'answer');
1.122     albertel  873: 	}
1.140     albertel  874: 	&Apache::response::setup_params('stringresponse',$safeeval);
1.122     albertel  875: 	if ($target eq 'answer') {
1.125     albertel  876: 	    $result.=&Apache::response::answer_header('stringresponse');
1.122     albertel  877: 	}
                    878: #	foreach my $ans (@answers) {
                    879: 	    if ($target eq 'answer') {
1.125     albertel  880: 		$result.=&Apache::response::answer_part('stringresponse',$answer);
1.178     albertel  881: 		if ($type eq 're') {
                    882: 		    $result.=&Apache::response::answer_part('stringresponse',
                    883: 							    $answerdisplay);
                    884: 		}
1.122     albertel  885: 	    } elsif ($target eq 'analyze') {
                    886: 		push (@{ $Apache::lonhomework::analyze{"$part.$id.answer"} },
                    887: 		      $answer);
                    888: 	    }
                    889: #	}
                    890: 	my $string='Case Insensitive';
                    891: 	if ($type eq 'mc') {
                    892: 	    $string='Multiple Choice';
                    893: 	} elsif ($type eq 'cs') {
                    894: 	    $string='Case Sensitive';
                    895: 	} elsif ($type eq 'ci') {
                    896: 	    $string='Case Insensitive';
                    897: 	} elsif ($type eq 're') {
                    898: 	    $string='Regular Expression';
                    899: 	}
                    900: 	if ($target eq 'answer') {
1.166     albertel  901: 	    if ($env{'form.answer_output_mode'} eq 'tex') {
1.125     albertel  902: 		$result.=&Apache::response::answer_part('stringresponse',
1.122     albertel  903: 							"$string");
                    904: 	    } else {
1.125     albertel  905: 		$result.=&Apache::response::answer_part('stringresponse',
1.122     albertel  906: 							"<b>$string</b>");
                    907: 	    }
                    908: 	} elsif ($target eq 'analyze') {
                    909: 	    push (@{$Apache::lonhomework::analyze{"$part.$id.str_type"}},
                    910: 		  $type);
                    911: 	}
                    912: 	if ($target eq 'answer') {
1.125     albertel  913: 	    $result.=&Apache::response::answer_footer('stringresponse');
1.122     albertel  914: 	}
                    915:     } elsif ($target eq 'edit') {
                    916: 	$result.='</td></tr>'.&Apache::edit::end_table;
                    917:     }
                    918:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
                    919: 	$target eq 'tex' || $target eq 'analyze') {
                    920: 	&Apache::lonxml::increment_counter($increment);
                    921:     }
                    922:     &Apache::response::end_response;
                    923:     return $result;
1.44      albertel  924: }
                    925: 
                    926: sub start_formularesponse {
1.89      albertel  927:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    928:     my $result;
                    929:     if ($target eq 'meta') {
1.104     bowersj2  930: 	&Apache::response::start_response($parstack,$safeeval);
1.89      albertel  931: 	$result=&Apache::response::meta_package_write('formularesponse');
1.104     bowersj2  932: 	&Apache::response::end_response();
1.89      albertel  933:     } else {
                    934: 	$result.=&start_numericalresponse(@_);
                    935:     }
                    936:     return $result;
1.44      albertel  937: }
                    938: 
                    939: sub end_formularesponse {
1.89      albertel  940:     return end_numericalresponse(@_);
1.3       albertel  941: }
                    942: 
1.1       albertel  943: 1;
1.3       albertel  944: __END__
1.89      albertel  945: 

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