WeBWorK Main Forum

give reduced fraction warning even if answer is a string

Re: give reduced fraction warning even if answer is a string

by Davide Cervone -
Number of replies: 0

There are several issues involved, here.

First, the reduced fraction warning is produces by a post-processor on the Fraction answer checker, so it is not called for when the string checker is being used. You would need to run the post-processor by hand to get it to process a Fraction answer when the correct answer is a String.

Second there are answer checker flags that control whether students must reduce fractions, and whether an error is displayed telling them that if they don't do so (studentsMustReduceFractions and showFractionReduceWarnings). But these are only applied to Fraction objects, so you would need to set them for the String answer that you are using, either in the cmp() call, or in the Context's cmpDefaults list.

Third, there is a flag that controls whether fractions are reduced automatically (reduceFractions), and it is true by default, (but the Fraction contexts set it to the negation of studentsMustReduceFractions), so you would need to set this yourself in the Context in order to make it have an effect during processing of the student's answer when the correct one is a string.

So one possible solution is the following:

Context()->flags->set(reduceFractions => 0);
Context()->{cmpDefaults}{String}{studentsMustReduceFractions} = 1;
Context()->{cmpDefaults}{String}{showFractionReduceWarnings} = 1;

$ans = Compute("DNE");

Answer is "DNE": \{and_rule(10)\}

ANS($ans->cmp()->withPostFilter(sub {
  my $ans = shift; 
  context::Fraction::Fraction::cmp_postprocess($ans->{correct_value}, $ans);
  return $ans;

If you need this a lot, you could package up the context settings and the post-filter into a macro file that you load.

An alternative would be to subclass the String object and have it do this stuff automatically, but it is probably not worth it unless you need to do this a lot. If so, I can work that out for you.