File:  [LON-CAPA] / loncom / homework / caparesponse / caparesponse.pm
Revision 1.107.2.3: download - view: text, annotated - select for diffs
Wed Oct 15 21:26:46 2003 UTC (20 years, 7 months ago) by albertel
Branches: version_1_0_2
CVS tags: version_1_0_3
Diff to branchpoint 1.107: preferred, unified
- backport 1.115

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

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