Programmatically answer and style questions based on previous answer

More
3 months 1 week ago - 3 months 1 week ago #231270 by Haris.Pap
LimeSurvey version: Version 5.3.24+220711
Own server or LimeSurvey hosting: LS Community on own server
Survey theme/template: Vanilla extension
===================
Hi everyone
I am trying to auto-answer questions based on previous answers, using a Group by Group survey format. The target questions are scattered in different Question-groups within the survey. So far so good – this can be well achieved using an equation question

However, I also need to style the target questions so that it’s clear that they have been auto-answered (eg. having a green background and a hint-text).
For this purpose, I tried using the update_response API-function, followed by set_question_property for the styling (not there yet).
My problems with this approach are:
  • Auto-answered questions appear empty, although they are dynamically set successfully (as seen in the responses)
  • Auto-answers are overwritten when the survey is saved or submitted (I guess because the fields remain empty)
> Is there a better way to do this?
> Is there a way to display those auto-answers when navigating to the related Question-groups?
> If yes, is there a better way to get the target answers’ names than using list_questions (API) and compose their SGQ code? 

Thanks for your help
 
Last edit: 3 months 1 week ago by Haris.Pap.

Please Log in to join the conversation.

More
3 months 1 week ago #231275 by tpartner

However, I also need to style the target questions so that it’s clear that they have been auto-answered (eg. having a green background and a hint-text).

If you are using ExpressionScript to set the question, you should also be able to use ExpressionScript to define a CSS class for the question.

- manual.limesurvey.org/Question_type_-_Li...lass_.28css_class.29

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.

More
3 months 1 week ago #231361 by Haris.Pap
Thanks Tony.
Do you mean that it's possible to set a css class of another question via the logic field of an equation question?
  • I checked Expression Manager's " Access to variables ", but there is nothing mentioned
  • I tried this code within an equation question too, but it doesn't work: {Q02.css_class= "autoAnswered"}
Apart from that, I would be grateful for an answer on the remaining questions (see first post):
 > Is there a way to display those auto-answers when navigating to the related Question-groups?
 > If yes, is there a better way to get the target answers’ names than using list_questions (API) and compose their SGQ code?

Please Log in to join the conversation.

More
3 months 1 week ago #231362 by Joffm

Do you mean that it's possible to set a css class of another question via the logic field of an equation question?

I think, Tony wanted to say:
"In the field "css classes" of this question enter something like {if(yourCondition,"autoAnsweredClass","")}

And I do not understad your further questions.

Regarding the second:
What do you "compose"? In "Access to variables" there is a property ".sgqa"

Joffm


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

Please Log in to join the conversation.

More
3 months 1 week ago #231363 by Haris.Pap
Hi Joffm

I think, Tony wanted to say:
"In the field "css classes" of this question enter something like {if(yourCondition,"autoAnsweredClass","")}
 
Thanks for clarifying this, it worked well. I am attaching an example survey, in case anyone needs to have a look.
It's good to mention though, that this won't work dynamically on the same page - It works well for target questions found on upcoming pages.

Regarding the second:
What do you "compose"? In "Access to variables" there is a property ".sgqa"

Let's take it step-by-step:
> (Using the update_response API-function) Auto-answered questions appear empty, although they are dynamically set successfully (as seen in the responses)
   Trying to re-phrase the question below:
   Why would an answer field appear empty, if an answer is set to it programmatically in a previous question group (using the update_response API-function)?

Thx again
Attachments:

Please Log in to join the conversation.

More
3 months 1 week ago #231365 by tpartner

Why would an answer field appear empty, if an answer is set to it programmatically in a previous question group (using the update_response API-function)?

Maybe it's related to the session.

But, why use the API to set the questions? Can't you do it with ExpressionScript in the previous page?

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.

More
3 months 1 week ago #231368 by Joffm
And to style?
You may use your two css classes
Like:  {if(Q00 == 'test',"show-alert","no-alert")}

Seeing your "alert-warning" I suppose you want to show a bootstrap alert.
First you have to insert the alert. Here with a "close" option
<div class="conditional-text alert alert-warning alert-dismissible" role="alert">
  <button aria-label="Close" class="close" data-dismiss="alert" type="button">
    <span aria-hidden="true">×</span>
  </button>
  This question was auto-answered
</div>

And some rough css:
<style type="text/css">
/* 1. To show the alert and change background and color of the answer */
  .show-alert .form-control {
    background-color:yellow;
    color: red;
  }
 
/* 2. To hide the alert  */
.no-alert .alert {
  display:none;
}
</style>
  
Either this
 
  
or this
 

Joffm


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

Please Log in to join the conversation.

More
3 months 1 week ago #231405 by tammo
I used setting a css class conditionally in the following example:
www.respondage.nl/scoring_an_array/

You can download the survey and see how it is done.

Tammo


Tammo ter Hark at Respondage
For Limesurvey reporting, education and customized themes
respondage.nl

Please Log in to join the conversation.

More
3 months 16 hours ago #231579 by Haris.Pap
Thanks to everyone for these helpful answers.
Indeed, an Equation question is great for auto-answering questions. There are some peculiarities and limitations though, which do not seem documented yet ... ð  Suggestion: Expand the related Manual page with the attached guide and / or link both ways to the related EM section . Any improvements are very welcome. 

Why would an answer field appear empty, if an answer is set to it programmatically in a previous question group (using the update_response API-function)?

Maybe it's related to the session.

But, why use the API to set the questions? Can't you do it with ExpressionScript in the previous page?
 
I am really not informed here, how this can be related to the session (do you mean a caching issue?).
Shouldn't each page show the related stored answers when loaded?

There are still two reasons, why I would still use the API to set the answers:
  • An Equation question will not (yet?) work with OfflineSurveys (related forum discussion | workaround1 on page2   | workaround2 )
  • An Equation question cannot set answers dynamically on the same page of the triggering question, which can easily be handled by javascript (see for example workaround1 above).
Any further help would be appreciated!
Attachments:

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now