Welcome to the LimeSurvey Community Forum

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

Array (numbers): randomized order of columns with last column as exclusive optio

  • Sidvic
  • Sidvic's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
1 year 9 months ago #230478 by Sidvic
Please help us help you and fill where relevant:
Your LimeSurvey version: 5.3.23+220705
Own server or LimeSurvey hosting:  own server
Survey theme/template: fruity
==================

Hi,

First of all, I would like to thank you for the great work you do! The answers in the forum have been very helpful for me throughout the years. 

There is just one thing that I am unable to find. 

For one array (number) question (see attachment: test survey as lss), I combined two scripts that allow to:
1) randomize the columns
2) make the last column exclusive

The problem is that the last column is also included in the randomization...

Do you maybe have a script or any other solution that allows to:
1) randomize the columns
2) while keeping the last column always at the end
3) allowing in this way the answer option of the column to be the exclusive option (and not the one that happened to be at the end because of rotation)

By the way, in questions which allow multiple answers, when someone clicks on the exclusive option, all the other answers options look kind of "deactivated" and have a less intense color. Is it possible to create the same effect for an array (numbers) question, when the exclusive option  

File Attachment:

File Name: limesurvey...9128.lss
File Size:34 KB
 

File Attachment:

File Name: limesurvey...9128.lss
File Size:34 KB
is selected?

Thank you very much!

Best regards,
Vik

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 year 9 months ago #230493 by tpartner
1) This script will randomize the columns in an array-numbers-checkboxes question while leaving the last column in place:

Code:
<script type="text/javascript" data-author="Tony Partner">  
  $(document).on('ready pjax:scriptcomplete',function(){
    randomArrayNumCols2({QID});
  });
 
  // A function to randomize columns in an array-numbers question leaving the last column in place
  function randomArrayNumCols2(qID){
 
    var thisQuestion = $('#question'+qID);
 
    // Index the array columns and identify the last column
    $('table.subquestion-list tr', thisQuestion).each(function(i) {
      $('> *', this).each(function(i) {
        $(this).attr('data-index', i);
      });
 
      $('> *:last', this).addClass('last-item');
    });
 
    // Create an array of the columns
    var columns = [];
    $('table.subquestion-list tr[id^="javatbd"]:eq(0) td.answer-item', thisQuestion).each(function(i) {
      columns.push($(this).attr('data-index'));
    });
 
    // Shuffle the columns array
    columns = $.shuffle(columns);
 
    // Reposition the row elements
    $('table.subquestion-list tr', thisQuestion).each(function(i) {
      var thisRow = $(this);
      $(columns).each(function(i, val) {
        thisRow.append($('[data-index="'+val+'"]', thisRow));
        thisRow.append($('.col-no-answer', thisRow));
      });
      thisRow.append($('.last-item', thisRow));
    });
  }
 
  // A function to shuffle elements or contents of arrays
  (function($){
    $.fn.shuffle = function() {
      return this.each(function(){
        var items = $(this).children();
        return (items.length)
          ? $(this).html($.shuffle(items))
          : this;
      });
    }
 
    $.shuffle = function(arr) {
      for(
        var j, x, i = arr.length; i;
        j = parseInt(Math.random() * i),
        x = arr[--i], arr[i] = arr[j], arr[j] = x
      );
      return arr;
 
    }
  })(jQuery);
</script>


2) This script will render the last column in an array-numbers-checkboxes question as exclusive and will also disable the non-exclusive items when exclusive is checked:

Code:
<script type="text/javascript" data-author="Tony Partner">
  $(document).ready(function() {
    // Call the exclude function using question ID
    excludeOpt({QID});
  });
 
  // A function to make the last option in each array row exclusive
  function excludeOpt (qID) {
 
    var thisQuestion = $('#question'+qID)
 
    // Add some classes to the checkbox cells
    $('td.checkbox-item', thisQuestion).addClass('normal-item');
    $('tr.subquestion-list', thisQuestion).each(function(i) {
      $('.normal-item:last', this).removeClass('normal-item').addClass('exclusive-item')
    });
 
    // A listener on the checkboxes
    $('input[type="checkbox"]', thisQuestion).on('change', function (event) {
      handleExclusive($(this));
    });
 
    function handleExclusive(thisInput) {
 
      var thisCell = $(thisInput).closest('td'); 
      var thisRow = $(thisCell).closest('tr');
 
      $('input[type="checkbox"]', thisRow).prop('disabled', false);
 
      if(thisInput.is(':checked')) {
        // Uncheck the appropriate boxes in a row
        if ($(thisCell).hasClass('normal-item')) {
          $('.exclusive-item input[type="hidden"]', thisRow).val('');
          $('.exclusive-item input[type="checkbox"]', thisRow).prop('checked', false);
        }
        else {
          $('.normal-item input[type="hidden"]', thisRow).val('');
          $('.normal-item input[type="checkbox"]', thisRow).prop('checked', false).prop('disabled', true);
        }
 
        // Check conditions (relevance)
        $('td.checkbox-item', thisRow).each(function(i) {
          var thisValue = '';
          if($('input[type="checkbox"]', this).is(':checked')) {
            thisValue = 1;
          }
          var thisSGQA = $('input[type="checkbox"]', this).attr('id').replace(/cbox_/, '');
 
          $('input[type="hidden"]', this).attr('value', thisValue);
          fixnum_checkconditions(thisValue, thisSGQA, 'hidden');
        });
      }
    }
  }
</script>

Sample survey attached:  

File Attachment:

File Name: limesurvey...6546.lss
File Size:33 KB

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.

  • Sidvic
  • Sidvic's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
1 year 9 months ago #230497 by Sidvic
Hallo tpartner,

thank you very much for your prompt and extremely useful reply. It works so smoothly and exactly as I imagined it.

Thanks again and have a wonderful day,
Vik

Please Log in to join the conversation.

Lime-years ahead

Online-surveys for every purse and purpose