File:  [LON-CAPA] / loncom / homework / externalresponse.pm
Revision 1.16: download - view: text, annotated - select for diffs
Mon Dec 20 15:54:03 2010 UTC (13 years, 4 months ago) by www
Branches: MAIN
CVS tags: HEAD
Actually output the <message> to the students (as advertised in the documentation)

    1: # The LearningOnline Network with CAPA
    2: # external style responses
    3: #
    4: # $Id: externalresponse.pm,v 1.16 2010/12/20 15:54:03 www Exp $
    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: #
   28: 
   29: 
   30: 
   31: package Apache::externalresponse;
   32: use strict;
   33: use HTTP::Request::Common;
   34: use Apache::loncapagrade;
   35: use Apache::lonnet;
   36: 
   37: BEGIN {
   38:     &Apache::lonxml::register('Apache::externalresponse',('externalresponse'));
   39: }
   40: 
   41: =pod
   42: 
   43: =head1 FIXME (start_externalresponse)
   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: 
   51: sub start_externalresponse {
   52:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   53:     my $result;
   54:     my $id = &Apache::response::start_response($parstack,$safeeval);
   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:     
   73:     return $result;
   74: }
   75: 
   76: sub end_externalresponse {
   77:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   78:     my $result;
   79:     my $part=$Apache::inputtags::part;
   80:     my $id = $Apache::inputtags::response['-1'];
   81:     my $increment     = 1;
   82:     if ($target eq 'grade') {
   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();
   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,
  100: 								     $part,$id);
  101: 		%Apache::loncapagrade::results=();
  102: 		$Apache::lonhomework::results{"resource.$part.$id.submission"}=$response;
  103: 		if ($res->is_error()) {
  104: 		    $Apache::loncapagrade::results{'awarddetail'}='ERROR';
  105: 		} else {
  106: 		    &Apache::lonxml::register('Apache::loncapagrade',
  107: 					      ('loncapagrade'));
  108: 		    @Apache::scripttag::parser_env = @_;
  109: 		    my $result=&Apache::scripttag::xmlparse($res->{_content});
  110: 		    &Apache::lonxml::debug("Got a result of :$result:");
  111: 		}
  112: 		foreach my $key (keys(%Apache::loncapagrade::results)) {
  113: 		    $Apache::lonhomework::results{"resource.$part.$id.$key"}=
  114: 			$Apache::loncapagrade::results{$key};
  115: 		}
  116:                 $Apache::externalresponse::message=$Apache::loncapagrade::results{'message'};
  117: 		&Apache::response::handle_previous(\%previous,
  118: 						   $Apache::loncapagrade::results{'awarddetail'});
  119: 		&Apache::lonxml::debug("response of");
  120: 		&Apache::lonhomework::showhash(%$res);
  121: 		&Apache::lonxml::debug("capagrade of");
  122: 		&Apache::lonhomework::showhash(%Apache::loncapagrade::results);
  123: 		&Apache::lonxml::debug("results of");
  124: 		&Apache::lonhomework::showhash(%Apache::lonhomework::results);
  125: 	    }
  126: 	}
  127:     }
  128:     if ($target eq 'web') {
  129: 	&Apache::response::setup_prior_tries_hash(\&format_prior_response);
  130:         if ($Apache::externalresponse::message) {
  131:            $result.='<br />'.$Apache::externalresponse::message;
  132:         }
  133:         $Apache::externalresponse::message='';
  134:     }
  135: 
  136:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
  137: 	$target eq 'tex' || $target eq 'analyze') {
  138: 	&Apache::lonxml::increment_counter($increment, "$part.$id");
  139: 
  140: 	if ($target eq 'analyze') {
  141:             $Apache::lonhomework::analyze{"$part.$id.type"} = 'externalresponse';
  142: 	    &Apache::lonhomework::set_bubble_lines();
  143: 	}
  144:     }
  145:     &Apache::response::end_response();
  146:     return $result;
  147: }
  148: 
  149: sub format_prior_response {
  150:     my ($mode,$answer) =@_;
  151:     return '<span class="LC_prior_external">'.
  152: 	    &HTML::Entities::encode($answer,'"<>&').'</span>';
  153: }
  154: 
  155: 1;
  156: __END__
  157: 
  158: 
  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: 
  184: =cut

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