Difference between revisions of "NamedAnswerRules"

From WeBWorK_wiki
Jump to navigation Jump to search
Line 47: Line 47:
 
$ans = Compute(random(1, 5));
 
$ans = Compute(random(1, 5));
   
$ansName = NEW_ANS_NAME();
+
$ansName = NEW_ANS_NAME;
 
</pre>
 
</pre>
 
</td>
 
</td>
Line 53: Line 53:
 
<p>
 
<p>
 
<b>Setup:</b>
 
<b>Setup:</b>
We specify that the Context should be <code>......</code>, and define the answer to be a formula.
 
  +
Generate the answers and do the usual setup.
 
</p>
 
</p>
 
<p>
 
<p>
Notes: on using this and related Contexts.
 
  +
Generate an answer name to use. Many older problems just used a made up answer. That method will not work in gateway quizzes as it lacks the quiz prefix. The NEW_ANS_NAME method will give you an answer name that contains the quiz prefix if the problem is used in a gateway quiz. This answer name can now be used via the variable $ansName as needed to do whatever it is that you needed the answer name for. It will be the id of the html input for the answer name, and so can be used to do things with the html input via javascript, etc.
 
</p>
 
</p>
 
 
</td>
 
</td>
 
</tr>
 
</tr>
Line 68: Line 67:
 
<pre>
 
<pre>
 
BEGIN_PGML
 
BEGIN_PGML
Enter [$ans]: [_]{$ans}{
+
Enter [`[$ans]`]: [_]{$ans}{5}{$ansName}
 
END_PGML
 
END_PGML
  +
  +
ENDDOCUMENT();
 
</pre>
 
</pre>
 
<td style="background-color:#ffcccc;padding:7px;">
 
<td style="background-color:#ffcccc;padding:7px;">
 
<p>
 
<p>
 
<b>Main Text:</b>
 
<b>Main Text:</b>
The problem text section of the file is as we'd expect.
+
Insert all of your problem text using the $ansName for the answer name.
 
</p>
 
</p>
 
</td>
 
</td>
 
</tr>
 
</tr>
   
<!-- Answer section -->
 
 
<tr valign="top">
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<pre>
 
$showPartialCorrectAnswers = 1;
 
 
ANS( $expr->cmp() );
 
 
ENDDOCUMENT();
 
</pre>
 
<td style="background-color:#eeccff;padding:7px;">
 
<p>
 
<b>Answer Evaluation:</b>
 
As is the answer.
 
</p>
 
</td>
 
</tr>
 
 
</table>
 
</table>
   

Revision as of 14:41, 6 May 2021

Name Answer Rules


This PG code shows how to used named answers in problems in such a way that the problem will work in both homework and gateway quizzes. Note that this is an insertion, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.

Problem Techniques Index

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

Initialization: The usual stuff here.

$ans = Compute(random(1, 5));

$ansName = NEW_ANS_NAME;

Setup: Generate the answers and do the usual setup.

Generate an answer name to use. Many older problems just used a made up answer. That method will not work in gateway quizzes as it lacks the quiz prefix. The NEW_ANS_NAME method will give you an answer name that contains the quiz prefix if the problem is used in a gateway quiz. This answer name can now be used via the variable $ansName as needed to do whatever it is that you needed the answer name for. It will be the id of the html input for the answer name, and so can be used to do things with the html input via javascript, etc.

BEGIN_PGML
Enter [`[$ans]`]: [_]{$ans}{5}{$ansName}
END_PGML

ENDDOCUMENT();

Main Text: Insert all of your problem text using the $ansName for the answer name.

Problem Techniques Index