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

1.3       albertel    1: # The LearningOnline Network with CAPA
                      2: # caparesponse definition
1.47      albertel    3: #
1.120   ! sakharuk    4: # $Id: caparesponse.pm,v 1.119 2003/10/23 19:29:24 albertel Exp $
1.47      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.3       albertel   28: 
                     29: package Apache::caparesponse;
                     30: use strict;
1.4       albertel   31: use capa;
1.3       albertel   32: 
1.50      harris41   33: BEGIN {
1.89      albertel   34:     &Apache::lonxml::register('Apache::caparesponse',('caparesponse','numericalresponse','stringresponse','formularesponse'));
1.3       albertel   35: }
                     36: 
1.89      albertel   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);
1.117     albertel   44: 	$result.=&Apache::edit::text_arg('Incorrect Answers:','incorrect',
                     45: 					 $token);
1.89      albertel   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',
1.117     albertel   66: 						      'incorrect','unit',
                     67: 						      'format');
1.89      albertel   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();
1.22      albertel   80: 	}
1.89      albertel   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();
1.96      albertel   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: 	}
1.16      albertel   95:     }
1.89      albertel   96:     return $result;
1.21      albertel   97: }
                     98: 
1.89      albertel   99: sub end_numericalresponse {
                    100:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.90      albertel  101:     my $increment=1;
1.89      albertel  102:     my $result = '';
                    103:     if (!$Apache::lonxml::default_homework_loaded) {
                    104: 	&Apache::lonxml::default_homework_load($safeeval);
1.34      albertel  105:     }
1.95      albertel  106:     if ( $target eq 'grade' && defined($ENV{'form.submitted'})) {
1.90      albertel  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'];
1.95      albertel  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') {
1.117     albertel  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);
1.95      albertel  130: 		    $response=$values[$response];
                    131: 		} else {
                    132: 		    $response =~ s/\\/\\\\/g;
                    133: 		    $response =~ s/\'/\\\'/g;
                    134: 		}
1.115     albertel  135: 		$Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
1.95      albertel  136: 		&Apache::lonxml::debug("current $response");
                    137: 		my $expression="&caparesponse_check_list('".$response."','".
                    138: 		    $$parstack[-1];
1.96      albertel  139: 		my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
1.95      albertel  140: 		#no way to enter units, with radio buttons
1.96      albertel  141: 		if ($Apache::lonhomework::type eq 'exam' ||
                    142: 		    lc($hideunit) eq "yes") {
1.95      albertel  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.="');";
1.105     albertel  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: 		
1.95      albertel  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='';
1.90      albertel  167: 	    }
1.89      albertel  168: 	}
                    169:     } elsif ($target eq 'web' || $target eq 'tex') {
1.90      albertel  170: 	my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
                    171: 						     $safeeval);
1.89      albertel  172: 	my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
                    173: 	my $status = $Apache::inputtags::status['-1'];
1.114     albertel  174: 	if (  &Apache::response::show_answer() ) {
1.89      albertel  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 '')) {
1.109     albertel  192: 		    $format=~s/e/E/g;
1.89      albertel  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') {
1.118     albertel  211: 	    my $partid=$Apache::inputtags::part;
                    212: 	    my $id=$Apache::inputtags::response[-1];
1.117     albertel  213: 	    my $number_of_bubbles = &Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.numbubbles');
                    214: 	    if (!$number_of_bubbles) { $number_of_bubbles=8; }
1.89      albertel  215: 	    my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,
                    216: 							 $safeeval);
                    217: 	    my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
                    218: 						    $safeeval);
1.117     albertel  219: 	    my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
1.90      albertel  220: 	    my @bubble_values=&make_numerical_bubbles($number_of_bubbles,
1.91      albertel  221: 						      $target,$answers[0],
1.117     albertel  222: 						      $formats[0],\@incorrect);
1.89      albertel  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>';
1.116     albertel  228: 		    my $previous=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.$id.submission"};
1.90      albertel  229: 		    for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
1.116     albertel  230: 			my $checked='';
                    231: 			if ($previous eq $bubble_values[$ind]) {
                    232: 			    $checked=" checked='on' ";
                    233: 			}
1.90      albertel  234: 			$result.='<td><input type="radio" name="HWVAL_'.$id.
1.116     albertel  235: 			    '" value="'.$bubble_values[$ind].'" '.$checked
                    236: 			    .' /><b>'.$alphabet[$ind].'</b>: '.
                    237: 			    $bubble_values[$ind].'</td>';
1.89      albertel  238: 		    }
                    239: 		    $result.='</tr></table>';
                    240: 		} elsif ($$tagstack[-1] eq 'formularesponse') {
1.90      albertel  241: 		    $result.= '<br /><br /><font color="red">
                    242:                            <textarea name="HWVAL_'.$id.'" rows="4" cols="50">
                    243:                            </textarea></font> <br /><br />';
1.89      albertel  244: 		}
                    245: 	    } elsif ($target eq 'tex') {
1.107     sakharuk  246: 		if ((defined $unit) and ($unit=~/\S/) and ($Apache::lonhomework::type eq 'exam')) {
1.89      albertel  247: 		    $result.=' \textit{(in} \verb|'.$unit.'|\textit{)} ';
                    248: 		}
                    249: 		if ($$tagstack[-1] eq 'numericalresponse') {
1.90      albertel  250: 		    my ($celllength,$number_of_tables,@table_range)=
                    251: 			&get_table_sizes($formats[0],$number_of_bubbles);
1.89      albertel  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}{';
1.90      albertel  257: 			for (my $ind=0;$ind<$table_range[$j];$ind++) {
1.89      albertel  258: 			    $result.='lp{'.$celllength.' mm}';
                    259: 			}
                    260: 			$result.='}';
1.90      albertel  261: 			for (my $ind=$cou;$ind<$cou+$table_range[$j];$ind++) {
1.91      albertel  262: 			    $result.='\hskip -3 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -2 mm & {\small '.$bubble_values[$ind].'} ';
1.89      albertel  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\\\\}}}';
1.93      albertel  272: 		    my $repetition = &Apache::response::repetition();
1.89      albertel  273: 		    $result.='\begin{enumerate}';
                    274: 		    for (my $i=0;$i<$repetition;$i++) {
1.93      albertel  275: 			$result.='\item[\textbf{'.($Apache::lonxml::counter+$i).'}.]\textit{Leave blank on scoring form}\vskip 0 mm';
1.89      albertel  276: 		    }
1.90      albertel  277: 		    $increment=$repetition;
1.89      albertel  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') {
1.58      sakharuk  285: 	
1.89      albertel  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];
1.117     albertel  290: 	    my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
                    291: 	    push (@{ $Apache::lonhomework::analyze{"$part_id.incorrect"} }, @incorrect);
1.89      albertel  292: 	}
                    293: 	&Apache::response::setup_params($$tagstack[-1]);
                    294: 	my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
1.58      sakharuk  295: 	my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
                    296: 	my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval);
1.89      albertel  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];
1.90      albertel  304: 	    my $fmt=$formats[0];
                    305: 	    if (@formats && $#formats) {$fmt=$formats[$i];}
1.89      albertel  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: 	    }
1.102     albertel  314: 	    if ($fmt && $$tagstack[-1] eq 'numericalresponse') {
1.112     albertel  315: 		$fmt=~s/e/E/g;
1.89      albertel  316: 		$ans = sprintf('%.'.$fmt,$ans);
                    317: 		if ($high) {
1.90      albertel  318: 		    $high=sprintf('%.'.$fmt,$high);
                    319: 		    $low =sprintf('%.'.$fmt,$low);
1.62      sakharuk  320: 		}
1.60      sakharuk  321: 	    }
1.89      albertel  322: 	    if ($target eq 'answer') {
1.102     albertel  323: 		if ($high && $$tagstack[-1] eq 'numericalresponse') { $ans.=' ['.$low.','.$high.']'; }
                    324: 		if ($sighigh && $$tagstack[-1] eq 'numericalresponse') {
1.101     albertel  325: 		    if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.106     sakharuk  326: 			$ans.= " Sig $siglow - $sighigh";
1.98      sakharuk  327: 		    } else {
1.101     albertel  328: 			$ans.= " Sig <i>$siglow - $sighigh</i>";
1.98      sakharuk  329: 		    }
                    330: 		}
1.89      albertel  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);
1.58      sakharuk  337: 		}
1.79      sakharuk  338: 	    }
1.36      albertel  339: 	}
1.102     albertel  340: 	if (defined($unit) and ($unit ne '') and
                    341: 	    $$tagstack[-1] eq 'numericalresponse') {
1.89      albertel  342: 	    if ($target eq 'answer') {
1.101     albertel  343: 		if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.98      sakharuk  344: 		    $result.=&Apache::response::answer_part($$tagstack[-1],
1.106     sakharuk  345: 							    " Unit: $unit ");
1.98      sakharuk  346: 		} else {
                    347: 		    $result.=&Apache::response::answer_part($$tagstack[-1],
                    348: 							    "Unit: <b>$unit</b>");
                    349: 		}
1.89      albertel  350: 	    } elsif ($target eq 'analyze') {
1.103     albertel  351: 		push (@{ $Apache::lonhomework::analyze{"$part_id.unit"} }, $unit);
1.89      albertel  352: 	    }
                    353: 	}
1.103     albertel  354: 	if ($type || $$tagstack[-1] eq 'stringresponse') {
1.89      albertel  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') {
1.101     albertel  366: 		if ($ENV{'form.answer_output_mode'} eq 'tex') {
                    367: 		    $result.=&Apache::response::answer_part($$tagstack[-1],
1.108     sakharuk  368: 							  "$string");
1.101     albertel  369: 		} else {
                    370: 		    $result.=&Apache::response::answer_part($$tagstack[-1],
                    371: 							    "<b>$string</b>");
                    372: 		}
1.89      albertel  373: 	    } elsif ($target eq 'analyze') {
1.103     albertel  374: 		push (@{ $Apache::lonhomework::analyze{"$part_id.str_type"} },
1.89      albertel  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: 	}
1.69      sakharuk  385:     }
1.120   ! sakharuk  386:     if ($target eq 'grade' || $target eq 'web' || 
1.90      albertel  387: 	$target eq 'tex' || $target eq 'analyze') {
                    388: 	&Apache::lonxml::increment_counter($increment);
                    389:     }
1.89      albertel  390:     &Apache::response::end_response;
                    391:     return $result;
1.90      albertel  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 '') {
1.109     albertel  418: 	$format=~s/e/E/g;
1.90      albertel  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*)$/) {
1.113     sakharuk  424: 	    $format = '3e';
1.90      albertel  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 {
1.117     albertel  445:     my ($number_of_bubbles,$target,$answer,$format,$incorrect) =@_;
1.91      albertel  446:     my @bubble_values = ();
1.119     albertel  447:     &Apache::lonxml::debug("answer is $answer incorrect is $incorrect");
                    448:     my @oldseed=&Math::Random::random_get_seed();
1.117     albertel  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));
1.119     albertel  458: 	    &Math::Random::random_set_seed(@oldseed);
1.117     albertel  459: 	    return @bubble_values;
                    460: 	}
                    461: 	#FIXME what to do when not enough incorrects specified?
                    462:     }
1.90      albertel  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++) {
1.91      albertel  471: 	$bubble_values[$ind] = $answer*($factor**($power-$powers[$#powers-$ind]));
                    472: 	$bubble_values[$ind] = &format_number($bubble_values[$ind],
                    473: 					       $format,$target);
                    474: 
1.90      albertel  475:     }
1.119     albertel  476:     &Math::Random::random_set_seed(@oldseed);
1.91      albertel  477:     return @bubble_values;
1.51      albertel  478: }
                    479: 
                    480: sub get_tolrange {
1.89      albertel  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);
1.52      albertel  493: }
                    494: 
                    495: sub get_sigrange {
1.89      albertel  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; }
1.52      albertel  510:     }
1.89      albertel  511:     return ($sig_ubound,$sig_lbound);
1.34      albertel  512: }
                    513: 
                    514: sub start_stringresponse {
1.89      albertel  515:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    516:     my $result;
                    517:     if ($target eq 'meta') {
1.104     bowersj2  518: 	&Apache::response::start_response($parstack,$safeeval);
1.89      albertel  519: 	$result=&Apache::response::meta_package_write('stringresponse');
1.104     bowersj2  520: 	&Apache::response::end_response();
1.89      albertel  521:     } else {
                    522: 	$result.=&start_numericalresponse(@_);
                    523:     }
                    524:     return $result;
1.34      albertel  525: }
                    526: 
                    527: sub end_stringresponse {
1.89      albertel  528:     return end_numericalresponse(@_);
1.44      albertel  529: }
                    530: 
                    531: sub start_formularesponse {
1.89      albertel  532:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    533:     my $result;
                    534:     if ($target eq 'meta') {
1.104     bowersj2  535: 	&Apache::response::start_response($parstack,$safeeval);
1.89      albertel  536: 	$result=&Apache::response::meta_package_write('formularesponse');
1.104     bowersj2  537: 	&Apache::response::end_response();
1.89      albertel  538:     } else {
                    539: 	$result.=&start_numericalresponse(@_);
                    540:     }
                    541:     return $result;
1.44      albertel  542: }
                    543: 
                    544: sub end_formularesponse {
1.89      albertel  545:     return end_numericalresponse(@_);
1.3       albertel  546: }
                    547: 
1.1       albertel  548: 1;
1.3       albertel  549: __END__
1.89      albertel  550: 

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