Welcome to the LimeSurvey Community Forum

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

Ranking question filtering

  • bcjewel
  • bcjewel's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 4 months ago #144169 by bcjewel
Ranking question filtering was created by bcjewel
I would like to set up a ranking question where the options are dependent on a previous calculated response (which is string). I don't know how to approach this...

If the calculated response ="Option 1", then my Ranked items would be, for example, 101,102, 103, 104, but if the calculated response ="Option 2", then my Ranked items would be 101,102, 103, 104, 105, 106, etc.

Can I use the Logic section in the Advanced settings for this, or is there another way to do this?

Thanks for any advice you can provide.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 4 months ago - 7 years 4 months ago #144177 by tpartner
Replied by tpartner on topic Ranking question filtering
I think the only way to achieve that would be to insert a hidden multiple-choice question and filter the ranking question by that. The script required to toggle the hidden multiple-choice would vary depending on what you mean by "calculated response".

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 7 years 4 months ago by tpartner.
The topic has been locked.
  • bcjewel
  • bcjewel's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 4 months ago #144185 by bcjewel
Replied by bcjewel on topic Ranking question filtering
Sorry, I was not clear about what I meant by a calculated response.

Earlier in the survey I have a date question, which is "Birthdate". I then have an equation question (the "calculated response") called "Branch" which is calculated as follows:
Code:
if(Birthdate<"2002-01-01",'Ranger',if(Birthdate<"2005-01-01",'Pathfinder','Guide'))
For subsequent ranking questions, the items to be ranked depends on the value in "Branch", of which there are only three choices.

How then would I set up a hidden multiple choice question and subsequent ranking questions?

Thanks for your help.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 4 months ago - 7 years 4 months ago #144217 by tpartner
Replied by tpartner on topic Ranking question filtering
Assuming the "Birthdate" and "Branch" questions are on a previous page...

1) Insert a multiple-choice question directly before the ranking question (let's call it "qHidden"). The sub-question codes need to be exactly the same as the answer codes in the ranking question.

2) Set Array filter in the ranking question to the question code of the multiple-choice question (qHidden).

3) 1) Set up your survey to use JavaScript and add the following script to the source of the multiple-choice question (qHidden). The script will hide qHidden and toggle the check-boxes depending on the value of "Branch".

4) Adjust the codes in the "answerCodes" variable as necessary.

Code:
<script type="text/javascript" charset="utf-8">  
  $(document).ready(function() {
 
    // Identify the questions
    var qHidden = $('#question{QID}');
    var qHiddenID = $(qHidden).attr('id').replace(/question/, '');
 
    // Hide the checkbox question
    qHidden.hide();
 
    // The ranking answer codes relevant for the various values of "Branch"
    // EDIT HERE AS REQUIRED
    var answerCodes = {
      1: ['101', '102', '103', '104'], // Seen by "Rangers"
      2: ['101', '102', '103', '104', '105', '106'], // Seen by "Pathfinders"
      3: ['101', '102', '103', '104', '105', '106', '107', '108'] // Seen by everybody else
    }
 
    // Get the value of the "Branch" question
    var branch = '{Branch}';
    console.log(branch);
 
    // Define which set of ranking answer codes to display
    if(branch == 'Ranger') {
      var codeArray = answerCodes[1];
    }
    else if(branch == 'Pathfinder') {
      var codeArray = answerCodes[2];
    }
    else {
      var codeArray = answerCodes[3];
    }
 
    // Toggle the appropriate checkboxes and fire Expression Manager
    $('input.checkbox[id$="X'+qHiddenID+this+'"]').prop('checked', false);
    $('.question-item input:hidden', qHidden).val('');
    $(codeArray).each(function(i) {
      var thisInput = $('input.checkbox[id$="X'+qHiddenID+this+'"]');
      thisInput.prop('checked', true);
      thisInput.nextAll('input:hidden').val('Y');
      checkconditions(thisInput.value, thisInput.name, thisInput.type)
    });
    });
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...-3-4.lss
File Size:24 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 7 years 4 months ago by tpartner.
The topic has been locked.
  • bcjewel
  • bcjewel's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 4 months ago #144226 by bcjewel
Replied by bcjewel on topic Ranking question filtering
Hi Tony,

Thank you so much for taking the time to answer my question and providing the code and sample survey. I am on the road today and don't have the opportunity to try this yet, but will respond further when I try this in my survey.

I very much appreciate your assistance.
The topic has been locked.
  • bcjewel
  • bcjewel's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 4 months ago - 7 years 4 months ago #144353 by bcjewel
Replied by bcjewel on topic Ranking question filtering
I finally got a chance to implement this code, and it works fine for creating the rankings lists (I have multiple questions that depend on the "Branch" value), however there is a problem when the survey is activated... it is not saving the responses for these ranking questions within the database. The questions I have that don't depend on Branch are recording fine, but not these dependent ranked questions - they end up blank (which is not good!). Any idea why?

I just activated and executed the sample you sent, and it has the same problem.
Last edit: 7 years 4 months ago by bcjewel. Reason: extra
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 4 months ago #144359 by tpartner
Replied by tpartner on topic Ranking question filtering
That's the weirdest thing. The data is not recorded if the question is hidden with...
Code:
display:none
...but is if hidden with
Code:
position:absolute
left:-9999em
I've no clue why.

Anyway, use this script in the multiple-choice:
Code:
<script type="text/javascript" charset="utf-8">  
  $(document).ready(function() {
 
    // Identify the questions
    var qHidden = $('#question{QID}');
    var qHiddenID = $(qHidden).attr('id').replace(/question/, '');
 
    // Hide the checkbox question
    qHidden.css({
        'position': 'absolute',
        'left': '-9999em'
    });
 
    // The ranking answer codes relevant for the various values of "Branch"
    // EDIT HERE AS REQUIRED
    var answerCodes = {
      1: ['101', '102', '103', '104'], // Seen by "Rangers"
      2: ['101', '102', '103', '104', '105', '106'], // Seen by "Pathfinders"
      3: ['101', '102', '103', '104', '105', '106', '107', '108'] // Seen by everybody else
    }
 
    // Get the value of the "Branch" question
    var branch = '{Branch}';
    console.log(branch);
 
    // Define which set of ranking answer codes to display
    if(branch == 'Ranger') {
      var codeArray = answerCodes[1];
    }
    else if(branch == 'Pathfinder') {
      var codeArray = answerCodes[2];
    }
    else {
      var codeArray = answerCodes[3];
    }
 
    // Toggle the appropriate checkboxes and fire Expression Manager
    $('input.checkbox[id$="X'+qHiddenID+this+'"]').prop('checked', false);
    $('.question-item input:hidden', qHidden).val('');
    $(codeArray).each(function(i) {
      var thisInput = $('input.checkbox[id$="X'+qHiddenID+this+'"]');
      thisInput.prop('checked', true);
      thisInput.nextAll('input:hidden').val('Y');
      checkconditions(thisInput.value, thisInput.name, thisInput.type)
    });
    });
</script>

New sample survey attached:

File Attachment:

File Name: limesurvey...4858.lss
File Size:24 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • bcjewel
  • bcjewel's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 4 months ago #144361 by bcjewel
Replied by bcjewel on topic Ranking question filtering
Great! That fixed it. Very odd indeed! Thanks for your help.

Now to figure out why the automatic message for the ranking questions say "Double-click or drag-and-drop items in the left list to move them to the right - your highest ranking item should be on the top right, moving through to your lowest ranking item." when my list of items is on the right (not the left) and I have to move them to the left (not the right)....
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 4 months ago #144378 by tpartner
Replied by tpartner on topic Ranking question filtering
Why are the lists reversed left-to-right?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • bcjewel
  • bcjewel's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 4 months ago #144380 by bcjewel
Replied by bcjewel on topic Ranking question filtering
It appears to be browser dependent. I have only tried two browsers (I'm using a Mac), but on Firefox (my default browser) it is reversed, whereas on Safari it is not.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 4 months ago #144381 by tpartner
Replied by tpartner on topic Ranking question filtering
Hmm...that's not right. Have you tested with the default template in the latest release? If so, please file a bug report.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • bcjewel
  • bcjewel's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 4 months ago #144382 by bcjewel
Replied by bcjewel on topic Ranking question filtering
Have been using version 2.0.6+. Just upgraded to 2.55.2+ to see if that would fix it. Tested with the upgrade and the filtering for ranking questions stopped working. It also still showed the questions on the right side within Firefox (didn't try Safari this time). Also don't like the new backend... so restored my previous version. I'm ok with it being on the wrong side as long as it works for me otherwise!
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose