Welcome to the LimeSurvey Community Forum

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

Changing text of a Multiple Choice Question Type by an external resource file

  • achecchini
  • achecchini's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
3 years 6 months ago #219457 by achecchini
Have a nice day community!

I have question using LS 3.5. Suppose you have a Multiple choice Question Type like this:

A vs B    []
C vs D    []
E vs F    []
G vs H    []

the text of the question is valid for this week. But next week I want the Question Type texts became:

A vs H    []
G vs D    []
E vs B    []
C vs F    []

because my survey don't change every wheek for only this question my goal is to not edit every week the Multiple choice question type but "read" on an external input list file the new list of questions.

Is this possible? And if yes had someone suggestions for solve the problem?

Thanks in advance!

Ago

PS The external input file maybe have the shape like:
w1;A vs B;C vs D;E vs F;C vs F;
w2;G vs H;A vs H;G vs D;E vs B;
or similar ...

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 6 months ago #219461 by Joffm
Hi,
without an external file, without javascript (because I am not familiar with it), only equations.
1. Create a question of type "multiple short text" (here QDef) with as many subquestions as there will be weeks.
2. In the "default answers" of this questions enter the options per week, with a fixed width; meaning each option has the same length and starts at a multiple of x.
In the example each option has a length of 10 characters and they start at position 1, 11, 21,...
Code:
A vs B    C vs D    E vs F    C vs F
G vs H    A vs H    G vs D    E vs B
X vs Y    S vs T    U vs V    Q vs R

3. Create a question of type "equation" to calculate the current week by
{1+floor(((strtotime("now")-mktime(0,0,0,9,1,2021))/86400)/7)}

strtotime("now") : the actual date converted to a timestamp
mktime(0,0,0,9,1,2021): the start date also as timestamp, here 1st of september 2021

Both question are hidden.

3. In your multiple question you display the options by micro-tayloring, that is, depending on the week you capture the options with the function "substr" and a "trim" to remove the blanks.
1:{trim(if(week==1,substr(QDef_1,0,10),if(week==2,substr(QDef_2,0,10),if(week==3,substr(QDef_3,0,10),""))))}
2:{trim(if(week==1,substr(QDef_1,10,10),if(week==2,substr(QDef_2,10,10),if(week==3,substr(QDef_3,10,10),""))))}
3:{trim(if(week==1,substr(QDef_1,20,10),if(week==2,substr(QDef_2,20,10),if(week==3,substr(QDef_3,20,10),""))))}
4:{trim(if(week==1,substr(QDef_1,30,10),if(week==2,substr(QDef_2,30,10),if(week==3,substr(QDef_3,30,10),""))))}
Of course you have to extend this if there are more weeks. But you see the structure.

 

File Attachment:

File Name: limesurvey...5181.lss
File Size:22 KB


Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • achecchini
  • achecchini's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
3 years 5 months ago #219527 by achecchini
Hi !
I can't imagine a more creative and imaginative way to use equations in LS !!
I would never, ever be able to think at that solution!
But my example was a little simplified: the weeks are many more and the variable text much longer.
It is a very good example of a solution but I think that - for my specific purpose - it is actually necessary to verify the use of javascript or similar.

A very grateful thanks!!

Ago

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 5 months ago #219534 by Joffm
Hi,
first

But my example was a little simplified

If you do not tell us the exact requirements you cannot expect a solution that works for you.

But uin your case: I do not see a problem.
Each default value is of type "text", so it is able to contain 64kB of unicode characters
And the equation itself is only extended for some more weeks, this column is implemented in thed atabase as "mediumtext", meaning 16MB of unicode characters.
Should be sufficient.

Here a screenshot of the 7th week, each item 250 characters long
 

Concerning a javascript solution.
Did you have a look at the "jquery.csv.js". It is here often used to read a csv file into an autocomplete question
[url] searchcode.com/codesearch/view/89459724/ [/url]

Or jquery-csv.js
[url] stackoverflow.com/questions/55244410/get...ray-using-jquery-csv [/url]
Could be a way to do it.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 5 months ago #219540 by Joffm
Hi,
as I said yesterday, the "jquery.csv.js" will do the trick.
Supposing that the information about 4 subquestions was correct, I created a question of type "multiple short text" (QXH) (H=hidden) with four subquestions.
Here I included the following script
Code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-csv/1.0.11/jquery.csv.min.js"></script>
<script type="text/javascript" charset="utf-8">
  $(document).on('ready pjax:complete',function() {
    // Calculate the number of weeks since start date
    // Note: JavaScript counts months from 0 to 11
[b]    // [/b]Here start day is 1st of september 2021
    const startdate = new Date(2021, 8, 1);
    const aktdate = new Date();
    var dd = Math.floor((aktdate - startdate)/(60*60*24*7*1000)); 
 
    var url = "/upload/surveys/123456/files/items.csv";
 
    var Items = new Array();
 
    $.get(url,function(data){
      fullArray = $.csv.toArrays(data);
      $(fullArray).each(function(i, item){
        Items.push(item[0]);
      });
      $("#question{QID} input[type=text]:eq(0)").val(Items[0+4*dd]);
      $("#question{QID} input[type=text]:eq(1)").val(Items[1+4*dd]);
      $("#question{QID} input[type=text]:eq(2)").val(Items[2+4*dd]);
      $("#question{QID} input[type=text]:eq(3)").val(Items[3+4*dd]);
 
    });
      $('#question{QID}').hide();
 
  });
</script>

In the multiple  question I refer to these values by the usual {QXH_SQ001}
 

And the file is a plain text file like (each item in one line).
 

Important: The (hidden) question to capture the items must not be in the same group as the question itself.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 5 months ago #219541 by Joffm
I could not edit because of this sometimes strange behaviour of the editor.

But; Ignore the [  b ]  and [ /b ] here 
Code:
[b]    // [/b]Here start day is 1st of september 2021

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • achecchini
  • achecchini's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
3 years 5 months ago #219556 by achecchini
 Hi Joffm!

I am truly grateful to you for the extraordinary help you are giving me.

Reflecting on your answers, however, I am convinced that the path you indicated in the first email is still probably the most advantageous.

So I adapted your solution to my specific case.
I am attaching the result. 

File Attachment:

File Name: limesurvey... (1).lss
File Size:44 KB


Just one more question (also to me sometimes the requirements change) if it was necessary to use the radiobuttos rather than the check boxes ??

Have a nice day!

Ago

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 5 months ago #219560 by Joffm

Just one more question (also to me sometimes the requirements change) if it was necessary to use the radiobuttos rather than the check boxes ??
 

Of course, why not.
There is no difference between inserting the text in "answer option" and in "subquestions".
 

The typo "Bari" is yours.


One last thing.
In questionnaires we - market researchers - are used to this:
a circle means: single punch
a square means: multi punch

Therefore I showed it in a multiple question.

Joffm

And you are right. For this small amount of text this solution is sufficient.

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 5 months ago - 3 years 5 months ago #219583 by Joffm
Hi, I saw that it is not Bari, but should be Cagliari.
Some texts are shorter than 24 characters, like #Bologna-Verona     #
 

Furthermore you have to revise this:
1. the typo "145" instead "144"
2. the repeated "QDef_3, 145" for all weeks > 3
,if(week==3,substr(QDef_3,144,24),if(week==4,substr(QDef_3,145,24),if(week==5,substr(QDef_3,145,24),if(w...

Best regards
Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 3 years 5 months ago by Joffm.

Please Log in to join the conversation.

  • achecchini
  • achecchini's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
3 years 5 months ago - 3 years 5 months ago #219588 by achecchini
Hi Joffm!!
you're practically adopting me :-) !!
thank you, I saw the misalignments yesterday and made the version with the radio buttons but it could be that both types of question are useful!

I am truly grateful to you for the wonderful help you have provided me !!

One last clarification if possible. The change of week is calculated based on the time settings of the server right? If you need to calculate the week start from Tuesday like you just need to change the day in the formula of the equation right?

Thanks.

Ago
Last edit: 3 years 5 months ago by achecchini. Reason: forgot to

Please Log in to join the conversation.

  • achecchini
  • achecchini's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
2 years 7 months ago #230799 by achecchini
Hi Joffm! Hope you are fine.

I have question about the solution you suggested me some mounth ago.

When the user make the choice checking the radio button (he can choice only one game) the survey write in the DB a string like this:

{trim(if(week==1,substr(QDef_1,140,46),if(week==2,substr(QDef_2,140,46),if(week==3,substr(QDef_3,140,46),if(week==4,substr(QDef_4,140,46),if(week==5,substr(QDef_5,140,46),if(week==6,substr(QDef_6,140,46),if(week==7,substr(QDef_7,140,46),if(week==8,substr(QDef_8,140,46),if(week==9,substr(QDef_9,140,46),if(week==10,substr(QDef_10,140,46),if(week==11,substr(QDef_11,140,46),if(week==12,substr(QDef_12,140,46),if(week==13,substr(QDef_13,140,46),if(week==14,substr(QDef_14,140,46),if(week==15,substr(QDef_15,140,46),if(week==16,substr(QDef_16,140,46),if(week==17,substr(QDef_17,140,46),if(week==18,substr(QDef_18,140,46),if(week==19,substr(QDef_19,140,46),""))))))))))))))))))))}

that corrispond at the the fourth games of this text string:
Milan-Udinese            sabato    13/08 18:30Sampdoria-Atalanta       sabato    13/08 18:30Lecce-Inter              sabato    13/08 20:45Monza-Torino             sabato    13/08 20:45Fiorentina-Cremonese     domenica  14/08 18:30Lazio-Bologna            domenica  14/08 18:30Spezia-Empoli            domenica  14/08 20:45Salernitana-Roma         domenica  14/08 20:45Verona-Napoli            lunedi    15/08 18:30Juventus-Sassuolo        lunedi    15/08 20:45

There is a way to record the game text string? In the sample Monza-Torino             sabato    13/08 20:45 instead of {trim(if(week==1,substr(QDef_1,140,46),if(week==2,substr(QDef_2,140,46) .... etc.

Thanks in advance for any suggestion !

Ago
 

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 7 months ago - 2 years 7 months ago #230800 by Joffm
Hi,
unfortunately you did not attach a lss export of your survey.
Do you really think I remember what I did nearly a year ago??

But if you only want to store the matches somewhere in the answer table I should say.
Create a (hidden) question of type "multiple short text" with the same number of subquestions and enter all the equations as "default answer".

In case of questions send the lss export.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 2 years 7 months ago by Joffm.

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose