File:  [LON-CAPA] / loncom / homework / caparesponse / caparesponse.pm
Revision 1.125: download - view: text, annotated - select for diffs
Thu Dec 11 23:39:54 2003 UTC (20 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- remove use of $$tagstack[-1] for corrupt problems it can cause ISE BUG#2487

    1: # The LearningOnline Network with CAPA
    2: # caparesponse definition
    3: #
    4: # $Id: caparesponse.pm,v 1.125 2003/12/11 23:39:54 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('Incorrect Answers:','incorrect',
   46: 					     $token);
   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 'formularesponse') {
   52: 	    $result.=&Apache::edit::text_arg('Sample Points:','samples',
   53: 					     $token,40).
   54: 	      &Apache::loncommon::help_open_topic('Formula_Response_Sampling');
   55: 	}
   56: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
   57:     } elsif ($target eq 'modified') {
   58: 	my $constructtag;
   59: 	if ($token->[1] eq 'numericalresponse') {
   60: 	    $constructtag=&Apache::edit::get_new_args($token,$parstack,
   61: 						      $safeeval,'answer',
   62: 						      'incorrect','unit',
   63: 						      'format');
   64: 	} elsif ($token->[1] eq 'formularesponse') {
   65: 	    $constructtag=&Apache::edit::get_new_args($token,$parstack,
   66: 						      $safeeval,'answer',
   67: 						      'samples');
   68: 	}
   69: 	if ($constructtag) {
   70: 	    $result = &Apache::edit::rebuild_tag($token);
   71: 	    $result.=&Apache::edit::handle_insert();
   72: 	}
   73:     } elsif ($target eq 'meta') {
   74: 	$result=&Apache::response::meta_package_write('numericalresponse');
   75:     } elsif ($target eq 'answer' || $target eq 'grade') {
   76: 	&Apache::response::reset_params();
   77:     } elsif ($target eq 'web') {
   78: 	my $partid = $Apache::inputtags::part;
   79: 	my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
   80: 	&Apache::lonxml::debug("Got unit $hideunit for $partid $id");
   81: 	#no way to enter units, with radio buttons
   82: 	if (lc($hideunit) eq "yes") {
   83: 	    my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
   84: 						    $safeeval);
   85: 	    if ($unit =~ /\S/) { $result.=" (in $unit) "; }
   86: 	}
   87:     }
   88:     return $result;
   89: }
   90: 
   91: sub end_numericalresponse {
   92:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   93:     my $increment=1;
   94:     my $result = '';
   95:     if (!$Apache::lonxml::default_homework_loaded) {
   96: 	&Apache::lonxml::default_homework_load($safeeval);
   97:     }
   98:     my $tag;
   99:     if (scalar(@$tagstack)) { $tag=$$tagstack[-1]; }
  100:     if ( $target eq 'grade' && defined($ENV{'form.submitted'})) {
  101: 	&Apache::response::setup_params($tag);
  102: 	$safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
  103: 	my $partid = $Apache::inputtags::part;
  104: 	my $id = $Apache::inputtags::response['-1'];
  105: 	if ($Apache::lonhomework::type eq 'exam' && 
  106: 	    $tag eq 'formularesponse') {
  107: 	    $increment=&Apache::response::scored_response($partid,$id);
  108: 	} else {
  109: 	    my $response = &Apache::response::getresponse();
  110: 	    if ( $response =~ /[^\s]/) {
  111: 		my $ad;
  112: 		my %previous = &Apache::response::check_for_previous($response,$partid,$id);
  113: 		&Apache::lonxml::debug("submitted a $response<br>\n");
  114: 		&Apache::lonxml::debug($$parstack[-1] . "\n<br>");
  115: 		
  116: 		if ($ENV{'form.submitted'} eq 'scantron') {
  117: 		    my $number_of_bubbles = &Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.numbubbles');
  118: 		    if (!$number_of_bubbles) { $number_of_bubbles=8; }
  119: 		    my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
  120: 		    my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
  121: 		    my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
  122: 		    my @values=&make_numerical_bubbles($number_of_bubbles,$target,$answers[0],$formats[0],\@incorrect);
  123: 		    $response=$values[$response];
  124: 		} else {
  125: 		    $response =~ s/\\/\\\\/g;
  126: 		    $response =~ s/\'/\\\'/g;
  127: 		}
  128: 		$Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
  129: 		&Apache::lonxml::debug("current $response");
  130: 		my $expression="&caparesponse_check_list('".$response."','".
  131: 		    $$parstack[-1];
  132: 		my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
  133: 
  134: 		foreach my $key (keys(%Apache::inputtags::params)) {
  135: 		    $expression.= ';my $'. #'
  136: 			$key.'="'.$Apache::inputtags::params{$key}.'"';
  137: 		}
  138: 
  139: 		#no way to enter units, with radio buttons
  140: 		if ($Apache::lonhomework::type eq 'exam' ||
  141: 		    lc($hideunit) eq "yes") {
  142: 		    $expression.=';my $unit=undef;';
  143: 		}
  144: 		#sig fig don't make much sense either
  145: 		if (($Apache::lonhomework::type eq 'exam' ||
  146: 		     $ENV{'form.submitted'} eq 'scantron') &&
  147: 		    $tag eq 'numericalresponse') {
  148: 		    $expression.=';my $sig=undef;';
  149: 		}
  150: 
  151: 		if ($tag eq 'formularesponse') {
  152: 		    $expression.=';my $type="fml";';
  153: 		} elsif ($tag eq 'numericalresponse') {
  154: 		    $expression.=';my $type="float";';
  155: 		}
  156: 		$expression.="');";
  157: 		my @answer=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
  158: 		&Apache::lonxml::debug('answer is'.join(':',@answer));
  159: 		@{$safeeval->varglob('CAPARESPONSE_CHECK_LIST_answer')}=@answer;
  160: 		
  161: 		$result = &Apache::run::run($expression,$safeeval);
  162: 		my ($awards) = split /:/ , $result;
  163: 		($ad) = &Apache::inputtags::finalizeawards(split /,/ , $awards);
  164: 		&Apache::lonxml::debug("$expression");
  165: 		&Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
  166: 		&Apache::response::handle_previous(\%previous,$ad);
  167: 		$Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
  168: 		$result='';
  169: 	    }
  170: 	}
  171:     } elsif ($target eq 'web' || $target eq 'tex') {
  172: 	my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
  173: 						     $safeeval);
  174: 	my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
  175: 	my $status = $Apache::inputtags::status['-1'];
  176: 	if (  &Apache::response::show_answer() ) {
  177: 	    my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,
  178: 							 $safeeval);
  179: 	    my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
  180: 						    $safeeval);
  181: 	    if ($target eq 'web') {
  182: 		$result="<br />The correct answer is ";
  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: 		    $format=~s/e/E/g;
  195: 		    &Apache::lonxml::debug("formatting with :$format: answer :$answer:");
  196: 		    $formatted=sprintf('%.'.$format,$answer).',';
  197: 		} else {
  198: 		    &Apache::lonxml::debug("no format answer :$answer:");
  199: 		    $formatted="$answer,";
  200: 		}
  201: 		if ($target eq 'tex') {
  202: 		    $formatted='';
  203: 		    #$formatted=&Apache::lonxml::latex_special_symbols($formatted);
  204: 		}
  205: 		$result.=$formatted;
  206: 	    }
  207: 	    chop $result;
  208: 	    if ($target eq 'web') {
  209: 		$result.=" $unit.<br />";
  210: 	    }
  211: 	}
  212: 	if ($Apache::lonhomework::type eq 'exam') {
  213: 	    my $partid=$Apache::inputtags::part;
  214: 	    my $id=$Apache::inputtags::response[-1];
  215: 	    my $number_of_bubbles = &Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.numbubbles');
  216: 	    if (!$number_of_bubbles) { $number_of_bubbles=8; }
  217: 	    my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,
  218: 							 $safeeval);
  219: 	    my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
  220: 						    $safeeval);
  221: 	    my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
  222: 	    my @bubble_values=&make_numerical_bubbles($number_of_bubbles,
  223: 						      $target,$answers[0],
  224: 						      $formats[0],\@incorrect);
  225: 	    my @alphabet=('A'..'Z');
  226: 	    if ($target eq 'web') {
  227: 		if ($tag eq 'numericalresponse') {
  228: 		    if ($unit=~/\S/) {$result.=' (in '.$unit.')<br /><br />';}
  229: 		    $result.= '<table border="1"><tr>';
  230: 		    my $previous=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.$id.submission"};
  231: 		    for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
  232: 			my $checked='';
  233: 			if ($previous eq $bubble_values[$ind]) {
  234: 			    $checked=" checked='on' ";
  235: 			}
  236: 			$result.='<td><input type="radio" name="HWVAL_'.$id.
  237: 			    '" value="'.$bubble_values[$ind].'" '.$checked
  238: 			    .' /><b>'.$alphabet[$ind].'</b>: '.
  239: 			    $bubble_values[$ind].'</td>';
  240: 		    }
  241: 		    $result.='</tr></table>';
  242: 		} elsif ($tag eq 'formularesponse') {
  243: 		    $result.= '<br /><br /><font color="red">
  244:                            <textarea name="HWVAL_'.$id.'" rows="4" cols="50">
  245:                            </textarea></font> <br /><br />';
  246: 		}
  247: 	    } elsif ($target eq 'tex') {
  248: 		if ((defined $unit) and ($unit=~/\S/) and ($Apache::lonhomework::type eq 'exam')) {
  249: 		    $result.=' \textit{(in} \verb|'.$unit.'|\textit{)} ';
  250: 		}
  251: 		if ($tag eq 'numericalresponse') {
  252: 		    my ($celllength,$number_of_tables,@table_range)=
  253: 			&get_table_sizes($formats[0],$number_of_bubbles);
  254: 		    my $j=0;
  255: 		    my $cou=0;
  256: 		    $result.='\vskip -1 mm \noindent \begin{enumerate}\item[\textbf{'.$Apache::lonxml::counter.'}.]';
  257: 		    for (my $i=0;$i<$number_of_tables;$i++) {
  258: 			$result.='\vskip -1 mm \noindent \begin{tabular}{';
  259: 			for (my $ind=0;$ind<$table_range[$j];$ind++) {
  260: 			    $result.='lp{'.$celllength.' mm}';
  261: 			}
  262: 			$result.='}';
  263: 			for (my $ind=$cou;$ind<$cou+$table_range[$j];$ind++) {
  264: 			    $result.='\hskip -3 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -2 mm & {\small '.$bubble_values[$ind].'} ';
  265: 			    if ($ind != $cou+$table_range[$j]-1) {$result.=' & ';}
  266: 			}
  267: 			$cou += $table_range[$j];
  268: 			$j++;
  269: 			$result.='\\\\\end{tabular}\vskip 0 mm ';
  270: 		    }
  271: 		    $result.='\end{enumerate}';
  272: 		} else {
  273: 		    $result.='\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
  274: 		    my $repetition = &Apache::response::repetition();
  275: 		    $result.='\begin{enumerate}';
  276: 		    for (my $i=0;$i<$repetition;$i++) {
  277: 			$result.='\item[\textbf{'.($Apache::lonxml::counter+$i).'}.]\textit{Leave blank on scoring form}\vskip 0 mm';
  278: 		    }
  279: 		    $increment=$repetition;
  280: 		    $result.= '\end{enumerate}';
  281: 		}
  282: 	    }
  283: 	}
  284:     } elsif ($target eq 'edit') {
  285: 	$result.='</td></tr>'.&Apache::edit::end_table;
  286:     } elsif ($target eq 'answer' || $target eq 'analyze') {
  287: 	
  288: 	my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
  289: 	if ($target eq 'analyze') {
  290: 	    push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
  291: 	    $Apache::lonhomework::analyze{"$part_id.type"} = $tag;
  292: 	    my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
  293: 	    push (@{ $Apache::lonhomework::analyze{"$part_id.incorrect"} }, @incorrect);
  294: 	}
  295: 	if (scalar(@$tagstack)) {
  296: 	    &Apache::response::setup_params($tag);
  297: 	}
  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($tag);
  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 && $tag eq 'numericalresponse') {
  319: 		$fmt=~s/e/E/g;
  320: 		$ans = sprintf('%.'.$fmt,$ans);
  321: 		if ($high) {
  322: 		    $high=sprintf('%.'.$fmt,$high);
  323: 		    $low =sprintf('%.'.$fmt,$low);
  324: 		}
  325: 	    }
  326: 	    if ($target eq 'answer') {
  327: 		if ($high && $tag eq 'numericalresponse') { $ans.=' ['.$low.','.$high.']'; }
  328: 		if ($sighigh && $tag eq 'numericalresponse') {
  329: 		    if ($ENV{'form.answer_output_mode'} eq 'tex') {
  330: 			$ans.= " Sig $siglow - $sighigh";
  331: 		    } else {
  332: 			$ans.= " Sig <i>$siglow - $sighigh</i>";
  333: 		    }
  334: 		}
  335: 		$result.=&Apache::response::answer_part($tag,$ans);
  336: 	    } elsif ($target eq 'analyze') {
  337: 		push (@{ $Apache::lonhomework::analyze{"$part_id.answer"} }, $ans);
  338: 		if ($high) {
  339: 		    push (@{ $Apache::lonhomework::analyze{"$part_id.ans_high"} }, $high);
  340: 		    push (@{ $Apache::lonhomework::analyze{"$part_id.ans_low"} }, $low);
  341: 		}
  342: 	    }
  343: 	}
  344: 	if (defined($unit) and ($unit ne '') and
  345: 	    $tag eq 'numericalresponse') {
  346: 	    if ($target eq 'answer') {
  347: 		if ($ENV{'form.answer_output_mode'} eq 'tex') {
  348: 		    $result.=&Apache::response::answer_part($tag,
  349: 							    " Unit: $unit ");
  350: 		} else {
  351: 		    $result.=&Apache::response::answer_part($tag,
  352: 							    "Unit: <b>$unit</b>");
  353: 		}
  354: 	    } elsif ($target eq 'analyze') {
  355: 		push (@{ $Apache::lonhomework::analyze{"$part_id.unit"} }, $unit);
  356: 	    }
  357: 	}
  358: 	if ($tag eq 'formularesponse' && $target eq 'answer') {
  359: 	    my $samples=&Apache::lonxml::get_param('samples',$parstack,$safeeval);
  360: 	    $result.=&Apache::response::answer_part($tag,$samples);
  361: 	}
  362: 	if ($target eq 'answer') {
  363: 	    $result.=&Apache::response::answer_footer($tag);
  364: 	}
  365:     }
  366:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
  367: 	$target eq 'tex' || $target eq 'analyze') {
  368: 	&Apache::lonxml::increment_counter($increment);
  369:     }
  370:     &Apache::response::end_response;
  371:     return $result;
  372: }
  373: 
  374: sub get_table_sizes {
  375:     my ($format,$number_of_bubbles)=@_;
  376:     my $max_val = 0;
  377:     if ($format=~m/^(\d+)E([^\d]*)(\d*)$/) {
  378: 	$max_val=$1+$2+4;
  379:     } else {
  380: 	$max_val=4;
  381:     }
  382:     $max_val = int(0.9*$ENV{'form.textwidth'}/(($max_val+6)*2));
  383:     my $celllength = 0.9*$ENV{'form.textwidth'}/$max_val-10;
  384:     my @table_range = ();
  385:     my $number_of_tables = int($number_of_bubbles/$max_val);
  386:     for (my $i=0;$i<$number_of_tables;$i++) {push @table_range,$max_val;}
  387:     if ($number_of_bubbles % $max_val != 0) {
  388: 	$number_of_tables++;
  389: 	push @table_range,($number_of_bubbles % $max_val);
  390:     }
  391:     return ($celllength,$number_of_tables,@table_range);
  392: }
  393: 
  394: sub format_number {
  395:     my ($number,$format,$target)=@_;
  396:     my $ans;
  397:     if ($format ne '') {
  398: 	$format=~s/e/E/g;
  399: 	$ans = sprintf('%.'.$format,$number);
  400:     } else {
  401: 	my $format = '';
  402: 	#What is the number? (integer,decimal,floating point)
  403: 	if ($number=~/^(\d*\.?\d*)(E|e)(\d*)$/) {
  404: 	    $format = '3e';
  405: 	} elsif ($number=~/^(\d*)\.(\d*)$/) {
  406: 	    $format = '4f';
  407: 	} elsif ($number=~/^(\d*)$/) {
  408: 	    $format = 'd';
  409: 	}
  410: 	$ans = sprintf('%.'.$format,$number);
  411:     }
  412:     if ($target eq 'tex') {
  413: 	if ($ans =~ m/([0-9\.\-\+]+)E([0-9\-\+]+)/ ) {
  414: 	    my $number = $1;
  415: 	    my $power = $2;
  416: 	    $power=~s/^\+//;
  417: 	    $power=~s/^(-?)0+(\d+)/$1$2/;
  418: 	    $ans=$number.'$\times 10^{'.$power.'}$'; #'stupidemacs
  419: 	}
  420:     }
  421:     return $ans;
  422: }
  423: 
  424: sub make_numerical_bubbles {
  425:     my ($number_of_bubbles,$target,$answer,$format,$incorrect) =@_;
  426:     my @bubble_values = ();
  427:     &Apache::lonxml::debug("answer is $answer incorrect is $incorrect");
  428:     my @oldseed=&Math::Random::random_get_seed();
  429:     if (defined($incorrect) && ref($incorrect)) {
  430: 	&Apache::lonxml::debug("inside ".(scalar(@$incorrect)+1 gt $number_of_bubbles));
  431: 	if (scalar(@$incorrect)+1 >= $number_of_bubbles) {
  432: 	    &Apache::lonxml::debug("inside ".(scalar(@$incorrect)+1).":$number_of_bubbles");
  433: 	    &Apache::response::setrandomnumber();
  434: 	    my @rand_inc=&Math::Random::random_permutation(@$incorrect);
  435: 	    @bubble_values=@rand_inc[0..($number_of_bubbles-2)];
  436: 	    @bubble_values=sort {$a <=> $b} (@bubble_values,$answer);
  437: 	    &Apache::lonxml::debug("Answer was :$answer: returning :".$#bubble_values.": whih are :".join(':',@bubble_values));
  438: 	    &Math::Random::random_set_seed(@oldseed);
  439: 	    return @bubble_values;
  440: 	}
  441: 	#FIXME what to do when not enough incorrects specified?
  442:     }
  443:     my @factors = (1.13,1.17,1.25,1.33,1.45); #default values of factors
  444:     my @powers = (1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0); #default values for powers
  445:     &Apache::response::setrandomnumber();
  446:     my $ind=&Math::Random::random_uniform_integer(1,0,$#powers);
  447:     my $power = $powers[$ind];
  448:     $ind=&Math::Random::random_uniform_integer(1,0,$#factors);
  449:     my $factor = $factors[$ind];
  450:     for ($ind=0;$ind<$number_of_bubbles;$ind++) {
  451: 	$bubble_values[$ind] = $answer*($factor**($power-$powers[$#powers-$ind]));
  452: 	$bubble_values[$ind] = &format_number($bubble_values[$ind],
  453: 					       $format,$target);
  454: 
  455:     }
  456:     &Math::Random::random_set_seed(@oldseed);
  457:     return @bubble_values;
  458: }
  459: 
  460: sub get_tolrange {
  461:     my ($ans,$tol)=@_;
  462:     my ($high,$low);
  463:     if ($tol =~ /%$/) {
  464: 	chop($tol);
  465: 	my $change=$ans*($tol/100.0);
  466: 	$high=$ans+$change;
  467: 	$low=$ans-$change;
  468:     } else {
  469: 	$high=$ans+$tol;
  470: 	$low=$ans-$tol;
  471:     }
  472:     return ($high,$low);
  473: }
  474: 
  475: sub get_sigrange {
  476:     my ($sig)=@_;
  477:     &Apache::lonxml::debug("Got a sig of :$sig:");
  478:     my $sig_lbound;
  479:     my $sig_ubound;
  480:     if ($sig eq '') {
  481: 	$sig_lbound = 0; #SIG_LB_DEFAULT
  482: 	$sig_ubound =15; #SIG_UB_DEFAULT
  483:     } else {
  484: 	($sig_lbound,$sig_ubound) = split(/,/,$sig);
  485: 	if (!$sig_lbound) {
  486: 	    $sig_lbound = 0; #SIG_LB_DEFAULT
  487: 	    $sig_ubound =15; #SIG_UB_DEFAULT
  488: 	}
  489: 	if (!$sig_ubound) { $sig_ubound=$sig_lbound; }
  490:     }
  491:     return ($sig_ubound,$sig_lbound);
  492: }
  493: 
  494: sub start_stringresponse {
  495:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  496:     my $result;
  497:     my $id = &Apache::response::start_response($parstack,$safeeval);
  498:     if ($target eq 'meta') {
  499: 	&Apache::response::start_response($parstack,$safeeval);
  500: 	$result=&Apache::response::meta_package_write('stringresponse');
  501: 	&Apache::response::end_response();
  502:     } elsif ($target eq 'edit') {
  503: 	$result.=&Apache::edit::tag_start($target,$token);
  504: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
  505: 	$result.=&Apache::edit::select_arg('Type:','type',
  506: 			 [['cs','Case Sensitive'],['ci','Case Insensitive'],
  507: 			  ['mc','Case Insensitive, Any Order'],
  508: 			  ['re','Regular Expression']],$token);
  509: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  510:     } elsif ($target eq 'modified') {
  511: 	my $constructtag;
  512: 	$constructtag=&Apache::edit::get_new_args($token,$parstack,
  513: 						  $safeeval,'answer',
  514: 						  'type');
  515: 	if ($constructtag) {
  516: 	    $result = &Apache::edit::rebuild_tag($token);
  517: 	    $result.=&Apache::edit::handle_insert();
  518: 	}
  519:     } elsif ($target eq 'answer' || $target eq 'grade') {
  520: 	&Apache::response::reset_params();
  521:     }
  522:     return $result;
  523: }
  524: 
  525: sub end_stringresponse {
  526:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  527:     my $increment=1;
  528:     my $result = '';
  529:     my $part=$Apache::inputtags::part;
  530:     my $id=$Apache::inputtags::response[-1];
  531:     my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
  532:     my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
  533:     &Apache::lonxml::debug("current $answer ".$token->[2]);
  534:     if (!$Apache::lonxml::default_homework_loaded) {
  535: 	&Apache::lonxml::default_homework_load($safeeval);
  536:     }
  537:     if ( $target eq 'grade' && defined($ENV{'form.submitted'})) {
  538: 	&Apache::response::setup_params('stringresponse');
  539: 	$safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
  540: 	if ($Apache::lonhomework::type eq 'exam' ||
  541: 	    $ENV{'form.submitted'} eq 'scantron') {
  542: 	    $increment=&Apache::response::scored_response($part,$id);
  543: 	} else {
  544: 	    my $response = &Apache::response::getresponse();
  545: 	    if ( $response =~ /[^\s]/) {
  546: 		my %previous = &Apache::response::check_for_previous($response,
  547: 								    $part,$id);
  548: 		&Apache::lonxml::debug("submitted a $response<br>\n");
  549: 		&Apache::lonxml::debug($$parstack[-1] . "\n<br>");
  550: 		
  551: 		$Apache::lonhomework::results{"resource.$part.$id.submission"}=
  552: 		    $response;
  553: 		my $ad;
  554: 		if ($type eq 're' ) { 
  555: 		    # if the RE wasn't in a var it likely got munged,
  556:                     # thus grab it from the var directly
  557: #		    my $testans=$token->[2]->{'answer'};
  558: #		    if ($testans !~ m/^\s*\$/) {
  559: #			$answer=$token->[2]->{'answer'};
  560: #		    }
  561: 		    ${$safeeval->varglob('LONCAPA_INTERNAL_response')}=
  562: 			$response;
  563: 		    $result = &Apache::run::run('return $LONCAPA_INTERNAL_response=~m'.$answer,$safeeval);
  564: 		    &Apache::lonxml::debug("current $response");
  565: 		    &Apache::lonxml::debug("current $answer");
  566: 		    $ad = ($result) ? 'APPROX_ANS' : 'INCORRECT';
  567: 		} else {
  568: 		    $response =~ s/\\/\\\\/g;
  569: 		    $response =~ s/\'/\\\'/g;
  570: 		    &Apache::lonxml::debug("current $response");
  571: 		    my $expression="&caparesponse_check_list('".$response."','".
  572: 			$$parstack[-1];
  573: 		    foreach my $key (keys(%Apache::inputtags::params)) {
  574: 			$expression.= ';my $'. #'
  575: 			    $key.'="'.$Apache::inputtags::params{$key}.'"';
  576: 		    }
  577: 		    $expression.="');";
  578: 		    &Apache::lonxml::debug('answer is'.join(':',$answer));
  579: 		    @{$safeeval->varglob('CAPARESPONSE_CHECK_LIST_answer')}=($answer);
  580: 		    $result = &Apache::run::run($expression,$safeeval);
  581: 		    my ($awards) = split /:/ , $result;
  582: 		    ($ad) = &Apache::inputtags::finalizeawards(split /,/ , $awards);
  583: 		    &Apache::lonxml::debug("$expression");
  584: 		    &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
  585: 		}
  586: 		&Apache::response::handle_previous(\%previous,$ad);
  587: 		$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  588: 	    }
  589: 	}
  590:     } elsif ($target eq 'web' || $target eq 'tex') {
  591: 	my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
  592: 	my $status = $Apache::inputtags::status['-1'];
  593: 	if (  &Apache::response::show_answer() ) {
  594: 	    if ($target eq 'web') {
  595: 		$result="<br />The correct answer is ".$answer;
  596: #		    join(', ',@answers).".<br />";
  597: 	    }
  598: 	}
  599: 	if ($Apache::lonhomework::type eq 'exam' && $target eq 'tex') {
  600: 	    $result.='\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
  601: 	    $increment = &Apache::response::repetition();
  602: 	    $result.='\begin{enumerate}';
  603: 	    for (my $i=0;$i<$increment;$i++) {
  604: 		$result.='\item[\textbf{'.($Apache::lonxml::counter+$i).
  605: 		    '}.]\textit{Leave blank on scoring form}\vskip 0 mm';
  606: 	    }
  607: 	    $result.= '\end{enumerate}';
  608: 	}
  609:     } elsif ($target eq 'answer' || $target eq 'analyze') {
  610: 	if ($target eq 'analyze') {
  611: 	    push (@{ $Apache::lonhomework::analyze{"parts"} },"$part.$id");
  612: 	    $Apache::lonhomework::analyze{"$part.$id.type"} = 'stringresponse';
  613: 	}
  614: 	&Apache::response::setup_params('stringresponse');
  615: 	if ($target eq 'answer') {
  616: 	    $result.=&Apache::response::answer_header('stringresponse');
  617: 	}
  618: #	foreach my $ans (@answers) {
  619: 	    if ($target eq 'answer') {
  620: 		$result.=&Apache::response::answer_part('stringresponse',$answer);
  621: 	    } elsif ($target eq 'analyze') {
  622: 		push (@{ $Apache::lonhomework::analyze{"$part.$id.answer"} },
  623: 		      $answer);
  624: 	    }
  625: #	}
  626: 	my $string='Case Insensitive';
  627: 	if ($type eq 'mc') {
  628: 	    $string='Multiple Choice';
  629: 	} elsif ($type eq 'cs') {
  630: 	    $string='Case Sensitive';
  631: 	} elsif ($type eq 'ci') {
  632: 	    $string='Case Insensitive';
  633: 	} elsif ($type eq 're') {
  634: 	    $string='Regular Expression';
  635: 	}
  636: 	if ($target eq 'answer') {
  637: 	    if ($ENV{'form.answer_output_mode'} eq 'tex') {
  638: 		$result.=&Apache::response::answer_part('stringresponse',
  639: 							"$string");
  640: 	    } else {
  641: 		$result.=&Apache::response::answer_part('stringresponse',
  642: 							"<b>$string</b>");
  643: 	    }
  644: 	} elsif ($target eq 'analyze') {
  645: 	    push (@{$Apache::lonhomework::analyze{"$part.$id.str_type"}},
  646: 		  $type);
  647: 	}
  648: 	if ($target eq 'answer') {
  649: 	    $result.=&Apache::response::answer_footer('stringresponse');
  650: 	}
  651:     } elsif ($target eq 'edit') {
  652: 	$result.='</td></tr>'.&Apache::edit::end_table;
  653:     }
  654:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
  655: 	$target eq 'tex' || $target eq 'analyze') {
  656: 	&Apache::lonxml::increment_counter($increment);
  657:     }
  658:     &Apache::response::end_response;
  659:     return $result;
  660: }
  661: 
  662: sub start_formularesponse {
  663:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  664:     my $result;
  665:     if ($target eq 'meta') {
  666: 	&Apache::response::start_response($parstack,$safeeval);
  667: 	$result=&Apache::response::meta_package_write('formularesponse');
  668: 	&Apache::response::end_response();
  669:     } else {
  670: 	$result.=&start_numericalresponse(@_);
  671:     }
  672:     return $result;
  673: }
  674: 
  675: sub end_formularesponse {
  676:     return end_numericalresponse(@_);
  677: }
  678: 
  679: 1;
  680: __END__
  681: 

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