On a website I'm currently using Webform on, the requirement of finer grained settings of option randomization came up. In questionnaires, where answers include an "I don't know" item, it is often required to keep this item out from randomization and putting it to the beginning or the end of the list, in a fixed position. This is the solution I came up with. Sorry, I don't have the time now to roll it into a patch but somebody might still need this functionality, use and test it and eventually, providing a patch for it.
The file to modify is select.inc. Look for the string 'optrand' to find the three blocks to replace:
$form['display']['optrand'] = array(
'#type' => 'select',
'#title' => t('Display order'),
'#options' => array(0 => t('Keep original order'), 1 => t('Randomize options'), 2 => t('Randomize but keep item 0 as first'), 3 => t('Randomize but keep item 0 as last')),
'#default_value' => $component['extra']['optrand'],
'#description' => t('Determines the order of the options when they are displayed in the form.'),
'#parents' => array('extra', 'optrand'),
);
switch ($component['extra']['optrand']) {
case 1:
_webform_shuffle_options($options);
break;
case 2:
_webform_shuffle_options($options, true, false);
break;
case 3:
_webform_shuffle_options($options, false, true);
break;
}
function _webform_shuffle_options(&$array, $zero_to_first = false, $zero_to_last = false) {
// First shuffle the array keys, then use them as the basis for ordering
// the options.
$aux = array();
$keys = array_keys($array);
shuffle($keys);
$look_for_zero = $zero_to_first || $zero_to_last;
if ($zero_to_first) {
$aux[0] = $array[0];
}
foreach ($keys as $key) {
if (($look_for_zero && $key != 0) || !$look_for_zero) {
$aux[$key] = $array[$key];
}
}
if ($zero_to_last) {
$aux[0] = $array[0];
}
$array = $aux;
}
The idea is really simple, if there is an option with zero as its key, this code will offer a setting to specify this extra requirement. The handling uses the same data as before, there is no need to introduce new variables and all previous webforms keep functioning as before.
Comments
Comment #1
danchadwick commentedThere won't be further feature development in the X.x-3.x branches.