WeBWorK Problems

Multiple MC questions

Multiple MC questions

by Robert Mařík -
Number of replies: 6

Hello, I use the attached file to define several multiple-choice questions on one page. It works reasonably fine, but I would like to replace the spaghetti code bellow by some cycle. Is it possible?

Thank you. Robert Marik

Code:
### [@ $nadpis[0] @]

[@ $text[0] @]

[_]{$radio[0]}

-------------------
### [@ $nadpis[1] @]

[@ $text[1] @]

[_]{$radio[1]}
-------------------
### [@ $nadpis[2] @]

[@ $text[2] @]

[_]{$radio[2]}
-------------------
In reply to Robert Mařík

Re: Multiple MC questions

by Danny Glin -

You can put a loop outside a BEGIN_PGML / END_PGML block:

foreach $i (0..2) {

BEGIN_PGML

### [@ $nadpis[$i] @]

[@ $text[$i] @]

[_]{$radio[$i]}

-------------------

END_PGML

}


In reply to Danny Glin

Re: Multiple MC questions

by Robert Mařík -

Thank you very much. The following works.

foreach $i (0..2) {

BEGIN_PGML

### [$nadpis[$i] ]

[$text[$i] ]

[_]{$radio[$i]}

-------------------

END_PGML

}

I would like to use the same construction in BEGIN_PGML_SOLUTION and END_PGML_SOLUTION and in this case the solution does not work, it produces multiple "Solutions" links.

Any other idea?


Thank you a lot. Robert

In reply to Robert Mařík

Re: Multiple MC questions

by Alex Jordan -
You could loop through and build the answer string before the PGML solution, then drop it in. Like the following, but modify for however your answers should be phrased.

$answer = '';
for my $i (0..2) {$answer .= "The answer is [@~~$radio[$i]->correct_ans()@].~~n~~n"}

BEGIN_PGML_SOLUTION

[$answer]**

END_PGML_SOLUTION



The ~~ and the ** are things that can be explained if needed.
In reply to Alex Jordan

Re: Multiple MC questions

by Robert Mařík -

Thank you. I finally will use the other solution with more BEGIN/END_PGML_SOLUTION blocks, but your idea how to escape things will be useful in other problems. Thank you.

In reply to Robert Mařík

Re: Multiple MC questions

by Danny Glin -

If your solutions correspond to the MC questions, then you can put the PGML and the PGML solution in the same loop.  This will put the solution to each question immediately after the question.

foreach $i (0..2) {

BEGIN_PGML

### [$nadpis[$i] ]

[$text[$i] ]

[_]{$radio[$i]}

-------------------

END_PGML

BEGIN_PGML_SOLUTION

[$solutiontext[$i]]

END_PGML_SOLUTION

}


In reply to Danny Glin

Re: Multiple MC questions

by Robert Mařík -
Thank you. I was not aware of the fact that there can be more PGML_SOLUTION blocks. This code is fine to me, since in fact I have about 10 MC questions on a page and it is better to place the solution immediately after the question.

Robert