[system] / trunk / pg / macros / answerCustom.pl Repository:
ViewVC logotype

View of /trunk/pg/macros/answerCustom.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4831 - (download) (as text) (annotate)
Sun Mar 4 22:46:01 2007 UTC (12 years, 11 months ago) by gage
File size: 5938 byte(s)
Documentation formatting changes

    1 loadMacros('Parser.pl');
    2 
    3 sub _answerCustom_init {}; # don't reload this file
    4 
    5 =head3 custom response evaluator
    6 
    7  ######################################################################
    8  #
    9  #  This answer checker provides an easy method for creating an answer
   10  #  checker with a custom subroutine that performs the check for
   11  #  correctness.
   12  #
   13  #  Pass the correct answer (either as a string or as a Parser object)
   14  #  as the first argument, and a reference to the checker subroutine
   15  #  as the second argument.  Additional parameters can follow.  These
   16  #  include any of the parameters for the usual answer checker of the
   17  #  of the type of the correct answer (e.g., showCoordinateHints), plus
   18  #  the following:
   19  #
   20  #     sameClass => 0 or 1      If 1 (the default), only call the
   21  #                              custom checker if the student answer
   22  #                              is the same object class as the correct
   23  #                              answer (e.g., both are points).
   24  #                              If 0, the checker will be called
   25  #                              whenever the student answer passes
   26  #                              the typeMatch check for the correct
   27  #                              answer.  For example, if the correct
   28  #                              answer is a vector, and promotePoints
   29  #                              has been set to 1, then the checker
   30  #                              will be called when the student answer
   31  #                              is a vector OR a point.
   32  #
   33  #    sameLength => 0 or 1      If 1 (the default), only call the
   34  #                              custom checker if the student answer
   35  #                              has the same number of coordinates as
   36  #                              the correct answer.
   37  #
   38  #  If the correct answer is a list, the custom checker will be called
   39  #  on the individual entries of the list, not on the list as a whole.
   40  #  If the list is an unordered list, the routine may be called
   41  #  multiple times with various combinations of student and professor's
   42  #  answers in order to find a correct match.
   43  #
   44  #  The checker routine will be passed the correct answer, the
   45  #  student's answer, and the answer evaluator object, in that order.
   46  #
   47  #  For example, the following checks if a student entered
   48  #  a unit vector (any unit vector in R^3 will do):
   49  #
   50  #     custom_cmp("<1,2,3>",sub {
   51  #       my ($correct,$student,$ans) = @_;
   52  #       return norm($student) == 1;
   53  #     });
   54  #
   55  #  The checker subroutine can call Value::Error(message) to generate
   56  #  an error message that will be reported in the table at the top of
   57  #  the page.  If the checker generates a fatal runtime error (e.g.,
   58  #  calls the "die" function), then the message is reported with the
   59  #  "pink screen of death", and includes a request for the student to
   60  #  inform the instructor.
   61  #
   62 
   63 =cut
   64 
   65 sub custom_cmp {
   66   my $correct = shift; my $checker = shift;
   67   die "custom_cmp requires a correct answer" unless defined($correct);
   68   die "custom_cmp requires a checker subroutine" unless defined($checker);
   69   $correct = Value::makeValue($correct);
   70   $correct = main::Formula($correct) unless Value::isValue($correct);
   71   $correct->cmp(
   72     checker => sub {
   73       my ($correct,$student,$ans) = @_;
   74       return 0 if $ans->{sameClass} && $correct->class ne $student->class;
   75       return 0 if $ans->{sameLength} && $correct->length != $student->length;
   76       return &{$ans->{custom_checker}}($correct,$student,$ans);
   77     },
   78     custom_checker => $checker,
   79     sameClass => 1,
   80     sameLength => 1,
   81     showEqualErrors => 1,  # make sure we see errors in list checker
   82     @custom_cmp_defaults,
   83     @_,
   84   );
   85 }
   86 
   87 #
   88 #  Set this to include any default parameters you want
   89 #  to include in the custom answer checkers
   90 #
   91 @custom_cmp_defaults = ();
   92 
   93 
   94 #
   95 #  This one installs a custom list-based answer checker (for the
   96 #  List and Union classes).  Basically it is just a shell that makes
   97 #  it a little easier to do, and provides an interface similar to
   98 #  custom_cmp.
   99 #
  100 #  You pass the correct answer (as a string or as a List or Union
  101 #  object) as the first argument, and the custom list checker as
  102 #  the second argument.  You can pass any additional parameters
  103 #  that should be included in the answer checker following those
  104 #  two required ones.
  105 #
  106 #  The checker will be passed a reference to the array of correct
  107 #  answers, a reference to the array of student answers, and
  108 #  the answer evaluator object.  Note that the correct and student
  109 #  answers are array references, not List structures (this is because
  110 #  a list of formulas becomes a formula returning a list, so in order
  111 #  to keep the formulas separate, they are passed in an array).
  112 #
  113 #  For example, the following checks for any list of the same length
  114 #  as the instructor's list.  (A stupid checker, but just an example.)
  115 #
  116 #      custom_list_cmp("1,2,3",sub {
  117 #        my ($correct,$student,$ans) = @_;
  118 #        scalar(@{$correct}) == scalar(@{$student});
  119 #      });
  120 #
  121 #  The checker subroutine can call Value::Error(message) to generate
  122 #  an error message that will be reported in the table at the top of
  123 #  the page.  If the checker generates a fatal runtime error (e.g.,
  124 #  calls the "die" function), then the message is reported with the
  125 #  "pink screen of death", and includes a request for the student to
  126 #  inform the instructor.
  127 #
  128 
  129 sub custom_list_cmp {
  130   my $correct = shift; my $checker = shift;
  131   die "custom_list_cmp requires a correct answer" unless defined($correct);
  132   die "custom_list_cmp requires a checker subroutine" unless defined($checker);
  133   $correct = Value::makeValue($correct);
  134   $correct = main::Formula($correct) unless Value::isValue($correct);
  135   $correct->cmp(
  136     list_checker => $checker,
  137     @custom_list_cmp_defaults,
  138     @_,
  139   );
  140 }
  141 
  142 #
  143 #  Set this to include any default parameters you want
  144 #  to include in the custom answer checkers
  145 #
  146 @custom_list_cmp_defaults = ();

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9