WeBWorK Main Forum

Question about debubbing/improving a custom grader that gives partial credit for wrong units.

Re: Question about debubbing/improving a custom grader that gives partial credit for wrong units.

by Christian Seberino -
Number of replies: 0
Below is a version given to me by Davide offline with some minor
changes by me that works and avoids some other problems.

Enjoy...

DOCUMENT();
loadMacros(
  "MathObjects.pl",
  "PGstandard.pl",
  "PGML.pl",
  "PGcourse.pl",
  "parserNumberWithUnits.pl",
  "contextArbitraryString.pl",
  "parserPopUp.pl",
);
TEXT(beginproblem());
$showPartialCorrectAnswers = 1;
######################################################################

Context("Numeric") ; Context()->flags->set(tolerance => 0.01);

    $A = NumberWithUnits("2 ft"); $A->{value} = $A->value;

    $cmp = $A->cmp->withPostFilter(sub {
      my $ans = shift;
      return $ans if $ans->{ans_message} &&
                     ($ans->{ans_message} ne
                         "Your answer doesn't look like a number with units") &&
                     ($ans->{ans_message} ne
                         "The units for your answer are not correct");
     my $correct = $ans->{correct_value};
      my $student = $ans->{student_value};
      if (!defined($student)) {
        my $num = "^ *".Context()->{pattern}{signedNumber}." *~~$";
        $student = Compute($ans->{student_ans}) if $ans->{student_ans} =~
                                                                        m/$num/;
      }
      return $ans unless defined($correct) && defined($student);
      if ($student->{units} ne $correct->{units}) {
        if ($correct->{value} == $student->value) {
          $ans->{score} = .75;
        } else {
          $ans->{score} = 0;
        }
      }
      return $ans;
    });

    BEGIN_PGML
    How many feet are equal to 24 inches?

    [___________]{$cmp}
    END_PGML

######################################################################
ENDDOCUMENT();