Difference between revisions of "NamedAnswerRules"

From WeBWorK_wiki
Jump to navigation Jump to search
(Created page with "<h2>Your title here: PG Code Snippet</h2> <!-- Header for these sections -- no modification needed --> <p style="background-color:#eeeeee;border:black solid 1px;padding:3...")
 
 
(15 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<h2>Your title here: PG Code Snippet</h2>
+
<h2>Name Answer Rules</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 check student answers that are equations. 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 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 <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>
 
</p>
 
</p>
   
Line 14: Line 14:
   
 
<tr valign="top">
 
<tr valign="top">
<th> PG problem file </th>
+
<th style="width:50%"> PG problem file </th>
 
<th> Explanation </th>
 
<th> Explanation </th>
 
</tr>
 
</tr>
Line 27: Line 27:
 
"PGstandard.pl",
 
"PGstandard.pl",
 
"MathObjects.pl",
 
"MathObjects.pl",
  +
"PGML.pl"
 
);
 
);
 
TEXT(beginproblem());
 
TEXT(beginproblem());
Line 34: Line 35:
 
<p>
 
<p>
 
<b>Initialization:</b>
 
<b>Initialization:</b>
To do ..(what you are doing)........., we don't have to change the
 
  +
The usual stuff here.
tagging and documentation section of the problem file.
 
In the initialization section, we need to include the macros file <code>-------.pl</code>.
 
 
</p>
 
</p>
 
</td>
 
</td>
Line 46: Line 45:
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<pre>
 
<pre>
Context(".....");
 
  +
$ans = Compute(random(1, 5));
Define context and variables for the questions
 
   
$expr = Formula("....");
 
  +
$ansName = NEW_ANS_NAME;
 
</pre>
 
</pre>
 
</td>
 
</td>
Line 55: 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.
 
  +
Create the answers and do the usual setup.
 
</p>
 
</p>
 
<p>
 
<p>
Notes: on using this and related Contexts.
 
  +
Generate an answer name to use. 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>
  +
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.
 
</p>
 
</p>
 
 
</td>
 
</td>
 
</tr>
 
</tr>
Line 69: Line 69:
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<pre>
 
<pre>
  +
# PGML usage
  +
BEGIN_PGML
  +
Enter [`[$ans]`]: [_]{$ans}{5}{$ansName}
  +
END_PGML
  +
  +
# Basic PG usage (prefer PGML)
 
BEGIN_TEXT
 
BEGIN_TEXT
...... question text ......
 
  +
Enter \($ans\): \{$ans->named_ans_rule($ansName, 5)\}
 
END_TEXT
 
END_TEXT
  +
LABELED_ANS($ansName, $ans->cmp);
  +
  +
# Add 'less than or equal' and 'greater than or equal' MathQuill toolbar buttons.
  +
TEXT(MODES(TeX => "", HTML => <<END_SCRIPT));
  +
<script>
  +
window.addEventListener('DOMContentLoaded', function() {
  +
if (!window.answerQuills) return;
  +
answerQuills.$ansName.buttons.splice(-1, 0,
  +
{ id: 'leq', latex: '\\leq',
  +
tooltip: 'less than or equal (<=)',
  +
icon: '\\leq' },
  +
{ id: 'geq', latex: '\\geq',
  +
tooltip: 'greater than or equal (>=)',
  +
icon: '\\geq' });
  +
});
  +
</script>
  +
END_SCRIPT
  +
  +
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 $ansName for the answer name. Both the PGML and basic PG usage are demonstrated. Your problem should only use one of these. If you are not using PGML you should make the switch!
 
</p>
 
</p>
</td>
 
</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>
 
<p>
<b>Answer Evaluation:</b>
 
  +
An example of a possible usage of the named answer rule is shown. Here we add "greater than or equal to" and "less than or equal to" buttons to the MathQuill toolbar for the answer. This will only have effect if MathQuill is enabled for the course.
As is the answer.
 
 
</p>
 
</p>
 
</td>
 
</td>
 
</tr>
 
</tr>
  +
 
</table>
 
</table>
   
Line 106: Line 116:
   
 
[[Category:Problem Techniques]]
 
[[Category:Problem Techniques]]
 
 
<ul>
 
<li>POD documentation: [https://webwork.maa.org/pod/pg/current/nameOfMacro.html nameOfMacro.pl]</li>
 
<li>PG macro: [https://github.com/openwebwork/pg/blob/master/macros/nameOfMacro.pl nameOfMacro.pl]</li>
 
</ul>
 

Latest revision as of 16:19, 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: Create the answers and do the usual setup.

Generate an answer name to use. 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.

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.

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

# Basic PG usage (prefer PGML)
BEGIN_TEXT
Enter \($ans\): \{$ans->named_ans_rule($ansName, 5)\}
END_TEXT
LABELED_ANS($ansName, $ans->cmp);

# Add 'less than or equal' and 'greater than or equal' MathQuill toolbar buttons.
TEXT(MODES(TeX => "", HTML => <<END_SCRIPT));
<script>
window.addEventListener('DOMContentLoaded', function() {
    if (!window.answerQuills) return;
    answerQuills.$ansName.buttons.splice(-1, 0,
        { id: 'leq', latex: '\\leq',
            tooltip: 'less than or equal (<=)',
            icon: '\\leq' },
        { id: 'geq', latex: '\\geq',
            tooltip: 'greater than or equal (>=)',
            icon: '\\geq' });
});
</script>
END_SCRIPT

ENDDOCUMENT();

Main Text: Insert all of your problem text using $ansName for the answer name. Both the PGML and basic PG usage are demonstrated. Your problem should only use one of these. If you are not using PGML you should make the switch!

An example of a possible usage of the named answer rule is shown. Here we add "greater than or equal to" and "less than or equal to" buttons to the MathQuill toolbar for the answer. This will only have effect if MathQuill is enabled for the course.

Problem Techniques Index