Annotation of loncom/homework/externalresponse.pm, revision 1.14

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # external style responses
                      3: #
1.14    ! jms         4: # $Id: externalresponse.pm,v 1.13 2008/02/01 22:05:45 raeburn Exp $
1.1       albertel    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.6       albertel   28: 
1.14    ! jms        29: =pod
        !            30: 
        !            31: =head1 NAME
        !            32: 
        !            33: Apache::externalresponse.pm
        !            34: 
        !            35: =head1 SYNOPSIS
        !            36: 
        !            37: Handler to evaluate externally graded responses.
        !            38: 
        !            39: This is part of the LearningOnline Network with CAPA project
        !            40: described at http://www.lon-capa.org.
        !            41: 
        !            42: 
        !            43: =cut
        !            44: 
1.1       albertel   45: package Apache::externalresponse;
                     46: use strict;
                     47: use HTTP::Request::Common;
                     48: use Apache::loncapagrade;
1.4       albertel   49: use Apache::lonnet;
1.1       albertel   50: 
                     51: BEGIN {
1.5       albertel   52:     &Apache::lonxml::register('Apache::externalresponse',('externalresponse'));
1.1       albertel   53: }
                     54: 
1.14    ! jms        55: =pod
        !            56: 
        !            57: =head1 FIXME
        !            58: 
        !            59: send of response params and their current values (form good enough? what parameters to send?)
        !            60: Need to get returned message displayed
        !            61: 
        !            62: =cut
        !            63: 
        !            64: 
1.1       albertel   65: sub start_externalresponse {
1.5       albertel   66:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     67:     my $result;
                     68:     my $id = &Apache::response::start_response($parstack,$safeeval);
1.10      albertel   69: 
                     70:     if ($target eq 'edit') {
                     71:   	$result .=&Apache::edit::tag_start($target,$token);
                     72: 	$result .=&Apache::edit::text_arg('URL:','url',$token,60).'<br />';
                     73: 	$result .=&Apache::edit::text_arg('Answer:','answer',$token);
                     74: 	$result .=&Apache::edit::text_arg('Form:','form',$token);
                     75: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                     76:     } elsif ($target eq 'modified') {
                     77: 	my $constructtag =
                     78: 	    &Apache::edit::get_new_args($token,$parstack,$safeeval,
                     79: 					'answer','form','url');
                     80: 	if ($constructtag) {
                     81: 	    $result = &Apache::edit::rebuild_tag($token);
                     82: 	}
                     83:     } elsif ($target eq 'meta') {
                     84:   	$result=&Apache::response::meta_package_write('externalresponse');
                     85:     }
                     86:     
1.5       albertel   87:     return $result;
1.1       albertel   88: }
                     89: 
                     90: sub end_externalresponse {
1.5       albertel   91:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     92:     my $result;
1.6       albertel   93:     my $part=$Apache::inputtags::part;
                     94:     my $id = $Apache::inputtags::response['-1'];
                     95:     my $increment     = 1;
1.5       albertel   96:     if ($target eq 'grade') {
1.6       albertel   97: 	if (  &Apache::response::submitted('scantron') ) {
                     98: 	    my $increment=&Apache::response::scored_response($part,$id);
                     99: 	} elsif ( &Apache::response::submitted() ) {
                    100: 	    my $response = &Apache::response::getresponse();
1.5       albertel  101: 	    if ( $response =~ /[^\s]/) {
                    102: 		my $url = &Apache::lonxml::get_param('url',$parstack,$safeeval);
                    103: 		my $answer = &Apache::lonxml::get_param('answer',$parstack,$safeeval);
                    104: 		my %form = &Apache::lonxml::get_param_var('form',$parstack,$safeeval);
                    105: 		$form{'LONCAPA_student_response'}=$response;
                    106: 		$form{'LONCAPA_correct_answer'}=$answer;
                    107: 		$form{'LONCAPA_language'}=
                    108: 		    &Apache::lonnet::metadata($ENV{'REQUEST_URI'},'language');
                    109: 		&Apache::lonxml::debug("Asking $url, with:");
                    110: 		&Apache::lonhomework::showhash(%form);
                    111: 		my $ua = LWP::UserAgent->new;
                    112: 		my $res = $ua->request(POST $url, \%form);
                    113: 		my %previous = &Apache::response::check_for_previous($response,
1.6       albertel  114: 								     $part,$id);
1.5       albertel  115: 		%Apache::loncapagrade::results=();
1.6       albertel  116: 		$Apache::lonhomework::results{"resource.$part.$id.submission"}=$response;
1.9       albertel  117: 		if ($res->is_error()) {
1.6       albertel  118: 		    $Apache::loncapagrade::results{'awarddetail'}='ERROR';
1.5       albertel  119: 		} else {
1.6       albertel  120: 		    &Apache::lonxml::register('Apache::loncapagrade',
                    121: 					      ('loncapagrade'));
1.8       albertel  122: 		    @Apache::scripttag::parser_env = @_;
1.5       albertel  123: 		    my $result=&Apache::scripttag::xmlparse($res->{_content});
                    124: 		    &Apache::lonxml::debug("Got a result of :$result:");
                    125: 		}
1.6       albertel  126: 		foreach my $key (keys(%Apache::loncapagrade::results)) {
                    127: 		    $Apache::lonhomework::results{"resource.$part.$id.$key"}=
1.5       albertel  128: 			$Apache::loncapagrade::results{$key};
                    129: 		}
                    130: 		&Apache::response::handle_previous(\%previous,
                    131: 						   $Apache::loncapagrade::results{'awarddetail'});
                    132: 		&Apache::lonxml::debug("response of");
                    133: 		&Apache::lonhomework::showhash(%$res);
1.6       albertel  134: 		&Apache::lonxml::debug("capagrade of");
1.5       albertel  135: 		&Apache::lonhomework::showhash(%Apache::loncapagrade::results);
1.6       albertel  136: 		&Apache::lonxml::debug("results of");
                    137: 		&Apache::lonhomework::showhash(%Apache::lonhomework::results);
1.5       albertel  138: 	    }
1.1       albertel  139: 	}
                    140:     }
1.7       albertel  141:     if ($target eq 'web') {
                    142: 	&Apache::response::setup_prior_tries_hash(\&format_prior_response);
                    143:     }
                    144: 
1.6       albertel  145:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
                    146: 	$target eq 'tex' || $target eq 'analyze') {
1.12      foxr      147: 	&Apache::lonxml::increment_counter($increment, "$part.$id");
1.11      foxr      148: 
                    149: 	if ($target eq 'analyze') {
1.13      raeburn   150:             $Apache::lonhomework::analyze{"$part.$id.type"} = 'externalresponse';
1.11      foxr      151: 	    &Apache::lonhomework::set_bubble_lines();
                    152: 	}
1.6       albertel  153:     }
                    154:     &Apache::response::end_response();
1.5       albertel  155:     return $result;
1.1       albertel  156: }
                    157: 
1.7       albertel  158: sub format_prior_response {
                    159:     my ($mode,$answer) =@_;
                    160:     return '<span class="LC_prior_external">'.
                    161: 	    &HTML::Entities::encode($answer,'"<>&').'</span>';
                    162: }
                    163: 
1.1       albertel  164: 1;
                    165: __END__
                    166: 
                    167: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>