Difference between revisions of "PopUpLists"

From WeBWorK_wiki
Jump to navigation Jump to search
m
(add historical tag and give links to newer problems.)
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
<h2>Pop-Up Lists: PG Code Snippet</h2>
 
  +
{{historical}}
  +
  +
<p style="font-size: 120%;font-weight:bold">This problem has been replaced with [https://openwebwork.github.io/pg-docs/sample-problems/problem-techniques/SimplePopUp.html a newer version of this problem]</p>
  +
  +
<h2>Pop-Up Lists (and Solutions)</h2>
   
 
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
 
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
<em>This code snippet shows the essential PG code to use a pop-up list for answers in a WeBWorK problem. Note that these are <b>insertions</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 is the PG code to use a pop-up list (a.k.a. drop down menu) for answers. It also has the proper syntax for including solutions for students.
  +
</em>
 
</p>
 
</p>
   
Line 19: Line 25:
 
<td style="background-color:#ddffdd;border:black 1px dashed;">
 
<td style="background-color:#ddffdd;border:black 1px dashed;">
 
<pre>
 
<pre>
loadMacros("parserPopUp.pl");
 
  +
DOCUMENT();
  +
loadMacros(
  +
"PGstandard.pl",
  +
"parserPopUp.pl",
  +
);
  +
TEXT(beginproblem());
 
</pre>
 
</pre>
 
</td>
 
</td>
Line 31: Line 42:
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<pre>
 
<pre>
# the arguments of PopUp are [list of answers],
+
# the arguments of PopUp are [list of answers],
# correct answer
+
# correct answer
$popup = PopUp(["?", "one", "two", "three"], "three");
+
$popup = PopUp(["?", "one", "two", "three"], "three");
 
</pre>
 
</pre>
 
</td>
 
</td>
Line 45: Line 56:
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<pre>
 
<pre>
BEGIN_TEXT
+
BEGIN_TEXT
There is/are
+
There is/are
\{ $popup-&gt;menu() \}
+
\{ $popup-&gt;menu() \}
word(s) written below.
+
word(s) written below.
$PAR
+
$PAR
there $BR
+
there $BR
are $BR
+
are $BR
three
+
three
END_TEXT
+
END_TEXT
 
</pre>
 
</pre>
 
<td style="background-color:#ffcccc;padding:7px;">
 
<td style="background-color:#ffcccc;padding:7px;">
Line 64: Line 75:
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<pre>
 
<pre>
ANS( $popup->cmp() );
+
ANS( $popup->cmp() );
   
Context()->texStrings;
+
Context()->texStrings;
SOLUTION(EV3(<<'END_SOLUTION'));
+
SOLUTION(EV3(<<'END_SOLUTION'));
$PAR SOLUTION $PAR
+
$PAR SOLUTION $PAR
The correct answer is \{ $popup-&gt;correct_ans() \}
+
The correct answer is \{ $popup-&gt;correct_ans() \}
words.
+
words.
END_SOLUTION
+
END_SOLUTION
Context()->normalStrings;
+
Context()->normalStrings;
  +
ENDDOCUMENT();
 
</pre>
 
</pre>
 
<td style="background-color:#eeccff;padding:7px;">
 
<td style="background-color:#eeccff;padding:7px;">
Line 79: Line 90:
 
</p>
 
</p>
 
<p>
 
<p>
Note that if we want to include the answer to the pop-up list in the solution (or somewhere else), we can do that by using the <code>correct_ans()</code> method of the popup object, as suggested here.
+
Note that if we want to include the answer to the pop-up list in the solution (or somewhere else), we can do that by using the <code>correct_ans()</code> method of the popup object, as suggested here. The text block between <code>SOLUTION(EV3(<<'END_SOLUTION'));</code> and <code>END_SOLUTION</code> is what students will see when solutions are made available.
  +
</p>
  +
<p>
 
</p>
 
</p>
 
</td>
 
</td>
Line 137: Line 148:
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<pre>
 
<pre>
ANS(str_cmp( $popup->ra_correct_ans() ) ) ;
+
ANS(str_cmp( $popup->ra_correct_ans() )) ;
 
</pre>
 
</pre>
 
<td style="background-color:#eeccff;padding:7px;">
 
<td style="background-color:#eeccff;padding:7px;">
Line 149: Line 160:
 
[[IndexOfProblemTechniques|Problem Techniques Index]]
 
[[IndexOfProblemTechniques|Problem Techniques Index]]
 
</p>
 
</p>
  +
  +
[[Category:Problem Techniques]]

Latest revision as of 11:29, 16 July 2023

This article has been retained as a historical document. It is not up-to-date and the formatting may be lacking. Use the information herein with caution.

This problem has been replaced with a newer version of this problem

Pop-Up Lists (and Solutions)

This is the PG code to use a pop-up list (a.k.a. drop down menu) for answers. It also has the proper syntax for including solutions for students.

Below we consider two ways of including pop-up lists in a problem. The first is simpler, and simply introduces a pop-up somewhere in the text of the problem. The second uses old-style answer evaluators, and is more sophisticated in that it allows us to define multiple questions that use the pop-ups to define possible answers, and then allows selection of a random subset of those to display in the problem.

Problem Techniques Index

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

We don't need any additions to the tagging and description section of the WeBWorK problem, but we do need to include the parserPopUp.pl macro file in the initialization section.

# the arguments of PopUp are [list of answers], 
#    correct answer
$popup = PopUp(["?", "one", "two", "three"], "three");

Then, in the set-up section of the file we define the pop-up object, which will display as a single pop-up selector.

BEGIN_TEXT
There is/are 
\{ $popup->menu() \}
word(s) written below.
$PAR
there $BR
are $BR
three
END_TEXT

In the text section of the file, we then include the menu for the pop-up in the text as part of our problem text.

ANS( $popup->cmp() );

Context()->texStrings;
SOLUTION(EV3(<<'END_SOLUTION'));
$PAR SOLUTION $PAR
The correct answer is \{ $popup->correct_ans() \}
words.
END_SOLUTION
Context()->normalStrings;
ENDDOCUMENT();

And then we can check the answer for the pop-up list as we'd expect.

Note that if we want to include the answer to the pop-up list in the solution (or somewhere else), we can do that by using the correct_ans() method of the popup object, as suggested here. The text block between SOLUTION(EV3(<<'END_SOLUTION')); and END_SOLUTION is what students will see when solutions are made available.

Using pop-up lists with the old-style answer evaluators is somewhat more complicated, because the code is trying to do more than the new MathObjects based pop-up object is. In particular, it is designed to flexibly allow multiple pop-up questions from which a subset is selected.

PG problem file Explanation
  $popup = new_select_list();
  $popup->rf_print_q(~~&pop_up_list_print_q);
  $popup->ra_pop_up_list( [ No_answer => "",
    True => "T", False => "F" ] );

  $popup->qa("This is a pop-up question", "T",
    "This problem has two questions", "T");

  $popup->choose(2);

To include a pop-up list we need make no changes to the first two sections of the WeBWorK PG file (documentation and tagging, and problem initialization). In the set-up section, we have to define the list. Note that because we're allowing ourselves to create several pop-up questions and then select from among those, the set-up defines several questions in this case.

We first initialize a select list by calling new_select_list(), and then make it into a pop-up list with the call to rf_print_q(). After that, our variable $popup is a(n empty) pop-up list object. The next step is to add the answers that will be displayed in the list, which is done with the call to ra_pop_up_list(). This list of answers is then displayed for each of the questions that we're coding in the pop-up list object.

Next, we define the actual questions to display. This is done with the call to qa, which has a question, answer pair for each question we could display. Here we've defined two questions, and then the correct answer for each (which must be one of the answers that was defined previously).

Finally, we select some number of the questions to display. Here, we've opted to display both of the questions (in a random order).

  \{ $popup->print_q() \}

In the text section of the problem, we call print_q() to create the questions that we are displaying.

  ANS(str_cmp( $popup->ra_correct_ans() )) ;

And in the answer and solutions section, we call the ra_correct_ans() method of the popup list object.

Problem Techniques Index