File:  [LON-CAPA] / loncom / homework / templates / sampleexternal.pl
Revision 1.2: download - view: text, annotated - select for diffs
Fri Apr 29 00:32:11 2011 UTC (13 years, 2 months ago) by www
Branches: MAIN
CVS tags: version_2_10_X, version_2_10_1, version_2_10_0, loncapaMITrelate_1, language_hyphenation_merge, language_hyphenation, HEAD, BZ4492-merge, BZ4492-feature_horizontal_radioresponse, BZ4492-feature_Support_horizontal_radioresponse, BZ4492-Support_horizontal_radioresponse
Bug #6452: partial correctness from 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($FORM{'LONCAPA_student_response'}.'&factorial('.$value.')');
   50: # A syntax error occurred
   51:    if ($@) {
   52:       $award='WRONG_FORMAT';
   53:       $message='Syntax error: '.$@;
   54:       last;
   55:    }
   56: # The result is not correct for a test case
   57:    unless ($studentanswer==$result) {
   58:       $award='INCORRECT';
   59:       $message="Returned wrong result.";
   60:       last;
   61:    }   
   62: }
   63: 
   64: #
   65: # ==== The remainder is sending results $award and $message back to LON-CAPA
   66: #
   67: 
   68: # Send result back to LON-CAPA in standard format
   69: # Possible responses
   70: # 'EXTRA_ANSWER','MISSING_ANSWER', 'ERROR',
   71: # 'NO_RESPONSE',
   72: # 'TOO_LONG', 'UNIT_INVALID_INSTRUCTOR',
   73: # 'UNIT_INVALID_STUDENT', 'UNIT_IRRECONCIBLE',
   74: # 'UNIT_FAIL', 'NO_UNIT',
   75: # 'UNIT_NOTNEEDED', 'WANTED_NUMERIC',
   76: # 'BAD_FORMULA', 'NOT_FUNCTION', 'WRONG_FORMAT',
   77: # 'INTERNAL_ERROR', 'SIG_FAIL', 'INCORRECT',
   78: # 'MISORDERED_RANK', 'INVALID_FILETYPE',
   79: # 'EXCESS_FILESIZE', 'FILENAME_INUSE',
   80: # 'DRAFT', 'SUBMITTED', 'SUBMITTED_CREDIT',
   81: # 'ANONYMOUS', 'ANONYMOUS_CREDIT',
   82: # 'ASSIGNED_SCORE', 'APPROX_ANS',
   83: # 'EXACT_ANS','COMMA_FAIL'
   84: #
   85: # plus a free-form $message.
   86: #
   87: # For partial correctness, awarddetail needs to be ASSIGNED_SCORE
   88: # The partial score would be in <awarded>
   89: #
   90: 
   91: print (<<ENDOUT);
   92: <loncapagrade>
   93:     <awarddetail>$award</awarddetail>
   94:     <message>$message</message>
   95:     <awarded></awarded>
   96: </loncapagrade>
   97: ENDOUT
   98: exit;

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