1: # The LearningOnline Network with CAPA
2: # caparesponse definition
3: #
4: # $Id: caparesponse.pm,v 1.52 2002/04/11 19:48:16 albertel Exp $
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: #
28:
29: package Apache::caparesponse;
30: use strict;
31: use capa;
32:
33: BEGIN {
34: &Apache::lonxml::register('Apache::caparesponse',('caparesponse','numericalresponse','stringresponse','formularesponse'));
35: }
36:
37: sub start_caparesponse {
38: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
39: my $id = &Apache::response::start_response($parstack,$safeeval);
40: return '';
41: }
42:
43: sub end_caparesponse {
44: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
45: my $result = '';
46: if ( $target eq 'grade' ) {
47: if ( defined $ENV{'form.submitted'}) {
48: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
49: my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']};
50: if ( $response =~ /[^\s]/) {
51: my $id = $Apache::inputtags::response['-1'];
52: my $previous= &Apache::response::check_for_previous($response,$id,$response);
53: if ( $previous ) {
54: $result = 'PREVIOUSLY_USED';
55: } else {
56: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$response;
57: &Apache::lonxml::debug("submitted a $response<br>\n");
58: &Apache::lonxml::debug($$parstack[$#$parstack] . "\n<br>");
59: my $expression="&caparesponse_check('".$response."','".
60: $$parstack[$#$parstack].
61: ';my $tol="'.$Apache::inputtags::params{'tol'}.'"'.
62: ';my $sig="'.$Apache::inputtags::params{'sig'}.'"'.
63: "');";
64: $result = &Apache::run::run($expression,$safeeval);
65: my ($ad) = split /:/ , $result;
66: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}=$ad;
67: &Apache::lonxml::debug("$expression");
68: &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
69: }
70: }
71: }
72: } elsif ($target eq 'web') {
73: #$result .= "The correct answer is ".&Apache::run::run("{$$parstack['-1'];".'return $answer}',$safeeval)."\n";
74: }
75: #Apache::lonxml::debug("\n<br>\nreal end caparesponse<br>\n");
76: &Apache::response::end_response;
77: return '';
78: }
79:
80: sub start_numericalresponse {
81: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
82: my $id = &Apache::response::start_response($parstack,$safeeval);
83: my $result;
84: if ($target eq 'edit') {
85: $result.=&Apache::edit::tag_start($target,$token);
86: $result.=&Apache::edit::text_arg('Answer:','answer',$token);
87: if ($token->[1] eq 'numericalresponse') {
88: $result.=&Apache::edit::text_arg('Unit:','unit',$token,5);
89: $result.=&Apache::edit::text_arg('Format:','format',$token,4);
90: } elsif ($token->[1] eq 'stringresponse') {
91: $result.=&Apache::edit::select_arg('Type:','type',
92: ['cs','ci','mc'],$token);
93: } elsif ($token->[1] eq 'formularesponse') {
94: $result.=&Apache::edit::text_arg('Sample Points:','samples',$token,40);
95: }
96: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
97: } elsif ($target eq 'modified') {
98: my $constructtag;
99: if ($token->[1] eq 'numericalresponse') {
100: $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
101: 'answer','unit','format');
102: } elsif ($token->[1] eq 'stringresponse') {
103: $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
104: 'answer','type');
105: } elsif ($token->[1] eq 'formularesponse') {
106: $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
107: 'answer','samples');
108: }
109: if ($constructtag) {
110: $result = &Apache::edit::rebuild_tag($token);
111: $result.=&Apache::edit::handle_insert();
112: }
113: } elsif ($target eq 'meta') {
114: $result=&Apache::response::meta_package_write('numericalresponse');
115: }
116: return $result;
117: }
118:
119: sub end_numericalresponse {
120: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
121: my $result = '';
122: if ( $target eq 'grade' ) {
123: if ( defined $ENV{'form.submitted'}) {
124: &Apache::response::setup_params($$tagstack[-1]);
125: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
126: my $partid = $Apache::inputtags::part;
127: my $id = $Apache::inputtags::response['-1'];
128: my $response = $ENV{'form.HWVAL'.$id};
129: if ( $response =~ /[^\s]/) {
130: my %previous = &Apache::response::check_for_previous($response,$partid,$id);
131: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
132: &Apache::lonxml::debug("submitted a $response<br>\n");
133: &Apache::lonxml::debug($$parstack[$#$parstack] . "\n<br>");
134: $response =~ s/\\/\\\\/g;
135: $response =~ s/\'/\\\'/g;
136: &Apache::lonxml::debug("current $response");
137: my $expression="&caparesponse_check_list('".$response."','".
138: $$parstack[$#$parstack];
139: foreach my $key (keys(%Apache::inputtags::params)) {
140: $expression.= ';my $'. #'
141: $key.'="'.$Apache::inputtags::params{$key}.'"';
142: }
143: if ($$tagstack[-1] eq 'formularesponse') {
144: $expression.=';my $type="fml";';
145: } elsif ($$tagstack[-1] eq 'numericalresponse') {
146: $expression.=';my $type="float";';
147: }
148: $expression.="');";
149: $result = &Apache::run::run($expression,$safeeval);
150: my ($awards) = split /:/ , $result;
151: my ($ad) = &Apache::inputtags::finalizeawards(split /,/ , $awards);
152: &Apache::response::handle_previous(\%previous,$ad);
153: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
154: &Apache::lonxml::debug("$expression");
155: &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
156: $result='';
157: }
158: }
159: } elsif ($target eq 'web') {
160: my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
161: my $status = $Apache::inputtags::status['-1'];
162: if ($award =~ /^correct/ || $status eq "SHOW_ANSWER" ) {
163: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
164: $safeeval);
165: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,
166: $safeeval);
167: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval);
168:
169: $result="<br />The computer got ";
170:
171: for (my $i=0; $i <= $#answers; $i++) {
172: my $answer=$answers[$i];
173: my $format;
174: if ($#formats > 0) {
175: $format=$formats[$i];
176: } else {
177: $format=$formats[0];
178: }
179: if ((defined($format)) && ($format ne '')) {
180: &Apache::lonxml::debug("formatting with :$format: answer :$answer:");
181: $result.=sprintf('%.'.$format,$answer).',';
182: } else {
183: &Apache::lonxml::debug("no format answer :$answer:");
184: $result.="$answer,";
185: }
186: }
187: chop $result;
188: $result.=" $unit.<br></br>";
189: }
190: } elsif ($target eq 'edit') {
191: $result.='</td></tr>'.&Apache::edit::end_table;
192: } elsif ($target eq 'answer') {
193:
194: &Apache::response::setup_params($$tagstack[-1]);
195: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
196: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
197: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval);
198: my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
199:
200: $result.=&Apache::response::answer_header($$tagstack[-1]);
201: for(my $i=0;$i<=$#answers;$i++) {
202: my $ans=$answers[$i];
203: my $fmt;
204: if (@formats) {
205: if ($#formats) {
206: $fmt=$formats[$i];
207: } else {
208: $fmt=$formats[0];
209: }
210: }
211: my ($high,$low);
212: if ($Apache::inputtags::params{'tol'}) {
213: ($high,$low)=&get_tolrange($ans,$Apache::inputtags::params{'tol'});
214: }
215: my ($sighigh,$siglow);
216: if ($Apache::inputtags::params{'sig'}) {
217: ($sighigh,$siglow)=&get_sigrange($Apache::inputtags::params{'sig'});
218: }
219: if ($fmt) {
220: $ans = sprintf('%.'.$fmt,$ans);
221: if ($high) {
222: $high = sprintf('%.'.$fmt,$high); $low = sprintf('%.'.$fmt,$low);
223: }
224: }
225: if ($high) { $ans.=' ['.$low.','.$high.'] '; }
226: if ($sighigh) { $ans.= "Sig <i>$siglow - $sighigh</i>"; }
227: $result.=&Apache::response::answer_part($$tagstack[-1],$ans);
228: }
229: if ($unit) {
230: $result.=&Apache::response::answer_part($$tagstack[-1],"Unit: <b>$unit</b>");
231: }
232: if ($type || $token->[1] eq 'stringresponse') {
233: my $string='Case Insensitive';
234: if ($type eq 'mc') {
235: $string='Multiple Choice';
236: } elsif ($type eq 'cs') {
237: $string='Case Sensitive';
238: } elsif ($type eq 'ci') {
239: $string='Case Insensitive';
240: } elsif ($type eq 'fml') {
241: $string='Formula';
242: }
243: $result.=&Apache::response::answer_part($$tagstack[-1],
244: '<b>'.$string.'</b>');
245: }
246: if ($$tagstack[-1] eq 'formularesponse') {
247: my $samples=&Apache::lonxml::get_param('samples',$parstack,$safeeval);
248: $result.=&Apache::response::answer_part($$tagstack[-1],$samples);
249: }
250: $result.=&Apache::response::answer_footer($$tagstack[-1]);
251: }
252: &Apache::response::end_response;
253: return $result;
254: }
255:
256: sub get_tolrange {
257: my ($ans,$tol)=@_;
258: my ($high,$low);
259: if ($tol =~ /%$/) {
260: chop($tol);
261: my $change=$ans*($tol/100.0);
262: $high=$ans+$change;
263: $low=$ans-$change;
264: } else {
265: $high=$ans+$tol;
266: $low=$ans-$tol;
267: }
268: return ($high,$low);
269: }
270:
271: sub get_sigrange {
272: my ($sig)=@_;
273: &Apache::lonxml::debug("Got a sig of :$sig:");
274: my $sig_lbound;
275: my $sig_ubound;
276: if ($sig eq '') {
277: $sig_lbound = 0; #SIG_LB_DEFAULT
278: $sig_ubound =15; #SIG_UB_DEFAULT
279: } else {
280: ($sig_lbound,$sig_ubound) = split(/,/,$sig);
281: if (!$sig_lbound) {
282: $sig_lbound = 0; #SIG_LB_DEFAULT
283: $sig_ubound =15; #SIG_UB_DEFAULT
284: }
285: if (!$sig_ubound) { $sig_ubound=$sig_lbound; }
286: }
287: return ($sig_ubound,$sig_lbound);
288: }
289:
290: sub start_stringresponse {
291: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
292: my $result;
293: if ($target eq 'meta') {
294: $result=&Apache::response::meta_package_write('stringresponse');
295: } else {
296: $result.=&start_numericalresponse(@_);
297: }
298: return $result;
299: }
300:
301: sub end_stringresponse {
302: return end_numericalresponse(@_);
303: }
304:
305: sub start_formularesponse {
306: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
307: my $result;
308: if ($target eq 'meta') {
309: $result=&Apache::response::meta_package_write('formularesponse');
310: } else {
311: $result.=&start_numericalresponse(@_);
312: }
313: return $result;
314: }
315:
316: sub end_formularesponse {
317: return end_numericalresponse(@_);
318: }
319:
320: 1;
321: __END__
322:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>