Annotation of loncom/homework/caparesponse/caparesponse.pm, revision 1.93
1.3 albertel 1: # The LearningOnline Network with CAPA
2: # caparesponse definition
1.47 albertel 3: #
1.93 ! albertel 4: # $Id: caparesponse.pm,v 1.92 2003/04/08 04:17:13 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.3 albertel 32:
1.50 harris41 33: BEGIN {
1.89 albertel 34: &Apache::lonxml::register('Apache::caparesponse',('caparesponse','numericalresponse','stringresponse','formularesponse'));
1.3 albertel 35: }
36:
1.89 albertel 37: sub start_numericalresponse {
38: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
39: my $id = &Apache::response::start_response($parstack,$safeeval);
40: my $result;
41: if ($target eq 'edit') {
42: $result.=&Apache::edit::tag_start($target,$token);
43: $result.=&Apache::edit::text_arg('Answer:','answer',$token);
44: if ($token->[1] eq 'numericalresponse') {
45: $result.=&Apache::edit::text_arg('Unit:','unit',$token,5).
46: &Apache::loncommon::help_open_topic('Physical_Units');
47: $result.=&Apache::edit::text_arg('Format:','format',$token,4).
48: &Apache::loncommon::help_open_topic('Numerical_Response_Format');
49: } elsif ($token->[1] eq 'stringresponse') {
50: $result.=&Apache::edit::select_arg('Type:','type',
51: [['cs','Case Sensitive'],['ci','Case Insensitive'],
52: ['mc','Case Insensitive, Any Order']],$token);
53: } elsif ($token->[1] eq 'formularesponse') {
54: $result.=&Apache::edit::text_arg('Sample Points:','samples',
55: $token,40).
56: &Apache::loncommon::help_open_topic('Formula_Response_Sampling');
57: }
58: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
59: } elsif ($target eq 'modified') {
60: my $constructtag;
61: if ($token->[1] eq 'numericalresponse') {
62: $constructtag=&Apache::edit::get_new_args($token,$parstack,
63: $safeeval,'answer',
64: 'unit','format');
65: } elsif ($token->[1] eq 'stringresponse') {
66: $constructtag=&Apache::edit::get_new_args($token,$parstack,
67: $safeeval,'answer',
68: 'type');
69: } elsif ($token->[1] eq 'formularesponse') {
70: $constructtag=&Apache::edit::get_new_args($token,$parstack,
71: $safeeval,'answer',
72: 'samples');
73: }
74: if ($constructtag) {
75: $result = &Apache::edit::rebuild_tag($token);
76: $result.=&Apache::edit::handle_insert();
1.22 albertel 77: }
1.89 albertel 78: } elsif ($target eq 'meta') {
79: $result=&Apache::response::meta_package_write('numericalresponse');
80: } elsif ($target eq 'answer' || $target eq 'grade') {
81: &Apache::response::reset_params();
1.16 albertel 82: }
1.89 albertel 83: return $result;
1.21 albertel 84: }
85:
1.89 albertel 86: sub end_numericalresponse {
87: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.90 albertel 88: my $increment=1;
1.89 albertel 89: my $result = '';
90: if (!$Apache::lonxml::default_homework_loaded) {
91: &Apache::lonxml::default_homework_load($safeeval);
1.34 albertel 92: }
1.90 albertel 93: if ( $target eq 'grade' && defined $ENV{'form.submitted'}) {
94: &Apache::response::setup_params($$tagstack[-1]);
95: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
96: my $partid = $Apache::inputtags::part;
97: my $id = $Apache::inputtags::response['-1'];
98: my $response = &Apache::response::getresponse();
99: if ( $response =~ /[^\s]/) {
100: my $ad;
101: my %previous = &Apache::response::check_for_previous($response,$partid,$id);
102: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
103: &Apache::lonxml::debug("submitted a $response<br>\n");
1.92 albertel 104: &Apache::lonxml::debug($$parstack[-1] . "\n<br>");
1.90 albertel 105:
106: if ($ENV{'form.submitted'} eq 'scantron') {
1.92 albertel 107: my $number_of_bubbles = 8;#default values for number of bubbles
108: my (@formats)=&Apache::lonxml::get_param_var('format',
109: $parstack,$safeeval);
110: my (@answers)=&Apache::lonxml::get_param_var('answer',
111: $parstack,$safeeval);
112: my @values=&make_numerical_bubbles($number_of_bubbles,$target,
113: $answers[0],$formats[0]);
114: $response=$values[$response];
115: } else {
116: $response =~ s/\\/\\\\/g;
117: $response =~ s/\'/\\\'/g;
1.90 albertel 118: }
119: &Apache::lonxml::debug("current $response");
120: my $expression="&caparesponse_check_list('".$response."','".
121: $$parstack[-1];
1.92 albertel 122: #no way to enter units, with radio buttons
123: if ($Apache::lonhomework::type eq 'exam') {
124: $expression.=';my $unit=undef;';
125: }
1.90 albertel 126: foreach my $key (keys(%Apache::inputtags::params)) {
127: $expression.= ';my $'. #'
128: $key.'="'.$Apache::inputtags::params{$key}.'"';
129: }
130: if ($$tagstack[-1] eq 'formularesponse') {
131: $expression.=';my $type="fml";';
132: } elsif ($$tagstack[-1] eq 'numericalresponse') {
133: $expression.=';my $type="float";';
134: }
135: $expression.="');";
136: $result = &Apache::run::run($expression,$safeeval);
137: my ($awards) = split /:/ , $result;
138: ($ad) = &Apache::inputtags::finalizeawards(split /,/ , $awards);
139: &Apache::lonxml::debug("$expression");
140: &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
141: &Apache::response::handle_previous(\%previous,$ad);
142: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
143: $result='';
1.89 albertel 144: }
145: } elsif ($target eq 'web' || $target eq 'tex') {
1.90 albertel 146: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
147: $safeeval);
1.89 albertel 148: my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
149: my $status = $Apache::inputtags::status['-1'];
150: if ($award =~ /^correct/ || $status eq "SHOW_ANSWER" ) {
151: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,
152: $safeeval);
153: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
154: $safeeval);
155: if ($target eq 'web') {
156: $result="<br />The correct answer is ";
157: } elsif ($target eq 'tex') {
158: #$result='\vskip 0 mm The correct answer is \\texttt{';
159: }
160: for (my $i=0; $i <= $#answers; $i++) {
161: my $answer=$answers[$i];
162: my $format;
163: if ($#formats > 0) {
164: $format=$formats[$i];
165: } else {
166: $format=$formats[0];
167: }
168: my $formatted;
169: if ((defined($format)) && ($format ne '')) {
170: &Apache::lonxml::debug("formatting with :$format: answer :$answer:");
171: $formatted=sprintf('%.'.$format,$answer).',';
172: } else {
173: &Apache::lonxml::debug("no format answer :$answer:");
174: $formatted="$answer,";
175: }
176:
177: if ($target eq 'tex') {
178: $formatted='';
179: #$formatted=&Apache::lonxml::latex_special_symbols($formatted);
180: }
181: $result.=$formatted;
182: }
183: chop $result;
184: if ($target eq 'web') {
185: $result.=" $unit.<br />";
186: } elsif ($target eq 'tex') {
187: #$result.=&Apache::lonxml::latex_special_symbols($unit);
188: #$result.="}. \\vskip 0 mm ";
189: }
190: }
191: if ($Apache::lonhomework::type eq 'exam') {
1.90 albertel 192: my $number_of_bubbles = 8; #default values for number of bubbles
1.89 albertel 193: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,
194: $safeeval);
195: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
196: $safeeval);
1.90 albertel 197: my @bubble_values=&make_numerical_bubbles($number_of_bubbles,
1.91 albertel 198: $target,$answers[0],
199: $formats[0]);
1.89 albertel 200: my @alphabet=('A'..'Z');
1.90 albertel 201: my $id=$Apache::inputtags::response[-1];
1.89 albertel 202: if ($target eq 'web') {
203: if ($$tagstack[-1] eq 'numericalresponse') {
204: if ($unit=~/\S/) {$result.=' (in '.$unit.')<br /><br />';}
205: $result.= '<table border="1"><tr>';
1.90 albertel 206: for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
207: $result.='<td><input type="radio" name="HWVAL_'.$id.
1.91 albertel 208: '" value="'.$bubble_values[$ind].'"><b>'.
209: $alphabet[$ind].'</b>: '.
210: $bubble_values[$ind].'</td>';
1.89 albertel 211: }
212: $result.='</tr></table>';
213: } elsif ($$tagstack[-1] eq 'formularesponse') {
1.90 albertel 214: $result.= '<br /><br /><font color="red">
215: <textarea name="HWVAL_'.$id.'" rows="4" cols="50">
216: </textarea></font> <br /><br />';
1.89 albertel 217: }
218: } elsif ($target eq 'tex') {
219: if (defined $unit and $Apache::lonhomework::type eq 'exam') {
220: $result.=' \textit{(in} \verb|'.$unit.'|\textit{)} ';
221: }
222: if ($$tagstack[-1] eq 'numericalresponse') {
1.90 albertel 223: my ($celllength,$number_of_tables,@table_range)=
224: &get_table_sizes($formats[0],$number_of_bubbles);
1.89 albertel 225: my $j=0;
226: my $cou=0;
227: $result.='\vskip -1 mm \noindent \begin{enumerate}\item[\textbf{'.$Apache::lonxml::counter.'}.]';
228: for (my $i=0;$i<$number_of_tables;$i++) {
229: $result.='\vskip -1 mm \noindent \begin{tabular}{';
1.90 albertel 230: for (my $ind=0;$ind<$table_range[$j];$ind++) {
1.89 albertel 231: $result.='lp{'.$celllength.' mm}';
232: }
233: $result.='}';
1.90 albertel 234: for (my $ind=$cou;$ind<$cou+$table_range[$j];$ind++) {
1.91 albertel 235: $result.='\hskip -3 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$\hskip -2 mm & {\small '.$bubble_values[$ind].'} ';
1.89 albertel 236: if ($ind != $cou+$table_range[$j]-1) {$result.=' & ';}
237: }
238: $cou += $table_range[$j];
239: $j++;
240: $result.='\\\\\end{tabular}\vskip 0 mm ';
241: }
242: $result.='\end{enumerate}';
243: } else {
244: $result.='\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
1.93 ! albertel 245: my $repetition = &Apache::response::repetition();
1.89 albertel 246: $result.='\begin{enumerate}';
247: for (my $i=0;$i<$repetition;$i++) {
1.93 ! albertel 248: $result.='\item[\textbf{'.($Apache::lonxml::counter+$i).'}.]\textit{Leave blank on scoring form}\vskip 0 mm';
1.89 albertel 249: }
1.90 albertel 250: $increment=$repetition;
1.89 albertel 251: $result.= '\end{enumerate}';
252: }
253: }
254: }
255: } elsif ($target eq 'edit') {
256: $result.='</td></tr>'.&Apache::edit::end_table;
257: } elsif ($target eq 'answer' || $target eq 'analyze') {
1.58 sakharuk 258:
1.89 albertel 259: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
260: if ($target eq 'analyze') {
261: push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
262: $Apache::lonhomework::analyze{"$part_id.type"} = $$tagstack[-1];
263: }
264: &Apache::response::setup_params($$tagstack[-1]);
265: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
1.58 sakharuk 266: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
267: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval);
1.89 albertel 268: my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
269:
270: if ($target eq 'answer') {
271: $result.=&Apache::response::answer_header($$tagstack[-1]);
272: }
273: for(my $i=0;$i<=$#answers;$i++) {
274: my $ans=$answers[$i];
1.90 albertel 275: my $fmt=$formats[0];
276: if (@formats && $#formats) {$fmt=$formats[$i];}
1.89 albertel 277: my ($high,$low);
278: if ($Apache::inputtags::params{'tol'}) {
279: ($high,$low)=&get_tolrange($ans,$Apache::inputtags::params{'tol'});
280: }
281: my ($sighigh,$siglow);
282: if ($Apache::inputtags::params{'sig'}) {
283: ($sighigh,$siglow)=&get_sigrange($Apache::inputtags::params{'sig'});
284: }
285: if ($fmt) {
286: $ans = sprintf('%.'.$fmt,$ans);
287: if ($high) {
1.90 albertel 288: $high=sprintf('%.'.$fmt,$high);
289: $low =sprintf('%.'.$fmt,$low);
1.62 sakharuk 290: }
1.60 sakharuk 291: }
1.89 albertel 292: if ($target eq 'answer') {
293: if ($high) { $ans.=' ['.$low.','.$high.']'; }
294: if ($sighigh) { $ans.= " Sig <i>$siglow - $sighigh</i>"; }
295: $result.=&Apache::response::answer_part($$tagstack[-1],$ans);
296: } elsif ($target eq 'analyze') {
297: push (@{ $Apache::lonhomework::analyze{"$part_id.answer"} }, $ans);
298: if ($high) {
299: push (@{ $Apache::lonhomework::analyze{"$part_id.ans_high"} }, $high);
300: push (@{ $Apache::lonhomework::analyze{"$part_id.ans_low"} }, $low);
1.58 sakharuk 301: }
1.79 sakharuk 302: }
1.36 albertel 303: }
1.89 albertel 304: if ($unit) {
305: if ($target eq 'answer') {
306: $result.=&Apache::response::answer_part($$tagstack[-1],
307: "Unit: <b>$unit</b>");
308: } elsif ($target eq 'analyze') {
309: push (@{ $Apache::lonhomework::analyze{"$part_id.unit"} },
310: $unit);
311: }
312: }
313: if ($type || $token->[1] eq 'stringresponse') {
314: my $string='Case Insensitive';
315: if ($type eq 'mc') {
316: $string='Multiple Choice';
317: } elsif ($type eq 'cs') {
318: $string='Case Sensitive';
319: } elsif ($type eq 'ci') {
320: $string='Case Insensitive';
321: } elsif ($type eq 'fml') {
322: $string='Formula';
323: }
324: if ($target eq 'answer') {
325: $result.=&Apache::response::answer_part($$tagstack[-1],
326: '<b>'.$string.'</b>');
327: } elsif ($target eq 'analyze') {
328: push (@{ $Apache::lonhomework::analyze{"$part_id.type"} },
329: $type);
330: }
331: }
332: if ($$tagstack[-1] eq 'formularesponse' && $target eq 'answer') {
333: my $samples=&Apache::lonxml::get_param('samples',$parstack,$safeeval);
334: $result.=&Apache::response::answer_part($$tagstack[-1],$samples);
335: }
336: if ($target eq 'answer') {
337: $result.=&Apache::response::answer_footer($$tagstack[-1]);
338: }
1.69 sakharuk 339: }
1.90 albertel 340: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
341: $target eq 'tex' || $target eq 'analyze') {
342: &Apache::lonxml::increment_counter($increment);
343: }
1.89 albertel 344: &Apache::response::end_response;
345: return $result;
1.90 albertel 346: }
347:
348: sub get_table_sizes {
349: my ($format,$number_of_bubbles)=@_;
350: my $max_val = 0;
351: if ($format=~m/^(\d+)E([^\d]*)(\d*)$/) {
352: $max_val=$1+$2+4;
353: } else {
354: $max_val=4;
355: }
356: $max_val = int(0.9*$ENV{'form.textwidth'}/(($max_val+6)*2));
357: my $celllength = 0.9*$ENV{'form.textwidth'}/$max_val-10;
358: my @table_range = ();
359: my $number_of_tables = int($number_of_bubbles/$max_val);
360: for (my $i=0;$i<$number_of_tables;$i++) {push @table_range,$max_val;}
361: if ($number_of_bubbles % $max_val != 0) {
362: $number_of_tables++;
363: push @table_range,($number_of_bubbles % $max_val);
364: }
365: return ($celllength,$number_of_tables,@table_range);
366: }
367:
368: sub format_number {
369: my ($number,$format,$target)=@_;
370: my $ans;
371: if ($format ne '') {
372: $ans = sprintf('%.'.$format,$number);
373: } else {
374: my $format = '';
375: #What is the number? (integer,decimal,floating point)
376: if ($number=~/^(\d*\.?\d*)(E|e)(\d*)$/) {
377: $format = 'e'.$2;
378: } elsif ($number=~/^(\d*)\.(\d*)$/) {
379: $format = '4f';
380: } elsif ($number=~/^(\d*)$/) {
381: $format = 'd';
382: }
383: $ans = sprintf('%.'.$format,$number);
384: }
385: if ($target eq 'tex') {
386: if ($ans =~ m/([0-9\.\-\+]+)E([0-9\-\+]+)/ ) {
387: my $number = $1;
388: my $power = $2;
389: $power=~s/^\+//;
390: $power=~s/^(-?)0+(\d+)//;
391: $ans=$number.'$\times 10^{'.$1.$2.'}$'; #'stupidemacs
392: }
393: }
394: return $ans;
395: }
396:
397: sub make_numerical_bubbles {
1.91 albertel 398: my ($number_of_bubbles,$target,$answer,$format) =@_;
399: my @bubble_values = ();
1.90 albertel 400: my @factors = (1.13,1.17,1.25,1.33,1.45); #default values of factors
401: my @powers = (1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0); #default values for powers
402: &Apache::response::setrandomnumber();
403: my $ind=&Math::Random::random_uniform_integer(1,0,$#powers);
404: my $power = $powers[$ind];
405: $ind=&Math::Random::random_uniform_integer(1,0,$#factors);
406: my $factor = $factors[$ind];
407: for ($ind=0;$ind<$number_of_bubbles;$ind++) {
1.91 albertel 408: $bubble_values[$ind] = $answer*($factor**($power-$powers[$#powers-$ind]));
409: $bubble_values[$ind] = &format_number($bubble_values[$ind],
410: $format,$target);
411:
1.90 albertel 412: }
1.91 albertel 413: return @bubble_values;
1.51 albertel 414: }
415:
416: sub get_tolrange {
1.89 albertel 417: my ($ans,$tol)=@_;
418: my ($high,$low);
419: if ($tol =~ /%$/) {
420: chop($tol);
421: my $change=$ans*($tol/100.0);
422: $high=$ans+$change;
423: $low=$ans-$change;
424: } else {
425: $high=$ans+$tol;
426: $low=$ans-$tol;
427: }
428: return ($high,$low);
1.52 albertel 429: }
430:
431: sub get_sigrange {
1.89 albertel 432: my ($sig)=@_;
433: &Apache::lonxml::debug("Got a sig of :$sig:");
434: my $sig_lbound;
435: my $sig_ubound;
436: if ($sig eq '') {
437: $sig_lbound = 0; #SIG_LB_DEFAULT
438: $sig_ubound =15; #SIG_UB_DEFAULT
439: } else {
440: ($sig_lbound,$sig_ubound) = split(/,/,$sig);
441: if (!$sig_lbound) {
442: $sig_lbound = 0; #SIG_LB_DEFAULT
443: $sig_ubound =15; #SIG_UB_DEFAULT
444: }
445: if (!$sig_ubound) { $sig_ubound=$sig_lbound; }
1.52 albertel 446: }
1.89 albertel 447: return ($sig_ubound,$sig_lbound);
1.34 albertel 448: }
449:
450: sub start_stringresponse {
1.89 albertel 451: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
452: my $result;
453: if ($target eq 'meta') {
454: $result=&Apache::response::meta_package_write('stringresponse');
455: } else {
456: $result.=&start_numericalresponse(@_);
457: }
458: return $result;
1.34 albertel 459: }
460:
461: sub end_stringresponse {
1.89 albertel 462: return end_numericalresponse(@_);
1.44 albertel 463: }
464:
465: sub start_formularesponse {
1.89 albertel 466: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
467: my $result;
468: if ($target eq 'meta') {
469: $result=&Apache::response::meta_package_write('formularesponse');
470: } else {
471: $result.=&start_numericalresponse(@_);
472: }
473: return $result;
1.44 albertel 474: }
475:
476: sub end_formularesponse {
1.89 albertel 477: return end_numericalresponse(@_);
1.3 albertel 478: }
479:
1.1 albertel 480: 1;
1.3 albertel 481: __END__
1.89 albertel 482:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>