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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # external style responses
                      3: #
1.9     ! albertel    4: # $Id: externalresponse.pm,v 1.8 2007/09/11 19:07:01 albertel 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.1       albertel   29: package Apache::externalresponse;
                     30: use strict;
                     31: use HTTP::Request::Common;
                     32: use Apache::loncapagrade;
1.4       albertel   33: use Apache::lonnet;
1.1       albertel   34: 
                     35: BEGIN {
1.5       albertel   36:     &Apache::lonxml::register('Apache::externalresponse',('externalresponse'));
1.1       albertel   37: }
                     38: 
                     39: #FIXME
                     40: # send of response params and there current values
                     41: # accept a string to be printed out (Where does it get shown?) (.message)
                     42: # need to get a correct answer from somewhere (probably here since randomized)
                     43: sub start_externalresponse {
1.5       albertel   44:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     45:     my $result;
                     46:     my $id = &Apache::response::start_response($parstack,$safeeval);
                     47:     return $result;
1.1       albertel   48: }
                     49: 
                     50: sub end_externalresponse {
1.5       albertel   51:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     52:     my $result;
1.6       albertel   53:     my $part=$Apache::inputtags::part;
                     54:     my $id = $Apache::inputtags::response['-1'];
                     55:     my $increment     = 1;
1.5       albertel   56:     if ($target eq 'grade') {
1.6       albertel   57: 	if (  &Apache::response::submitted('scantron') ) {
                     58: 	    my $increment=&Apache::response::scored_response($part,$id);
                     59: 	} elsif ( &Apache::response::submitted() ) {
                     60: 	    my $response = &Apache::response::getresponse();
1.5       albertel   61: 	    if ( $response =~ /[^\s]/) {
                     62: 		my $url = &Apache::lonxml::get_param('url',$parstack,$safeeval);
                     63: 		my $answer = &Apache::lonxml::get_param('answer',$parstack,$safeeval);
                     64: 		my %form = &Apache::lonxml::get_param_var('form',$parstack,$safeeval);
                     65: 		$form{'LONCAPA_student_response'}=$response;
                     66: 		$form{'LONCAPA_correct_answer'}=$answer;
                     67: 		$form{'LONCAPA_language'}=
                     68: 		    &Apache::lonnet::metadata($ENV{'REQUEST_URI'},'language');
                     69: 		&Apache::lonxml::debug("Asking $url, with:");
                     70: 		&Apache::lonhomework::showhash(%form);
                     71: 		my $ua = LWP::UserAgent->new;
                     72: 		my $res = $ua->request(POST $url, \%form);
                     73: 		my %previous = &Apache::response::check_for_previous($response,
1.6       albertel   74: 								     $part,$id);
1.5       albertel   75: 		%Apache::loncapagrade::results=();
1.6       albertel   76: 		$Apache::lonhomework::results{"resource.$part.$id.submission"}=$response;
1.9     ! albertel   77: 		if ($res->is_error()) {
1.6       albertel   78: 		    $Apache::loncapagrade::results{'awarddetail'}='ERROR';
1.5       albertel   79: 		} else {
1.6       albertel   80: 		    &Apache::lonxml::register('Apache::loncapagrade',
                     81: 					      ('loncapagrade'));
1.8       albertel   82: 		    @Apache::scripttag::parser_env = @_;
1.5       albertel   83: 		    my $result=&Apache::scripttag::xmlparse($res->{_content});
                     84: 		    &Apache::lonxml::debug("Got a result of :$result:");
                     85: 		}
1.6       albertel   86: 		foreach my $key (keys(%Apache::loncapagrade::results)) {
                     87: 		    $Apache::lonhomework::results{"resource.$part.$id.$key"}=
1.5       albertel   88: 			$Apache::loncapagrade::results{$key};
                     89: 		}
                     90: 		&Apache::response::handle_previous(\%previous,
                     91: 						   $Apache::loncapagrade::results{'awarddetail'});
                     92: 		&Apache::lonxml::debug("response of");
                     93: 		&Apache::lonhomework::showhash(%$res);
1.6       albertel   94: 		&Apache::lonxml::debug("capagrade of");
1.5       albertel   95: 		&Apache::lonhomework::showhash(%Apache::loncapagrade::results);
1.6       albertel   96: 		&Apache::lonxml::debug("results of");
                     97: 		&Apache::lonhomework::showhash(%Apache::lonhomework::results);
1.5       albertel   98: 	    }
1.1       albertel   99: 	}
                    100:     }
1.7       albertel  101:     if ($target eq 'web') {
                    102: 	&Apache::response::setup_prior_tries_hash(\&format_prior_response);
                    103:     }
                    104: 
1.6       albertel  105:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
                    106: 	$target eq 'tex' || $target eq 'analyze') {
                    107: 	&Apache::lonxml::increment_counter($increment);
                    108:     }
                    109:     &Apache::response::end_response();
1.5       albertel  110:     return $result;
1.1       albertel  111: }
                    112: 
1.7       albertel  113: sub format_prior_response {
                    114:     my ($mode,$answer) =@_;
                    115:     return '<span class="LC_prior_external">'.
                    116: 	    &HTML::Entities::encode($answer,'"<>&').'</span>';
                    117: }
                    118: 
1.1       albertel  119: 1;
                    120: __END__
                    121: 
                    122: 

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