Skip to content

Commit

Permalink
Add default checker to parserMultiAnswer.
Browse files Browse the repository at this point in the history
This adds a simple default checker to parserMultiAnswer that is used
if no checker is provided. The checker checks if each answer part
is correct using the overloaded `==` operator. It can also be configured
to either return which parts are correct, allowing for partial credit,
or only return 0 or 1 depending on if all answer parts are correct.
The `partialCredit` option can be used to control which behavior
to use, and defaults to the value of `$showPartialCorrectAnswers`.

This is just for convince when using MultiAnswer with a simple
checker.
  • Loading branch information
somiaj committed Nov 6, 2024
1 parent ba7c8fd commit 1627b2d
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions macros/parsers/parserMultiAnswer.pl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ sub new {
format => undef,
context => $context,
single_ans_messages => [],
partialCredit => $main::showPartialCorrectAnswers,
}, $class;
}

Expand Down Expand Up @@ -100,7 +101,23 @@ sub cmp {
delete $options{$id};
}
}
die "You must supply a checker subroutine" unless ref($self->{checker}) eq 'CODE';

unless (ref($self->{checker}) eq 'CODE') {
$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 @@ -464,9 +481,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 @@ -480,6 +497,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 Down

0 comments on commit 1627b2d

Please sign in to comment.