File:  [LON-CAPA] / loncom / homework / caparesponse / caparesponse.pm
Revision 1.148: download - view: text, annotated - select for diffs
Thu Jun 10 21:19:59 2004 UTC (19 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- BUG#3104

    1: # The LearningOnline Network with CAPA
    2: # caparesponse definition
    3: #
    4: # $Id: caparesponse.pm,v 1.148 2004/06/10 21:19:59 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 ";
  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 $courseid=$ENV{'request.course.id'};
  521:     if (lc($ENV{"course.$courseid.disablesigfigs"}) eq 'yes') {
  522: 	return (15,0);
  523:     }
  524:     my $sig_lbound;
  525:     my $sig_ubound;
  526:     if ($sig eq '') {
  527: 	$sig_lbound = 0; #SIG_LB_DEFAULT
  528: 	$sig_ubound =15; #SIG_UB_DEFAULT
  529:     } else {
  530: 	($sig_lbound,$sig_ubound) = split(/,/,$sig);
  531: 	if (!defined($sig_lbound)) {
  532: 	    $sig_lbound = 0; #SIG_LB_DEFAULT
  533: 	    $sig_ubound =15; #SIG_UB_DEFAULT
  534: 	}
  535: 	if (!defined($sig_ubound)) { $sig_ubound=$sig_lbound; }
  536:     }
  537:     if (($sig_ubound<$sig_lbound) ||
  538: 	($sig_lbound > 15) ||
  539: 	($sig =~/(\+|-)/ ) ) {
  540: 	my $errormsg=&mt("Invalid Significant figures detected")." ($sig)";
  541: 	if ($ENV{'request.state'} eq 'construct') {
  542: 	    $errormsg.=
  543: 		&Apache::loncommon::help_open_topic('Significant_Figures');
  544: 	}
  545: 	&Apache::lonxml::error($errormsg);
  546:     }
  547:     return ($sig_ubound,$sig_lbound);
  548: }
  549: 
  550: sub start_stringresponse {
  551:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  552:     my $result;
  553:     my $id = &Apache::response::start_response($parstack,$safeeval);
  554:     if ($target eq 'meta') {
  555: 	&Apache::response::start_response($parstack,$safeeval);
  556: 	$result=&Apache::response::meta_package_write('stringresponse');
  557: 	&Apache::response::end_response();
  558:     } elsif ($target eq 'edit') {
  559: 	$result.=&Apache::edit::tag_start($target,$token);
  560: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
  561: 	$result.=&Apache::edit::select_arg('Type:','type',
  562: 			 [['cs','Case Sensitive'],['ci','Case Insensitive'],
  563: 			  ['mc','Case Insensitive, Any Order'],
  564: 			  ['re','Regular Expression']],$token);
  565: 	$result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  566:     } elsif ($target eq 'modified') {
  567: 	my $constructtag;
  568: 	$constructtag=&Apache::edit::get_new_args($token,$parstack,
  569: 						  $safeeval,'answer',
  570: 						  'type','answerdisplay');
  571: 	if ($constructtag) {
  572: 	    $result = &Apache::edit::rebuild_tag($token);
  573: 	    $result.=&Apache::edit::handle_insert();
  574: 	}
  575:     } elsif ($target eq 'web') {
  576: 	if (  &Apache::response::show_answer() ) {
  577: 	    $Apache::inputtags::answertxt{$id}=
  578: 		&Apache::lonxml::get_param('answer',$parstack,$safeeval);
  579: 	} 
  580:     } elsif ($target eq 'answer' || $target eq 'grade') {
  581: 	&Apache::response::reset_params();
  582:     }
  583:     return $result;
  584: }
  585: 
  586: sub end_stringresponse {
  587:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  588:     my $increment=1;
  589:     my $result = '';
  590:     my $part=$Apache::inputtags::part;
  591:     my $id=$Apache::inputtags::response[-1];
  592:     my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
  593:     my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
  594:     my $answerdisplay=&Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval);
  595:     &Apache::lonxml::debug("current $answer ".$token->[2]);
  596:     if (!$Apache::lonxml::default_homework_loaded) {
  597: 	&Apache::lonxml::default_homework_load($safeeval);
  598:     }
  599:     if ( $target eq 'grade' && defined($ENV{'form.submitted'})) {
  600: 	&Apache::response::setup_params('stringresponse',$safeeval);
  601: 	$safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
  602: 	if ($Apache::lonhomework::type eq 'exam' ||
  603: 	    $ENV{'form.submitted'} eq 'scantron') {
  604: 	    $increment=&Apache::response::scored_response($part,$id);
  605: 	} else {
  606: 	    my $response = &Apache::response::getresponse();
  607: 	    if ( $response =~ /[^\s]/) {
  608: 		my %previous = &Apache::response::check_for_previous($response,
  609: 								    $part,$id);
  610: 		&Apache::lonxml::debug("submitted a $response<br>\n");
  611: 		&Apache::lonxml::debug($$parstack[-1] . "\n<br>");
  612: 		$Apache::lonhomework::results{"resource.$part.$id.submission"}=
  613: 		    $response;
  614: 		my ($ad,$msg);
  615: 		if ($type eq 're' ) { 
  616: 		    # if the RE wasn't in a var it likely got munged,
  617:                     # thus grab it from the var directly
  618: #		    my $testans=$token->[2]->{'answer'};
  619: #		    if ($testans !~ m/^\s*\$/) {
  620: #			$answer=$token->[2]->{'answer'};
  621: #		    }
  622: 		    ${$safeeval->varglob('LONCAPA::response')}=$response;
  623: 		    $result = &Apache::run::run('return $LONCAPA::response=~m'.$answer,$safeeval);
  624: 		    &Apache::lonxml::debug("current $response");
  625: 		    &Apache::lonxml::debug("current $answer");
  626: 		    $ad = ($result) ? 'APPROX_ANS' : 'INCORRECT';
  627: 		} else {
  628: 		    my $args_ref= 
  629: 			\%{$safeeval->varglob('LONCAPA::CAPAresponse_args')};
  630: 		
  631: 		    $$args_ref{'response'}=$response;
  632: 		    &Apache::lonxml::debug("current $response");
  633: 		    $$args_ref{'type'}=
  634: 			&Apache::lonxml::get_param('type',$parstack,$safeeval);
  635: 		    foreach my $key (keys(%Apache::inputtags::params)) {
  636: 			$$args_ref{$key}=$Apache::inputtags::params{$key};
  637: 		    }
  638: 		    &Apache::lonxml::debug('answer is'.join(':',$answer));
  639: 		    @{$safeeval->varglob('LONCAPA::CAPAresponse_answer')}=($answer);
  640: 		    ($result, my @msgs)=&Apache::run::run("&caparesponse_check_list()",$safeeval);
  641: 		    &Apache::lonxml::debug('msgs are'.join(':',@msgs));
  642: 		    my ($awards)=split(/:/,$result);
  643: 		    my (@awards) = split(/,/,$awards);
  644: 		    ($ad,$msg) = &Apache::inputtags::finalizeawards(\@awards,\@msgs);
  645: 		    &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
  646: 		}
  647: 		&Apache::response::handle_previous(\%previous,$ad);
  648: 		$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  649: 		$Apache::lonhomework::results{"resource.$part.$id.awardmsg"}=$msg;
  650: 	    }
  651: 	}
  652:     } elsif ($target eq 'web' || $target eq 'tex') {
  653: 	my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
  654: 	my $status = $Apache::inputtags::status['-1'];
  655: 	if ($Apache::lonhomework::type eq 'exam' && $target eq 'tex') {
  656: 	    $result.='\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
  657: 	    $increment = &Apache::response::repetition();
  658: 	    $result.='\begin{enumerate}';
  659: 	    for (my $i=0;$i<$increment;$i++) {
  660: 		$result.='\item[\textbf{'.($Apache::lonxml::counter+$i).
  661: 		    '}.]\textit{Leave blank on scoring form}\vskip 0 mm';
  662: 	    }
  663: 	    $result.= '\end{enumerate}';
  664: 	}
  665:     } elsif ($target eq 'answer' || $target eq 'analyze') {
  666: 	if ($target eq 'analyze') {
  667: 	    push (@{ $Apache::lonhomework::analyze{"parts"} },"$part.$id");
  668: 	    $Apache::lonhomework::analyze{"$part.$id.type"} = 'stringresponse';
  669: 	}
  670: 	&Apache::response::setup_params('stringresponse',$safeeval);
  671: 	if ($target eq 'answer') {
  672: 	    $result.=&Apache::response::answer_header('stringresponse');
  673: 	}
  674: #	foreach my $ans (@answers) {
  675: 	    if ($target eq 'answer') {
  676: 		$result.=&Apache::response::answer_part('stringresponse',$answer);
  677: 	    } elsif ($target eq 'analyze') {
  678: 		push (@{ $Apache::lonhomework::analyze{"$part.$id.answer"} },
  679: 		      $answer);
  680: 	    }
  681: #	}
  682: 	my $string='Case Insensitive';
  683: 	if ($type eq 'mc') {
  684: 	    $string='Multiple Choice';
  685: 	} elsif ($type eq 'cs') {
  686: 	    $string='Case Sensitive';
  687: 	} elsif ($type eq 'ci') {
  688: 	    $string='Case Insensitive';
  689: 	} elsif ($type eq 're') {
  690: 	    $string='Regular Expression';
  691: 	}
  692: 	if ($target eq 'answer') {
  693: 	    if ($ENV{'form.answer_output_mode'} eq 'tex') {
  694: 		$result.=&Apache::response::answer_part('stringresponse',
  695: 							"$string");
  696: 	    } else {
  697: 		$result.=&Apache::response::answer_part('stringresponse',
  698: 							"<b>$string</b>");
  699: 	    }
  700: 	} elsif ($target eq 'analyze') {
  701: 	    push (@{$Apache::lonhomework::analyze{"$part.$id.str_type"}},
  702: 		  $type);
  703: 	}
  704: 	if ($target eq 'answer') {
  705: 	    $result.=&Apache::response::answer_footer('stringresponse');
  706: 	}
  707:     } elsif ($target eq 'edit') {
  708: 	$result.='</td></tr>'.&Apache::edit::end_table;
  709:     }
  710:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || 
  711: 	$target eq 'tex' || $target eq 'analyze') {
  712: 	&Apache::lonxml::increment_counter($increment);
  713:     }
  714:     &Apache::response::end_response;
  715:     return $result;
  716: }
  717: 
  718: sub start_formularesponse {
  719:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  720:     my $result;
  721:     if ($target eq 'meta') {
  722: 	&Apache::response::start_response($parstack,$safeeval);
  723: 	$result=&Apache::response::meta_package_write('formularesponse');
  724: 	&Apache::response::end_response();
  725:     } else {
  726: 	$result.=&start_numericalresponse(@_);
  727:     }
  728:     return $result;
  729: }
  730: 
  731: sub end_formularesponse {
  732:     return end_numericalresponse(@_);
  733: }
  734: 
  735: 1;
  736: __END__
  737: 

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