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

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

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