WeBWorK Problems

units in custom answer checker

units in custom answer checker

by Michael Shulman -
Number of replies: 9

I want to write a custom answer checker that will accept either of two possible answers, each of which is a number with units.  I tried this:

$ans1 = NumberWithUnits($r1, "in/day");
$ans2 = NumberWithUnits($r2, "in/day");

ANS($ans1->cmp(checker => sub {
    my ($correct,$student,$ansHash) = @_;
    my $v = $student->value;
    return (($ans1 == $v || $ans2 == $v) ? 1 : 0);
}));

but it doesn't work: when I enter one of the correct answers (with units) I get "You must provide units for your number".  I guess that maybe the "==" comparison operator is not taking account of units correctly?  How can I do this?

In reply to Michael Shulman

units in custom answer checker

by Alex Jordan -

You can do like the following, some of which uses the very new contextUnits.pl:

DOCUMENT();

loadMacros(qw(PGstandard.pl PGML.pl contextUnits.pl parserOneOf.pl PGcourse.pl));

Context("Units")->withUnitsFor("length", "time");

$answer = OneOf(
    Compute("1 in/day"),
    Compute("2 in/day")
);

BEGIN_PGML
Enter [`[$answer]`].

[_]{$answer}

END_PGML
ENDDOCUMENT();

In reply to Alex Jordan

units in custom answer checker

by Glenn Rice -

I highly recommend using Alex's approach with the new contextUnits.pl macro and parserOneOf.pl.  All newly written problems should use contextUnits.pl rather than the parserNumberWithUnits.pl macro.

That being said, all you need to do to fix the problem with your code is remove the "value" call in the checker.  If you just do

ANS($ans1->cmp(checker => sub {
    my ($correct,$student,$ansHash) = @_;
    return ($ans1 == $student || $ans2 == $student) ? 1 : 0;
}));

it works as expected.

In reply to Glenn Rice

units in custom answer checker

by Michael Shulman -

Thank you both!  You have solved both the X and the Y of my XY problem.

Is parserUnits.pl documented anywhere yet?

In reply to Michael Shulman

units in custom answer checker

by Alex Jordan -

Yes. Recent versions of WeBWorK have made great progress in getting documentation inside WeBWorK, from the editor. (But not much progress advertising this.)

At the top of the PG editor, one of the buttons is POD, and it takes you to documentation for (among other things) library files. This one is contextUnits.pl (not parserUnits.pl).

In reply to Alex Jordan

units in custom answer checker

by Michael Shulman -

Awesome; I see it does many other great things too!

In addition to advertising this more broadly, I would suggest changing the label on the button (I think only Perl geeks know what "POD" means), and also making this documentation available and searchable on a public web site somewhere, such as the webwork wiki.  Googling for "webwork authoring problems with units" doesn't turn up anything about contextUnits, only the old https://wiki.openwebwork.org/wiki/ProblemsWithUnits.

In reply to Michael Shulman

units in custom answer checker

by Alex Jordan -

I agree about "POD". I think we've had trouble finding a short label for the limited real estate there.

I'm not sure about the wiki, unless it can be automated to keep up. As it's been for 20 or so years, it suffers from bitrot. With the in-app documentation now, documentation is pulled directly from the .pl ad .pm files. So if there's a new version of WeBWorK/PG with a correction, or a new feature, it's going to show up automatically in that documentation. But with the wiki, someone has to remember to update that in parallel. And along with remembering, they have to have spare time to do that. And sometimes involves hunting down references that are not obvious. But if the wiki could essentially just pull from https://webwork-hosting.runestone.academy/webwork2/pod or other such places, that would be nice.

In reply to Alex Jordan

units in custom answer checker

by Michael Shulman -
"Docs"? or just "Doc"?

Yeah, manually updating documentation on a wiki is no good. Given that https://webwork-hosting.runestone.academy/webwork2/pod exists on its own, I would think there's no reason for the wiki to pull from it at all, just link to it is better. But https://webwork-hosting.runestone.academy/webwork2/pod doesn't have any introductory text or explanations about what any of its sub-pages are for, and currently it doesn't show up in a google search for "webwork authoring problems with units", so it would be nice to do something about that.
In reply to Michael Shulman

units in custom answer checker

by Glenn Rice -

The POD is already linked to from the wiki.  It is located at https://webwork.maa.org/pod/, and there is a link to it from https://wiki.openwebwork.org/wiki/Authors.

The POD there is automatically updated on that server from the GitHub repository.

Also, if you click on the "Sample Problems" button in the problem editor, there is a search that you can use. That searches both the sample problem documentation and the POD.

In reply to Glenn Rice

units in custom answer checker

by Glenn Rice -

I also wanted to point out that the "POD" name on the link in the problem editor has been that way for as long as I have known WeBWorK.  Also, for all of the links in the problem editor, if you move your mouse over them, a tooltip displays that shows a longer description of what the links are for.  For the "POD" link it says "Documentation from source code for PG modules and macro files."  So the information is there for those that look.  At the beginning I used those links quite a bit.  I still use the POD link frequently to look things up.

I think the real problem is that there was a period where several of those links were broken for a while, and as a result many started to ignore them.  Also, much of the documentation in the macro and module files was not very good.  We have worked to improve the documentation quite a bit recently.