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 significantly (!) simpler to create all items in a single question. First, input the items for the first construct, followed by the items for the second construct, and so forth. This approach is feasible when the total number of items to be rotated is less than 100.
Storing all items in one question significantly simplifies the rotation process: Rotation of Items or Options.
If you wish to clarify the association of items with constructs, you can customize the variable names in the question under Additional Settings → Variables.
If you have decided to store items in different questions, you need to display each item separately using a question()
command and ensure that (a) the question text appears only once at the top, and (b) the subsequent items are displayed directly below without any spacing.
Firstly, you need to create a list (Array) of all items. This array should include both the item identifier and the question identifier. In the following example, the items from questions “AB01,” “AB02,” and “AB03” are to be mixed. The following PHP code creates an empty list $items,
retrieves the items for each question (foreach
), and stores an entry in the $items
list for each item (foreach
). This entry is itself an array containing 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, for example:
[ ['AB01', 1], ['AB01', 2], ['AB01', 3], ['AB02', 1], ['AB02', 2], ['AB02', 3], ['AB03', 1], ['AB03', 2], ['AB03', 3] ]
This list is then shuffled using shuffle().
. The use of isset()
and registerItems()
ensures that the list is created and shuffled only once and not again if the page is reloaded (e.g., because not all items have been answered).
Furthermore, the following PHP code ensures that for each list entry (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'); } }