Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Customise item rotation in limesurvey matrix question

  • BBSR-SR5
  • BBSR-SR5's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
1 week 6 days ago #260551 by BBSR-SR5
Thank you very much, this works great!

It also has the advantage that the scientists can check for item order effects.

Please Log in to join the conversation.

  • BBSR-SR5
  • BBSR-SR5's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
1 week 3 hours ago - 1 week 2 hours ago #261205 by BBSR-SR5
I've been using this script successfully, but one weird thing is occuring. It's a longer table so I've been using repeated headers so people can see the meaning of the options even when they scroll down. For some reason in most cases it does seem to automatically sort the repeated header to the bottom of the list '(Kinda buggy, but useful behaviour for me since I rather like to have the header at the bottom of the table). But with my current script it's just vanishing.
Code:
<script type="text/javascript" data-author="BBSR-SR5">
  $(document).on('ready pjax:scriptcomplete',function(){
    
 
    //alert($('.modal-footer a').length)
    
    // Find the modal with the ID "bootstrap-alert-box-modal"
    var $modal = $('#bootstrap-alert-box-modal');
 
    // Check if the modal is found
    if ($modal.length > 0) {
        // Find the modal footer within the modal
        var $modalFooter = $modal.find('.modal-footer');
        var $anchors = $modalFooter.find('a');
 
        // Check if there are exactly two anchor elements
        if ($anchors.length === 2) {
            // Detach the anchor elements from the DOM
            var $firstAnchor = $anchors.eq(0).detach();
            var $secondAnchor = $anchors.eq(1).detach();
 
            // Reinsert the anchor elements in reverse order into the modal footer
            $modalFooter.append($secondAnchor).append($firstAnchor);
        }
    }
    
    //Change warning text
    // Define some text strings
    var originalModalText = "Eine oder mehrere Pflichtfragen sind nicht beantwortet worden. Bitte beantworten Sie diese zuerst, um fortzufahren!";
    var newModalText = "Sie haben auf dieser Seite mind. 1 Frage nicht beantwortet. Möchten Sie diese noch beantworten oder zur nächsten Seite weitergehen?";
 
    // Modify the modal text
    var modalBody = $('.modal-body p').filter(function() {
      return $.trim($(this).text()) == originalModalText;
    });
    if(modalBody.length > 0) {
      modalBody.text(newModalText);
    }
    
    
    
    //'' Randomisation Function begins here! **//
    //**********************************************//
 
   function shuffleArray(array) {
            for (var i = array.length - 1; i > 0; i--) {
                var j = Math.floor(Math.random() * (i + 1));
                var temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
            return array;
        }
 
        // Merge function
        function mergeArrays(array1, array2) {
            var mergedArray = ;
            var maxLength = Math.max(array1.length, array2.length);
 
            for (var i = 0; i < maxLength; i++) {
                if (i < array2.length) {
                    mergedArray.push(array2[i]);
                }
                if (i < array1.length) {
                    mergedArray.push(array1[i]);
                }
            }
 
            return mergedArray;
        }
 
        // Identify some elements and define variables
        var thisQuestion = $('#question{QID}');
        var thisAnswerList = $('tr.answers-list:eq(0)', thisQuestion).parent();
        var thisTable = $('table.subquestion-list:eq(0)', thisQuestion);
        var orderQuestion = $(thisQuestion).nextAll('.text-short:eq(0)');
        var orderInput = $(':text.form-control:eq(0)', orderQuestion);
        var array_res;
        var aOrder = ;
        var $headerRow = $("table thead tr").clone()
    
 
        // Previously set order
        if($.trim($(orderInput).val()) != '') {
            array_res = $.trim($(orderInput).val()).split(',');
        }
        // No previous order set
        else {    
            // Special codes appear after
            var afterCode = 4;
 
            //Array for K Codes
            var array_k = ["b","d","g","h","i","j","l"];
 
            //Array for N Codes
            var array_n = ["c","e","f","k","m","n"];
 
            //Array for Special K Codes
            var array_sk = ;
 
            //Array for Special N codes
            var array_sn = ["a"];
 
            //Array for super special codes
            var array_ss = ["o"];
 
            // Shuffle order of "normal" arrays
            shuffleArray(array_k);
            shuffleArray(array_n);
 
            // Slice "normal" arrays and merge second part to special codes
            array_sk = array_sk.concat(array_k.slice(afterCode));
            array_sn = array_sn.concat(array_n.slice(afterCode));
 
            // Save only first part of "normal" arrays
            array_k = array_k.slice(0, afterCode);
            array_n = array_n.slice(0, afterCode);
 
            // Shuffle special arrays
            shuffleArray(array_sk);
            shuffleArray(array_sn);
            shuffleArray(array_ss);
 
            // Combine normal and special arrays
            array_k = array_k.concat(array_sk);
            array_n = array_n.concat(array_sn);
 
            // Merge arrays in order n,k,n,k...
            array_res = mergeArrays(array_k, array_n);
 
            //Add super special array to end of result array
            array_res = array_res.concat(array_ss);
 
            // Load the hidden order input
            $(orderInput).val(array_res);
        }
 
        // Loop through the answer codes
        $.each(array_res, function(i, val) {
            // Move the answer item
            $(thisAnswerList).append($('tr.answers-list[id$="X{QID}'+val+'"]', thisQuestion));
        });
    
      
 
        // Fix up the row background colours
        $('tr.answers-list', thisQuestion). each (function(i){
            $(this).removeClass('ls-even ls-odd');
            if(i % 2 == 0) {
                $(this).addClass('ls-even');
            }
            else {
                $(this).addClass('ls-odd');
            }
        });
    });   
 
 //$('tr.answers-list:eq(0)', thisQuestion).append('<tr class="header-row"></tr>');
 
</script>[/i][/i][/i][/i]

I've been trying to just copy the header and paste it to the bottom of the table, but my code doesn't work. Any ideas how to fix/do this?
Last edit: 1 week 2 hours ago by tpartner. Reason: Remove invalid credit

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 week 2 hours ago #261239 by tpartner
I have removed my name from your script credit - I did not code the randomization.

If you want to duplicate the headers at the bottom of the table, try this after your randomization. (it will result in slightly invalid HTML)

Code:
$('table.subquestion-list', thisQuestion).append($('table.subquestion-list thead', thisQuestion).clone());

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

  • BBSR-SR5
  • BBSR-SR5's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
6 days 10 hours ago #261274 by BBSR-SR5
Thank you this works great.

Apologies for giving wrong credit. I've been using a lot of your code and copying the scripts between questions, so it apparently remained in a question that had very little of your code left.

Please Log in to join the conversation.

Lime-years ahead

Online-surveys for every purse and purpose