PopUpLists

From WeBWorK_wiki
Revision as of 15:29, 1 February 2008 by Glarose (talk | contribs)
Jump to navigation Jump to search

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.

Problem Techniques Index

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 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;

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.

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