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

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

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