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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # external style responses
                      3: #
1.10    ! albertel    4: # $Id: externalresponse.pm,v 1.9 2007/09/11 19:24:14 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
1.10    ! albertel   40: # send of response params and their current values (form good enough?
        !            41: # what parameters to send?)
        !            42: # Need to get returned message displayed 
1.1       albertel   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);
1.10    ! albertel   47: 
        !            48:     if ($target eq 'edit') {
        !            49:   	$result .=&Apache::edit::tag_start($target,$token);
        !            50: 	$result .=&Apache::edit::text_arg('URL:','url',$token,60).'<br />';
        !            51: 	$result .=&Apache::edit::text_arg('Answer:','answer',$token);
        !            52: 	$result .=&Apache::edit::text_arg('Form:','form',$token);
        !            53: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
        !            54:     } elsif ($target eq 'modified') {
        !            55: 	my $constructtag =
        !            56: 	    &Apache::edit::get_new_args($token,$parstack,$safeeval,
        !            57: 					'answer','form','url');
        !            58: 	if ($constructtag) {
        !            59: 	    $result = &Apache::edit::rebuild_tag($token);
        !            60: 	}
        !            61:     } elsif ($target eq 'meta') {
        !            62:   	$result=&Apache::response::meta_package_write('externalresponse');
        !            63:     }
        !            64:     
1.5       albertel   65:     return $result;
1.1       albertel   66: }
                     67: 
                     68: sub end_externalresponse {
1.5       albertel   69:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     70:     my $result;
1.6       albertel   71:     my $part=$Apache::inputtags::part;
                     72:     my $id = $Apache::inputtags::response['-1'];
                     73:     my $increment     = 1;
1.5       albertel   74:     if ($target eq 'grade') {
1.6       albertel   75: 	if (  &Apache::response::submitted('scantron') ) {
                     76: 	    my $increment=&Apache::response::scored_response($part,$id);
                     77: 	} elsif ( &Apache::response::submitted() ) {
                     78: 	    my $response = &Apache::response::getresponse();
1.5       albertel   79: 	    if ( $response =~ /[^\s]/) {
                     80: 		my $url = &Apache::lonxml::get_param('url',$parstack,$safeeval);
                     81: 		my $answer = &Apache::lonxml::get_param('answer',$parstack,$safeeval);
                     82: 		my %form = &Apache::lonxml::get_param_var('form',$parstack,$safeeval);
                     83: 		$form{'LONCAPA_student_response'}=$response;
                     84: 		$form{'LONCAPA_correct_answer'}=$answer;
                     85: 		$form{'LONCAPA_language'}=
                     86: 		    &Apache::lonnet::metadata($ENV{'REQUEST_URI'},'language');
                     87: 		&Apache::lonxml::debug("Asking $url, with:");
                     88: 		&Apache::lonhomework::showhash(%form);
                     89: 		my $ua = LWP::UserAgent->new;
                     90: 		my $res = $ua->request(POST $url, \%form);
                     91: 		my %previous = &Apache::response::check_for_previous($response,
1.6       albertel   92: 								     $part,$id);
1.5       albertel   93: 		%Apache::loncapagrade::results=();
1.6       albertel   94: 		$Apache::lonhomework::results{"resource.$part.$id.submission"}=$response;
1.9       albertel   95: 		if ($res->is_error()) {
1.6       albertel   96: 		    $Apache::loncapagrade::results{'awarddetail'}='ERROR';
1.5       albertel   97: 		} else {
1.6       albertel   98: 		    &Apache::lonxml::register('Apache::loncapagrade',
                     99: 					      ('loncapagrade'));
1.8       albertel  100: 		    @Apache::scripttag::parser_env = @_;
1.5       albertel  101: 		    my $result=&Apache::scripttag::xmlparse($res->{_content});
                    102: 		    &Apache::lonxml::debug("Got a result of :$result:");
                    103: 		}
1.6       albertel  104: 		foreach my $key (keys(%Apache::loncapagrade::results)) {
                    105: 		    $Apache::lonhomework::results{"resource.$part.$id.$key"}=
1.5       albertel  106: 			$Apache::loncapagrade::results{$key};
                    107: 		}
                    108: 		&Apache::response::handle_previous(\%previous,
                    109: 						   $Apache::loncapagrade::results{'awarddetail'});
                    110: 		&Apache::lonxml::debug("response of");
                    111: 		&Apache::lonhomework::showhash(%$res);
1.6       albertel  112: 		&Apache::lonxml::debug("capagrade of");
1.5       albertel  113: 		&Apache::lonhomework::showhash(%Apache::loncapagrade::results);
1.6       albertel  114: 		&Apache::lonxml::debug("results of");
                    115: 		&Apache::lonhomework::showhash(%Apache::lonhomework::results);
1.5       albertel  116: 	    }
1.1       albertel  117: 	}
                    118:     }
1.7       albertel  119:     if ($target eq 'web') {
                    120: 	&Apache::response::setup_prior_tries_hash(\&format_prior_response);
                    121:     }
                    122: 
1.6       albertel  123:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
                    124: 	$target eq 'tex' || $target eq 'analyze') {
                    125: 	&Apache::lonxml::increment_counter($increment);
                    126:     }
                    127:     &Apache::response::end_response();
1.5       albertel  128:     return $result;
1.1       albertel  129: }
                    130: 
1.7       albertel  131: sub format_prior_response {
                    132:     my ($mode,$answer) =@_;
                    133:     return '<span class="LC_prior_external">'.
                    134: 	    &HTML::Entities::encode($answer,'"<>&').'</span>';
                    135: }
                    136: 
1.1       albertel  137: 1;
                    138: __END__
                    139: 
                    140: 

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