Annotation of loncom/homework/caparesponse/caparesponse.pm, revision 1.43
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.34 albertel 9: &Apache::lonxml::register('Apache::caparesponse',('caparesponse','numericalresponse','stringresponse'));
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.34 albertel 65: } else {
1.35 albertel 66: $result.=&Apache::edit::select_arg('Type:','type',
67: ['cs','ci','mc'],$token)
1.34 albertel 68: }
69: $result.='</td></tr><tr><td colspan="3">';
1.32 albertel 70: } elsif ($target eq 'modified') {
1.34 albertel 71: my $constructtag;
72: if ($token->[1] eq 'numericalresponse') {
73: $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
1.36 albertel 74: 'answer','unit','format');
1.34 albertel 75: } else {
76: $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
77: 'answer','type');
78: }
1.33 albertel 79: if ($constructtag) {
80: $result = &Apache::edit::rebuild_tag($token);
81: $result.=&Apache::edit::handle_insert();
82: }
1.39 albertel 83: } elsif ($target eq 'meta') {
1.40 albertel 84: $result=&Apache::response::meta_package_write('numericalresponse');
1.32 albertel 85: }
86: return $result;
1.21 albertel 87: }
88:
89: sub end_numericalresponse {
1.31 albertel 90: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.22 albertel 91: my $result = '';
92: if ( $target eq 'grade' ) {
1.27 albertel 93: if ( defined $ENV{'form.submitted'}) {
1.41 albertel 94: &Apache::response::setup_params('numericalresponse');
1.22 albertel 95: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
1.38 albertel 96: my $partid = $Apache::inputtags::part;
97: my $id = $Apache::inputtags::response['-1'];
98: my $response = $ENV{'form.HWVAL'.$id};
1.22 albertel 99: if ( $response =~ /[^\s]/) {
1.38 albertel 100: my %previous = &Apache::response::check_for_previous($response,$partid,$id);
101: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
102: &Apache::lonxml::debug("submitted a $response<br>\n");
103: &Apache::lonxml::debug($$parstack[$#$parstack] . "\n<br>");
104: $response =~ s/\\/\\\\/g;
105: $response =~ s/\'/\\\'/g;
106: &Apache::lonxml::debug("current $response");
107: my $expression="&caparesponse_check_list('".$response."','".
1.41 albertel 108: $$parstack[$#$parstack];
109: foreach my $key (keys(%Apache::inputtags::params)) {
110: $expression.= ';my $'. #'
111: $key.'="'.$Apache::inputtags::params{$key}.'"';
112: }
113: $expression.="');";
1.38 albertel 114: $result = &Apache::run::run($expression,$safeeval);
115: my ($awards) = split /:/ , $result;
116: my ($ad) = &Apache::inputtags::finalizeawards(split /,/ , $awards);
117: if ( $previous{'used'} && ($previous{'award'} eq $ad) ) {
118: if ($previous{'last'}) {
119: push(@Apache::inputtags::previous,'PREVIOUSLY_LAST');
120: } else {
121: push(@Apache::inputtags::previous,'PREVIOUSLY_USED');
122: }
1.22 albertel 123: }
1.38 albertel 124: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
125: &Apache::lonxml::debug("$expression");
126: &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
127: $result='';
1.22 albertel 128: }
1.24 albertel 129: }
130: } elsif ($target eq 'web') {
131: my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
1.28 albertel 132: my $status = $Apache::inputtags::status['-1'];
133: if ($award =~ /^correct/ || $status eq "SHOW_ANSWER" ) {
1.36 albertel 134: my $answervar=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
135: my (@answers)=&Apache::run::run("return $answervar",$safeeval,1);
136: if (!defined($answers[0])) { $answers[0]=$answervar; } else {
137: &Apache::lonxml::debug("answers is $#answers :$answers[0]:");
138: }
139: my $formatvar=&Apache::lonxml::get_param('format',$parstack,$safeeval);
140: my (@formats)=&Apache::run::run("return $formatvar",$safeeval,1);
141: if (!defined($formats[0])) { $formats[0]=$formatvar; }
142:
143: my $unit=&Apache::lonxml::get_param('unit',$parstack,$safeeval);
144:
1.24 albertel 145: $result="<br></br>The computer got ";
1.36 albertel 146:
147: for (my $i=0; $i <= $#answers; $i++) {
148: my $answer=$answers[$i];
149: my $format;
150: if ($#formats > 0) {
151: $format=$formats[$i];
152: } else {
153: $format=$formats[0];
154: }
1.37 albertel 155: if ((defined($format)) && ($format ne '')) {
1.36 albertel 156: &Apache::lonxml::debug("formatting with :$format: answer :$answer:");
157: $result.=sprintf('%.'.$format,$answer).',';
158: } else {
159: &Apache::lonxml::debug("no format answer :$answer:");
160: $result.="$answer,";
161: }
162: }
1.24 albertel 163: chop $result;
164: $result.=" $unit.<br></br>";
1.22 albertel 165: }
1.32 albertel 166: } elsif ($target eq 'edit') {
167: $result.='</td></tr>'.&Apache::edit::end_table;
1.43 ! albertel 168: } elsif ($target eq 'answer') {
! 169: my $answer= &Apache::run::run('eval \''.$$parstack[-1].';return $answer\'',
! 170: $safeeval);
! 171: my (@answers) = &Apache::run::run("eval ".$answer,$safeeval);
! 172: # if the eval fails just use what is in the answer exactly
! 173: if (!defined(@answers) || !defined($answers[0])) {
! 174: $result.="list zero is undefined<br />";
! 175: $answers[0]=$answer;
! 176: }
! 177: $result.="<br /> An answers: $answer";
! 178: foreach my $ans (@answers) {
! 179: $result.="<br /> An answer: $ans";
! 180: }
! 181: $result .= "<br />".&Apache::run::run('return $result',$safeeval);
1.22 albertel 182: }
183: &Apache::response::end_response;
184: return $result;
1.34 albertel 185: }
186:
187: sub start_stringresponse {
1.42 albertel 188: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
189: my $result;
190: if ($target eq 'meta') {
191: $result=&Apache::response::meta_package_write('stringresponse');
192: } else {
193: $result.=&start_numericalresponse(@_);
194: }
195: return $result;
1.34 albertel 196: }
197:
198: sub end_stringresponse {
199: return end_numericalresponse(@_);
1.3 albertel 200: }
201:
1.1 albertel 202: 1;
1.3 albertel 203: __END__
204:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>