Welcome to the LimeSurvey Community Forum

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

Access variables inserted using RemoteControl 2 API in relevance equation

  • zuercher8353
  • zuercher8353's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 1 week ago #214602 by zuercher8353
Hi there

I`m using the RemoteControl 2 API to insert values into hidden question. These values are based on the response a survey participant gave to a chatbot which is integrated into the survey using an Iframe. In more detail the actions are:

Q1 : User interacts with Chatbot integarted into Q1 (iframe)
Chatbot Backend: Inserts value into Q2 (hidden yes/no question) based on survey participants answer to Q1.
Q3: Uses the value of Q2 in a relevance equation (goal it should only be visible if Q2 was set to yes).
 
The problem is that Q3 doesn't use the value stored in the database to evaluate the relevance equation. I tested that the relevance equation is correct by unhidding Q2 and answering it directly in the survey. Also, I can see in the response tab the inserting of the result of Q2 worked fine.

Is there a way to achieve such a behavior? Such that the relevance equation considers the response saved in the database? For me, it would be the best if I don’t have to change the source code.

Would appreciate it if someone could help me. I'm also willing to donate. 

Best regards, Jonas
The topic has been locked.
More
3 years 1 week ago - 3 years 1 week ago #214609 by jelo

The problem is that Q3 doesn't use the value stored in the database to evaluate the relevance equation. I tested that the relevance equation is correct by unhidding Q2 and answering it directly in the survey. Also, I can see in the response tab the inserting of the result of Q2 worked fine.

Can  you provide a LSS export of these three questions and attach it here? It's a lot easier to understand what you are doing and spot issues.

How is Q2 hidden? Via CSS Class? Or do you use the LimeSurvey option to hide the question? Dependng on how you hide a question, the question is no longer available to save data via Javascript. If you use the RemoteControl 2 API, you normally would't use that approach. But perhaps I didn't get it right.

If the data is correctly available while being hidden, it is available in the response data set. If that is the case, my second idea would be that Q3 cannot access the data, if it is in the same group/page as Q2. Place Q3 in a different group and check again.

 

The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
Last edit: 3 years 1 week ago by jelo.
The following user(s) said Thank You: zuercher8353
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 1 week ago #214620 by tpartner
You need to fire the ExpressionScript checkconditions() function after loading the question.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
More
3 years 1 week ago #214622 by jelo
Tony, do you use the approach the threadstarter described with LS3?

The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 1 week ago #214623 by tpartner
That's hard to answer because I see no indication of how they are inserting the Q2 value. (script details?)

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: DenisChenu, zuercher8353
The topic has been locked.
  • zuercher8353
  • zuercher8353's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 1 week ago - 3 years 1 week ago #214630 by zuercher8353
Hey there thank you for your replies. I try to answer in more detail and I also attached a screenshot of the Infrastructure as well as a demo survey that I created. I'm using LimeSurvey  Version 3.25.20

Question Group1:

Q1: Chatbot. This question is a Text Display Question, in the source of Q1 I integrated a Chatbot using an Iframe. I pass the Limesurvey-userToken to the chatbot, so I know which user is interacting with the chatbot. When the conversation is finished the chatbot decides whether Q2 should be answered with yes or no. To communicate this decision  I use the Limesurvey 2 API in Python. In more detail I use the API method: update_response(string $sSessionKey,integer $iSurveyID,array $aResponseData): string|boolean. In order that to update the correct data I use the question-identifier and the Limesurvey-userToken. To check whether the update_response call worked I go to the Responses-Tab in the Limesurvey Menu, where I can see that Q2 is now answered.

Q2: Decision is a Yes/No question. This question is hidden by entering 0 into the relevance equation of the question. I use it as a "dummy" question that is used to communicated from the chatbot to the LimeSurvey.

Question Group2:

Q3: OptionalQuestion. This question should only be displayed if Q2 is set to yes, this should be achieved by the following relevance equation: ((Decision.NAOK == "Y")).

However, here comes the problem:The relevance equation is evaluated as false and the question is not displayed, even if the result of Q2 is set to "Y" in the database (verified by looking at the Responses Tab in the LimeSurvey).
I think the problem is that the relevance equation doesn't use the value of Q2 that is saved in the database.

Is this correct does the relevance equation use some kind of "cached" values?
If so is there a workaround such that this relevance equation uses the value that is stored in the database?
If not is there another way to achieve such a behavior: Display certain questions based on an external Program?

Thanks a lot for your help

Jonas
Last edit: 3 years 1 week ago by zuercher8353.
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 1 week ago #214632 by Joffm

This question is hidden by entering 0 into the relevance equation

This seems to be the point.
Hide it with css class "hidden" or by "Always hide this question"

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: zuercher8353
The topic has been locked.
  • zuercher8353
  • zuercher8353's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 1 week ago #214633 by zuercher8353
Thanks I tried it out. Unfortunately this doesn't fix the problem.

If I updated an Answer during the Survey execution using the API, the updated value, doesn't affect the relevance equation of the following questions.

This behavior can also be observed when using the Equation Question type to display the value of a question using: {Decision}, the value that gets displayed is the value that was set using the LimeSurvey UI (in this case its empty) and not the value that is in the database, which was inserted using the API. However, if I close the survey and reopen it (using the same token) the value that was saved in the database (by using the API) gets displayed. Therefore, I think the problem is that values are not loaded from the database as described above.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 1 week ago - 3 years 1 week ago #214637 by tpartner
I think that the problem is that ExpressionScript is looking at the session data, not the database.

Since you seem to have access to the chatbot code, you could use a JavaScript message to push an answer directly from the iframe to the parent frame.

- developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

- forums.limesurvey.org/forum/can-i-do-thi...-of-recording#149584

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 3 years 1 week ago by tpartner.
The following user(s) said Thank You: DenisChenu, zuercher8353
The topic has been locked.
  • zuercher8353
  • zuercher8353's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 1 week ago #214639 by zuercher8353
Thanks tpartner. I think that is exactly the problem, I didn`t know that LimeSurvey uses SessionData. So I have to change my plan. Do you think something like this is possible or are there any problems which I don't see:

1) get Data back from the Chatbot.
2 ) use this Data to fill Q2 that is hidden by using the CSS attribute "hidden"
3) use the answer that was filled into Q2 in the relevance equation of Q3
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 1 week ago #214643 by tpartner
Yes, it is possible using a JavaScript message and a script to receive it in the source of Q2.

I cannot help with the code for the chatbot side because I have no idea what you are doing there.

My links should get you started on the JS for the survey side. I think the code in Q2 would be something like this:

Code:
<script type="text/javascript" charset="utf-8">  
 
  // Function to run when message received
  function receiveMessage(event) {
    // Quit if message not from expected origin
    if (event.origin !== 'http://theIframeDomain.com') { 
      return;
    }
    // It's a valid origin...
    else {
      if(event.data ==  'Y') {
        $('#question{QID} input:radio[value="Y"]').trigger('click');
 
      if(event.data ==  'N') {
        $('#question{QID} input:radio[value="N"]').trigger('click');
      }
    }
  }
  // Message listener
  window.addEventListener("message", receiveMessage, false);
</script>

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: zuercher8353
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 1 week ago #214673 by DenisChenu
Filling data from session in a plugin (sample)

gitlab.com/SondagesPro/ExportAndStats/ge...raphQuestion.php#L61

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The following user(s) said Thank You: zuercher8353
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose