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

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

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