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