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