File:  [LON-CAPA] / loncom / homework / caparesponse / caparesponse.pm
Revision 1.161: download - view: text, annotated - select for diffs
Wed Jan 26 22:43:43 2005 UTC (19 years, 4 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- BUG#3863 make it easier to use incorrect, incorrect="1,2,3,4,5,6,7,8,9" works now

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

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