File:  [LON-CAPA] / loncom / homework / templates / sampleexternal.pl
Revision 1.3: download - view: text, annotated - select for diffs
Sat Feb 4 15:12:40 2012 UTC (12 years, 5 months ago) by www
Branches: MAIN
CVS tags: version_2_11_X, version_2_11_5, version_2_11_4_uiuc, version_2_11_4_msu, version_2_11_4, version_2_11_3_uiuc, version_2_11_3_msu, version_2_11_3, version_2_11_2_uiuc, version_2_11_2_msu, version_2_11_2_educog, version_2_11_2, version_2_11_1, version_2_11_0_RC3, version_2_11_0_RC2, version_2_11_0_RC1, version_2_11_0, HEAD
More comprehensive example for <externalresponse>

    1: #!/usr/bin/perl
    2: $|=1;
    3: 
    4: use Safe;
    5: use strict;
    6: 
    7: #
    8: # Sample evaluation script for externalresponse
    9: # If you do this for real, this script should be on another server.
   10: # On that server, it could do anything: run simulations, access databases, run Java, etc, etc.
   11: # Make sure, though, that the student submissions cannot crash or destroy your server.
   12: # This sample script just runs the student code in a Perl safe environment to show how this works.
   13: #
   14: 
   15: # Header
   16: print "Content-type: text/html\n\n";
   17: 
   18: # Load POST variables into hash %FORM
   19: my %FORM=();
   20: my $buffer;
   21: read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
   22: foreach my $pair (split(/&/, $buffer)) {
   23:    my ($name, $value) = split(/=/, $pair);
   24:    $value =~ tr/+/ /;
   25:    $value =~ s/%(..)/pack("C", hex($1))/eg;
   26:    $FORM{$name} = $value;
   27: }
   28: 
   29: #
   30: # ==== This is where your logic needs to be implemented
   31: #
   32: 
   33: # Ready to do stuff
   34: # Do this in a Safe compartment
   35: 
   36: my $compartment = new Safe;
   37: 
   38: # First assume that everything is wonderful 
   39: 
   40: my $award='EXACT_ANS';
   41: my $message='';
   42: 
   43: #
   44: # Passing test cases in the format argument=result,argument=result,...
   45: # 
   46: foreach my $testcase (split(/\,/,$FORM{'LONCAPA_correct_answer'})) {
   47:    my ($value,$result)=split(/\=/,$testcase);
   48: # Execute the student code and call the expected function
   49:    my $studentanswer=$compartment->reval(
   50: $FORM{'somecode'}."\n".
   51: $FORM{'LONCAPA_student_response'}."\n".
   52: '&factorial('.$value.')'
   53: );
   54: # A syntax error occurred
   55:    if ($@) {
   56:       $award='WRONG_FORMAT';
   57:       $message='Syntax error: '.$@;
   58:       last;
   59:    }
   60: # The result is not correct for a test case
   61:    unless ($studentanswer==$result) {
   62:       $award='INCORRECT';
   63:       $message="Returned wrong result.";
   64:       last;
   65:    }   
   66: }
   67: 
   68: #
   69: # ==== The remainder is sending results $award and $message back to LON-CAPA
   70: #
   71: 
   72: # Send result back to LON-CAPA in standard format
   73: # Possible responses
   74: # 'EXTRA_ANSWER','MISSING_ANSWER', 'ERROR',
   75: # 'NO_RESPONSE',
   76: # 'TOO_LONG', 'UNIT_INVALID_INSTRUCTOR',
   77: # 'UNIT_INVALID_STUDENT', 'UNIT_IRRECONCIBLE',
   78: # 'UNIT_FAIL', 'NO_UNIT',
   79: # 'UNIT_NOTNEEDED', 'WANTED_NUMERIC',
   80: # 'BAD_FORMULA', 'NOT_FUNCTION', 'WRONG_FORMAT',
   81: # 'INTERNAL_ERROR', 'SIG_FAIL', 'INCORRECT',
   82: # 'MISORDERED_RANK', 'INVALID_FILETYPE',
   83: # 'EXCESS_FILESIZE', 'FILENAME_INUSE',
   84: # 'DRAFT', 'SUBMITTED', 'SUBMITTED_CREDIT',
   85: # 'ANONYMOUS', 'ANONYMOUS_CREDIT',
   86: # 'ASSIGNED_SCORE', 'APPROX_ANS',
   87: # 'EXACT_ANS','COMMA_FAIL'
   88: #
   89: # plus a free-form $message.
   90: #
   91: # For partial correctness, awarddetail needs to be ASSIGNED_SCORE
   92: # The partial score would be in <awarded>
   93: # The message is passed as unparsed character data, so embedded HTML
   94: # or entities do not get parsed by LON-CAPA's internal parser. Remove
   95: # the CDATA wrapper if you want the parser to process the message.
   96: #
   97: 
   98: print (<<ENDOUT);
   99: <loncapagrade>
  100:     <awarddetail>$award</awarddetail>
  101:     <message><![CDATA[$message]]></message>
  102:     <awarded></awarded>
  103: </loncapagrade>
  104: ENDOUT
  105: exit;

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