Welcome to the LimeSurvey Community Forum

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

How to get all questions in a function, created for ExpressionScript?

  • Templar
  • Templar's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
4 months 1 week ago #253498 by Templar
Please help us help you and fill where relevant:
Your LimeSurvey version: 6.3.9+231211 
Own server or LimeSurvey hosting: Own server 
Survey theme/template: irrelevant
==================
I want to extend ExpressionScript with a new function. For its logic It needs a full list of all questions' IDs from survey. How can I get them? 
I saw a public API with getQuestions, but it needs an API reference - can I get it in such function?
Or is there possibility to pass them as parameter from ExpressionScript?

And second question: Do I need to prepare both Javascript and PHP versions of this function, as in example?

Please Log in to join the conversation.

  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 months 1 week ago - 4 months 1 week ago #253533 by DenisChenu
I don't think you can get all question id, no API for this (i think).

In php :
Code:
$command = Yii::app()->db->createCommand()
->select("{{questions}}.qid,{{groups}}.group_order, {{questions}}.question_order")
->from($questionTable)
->where("({{questions}}.sid = :sid AND {{questions}}.parent_qid = 0)")
->join('{{groups}}', "{{groups}}.gid = {{questions}}.gid")
->order("{{groups}}.group_order asc, {{questions}}.question_order asc")
->bindParam(":sid", $iSurvey, PDO::PARAM_INT);
$allQuestions = $command->query()->readAll();


> Do I need to prepare both Javascript and PHP versions of this function, as in example?

No : see manual  : manual.limesurvey.org/ExpressionManagerS...#Function_definition

Sample github.com/LimeSurvey/LimeSurvey/blob/41...tatFunctions.php#L41
 

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.
Last edit: 4 months 1 week ago by DenisChenu. Reason: delete question_l10ns
The following user(s) said Thank You: Templar

Please Log in to join the conversation.

  • Templar
  • Templar's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
4 months 1 week ago #253620 by Templar
Thanks for your help! In the given sample I found a way to get reference to API:  github.com/LimeSurvey/LimeSurvey/blob/41...untFunctions.php#L31
And all IDs I got like this:
Code:
            $api = new LimesurveyApi();
            $lang = $api->getCurrentUser()['lang'];
            $questions = $api->getQuestions($surveyId, $lang, ['gid' => $groupId]);
 
            $questionIds = [];
            foreach ($questions as $question) {
                $questionIds[] = $question['qid'];
            }
And use of a direct query to DB I'll remember for later, if there will be issues with big dataset.

I'm making another solution to the problem of how to show random X questions out of Y in the group, and soon I'll make a post about it. 
P.S. I'm a newbie to PHP, so if you see any possible improvements to my code, you are welcome to tell me.
The following user(s) said Thank You: DenisChenu

Please Log in to join the conversation.

Lime-years ahead

Online-surveys for every purse and purpose