PopUpLists
Pop-Up Lists: PG Code Snippet
This code snippet shows the essential PG code to use a pop-up list for answers in a WeBWorK problem. Note that these are insertions, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.
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.
PG problem file | Explanation |
---|---|
loadMacros("parserPopUp.pl"); |
We don't need any additions to the tagging and description section of the WeBWorK problem, but we do need to include the |
# 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 |
ANS( $popup->cmp() ); |
And then we can check the answer for the pop-up list as we'd expect. |
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
Next, we define the actual questions to display. This is done with the call to 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 |
ANS(str_cmp( $popup->ra_correct_ans() ) ) ; |
And in the answer and solutions section, we call the |