File:  [LON-CAPA] / loncom / homework / caparesponse / caparesponse.pm
Revision 1.128: download - view: text, annotated - select for diffs
Wed Feb 11 21:10:45 2004 UTC (20 years, 3 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
Algorithm for calculation the width of the cell for exam bubbles (tex output) is changed. I very want to have any responces from people used exams.

    1: # The LearningOnline Network with CAPA
    2: # caparesponse definition
    3: #
    4: # $Id: caparesponse.pm,v 1.128 2004/02/11 21:10:45 sakharuk 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 ($Apache::inputtags::params{'numbubbles'}) {
  217: 		$number_of_bubbles = $Apache::inputtags::params{'numbubbles'};
  218: 	    }
  219: 	    if (!$number_of_bubbles) { $number_of_bubbles=8; }
  220: 	    
  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 (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
  226: 	    my @bubble_values=&make_numerical_bubbles($number_of_bubbles,
  227: 						      $target,$answers[0],
  228: 						      $formats[0],\@incorrect);
  229: 	    my @alphabet=('A'..'Z');
  230: 	    if ($target eq 'web') {
  231: 		if ($tag 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 ($tag 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 ($tag eq 'numericalresponse') {
  256: 		    my ($celllength,$number_of_tables,@table_range)=
  257: 			&get_table_sizes($number_of_bubbles,\@bubble_values);
  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.='p{3 mm}p{'.$celllength.' mm}';
  265: 			}
  266: 			$result.='}';
  267: 			for (my $ind=$cou;$ind<$cou+$table_range[$j];$ind++) {
  268: 			    $result.='\hskip -4 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$ & \hskip -3 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"} = $tag;
  296: 	    my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
  297: 	    push (@{ $Apache::lonhomework::analyze{"$part_id.incorrect"} }, @incorrect);
  298: 	}
  299: 	if (scalar(@$tagstack)) {
  300: 	    &Apache::response::setup_params($tag);
  301: 	}
  302: 	my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
  303: 	my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
  304: 	my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval);
  305: 	my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
  306: 	
  307: 	if ($target eq 'answer') {
  308: 	    $result.=&Apache::response::answer_header($tag);
  309: 	}
  310: 	for(my $i=0;$i<=$#answers;$i++) {
  311: 	    my $ans=$answers[$i];
  312: 	    my $fmt=$formats[0];
  313: 	    if (@formats && $#formats) {$fmt=$formats[$i];}
  314: 	    my ($high,$low);
  315: 	    if ($Apache::inputtags::params{'tol'}) {
  316: 		($high,$low)=&get_tolrange($ans,$Apache::inputtags::params{'tol'});
  317: 	    }
  318: 	    my ($sighigh,$siglow);
  319: 	    if ($Apache::inputtags::params{'sig'}) {
  320: 		($sighigh,$siglow)=&get_sigrange($Apache::inputtags::params{'sig'});
  321: 	    }
  322: 	    if ($fmt && $tag eq 'numericalresponse') {
  323: 		$fmt=~s/e/E/g;
  324: 		$ans = sprintf('%.'.$fmt,$ans);
  325: 		if ($high) {
  326: 		    $high=sprintf('%.'.$fmt,$high);
  327: 		    $low =sprintf('%.'.$fmt,$low);
  328: 		}
  329: 	    }
  330: 	    if ($target eq 'answer') {
  331: 		if ($high && $tag eq 'numericalresponse') { $ans.=' ['.$low.','.$high.']'; }
  332: 		if ($sighigh && $tag eq 'numericalresponse') {
  333: 		    if ($ENV{'form.answer_output_mode'} eq 'tex') {
  334: 			$ans.= " Sig $siglow - $sighigh";
  335: 		    } else {
  336: 			$ans.= " Sig <i>$siglow - $sighigh</i>";
  337: 		    }
  338: 		}
  339: 		$result.=&Apache::response::answer_part($tag,$ans);
  340: 	    } elsif ($target eq 'analyze') {
  341: 		push (@{ $Apache::lonhomework::analyze{"$part_id.answer"} }, $ans);
  342: 		if ($high) {
  343: 		    push (@{ $Apache::lonhomework::analyze{"$part_id.ans_high"} }, $high);
  344: 		    push (@{ $Apache::lonhomework::analyze{"$part_id.ans_low"} }, $low);
  345: 		}
  346: 	    }
  347: 	}
  348: 	if (defined($unit) and ($unit ne '') and
  349: 	    $tag eq 'numericalresponse') {
  350: 	    if ($target eq 'answer') {
  351: 		if ($ENV{'form.answer_output_mode'} eq 'tex') {
  352: 		    $result.=&Apache::response::answer_part($tag,
  353: 							    " Unit: $unit ");
  354: 		} else {
  355: 		    $result.=&Apache::response::answer_part($tag,
  356: 							    "Unit: <b>$unit</b>");
  357: 		}
  358: 	    } elsif ($target eq 'analyze') {
  359: 		push (@{ $Apache::lonhomework::analyze{"$part_id.unit"} }, $unit);
  360: 	    }
  361: 	}
  362: 	if ($tag eq 'formularesponse' && $target eq 'answer') {
  363: 	    my $samples=&Apache::lonxml::get_param('samples',$parstack,$safeeval);
  364: 	    $result.=&Apache::response::answer_part($tag,$samples);
  365: 	}
  366: 	if ($target eq 'answer') {
  367: 	    $result.=&Apache::response::answer_footer($tag);
  368: 	}
  369:     }
  370:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
  371: 	$target eq 'tex' || $target eq 'analyze') {
  372: 	&Apache::lonxml::increment_counter($increment);
  373:     }
  374:     &Apache::response::end_response;
  375:     return $result;
  376: }
  377: 
  378: sub get_table_sizes {
  379:     my ($number_of_bubbles,$rbubble_values)=@_;
  380:     my $scale=2; #mm for one digit
  381:     my $cell_width=0;
  382:     foreach my $member (@$rbubble_values) {
  383: 	my $cell_width_real=0;
  384: 	if ($member=~/(\d*)\.?(\d*)(E|e)(\+|-)?(\d*)/) {
  385: 	    $cell_width_real=(length($1)+length($2)+length($5)+9)*$scale;
  386: 	} elsif ($member=~/(\d*)\.?(\d*)/) {
  387: 	    $cell_width_real=(length($1)+length($2)+2)*$scale;
  388: 	} else {
  389: 	    $cell_width_real=(length($member)+1)*$scale;
  390: 	}
  391: 	if ($cell_width_real>$cell_width) {$cell_width=$cell_width_real;}
  392:     }
  393:     $cell_width+=8;
  394:     $ENV{'form.textwidth'}=~/(\d+)/;
  395:     my $textwidth=$1;
  396:     my $bubbles_per_line=int($textwidth/$cell_width);
  397:     my $number_of_tables = int($number_of_bubbles/$bubbles_per_line);
  398:     my @table_range = ();
  399:     for (my $i=0;$i<$number_of_tables;$i++) {push @table_range,$bubbles_per_line;}
  400:     if ($number_of_bubbles % $bubbles_per_line) {
  401: 	$number_of_tables++;
  402: 	push @table_range,($number_of_bubbles % $bubbles_per_line);
  403:     }
  404:     $cell_width-=8;
  405:     return ($cell_width,$number_of_tables,@table_range);
  406: }
  407: 
  408: sub format_number {
  409:     my ($number,$format,$target)=@_;
  410:     my $ans;
  411:     if ($format ne '') {
  412: 	$format=~s/e/E/g;
  413: 	$ans = sprintf('%.'.$format,$number);
  414:     } else {
  415: 	my $format = '';
  416: 	#What is the number? (integer,decimal,floating point)
  417: 	if ($number=~/^(\d*\.?\d*)(E|e)(\d*)$/) {
  418: 	    $format = '3e';
  419: 	} elsif ($number=~/^(\d*)\.(\d*)$/) {
  420: 	    $format = '4f';
  421: 	} elsif ($number=~/^(\d*)$/) {
  422: 	    $format = 'd';
  423: 	}
  424: 	$ans = sprintf('%.'.$format,$number);
  425:     }
  426:     if ($target eq 'tex') {
  427: 	if ($ans =~ m/([0-9\.\-\+]+)E([0-9\-\+]+)/ ) {
  428: 	    my $number = $1;
  429: 	    my $power = $2;
  430: 	    $power=~s/^\+//;
  431: 	    $power=~s/^(-?)0+(\d+)/$1$2/;
  432: 	    $ans=$number.'$\times 10^{'.$power.'}$'; #'stupidemacs
  433: 	}
  434:     }
  435:     return $ans;
  436: }
  437: 
  438: sub make_numerical_bubbles {
  439:     my ($number_of_bubbles,$target,$answer,$format,$incorrect) =@_;
  440:     my @bubble_values = ();
  441:     &Apache::lonxml::debug("answer is $answer incorrect is $incorrect");
  442:     my @oldseed=&Math::Random::random_get_seed();
  443:     if (defined($incorrect) && ref($incorrect)) {
  444: 	&Apache::lonxml::debug("inside ".(scalar(@$incorrect)+1 gt $number_of_bubbles));
  445: 	if (defined($$incorrect[0]) &&
  446: 	    scalar(@$incorrect)+1 >= $number_of_bubbles) {
  447: 	    &Apache::lonxml::debug("inside ".(scalar(@$incorrect)+1).":$number_of_bubbles");
  448: 	    &Apache::response::setrandomnumber();
  449: 	    my @rand_inc=&Math::Random::random_permutation(@$incorrect);
  450: 	    @bubble_values=@rand_inc[0..($number_of_bubbles-2)];
  451: 	    @bubble_values=sort {$a <=> $b} (@bubble_values,$answer);
  452: 	    &Apache::lonxml::debug("Answer was :$answer: returning :".$#bubble_values.": whih are :".join(':',@bubble_values));
  453: 	    &Math::Random::random_set_seed(@oldseed);
  454: 	    return @bubble_values;
  455: 	}
  456: 	if (defined($$incorrect[0]) &&
  457: 	    scalar(@$incorrect)+1 < $number_of_bubbles) {
  458: 	    &Apache::lonxml::warning("Not enough incorrect answers were specified in the incorrect array, ignoring the specified incorrect answers and instead generating them.");
  459: 	}
  460:     }
  461:     my @factors = (1.13,1.17,1.25,1.33,1.45); #default values of factors
  462:     my @powers = (1..$number_of_bubbles);
  463:     &Apache::response::setrandomnumber();
  464:     my $ind=&Math::Random::random_uniform_integer(1,0,$#powers);
  465:     my $power = $powers[$ind];
  466:     $ind=&Math::Random::random_uniform_integer(1,0,$#factors);
  467:     my $factor = $factors[$ind];
  468:     for ($ind=0;$ind<$number_of_bubbles;$ind++) {
  469: 	$bubble_values[$ind] = $answer*($factor**($power-$powers[$#powers-$ind]));
  470: 	$bubble_values[$ind] = &format_number($bubble_values[$ind],
  471: 					       $format,$target);
  472: 
  473:     }
  474:     &Math::Random::random_set_seed(@oldseed);
  475:     return @bubble_values;
  476: }
  477: 
  478: sub get_tolrange {
  479:     my ($ans,$tol)=@_;
  480:     my ($high,$low);
  481:     if ($tol =~ /%$/) {
  482: 	chop($tol);
  483: 	my $change=$ans*($tol/100.0);
  484: 	$high=$ans+$change;
  485: 	$low=$ans-$change;
  486:     } else {
  487: 	$high=$ans+$tol;
  488: 	$low=$ans-$tol;
  489:     }
  490:     return ($high,$low);
  491: }
  492: 
  493: sub get_sigrange {
  494:     my ($sig)=@_;
  495:     &Apache::lonxml::debug("Got a sig of :$sig:");
  496:     my $sig_lbound;
  497:     my $sig_ubound;
  498:     if ($sig eq '') {
  499: 	$sig_lbound = 0; #SIG_LB_DEFAULT
  500: 	$sig_ubound =15; #SIG_UB_DEFAULT
  501:     } else {
  502: 	($sig_lbound,$sig_ubound) = split(/,/,$sig);
  503: 	if (!$sig_lbound) {
  504: 	    $sig_lbound = 0; #SIG_LB_DEFAULT
  505: 	    $sig_ubound =15; #SIG_UB_DEFAULT
  506: 	}
  507: 	if (!$sig_ubound) { $sig_ubound=$sig_lbound; }
  508:     }
  509:     return ($sig_ubound,$sig_lbound);
  510: }
  511: 
  512: sub start_stringresponse {
  513:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  514:     my $result;
  515:     my $id = &Apache::response::start_response($parstack,$safeeval);
  516:     if ($target eq 'meta') {
  517: 	&Apache::response::start_response($parstack,$safeeval);
  518: 	$result=&Apache::response::meta_package_write('stringresponse');
  519: 	&Apache::response::end_response();
  520:     } elsif ($target eq 'edit') {
  521: 	$result.=&Apache::edit::tag_start($target,$token);
  522: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
  523: 	$result.=&Apache::edit::select_arg('Type:','type',
  524: 			 [['cs','Case Sensitive'],['ci','Case Insensitive'],
  525: 			  ['mc','Case Insensitive, Any Order'],
  526: 			  ['re','Regular Expression']],$token);
  527: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  528:     } elsif ($target eq 'modified') {
  529: 	my $constructtag;
  530: 	$constructtag=&Apache::edit::get_new_args($token,$parstack,
  531: 						  $safeeval,'answer',
  532: 						  'type');
  533: 	if ($constructtag) {
  534: 	    $result = &Apache::edit::rebuild_tag($token);
  535: 	    $result.=&Apache::edit::handle_insert();
  536: 	}
  537:     } elsif ($target eq 'answer' || $target eq 'grade') {
  538: 	&Apache::response::reset_params();
  539:     }
  540:     return $result;
  541: }
  542: 
  543: sub end_stringresponse {
  544:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  545:     my $increment=1;
  546:     my $result = '';
  547:     my $part=$Apache::inputtags::part;
  548:     my $id=$Apache::inputtags::response[-1];
  549:     my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
  550:     my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
  551:     &Apache::lonxml::debug("current $answer ".$token->[2]);
  552:     if (!$Apache::lonxml::default_homework_loaded) {
  553: 	&Apache::lonxml::default_homework_load($safeeval);
  554:     }
  555:     if ( $target eq 'grade' && defined($ENV{'form.submitted'})) {
  556: 	&Apache::response::setup_params('stringresponse');
  557: 	$safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
  558: 	if ($Apache::lonhomework::type eq 'exam' ||
  559: 	    $ENV{'form.submitted'} eq 'scantron') {
  560: 	    $increment=&Apache::response::scored_response($part,$id);
  561: 	} else {
  562: 	    my $response = &Apache::response::getresponse();
  563: 	    if ( $response =~ /[^\s]/) {
  564: 		my %previous = &Apache::response::check_for_previous($response,
  565: 								    $part,$id);
  566: 		&Apache::lonxml::debug("submitted a $response<br>\n");
  567: 		&Apache::lonxml::debug($$parstack[-1] . "\n<br>");
  568: 		
  569: 		$Apache::lonhomework::results{"resource.$part.$id.submission"}=
  570: 		    $response;
  571: 		my $ad;
  572: 		if ($type eq 're' ) { 
  573: 		    # if the RE wasn't in a var it likely got munged,
  574:                     # thus grab it from the var directly
  575: #		    my $testans=$token->[2]->{'answer'};
  576: #		    if ($testans !~ m/^\s*\$/) {
  577: #			$answer=$token->[2]->{'answer'};
  578: #		    }
  579: 		    ${$safeeval->varglob('LONCAPA_INTERNAL_response')}=
  580: 			$response;
  581: 		    $result = &Apache::run::run('return $LONCAPA_INTERNAL_response=~m'.$answer,$safeeval);
  582: 		    &Apache::lonxml::debug("current $response");
  583: 		    &Apache::lonxml::debug("current $answer");
  584: 		    $ad = ($result) ? 'APPROX_ANS' : 'INCORRECT';
  585: 		} else {
  586: 		    $response =~ s/\\/\\\\/g;
  587: 		    $response =~ s/\'/\\\'/g;
  588: 		    &Apache::lonxml::debug("current $response");
  589: 		    my $expression="&caparesponse_check_list('".$response."','".
  590: 			$$parstack[-1];
  591: 		    foreach my $key (keys(%Apache::inputtags::params)) {
  592: 			$expression.= ';my $'. #'
  593: 			    $key.'="'.$Apache::inputtags::params{$key}.'"';
  594: 		    }
  595: 		    $expression.="');";
  596: 		    &Apache::lonxml::debug('answer is'.join(':',$answer));
  597: 		    @{$safeeval->varglob('CAPARESPONSE_CHECK_LIST_answer')}=($answer);
  598: 		    $result = &Apache::run::run($expression,$safeeval);
  599: 		    my ($awards) = split /:/ , $result;
  600: 		    ($ad) = &Apache::inputtags::finalizeawards(split /,/ , $awards);
  601: 		    &Apache::lonxml::debug("$expression");
  602: 		    &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
  603: 		}
  604: 		&Apache::response::handle_previous(\%previous,$ad);
  605: 		$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  606: 	    }
  607: 	}
  608:     } elsif ($target eq 'web' || $target eq 'tex') {
  609: 	my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
  610: 	my $status = $Apache::inputtags::status['-1'];
  611: 	if (  &Apache::response::show_answer() ) {
  612: 	    if ($target eq 'web') {
  613: 		$result="<br />The correct answer is ".$answer;
  614: #		    join(', ',@answers).".<br />";
  615: 	    }
  616: 	}
  617: 	if ($Apache::lonhomework::type eq 'exam' && $target eq 'tex') {
  618: 	    $result.='\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
  619: 	    $increment = &Apache::response::repetition();
  620: 	    $result.='\begin{enumerate}';
  621: 	    for (my $i=0;$i<$increment;$i++) {
  622: 		$result.='\item[\textbf{'.($Apache::lonxml::counter+$i).
  623: 		    '}.]\textit{Leave blank on scoring form}\vskip 0 mm';
  624: 	    }
  625: 	    $result.= '\end{enumerate}';
  626: 	}
  627:     } elsif ($target eq 'answer' || $target eq 'analyze') {
  628: 	if ($target eq 'analyze') {
  629: 	    push (@{ $Apache::lonhomework::analyze{"parts"} },"$part.$id");
  630: 	    $Apache::lonhomework::analyze{"$part.$id.type"} = 'stringresponse';
  631: 	}
  632: 	&Apache::response::setup_params('stringresponse');
  633: 	if ($target eq 'answer') {
  634: 	    $result.=&Apache::response::answer_header('stringresponse');
  635: 	}
  636: #	foreach my $ans (@answers) {
  637: 	    if ($target eq 'answer') {
  638: 		$result.=&Apache::response::answer_part('stringresponse',$answer);
  639: 	    } elsif ($target eq 'analyze') {
  640: 		push (@{ $Apache::lonhomework::analyze{"$part.$id.answer"} },
  641: 		      $answer);
  642: 	    }
  643: #	}
  644: 	my $string='Case Insensitive';
  645: 	if ($type eq 'mc') {
  646: 	    $string='Multiple Choice';
  647: 	} elsif ($type eq 'cs') {
  648: 	    $string='Case Sensitive';
  649: 	} elsif ($type eq 'ci') {
  650: 	    $string='Case Insensitive';
  651: 	} elsif ($type eq 're') {
  652: 	    $string='Regular Expression';
  653: 	}
  654: 	if ($target eq 'answer') {
  655: 	    if ($ENV{'form.answer_output_mode'} eq 'tex') {
  656: 		$result.=&Apache::response::answer_part('stringresponse',
  657: 							"$string");
  658: 	    } else {
  659: 		$result.=&Apache::response::answer_part('stringresponse',
  660: 							"<b>$string</b>");
  661: 	    }
  662: 	} elsif ($target eq 'analyze') {
  663: 	    push (@{$Apache::lonhomework::analyze{"$part.$id.str_type"}},
  664: 		  $type);
  665: 	}
  666: 	if ($target eq 'answer') {
  667: 	    $result.=&Apache::response::answer_footer('stringresponse');
  668: 	}
  669:     } elsif ($target eq 'edit') {
  670: 	$result.='</td></tr>'.&Apache::edit::end_table;
  671:     }
  672:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
  673: 	$target eq 'tex' || $target eq 'analyze') {
  674: 	&Apache::lonxml::increment_counter($increment);
  675:     }
  676:     &Apache::response::end_response;
  677:     return $result;
  678: }
  679: 
  680: sub start_formularesponse {
  681:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  682:     my $result;
  683:     if ($target eq 'meta') {
  684: 	&Apache::response::start_response($parstack,$safeeval);
  685: 	$result=&Apache::response::meta_package_write('formularesponse');
  686: 	&Apache::response::end_response();
  687:     } else {
  688: 	$result.=&start_numericalresponse(@_);
  689:     }
  690:     return $result;
  691: }
  692: 
  693: sub end_formularesponse {
  694:     return end_numericalresponse(@_);
  695: }
  696: 
  697: 1;
  698: __END__
  699: 

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