Custom Answer Checker

This provides a custom answer checker.

Complete Code

Download file: CustomAnswerCheckers.pg

PG problem file

Explanation

DOCUMENT();
loadMacros('PGstandard.pl', 'PGML.pl', 'PGcourse.pl');

Preamble

These standard macros need to be loaded.
$ans = Compute('pi/3')->cmp(
    checker => sub {
        my ($correct, $student, $ansHash) = @_;
        Value->Error('Try again.')
            if cos($student) == sqrt(3) / 2 && !$ansHash->{isPreview};
        return cos($correct) == cos($student);
    }
);

Setup

This problem asks the student to enter a value of x such that cos(x) = cos($ans). A custom checker is needed since the particular correct answer computed in the setup will only be one of the actual correct answers for the problem.

To set up the custom answer checker pass the custom checker subroutine for the checker option to the cmp method.

The custom checker subroutine checks that the cosine of the student answer is equal to the cosine of the particular correct answer computed in the setup.

An error message can be set in the answer checker by calling Value->Error("message"). This will set the message that is displayed to the student and exit the checker with an incorrect return value.

Another tip for troubleshooting. To see all of the keys and values in the $ansHash when the submit answers button is pressed, include the following in the custom answer checker.

for my $key (keys %$ansHash) {
    warn "key: $key, value: $ansHash->{$key}";
}
BEGIN_PGML
Enter a value of [`x`] for which [`\cos(x) = [@ Compute('cos(pi/3)') @]`]

[`x =`] [___]{$ans}
END_PGML

Statement

This is the problem statement in PGML.
BEGIN_PGML_SOLUTION
Solution explanation goes here.
END_PGML_SOLUTION

ENDDOCUMENT();

Solution

A solution should be provided here.