WeBWorK Problems

accessing raw student input for multiAnswer problems

accessing raw student input for multiAnswer problems

by roo biki -
Number of replies: 4

Sometimes it is useful to work directly with a students input (before it is converted to a Math Object) to parse it to ensure it is written in the correct form.

For example, to check that powers of a variable have been combined properly, one may check to make sure that the variable only appears once in the expression.

For a problem with a single custom answer checker, this may be accessed through the answer hash, which is the third parameter passed to the checker subroutine.  The code would look something like this:

$ans = Formula("x^2")->cmp(
  checker => sub {
    my ($correct, $student, $hash) = @_;
    my $str = $hash->{original_student_ans};
    # now $str contains exactly what the student entered
  }
);

But for a problem with a multi answer, I'm having problems accessing anything beyond the first part of the answer. According to this example, the answer hash is passed as the fourth parameter to the checker subroutine (I'm not sure what the third parameter $self is used for, but that may be entirely irrelevant to this question).  So I tried using the following code:

$multians = MultiAnswer($num, $den)->with(
  checker => sub {
    my ($correct, $student, $self, $hash) = @_;
    my $str = $hash->{original_student_ans};
    # now $str contains exactly what the student entered for the FIRST answer only
  }
);

I've tested this (by inserting $str into error messages) and what seems to happen is that $str will contain exactly what the student entered for the first part of the answer.  Is there a way to do something similar for $hash->{original_student_ans} but for each of the subsequent parts of the problem?

In reply to roo biki

Re: accessing raw student input for multiAnswer problems

by Glenn Rice -
The $self parameter is actually what you are looking for. $self is the original MultiAnswer object (it is in fact $multians). You can access the answer hash for each part with that. In your example, the answer hash for $num can be accessed via $self->{cmp}[0]->rh_ans and the answer hash for $den is in $self->{cmp}[1]->rh_ans.
In reply to Glenn Rice

Re: accessing raw student input for multiAnswer problems

by roo biki -

Thanks for your answer Glenn.  Unfortunately this didn't work.

I tested this with a multianswer object that looks something like the following:

$multians = MultiAnswer($num, $den)->with(
  singleResult => 1,
  checker => sub {
    my ($correct, $student, $self, $ansHash) = @_;
    my ($s_num, $s_den) = @{$student};
    my ($c_num, $c_den) = @{$correct};
    
    my $raw_num = $self{cmp}[0]->rh_ans;
    my $raw_den = $self{cmp}[1]->rh_ans;
    
    ...

But then an error fires on the second to last line:

An error occurred while checking your answer:

Can't call method "rh_ans" on an undefined value at line 38 of (eval 1985)

If it is important, I'm using version 2.16.

Thanks again!

In reply to roo biki

Re: accessing raw student input for multiAnswer problems

by Glenn Rice -

First, you have a syntax error.  You are missing the first de-referencing operator after $self.   It should be $self->{cmp}[0]->rh_ans.

Second, that will not give the numerator.  That will give the answer hash for the numerator.  It will contain lots of other things.  The original student answer would be $self->{cmp}[0]->rh_ans->{original_student_ans}.

In reply to Glenn Rice

Re: accessing raw student input for multiAnswer problems

by roo biki -

Ahah!  Thank you once again Glenn, my foolish mistake!