File:  [LON-CAPA] / loncom / homework / caparesponse / caparesponse.pm
Revision 1.202: download - view: text, annotated - select for diffs
Fri Dec 15 22:11:43 2006 UTC (17 years, 6 months ago) by www
Branches: MAIN
CVS tags: HEAD
Make formula fix work with <mathresponse>

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

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