File:  [LON-CAPA] / loncom / homework / caparesponse / caparesponse.pm
Revision 1.119: download - view: text, annotated - select for diffs
Thu Oct 23 19:29:24 2003 UTC (20 years, 7 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- need to reset seed so as to not futz with things since the grade pass doesn't make bubbles

    1: # The LearningOnline Network with CAPA
    2: # caparesponse definition
    3: #
    4: # $Id: caparesponse.pm,v 1.119 2003/10/23 19:29:24 albertel 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: 
   33: BEGIN {
   34:     &Apache::lonxml::register('Apache::caparesponse',('caparesponse','numericalresponse','stringresponse','formularesponse'));
   35: }
   36: 
   37: sub start_numericalresponse {
   38:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   39:     my $id = &Apache::response::start_response($parstack,$safeeval);
   40:     my $result;
   41:     if ($target eq 'edit') {
   42: 	$result.=&Apache::edit::tag_start($target,$token);
   43: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
   44: 	$result.=&Apache::edit::text_arg('Incorrect Answers:','incorrect',
   45: 					 $token);
   46: 	if ($token->[1] eq 'numericalresponse') {
   47: 	    $result.=&Apache::edit::text_arg('Unit:','unit',$token,5).
   48: 		&Apache::loncommon::help_open_topic('Physical_Units');
   49: 	    $result.=&Apache::edit::text_arg('Format:','format',$token,4).
   50: 		&Apache::loncommon::help_open_topic('Numerical_Response_Format');
   51: 	} elsif ($token->[1] eq 'stringresponse') {
   52: 	    $result.=&Apache::edit::select_arg('Type:','type',
   53: 			 [['cs','Case Sensitive'],['ci','Case Insensitive'],
   54: 			  ['mc','Case Insensitive, Any Order']],$token);
   55: 	} elsif ($token->[1] eq 'formularesponse') {
   56: 	    $result.=&Apache::edit::text_arg('Sample Points:','samples',
   57: 					     $token,40).
   58: 	      &Apache::loncommon::help_open_topic('Formula_Response_Sampling');
   59: 	}
   60: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
   61:     } elsif ($target eq 'modified') {
   62: 	my $constructtag;
   63: 	if ($token->[1] eq 'numericalresponse') {
   64: 	    $constructtag=&Apache::edit::get_new_args($token,$parstack,
   65: 						      $safeeval,'answer',
   66: 						      'incorrect','unit',
   67: 						      'format');
   68: 	} elsif ($token->[1] eq 'stringresponse') {
   69: 	    $constructtag=&Apache::edit::get_new_args($token,$parstack,
   70: 						      $safeeval,'answer',
   71: 						      'type');
   72: 	} elsif ($token->[1] eq 'formularesponse') {
   73: 	    $constructtag=&Apache::edit::get_new_args($token,$parstack,
   74: 						      $safeeval,'answer',
   75: 						      'samples');
   76: 	}
   77: 	if ($constructtag) {
   78: 	    $result = &Apache::edit::rebuild_tag($token);
   79: 	    $result.=&Apache::edit::handle_insert();
   80: 	}
   81:     } elsif ($target eq 'meta') {
   82: 	$result=&Apache::response::meta_package_write('numericalresponse');
   83:     } elsif ($target eq 'answer' || $target eq 'grade') {
   84: 	&Apache::response::reset_params();
   85:     } elsif ($target eq 'web') {
   86: 	my $partid = $Apache::inputtags::part;
   87: 	my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
   88: 	&Apache::lonxml::debug("Got unit $hideunit for $partid $id");
   89: 	#no way to enter units, with radio buttons
   90: 	if (lc($hideunit) eq "yes") {
   91: 	    my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
   92: 						    $safeeval);
   93: 	    if ($unit =~ /\S/) { $result.=" (in $unit) "; }
   94: 	}
   95:     }
   96:     return $result;
   97: }
   98: 
   99: sub end_numericalresponse {
  100:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  101:     my $increment=1;
  102:     my $result = '';
  103:     if (!$Apache::lonxml::default_homework_loaded) {
  104: 	&Apache::lonxml::default_homework_load($safeeval);
  105:     }
  106:     if ( $target eq 'grade' && defined($ENV{'form.submitted'})) {
  107: 	&Apache::response::setup_params($$tagstack[-1]);
  108: 	$safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
  109: 	my $partid = $Apache::inputtags::part;
  110: 	my $id = $Apache::inputtags::response['-1'];
  111: 	if ($Apache::lonhomework::type eq 'exam' && 
  112: 	    ($$tagstack[-1] eq 'formularesponse' ||
  113: 	     $$tagstack[-1] eq 'stringresponse')) {
  114: 	    $increment=&Apache::response::scored_response($partid,$id);
  115: 	} else {
  116: 	    my $response = &Apache::response::getresponse();
  117: 	    if ( $response =~ /[^\s]/) {
  118: 		my $ad;
  119: 		my %previous = &Apache::response::check_for_previous($response,$partid,$id);
  120: 		&Apache::lonxml::debug("submitted a $response<br>\n");
  121: 		&Apache::lonxml::debug($$parstack[-1] . "\n<br>");
  122: 		
  123: 		if ($ENV{'form.submitted'} eq 'scantron') {
  124: 		    my $number_of_bubbles = &Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.numbubbles');
  125: 		    if (!$number_of_bubbles) { $number_of_bubbles=8; }
  126: 		    my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
  127: 		    my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
  128: 		    my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
  129: 		    my @values=&make_numerical_bubbles($number_of_bubbles,$target,$answers[0],$formats[0],\@incorrect);
  130: 		    $response=$values[$response];
  131: 		} else {
  132: 		    $response =~ s/\\/\\\\/g;
  133: 		    $response =~ s/\'/\\\'/g;
  134: 		}
  135: 		$Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
  136: 		&Apache::lonxml::debug("current $response");
  137: 		my $expression="&caparesponse_check_list('".$response."','".
  138: 		    $$parstack[-1];
  139: 		my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
  140: 		#no way to enter units, with radio buttons
  141: 		if ($Apache::lonhomework::type eq 'exam' ||
  142: 		    lc($hideunit) eq "yes") {
  143: 		    $expression.=';my $unit=undef;';
  144: 		}
  145: 		foreach my $key (keys(%Apache::inputtags::params)) {
  146: 		    $expression.= ';my $'. #'
  147: 			$key.'="'.$Apache::inputtags::params{$key}.'"';
  148: 		}
  149: 		if ($$tagstack[-1] eq 'formularesponse') {
  150: 		    $expression.=';my $type="fml";';
  151: 		} elsif ($$tagstack[-1] eq 'numericalresponse') {
  152: 		    $expression.=';my $type="float";';
  153: 		}
  154: 		$expression.="');";
  155: 		my @answer=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
  156: 		&Apache::lonxml::debug('answer is'.join(':',@answer));
  157: 		@{$safeeval->varglob('CAPARESPONSE_CHECK_LIST_answer')}=@answer;
  158: 		
  159: 		$result = &Apache::run::run($expression,$safeeval);
  160: 		my ($awards) = split /:/ , $result;
  161: 		($ad) = &Apache::inputtags::finalizeawards(split /,/ , $awards);
  162: 		&Apache::lonxml::debug("$expression");
  163: 		&Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
  164: 		&Apache::response::handle_previous(\%previous,$ad);
  165: 		$Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
  166: 		$result='';
  167: 	    }
  168: 	}
  169:     } elsif ($target eq 'web' || $target eq 'tex') {
  170: 	my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
  171: 						     $safeeval);
  172: 	my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
  173: 	my $status = $Apache::inputtags::status['-1'];
  174: 	if (  &Apache::response::show_answer() ) {
  175: 	    my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,
  176: 							 $safeeval);
  177: 	    my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
  178: 						    $safeeval);
  179: 	    if ($target eq 'web') {
  180: 		$result="<br />The correct answer is ";
  181: 	    }
  182: 	    for (my $i=0; $i <= $#answers; $i++) {
  183: 		my $answer=$answers[$i];
  184: 		my $format;
  185: 		if ($#formats > 0) {
  186: 		    $format=$formats[$i];
  187: 		} else {
  188: 		    $format=$formats[0];
  189: 		}
  190: 		my $formatted;
  191: 		if ((defined($format)) && ($format ne '')) {
  192: 		    $format=~s/e/E/g;
  193: 		    &Apache::lonxml::debug("formatting with :$format: answer :$answer:");
  194: 		    $formatted=sprintf('%.'.$format,$answer).',';
  195: 		} else {
  196: 		    &Apache::lonxml::debug("no format answer :$answer:");
  197: 		    $formatted="$answer,";
  198: 		}
  199: 		if ($target eq 'tex') {
  200: 		    $formatted='';
  201: 		    #$formatted=&Apache::lonxml::latex_special_symbols($formatted);
  202: 		}
  203: 		$result.=$formatted;
  204: 	    }
  205: 	    chop $result;
  206: 	    if ($target eq 'web') {
  207: 		$result.=" $unit.<br />";
  208: 	    }
  209: 	}
  210: 	if ($Apache::lonhomework::type eq 'exam') {
  211: 	    my $partid=$Apache::inputtags::part;
  212: 	    my $id=$Apache::inputtags::response[-1];
  213: 	    my $number_of_bubbles = &Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.numbubbles');
  214: 	    if (!$number_of_bubbles) { $number_of_bubbles=8; }
  215: 	    my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,
  216: 							 $safeeval);
  217: 	    my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
  218: 						    $safeeval);
  219: 	    my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
  220: 	    my @bubble_values=&make_numerical_bubbles($number_of_bubbles,
  221: 						      $target,$answers[0],
  222: 						      $formats[0],\@incorrect);
  223: 	    my @alphabet=('A'..'Z');
  224: 	    if ($target eq 'web') {
  225: 		if ($$tagstack[-1] eq 'numericalresponse') {
  226: 		    if ($unit=~/\S/) {$result.=' (in '.$unit.')<br /><br />';}
  227: 		    $result.= '<table border="1"><tr>';
  228: 		    my $previous=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.$id.submission"};
  229: 		    for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
  230: 			my $checked='';
  231: 			if ($previous eq $bubble_values[$ind]) {
  232: 			    $checked=" checked='on' ";
  233: 			}
  234: 			$result.='<td><input type="radio" name="HWVAL_'.$id.
  235: 			    '" value="'.$bubble_values[$ind].'" '.$checked
  236: 			    .' /><b>'.$alphabet[$ind].'</b>: '.
  237: 			    $bubble_values[$ind].'</td>';
  238: 		    }
  239: 		    $result.='</tr></table>';
  240: 		} elsif ($$tagstack[-1] eq 'formularesponse') {
  241: 		    $result.= '<br /><br /><font color="red">
  242:                            <textarea name="HWVAL_'.$id.'" rows="4" cols="50">
  243:                            </textarea></font> <br /><br />';
  244: 		}
  245: 	    } elsif ($target eq 'tex') {
  246: 		if ((defined $unit) and ($unit=~/\S/) and ($Apache::lonhomework::type eq 'exam')) {
  247: 		    $result.=' \textit{(in} \verb|'.$unit.'|\textit{)} ';
  248: 		}
  249: 		if ($$tagstack[-1] eq 'numericalresponse') {
  250: 		    my ($celllength,$number_of_tables,@table_range)=
  251: 			&get_table_sizes($formats[0],$number_of_bubbles);
  252: 		    my $j=0;
  253: 		    my $cou=0;
  254: 		    $result.='\vskip -1 mm \noindent \begin{enumerate}\item[\textbf{'.$Apache::lonxml::counter.'}.]';
  255: 		    for (my $i=0;$i<$number_of_tables;$i++) {
  256: 			$result.='\vskip -1 mm \noindent \begin{tabular}{';
  257: 			for (my $ind=0;$ind<$table_range[$j];$ind++) {
  258: 			    $result.='lp{'.$celllength.' mm}';
  259: 			}
  260: 			$result.='}';
  261: 			for (my $ind=$cou;$ind<$cou+$table_range[$j];$ind++) {
  262: 			    $result.='\hskip -3 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -2 mm & {\small '.$bubble_values[$ind].'} ';
  263: 			    if ($ind != $cou+$table_range[$j]-1) {$result.=' & ';}
  264: 			}
  265: 			$cou += $table_range[$j];
  266: 			$j++;
  267: 			$result.='\\\\\end{tabular}\vskip 0 mm ';
  268: 		    }
  269: 		    $result.='\end{enumerate}';
  270: 		} else {
  271: 		    $result.='\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
  272: 		    my $repetition = &Apache::response::repetition();
  273: 		    $result.='\begin{enumerate}';
  274: 		    for (my $i=0;$i<$repetition;$i++) {
  275: 			$result.='\item[\textbf{'.($Apache::lonxml::counter+$i).'}.]\textit{Leave blank on scoring form}\vskip 0 mm';
  276: 		    }
  277: 		    $increment=$repetition;
  278: 		    $result.= '\end{enumerate}';
  279: 		}
  280: 	    }
  281: 	}
  282:     } elsif ($target eq 'edit') {
  283: 	$result.='</td></tr>'.&Apache::edit::end_table;
  284:     } elsif ($target eq 'answer' || $target eq 'analyze') {
  285: 	
  286: 	my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
  287: 	if ($target eq 'analyze') {
  288: 	    push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
  289: 	    $Apache::lonhomework::analyze{"$part_id.type"} = $$tagstack[-1];
  290: 	    my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
  291: 	    push (@{ $Apache::lonhomework::analyze{"$part_id.incorrect"} }, @incorrect);
  292: 	}
  293: 	&Apache::response::setup_params($$tagstack[-1]);
  294: 	my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
  295: 	my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
  296: 	my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval);
  297: 	my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
  298: 	
  299: 	if ($target eq 'answer') {
  300: 	    $result.=&Apache::response::answer_header($$tagstack[-1]);
  301: 	}
  302: 	for(my $i=0;$i<=$#answers;$i++) {
  303: 	    my $ans=$answers[$i];
  304: 	    my $fmt=$formats[0];
  305: 	    if (@formats && $#formats) {$fmt=$formats[$i];}
  306: 	    my ($high,$low);
  307: 	    if ($Apache::inputtags::params{'tol'}) {
  308: 		($high,$low)=&get_tolrange($ans,$Apache::inputtags::params{'tol'});
  309: 	    }
  310: 	    my ($sighigh,$siglow);
  311: 	    if ($Apache::inputtags::params{'sig'}) {
  312: 		($sighigh,$siglow)=&get_sigrange($Apache::inputtags::params{'sig'});
  313: 	    }
  314: 	    if ($fmt && $$tagstack[-1] eq 'numericalresponse') {
  315: 		$fmt=~s/e/E/g;
  316: 		$ans = sprintf('%.'.$fmt,$ans);
  317: 		if ($high) {
  318: 		    $high=sprintf('%.'.$fmt,$high);
  319: 		    $low =sprintf('%.'.$fmt,$low);
  320: 		}
  321: 	    }
  322: 	    if ($target eq 'answer') {
  323: 		if ($high && $$tagstack[-1] eq 'numericalresponse') { $ans.=' ['.$low.','.$high.']'; }
  324: 		if ($sighigh && $$tagstack[-1] eq 'numericalresponse') {
  325: 		    if ($ENV{'form.answer_output_mode'} eq 'tex') {
  326: 			$ans.= " Sig $siglow - $sighigh";
  327: 		    } else {
  328: 			$ans.= " Sig <i>$siglow - $sighigh</i>";
  329: 		    }
  330: 		}
  331: 		$result.=&Apache::response::answer_part($$tagstack[-1],$ans);
  332: 	    } elsif ($target eq 'analyze') {
  333: 		push (@{ $Apache::lonhomework::analyze{"$part_id.answer"} }, $ans);
  334: 		if ($high) {
  335: 		    push (@{ $Apache::lonhomework::analyze{"$part_id.ans_high"} }, $high);
  336: 		    push (@{ $Apache::lonhomework::analyze{"$part_id.ans_low"} }, $low);
  337: 		}
  338: 	    }
  339: 	}
  340: 	if (defined($unit) and ($unit ne '') and
  341: 	    $$tagstack[-1] eq 'numericalresponse') {
  342: 	    if ($target eq 'answer') {
  343: 		if ($ENV{'form.answer_output_mode'} eq 'tex') {
  344: 		    $result.=&Apache::response::answer_part($$tagstack[-1],
  345: 							    " Unit: $unit ");
  346: 		} else {
  347: 		    $result.=&Apache::response::answer_part($$tagstack[-1],
  348: 							    "Unit: <b>$unit</b>");
  349: 		}
  350: 	    } elsif ($target eq 'analyze') {
  351: 		push (@{ $Apache::lonhomework::analyze{"$part_id.unit"} }, $unit);
  352: 	    }
  353: 	}
  354: 	if ($type || $$tagstack[-1] eq 'stringresponse') {
  355: 	    my $string='Case Insensitive';
  356: 	    if ($type eq 'mc') {
  357: 		$string='Multiple Choice';
  358: 	    } elsif ($type eq 'cs') {
  359: 		$string='Case Sensitive';
  360: 	    } elsif ($type eq 'ci') {
  361: 		$string='Case Insensitive';
  362: 	    } elsif ($type eq 'fml') {
  363: 		$string='Formula';
  364: 	    }
  365: 	    if ($target eq 'answer') {
  366: 		if ($ENV{'form.answer_output_mode'} eq 'tex') {
  367: 		    $result.=&Apache::response::answer_part($$tagstack[-1],
  368: 							  "$string");
  369: 		} else {
  370: 		    $result.=&Apache::response::answer_part($$tagstack[-1],
  371: 							    "<b>$string</b>");
  372: 		}
  373: 	    } elsif ($target eq 'analyze') {
  374: 		push (@{ $Apache::lonhomework::analyze{"$part_id.str_type"} },
  375: 		      $type);
  376: 	    }
  377: 	}
  378: 	if ($$tagstack[-1] eq 'formularesponse' && $target eq 'answer') {
  379: 	    my $samples=&Apache::lonxml::get_param('samples',$parstack,$safeeval);
  380: 	    $result.=&Apache::response::answer_part($$tagstack[-1],$samples);
  381: 	}
  382: 	if ($target eq 'answer') {
  383: 	    $result.=&Apache::response::answer_footer($$tagstack[-1]);
  384: 	}
  385:     }
  386:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
  387: 	$target eq 'tex' || $target eq 'analyze') {
  388: 	&Apache::lonxml::increment_counter($increment);
  389:     }
  390:     &Apache::response::end_response;
  391:     return $result;
  392: }
  393: 
  394: sub get_table_sizes {
  395:     my ($format,$number_of_bubbles)=@_;
  396:     my $max_val = 0;
  397:     if ($format=~m/^(\d+)E([^\d]*)(\d*)$/) {
  398: 	$max_val=$1+$2+4;
  399:     } else {
  400: 	$max_val=4;
  401:     }
  402:     $max_val = int(0.9*$ENV{'form.textwidth'}/(($max_val+6)*2));
  403:     my $celllength = 0.9*$ENV{'form.textwidth'}/$max_val-10;
  404:     my @table_range = ();
  405:     my $number_of_tables = int($number_of_bubbles/$max_val);
  406:     for (my $i=0;$i<$number_of_tables;$i++) {push @table_range,$max_val;}
  407:     if ($number_of_bubbles % $max_val != 0) {
  408: 	$number_of_tables++;
  409: 	push @table_range,($number_of_bubbles % $max_val);
  410:     }
  411:     return ($celllength,$number_of_tables,@table_range);
  412: }
  413: 
  414: sub format_number {
  415:     my ($number,$format,$target)=@_;
  416:     my $ans;
  417:     if ($format ne '') {
  418: 	$format=~s/e/E/g;
  419: 	$ans = sprintf('%.'.$format,$number);
  420:     } else {
  421: 	my $format = '';
  422: 	#What is the number? (integer,decimal,floating point)
  423: 	if ($number=~/^(\d*\.?\d*)(E|e)(\d*)$/) {
  424: 	    $format = '3e';
  425: 	} elsif ($number=~/^(\d*)\.(\d*)$/) {
  426: 	    $format = '4f';
  427: 	} elsif ($number=~/^(\d*)$/) {
  428: 	    $format = 'd';
  429: 	}
  430: 	$ans = sprintf('%.'.$format,$number);
  431:     }
  432:     if ($target eq 'tex') {
  433: 	if ($ans =~ m/([0-9\.\-\+]+)E([0-9\-\+]+)/ ) {
  434: 	    my $number = $1;
  435: 	    my $power = $2;
  436: 	    $power=~s/^\+//;
  437: 	    $power=~s/^(-?)0+(\d+)//;
  438: 	    $ans=$number.'$\times 10^{'.$1.$2.'}$'; #'stupidemacs
  439: 	}
  440:     }
  441:     return $ans;
  442: }
  443: 
  444: sub make_numerical_bubbles {
  445:     my ($number_of_bubbles,$target,$answer,$format,$incorrect) =@_;
  446:     my @bubble_values = ();
  447:     &Apache::lonxml::debug("answer is $answer incorrect is $incorrect");
  448:     my @oldseed=&Math::Random::random_get_seed();
  449:     if (defined($incorrect) && ref($incorrect)) {
  450: 	&Apache::lonxml::debug("inside ".(scalar(@$incorrect)+1 gt $number_of_bubbles));
  451: 	if (scalar(@$incorrect)+1 >= $number_of_bubbles) {
  452: 	    &Apache::lonxml::debug("inside ".(scalar(@$incorrect)+1).":$number_of_bubbles");
  453: 	    &Apache::response::setrandomnumber();
  454: 	    my @rand_inc=&Math::Random::random_permutation(@$incorrect);
  455: 	    @bubble_values=@rand_inc[0..($number_of_bubbles-2)];
  456: 	    @bubble_values=sort {$a <=> $b} (@bubble_values,$answer);
  457: 	    &Apache::lonxml::debug("Answer was :$answer: returning :".$#bubble_values.": whih are :".join(':',@bubble_values));
  458: 	    &Math::Random::random_set_seed(@oldseed);
  459: 	    return @bubble_values;
  460: 	}
  461: 	#FIXME what to do when not enough incorrects specified?
  462:     }
  463:     my @factors = (1.13,1.17,1.25,1.33,1.45); #default values of factors
  464:     my @powers = (1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0); #default values for powers
  465:     &Apache::response::setrandomnumber();
  466:     my $ind=&Math::Random::random_uniform_integer(1,0,$#powers);
  467:     my $power = $powers[$ind];
  468:     $ind=&Math::Random::random_uniform_integer(1,0,$#factors);
  469:     my $factor = $factors[$ind];
  470:     for ($ind=0;$ind<$number_of_bubbles;$ind++) {
  471: 	$bubble_values[$ind] = $answer*($factor**($power-$powers[$#powers-$ind]));
  472: 	$bubble_values[$ind] = &format_number($bubble_values[$ind],
  473: 					       $format,$target);
  474: 
  475:     }
  476:     &Math::Random::random_set_seed(@oldseed);
  477:     return @bubble_values;
  478: }
  479: 
  480: sub get_tolrange {
  481:     my ($ans,$tol)=@_;
  482:     my ($high,$low);
  483:     if ($tol =~ /%$/) {
  484: 	chop($tol);
  485: 	my $change=$ans*($tol/100.0);
  486: 	$high=$ans+$change;
  487: 	$low=$ans-$change;
  488:     } else {
  489: 	$high=$ans+$tol;
  490: 	$low=$ans-$tol;
  491:     }
  492:     return ($high,$low);
  493: }
  494: 
  495: sub get_sigrange {
  496:     my ($sig)=@_;
  497:     &Apache::lonxml::debug("Got a sig of :$sig:");
  498:     my $sig_lbound;
  499:     my $sig_ubound;
  500:     if ($sig eq '') {
  501: 	$sig_lbound = 0; #SIG_LB_DEFAULT
  502: 	$sig_ubound =15; #SIG_UB_DEFAULT
  503:     } else {
  504: 	($sig_lbound,$sig_ubound) = split(/,/,$sig);
  505: 	if (!$sig_lbound) {
  506: 	    $sig_lbound = 0; #SIG_LB_DEFAULT
  507: 	    $sig_ubound =15; #SIG_UB_DEFAULT
  508: 	}
  509: 	if (!$sig_ubound) { $sig_ubound=$sig_lbound; }
  510:     }
  511:     return ($sig_ubound,$sig_lbound);
  512: }
  513: 
  514: sub start_stringresponse {
  515:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  516:     my $result;
  517:     if ($target eq 'meta') {
  518: 	&Apache::response::start_response($parstack,$safeeval);
  519: 	$result=&Apache::response::meta_package_write('stringresponse');
  520: 	&Apache::response::end_response();
  521:     } else {
  522: 	$result.=&start_numericalresponse(@_);
  523:     }
  524:     return $result;
  525: }
  526: 
  527: sub end_stringresponse {
  528:     return end_numericalresponse(@_);
  529: }
  530: 
  531: sub start_formularesponse {
  532:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  533:     my $result;
  534:     if ($target eq 'meta') {
  535: 	&Apache::response::start_response($parstack,$safeeval);
  536: 	$result=&Apache::response::meta_package_write('formularesponse');
  537: 	&Apache::response::end_response();
  538:     } else {
  539: 	$result.=&start_numericalresponse(@_);
  540:     }
  541:     return $result;
  542: }
  543: 
  544: sub end_formularesponse {
  545:     return end_numericalresponse(@_);
  546: }
  547: 
  548: 1;
  549: __END__
  550: 

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