[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 4968 - (download) (as text) (annotate)
Thu May 24 12:44:58 2007 UTC (12 years, 6 months ago) by gage
File size: 6243 byte(s)
Pod documentation added

    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  #  Note: If you want a correct answer whose class is a complex variable
   45  #  to check a real number entry you will have to set both sameClass and
   46  #  sameLength to 0 since a complex number has length 2 and a real number
   47  #  has length 1.
   48  #
   49  #  The checker routine will be passed the correct answer, the
   50  #  student's answer, and the answer evaluator object, in that order.
   51  #
   52  #  For example, the following checks if a student entered
   53  #  a unit vector (any unit vector in R^3 will do):
   54  #
   55  #     custom_cmp("<1,2,3>",sub {
   56  #       my ($correct,$student,$ans) = @_;
   57  #       return norm($student) == 1;
   58  #     });
   59  #
   60  #  The checker subroutine can call Value::Error(message) to generate
   61  #  an error message that will be reported in the table at the top of
   62  #  the page.  If the checker generates a fatal runtime error (e.g.,
   63  #  calls the "die" function), then the message is reported with the
   64  #  "pink screen of death", and includes a request for the student to
   65  #  inform the instructor.
   66  #
   67 
   68 =cut
   69 
   70 sub custom_cmp {
   71   my $correct = shift; my $checker = shift;
   72   die "custom_cmp requires a correct answer" unless defined($correct);
   73   die "custom_cmp requires a checker subroutine" unless defined($checker);
   74   $correct = Value::makeValue($correct);
   75   $correct = main::Formula($correct) unless Value::isValue($correct);
   76   $correct->cmp(
   77     checker => sub {
   78       my ($correct,$student,$ans) = @_;
   79       return 0 if $ans->{sameClass} && $correct->class ne $student->class;
   80       return 0 if $ans->{sameLength} && $correct->length != $student->length;
   81       return &{$ans->{custom_checker}}($correct,$student,$ans);
   82     },
   83     custom_checker => $checker,
   84     sameClass => 1,
   85     sameLength => 1,
   86     showEqualErrors => 1,  # make sure we see errors in list checker
   87     @custom_cmp_defaults,
   88     @_,
   89   );
   90 }
   91 
   92 #
   93 #  Set this to include any default parameters you want
   94 #  to include in the custom answer checkers
   95 #
   96 @custom_cmp_defaults = ();
   97 
   98 =head3 another example
   99 
  100  #
  101  #  This one installs a custom list-based answer checker (for the
  102  #  List and Union classes).  Basically it is just a shell that makes
  103  #  it a little easier to do, and provides an interface similar to
  104  #  custom_cmp.
  105  #
  106  #  You pass the correct answer (as a string or as a List or Union
  107  #  object) as the first argument, and the custom list checker as
  108  #  the second argument.  You can pass any additional parameters
  109  #  that should be included in the answer checker following those
  110  #  two required ones.
  111  #
  112  #  The checker will be passed a reference to the array of correct
  113  #  answers, a reference to the array of student answers, and
  114  #  the answer evaluator object.  Note that the correct and student
  115  #  answers are array references, not List structures (this is because
  116  #  a list of formulas becomes a formula returning a list, so in order
  117  #  to keep the formulas separate, they are passed in an array).
  118  #
  119  #  For example, the following checks for any list of the same length
  120  #  as the instructor's list.  (A stupid checker, but just an example.)
  121  #
  122  #      custom_list_cmp("1,2,3",sub {
  123  #        my ($correct,$student,$ans) = @_;
  124  #        scalar(@{$correct}) == scalar(@{$student});
  125  #      });
  126  #
  127  #  The checker subroutine can call Value::Error(message) to generate
  128  #  an error message that will be reported in the table at the top of
  129  #  the page.  If the checker generates a fatal runtime error (e.g.,
  130  #  calls the "die" function), then the message is reported with the
  131  #  "pink screen of death", and includes a request for the student to
  132  #  inform the instructor.
  133  #
  134 
  135 =cut
  136 
  137 sub custom_list_cmp {
  138   my $correct = shift; my $checker = shift;
  139   die "custom_list_cmp requires a correct answer" unless defined($correct);
  140   die "custom_list_cmp requires a checker subroutine" unless defined($checker);
  141   $correct = Value::makeValue($correct);
  142   $correct = main::Formula($correct) unless Value::isValue($correct);
  143   $correct->cmp(
  144     list_checker => $checker,
  145     @custom_list_cmp_defaults,
  146     @_,
  147   );
  148 }
  149 
  150 #
  151 #  Set this to include any default parameters you want
  152 #  to include in the custom answer checkers
  153 #
  154 @custom_list_cmp_defaults = ();

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9