File:  [LON-CAPA] / loncom / homework / caparesponse / caparesponse.pm
Revision 1.173: download - view: text, annotated - select for diffs
Mon Jul 11 18:24:12 2005 UTC (18 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- BUG#4193 - improve can't get things correct error message

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

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