forked from openwebwork/pg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPGmiscevaluators.pl
134 lines (105 loc) · 4.16 KB
/
PGmiscevaluators.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
################################################################################
# WeBWorK Online Homework Delivery System
# Copyright © 2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
# $CVSHeader$
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of either: (a) the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any later
# version, or (b) the "Artistic License" which comes with this package.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the
# Artistic License for more details.
################################################################################
=head1 NAME
PGmiscevaluators.pl - Some miscellaneous answer macros.
=head1 SYNOPSIS
=head1 DESCRIPTION
Currently contains answer evaluators for radio buttons and checkboxes.
=head2 MathObjects and answer evaluators
The MathObjects system provides a parserRadioButtons.pl file that manages
display and checking of radio-button based answers. It is recommended that you
use this method directly if possible.
=cut
BEGIN { be_strict() }
sub _PGmiscevaluators_init {}
=head1 checkbox_cmp
ANS(checkbox_cmp($correctAnswer))
$correctAnswer is a string containing the names of the correct boxes, e.g.
"ACD". Note that this means that individual checkbox names can only be one
character. Internally, this is largely the same as unordered_cs_str_cmp().
=cut
# added 6/14/2000 by David Etlinger
# because of the conversion of the answer
# string to an array, I thought it better not
# to force STR_CMP() to work with this
#added 2/26/2003 by Mike Gage
# handled the case where multiple answers are passed as an array reference
# rather than as a \0 delimited string.
sub checkbox_cmp {
my $correctAnswer = shift @_;
my %options = @_;
assign_option_aliases( \%options,
);
set_default_options( \%options,
'debug' => 0,
'type' => 'checkbox_cmp',
);
my $answer_evaluator = new AnswerEvaluator(
correct_ans => $correctAnswer,
type => $options{type},
);
# pass along debug requests
$answer_evaluator->{debug} = $options{debug};
# join student answer array into a single string if necessary
$answer_evaluator->install_pre_filter(sub {
my $rh_ans = shift;
$rh_ans->{_filter_name} = 'convert student_ans to string';
$rh_ans->{student_ans} = join("", @{$rh_ans->{student_ans}})
if ref($rh_ans->{student_ans}) =~/ARRAY/i;
$rh_ans;
});
# ignore order of check boxes
$answer_evaluator->install_pre_filter(\&ignore_order);
# compare as strings
$answer_evaluator->install_evaluator(sub {
my $rh_ans = shift;
$rh_ans->{_filter_name} = 'compare strings generated by checked boxes';
$rh_ans->{score} = ($rh_ans->{student_ans} eq $rh_ans->{correct_ans}) ? 1 : 0;
$rh_ans;
});
# fix up preview displays
$answer_evaluator->install_post_filter( sub {
my $rh_ans = shift;
$rh_ans->{_filter_name} = 'adjust preview strings';
$rh_ans->{type} = $options{type};
$rh_ans->{preview_text_string} = '\\text{'.$rh_ans->{student_ans}.'}',
$rh_ans->{preview_latex_string} = '\\text{'.$rh_ans->{student_ans}.'}',
$rh_ans;
});
return $answer_evaluator;
}
=head1 radio_cmp
ANS(radio_cmp($correctAnswer))
$correctAnswer is a string containing the name of the correct radio button,
e.g. "Choice1". This is case sensitive and whitespace sensitive, so the correct
answer must match the name of the radio button exactly.
=cut
#added 6/28/2000 by David Etlinger
#exactly the same as strict_str_cmp,
#but more intuitive to the user
# check that answer is really a string and not an array
# also use ordinary string compare
sub radio_cmp {
#strict_str_cmp( @_ );
my $response = shift; # there should be only one item.
warn "Multiple choices -- this should not happen with radio buttons. Have
you used checkboxes perhaps?" if ref($response); #triggered if an ARRAY is passed
str_cmp($response);
}
=head1 SEE ALSO
L<PGanswermacros.pl>, L<MathObjects>.
=cut
1;