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

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

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