File:  [LON-CAPA] / loncom / homework / caparesponse / caparesponse.pm
Revision 1.136: download - view: text, annotated - select for diffs
Thu Feb 26 21:13:54 2004 UTC (20 years, 3 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
Bug 2759 (Format of correct answer not followed in exam mode printing) is fixed for sure and probably bug 2765(numericalresponse sometimes still creates incorrect width tables) is fixed either. But I need to make more checks for the last one.

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

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