File:  [LON-CAPA] / loncom / homework / caparesponse / caparesponse.pm
Revision 1.146: download - view: text, annotated - select for diffs
Fri Jun 4 22:06:07 2004 UTC (20 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- fixing display of numerical/formula/string respons types to make BUG#873 go away
- now longer says 'The correct answers is ...' but rather just puts the correct answer up there, and whenever it would display the computer's answer is says 'Computer's answer now show'

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

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