Annotation of loncom/homework/caparesponse/caparesponse.pm, revision 1.47
1.3 albertel 1: # The LearningOnline Network with CAPA
2: # caparesponse definition
1.47 ! albertel 3: #
! 4: # $Id: gplheader.pl,v 1.1 2001/11/29 18:19:27 www 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: #
1.3 albertel 28:
29: package Apache::caparesponse;
30: use strict;
1.4 albertel 31: use capa;
1.3 albertel 32:
33: sub BEGIN {
1.44 albertel 34: &Apache::lonxml::register('Apache::caparesponse',('caparesponse','numericalresponse','stringresponse','formularesponse'));
1.3 albertel 35: }
36:
37: sub start_caparesponse {
1.31 albertel 38: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.19 albertel 39: my $id = &Apache::response::start_response($parstack,$safeeval);
1.13 albertel 40: return '';
1.3 albertel 41: }
42:
43: sub end_caparesponse {
1.31 albertel 44: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.12 albertel 45: my $result = '';
1.16 albertel 46: if ( $target eq 'grade' ) {
1.27 albertel 47: if ( defined $ENV{'form.submitted'}) {
1.16 albertel 48: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
1.19 albertel 49: my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']};
1.18 albertel 50: if ( $response =~ /[^\s]/) {
1.23 albertel 51: my $id = $Apache::inputtags::response['-1'];
52: my $previous= &Apache::response::check_for_previous($response,$id,$response);
1.22 albertel 53: if ( $previous ) {
54: $result = 'PREVIOUSLY_USED';
55: } else {
1.26 albertel 56: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$response;
1.22 albertel 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;
1.26 albertel 66: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}=$ad;
1.22 albertel 67: &Apache::lonxml::debug("$expression");
68: &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
69: }
1.18 albertel 70: }
1.16 albertel 71: }
1.30 albertel 72: } elsif ($target eq 'web') {
1.17 albertel 73: #$result .= "The correct answer is ".&Apache::run::run("{$$parstack['-1'];".'return $answer}',$safeeval)."\n";
1.7 albertel 74: }
75: #Apache::lonxml::debug("\n<br>\nreal end caparesponse<br>\n");
1.19 albertel 76: &Apache::response::end_response;
1.24 albertel 77: return '';
1.21 albertel 78: }
79:
80: sub start_numericalresponse {
1.31 albertel 81: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.22 albertel 82: my $id = &Apache::response::start_response($parstack,$safeeval);
1.32 albertel 83: my $result;
84: if ($target eq 'edit') {
85: $result.=&Apache::edit::tag_start($target,$token,&Apache::lonxml::description($token));
1.34 albertel 86: $result.=&Apache::edit::text_arg('Answer:','answer',$token);
87: if ($token->[1] eq 'numericalresponse') {
1.36 albertel 88: $result.=&Apache::edit::text_arg('Unit:','unit',$token,5);
89: $result.=&Apache::edit::text_arg('Format:','format',$token,4);
1.44 albertel 90: } elsif ($token->[1] eq 'stringresponse') {
1.35 albertel 91: $result.=&Apache::edit::select_arg('Type:','type',
92: ['cs','ci','mc'],$token)
1.44 albertel 93: } elsif ($token->[1] eq 'formularesponse') {
1.45 albertel 94: $result.=&Apache::edit::text_arg('Sample Points:','samples',$token,40);
1.34 albertel 95: }
96: $result.='</td></tr><tr><td colspan="3">';
1.32 albertel 97: } elsif ($target eq 'modified') {
1.34 albertel 98: my $constructtag;
99: if ($token->[1] eq 'numericalresponse') {
100: $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
1.36 albertel 101: 'answer','unit','format');
1.44 albertel 102: } elsif ($token->[1] eq 'stringresponse') {
1.34 albertel 103: $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
104: 'answer','type');
1.44 albertel 105: } elsif ($token->[1] eq 'formularesponse') {
106: $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
1.45 albertel 107: 'answer','samples');
1.34 albertel 108: }
1.33 albertel 109: if ($constructtag) {
110: $result = &Apache::edit::rebuild_tag($token);
111: $result.=&Apache::edit::handle_insert();
112: }
1.39 albertel 113: } elsif ($target eq 'meta') {
1.40 albertel 114: $result=&Apache::response::meta_package_write('numericalresponse');
1.32 albertel 115: }
116: return $result;
1.21 albertel 117: }
118:
119: sub end_numericalresponse {
1.31 albertel 120: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.22 albertel 121: my $result = '';
122: if ( $target eq 'grade' ) {
1.27 albertel 123: if ( defined $ENV{'form.submitted'}) {
1.45 albertel 124: &Apache::response::setup_params($$tagstack[-1]);
1.22 albertel 125: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
1.38 albertel 126: my $partid = $Apache::inputtags::part;
127: my $id = $Apache::inputtags::response['-1'];
128: my $response = $ENV{'form.HWVAL'.$id};
1.22 albertel 129: if ( $response =~ /[^\s]/) {
1.38 albertel 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."','".
1.41 albertel 138: $$parstack[$#$parstack];
139: foreach my $key (keys(%Apache::inputtags::params)) {
140: $expression.= ';my $'. #'
141: $key.'="'.$Apache::inputtags::params{$key}.'"';
142: }
1.45 albertel 143: if ($$tagstack[-1] eq 'formularesponse') {
1.46 albertel 144: $expression.=';my $type="fml";';
145: } elsif ($$tagstack[-1] eq 'numericalresponse') {
146: $expression.=';my $type="float";';
1.45 albertel 147: }
1.41 albertel 148: $expression.="');";
1.38 albertel 149: $result = &Apache::run::run($expression,$safeeval);
150: my ($awards) = split /:/ , $result;
151: my ($ad) = &Apache::inputtags::finalizeawards(split /,/ , $awards);
152: if ( $previous{'used'} && ($previous{'award'} eq $ad) ) {
153: if ($previous{'last'}) {
154: push(@Apache::inputtags::previous,'PREVIOUSLY_LAST');
155: } else {
156: push(@Apache::inputtags::previous,'PREVIOUSLY_USED');
157: }
1.22 albertel 158: }
1.38 albertel 159: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
160: &Apache::lonxml::debug("$expression");
161: &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
162: $result='';
1.22 albertel 163: }
1.24 albertel 164: }
165: } elsif ($target eq 'web') {
166: my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
1.28 albertel 167: my $status = $Apache::inputtags::status['-1'];
168: if ($award =~ /^correct/ || $status eq "SHOW_ANSWER" ) {
1.44 albertel 169: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
170: $safeeval);
171: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,
172: $safeeval);
173: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval);
1.36 albertel 174:
1.45 albertel 175: $result="<br />The computer got ";
1.36 albertel 176:
177: for (my $i=0; $i <= $#answers; $i++) {
178: my $answer=$answers[$i];
179: my $format;
180: if ($#formats > 0) {
181: $format=$formats[$i];
182: } else {
183: $format=$formats[0];
184: }
1.37 albertel 185: if ((defined($format)) && ($format ne '')) {
1.36 albertel 186: &Apache::lonxml::debug("formatting with :$format: answer :$answer:");
187: $result.=sprintf('%.'.$format,$answer).',';
188: } else {
189: &Apache::lonxml::debug("no format answer :$answer:");
190: $result.="$answer,";
191: }
192: }
1.24 albertel 193: chop $result;
194: $result.=" $unit.<br></br>";
1.22 albertel 195: }
1.32 albertel 196: } elsif ($target eq 'edit') {
197: $result.='</td></tr>'.&Apache::edit::end_table;
1.43 albertel 198: } elsif ($target eq 'answer') {
1.44 albertel 199:
200: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
201: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
202: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval);
203: my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
204:
1.45 albertel 205: $result.=&Apache::response::answer_header($$tagstack[-1]);
1.44 albertel 206: for(my $i=0;$i<=$#answers;$i++) {
207: my $ans=$answers[$i];
208: my $fmt;
209: if (@formats) {
210: if ($#formats) {
211: $fmt=$formats[$i];
212: } else {
213: $fmt=$formats[0];
214: }
215: }
216: if ($fmt) { $ans = sprintf('%.'.$fmt,$ans); }
1.45 albertel 217: $result.=&Apache::response::answer_part($$tagstack[-1],$ans);
1.43 albertel 218: }
1.44 albertel 219: if ($unit) {
1.45 albertel 220: $result.=&Apache::response::answer_part($$tagstack[-1],
1.44 albertel 221: '<b>'.$unit.'</b>');
222: }
223: if ($type || $token->[1] eq 'stringresponse') {
224: my $string='Case Insensitive';
225: if ($type eq 'mc') {
226: $string='Multiple Choice';
227: } elsif ($type eq 'cs') {
228: $string='Case Sensitive';
229: } elsif ($type eq 'ci') {
230: $string='Case Insensitive';
1.45 albertel 231: } elsif ($type eq 'fml') {
232: $string='Formula';
1.44 albertel 233: }
1.45 albertel 234: $result.=&Apache::response::answer_part($$tagstack[-1],
1.44 albertel 235: '<b>'.$string.'</b>');
236: }
1.45 albertel 237: if ($$tagstack[-1] eq 'formularesponse') {
238: my $samples=&Apache::lonxml::get_param('samples',$parstack,$safeeval);
239: $result.=&Apache::response::answer_part($$tagstack[-1],$samples);
240: }
241: $result.=&Apache::response::answer_footer($$tagstack[-1]);
1.22 albertel 242: }
243: &Apache::response::end_response;
244: return $result;
1.34 albertel 245: }
246:
247: sub start_stringresponse {
1.42 albertel 248: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
249: my $result;
250: if ($target eq 'meta') {
251: $result=&Apache::response::meta_package_write('stringresponse');
252: } else {
253: $result.=&start_numericalresponse(@_);
254: }
255: return $result;
1.34 albertel 256: }
257:
258: sub end_stringresponse {
1.44 albertel 259: return end_numericalresponse(@_);
260: }
261:
262: sub start_formularesponse {
263: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
264: my $result;
265: if ($target eq 'meta') {
266: $result=&Apache::response::meta_package_write('formularesponse');
267: } else {
268: $result.=&start_numericalresponse(@_);
269: }
270: return $result;
271: }
272:
273: sub end_formularesponse {
1.34 albertel 274: return end_numericalresponse(@_);
1.3 albertel 275: }
276:
1.1 albertel 277: 1;
1.3 albertel 278: __END__
279:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>