[LON-CAPA-cvs] cvs: loncom /homework/templates sampleexternal.pl sampleexternal.problem

www www at source.lon-capa.org
Sat Feb 4 10:12:40 EST 2012


www		Sat Feb  4 15:12:40 2012 EDT

  Modified files:              
    /loncom/homework/templates	sampleexternal.pl sampleexternal.problem 
  Log:
  More comprehensive example for <externalresponse>
  
  
Index: loncom/homework/templates/sampleexternal.pl
diff -u loncom/homework/templates/sampleexternal.pl:1.2 loncom/homework/templates/sampleexternal.pl:1.3
--- loncom/homework/templates/sampleexternal.pl:1.2	Fri Apr 29 00:32:11 2011
+++ loncom/homework/templates/sampleexternal.pl	Sat Feb  4 15:12:40 2012
@@ -46,7 +46,11 @@
 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.')');
+   my $studentanswer=$compartment->reval(
+$FORM{'somecode'}."\n".
+$FORM{'LONCAPA_student_response'}."\n".
+'&factorial('.$value.')'
+);
 # A syntax error occurred
    if ($@) {
       $award='WRONG_FORMAT';
@@ -86,12 +90,15 @@
 #
 # For partial correctness, awarddetail needs to be ASSIGNED_SCORE
 # The partial score would be in <awarded>
+# The message is passed as unparsed character data, so embedded HTML
+# or entities do not get parsed by LON-CAPA's internal parser. Remove
+# the CDATA wrapper if you want the parser to process the message.
 #
 
 print (<<ENDOUT);
 <loncapagrade>
     <awarddetail>$award</awarddetail>
-    <message>$message</message>
+    <message><![CDATA[$message]]></message>
     <awarded></awarded>
 </loncapagrade>
 ENDOUT
Index: loncom/homework/templates/sampleexternal.problem
diff -u loncom/homework/templates/sampleexternal.problem:1.3 loncom/homework/templates/sampleexternal.problem:1.4
--- loncom/homework/templates/sampleexternal.problem:1.3	Tue Dec 21 01:53:13 2010
+++ loncom/homework/templates/sampleexternal.problem	Sat Feb  4 15:12:40 2012
@@ -14,14 +14,25 @@
    $rand=&random(3,8,1);
    $testvalues.=','.$rand.'='.&fact($rand);
 }
+
+%args=('somecode' => (<<'ENDCODE')
+sub negative {
+   my ($number)=@_;
+   return $number<0;
+}
+ENDCODE
+);
+
+
 </script>
 
 
-<startouttext />Write a Perl subroutine called <tt>factorial</tt>, which returns the factorial of its argument, e.g. <tt>&factorial(17)=17!</tt>.
+<startouttext />Write a Perl subroutine called <tt>factorial</tt>, which returns the factorial of its argument, e.g. <tt>&factorial(17)=17!</tt>. You can use the
+boolean function <tt>&negative(number)</tt>, which returns <tt>true</tt> if the argument is negative.
 <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" answerdisplay="The most elegant (and dangerous) solution is recursive.">
+<externalresponse url="http://localhost/cgi-bin/sampleexternal.pl" answer="$testvalues" form="%args" answerdisplay="The most elegant (and dangerous) solution is recursive.">
 <textfield />
 
 </externalresponse>




More information about the LON-CAPA-cvs mailing list