File:  [LON-CAPA] / loncom / homework / caparesponse / caparesponse.pm
Revision 1.263: download - view: text, annotated - select for diffs
Tue Oct 5 02:41:32 2021 UTC (2 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, version_2_11_X, version_2_11_4_uiuc, version_2_11_4_msu, version_2_11_4, HEAD
- Without this, get "TypeError" in caparesponse_capa_check_answer() for some
  random seeds in bubblesheet grading on Ubuntu 20LTS (perl 5.30/swig 4.0.1).

    1: # The LearningOnline Network with CAPA
    2: # caparesponse definition
    3: #
    4: # $Id: caparesponse.pm,v 1.263 2021/10/05 02:41:32 raeburn 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::lonmsg();
   37: use Apache::response();
   38: use Storable qw(dclone);
   39: use Apache::lonnet;
   40: 
   41: BEGIN {
   42:     &Apache::lonxml::register('Apache::caparesponse',('numericalresponse','stringresponse','formularesponse'));
   43: }
   44: 
   45: my %answer;
   46: my @answers;
   47: my @alphabet=('A'..'Z');
   48: 
   49: sub get_answer { return %answer; };
   50: sub push_answer{ push(@answers,dclone(\%answer)); undef(%answer) }
   51: sub pop_answer { %answer = %{pop(@answers)}; };
   52: 
   53: my $cur_name;
   54: my $tag_internal_answer_name = 'INTERNAL';
   55: 
   56: sub start_answer {
   57:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   58:     my $result;
   59:     $cur_name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
   60:     if ($cur_name =~ /^\s*$/) { $cur_name = $Apache::lonxml::curdepth; }
   61:     my $type = &Apache::lonxml::get_param('type',$parstack,$safeeval);
   62:     if (!defined($type) && $tagstack->[-2] eq 'answergroup') {
   63: 	$type = &Apache::lonxml::get_param('type',$parstack,$safeeval,-2);
   64:     }
   65:     if (!defined($type)) { $type = 'ordered' };
   66:     $answer{$cur_name}= { 'type' => $type,
   67: 			  'answers' => [] };
   68:     if ($target eq 'edit') {
   69:   	$result.=&Apache::edit::tag_start($target,$token);
   70: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
   71: 	$result.=&Apache::edit::select_arg('Type:','type',
   72: 					   [['ordered',  'Ordered'  ],
   73: 					    ['unordered','Unordered'],],
   74: 					   $token);
   75: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
   76:     } elsif ($target eq 'modified') {
   77: 	my $constructtag = &Apache::edit::get_new_args($token,$parstack,
   78: 						       $safeeval,'name',
   79: 						       'type');
   80: 	if ($constructtag) {
   81: 	    $result = &Apache::edit::rebuild_tag($token);
   82: 	    $result.= &Apache::edit::handle_insert();
   83: 	}
   84:     }
   85:     return $result;
   86: }
   87: 
   88: sub end_answer {
   89:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   90:     my $result;
   91:     if ($target eq 'edit') {
   92: 	$result .= &Apache::edit::tag_end();
   93:     }
   94: 
   95:     undef($cur_name);
   96:     return $result;
   97: }
   98: 
   99: sub insert_answer {
  100:     return '
  101:         <answer>
  102:             <value></value>
  103:         </answer>';
  104: }
  105: 
  106: sub start_answergroup {
  107:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  108:     my $result;
  109:     if ($target eq 'edit') {
  110:   	$result.=&Apache::edit::tag_start($target,$token);
  111: 	$result.=&Apache::edit::select_arg('Type:','type',
  112: 					   [['ordered',  'Ordered'  ],
  113: 					    ['unordered','Unordered'],],
  114: 					   $token);
  115: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  116:     } elsif ($target eq 'modified') {
  117: 	my $constructtag = &Apache::edit::get_new_args($token,$parstack,
  118: 						       $safeeval,'name',
  119: 						       'type');
  120: 	if ($constructtag) {
  121: 	    $result = &Apache::edit::rebuild_tag($token);
  122: 	    $result.= &Apache::edit::handle_insert();
  123: 	}
  124:     }
  125:     return $result;
  126: }
  127: 
  128: sub end_answergroup {
  129:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  130:     my $result;
  131:     if ($target eq 'web') {
  132:     	if (  &Apache::response::show_answer() ) {  
  133: 	    my $partid = $Apache::inputtags::part;
  134: 	    my $id = $Apache::inputtags::response[-1];
  135: 	    &set_answertext($Apache::lonhomework::history{"resource.$partid.$id.answername"},
  136: 			    $target,$token,$tagstack,$parstack,$parser,
  137: 			    $safeeval,-2);
  138: 	}
  139:     } elsif ($target eq 'edit') {
  140: 	$result .= &Apache::edit::tag_end();
  141:     }
  142:     return $result;
  143: }
  144: 
  145: sub insert_answergroup {
  146:     return '
  147:     <answergroup>
  148:         <answer>
  149:             <value></value>
  150:         </answer>
  151:     </answergroup>';
  152: }
  153: 
  154: sub start_value {
  155:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  156:     my $result;
  157:     if ( $target eq 'web' || $target eq 'tex' ||
  158: 	 $target eq 'grade' || $target eq 'webgrade' ||
  159: 	 $target eq 'answer' || $target eq 'analyze' ) {
  160: 	my $bodytext = &Apache::lonxml::get_all_text("/value",$parser,$style);
  161: 	$bodytext = &Apache::run::evaluate($bodytext,$safeeval,
  162: 					   $$parstack[-1]);
  163: 	
  164: 	push(@{ $answer{$cur_name}{'answers'} },[$bodytext]);
  165: 	
  166:     } elsif ($target eq 'edit') {
  167:   	$result.=&Apache::edit::tag_start($target,$token);
  168: 	my $bodytext = &Apache::lonxml::get_all_text("/value",$parser,$style);
  169: 	$result.=&Apache::edit::editline($token->[1],$bodytext,undef,40).
  170: 	    &Apache::edit::end_row();
  171:     } elsif ($target eq 'modified') {
  172: 	$result=$token->[4].&Apache::edit::modifiedfield('/value',$parser);
  173:     }
  174:     return $result;
  175: }
  176: 
  177: sub end_value {
  178:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  179:     my $result;
  180:     if ($target eq 'edit') {
  181: 	$result = &Apache::edit::end_table();
  182:     }
  183:     return $result;
  184: }
  185: 
  186: sub insert_value {
  187:     return '
  188:             <value></value>';
  189: }
  190: 
  191: sub start_vector {
  192:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  193:     my $result;
  194:     if ( $target eq 'web' || $target eq 'tex' ||
  195: 	 $target eq 'grade' || $target eq 'webgrade' ||
  196: 	 $target eq 'answer' || $target eq 'analyze' ) {
  197: 	my $bodytext = &Apache::lonxml::get_all_text("/vector",$parser,$style);
  198: 	my @values = &Apache::run::run($bodytext,$safeeval,$$parstack[-1]);
  199: 	if (@values == 1) {
  200: 	    @values = split(',',$values[0]);
  201: 	}
  202: 	push(@{ $answer{$cur_name}{'answers'} },\@values);
  203:     } elsif ($target eq 'edit') {
  204:   	$result.=&Apache::edit::tag_start($target,$token);
  205: 	my $bodytext = &Apache::lonxml::get_all_text("/vector",$parser,$style);
  206: 	$result.=&Apache::edit::editline($token->[1],$bodytext,undef,40).
  207: 	    &Apache::edit::end_row();
  208:     } elsif ($target eq 'modified') {
  209: 	$result=$token->[4].&Apache::edit::modifiedfield('/vector',$parser);
  210:     }
  211:     return $result;
  212: }
  213: 
  214: sub end_vector {
  215:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  216:     my $result;
  217:     if ($target eq 'edit') {
  218: 	$result = &Apache::edit::end_table();
  219:     }
  220:     return $result;
  221: }
  222: 
  223: sub insert_vector {
  224:     return '
  225:             <value></value>';
  226: }
  227: 
  228: sub start_array {
  229:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  230:     my $result;
  231:     if ( $target eq 'web' || $target eq 'tex' ||
  232: 	 $target eq 'grade' || $target eq 'webgrade' ||
  233: 	 $target eq 'answer' || $target eq 'analyze' ) {
  234: 	my $bodytext = &Apache::lonxml::get_all_text("/array",$parser,$style);
  235: 	my @values = &Apache::run::evaluate($bodytext,$safeeval,
  236: 					    $$parstack[-1]);
  237: 	push(@{ $answer{$cur_name}{'answers'} },@values);
  238:     }
  239:     return $result;
  240: }
  241: 
  242: sub end_array {
  243:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  244:     my $result;
  245:     return $result;
  246: }
  247: 
  248: sub start_unit {
  249:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  250:     my $result;
  251:     return $result;
  252: }
  253: 
  254: sub end_unit {
  255:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  256:     my $result;
  257:     return $result;
  258: }
  259: 
  260: sub start_numericalresponse {
  261:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  262:     &Apache::lonxml::register('Apache::caparesponse',
  263: 			      ('answer','answergroup','value','array','unit',
  264: 			       'vector'));
  265:     push(@Apache::lonxml::namespace,'caparesponse');
  266:     my $id = &Apache::response::start_response($parstack,$safeeval);
  267:     my $result;
  268:     undef(%answer);
  269:     undef(%{$safeeval->varglob('LONCAPA::CAPAresponse_args')});
  270:     if ($target eq 'edit') {
  271: 	$result.=&Apache::edit::tag_start($target,$token);
  272: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
  273: 	if ($token->[1] eq 'numericalresponse') {
  274: 	    $result.=&Apache::edit::text_arg('Incorrect Answers:','incorrect',
  275: 					     $token).
  276: 		&Apache::loncommon::help_open_topic('numerical_wrong_answers');
  277: 	    $result.=&Apache::edit::text_arg('Unit:','unit',$token,5).
  278: 		&Apache::loncommon::help_open_topic('Physical_Units');
  279: 	    $result.=&Apache::edit::text_arg('Format:','format',$token,4).
  280: 		&Apache::loncommon::help_open_topic('Numerical_Response_Format');
  281: 	} elsif ($token->[1] eq 'formularesponse') {
  282: 	    $result.=&Apache::edit::text_arg('Sample Points:','samples',
  283: 					     $token,40).
  284: 	      &Apache::loncommon::help_open_topic('Formula_Response_Sampling');
  285: 	}
  286:         $result.=&Apache::edit::text_arg('Pre-Processor Subroutine:','preprocess',
  287:                                              $token,10);
  288: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  289:     } elsif ($target eq 'modified') {
  290: 	my $constructtag;
  291: 	if ($token->[1] eq 'numericalresponse') {
  292: 	    $constructtag=&Apache::edit::get_new_args($token,$parstack,
  293: 						      $safeeval,'answer',
  294:  						      'incorrect','unit',
  295: 						      'format','preprocess');
  296: 	} elsif ($token->[1] eq 'formularesponse') {
  297: 	    $constructtag=&Apache::edit::get_new_args($token,$parstack,
  298: 						      $safeeval,'answer',
  299: 						      'samples','preprocess');
  300: 	}
  301: 	if ($constructtag) {
  302: 	    $result = &Apache::edit::rebuild_tag($token);
  303: 	    $result.=&Apache::edit::handle_insert();
  304: 	}
  305:     } elsif ($target eq 'meta') {
  306: 	$result=&Apache::response::meta_package_write('numericalresponse');
  307:     } elsif ($target eq 'answer' || $target eq 'grade') {
  308: 	&Apache::response::reset_params();
  309:     } elsif ($target eq 'web') {
  310: 	my $partid = $Apache::inputtags::part;
  311: 	my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
  312: 	&Apache::lonxml::debug("Got unit $hideunit for $partid $id");
  313: 	#no way to enter units, with radio buttons
  314: 	if ((lc($hideunit) eq "yes") && ($Apache::lonhomework::type ne 'exam')) {
  315: 	    my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
  316: 						    $safeeval);
  317: 	    if ($unit =~ /\S/) { $result.=" (in $unit) "; }
  318: 	}
  319: 	if (  &Apache::response::show_answer() ) {
  320: 	    &set_answertext($tag_internal_answer_name,$target,$token,$tagstack,
  321: 			    $parstack,$parser,$safeeval,-1);
  322: 	}
  323:     }
  324:     return $result;
  325: }
  326: 
  327: sub set_answertext {
  328:     my ($name,$target,$token,$tagstack,$parstack,$parser,$safeeval,
  329: 	$response_level) = @_;
  330:     &add_in_tag_answer($parstack,$safeeval,$response_level);
  331: 
  332:     if ($name eq '' || !ref($answer{$name})) {
  333: 	if (ref($answer{$tag_internal_answer_name})) {
  334: 	    $name = $tag_internal_answer_name;
  335: 	} else {
  336: 	    $name = (sort(keys(%answer)))[0];
  337: 	}
  338:     }
  339:     return if ($name eq '' || !ref($answer{$name}));
  340: 
  341:     my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,
  342: 						 $safeeval,$response_level);
  343:     my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval,
  344: 					    $response_level);
  345: 
  346:     &Apache::lonxml::debug("answer looks to be $name");
  347:     my @answertxt;
  348:     for (my $i=0; $i < scalar(@{$answer{$name}{'answers'}}); $i++) {
  349: 	my $answertxt;
  350: 	my $answer=$answer{$name}{'answers'}[$i];
  351: 	foreach my $element (@$answer) {
  352: 	    if ( scalar(@$tagstack)
  353: 		 && $tagstack->[$response_level] ne 'numericalresponse') {
  354: 		$answertxt.=$element.',';
  355: 	    } else {
  356: 		my $format;
  357: 		if ($#formats > 0) {
  358: 		    $format=$formats[$i];
  359: 		} else {
  360: 		    $format=$formats[0];
  361: 		}
  362: 		if ($unit=~/\$/) { $format="\$".$format; $unit=~s/\$//g; }
  363: 		if ($unit=~/\,/) { $format="\,".$format; $unit=~s/\,//g; }
  364: 		my $formatted=&format_number($element,$format,$target,
  365: 					     $safeeval);
  366: 		$answertxt.=' '.$formatted.',';
  367: 	    }
  368: 	    
  369: 	}
  370: 	chop($answertxt);
  371: 	if ($target eq 'web') {
  372: 	    $answertxt.=" $unit ";
  373: 	}
  374: 
  375: 	push(@answertxt,$answertxt)
  376:     }
  377: 	
  378:     my $id = $Apache::inputtags::response[-1];
  379:     $Apache::inputtags::answertxt{$id}=\@answertxt;
  380: }
  381: 
  382: sub setup_capa_args {
  383:     my ($safeeval,$parstack,$args,$response) = @_;
  384:     my $args_ref= \%{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
  385:     undef(%{ $args_ref });
  386:  
  387:     foreach my $arg (@{$args}) {
  388: 	$$args_ref{$arg}=
  389: 	    &Apache::lonxml::get_param($arg,$parstack,$safeeval);
  390:     }
  391:     foreach my $key (keys(%Apache::inputtags::params)) {
  392: 	$$args_ref{$key}=$Apache::inputtags::params{$key};
  393:     }
  394:     &setup_capa_response($args_ref,$response);
  395:     return $args_ref;
  396: }
  397: 
  398: sub setup_capa_response {
  399:     my ($args_ref,$response) = @_;   
  400: 
  401:     if (ref($response)) {
  402: 	$$args_ref{'response'}=dclone($response);
  403:     } else {
  404: 	$$args_ref{'response'}=dclone(["$response"]);
  405:     }
  406: }
  407: 
  408: sub check_submission {
  409:     my ($response,$partid,$id,$tag,$parstack,$safeeval,$ignore_sig)=@_;
  410:     my @args = ('type','tol','sig','format','unit','calc','samples','preprocess');
  411:     my $args_ref = &setup_capa_args($safeeval,$parstack,\@args,$response);
  412: 
  413:     my $hideunit=
  414: 	&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
  415:         #no way to enter units, with radio buttons
  416:     if ($Apache::lonhomework::type eq 'exam' ||
  417: 	lc($hideunit) eq "yes") {
  418: 	delete($$args_ref{'unit'});
  419:     }
  420:     #sig fig don't make much sense either
  421:     if (($Apache::lonhomework::type eq 'exam' ||
  422: 	 &Apache::response::submitted('scantron') ||
  423: 	 $ignore_sig) &&
  424: 	$tag eq 'numericalresponse') {
  425: 	delete($$args_ref{'sig'});
  426:     }
  427:     
  428:     if ($tag eq 'formularesponse') {
  429: 	if ($$args_ref{'samples'}) {
  430: 	    $$args_ref{'type'}='fml';
  431: 	} else {
  432: 	    $$args_ref{'type'}='math';
  433: 	}
  434:     } elsif ($tag eq 'numericalresponse') {
  435: 	$$args_ref{'type'}='float';
  436:     } elsif ($tag eq 'stringresponse') {
  437:         if ($$args_ref{'type'} eq '') {
  438:             $$args_ref{'type'} = 'ci';
  439:         }
  440:     }
  441: 
  442:     &add_in_tag_answer($parstack,$safeeval);
  443: 
  444:     if (!%answer) {
  445: 	&Apache::lonxml::error("No answers are defined");
  446:     }
  447: 
  448:     my (@final_awards,@final_msgs,@names);
  449:     foreach my $name (keys(%answer)) {
  450: 	&Apache::lonxml::debug(" doing $name with ".join(':',@{ $answer{$name}{'answers'} }));
  451: 	
  452: 	${$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=dclone($answer{$name});
  453: 	&setup_capa_response($args_ref,$response);
  454: 	use Time::HiRes;
  455: 	my $t0 = [Time::HiRes::gettimeofday()];
  456: 	my ($result,@msgs) = 
  457: 	    &Apache::run::run("&caparesponse_check_list()",$safeeval);
  458: 	&Apache::lonxml::debug("checking $name $result with $response took ".&Time::HiRes::tv_interval($t0));
  459: 	&Apache::lonxml::debug('msgs are '.join(':',@msgs));
  460: 	my ($awards)=split(/:/,$result);
  461: 	my @awards= split(/,/,$awards);
  462: 	my ($ad, $msg) = &Apache::inputtags::finalizeawards(\@awards,\@msgs);
  463: 	push(@final_awards,$ad);
  464: 	push(@final_msgs,$msg);
  465: 	push(@names,$name);
  466:     }
  467:     my ($ad, $msg, $name) = &Apache::inputtags::finalizeawards(\@final_awards,
  468: 							       \@final_msgs,
  469: 							       \@names,1);
  470:     &Apache::lonxml::debug(" name of picked award is $name from ".join(', ',@names));
  471:     return($ad,$msg, $name);
  472: }
  473: 
  474: sub stringresponse_gradechange {
  475:     my ($part,$id,$previous,$caller,$response,$ad,$type) = @_;
  476:     return unless (ref($previous) eq 'HASH');
  477:     my ($prevarray,$prevaward);
  478:     my %typenames = (
  479:                      cs => 'Case sensitive',
  480:                      ci => 'Case insensitive',
  481:                     );
  482:     if ($caller eq 'cs') {
  483:         return unless (ref($previous->{'version'}) eq 'ARRAY');
  484:         $prevarray = $previous->{'version'};
  485:         $prevaward = $previous->{'award'};
  486:     } elsif ($caller eq 'ci') {
  487:         return unless (ref($previous->{'versionci'}) eq 'ARRAY');
  488:         $prevarray = $previous->{'versionci'};
  489:         $prevaward = $previous->{'awardci'};
  490:     } else {
  491:         return;
  492:     }
  493:     my $count=0;
  494:     my %count_lookup;
  495:     foreach my $i (1..$Apache::lonhomework::history{'version'}) {
  496:         my $prefix = $i.":resource.$part";
  497:         next if (!exists($Apache::lonhomework::history{"$prefix.award"}));
  498:         $count++;
  499:         $count_lookup{$i} = $count;
  500:     }
  501:     my ($symb,$courseid,$domain,$name) = &Apache::lonnet::whichuser();
  502:     my %coursedesc = &Apache::lonnet::coursedescription($courseid);
  503:     my $cdom = $coursedesc{'domain'};
  504:     my $versions = ' (submissions: '.join(', ',map {$count_lookup{$_} } @{$prevarray}).')';
  505:     my $warning = "String Response ($typenames{$type}) grading discrepancy: award for response of $response changed from $prevaward".$versions." to $ad; user: $name:$domain in course: $courseid for part: $part response: $id for symb: $symb";
  506:     &Apache::lonnet::logthis($warning);
  507:     my $origmail = $Apache::lonnet::perlvar{'lonAdmEMail'};
  508:     my $recipients = &Apache::loncommon::build_recipient_list(undef,'errormail',
  509:                                                               $cdom,$origmail);
  510:     if ($recipients ne '') {
  511:         &Apache::lonmsg::sendemail($recipients,'Stringresponse Grading Discrepancy',$warning);
  512:     }
  513:     return;
  514: }
  515: 
  516: sub add_in_tag_answer {
  517:     my ($parstack,$safeeval,$response_level) = @_;
  518:     my @answer=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval,
  519: 					      $response_level);
  520:     &Apache::lonxml::debug('answer is'.join(':',@answer));
  521:     if (@answer && $answer[0] =~ /\S/) {
  522: 	$answer{$tag_internal_answer_name}= {'type' => 'ordered',
  523: 					     'answers' => [\@answer] };
  524:     }
  525: }
  526: 
  527: sub capa_formula_fix {
  528:    my ($expression)=@_;
  529:    return &Apache::response::implicit_multiplication($expression);
  530: }
  531: 
  532: sub end_numericalresponse {
  533:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  534: 
  535:     &Apache::lonxml::deregister('Apache::caparesponse',
  536: 			      ('answer','answergroup','value','array','unit',
  537: 			       'vector'));
  538:     pop(@Apache::lonxml::namespace);
  539: 
  540:     my $increment=1;
  541:     my $result = '';
  542:     if (!$Apache::lonxml::default_homework_loaded) {
  543: 	&Apache::lonxml::default_homework_load($safeeval);
  544:     }
  545:     my $partid = $Apache::inputtags::part;
  546:     my $id = $Apache::inputtags::response[-1];
  547:     my $tag;
  548:     my $safehole = new Safe::Hole;
  549:     $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
  550: 
  551:     if (scalar(@$tagstack)) { $tag=$$tagstack[-1]; }
  552:     if ( $target eq 'grade' && &Apache::response::submitted() ) {
  553: 	&Apache::response::setup_params($tag,$safeeval);
  554: 	if ($Apache::lonhomework::type eq 'exam' && 
  555: 	    (($tag eq 'formularesponse') || ($tag eq 'mathresponse'))) {
  556: 	    $increment=&Apache::response::scored_response($partid,$id);
  557: 	} else {
  558: 	    my $response = &Apache::response::getresponse();
  559: 	    if ( $response =~ /[^\s]/) {
  560: 		my %previous = &Apache::response::check_for_previous($response,$partid,$id);
  561: 		&Apache::lonxml::debug("submitted a $response<br>\n");
  562: 		&Apache::lonxml::debug($$parstack[-1] . "\n<br>");
  563: 		
  564: 		if ( &Apache::response::submitted('scantron')) {
  565: 	    &add_in_tag_answer($parstack,$safeeval);
  566: 	    my ($values,$display)=&make_numerical_bubbles($partid,$id,
  567: 					  $target,$parstack,$safeeval);
  568: 	    $response=$values->[$response];
  569: 	}
  570: 	$Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
  571: 		my ($ad,$msg,$name)=&check_submission($response,$partid,$id,
  572: 						      $tag,$parstack,
  573: 						      $safeeval);
  574: 
  575: 		&Apache::lonxml::debug('ad is'.$ad);
  576: 		if ($ad eq 'SIG_FAIL') {
  577: 		    my ($sig_u,$sig_l)=
  578: 			&get_sigrange($Apache::inputtags::params{'sig'});
  579: 		    $msg=join(':',$msg,$sig_l,$sig_u);
  580: 		    &Apache::lonxml::debug("sigs bad $sig_u $sig_l ".
  581: 					   $Apache::inputtags::params{'sig'});
  582: 		}
  583: 		&Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
  584:                 if (($ad eq 'INCORRECT' || $ad eq 'APPROX_ANS' ||
  585:                      $ad eq 'EXACT_ANS')) {
  586: 		    if ($Apache::lonhomework::type eq 'survey') {
  587: 		        $ad='SUBMITTED';
  588: 		    } elsif ($Apache::lonhomework::type eq 'surveycred') {
  589:                         $ad='SUBMITTED_CREDIT';
  590:                     } elsif ($Apache::lonhomework::type eq 'anonsurvey') {
  591:                         $ad='ANONYMOUS';
  592:                     } elsif ($Apache::lonhomework::type eq 'anonsurveycred') {
  593:                         $ad='ANONYMOUS_CREDIT';                     
  594:                     }
  595:                 }
  596: 		&Apache::response::handle_previous(\%previous,$ad);
  597: 		$Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
  598: 		$Apache::lonhomework::results{"resource.$partid.$id.awardmsg"}=$msg;
  599: 		$Apache::lonhomework::results{"resource.$partid.$id.answername"}=$name;
  600: 		$result='';
  601: 	    }
  602: 	}
  603:     } elsif ($target eq 'web' || $target eq 'tex') {
  604: 	&check_for_answer_errors($parstack,$safeeval);
  605: 	my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
  606: 	my $status = $Apache::inputtags::status['-1'];
  607: 	if ($Apache::lonhomework::type eq 'exam') {
  608: 	    # FIXME support multi dimensional numerical problems
  609:             #       in exam bubbles
  610: 	    my ($bubble_values,$bubble_display)=
  611: 		&make_numerical_bubbles($partid,$id,$target,$parstack,
  612: 					$safeeval);
  613: 	    my $number_of_bubbles = scalar(@{ $bubble_values });
  614: 	    my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
  615: 						    $safeeval);
  616: 	    if ($target eq 'web') {
  617: 		if ($tag eq 'numericalresponse') {
  618: 		    if ($unit=~/\S/) {$result.=' (in '.$unit.')<br /><br />';}
  619: 		    $result.= '<table border="1"><tr>';
  620: 		    my $previous=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.$id.submission"};
  621: 		    for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
  622: 			my $checked='';
  623: 			if ($previous eq $bubble_values->[$ind]) {
  624: 			    $checked=" checked='on' ";
  625: 			}
  626: 			$result.='<td><input type="radio" name="HWVAL_'.$id.
  627: 			    '" value="'.$bubble_values->[$ind].'" '.$checked
  628: 			    .' /><b>'.$alphabet[$ind].'</b>: '.
  629: 			    $bubble_display->[$ind].'</td>';
  630: 		    }
  631: 		    $result.='</tr></table>';
  632: 		}
  633: 	    } elsif ($target eq 'tex') {
  634: 		if ((defined $unit) and ($unit=~/\S/) and ($Apache::lonhomework::type eq 'exam')) {
  635: 		    $result.=' \textit{(in} \verb|'.$unit.'|\textit{)} ';
  636: 		}
  637: 		if ($tag eq 'numericalresponse') {
  638: 		    $result .= &make_horizontal_latex_bubbles($bubble_values, $bubble_display,
  639: 			'$\bigcirc$');
  640: 		} else {
  641: 		    $increment = &Apache::response::repetition();
  642: 		}
  643: 	    }
  644: 	}
  645: 
  646: 	&Apache::response::setup_prior_tries_hash(\&format_prior_response_numerical);
  647:     } elsif ($target eq 'edit') {
  648: 	$result.='</td></tr>'.&Apache::edit::end_table;
  649:     } elsif ($target eq 'answer' || $target eq 'analyze') {
  650: 	my $part_id="$partid.$id";
  651: 	if ($target eq 'analyze') {
  652: 	    push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
  653: 	    $Apache::lonhomework::analyze{"$part_id.type"} = $tag;
  654: 	    my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
  655: 	    if ($#incorrect eq 0) { @incorrect=(split(/,/,$incorrect[0])); }
  656: 	    push (@{ $Apache::lonhomework::analyze{"$part_id.incorrect"} }, @incorrect);
  657: 	    &Apache::response::check_if_computed($token,$parstack,
  658: 						 $safeeval,'answer');
  659: 	}
  660: 	if (scalar(@$tagstack)) {
  661: 	    &Apache::response::setup_params($tag,$safeeval);
  662: 	}
  663: 	&add_in_tag_answer($parstack,$safeeval);
  664: 	my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
  665: 
  666: 	my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval);
  667: 	my ($needsdollar,$needscomma);
  668: 	if ($unit =~ /\$/) {
  669: 	    $needsdollar = 1;
  670: 	}
  671: 	if ($unit =~ /\,/) {
  672: 	    $needscomma = 1;
  673: 	}
  674: 
  675: 	if ($target eq 'answer') {
  676: 	    $result.=&Apache::response::answer_header($tag,undef,
  677: 						      scalar(keys(%answer)));
  678: 	    if ($tag eq 'numericalresponse'
  679: 		&& $Apache::lonhomework::type eq 'exam') {
  680: 		my ($bubble_values,undef,$correct) = &make_numerical_bubbles($partid,
  681: 					     $id,$target,$parstack,$safeeval);
  682: 		$result.=&Apache::response::answer_part($tag,$correct);
  683: 	    }
  684: 	}
  685: 	foreach my $name (sort(keys(%answer))) {
  686: 	    my @answers = @{ $answer{$name}{'answers'} };
  687: 	    if ($target eq 'analyze') {
  688: 		foreach my $info ('answer','ans_high','ans_low','format') {
  689: 		    $Apache::lonhomework::analyze{"$part_id.$info"}{$name}=[];
  690: 		}
  691: 	    }
  692: 	    my ($sigline,$tolline);
  693: 	    if ($name ne $tag_internal_answer_name 
  694: 		|| scalar(keys(%answer)) > 1) {
  695: 		$result.=&Apache::response::answer_part($tag,$name);
  696: 	    }
  697: 	    for(my $i=0;$i<=$#answers;$i++) {
  698: 		my $ans=$answers[$i];
  699: 		my $fmt=$formats[0];
  700: 		if (@formats && $#formats) {$fmt=$formats[$i];}
  701: 		my ($sighigh,$siglow);
  702: 		if ($Apache::inputtags::params{'sig'}) {
  703: 		    ($sighigh,$siglow)=&get_sigrange($Apache::inputtags::params{'sig'});
  704: 		}
  705: 		my @vector;
  706: 		if (ref($ans)) {
  707: 		    @vector = @{ $ans };
  708: 		} else {
  709: 		    @vector = ($ans);
  710: 		}
  711: 		my @all_answer_info;
  712: 		foreach my $element (@vector) {
  713: 		    my ($high,$low);
  714: 		    if ($Apache::inputtags::params{'tol'}) {
  715: 			($high,$low)=&get_tolrange($element,$Apache::inputtags::params{'tol'});
  716: 		    }
  717: 		    if ($target eq 'answer') {
  718: 			if ($fmt && $tag eq 'numericalresponse') {
  719: 			    $fmt=~s/e/E/g;
  720: 			    if ($unit=~/\$/) { $fmt="\$".$fmt; $unit=~s/\$//g; }
  721: 			    if ($unit=~/\,/) { $fmt="\,".$fmt; $unit=~s/\,//g; }
  722: 			    $element = &format_number($element,$fmt,$target,$safeeval);
  723: 			    #if ($high) {
  724: 			    #    $high=&format_number($high,$fmt,$target,$safeeval);
  725: 			    #    $low =&format_number($low,$fmt,$target,$safeeval);
  726: 			    #}
  727: 			}
  728: 			if ($high && $tag eq 'numericalresponse') {
  729: 			    $element.='; ['.$low.'; '.$high.']';
  730: 			    $tolline .= "[$low, $high]";
  731: 			}
  732: 			if (defined($sighigh) && $tag eq 'numericalresponse') {
  733: 			    if ($env{'form.answer_output_mode'} eq 'tex') {
  734: 				$element.= "; Sig $siglow - $sighigh";
  735: 			    } else {
  736: 				$element.= " Sig <i>$siglow - $sighigh</i>";
  737: 				$sigline .= "[$siglow, $sighigh]";
  738: 			    }
  739: 			}
  740: 			push(@all_answer_info,$element);
  741: 			
  742: 		    } elsif ($target eq 'analyze') {
  743: 			push (@{ $Apache::lonhomework::analyze{"$part_id.answer"}{$name}[$i] }, $element);
  744: 			if ($high) {
  745: 			    push (@{ $Apache::lonhomework::analyze{"$part_id.ans_high"}{$name}[$i] }, $high);
  746: 			    push (@{ $Apache::lonhomework::analyze{"$part_id.ans_low"}{$name}[$i] }, $low);
  747: 			}
  748: 			if ($fmt) {
  749: 			    push (@{ $Apache::lonhomework::analyze{"$part_id.format"}{$name}[$i] }, $fmt);
  750: 			}
  751: 		    }
  752: 		}
  753: 		if ($target eq 'answer') {
  754: 		    $result.= &Apache::response::answer_part($tag,join('; ',@all_answer_info));
  755: 		}
  756: 	    }
  757: 
  758: 	    my @fmt_ans;
  759: 	    for(my $i=0;$i<=$#answers;$i++) {
  760: 		my $ans=$answers[$i];
  761: 		my $fmt=$formats[0];
  762: 		if (@formats && $#formats) {$fmt=$formats[$i];}
  763: 		my @answers;
  764: 		if (ref($ans) eq 'ARRAY') {
  765: 		    @answers = (@{$ans});
  766: 		}
  767: 		foreach my $element (@answers) {		    
  768: 		    if ($fmt && $tag eq 'numericalresponse') {
  769: 			$fmt=~s/e/E/g;
  770: 			if ($unit=~/\$/) { $fmt="\$".$fmt; $unit=~s/\$//g; }
  771: 			if ($unit=~/\,/) { $fmt="\,".$fmt; $unit=~s/\,//g; }
  772: 			$element = &format_number($element,$fmt,$target,
  773: 						  $safeeval);
  774: 			if ($fmt=~/\$/ && !$needsdollar) { $element=~s/\$//; }
  775: 		    }
  776: 		}
  777: 		push(@fmt_ans,join(',',@answers));
  778: 	    }
  779: 	    my $response=\@fmt_ans;
  780: 
  781: 	    my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.
  782: 					      $id.'.turnoffunit');
  783: 	    if ($unit ne ''  && 
  784: 		! ($Apache::lonhomework::type eq 'exam' ||
  785: 		   lc($hideunit) eq "yes") )  {
  786: 		my $cleanunit=$unit;
  787: 		$cleanunit=~s/[\$,]//g;
  788: 		foreach my $ans (@fmt_ans) {
  789: 		    $ans.=" $cleanunit";
  790: 		}
  791: 	    }
  792: 	    my ($ad,$msg)=&check_submission($response,$partid,$id,$tag,
  793: 					    $parstack,$safeeval);
  794: 	    if ($ad ne 'EXACT_ANS' && $ad ne 'APPROX_ANS') {
  795: 		my $error;
  796: 		if ($tag eq 'formularesponse') {
  797: 		    $error=&mt("Computer's answer is incorrect ([_1]).",'"'.join(', ',@$response).'"');
  798: 		} else {
  799: 		    # answer failed check if it is sig figs that is failing
  800: 		    my ($ad,$msg)=&check_submission($response,$partid,$id,
  801: 						    $tag,$parstack,
  802: 						    $safeeval,1);
  803: 		    $error=&mt("Computer's answer is incorrect ([_1]).",'"'.join(', ',@$response).'"').' ';
  804:                     if (($ad eq 'NO_UNIT') && $needsdollar) {
  805:                         $error.=&mt('The unit attribute includes [_1] but the answer format does not.','$').' '.
  806:                                 &mt('Either remove the [_1] from the unit or prepend [_1] to the answer format.','$');    
  807:                     } elsif (($ad eq 'COMMA_FAIL') && $needscomma) {
  808:                         $error.=&mt('The unit attribute includes [_1] but the answer format does not.',',').' '.
  809:                                 &mt('Either remove the [_1] from the unit or prepend [_1] to the answer format.',',');
  810:                     } elsif ($ad eq 'UNIT_INVALID_STUDENT') {
  811:                         $error.=&mt('Unable to interpret units. Computer reads units as "[_1]".',$msg).' '.
  812:                                 &mt('The unit attribute in the numericalresponse item needs to be a supported physical unit.');
  813: 		    } elsif ($sigline ne '') {
  814: 			$error.=&mt('It is likely that the tolerance range [_1] or significant figures [_2] need to be adjusted.',$tolline,$sigline);
  815: 		    } else {
  816: 			$error.=&mt('It is likely that the tolerance range [_1] needs to be adjusted.',$tolline);
  817: 		    }
  818: 		}
  819: 		if ($ad ne 'EXACT_ANS' && $ad ne 'APPROX_ANS') {
  820: 		    &Apache::lonxml::error($error);
  821: 		} else {
  822: 		    &Apache::lonxml::warning($error);
  823: 		}
  824: 	    }
  825: 
  826: 	    if (defined($unit) and ($unit ne '') and
  827: 		$tag eq 'numericalresponse') {
  828: 		if ($target eq 'answer') {
  829: 		    if ($env{'form.answer_output_mode'} eq 'tex') {
  830: 			$result.=&Apache::response::answer_part($tag,
  831: 								" Unit: $unit ");
  832: 		    } else {
  833: 			$result.=&Apache::response::answer_part($tag,
  834: 								"Unit: <b>$unit</b>");
  835: 		    }
  836: 		} elsif ($target eq 'analyze') {
  837: 		    push (@{ $Apache::lonhomework::analyze{"$part_id.unit"} }, $unit);
  838: 		}
  839: 	    }
  840: 	    if ($tag eq 'formularesponse' && $target eq 'answer') {
  841: 		my $samples=&Apache::lonxml::get_param('samples',$parstack,$safeeval);
  842: 		$result.=&Apache::response::answer_part($tag,$samples);
  843: 	    }
  844: 	    $result.=&Apache::response::next_answer($tag,$name);
  845: 	}
  846: 	if ($target eq 'answer') {
  847: 	    $result.=&Apache::response::answer_footer($tag);
  848: 	}
  849:     }
  850:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
  851: 	$target eq 'tex' || $target eq 'analyze') {
  852:         if (($tag eq 'formularesponse') && ($target eq 'analyze')) {
  853:             my $type = &Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.type');
  854:             if ($type eq 'exam') {
  855:                 $increment = &Apache::response::repetition();
  856:             }
  857:         }
  858: 	&Apache::lonxml::increment_counter($increment,"$partid.$id");
  859: 	if ($target eq 'analyze') {
  860: 	    &Apache::lonhomework::set_bubble_lines();
  861: 	}
  862:     }
  863:     &Apache::response::end_response();
  864:     return $result;
  865: }
  866: 
  867: sub format_prior_response_numerical {
  868:     my ($mode,$answer) = @_;
  869:     if (ref($answer)) {
  870: 	my $result = '<table class="LC_prior_numerical"><tr>';
  871: 	foreach my $element (@{ $answer }) {
  872: 	    $result.= '<td><span class="LC_prior_numerical">'.
  873: 		&HTML::Entities::encode($element,'"<>&').'</span></td>';
  874: 	}
  875: 	$result.='</tr></table>';
  876: 	return $result;
  877:     }
  878:     return '<span class="LC_prior_numerical">'.
  879: 	    &HTML::Entities::encode($answer,'"<>&').'</span>';
  880: 
  881: }
  882: 
  883: sub check_for_answer_errors {
  884:     my ($parstack,$safeeval) = @_;
  885:     &add_in_tag_answer($parstack,$safeeval);
  886:     my %counts;
  887:     foreach my $name (keys(%answer)) {
  888: 	push(@{$counts{scalar(@{$answer{$name}{'answers'}})}},$name);
  889:     }
  890:     if (scalar(keys(%counts)) > 1) {
  891: 	my $counts = join(' ',map {
  892: 	    my $count = $_;
  893: 	    &mt("Answers [_1] had [_2] components.",
  894: 		'<tt>'.join(', ',@{$counts{$count}}).'</tt>',
  895: 		$count);
  896: 	} (sort(keys(%counts))));
  897: 	&Apache::lonxml::error(&mt("All answers must have the same number of components. Varying numbers of answers were seen. ").$counts);
  898:     }
  899:     my $expected_number_of_inputs = (keys(%counts))[0];
  900:     if ( $expected_number_of_inputs > 0 
  901: 	 && $expected_number_of_inputs != scalar(@Apache::inputtags::inputlist)) {
  902: 	&Apache::lonxml::error(&mt("Expected [_1] input fields, but there were only [_2] seen.", 
  903: 				   $expected_number_of_inputs,
  904: 				   scalar(@Apache::inputtags::inputlist)));
  905:     }
  906: }
  907: 
  908: sub get_table_sizes {
  909:     my ($number_of_bubbles,$rbubble_values)=@_;
  910:     my $scale=2; #mm for one digit
  911:     my $cell_width=0;
  912:     foreach my $member (@$rbubble_values) {
  913: 	my $cell_width_real=0;
  914: 	if ($member=~/(\+|-)?(\d*)\.?(\d*)\s*\$?\\times\s*10\^\{(\+|-)?(\d+)}\$?/) {
  915: 	    $cell_width_real=(length($2)+length($3)+length($5)+7)*$scale;
  916: 	} elsif ($member=~/(\d*)\.?(\d*)(E|e)(\+|-)?(\d*)/) {
  917: 	    $cell_width_real=(length($1)+length($2)+length($5)+9)*$scale;
  918:         } elsif ($member=~/(\d*)\.(\d*)/) {
  919: 	    $cell_width_real=(length($1)+length($2)+3)*$scale;
  920: 	} else {
  921: 	    $cell_width_real=(length($member)+1)*$scale*0.9;
  922: 	}
  923: 	if ($cell_width_real>$cell_width) {$cell_width=$cell_width_real;}
  924:     }
  925:     $cell_width+=8; 
  926:     my $textwidth;
  927:     if ($env{'form.textwidth'} ne '') {
  928: 	$env{'form.textwidth'}=~/(\d*)\.?(\d*)/;
  929: 	$textwidth=$1.'.'.$2;
  930:     } else {
  931: 	$env{'form.textwidth'}=~/(\d+)\.?(\d*)/;
  932: 	$textwidth=$1.'.'.$2;
  933:     }
  934:     my $bubbles_per_line=int($textwidth/$cell_width);
  935:     if ($bubbles_per_line > $number_of_bubbles) {
  936: 	$bubbles_per_line=$number_of_bubbles;
  937:     } elsif (($bubbles_per_line > $number_of_bubbles/2) 
  938: 	     && ($number_of_bubbles % 2==0)) {
  939: 	$bubbles_per_line=$number_of_bubbles/2;
  940:     }
  941:     if ($bubbles_per_line < 1) {
  942: 	$bubbles_per_line=1;
  943:     }
  944:     my $number_of_tables = int($number_of_bubbles/$bubbles_per_line);
  945:     my @table_range = ();
  946:     for (my $i=0;$i<$number_of_tables;$i++) {push @table_range,$bubbles_per_line;}
  947:     if ($number_of_bubbles % $bubbles_per_line) {
  948: 	$number_of_tables++;
  949: 	push @table_range,($number_of_bubbles % $bubbles_per_line);
  950:     }
  951:     $cell_width-=8;
  952:     $cell_width=$cell_width*3/4;
  953:     return ($cell_width,$number_of_tables,@table_range);
  954: }
  955: 
  956: sub format_number {
  957:     my ($number,$format,$target,$safeeval)=@_;
  958:     my $ans;
  959:     if ($format eq '') {
  960: 	#What is the number? (integer,decimal,floating point)
  961: 	if ($number=~/^(\d*\.?\d*)(E|e)[+\-]?(\d*)$/) {
  962: 	    $format = '3e';
  963: 	} elsif ($number=~/^(\d*)\.(\d*)$/) {
  964: 	    $format = '4f';
  965: 	} elsif ($number=~/^(\d*)$/) {
  966: 	    $format = 'd';
  967: 	}
  968:     }
  969:     if (!$Apache::lonxml::default_homework_loaded) {
  970: 	&Apache::lonxml::default_homework_load($safeeval);
  971:     }
  972:     $ans=&Apache::run::run("&prettyprint(q\0$number\0,q\0$format\0,q\0$target\0)",$safeeval);
  973:     return $ans;
  974: }
  975: 
  976: sub make_numerical_bubbles {
  977:     my ($part,$id,$target,$parstack,$safeeval) =@_;
  978: 
  979:     if (!%answer) {
  980: 	&Apache::lonxml::error(&mt("No answers defined for response [_1] in part [_2] to make bubbles for.",$id,$part));
  981: 	return ([],[],undef);
  982:     }
  983:     
  984:     my $number_of_bubbles = 
  985: 	&Apache::response::get_response_param($part.'_'.$id,'numbubbles',8);
  986: 
  987:     #
  988:     # Fixes for BZ 6519 - number of bubbles <= 0 or non-integer.
  989:     # 
  990:     $number_of_bubbles = int($number_of_bubbles + 0.5);
  991:     if ($number_of_bubbles <= 0) {
  992: 	$number_of_bubbles = 8;
  993:     }
  994:     
  995: 
  996:     my ($format)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
  997:     my $name = (exists($answer{$tag_internal_answer_name}) 
  998: 		? $tag_internal_answer_name
  999: 		: (sort(keys(%answer)))[0]);
 1000: 
 1001:     if ( scalar(@{$answer{$name}{'answers'}}) > 1) {
 1002: 	&Apache::lonxml::error("Only answers with 1 component are supported in exam mode");
 1003:     }
 1004:     if (scalar(@{$answer{$name}{'answers'}[0]}) > 1) {
 1005: 	&Apache::lonxml::error("Vector answers are unsupported in exam mode.");
 1006:     }
 1007: 
 1008:     my $answer = $answer{$name}{'answers'}[0][0];
 1009:     my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,
 1010: 						   $safeeval);
 1011:     if ($#incorrect eq 0) { @incorrect=(split(/,/,$incorrect[0])); }
 1012:     
 1013:     my @bubble_values=();
 1014:     my @alphabet=('A'..'Z');
 1015: 
 1016:     &Apache::lonxml::debug("answer is $answer incorrect is @incorrect");
 1017:     my @oldseed=&Math::Random::random_get_seed();
 1018:     if (@incorrect) {
 1019: 	&Apache::lonxml::debug("inside ".(scalar(@incorrect)+1 gt $number_of_bubbles));
 1020: 	if (defined($incorrect[0]) &&
 1021: 	    scalar(@incorrect)+1 >= $number_of_bubbles) {
 1022: 	    &Apache::lonxml::debug("inside ".(scalar(@incorrect)+1).":$number_of_bubbles");
 1023: 	    &Apache::response::setrandomnumber();
 1024: 	    my @rand_inc=&Math::Random::random_permutation(@incorrect);
 1025: 	    @bubble_values=@rand_inc[0..($number_of_bubbles-2)];
 1026: 	    @bubble_values=sort {$a <=> $b} (@bubble_values,$answer);
 1027: 	    &Apache::lonxml::debug("Answer was :$answer: returning :".$#bubble_values.": which are :".join(':',@bubble_values));
 1028: 	    &Math::Random::random_set_seed(@oldseed);
 1029: 
 1030: 	    my $correct;
 1031: 	    for(my $i=0; $i<=$#bubble_values;$i++) {
 1032: 		if ($bubble_values[$i] eq $answer) {
 1033: 		    $correct = $alphabet[$i];
 1034: 		    last;
 1035: 		}
 1036: 	    }
 1037: 
 1038: 	    if (defined($format) && $format ne '') {
 1039: 		my @bubble_display;
 1040: 		foreach my $value (@bubble_values) {
 1041: 		    push(@bubble_display,
 1042: 			 &format_number($value,$format,$target,$safeeval));
 1043: 		}
 1044: 		return (\@bubble_values,\@bubble_display,$correct);
 1045: 	    } else {
 1046: 		return (\@bubble_values,\@bubble_values,$correct);
 1047: 	    }
 1048: 	}
 1049: 	if (defined($incorrect[0]) &&
 1050: 	    scalar(@incorrect)+1 < $number_of_bubbles) {
 1051: 	    &Apache::lonxml::warning("Not enough incorrect answers were specified in the incorrect array, ignoring the specified incorrect answers and instead generating them (".join(',',@incorrect).").");
 1052: 	}
 1053:     }
 1054:     my @factors = (1.13,1.17,1.25,1.33,1.45); #default values of factors
 1055:     my @powers = (1..$number_of_bubbles);
 1056:     &Apache::response::setrandomnumber();
 1057:     my $ind=&Math::Random::random_uniform_integer(1,0,$#powers);
 1058:     my $power = $powers[$ind];
 1059:     $ind=&Math::Random::random_uniform_integer(1,0,$#factors);
 1060:     my $factor = $factors[$ind];
 1061:     my @bubble_display;
 1062:     my $answerfactor=$answer;
 1063:     if ($answer==0) { 
 1064:        $answerfactor=&Math::Random::random_uniform_integer(1,1,100)/
 1065:                      &Math::Random::random_uniform_integer(1,1,10);
 1066:     }
 1067:     for ($ind=0;$ind<$number_of_bubbles;$ind++) {
 1068: 	my $exponent = $power-$powers[$#powers-$ind];
 1069: 	$bubble_values[$ind] = $answerfactor*($factor**$exponent);
 1070: 
 1071: 	# If bubble is for correct answer (i.e., exponent = 0), and value
 1072: 	# of $answerfactor * factor**$exponent is an integer with more than
 1073: 	# 15 digits, assign $answerfactor itself as bubble value.
 1074: 	# This prevents a "use fewer digits" issue on 64bit servers
 1075: 	# when correct answer is >= 1e+16, and when correct bubble is A.
 1076: 
 1077: 	if ($exponent == 0) {
 1078: 	    if ($bubble_values[$ind] =~ /^-?(\d+)$/) {
 1079: 		if (length($1) > 15) {
 1080: 		    $bubble_values[$ind] = $answerfactor;
 1081: 		}
 1082: 	    }
 1083: 	}
 1084: 
 1085: 	$bubble_display[$ind] = &format_number($bubble_values[$ind],
 1086: 					       $format,$target,$safeeval);
 1087:     }
 1088:     my $correct = $alphabet[$number_of_bubbles-$power];
 1089:     if ($answer==0) {
 1090:        $correct='A';
 1091:        $bubble_values[0]=0;
 1092:        $bubble_display[0] = &format_number($bubble_values[0],
 1093:                                            $format,$target,$safeeval);
 1094:     }
 1095:     &Math::Random::random_set_seed(@oldseed);
 1096:     return (\@bubble_values,\@bubble_display,$correct);
 1097: }
 1098: 
 1099: ##
 1100: # Produce LaTeX bubbles laid out horizontally given a set of bubble values:
 1101: #
 1102: # @param bubble_values  - reference to an array of bubble 'values'
 1103: # @param bubble_display - reference to the array of texts to display to the user
 1104: #                         for each bubble_value (this is mostly for numerical response
 1105: #                         when the displayed value may not be an exact
 1106: #                         representation of the bubble value. 
 1107: # @param bubble_fragment- The LaTeX fragment that will be plugged in to make
 1108: #                         the bubble itself. Note that the code will autonomously
 1109: #                         label each bubble with a lable...and that it's perfectly
 1110: #                         acceptable to use "" for the bubble_fragment.
 1111: # 
 1112: # @return string - the LaTeX fragment that produces the bubbles.
 1113: #
 1114: sub make_horizontal_latex_bubbles {
 1115:     my ($bubble_values, $bubble_display, $bubble_fragment)     = @_;
 1116:     my $result;
 1117: 
 1118:     my $number_of_bubbles = scalar(@{$bubble_values}); 
 1119: 
 1120:     # Get the number of rows and columns in each row of the bubble
 1121:     # table:
 1122: 
 1123:     my ($celllength, $number_of_tables, @table_range) =
 1124: 	&get_table_sizes($number_of_bubbles, $bubble_display);
 1125: 
 1126:     my $j=0;
 1127:     my $cou=0;
 1128:     $result.='\vskip 2mm \noindent ';
 1129:     $result .= '\textbf{'.$Apache::lonxml::counter.'.} \vskip -3mm ';
 1130: 
 1131:     for (my $i=0;$i<$number_of_tables;$i++) {
 1132: 	if ($i == 0) {
 1133: 	    $result .= '\vskip -1mm ';
 1134: 	} else {
 1135: 	    $result .= '\vskip 1mm ';
 1136: 	}
 1137: 	$result.='\noindent \setlength{\tabcolsep}{2 mm}\hskip 2pc\begin{tabular}{';
 1138: 	for (my $ind=0;$ind<$table_range[$j];$ind++) {
 1139: 	    $result.='p{4 mm}p{'.$celllength.' mm}';
 1140: 	}
 1141: 	$result.='}';
 1142: 	for (my $ind=$cou;$ind<$cou+$table_range[$j];$ind++) {
 1143: 	    $result.='\hskip -4 mm {\small \textbf{ '.$alphabet[$ind].'}}'
 1144: 		. $bubble_fragment 
 1145: 		. '& \hskip -3 mm {\small '.$bubble_display->[$ind].'} ';
 1146: 	    if ($ind != $cou+$table_range[$j]-1) {
 1147: 		$result.=' & ';
 1148: 	    }
 1149: 	}
 1150: 	$cou += $table_range[$j];
 1151: 	$j++;
 1152: 	$result.='\\\\\end{tabular}\vskip 0 mm ';
 1153:     }
 1154:     return $result;
 1155: }
 1156: 
 1157: sub get_tolrange {
 1158:     my ($ans,$tol)=@_;
 1159:     my ($high,$low);
 1160:     if ($tol =~ /%$/) {
 1161: 	chop($tol);
 1162: 	my $change=$ans*($tol/100.0);
 1163: 	$high=$ans+$change;
 1164: 	$low=$ans-$change;
 1165:     } else {
 1166: 	$high=$ans+$tol;
 1167: 	$low=$ans-$tol;
 1168:     }
 1169:     return ($high,$low);
 1170: }
 1171: 
 1172: sub get_sigrange {
 1173:     my ($sig)=@_;
 1174:     #&Apache::lonxml::debug("Got a sig of :$sig:");
 1175:     my $courseid=$env{'request.course.id'};
 1176:     if ($env{'request.state'} ne 'construct'
 1177: 	&& lc($env{"course.$courseid.disablesigfigs"}) eq 'yes') {
 1178: 	return (15,0);
 1179:     }
 1180:     my $sig_lbound;
 1181:     my $sig_ubound;
 1182:     if ($sig eq '') {
 1183: 	$sig_lbound = 0; #SIG_LB_DEFAULT
 1184: 	$sig_ubound =15; #SIG_UB_DEFAULT
 1185:     } else {
 1186: 	($sig_lbound,$sig_ubound) = split(/,/,$sig);
 1187: 	if (!defined($sig_lbound)) {
 1188: 	    $sig_lbound = 0; #SIG_LB_DEFAULT
 1189: 	    $sig_ubound =15; #SIG_UB_DEFAULT
 1190: 	}
 1191: 	if (!defined($sig_ubound)) { $sig_ubound=$sig_lbound; }
 1192:     }
 1193:     if (($sig_ubound<$sig_lbound) ||
 1194: 	($sig_lbound > 15) ||
 1195: 	($sig =~/(\+|-)/ ) ) {
 1196: 	my $errormsg=&mt("Invalid Significant figures detected")." ($sig)";
 1197: 	if ($env{'request.state'} eq 'construct') {
 1198: 	    $errormsg.=
 1199: 		&Apache::loncommon::help_open_topic('Significant_Figures');
 1200: 	}
 1201: 	&Apache::lonxml::error($errormsg);
 1202:     }
 1203:     return ($sig_ubound,$sig_lbound);
 1204: }
 1205: 
 1206: sub format_prior_response_string {
 1207:     my ($mode,$answer) =@_;
 1208:     return '<span class="LC_prior_string">'.
 1209: 	    &HTML::Entities::encode($answer,'"<>&').'</span>';
 1210: }
 1211: 
 1212: sub start_stringresponse {
 1213:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
 1214:     my $result;
 1215:     my $id = &Apache::response::start_response($parstack,$safeeval);
 1216:     undef(%answer);
 1217:     if ($target eq 'meta') {
 1218: 	$result=&Apache::response::meta_package_write('stringresponse');
 1219:     } elsif ($target eq 'edit') {
 1220: 	$result.=&Apache::edit::tag_start($target,$token);
 1221: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
 1222: 	$result.=&Apache::edit::select_arg('Type:','type',
 1223: 			 [['cs','Case Sensitive'],['ci','Case Insensitive'],
 1224: 			  ['mc','Case Insensitive, Any Order'],
 1225: 			  ['re','Regular Expression']],$token);
 1226: 	$result.=&Apache::edit::text_arg('String to display for answer:',
 1227: 					 'answerdisplay',$token);
 1228:         $result.=&Apache::edit::text_arg('Pre-Processor Subroutine:','preprocess',
 1229:                                              $token,10);
 1230: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
 1231:     } elsif ($target eq 'modified') {
 1232: 	my $constructtag;
 1233: 	$constructtag=&Apache::edit::get_new_args($token,$parstack,
 1234: 						  $safeeval,'answer',
 1235: 						  'type','answerdisplay','preprocess');
 1236: 	if ($constructtag) {
 1237: 	    $result = &Apache::edit::rebuild_tag($token);
 1238: 	    $result.=&Apache::edit::handle_insert();
 1239: 	}
 1240:     } elsif ($target eq 'web') {
 1241: 	if (  &Apache::response::show_answer() ) {
 1242: 	    my $answer=
 1243: 	       &Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval);
 1244: 	    if (!defined $answer || $answer eq '') {
 1245: 		$answer=
 1246: 		    &Apache::lonxml::get_param('answer',$parstack,$safeeval);
 1247: 	    }
 1248: 	    $Apache::inputtags::answertxt{$id}=[$answer];
 1249: 	} 
 1250:     } elsif ($target eq 'answer' || $target eq 'grade') {
 1251: 	&Apache::response::reset_params();
 1252:     }
 1253:     return $result;
 1254: }
 1255: 
 1256: sub end_stringresponse {
 1257:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
 1258: 
 1259:     my $result = '';
 1260:     my $part=$Apache::inputtags::part;
 1261:     my $id=$Apache::inputtags::response[-1];
 1262:     my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
 1263:     my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
 1264:     my $answerdisplay=&Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval);
 1265:     &Apache::lonxml::debug("current $answer ".$token->[2]);
 1266:     if (!$Apache::lonxml::default_homework_loaded) {
 1267: 	&Apache::lonxml::default_homework_load($safeeval);
 1268:     }
 1269:     if ( $target eq 'grade' && &Apache::response::submitted() ) {
 1270: 	&Apache::response::setup_params('stringresponse',$safeeval);
 1271: 	$safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
 1272: 	if ($Apache::lonhomework::type eq 'exam' ||
 1273: 	    &Apache::response::submitted('scantron')) {
 1274: 	    &Apache::response::scored_response($part,$id);
 1275: 
 1276: 	} else {
 1277: 	    my $response = &Apache::response::getresponse();
 1278: 	    if ( $response =~ /[^\s]/) {
 1279: 		my %previous = &Apache::response::check_for_previous($response,
 1280: 								    $part,$id,
 1281:                                                                     undef,$type);
 1282: 		&Apache::lonxml::debug("submitted a $response<br>\n");
 1283: 		&Apache::lonxml::debug($$parstack[-1] . "\n<br>");
 1284: 		$Apache::lonhomework::results{"resource.$part.$id.submission"}=
 1285: 		    $response;
 1286: 		my ($ad,$msg);
 1287: 		if ($type eq 're' ) { 
 1288: 		    # if the RE wasn't in a var it likely got munged,
 1289:                     # thus grab it from the var directly
 1290: #		    my $testans=$token->[2]->{'answer'};
 1291: #		    if ($testans !~ m/^\s*\$/) {
 1292: #			$answer=$token->[2]->{'answer'};
 1293: #		    }
 1294: 		    ${$safeeval->varglob('LONCAPA::response')}=$response;
 1295:                     my $preprocess=&Apache::lonxml::get_param('preprocess',$parstack,$safeeval);
 1296:                     $preprocess=~s/^\&//;
 1297:                     if (defined($preprocess)) {
 1298:                         &Apache::run::run('$LONCAPA::response=&'.$preprocess.'($LONCAPA::response);',$safeeval);
 1299:                     }
 1300: 		    $result = &Apache::run::run('if ($LONCAPA::response=~m'.$answer.') { return 1; } else { return 0; }',$safeeval);
 1301: 		    &Apache::lonxml::debug("current $response");
 1302: 		    &Apache::lonxml::debug("current $answer");
 1303: 		    $ad = ($result) ? 'APPROX_ANS' : 'INCORRECT';
 1304: 		} else {
 1305: 		    my @args = ('type','preprocess');
 1306: 		    my $args_ref = &setup_capa_args($safeeval,$parstack,
 1307: 						    \@args,$response);
 1308:                     if ($$args_ref{'type'} eq '') {
 1309:                         $$args_ref{'type'} = 'ci';
 1310:                     }
 1311: 		    &add_in_tag_answer($parstack,$safeeval);
 1312: 		    my (@final_awards,@final_msgs,@names,%ansstring);
 1313: 		    foreach my $name (keys(%answer)) {
 1314: 			&Apache::lonxml::debug(" doing $name with ".join(':',@{ $answer{$name}{'answers'} }));
 1315: 			${$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=dclone($answer{$name});
 1316: 			my ($result, @msgs)=&Apache::run::run("&caparesponse_check_list()",$safeeval);
 1317:                         if ($$args_ref{'type'} =~ /^c[si]$/) {
 1318:                             $ansstring{$name} = pop(@msgs);
 1319:                             my $control_chars_removed = pop(@msgs);
 1320:                             my $error = pop(@msgs);
 1321:                             if (($error ne '') || 
 1322:                                 ($control_chars_removed)) {
 1323:                                 my ($symb,$courseid,$sdomain,$sname) =
 1324:                                     &Apache::lonnet::whichuser();
 1325:                                 if ($control_chars_removed) {
 1326:                                     my $showresponse = $response;
 1327:                                     if ($response =~ /[\000-\037]/) {
 1328:                                         $response =~ s/[\000-\037]//g;
 1329:                                     }
 1330:                                     if ($showresponse  =~ /[\r\n\f]/) {
 1331:                                         my @lines = split(/[\r\n\f]+/,$showresponse);
 1332:                                         $showresponse = join('\\n',@lines);
 1333:                                     }
 1334:                                     &Apache::lonnet::logthis("Stringresponse grading: control characters stripped from submission ".$showresponse." for $sname:$sdomain in $courseid for part: $part response: $id and symb: $symb");
 1335:                                     $Apache::lonhomework::results{"resource.$part.$id.submission"} = $response;
 1336:                                 }
 1337:                                 if ($error ne '') {
 1338:                                     &Apache::lonnet::logthis("Stringresponse grading error: $error for $sname:$sdomain in $courseid for part: $part response: $id and symb: $symb");
 1339:                                 }
 1340:                             }
 1341:                         }
 1342: 			&Apache::lonxml::debug('msgs are'.join(':',@msgs));
 1343: 			my ($awards)=split(/:/,$result);
 1344: 			my (@awards) = split(/,/,$awards);
 1345: 			($ad,$msg) = 
 1346: 			    &Apache::inputtags::finalizeawards(\@awards,\@msgs);
 1347: 			push(@final_awards,$ad);
 1348: 			push(@final_msgs,$msg);
 1349: 			push(@names,$name);
 1350: 			&Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
 1351: 		    }
 1352: 		    ($ad, $msg, my $name) = 
 1353: 			&Apache::inputtags::finalizeawards(\@final_awards,
 1354: 							   \@final_msgs,
 1355: 							   \@names,1);
 1356:                     if (keys(%ansstring) > 0) {
 1357:                         $Apache::lonhomework::results{"resource.$part.$id.answerstring"} = &Apache::lonnet::hash2str(%ansstring);
 1358:                     }
 1359: 		}
 1360:                 if (($ad eq 'INCORRECT' || $ad eq 'APPROX_ANS' ||
 1361:                      $ad eq 'EXACT_ANS')) {
 1362: 		    if ($Apache::lonhomework::type eq 'survey') {
 1363: 		        $ad='SUBMITTED';
 1364: 		    } elsif ($Apache::lonhomework::type eq 'surveycred') {
 1365:                         $ad='SUBMITTED_CREDIT';
 1366:                     } elsif ($Apache::lonhomework::type eq 'anonsurvey') {
 1367:                         $ad='ANONYMOUS';
 1368:                     } elsif ($Apache::lonhomework::type eq 'anonsurveycred') {
 1369:                         $ad='ANONYMOUS_CREDIT';
 1370:                     }
 1371:                 }
 1372:                 unless (($env{'request.state'} eq 'construct') || 
 1373:                         ($Apache::lonhomework::type eq 'randomizetry')) {
 1374:                     if (($ad eq 'INCORRECT' || $ad eq 'APPROX_ANS' || $ad eq 'EXACT_ANS')) {
 1375:                         if ($previous{'used'}) {
 1376:                             if ($ad ne $previous{'award'}) {
 1377:                                 if (($previous{'award'} eq 'INCORRECT' || 
 1378:                                      $previous{'award'} eq 'APPROX_ANS' ||
 1379:                                      $previous{'award'} eq 'EXACT_ANS')) {
 1380:                                     &stringresponse_gradechange($part,$id,\%previous,
 1381:                                                                 'cs',$response,$ad,$type);
 1382:                                 }
 1383:                             }
 1384:                         } elsif ($previous{'usedci'}) {
 1385:                             if ($ad ne $previous{'awardci'}) {
 1386:                                 if (($previous{'awardci'} eq 'INCORRECT' || 
 1387:                                      $previous{'awardci'} eq 'APPROX_ANS' ||
 1388:                                      $previous{'awardci'} eq 'EXACT_ANS')) {
 1389:                                     &stringresponse_gradechange($part,$id,\%previous,
 1390:                                                                 'ci',$response,$ad,$type);
 1391:                                 }
 1392:                             }
 1393:                         }
 1394:                     }
 1395:                 }
 1396: 		&Apache::response::handle_previous(\%previous,$ad);
 1397: 		$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
 1398: 		$Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=$msg;
 1399: 	    }
 1400: 	}
 1401:     } elsif ($target eq 'answer' || $target eq 'analyze') {
 1402: 	&add_in_tag_answer($parstack,$safeeval);
 1403: 	if ($target eq 'analyze') {
 1404: 	    push (@{ $Apache::lonhomework::analyze{"parts"} },"$part.$id");
 1405: 	    $Apache::lonhomework::analyze{"$part.$id.type"} = 'stringresponse';
 1406: 	    &Apache::response::check_if_computed($token,$parstack,$safeeval,
 1407: 						 'answer');
 1408: 	}
 1409: 	&Apache::response::setup_params('stringresponse',$safeeval);
 1410: 	if ($target eq 'answer') {
 1411: 	    $result.=&Apache::response::answer_header('stringresponse');
 1412: 	}
 1413: 	foreach my $name (keys(%answer)) {
 1414: 	    my @answers = @{ $answer{$name}{'answers'} };
 1415: 	    for (my $i=0;$i<=$#answers;$i++) {
 1416: 		my $answer_part = $answers[$i];
 1417: 		foreach my $element (@{$answer_part}) {
 1418: 		    if ($target eq 'answer') {
 1419: 			$result.=&Apache::response::answer_part('stringresponse',
 1420: 								$element);
 1421:                         if ($env{'form.grade_retrieveanswers'}) {
 1422:                             $env{'form.grade_answers.resource.'.$part.'.'.$id} = $element;
 1423:                         }
 1424: 		    } elsif ($target eq 'analyze') {
 1425: 			push (@{ $Apache::lonhomework::analyze{"$part.$id.answer"}{$name}[$i] },
 1426: 			      $element);
 1427: 		    }
 1428: 		}
 1429: 		if ($target eq 'answer' && $type eq 're') {
 1430: 		    $result.=&Apache::response::answer_part('stringresponse',
 1431: 							    $answerdisplay);
 1432: 		}
 1433: 	    }
 1434: 	}
 1435: 	my $string='Case Insensitive';
 1436: 	if ($type eq 'mc') {
 1437: 	    $string='Multiple Choice';
 1438: 	} elsif ($type eq 'cs') {
 1439: 	    $string='Case Sensitive';
 1440: 	} elsif ($type eq 'ci') {
 1441: 	    $string='Case Insensitive';
 1442: 	} elsif ($type eq 're') {
 1443: 	    $string='Regular Expression';
 1444: 	}
 1445: 	if ($target eq 'answer') {
 1446: 	    if ($env{'form.answer_output_mode'} eq 'tex') {
 1447: 		$result.=&Apache::response::answer_part('stringresponse',
 1448: 							"$string");
 1449: 	    } else {
 1450: 		$result.=&Apache::response::answer_part('stringresponse',
 1451: 							"<b>$string</b>");
 1452: 	    }
 1453: 	} elsif ($target eq 'analyze') {
 1454: 	    push (@{$Apache::lonhomework::analyze{"$part.$id.str_type"}},
 1455: 		  $type);
 1456: 	}
 1457: 	if ($target eq 'answer') {
 1458: 	    $result.=&Apache::response::answer_footer('stringresponse');
 1459: 	}
 1460:     } elsif ($target eq 'edit') {
 1461: 	$result.='</td></tr>'.&Apache::edit::end_table;
 1462:     } elsif ($target eq 'web' || $target eq 'tex') {
 1463: 	&Apache::response::setup_prior_tries_hash(\&format_prior_response_string);
 1464:     }
 1465:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
 1466: 	$target eq 'tex' || $target eq 'analyze') {
 1467:         my $repetition = &Apache::response::repetition();
 1468: 	&Apache::lonxml::increment_counter($repetition,"$part.$id");
 1469: 	if ($target eq 'analyze') {
 1470: 	    &Apache::lonhomework::set_bubble_lines();
 1471: 	}
 1472:     }
 1473:     &Apache::response::end_response;
 1474:     return $result;
 1475: }
 1476: 
 1477: sub start_formularesponse {
 1478:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
 1479:     my $result;
 1480:     if ($target eq 'meta') {
 1481: 	&Apache::response::start_response($parstack,$safeeval);
 1482: 	$result=&Apache::response::meta_package_write('formularesponse');
 1483: 	&Apache::response::end_response();
 1484:     } else {
 1485: 	$result.=&start_numericalresponse(@_);
 1486:     }
 1487:     return $result;
 1488: }
 1489: 
 1490: sub end_formularesponse {
 1491:     return end_numericalresponse(@_);
 1492: }
 1493: 
 1494: 1;
 1495: __END__
 1496: 

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