Annotation of loncom/homework/templates/sampleexternal.pl, revision 1.4

1.1       www         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
1.3       www        49:    my $studentanswer=$compartment->reval(
                     50: $FORM{'somecode'}."\n".
                     51: $FORM{'LONCAPA_student_response'}."\n".
                     52: '&factorial('.$value.')'
                     53: );
1.1       www        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: 
1.4     ! kruse      68: my $handbackurl = "Here will be the URL of the handbackfile";
1.1       www        69: #
                     70: # ==== The remainder is sending results $award and $message back to LON-CAPA
                     71: #
                     72: 
                     73: # Send result back to LON-CAPA in standard format
                     74: # Possible responses
                     75: # 'EXTRA_ANSWER','MISSING_ANSWER', 'ERROR',
                     76: # 'NO_RESPONSE',
                     77: # 'TOO_LONG', 'UNIT_INVALID_INSTRUCTOR',
                     78: # 'UNIT_INVALID_STUDENT', 'UNIT_IRRECONCIBLE',
                     79: # 'UNIT_FAIL', 'NO_UNIT',
                     80: # 'UNIT_NOTNEEDED', 'WANTED_NUMERIC',
                     81: # 'BAD_FORMULA', 'NOT_FUNCTION', 'WRONG_FORMAT',
                     82: # 'INTERNAL_ERROR', 'SIG_FAIL', 'INCORRECT',
                     83: # 'MISORDERED_RANK', 'INVALID_FILETYPE',
                     84: # 'EXCESS_FILESIZE', 'FILENAME_INUSE',
                     85: # 'DRAFT', 'SUBMITTED', 'SUBMITTED_CREDIT',
                     86: # 'ANONYMOUS', 'ANONYMOUS_CREDIT',
                     87: # 'ASSIGNED_SCORE', 'APPROX_ANS',
                     88: # 'EXACT_ANS','COMMA_FAIL'
                     89: #
                     90: # plus a free-form $message.
1.2       www        91: #
                     92: # For partial correctness, awarddetail needs to be ASSIGNED_SCORE
                     93: # The partial score would be in <awarded>
1.3       www        94: # The message is passed as unparsed character data, so embedded HTML
                     95: # or entities do not get parsed by LON-CAPA's internal parser. Remove
                     96: # the CDATA wrapper if you want the parser to process the message.
1.2       www        97: #
1.1       www        98: 
                     99: print (<<ENDOUT);
                    100: <loncapagrade>
                    101:     <awarddetail>$award</awarddetail>
1.3       www       102:     <message><![CDATA[$message]]></message>
1.4     ! kruse     103:     <handbackurl>$handbackurl</handbackurl>
1.2       www       104:     <awarded></awarded>
1.1       www       105: </loncapagrade>
                    106: ENDOUT
                    107: exit;

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