[LON-CAPA-cvs] cvs: loncom /homework/templates sampleexternal.pl sampleexternal.problem sampleexternal.problem.meta doc/loncapafiles loncapafiles.lpml

www www@source.lon-capa.org
Mon, 20 Dec 2010 18:14:18 -0000


www		Mon Dec 20 18:14:18 2010 EDT

  Added files:                 
    /loncom/homework/templates	sampleexternal.pl sampleexternal.problem 
                              	sampleexternal.problem.meta 

  Modified files:              
    /doc/loncapafiles	loncapafiles.lpml 
  Log:
  Template for externalresponse
  
  
Index: doc/loncapafiles/loncapafiles.lpml
diff -u doc/loncapafiles/loncapafiles.lpml:1.713 doc/loncapafiles/loncapafiles.lpml:1.714
--- doc/loncapafiles/loncapafiles.lpml:1.713	Fri Nov 19 15:41:24 2010
+++ doc/loncapafiles/loncapafiles.lpml	Mon Dec 20 18:14:12 2010
@@ -2,7 +2,7 @@
  "http://lpml.sourceforge.net/DTD/lpml.dtd">
 <!-- loncapafiles.lpml -->
 
-<!-- $Id: loncapafiles.lpml,v 1.713 2010/11/19 15:41:24 www Exp $ -->
+<!-- $Id: loncapafiles.lpml,v 1.714 2010/12/20 18:14:12 www Exp $ -->
 
 <!--
 
@@ -1338,6 +1338,9 @@
 RandomLabelExample.problem.meta;
 randomvalueradio.problem;
 randomvalueradio.problem.meta;
+sampleexternal.problem;
+sampleexternal.problem.meta;
+sampleexternal.pl;
 SelectFromOptions-4ConceptGoups.problem;
 SelectFromOptions-4ConceptGoups.problem.meta;
 SelectFromOptions-5ConceptGoups.problem;
@@ -1742,6 +1745,12 @@
 <description>CGI script that shows users currently on machine</description>
 </file>
 <file>
+<source>loncom/homework/templates/sampleexternal.pl</source>
+<target dist='default'>home/httpd/cgi-bin/sampleexternal.pl</target>
+<categoryname>script</categoryname>
+<description>Sample External Response Script</description>
+</file>
+<file>
 <source>loncom/loncron</source>
 <target dist='default'>home/httpd/perl/loncron</target>
 <categoryname>script</categoryname>

Index: loncom/homework/templates/sampleexternal.pl
+++ loncom/homework/templates/sampleexternal.pl
#!/usr/bin/perl
$|=1;

use Safe;
use strict;

#
# Sample evaluation script for externalresponse
# If you do this for real, this script should be on another server.
# On that server, it could do anything: run simulations, access databases, run Java, etc, etc.
# Make sure, though, that the student submissions cannot crash or destroy your server.
# This sample script just runs the student code in a Perl safe environment to show how this works.
#

# Header
print "Content-type: text/html\n\n";

# Load POST variables into hash %FORM
my %FORM=();
my $buffer;
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
foreach my $pair (split(/&/, $buffer)) {
   my ($name, $value) = split(/=/, $pair);
   $value =~ tr/+/ /;
   $value =~ s/%(..)/pack("C", hex($1))/eg;
   $FORM{$name} = $value;
}

#
# ==== This is where your logic needs to be implemented
#

# Ready to do stuff
# Do this in a Safe compartment

my $compartment = new Safe;

# First assume that everything is wonderful 

my $award='EXACT_ANS';
my $message='';

#
# Passing test cases in the format argument=result,argument=result,...
# 
foreach my $testcase (split(/\,/,$FORM{'LONCAPA_correct_answer'})) {
   my ($value,$result)=split(/\=/,$testcase);
# Execute the student code and call the expected function
   my $studentanswer=$compartment->reval($FORM{'LONCAPA_student_response'}.'&factorial('.$value.')');
# A syntax error occurred
   if ($@) {
      $award='WRONG_FORMAT';
      $message='Syntax error: '.$@;
      last;
   }
# The result is not correct for a test case
   unless ($studentanswer==$result) {
      $award='INCORRECT';
      $message="Returned wrong result.";
      last;
   }   
}

#
# ==== The remainder is sending results $award and $message back to LON-CAPA
#

# Send result back to LON-CAPA in standard format
# Possible responses
# 'EXTRA_ANSWER','MISSING_ANSWER', 'ERROR',
# 'NO_RESPONSE',
# 'TOO_LONG', 'UNIT_INVALID_INSTRUCTOR',
# 'UNIT_INVALID_STUDENT', 'UNIT_IRRECONCIBLE',
# 'UNIT_FAIL', 'NO_UNIT',
# 'UNIT_NOTNEEDED', 'WANTED_NUMERIC',
# 'BAD_FORMULA', 'NOT_FUNCTION', 'WRONG_FORMAT',
# 'INTERNAL_ERROR', 'SIG_FAIL', 'INCORRECT',
# 'MISORDERED_RANK', 'INVALID_FILETYPE',
# 'EXCESS_FILESIZE', 'FILENAME_INUSE',
# 'DRAFT', 'SUBMITTED', 'SUBMITTED_CREDIT',
# 'ANONYMOUS', 'ANONYMOUS_CREDIT',
# 'ASSIGNED_SCORE', 'APPROX_ANS',
# 'EXACT_ANS','COMMA_FAIL'
#
# plus a free-form $message.


print (<<ENDOUT);
<loncapagrade>
    <awarddetail>$award</awarddetail>
    <message>$message</message>
</loncapagrade>
ENDOUT
exit;

Index: loncom/homework/templates/sampleexternal.problem
+++ loncom/homework/templates/sampleexternal.problem
<problem>
<script type="loncapa/perl">
sub fact {
   my $n=shift;
   if ($n==1) {
      return $n;
   } else {
      return $n*&fact($n-1);
   }
}

$testvalues='0=1';
for ($i=1,3) {
   $rand=&random(3,8,1);
   $testvalues.=','.$rand.'='.&fact($rand);
}
</script>


<startouttext />Write a Perl subroutine called <tt>factorial</tt>, which returns the factorial of its argument, e.g. <tt>&amp;factorial(17)=17!</tt>.
<instructorcomment>
Sample code for an evaluation script can be found <a href="/res/adm/includes/templates/sampleexternal.pl">here.</a></instructorcomment>
<endouttext />
<externalresponse url="http://localhost/cgi-bin/sampleexternal.pl" answer="$testvalues">
<textfield />

</externalresponse>
</problem>

Index: loncom/homework/templates/sampleexternal.problem.meta
+++ loncom/homework/templates/sampleexternal.problem.meta
<title>External Response</title>
<category>Free Form Problems</category>