File:  [LON-CAPA] / loncom / homework / caparesponse / caparesponse.pm
Revision 1.149: download - view: text, annotated - select for diffs
Tue Jun 15 18:34:44 2004 UTC (19 years, 11 months ago) by www
Branches: MAIN
CVS tags: HEAD
Help "?"

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

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