Annotation of loncom/homework/caparesponse/caparesponse.pm, revision 1.35
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') {
63: $result.=&Apache::edit::text_arg('Unit:','unit',$token)
64: } else {
1.35 ! albertel 65: $result.=&Apache::edit::select_arg('Type:','type',
! 66: ['cs','ci','mc'],$token)
1.34 albertel 67: }
68: $result.='</td></tr><tr><td colspan="3">';
1.32 albertel 69: } elsif ($target eq 'modified') {
1.34 albertel 70: my $constructtag;
71: if ($token->[1] eq 'numericalresponse') {
72: $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
73: 'answer','unit');
74: } else {
75: $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
76: 'answer','type');
77: }
1.33 albertel 78: if ($constructtag) {
79: $result = &Apache::edit::rebuild_tag($token);
80: $result.=&Apache::edit::handle_insert();
81: }
1.32 albertel 82: }
83: return $result;
1.21 albertel 84: }
85:
86: sub end_numericalresponse {
1.31 albertel 87: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.22 albertel 88: my $result = '';
89: if ( $target eq 'grade' ) {
1.27 albertel 90: if ( defined $ENV{'form.submitted'}) {
1.22 albertel 91: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
92: my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']};
93: if ( $response =~ /[^\s]/) {
1.23 albertel 94: my $id = $Apache::inputtags::response['-1'];
95: my $previous= &Apache::response::check_for_previous($response,$id,$response);
1.22 albertel 96: if ( $previous ) {
97: $result = 'PREVIOUSLY_USED';
98: } else {
1.26 albertel 99: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$response;
1.22 albertel 100: &Apache::lonxml::debug("submitted a $response<br>\n");
101: &Apache::lonxml::debug($$parstack[$#$parstack] . "\n<br>");
1.29 albertel 102: $response =~ s/\\/\\\\/g;
103: $response =~ s/\'/\\\'/g;
104: &Apache::lonxml::debug("current $response");
1.23 albertel 105: my $expression="&caparesponse_check_list('".$response."','".
1.22 albertel 106: $$parstack[$#$parstack].
107: ';my $tol="'.$Apache::inputtags::params{'tol'}.'"'.
108: ';my $sig="'.$Apache::inputtags::params{'sig'}.'"'.
109: "');";
110: $result = &Apache::run::run($expression,$safeeval);
1.23 albertel 111: my ($awards) = split /:/ , $result;
112: my ($ad) = &Apache::inputtags::finalizeawards(split /,/ , $awards);
1.26 albertel 113: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}=$ad;
1.22 albertel 114: &Apache::lonxml::debug("$expression");
1.25 albertel 115: &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
1.24 albertel 116: $result='';
1.22 albertel 117: }
118: }
1.24 albertel 119: }
120: } elsif ($target eq 'web') {
121: my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
1.28 albertel 122: my $status = $Apache::inputtags::status['-1'];
123: if ($award =~ /^correct/ || $status eq "SHOW_ANSWER" ) {
1.24 albertel 124: my $answervar=&Apache::run::run("{$$parstack['-1'];".'return $answer}',$safeeval);
125: my (@answers)=&Apache::run::run("$answervar",$safeeval);
126: my $unit=&Apache::run::run("{$$parstack['-1'];".'return $unit}',$safeeval);
127:
128: $result="<br></br>The computer got ";
129: my $answer;
130: foreach $answer (@answers) { $result.="$answer,"; }
131: chop $result;
132: $result.=" $unit.<br></br>";
1.22 albertel 133: }
1.32 albertel 134: } elsif ($target eq 'edit') {
135: $result.='</td></tr>'.&Apache::edit::end_table;
1.22 albertel 136: }
137: &Apache::response::end_response;
138: return $result;
1.34 albertel 139: }
140:
141: sub start_stringresponse {
142: return start_numericalresponse(@_);
143: }
144:
145: sub end_stringresponse {
146: return end_numericalresponse(@_);
1.3 albertel 147: }
148:
1.1 albertel 149: 1;
1.3 albertel 150: __END__
151:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>