This guide explains how to mix items from various questions (Rotation) and display them together on one or more pages.
In most cases it is much (!) easier to create the items all in one question. First the items of the first construct, then the items of the second construct and so on. This is possible if you want to rotate less than 100 items in total.
Storing all items in one question simplifies the rotation considerably: Rotation of Items or Options.
If you would like to make it even clearer how the items belong to the constructs, you can adjust the names of the variables accordingly in the question in Detail Settings → Variables.
If you have decided that the items should be stored in different questions, you have to display each item separately with a question()
-command and make sure that (a) the question text appears only once at the top and (b) the other items are displayed directly below without any space between them.
First of all you have to create a list (Array) of all items. In this array you have to add the question identifier as well as the item identifier. In the following example the items of the questions „AB01“, „AB02“ and „AB03“ are to be mixed. The following PHP code creates an empty list $items
, determines for each (foreach
) question its items and stores for each (foreach
) of them an entry in the list $items
. This entry is again an array, which contains the question identifier and the item number.
$questions = ['AB01', 'AB02', 'AB03']; $items = []; foreach ($questions as $qstID) { $qstItems = getItems($qstID, 'all'); foreach ($qstItems as $itemID) { $items[] = [$qstID, $itemID]; } }
The result in $items
could look like this:
[ ['AB01', 1], ['AB01', 2], ['AB01', 3], ['AB02', 1], ['AB02', 2], ['AB02', 3], ['AB03', 1], ['AB03', 2], ['AB03', 3] ]
This list is now shuffled using shuffle()
. Using isset()
and registerItems()
ensures that the list is created and shuffled only once and not again when the page is reloaded (e.g. because not all items have been answered).
Furthermore, the following PHP code ensures that for each list item (foreach
) the respective item from the list is displayed using question()
.
foreach ($items as $item) { $qstID = $item[0]; $itemID = $item[1]; question($qstID, [$itemID], 'spacing=0', 'show-title=no', 'show-explanation=no'); }
Or written a little more compactly:
foreach ($items as $item) { question($item[0], [$item[1]], 'spacing=0', 'show-title=no', 'show-explanation=no'); }
Of course the question text should be above the items. The complete PHP code looks like this:
if (!isset($items)) { $questions = ['AB01', 'AB02', 'AB03']; // Create a list with all items $items = []; foreach ($questions as $qstID) { $qstItems = getItems($qstID, 'all'); foreach ($qstItems as $itemID) { $items[] = [$qstID, $itemID]; } } // Merge list shuffle($items); registerVariable($items); } // show question text question('AB01', 'spacing=0', 'show-items=no'); // show items foreach ($items as $item) { question($item[0], [$item[1]], 'spacing=0', 'show-title=no', 'show-explanation=no'); }
If you don't want to show all items on one page, you can split the list with array_chunk()
(in the following example in blocks of 12 items each) and present each of them on a separate page..
if (!isset($itemBlocks)) { $questions = ['AB01', 'AB02', 'AB03']; // Create a list with all items $items = []; foreach ($questions as $qstID) { $qstItems = getItems($qstID, 'all'); foreach ($qstItem as $itemID) { $items[] = [$qstID, $itemID]; } } // merge list shuffle($items); // split the list $itemBlocks = array_chunk($items, 12); registerVariable($itemBlocks); } // present on several pages $i = loopPage(count($itemBlocks)); // show question text question('AB01', 'spacing=0', 'show-items=no'); // show items $items = $itemBlocks[$i]; foreach ($items as $item) { question($item[0], [$item[1]], 'spacing=0', 'show-title=no', 'show-explanation=no'); }
The automatic shading of items no longer works here, because technically speaking, each item is now the first (and only) item in the presented question. But you can use the question()
command:
foreach ($items as $n => $item) { if ($n % 2 == 0) { question($item[0], [$item[1]], 'spacing=0', 'show-title=no', 'show-explanation=no', 'shading=all-shaded'); } else { question($item[0], [$item[1]], 'spacing=0', 'show-title=no', 'show-explanation=no', 'shading=all-unshaded'); } }