Annotation of loncom/homework/caparesponse/caparesponse.pm, revision 1.153

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

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