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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4845 - (view) (download) (as text)

1 : dpvc 3208 loadMacros('Parser.pl');
2 :    
3 :     sub _answerCustom_init {}; # don't reload this file
4 :    
5 : gage 4831 =head3 custom response evaluator
6 : gage 4808
7 : gage 4831 ######################################################################
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 : dpvc 3208
63 : gage 4808 =cut
64 :    
65 : dpvc 3208 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 : gage 4845 =head3 another example
94 : dpvc 3208
95 :     #
96 :     # This one installs a custom list-based answer checker (for the
97 :     # List and Union classes). Basically it is just a shell that makes
98 :     # it a little easier to do, and provides an interface similar to
99 :     # custom_cmp.
100 :     #
101 :     # You pass the correct answer (as a string or as a List or Union
102 :     # object) as the first argument, and the custom list checker as
103 :     # the second argument. You can pass any additional parameters
104 :     # that should be included in the answer checker following those
105 :     # two required ones.
106 :     #
107 : dpvc 3297 # The checker will be passed a reference to the array of correct
108 :     # answers, a reference to the array of student answers, and
109 : dpvc 3208 # the answer evaluator object. Note that the correct and student
110 : dpvc 3297 # answers are array references, not List structures (this is because
111 : dpvc 3208 # a list of formulas becomes a formula returning a list, so in order
112 :     # to keep the formulas separate, they are passed in an array).
113 :     #
114 :     # For example, the following checks for any list of the same length
115 :     # as the instructor's list. (A stupid checker, but just an example.)
116 :     #
117 :     # custom_list_cmp("1,2,3",sub {
118 :     # my ($correct,$student,$ans) = @_;
119 :     # scalar(@{$correct}) == scalar(@{$student});
120 :     # });
121 :     #
122 :     # The checker subroutine can call Value::Error(message) to generate
123 :     # an error message that will be reported in the table at the top of
124 :     # the page. If the checker generates a fatal runtime error (e.g.,
125 :     # calls the "die" function), then the message is reported with the
126 :     # "pink screen of death", and includes a request for the student to
127 :     # inform the instructor.
128 :     #
129 :    
130 : gage 4845 =cut
131 :    
132 : dpvc 3208 sub custom_list_cmp {
133 :     my $correct = shift; my $checker = shift;
134 :     die "custom_list_cmp requires a correct answer" unless defined($correct);
135 :     die "custom_list_cmp requires a checker subroutine" unless defined($checker);
136 :     $correct = Value::makeValue($correct);
137 :     $correct = main::Formula($correct) unless Value::isValue($correct);
138 :     $correct->cmp(
139 : dpvc 4771 list_checker => $checker,
140 : dpvc 3208 @custom_list_cmp_defaults,
141 :     @_,
142 :     );
143 :     }
144 :    
145 :     #
146 :     # Set this to include any default parameters you want
147 :     # to include in the custom answer checkers
148 :     #
149 :     @custom_list_cmp_defaults = ();

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9