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