File:  [LON-CAPA] / loncom / homework / caparesponse / caparesponse.pm
Revision 1.163: download - view: text, annotated - select for diffs
Mon Feb 21 20:29:00 2005 UTC (19 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- when in survey mode, still need to check for sigfigs/units/etc as if these are incorrect it in a invalid answer

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

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