Welcome to the LimeSurvey Community Forum

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

How do I rank group assessment output?

  • InfoSolutionsProvider
  • InfoSolutionsProvider's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 5 months ago #144589 by InfoSolutionsProvider
How do I rank group assessment output? was created by InfoSolutionsProvider
I have a survey that does 5 group assessments and returns the output on the completed page as:

G1 - {PERC}
G2 - {PERC}
G3 - {PERC}
G4 - {PERC}
G5 - {PERC}
where {PERC} is a calculated number from an array question (1 question per group). All this is working perfectly.

My problem is that I also need to display a simple ranking or sorting of the same output in descending order on the same page as well as in admin and confirmation emails.

For example,
G1 - 370
G2 - 100
G3 - 210
G4 - 75
G5 - 300

should also display as G1G5G3G2G4.

I've spent the last day or so scouring the manuals and forum, but can't work out how to do this. Expression Manager seems to have the answer somewhere, but I can't really put my finger on it, partly because I'm not at all familiar with how it hangs together. Alternatively, might javascript do the trick?

Any help would be much appreciated.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 5 months ago #144612 by tpartner
Replied by tpartner on topic How do I rank group assessment output?
You can use JavaScript to sort an associative array .

So if, for example, you want to display the sorted scores in question text and store them in the data, add something like this to the source of a long-text question. (you will, of course need to replace the score values with Expression Manager variables)

Code:
<script type="text/javascript" charset="utf-8">  
 
  $(document).ready(function() {
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    // Define the group scores
    var scores = {
      G1: 370,
      G2: 100,
      G3: 210,
      G4: 75,
      G5: 300
    };
 
    // Sort the array
    var tuples = [];    
    for (var key in scores) tuples.push([key, scores[key]]);    
    tuples.sort(function(a, b) {
      a = a[1];
      b = b[1];
 
      return a < b ? -1 : (a > b ? 1 : 0);
    });  
 
    // Loop through the sorted array  
    for (var i = 0; i < tuples.length; i++) {
      var key = tuples[i][0];
      var value = tuples[i][1];
 
      // Display in question text
      $('.question-text', thisQuestion).append('<br />'+key+': '+value+'');
 
      // Insert into textarea
      $('textarea', thisQuestion).val($('textarea', thisQuestion).val()+key+': '+value+'\n');
    }
    });
</script>



Sample survey attached:

File Attachment:

File Name: limesurvey...-5-6.lss
File Size:13 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: InfoSolutionsProvider
The topic has been locked.
  • InfoSolutionsProvider
  • InfoSolutionsProvider's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 5 months ago #144627 by InfoSolutionsProvider
Replied by InfoSolutionsProvider on topic How do I rank group assessment output?
Thanks Tony, I'll use the script and report back on the thread.
The topic has been locked.
  • InfoSolutionsProvider
  • InfoSolutionsProvider's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 5 months ago #144637 by InfoSolutionsProvider
Replied by InfoSolutionsProvider on topic How do I rank group assessment output?
I have tried to use the javascript code, but can't see how to get the expression manager variables for the assessments. Don't know where to find the ID of each assessment rule.

My understanding is that each assessment rule output is transient and not written to the database. If so, how can I trap that output and manipulate it with javascript?

Thanks again for your help.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 5 months ago - 7 years 5 months ago #144654 by tpartner
Replied by tpartner on topic How do I rank group assessment output?
I would write your own "group score" values to hidden equation type questions . Then they are in the data and also can be accessed by Expression manager.

See Qcode.value here - manual.limesurvey.org/Expression_Manager#Access_to_Variables .

So, the first equation question could be something like:
Code:
{sum(G1Q1.value, G1Q2.value, G1Q3.value,...)}
The second equation question something like:
Code:
{sum(G2Q1.value, G2Q2.value, G2Q3.value,...)}
...

Then, your JS would look something like this (where equation1, equation2, etc. are the equation question codes):

Code:
<script type="text/javascript" charset="utf-8">  
 
  $(document).ready(function() {
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    // Define the group scores
    var scores = {
      G1: {equation1},
      G2: {equation2},
      G3: {equation3},
      G4: {equation4},
      G5: {equation5}
    };
 
    // Sort the array
    var tuples = [];    
    for (var key in scores) tuples.push([key, scores[key]]);    
    tuples.sort(function(a, b) {
      a = a[1];
      b = b[1];
 
      return a < b ? -1 : (a > b ? 1 : 0);
    });  
 
    // Loop through the sorted array  
    for (var i = 0; i < tuples.length; i++) {
      var key = tuples[i][0];
      var value = tuples[i][1];
 
      // Display in question text
      $('.question-text', thisQuestion).append('<br />'+key+': '+value+'');
 
      // Insert into textarea
      $('textarea', thisQuestion).val($('textarea', thisQuestion).val()+key+': '+value+'\n');
    }
    });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 7 years 5 months ago by tpartner.
The following user(s) said Thank You: InfoSolutionsProvider
The topic has been locked.
  • InfoSolutionsProvider
  • InfoSolutionsProvider's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 5 months ago #144656 by InfoSolutionsProvider
Replied by InfoSolutionsProvider on topic How do I rank group assessment output?
Thanks for this additional guidance. I've been trying it out, but no joy so far. I'm sure my own ignorance of the inner workings of LS is the issue, so I'll keep working on it and feed back again later.
The topic has been locked.
  • InfoSolutionsProvider
  • InfoSolutionsProvider's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 5 months ago #144671 by InfoSolutionsProvider
Replied by InfoSolutionsProvider on topic How do I rank group assessment output?
Things I've tried so far for the equation questions, none of them successful:
1. {sum({INSERTANS:566671X13X226SQ001},{INSERTANS:566671X13X226SQ002},{INSERTANS:566671X13X226SQ003},...)}

2. {sum({566671X13X226SQ001}.value,{566671X13X226SQ002}.value,{566671X13X226SQ003}.value,...)}

3. {sum(Q1_SQ001.value,Q2_SQ002.value,Q3_SQ003.value,...)}

And the following example for one of the equation codes in the javascript: {566671X19X292}

The javascript works fine when the values to be sorted are hard-coded within it, but not when they have to be pulled from elsewhere. Could this be because of this variable: var thisQuestion = $('#question{QID}');

As I know neither javascript, nor LS in any depth, I'm basically at a loss as to what I'm doing wrong. Any further help would greatly appreciated.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 5 months ago #144686 by tpartner
Replied by tpartner on topic How do I rank group assessment output?
Here is a working sample survey.

File Attachment:

File Name: limesurvey...1)-2.lss
File Size:40 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: InfoSolutionsProvider
The topic has been locked.
  • InfoSolutionsProvider
  • InfoSolutionsProvider's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 5 months ago #144709 by InfoSolutionsProvider
Replied by InfoSolutionsProvider on topic How do I rank group assessment output?
Thanks a million for this. My current question type is array and I see that this will have to change to list (radio) for the javascript to work.

I'll tackle the task over the weekend as there are lots of lots of subquestions that will now have to become questions.

Wish there was a way of batch converting subquestions into questions...
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 5 months ago #144716 by tpartner
Replied by tpartner on topic How do I rank group assessment output?

Thanks a million for this. My current question type is array and I see that this will have to change to list (radio) for the javascript to work.

No, the JavaScript will work with arrays. Load the equation questions with the assessments for the array sub-questions.

Code:
{sum(Q1_SQ001.value, Q1_SQ002.value, Q1_SQ003.value,...)}

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • InfoSolutionsProvider
  • InfoSolutionsProvider's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 5 months ago #144726 by InfoSolutionsProvider
Replied by InfoSolutionsProvider on topic How do I rank group assessment output?
Thanks so much for your patience and help! Problem solved now, fully working.

Very good result!
The topic has been locked.
  • InfoSolutionsProvider
  • InfoSolutionsProvider's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 4 months ago #144760 by InfoSolutionsProvider
Replied by InfoSolutionsProvider on topic How do I rank group assessment output?
Trying to make the output fancy :) , how can I strip out the actual scores after the sorting/ranking in decsending order, so that all the shows is like
G4 G1 G5 G3 G2?

I know it's possible within the javascript code, but after spending all weekend struggling with it, I have to humbly ask for help!

Thanks very much.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose