I’m trying to set a deterministic randomization order across four blocks (DS, HPB, MVS, AM) using setPageOrder() in a PHP page (RAND), directly after a Random Generator (B80). The function works only if I start the questionnaire from mid-point, but not from page 1. It seems value('B801') is not yet available when RAND runs. Could you confirm the correct timing or recommend the best practice for this setup?
Here the code:
/* =====================================================
RANDOMIZATION – BASE (ES) with Random Generator B801
===================================================== */
/* 1) Read the generator value 1..24 (Page 06) */
$rg = (int) value('B80'); // <-- B80, without _01
registerVariable($rg);
if ($rg < 1 || $rg > 24) { $rg = mt_rand(1, 24); }
/* 2) Define blocks (IDs of your pages) */
$DS = array('10','11','12','13','14');
$HPB = array('15','16','17','18','19');
$MVS = array('20','21','22','23','24');
$AM = array('25','26','27','28','29');
$TAIL = array('A27','A28','A30'); // without 'end'
$labels = array('DS','HPB','MVS','AM');
/* 3) 24 permutations */
$orders = array(
array(0,1,2,3), array(0,1,3,2), array(0,2,1,3), array(0,2,3,1),
array(0,3,1,2), array(0,3,2,1),
array(1,0,2,3), array(1,0,3,2), array(1,2,0,3), array(1,2,3,0),
array(1,3,0,2), array(1,3,2,0),
array(2,0,1,3), array(2,0,3,1), array(2,1,0,3), array(2,1,3,0),
array(2,3,0,1), array(2,3,1,0),
array(3,0,1,2), array(3,0,2,1), array(3,1,0,2), array(3,1,2,0),
array(3,2,0,1), array(3,2,1,0)
);
/* 4) Build sequence */
$orderIdxs = $orders[$rg - 1];
$blocks = array($DS, $HPB, $MVS, $AM);
$sequence = array();
$labelsOrder = array();
foreach ($orderIdxs as $i) {
$sequence = array_merge($sequence, $blocks[$i]);
$labelsOrder[] = $labels[$i];
}
if (!empty($TAIL)) { $sequence = array_merge($sequence, $TAIL); }
$sequence = array_values(array_unique($sequence));
/* 5) Set the order ONCE and save traces */
setPageOrder($sequence);
put('F301_01', implode('-', $labelsOrder)); // human-readable order
put('F301_02', $rg); // index 1..24
put('F301_03', implode('-', $sequence)); // page IDs in order
/* 6) (Optional during tests) Show the computed order on screen */
// debug('RG=' . $rg . ' | ORDER=' . implode('-', $labelsOrder) . ' | SEQ=' . implode(' > ', $sequence));