Hi,
I want to randomize the order of questions in a question group.
I used this javascript which i found in the wiki and pasted it in the code format field group desctiption:
Code:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
function shuffleQuestions() {
// Create an array to hold question IDs
var qArray = new Array();
// Find the group ID of the page
var fieldNames = $( 'input#fieldnames' ).attr('value');
var tmp = fieldNames.split('X');
var sID = tmp[0];
var gID = tmp[1];
// A function to get the value of a cookie by name
function getCookie ( cookieName ) {
var results = document.cookie.match ( '(^|;) ?' + cookieName + '=([^;]*)(;|$)' );
if ( results ) {
return ( unescape ( results[2] ) );
}
else {
return null;
}
}
// Get the session name
var sessionName = getCookie ( 'PHPSESSID' );
// Check to see if the question order has already been shuffled in this session
var cookieArray = getCookie ( 'sArray' + gID + '_' + sessionName );
// If already shuffled, use that question order
if ( cookieArray ) {
//qArray = $.trim(cookieArray);
qArray = cookieArray.split(',');
}
// If not, go ahead and shuffle
else {
// Load the IDs of all questions on the page into the array
$( 'div[id^="question"]' ).each(function(i) {
qArray.push($( this ).attr('id'));
});
// Shuffle the questions array
function shuffle(o){
for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
}
shuffle(qArray);
// Set a cookie with the value of the shuffled array
var today = new Date();
today.setTime( today.getTime() );
var expires = 2 * 1000 * 60 * 60 * 24; // Expire the cookie in 2 days
var expiresDate = new Date( today.getTime() + (expires) );
document.cookie = 'sArray' + gID + '_' + sessionName + '=' + qArray + ';expires=' + expiresDate.toGMTString();
}
// Wrap the questions in a div
$( 'div[id^="question"]' ).wrapAll( document.createElement('div') );
$( 'div[id^="question"]:eq(0)' ).parent().attr('id', 'qWrapper');
$( 'div#qWrapper' ).css({
'padding':0,
'margin':0
});
// Insert the questions into the wrapper in the shuffled order
$.each(qArray, function(i, val){
$( 'div#' + val + '' ).appendTo( $( '#qWrapper' ) );
});
}
shuffleQuestions();
});
</script>
In the global options i have set Filter HTML XSS to 'No'. But when i use the test button to test the survey the questions in that group are NOT randomized.
I'm using Limesurvey Version 1.92+ Build 120801.
What could be causing it not to work?
In this group there is one question that has a condition which is related to another question in the same group. I suppose i should remove that one?
There are also 3 other questions which should only be shown when a condition is met but the are related to answers on questions in previous groups. I suppose that should not be a problem?