[LON-CAPA-cvs] cvs: loncom /homework externalresponse.pm gradesubmission.pl

albertel lon-capa-cvs@mail.lon-capa.org
Tue, 18 Oct 2005 15:33:56 -0000


albertel		Tue Oct 18 11:33:56 2005 EDT

  Added files:                 
    /loncom/homework	gradesubmission.pl 

  Modified files:              
    /loncom/homework	externalresponse.pm 
  Log:
  - making <externalresponse> work
  - added a really simple (and stupid) grading script
  
  
Index: loncom/homework/externalresponse.pm
diff -u loncom/homework/externalresponse.pm:1.5 loncom/homework/externalresponse.pm:1.6
--- loncom/homework/externalresponse.pm:1.5	Tue Oct 18 09:58:12 2005
+++ loncom/homework/externalresponse.pm	Tue Oct 18 11:33:55 2005
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # external style responses
 #
-# $Id: externalresponse.pm,v 1.5 2005/10/18 13:58:12 albertel Exp $
+# $Id: externalresponse.pm,v 1.6 2005/10/18 15:33:55 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -25,7 +25,7 @@
 #
 # http://www.lon-capa.org/
 #
-# 1/26 Guy
+
 package Apache::externalresponse;
 use strict;
 use HTTP::Request::Common;
@@ -50,10 +50,14 @@
 sub end_externalresponse {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
     my $result;
+    my $part=$Apache::inputtags::part;
+    my $id = $Apache::inputtags::response['-1'];
+    my $increment     = 1;
     if ($target eq 'grade') {
-	if ( defined $env{'form.submitted'}) {
-	    my $id = $Apache::inputtags::response['-1'];
-	    my $response = $env{'form.HWVAL_'.$id};
+	if (  &Apache::response::submitted('scantron') ) {
+	    my $increment=&Apache::response::scored_response($part,$id);
+	} elsif ( &Apache::response::submitted() ) {
+	    my $response = &Apache::response::getresponse();
 	    if ( $response =~ /[^\s]/) {
 		my $url = &Apache::lonxml::get_param('url',$parstack,$safeeval);
 		my $answer = &Apache::lonxml::get_param('answer',$parstack,$safeeval);
@@ -66,31 +70,38 @@
 		&Apache::lonhomework::showhash(%form);
 		my $ua = LWP::UserAgent->new;
 		my $res = $ua->request(POST $url, \%form);
-		my $partid = $Apache::inputtags::part;
-		my $id = $Apache::inputtags::response['-1'];
 		my %previous = &Apache::response::check_for_previous($response,
-								     $partid,$id);
+								     $part,$id);
 		%Apache::loncapagrade::results=();
+		$Apache::lonhomework::results{"resource.$part.$id.submission"}=$response;
 		if ($res->{_rc} != '200') {
-		    $Apache::loncapagrade::results{'awardetail'}='ERROR';
+		    $Apache::loncapagrade::results{'awarddetail'}='ERROR';
 		} else {
-		    &Apache::lonxml::register('Apache::loncapagrade',('loncapagrade'));
+		    &Apache::lonxml::register('Apache::loncapagrade',
+					      ('loncapagrade'));
 		    my $result=&Apache::scripttag::xmlparse($res->{_content});
 		    &Apache::lonxml::debug("Got a result of :$result:");
 		}
-		foreach my $key (%Apache::loncapagrade::results) {
-		    $Apache::lonhomework::results{"resource.$partid.$id.$key"}=
+		foreach my $key (keys(%Apache::loncapagrade::results)) {
+		    $Apache::lonhomework::results{"resource.$part.$id.$key"}=
 			$Apache::loncapagrade::results{$key};
 		}
 		&Apache::response::handle_previous(\%previous,
 						   $Apache::loncapagrade::results{'awarddetail'});
 		&Apache::lonxml::debug("response of");
 		&Apache::lonhomework::showhash(%$res);
+		&Apache::lonxml::debug("capagrade of");
 		&Apache::lonhomework::showhash(%Apache::loncapagrade::results);
+		&Apache::lonxml::debug("results of");
+		&Apache::lonhomework::showhash(%Apache::lonhomework::results);
 	    }
 	}
     }
-    &Apache::response::end_response;
+    if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
+	$target eq 'tex' || $target eq 'analyze') {
+	&Apache::lonxml::increment_counter($increment);
+    }
+    &Apache::response::end_response();
     return $result;
 }
 

Index: loncom/homework/gradesubmission.pl
+++ loncom/homework/gradesubmission.pl
#!/usr/bin/perl
# The LearningOnline Network with CAPA
# example submissions grader for use with <externalresponse>
#
# $Id: gradesubmission.pl,v 1.1 2005/10/18 15:33:55 albertel Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#

use strict;
use CGI qw(:standard);

my %form;
&main();
sub main {
    my $q=new CGI;
    print $q->header(-type => 'text/xml');
    %form = $q->Vars();
    my ($result,$message) = &grade();
    &print_result($result,$message);
}

sub print_result {
    my ($result,$message) = @_;
    print("<loncapagrade>\n");
    print("<awarddetail>$result</awarddetail>\n");
    print("<message>$message</message>\n");
    print "</loncapagrade>";
}


sub grade {
    if ($form{'LONCAPA_student_response'} =~ /a/) {
	return ('APPROX_ANS','Go a!');
    } 
    if ($form{'LONCAPA_student_response'} =~ /b/) {
	return ('EXACT_ANS','Go a!');
    }
    if ($form{'LONCAPA_student_response'} =~ /c/) {
	return ('INCORRECT','No c');
    }
    return ('INCORRECT','Hrrm');
}