Hi,
1. Create a question of type "short text". Let's call it "QOrder". Hide it with css class "d-none".
With the following script you generate a random order with one element at a fixed position
Code:
<script type="text/javascript" charset="utf-8">
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
$(document).on('ready pjax:scriptcomplete',function(){
// Define the code that has to be fixed
var fixedCode= 7;
// Fill the array with all codes, here Numbers fom 1 to 10
var arr = [];
for (var i = 1; i < 11; i++) {
arr.push(i);
}
arr = shuffle(arr);
// Find the fixed code
const index = arr.indexOf(fixedCode);
if (index > -1) { // only splice array when item is found
arr.splice(index, 1); // remove the element
}
// Insert the fixed code at his position
arr.splice(fixedCode-1, 0, fixedCode);
$('#question{QID} input[type="text"]').val(arr);
});
</script>
Now in your single resp. multiple questions enter this script
Code:
<script type="text/javascript" charset="utf-8">
$(document).on('ready pjax:scriptcomplete',function(){
//Identify this question
var thisQuestion = $('#question{QID}');
var thisAnswerList = $('li.answer-item:eq(0)', thisQuestion).parent();
// Retrieve the answer codes from the "QOrder" question
var answerCodes = '{QOrder}'.split(',');
// Loop through the answer codes
$.each(answerCodes, function(i, val) {
// Move the answer item
$(thisAnswerList).append($('li.answer-item[id$="X{QID}'+val+'"]', thisQuestion));
});
});
</script>
Joffm