Difference between revisions of "ErrorMessageCustomization"

From WeBWorK_wiki
Jump to navigation Jump to search
(17 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<h2>Error Message Customization: PG Code Snippet</h2>
+
<h2>Error Message Customization</h2>
   
 
<!-- Header for these sections -- no modification needed -->
 
<!-- Header for these sections -- no modification needed -->
   
 
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
 
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
<em>This PG code shows how to customize (remap) the error messages students receive after submitting an incorrect response or making a syntax error when entering their answer. This requires using MathObjects or a macro file that loads MathObjects, so that a Context is defined. Note that this is an <b>insertion</b>, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.</em>
+
<em>This PG code shows how to customize (remap) the error messages students receive after submitting an incorrect response or making a syntax error when entering their answer. This requires using MathObjects or a macro file that loads MathObjects, so that a Context is defined.
  +
<br />
  +
<br />
  +
You may also be interested in [http://webwork.maa.org/wiki/AnswerHints AnswerHints]
  +
</em>
 
</p>
 
</p>
   
Line 23: Line 23:
 
<td style="background-color:#ddffdd;border:black 1px dashed;">
 
<td style="background-color:#ddffdd;border:black 1px dashed;">
 
<pre>
 
<pre>
loadMacros("PGstandard.pl","MathObjects.pl");
 
  +
DOCUMENT();
  +
loadMacros(
  +
"PGstandard.pl",
  +
"MathObjects.pl",
  +
);
  +
TEXT(beginproblem());
 
</pre>
 
</pre>
 
</td>
 
</td>
Line 41: Line 46:
 
Context("Numeric");
 
Context("Numeric");
 
Context()->{error}{msg}{"Missing operand after '-'"}
 
Context()->{error}{msg}{"Missing operand after '-'"}
= "Enter '-1' instead of '-'";
+
= "Enter '-1' instead of '-'";
   
$ans1 = Real("-1");
+
$ans1 = Real(-1);
 
$ans2 = Formula("x-2");
 
$ans2 = Formula("x-2");
 
</pre>
 
</pre>
Line 51: Line 56:
 
<b>Setup:</b>
 
<b>Setup:</b>
 
First, specify a Context. One way to remap the error message
 
First, specify a Context. One way to remap the error message
is through the context. In factoring -x+2 as -(x-2), students
+
is through the context. In factoring <code>-x+2</code> as
  +
<code>-(x-2)</code>, students
 
may enter <code>-</code> instead of <code>-1</code> in the
 
may enter <code>-</code> instead of <code>-1</code> in the
 
first answer box and receive the error
 
first answer box and receive the error
 
message <code>Missing operand after '-'</code>. We can replace
 
message <code>Missing operand after '-'</code>. We can replace
 
this error message with <code>"Enter '-1' instead of '-'"</code> via
 
this error message with <code>"Enter '-1' instead of '-'"</code> via
the <code>Context()->{error}{msg} ....</code> code to the left.
+
the <code>Context()->{error}{msg}...</code> code to the left.
 
The replacement error message can be anything you like as long as
 
The replacement error message can be anything you like as long as
 
it is nonempty. To generate a blank error message, use a quoted
 
it is nonempty. To generate a blank error message, use a quoted
space <code>" "</code>.
+
space <code>" "</code>, not <code>""</code>.
 
</p>
 
</p>
 
<p>
 
<p>
Notes: on using this and related Contexts.
 
  +
For another way to customize the error message, see the
  +
Answer Evaluation section below.
 
</p>
 
</p>
 
 
</td>
 
</td>
 
</tr>
 
</tr>
Line 73: Line 78:
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<pre>
 
<pre>
  +
Context()->texStrings;
 
BEGIN_TEXT
 
BEGIN_TEXT
 
Factor \( -1 \) from \( -x+2 \).
 
Factor \( -1 \) from \( -x+2 \).
Line 82: Line 88:
 
\( \big) \)
 
\( \big) \)
 
END_TEXT
 
END_TEXT
  +
Context()->normalStrings;
 
</pre>
 
</pre>
 
<td style="background-color:#ffcccc;padding:7px;">
 
<td style="background-color:#ffcccc;padding:7px;">
Line 98: Line 105:
 
ANS( $ans1->cmp() );
 
ANS( $ans1->cmp() );
 
ANS( $ans2->cmp() );
 
ANS( $ans2->cmp() );
  +
  +
ENDDOCUMENT();
 
</pre>
 
</pre>
 
<td style="background-color:#eeccff;padding:7px;">
 
<td style="background-color:#eeccff;padding:7px;">
 
<p>
 
<p>
 
<b>Answer Evaluation:</b>
 
<b>Answer Evaluation:</b>
As is the answer.
 
  +
Another way to customize the error message <code>Missing operand after '-'</code> would be to use a post-filter to remove the message after the answer has been graded. The <code>answerHints.pl</code> macro provides one way to do this, but it is probably just as easy to do it manually, as in:
  +
<pre>
  +
ANS($ans1->cmp->withPostFilter(sub {
  +
my $ans = shift;
  +
$ans->{ans_message} = "Enter '-1' instead of '-'"
  +
if $ans->{ans_message} eq "Missing operand after '-'";
  +
return $ans;
  +
}));
  +
</pre>
  +
 
</p>
 
</p>
 
</td>
 
</td>
Line 113: Line 131:
   
 
[[Category:Problem Techniques]]
 
[[Category:Problem Techniques]]
  +
  +
  +
  +
<ul>
  +
<li>POD documentation: [http://webwork.maa.org/pod/pg/macros/answerHints.html answerHints.pl]</li>
  +
<li>PG macro: [http://webwork.maa.org/viewvc/system/trunk/pg/macros/answerHints.pl answerHints.pl]</li>
  +
</ul>

Revision as of 17:42, 7 April 2021

Error Message Customization


This PG code shows how to customize (remap) the error messages students receive after submitting an incorrect response or making a syntax error when entering their answer. This requires using MathObjects or a macro file that loads MathObjects, so that a Context is defined.

You may also be interested in AnswerHints

Problem Techniques Index

PG problem file Explanation
DOCUMENT();
loadMacros(
  "PGstandard.pl",
  "MathObjects.pl",
);
TEXT(beginproblem());

Initialization: To be able to customize the error messages students receive, we must be using MathObjects or a macro that loads MathObjects and has a Context.

Context("Numeric");
Context()->{error}{msg}{"Missing operand after '-'"} 
  = "Enter '-1' instead of '-'";

$ans1 = Real(-1);
$ans2 = Formula("x-2");

Setup: First, specify a Context. One way to remap the error message is through the context. In factoring -x+2 as -(x-2), students may enter - instead of -1 in the first answer box and receive the error message Missing operand after '-'. We can replace this error message with "Enter '-1' instead of '-'" via the Context()->{error}{msg}... code to the left. The replacement error message can be anything you like as long as it is nonempty. To generate a blank error message, use a quoted space " ", not "".

For another way to customize the error message, see the Answer Evaluation section below.

Context()->texStrings;
BEGIN_TEXT
Factor \( -1 \) from \( -x+2 \).
$BR
\( -x + 2 = \) 
\{ ans_rule(5) \} 
\( \cdot \big( \) 
\{ ans_rule(10) \} 
\( \big) \)
END_TEXT
Context()->normalStrings;

Main Text: The problem text section of the file is as we'd expect.

ANS( $ans1->cmp() );
ANS( $ans2->cmp() );

ENDDOCUMENT();

Answer Evaluation: Another way to customize the error message Missing operand after '-' would be to use a post-filter to remove the message after the answer has been graded. The answerHints.pl macro provides one way to do this, but it is probably just as easy to do it manually, as in:

ANS($ans1->cmp->withPostFilter(sub { 
  my $ans = shift; 
  $ans->{ans_message} = "Enter '-1' instead of '-'" 
    if $ans->{ans_message} eq "Missing operand after '-'";
  return $ans; 
}));

Problem Techniques Index