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

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

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