File:  [LON-CAPA] / loncom / homework / caparesponse / caparesponse.pm
Revision 1.170: download - view: text, annotated - select for diffs
Sun Jun 26 22:21:06 2005 UTC (18 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- apparently there are alot of published problems with bad sig figs.

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

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