Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MultiAnswer convenience methods (default checker and passing cmp options). #1140

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 42 additions & 5 deletions macros/parsers/parserMultiAnswer.pl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ sub new {
part => 0,
singleResult => 0,
namedRules => 0,
cmpOpts => undef,
checkTypes => 1,
allowBlankAnswers => 0,
tex_separator => $separator . '\,',
Expand All @@ -70,6 +71,7 @@ sub new {
format => undef,
context => $context,
single_ans_messages => [],
partialCredit => $main::showPartialCorrectAnswers,
}, $class;
}

Expand All @@ -89,15 +91,34 @@ sub setCmpFlags {
# the individual answer checkers.
#
sub cmp {
my $self = shift;
my %options = @_;
my ($self, %options) = @_;

%options = (%options, %{ $self->{cmpOpts} }) if ref($self->{cmpOpts}) eq 'HASH';

foreach my $id ('checker', 'separator') {
if (defined($options{$id})) {
$self->{$id} = $options{$id};
delete $options{$id};
}
}
die "You must supply a checker subroutine" unless ref($self->{checker}) eq 'CODE';

unless (ref($self->{checker}) eq 'CODE') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may wish to produce an error message if checker is defined but not a code reference, rather than just replacing it silently.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could put the check inside the unless clause, as

unless (ref($self->{checker} eq 'CODE') {
  die "Your checker must be a subroutine." if defined($self->{checker});

instead of having to check for CODE two times.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again.

die "Your checker must be a subroutine." if defined($self->{checker});
$self->{checker} = sub {
my ($correct, $student, $self, $ans) = @_;
my @scores;

for (0 .. $self->length - 1) {
push(@scores, $correct->[$_] == $student->[$_] ? 1 : 0);
}
return \@scores if $self->{partialCredit};
for (@scores) {
return 0 unless $_;
}
return 1;
}
}

if ($self->{allowBlankAnswers}) {
foreach my $cmp (@{ $self->{cmp} }) {
$cmp->install_pre_filter('erase');
Expand Down Expand Up @@ -474,9 +495,9 @@ =head1 ATTRIBUTES

C<MultiAnswer> objects have the following attributes:

=head2 checker (required)
=head2 checker

A coderef to be called to check student answers. This is the only required attribute.
A coderef to be called to check student answers.

The C<checker> routine receives four parameters: a reference to the array of correct answers,
a reference to the array of student answers, a reference to the C<MultiAnswer> object itself,
Expand All @@ -490,6 +511,16 @@ =head2 checker (required)
}
$multianswer_obj = $multianswer_obj->with(checker=>~~&always_right);

If a C<checker> is not provided, a default checker is used. The default checker checks if each
answer is equal to its correct answer (using the overloaded C<==> operator). If C<< partialCredit => 1 >>,
the checker returns an array of 0s and 1s listing which answers are correct giving partial credit.
If C<< partialCredit => 0 >>, the checker only returns 1 if all answers are correct, otherwise returns 0.

=head2 partialCredit

This is used with the default checker to determine if the default checker should reward partial
credit, based on the number of correct answers, or not. Default: C<$showPartialCorrectAnswers>.

=head2 singleResult

Indicates whether to show only one entry in the results table (C<< singleResult => 1 >>)
Expand All @@ -501,6 +532,12 @@ =head2 namedRules
if you need to intersperse other rules with the ones for the C<MultiAnswer>. In this case, you must
use C<NAMED_ANS> instead of C<ANS>. Default: 0.

=head2 cmpOpts

This is a hash of options that will be passed to the cmp method. For example,
C<< cmpOpts => { weight => 0.5 } >>. This option is provided to make it more convenient to pass
options to cmp when utilizing PGML. Default: undef (no options are sent).

=head2 checkTypes

Specifies whether the types of the student and professor's answers must match exactly
Expand Down