Difference between revisions of "PopUpListsLong"

From WeBWorK_wiki
Jump to navigation Jump to search
(add historical tag and give links to newer problems.)
 
(41 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<h2>Pop-Up Lists For Many Questions With Common Answers: 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/Misc/ManyMultipleChoice.html a newer version of this problem]</p>
  +
  +
  +
<h2>Pop-Up Lists For Many Questions With Common Answers</h2>
  +
  +
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
  +
This is the PG code for using pop-up menus displaying multiple questions with a common set of possible answers, such as a list of true or false questions or a list of multiple choice questions in which the choices are the same for each question.
  +
</p>
  +
<ul type="square">
  +
<li>Example 1: (Recommended) The newer method.</li>
  +
<li>Example 2: (Not Recommended) The older method, which perhaps offers more options for customization.</li>
  +
</ul>
  +
  +
  +
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
  +
<em><b>Example 1:</b> This code shows how to create lengthy pop-up lists in a WeBWorK problem using newer constructs. This example comes from /Library/Rochester/setMAAtutorial/popuplistexample.pg and you are also encouraged to look at Library/Rochester/setMAAtutorial/prob4.pg</em>
  +
</p>
  +
  +
<p style="text-align:center;">
  +
[[IndexOfProblemTechniques|Problem Techniques Index]]
  +
</p>
  +
<table cellspacing="0" cellpadding="2" border="0">
  +
<tr valign="top">
  +
<th> PG problem file </th>
  +
<th> Explanation </th>
  +
</tr>
  +
<tr valign="top">
  +
<td style="background-color:#ddffdd;border:black 1px dashed;">
  +
<pre>
  +
DOCUMENT();
  +
  +
loadMacros(
  +
"PGstandard.pl",
  +
"PGchoicemacros.pl",
  +
#"PGgraders.pl",
  +
);
  +
  +
TEXT(beginproblem());
  +
</pre>
  +
</td>
  +
<td style="background-color:#ccffcc;padding:7px;">
 
<p>
 
<p>
Often you may have many questions that have the same set of answers, such as a list of true or false questions, or a list of multiple choice questions in which the choices are the same for each question. This PG code allows takes the hassle out of creating such a PG problem by requiring only that you modify an array of questions and answers, the instructions for the student, and the list of choices for every question. To prevent students from being able to copy each other's answers verbatim, we shuffle the order of the questions. It is also possible to include random elements into each question (but not each answer, since the list of answers is a list of strings and needs to be the same for all questions).
 
  +
<b>Initialization:</b> to use these pop-up lists, load <code>PGchoicemacros.pl</code>. Note: <code>PGstandard.pl</code> simply loads <code>PG.pl, PGbasicmacros.pl, PGanswermacros.pl</code>.
 
</p>
 
</p>
  +
</td>
  +
</tr>
  +
<tr valign="top">
  +
<td style="background-color:#ffffdd;border:black 1px dashed;">
  +
<pre>
  +
# Create and use pop up lists
  +
$tf = new_select_list();
  +
$tf->rf_print_q(~~&pop_up_list_print_q);
  +
  +
# Specify choices presented to students
  +
$tf->ra_pop_up_list(
  +
[ No_answer => "?", "T"=>"True", "F"=>"False"] );
  +
  +
# Questions and answers
  +
$tf -> qa (
  +
"All continuous functions are differentiable.",
  +
"F",
  +
"All differentiable functions are continuous.",
  +
"T",
  +
"All polynomials are differentiable.",
  +
"T",
  +
"All functions with positive derivatives " .
  +
"are increasing.",
  +
"T",
  +
);
  +
  +
# How many questions to use
  +
$tf->choose(3);
  +
</pre>
  +
</td>
  +
<td style="background-color:#ffffcc;padding:7px;">
  +
<p>
  +
<b>Setup:</b>
  +
We make a new select list $tf that "contains" the select list object.
  +
Then, the line specifying the "printq" function changes the printing mechanism of the object to
  +
use a pop-up list instead of an answer rule.
  +
</p>
  +
<p>
  +
The line specifying "ra_pop_up_list" gives a list of the choices that are presented for each question. The format of the list reference is
  +
<code>[answer => list_element_text, answer => list_element_text]</code>:
  +
the text that the the pop-up list should contain, and the string it should
  +
submit for an answer when selected. In this example, if the student selects
  +
"True," the pop-up will submit "T," and similarly "F" for "False." If do not want to use abbreviations, use <code>"True"=>"True"</code> instead.
  +
The first choice is a blank to make the students select something.
  +
</p>
  +
<p>
  +
Next, we specify some questions and their answers, as a comma-separated list of question and answer pairs. At the end, we specify how many of these to display to the students.
  +
</p>
  +
<p>
  +
(Note that we've broken some of these questions into pieces to avoid long lines on this web page, by using string concatenation. In Perl, the period concatenates strings: thus, <code>"a" . "b"</code> is the same as the string <code>ab</code>.)
  +
</p>
  +
</td>
  +
</tr>
  +
<tr valign="top">
  +
<td style="background-color:#ffdddd;border:black 1px dashed;">
  +
<pre>
  +
BEGIN_TEXT
  +
Are the following statements true or false?
  +
$BR
  +
\{ $tf-> print_q \}
  +
END_TEXT
  +
</pre>
  +
<td style="background-color:#ffcccc;padding:7px;">
  +
<p>
  +
<b>Main Text:</b>
  +
You may modify the instructions if you want. You should explicitly say what all of the possible answers are so that students who have a pdf or paper copy can work directly from it.
  +
</p>
  +
</td>
  +
</tr>
  +
<tr valign="top">
  +
<td style="background-color:#eeddff;border:black 1px dashed;">
  +
<pre>
  +
install_problem_grader(~~&std_problem_grader);
  +
$showPartialCorrectAnswers = 0;
  +
  +
ANS(str_cmp($tf->ra_correct_ans));
  +
  +
ENDDOCUMENT();
  +
</pre>
  +
<td style="background-color:#eeccff;padding:7px;">
  +
<p>
  +
<b>Answer Evaluation:</b>
  +
Install the standard problem grader <code>~~&std_problem_grader</code> to withhold credit until all questions are answered correctly, and set <code>$showPartialCorrectAnswers = 0;</code> to withhold feedback on whether answers to individual questions are correct (to prevent students from guessing).
  +
</p>
  +
<p>
  +
Instead of having an all-or-nothing grader, it is possible to have an incremental weighted grader that will give students a custom score for having a certain number of answers correct. For example, suppose that we have 8 questions in <code>$tf->qa(...)</code>, all of which we want to use, and we want to give students 0% credit for 0-1 answers correct, 10% credit for 2-4 answers correct, 60% credit for 5-6 answers correct, 80% credit for 7 answers correct, and full credit for all 8 answers correct. This can be done using the <code>custom_problem_grader_fluid</code> provided by the <code>PGgraders.pl</code> macro. First, we would need to load the macro <code>PGgraders.pl</code> in the Initialization section (Note: You will need a version of PGgraders.pl updated on or after Sept. 16, 2010). Then, instead of using the standard problem grader, we would use the following.
  +
<pre>
  +
install_problem_grader(~~&custom_problem_grader_fluid);
  +
$ENV{'grader_numright'} = [2,5,7,8];
  +
$ENV{'grader_scores'} = [0.1,0.6,0.8,1];
  +
$ENV{'grader_message'} = "You can earn " .
  +
"10% partial credit for 2 - 4 correct answers, " .
  +
"60% partial credit for 5 - 6 correct answers, and " .
  +
"80% partial credit for 7 correct answers.";
  +
  +
$showPartialCorrectAnswers = 0;
  +
  +
ANS(str_cmp($tf->ra_correct_ans));
  +
  +
ENDDOCUMENT();
  +
</pre>
  +
Notice that the last entry "8" in <code>grader_numright</code> must be the total number of questions, and the last entry "1" in <code>grader_scores</code> must be "1" to award full credit (100%).
  +
</p>
  +
</td>
  +
</tr>
  +
</table>
  +
  +
  +
  +
  +
  +
  +
  +
   
 
<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 PG code to create lengthy pop-up lists 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><b>Example 2:</b> This code shows how to create lengthy pop-up lists in a WeBWorK problem using older-style answer checkers, some deprecated elements such as shuffle, and elementary bits of code to build things from scratch.</em>
 
</p>
 
</p>
   
Line 22: Line 178:
   
 
loadMacros(
 
loadMacros(
"PG.pl",
+
"PG.pl",
"PGstandard.pl",
+
"PGbasicmacros.pl",
"PGcourse.pl",
+
"PGchoicemacros.pl",
"PGchoicemacros.pl",
+
"PGanswermacros.pl",
  +
"PGcourse.pl",
 
);
 
);
   
TEXT(beginproblem);
+
TEXT(beginproblem());
 
</pre>
 
</pre>
 
</td>
 
</td>
 
<td style="background-color:#ccffcc;padding:7px;">
 
<td style="background-color:#ccffcc;padding:7px;">
 
<p>
 
<p>
Initialization: We need to include the <code>PGchoicemacros.pl</code> macro file.
+
Initialization: We need to include several macro files.
 
</p>
 
</p>
 
</td>
 
</td>
Line 43: Line 199:
   
 
@questions_and_answers = (
 
@questions_and_answers = (
"Larry is one of the three stooges.","True",
+
"Larry is one of the three stooges.",
"Gandalf is one of the three stooges.","False",
+
"True",
"\(\pi + $a\) is rational.","False",
+
"Gandalf is one of the three stooges.",
  +
"False",
  +
"\(\pi + $a\) is rational.",
  +
"False",
 
);
 
);
  +
  +
@choices = ("True", "False");
 
</pre>
 
</pre>
 
</td>
 
</td>
Line 53: Line 211:
 
Set-up: Create an array in which questions alternate with answers. Answers may be any string.
 
Set-up: Create an array in which questions alternate with answers. Answers may be any string.
 
For true or false questions, this is the only part of the example that needs to be modified.
 
For true or false questions, this is the only part of the example that needs to be modified.
We added a randomized element to the question about pi.
+
We added a randomized element to the question about pi. Do not use the names $numq, $qn, or $qns
  +
to avoid conflicts with the code below.
 
</p>
 
</p>
 
</td>
 
</td>
Line 61: Line 219:
 
<pre>
 
<pre>
 
BEGIN_TEXT
 
BEGIN_TEXT
 
 
Are the following statements true or false?
 
Are the following statements true or false?
$BR$BR
+
$BR
+
$BR
 
END_TEXT
 
END_TEXT
 
</pre>
 
</pre>
Line 76: Line 233:
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<pre>
 
<pre>
$n = scalar(@questions_and_answers)/2;
 
  +
# $numq = number of questions
@shuffle = shuffle($n);
 
  +
$numq = scalar(@questions_and_answers)/2;
@abc = (a..z);
 
  +
  +
@shuffle = shuffle($numq);
  +
@abc = (a..z); # or (A..Z) or (1..$numq)
  +
  +
# $qn = question number
  +
for $qn (0..$numq-1) {
  +
# $qns = question number shuffled
  +
$qns = $shuffle[$qn];
   
for $jj (0..$n-1) {
 
$j = $shuffle[$jj];
 
 
BEGIN_TEXT
 
BEGIN_TEXT
@abc[$jj].
+
@abc[$qn].
\{ pop_up_list(['?', 'True', 'False']) \} $SPACE
+
\{ pop_up_list(['?', @choices]) \}
$questions_and_answers[2*$j]
+
$SPACE
  +
$questions_and_answers[2*$qns]
 
$BR$BR
 
$BR$BR
 
END_TEXT
 
END_TEXT
ANS(str_cmp($questions_and_answers[2*$j+1], filters=>["trim_whitespace","compress_whitespace"]));
 
  +
  +
ANS(str_cmp($questions_and_answers[2*$qns+1],
  +
filters=>["trim_whitespace","compress_whitespace"]));
 
}
 
}
  +
  +
install_problem_grader(~~&std_problem_grader);
  +
$showPartialCorrectAnswers = 0;
   
 
ENDDOCUMENT();
 
ENDDOCUMENT();
Line 95: Line 262:
 
<td style="background-color:#eeccff;padding:7px;">
 
<td style="background-color:#eeccff;padding:7px;">
 
<p>
 
<p>
Main Text and Answer Evaluation: Shuffle the order of the questions and use a for loop to generate one question and one answer evaluator for each pair in the array @questions_and_answers. You can change the pop-up list options ['?','True','False'] to any number of strings you want.
+
Main Text and Answer Evaluation: You should not need to modify this section at all. We shuffle the order of the questions and use a for loop to generate one question and one answer evaluator for each pair in the array @questions_and_answers. Install the <code>~~&std_problem_grader</code> if you want students to have to answer all questions correctly to receive any credit.
 
</p>
 
</p>
 
</td>
 
</td>

Latest revision as of 12:30, 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 For Many Questions With Common Answers

This is the PG code for using pop-up menus displaying multiple questions with a common set of possible answers, such as a list of true or false questions or a list of multiple choice questions in which the choices are the same for each question.

  • Example 1: (Recommended) The newer method.
  • Example 2: (Not Recommended) The older method, which perhaps offers more options for customization.


Example 1: This code shows how to create lengthy pop-up lists in a WeBWorK problem using newer constructs. This example comes from /Library/Rochester/setMAAtutorial/popuplistexample.pg and you are also encouraged to look at Library/Rochester/setMAAtutorial/prob4.pg

Problem Techniques Index

PG problem file Explanation
DOCUMENT();

loadMacros(
"PGstandard.pl",
"PGchoicemacros.pl",
#"PGgraders.pl",
);

TEXT(beginproblem());

Initialization: to use these pop-up lists, load PGchoicemacros.pl. Note: PGstandard.pl simply loads PG.pl, PGbasicmacros.pl, PGanswermacros.pl.

# Create and use pop up lists 
$tf = new_select_list();
$tf->rf_print_q(~~&pop_up_list_print_q);

# Specify choices presented to students
$tf->ra_pop_up_list( 
[ No_answer => "?", "T"=>"True", "F"=>"False"] );

# Questions and answers
$tf -> qa ( 
"All continuous functions are differentiable.",
"F",
"All differentiable functions are continuous.",
"T",
"All polynomials are differentiable.",
"T",
"All functions with positive derivatives " .
    "are increasing.",
"T",
);

# How many questions to use
$tf->choose(3);

Setup: We make a new select list $tf that "contains" the select list object. Then, the line specifying the "printq" function changes the printing mechanism of the object to use a pop-up list instead of an answer rule.

The line specifying "ra_pop_up_list" gives a list of the choices that are presented for each question. The format of the list reference is [answer => list_element_text, answer => list_element_text]: the text that the the pop-up list should contain, and the string it should submit for an answer when selected. In this example, if the student selects "True," the pop-up will submit "T," and similarly "F" for "False." If do not want to use abbreviations, use "True"=>"True" instead. The first choice is a blank to make the students select something.

Next, we specify some questions and their answers, as a comma-separated list of question and answer pairs. At the end, we specify how many of these to display to the students.

(Note that we've broken some of these questions into pieces to avoid long lines on this web page, by using string concatenation. In Perl, the period concatenates strings: thus, "a" . "b" is the same as the string ab.)

BEGIN_TEXT
Are the following statements true or false? 
$BR
\{ $tf-> print_q \}
END_TEXT

Main Text: You may modify the instructions if you want. You should explicitly say what all of the possible answers are so that students who have a pdf or paper copy can work directly from it.

install_problem_grader(~~&std_problem_grader);
$showPartialCorrectAnswers = 0;

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

ENDDOCUMENT();

Answer Evaluation: Install the standard problem grader ~~&std_problem_grader to withhold credit until all questions are answered correctly, and set $showPartialCorrectAnswers = 0; to withhold feedback on whether answers to individual questions are correct (to prevent students from guessing).

Instead of having an all-or-nothing grader, it is possible to have an incremental weighted grader that will give students a custom score for having a certain number of answers correct. For example, suppose that we have 8 questions in $tf->qa(...), all of which we want to use, and we want to give students 0% credit for 0-1 answers correct, 10% credit for 2-4 answers correct, 60% credit for 5-6 answers correct, 80% credit for 7 answers correct, and full credit for all 8 answers correct. This can be done using the custom_problem_grader_fluid provided by the PGgraders.pl macro. First, we would need to load the macro PGgraders.pl in the Initialization section (Note: You will need a version of PGgraders.pl updated on or after Sept. 16, 2010). Then, instead of using the standard problem grader, we would use the following.

install_problem_grader(~~&custom_problem_grader_fluid);
$ENV{'grader_numright'} = [2,5,7,8];
$ENV{'grader_scores'} = [0.1,0.6,0.8,1];
$ENV{'grader_message'} = "You can earn " .
"10% partial credit for 2 - 4 correct answers, " .
"60% partial credit for 5 - 6 correct answers, and " .
"80% partial credit for 7 correct answers.";

$showPartialCorrectAnswers = 0;

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

ENDDOCUMENT(); 

Notice that the last entry "8" in grader_numright must be the total number of questions, and the last entry "1" in grader_scores must be "1" to award full credit (100%).





Example 2: This code shows how to create lengthy pop-up lists in a WeBWorK problem using older-style answer checkers, some deprecated elements such as shuffle, and elementary bits of code to build things from scratch.

Problem Techniques Index

PG problem file Explanation
DOCUMENT();

loadMacros(
"PG.pl",
"PGbasicmacros.pl",
"PGchoicemacros.pl",
"PGanswermacros.pl",
"PGcourse.pl",
);

TEXT(beginproblem());

Initialization: We need to include several macro files.

$a = random(1,5,1);

@questions_and_answers = (
"Larry is one of the three stooges.",
"True",  
"Gandalf is one of the three stooges.",
"False", 
"\(\pi + $a\) is rational.",
"False", 
);

@choices = ("True", "False");

Set-up: Create an array in which questions alternate with answers. Answers may be any string. For true or false questions, this is the only part of the example that needs to be modified. We added a randomized element to the question about pi. Do not use the names $numq, $qn, or $qns to avoid conflicts with the code below.

BEGIN_TEXT
Are the following statements true or false? 
$BR
$BR
END_TEXT

Instructions and Question for all parts: You may modify the instructions if you want. You should explicitly say what all of the possible answers are so that students who have a pdf or a paper copy can work directly from it.

# $numq = number of questions
$numq = scalar(@questions_and_answers)/2;

@shuffle = shuffle($numq);
@abc = (a..z); # or (A..Z) or (1..$numq)

# $qn  = question number
for $qn (0..$numq-1) { 
# $qns = question number shuffled
$qns = $shuffle[$qn];  

BEGIN_TEXT
@abc[$qn]. 
\{ pop_up_list(['?', @choices]) \}
$SPACE 
$questions_and_answers[2*$qns]
$BR$BR
END_TEXT

ANS(str_cmp($questions_and_answers[2*$qns+1], 
filters=>["trim_whitespace","compress_whitespace"]));
}

install_problem_grader(~~&std_problem_grader);
$showPartialCorrectAnswers = 0;

ENDDOCUMENT(); 

Main Text and Answer Evaluation: You should not need to modify this section at all. We shuffle the order of the questions and use a for loop to generate one question and one answer evaluator for each pair in the array @questions_and_answers. Install the ~~&std_problem_grader if you want students to have to answer all questions correctly to receive any credit.

Problem Techniques Index