Annotation of loncom/homework/caparesponse/caparesponse.pm, revision 1.10
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 {
9: &Apache::lonxml::register('Apache::caparesponse',('caparesponse'));
10: }
11:
1.7 albertel 12: $Apache::caparesponse::result='';
1.3 albertel 13: sub start_caparesponse {
1.7 albertel 14: my $envkey;
15:
1.8 albertel 16: #foreach $envkey (sort keys %ENV) {
17: # Apache::lonxml::debug("$envkey ---- $ENV{$envkey}<br>");
18: #}
1.7 albertel 19:
1.8 albertel 20: #Apache::lonxml::debug("\n<br>\nreal caparesponse<br>\n");
1.3 albertel 21: }
22:
23: sub end_caparesponse {
1.7 albertel 24: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
25: if ( defined $ENV{'form.'.$ENV{'form.request.prefix'}.'submit'}) {
26: $Apache::caparesponse::result='';
27: $safeeval->share('&caparesponse_capa_check_answer');
28: $safeeval->share('$Apache::caparesponse::result');
29: my $response = $ENV{'form.'.$ENV{'form.request.prefix'}.'HWVAL'.$Apache::inputtags::curvalue};
1.9 albertel 30: #Apache::lonxml::debug("submitted a $response<br>\n");
31: #Apache::lonxml::debug($$parstack[$#$parstack] . "\n<br>");
1.7 albertel 32: my $expression="caparesponse_check $response $$parstack[$#$parstack];";
1.9 albertel 33: #&Apache::run::run($expression,$safeeval);
1.7 albertel 34:
1.9 albertel 35: #&Apache::lonxml::debug("\n<br>result:$Apache::caparesponse::result:<br>\n");
36: my $args ='';
37: if ( $#$parstack > -1 ) {
38: $args=$$parstack[$#$parstack];
39: }
40: my $answer = &Apache::run::run("{$args;".'return $answer}',$safeeval);
1.10 ! albertel 41: if ( $answer == 1.0 * $answer ) {
! 42: my $tol = &Apache::run::run("{$args;".'return $tol}',$safeeval);
! 43: my $format = &Apache::run::run("{$args;".'return $format}',$safeeval);
! 44: my $formatedanswer = sprintf('%.'.$format,$answer);
! 45: my $difference = abs($answer - $formatedanswer);
! 46: my $studiff = abs($answer - $response);
! 47: if ( $difference > $studiff ) {
! 48: return "Correct, Computer had $answer<br></br>";
! 49: } else {
! 50: return "Incorrect, Computer had $answer<br></br>";
! 51: }
! 52: } else {
! 53: if ( $answer eq $response ) {
! 54: return "Correct, Computer had $answer<br></br>";
! 55: } else {
! 56: return "Incorrect, Computer had $answer<br></br>";
! 57: }
1.9 albertel 58: }
1.8 albertel 59: } else {
60: #return "The correct answer is ".&Apache::run::run("{$$parstack['-1'];".'return $answer}',$safeeval)."\n";
1.7 albertel 61: }
62: #Apache::lonxml::debug("\n<br>\nreal end caparesponse<br>\n");
1.8 albertel 63: return '';
1.3 albertel 64: }
65:
1.1 albertel 66: 1;
1.3 albertel 67: __END__
68:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>