how to get the multiple choice responses with the API function question_list

More
1 year 4 months ago #137818 by joost1982
joost1982 created the topic: how to get the multiple choice responses with the API function question_list
Hi there,

I'm using the API to get a list of my questions and answers from a particular survey (question_list command).
It works for every questiontype except for multiple choice. I only retreive the question and not the (seperate) answers for this type of question. How can I get those answers?

Please Log in to join the conversation.

More
1 year 3 weeks ago #142323 by crafter
crafter replied the topic: how to get the multiple choice responses with the API function question_list
This solution is specific to my solution, but it woks for me. I changed the code a bit to show my workings).

The code below show how I get all the data and generate a count of all responses for the question, Your required output may be different.
    const QUESTION_ID = 123;
    const QUESTION_NAME = DOHLINKS;
 
    private function getMultipleChoiceResponses()
    {
 
       // First, get all responses
      $this->allResponses = $this->getAllResponses();
 
        $answerOptions = Yii::app()
            ->limesurveyHelper
            ->getMultipleChoiceQuestionMeta(self::QUESTION_ID);
 
        $answerCount = array();
 
        foreach ($answerOptions as $answerCode => $answerDescription) {
            $answerCount[$answerCode]['count'] = 0;
            $answerCount[$answerCode]['description'] = $answerDescription;
        }
        $answerCount['OTHER']['count'] = 0;
        $answerCount['OTHER']['description'] = 'Other';
 
        foreach ($this->allResponses as $userResponse) {
 
            foreach ($answerOptions as $answerCode => $answerDescription) {
                $responseIndex = self::QUESTION_NAME  . '[' . $answerCode . ']';
 
                if ( !empty($userResponse[$responseIndex]) && ($userResponse[$responseIndex] == "Y") ) {
                    $answerCount[$answerCode]['count']++;
                }
            }
 
        }

In my helper class, I receive the question meta in the following code
public function getMultipleChoiceQuestionMeta($questionID) {
 
        $surveyResponses = array();
 
        Yii::import('application.libraries.jsonRPCClient', true);
 
        /*
         * Connect to the RemoteControl service
         */
        $limesurveyClient = new jsonRPCClient($this->lsBaseUrl . '/admin/remotecontrol', false);
 
        $sSessionKey = $limesurveyClient->get_session_key($this->lsUser, $this->lsPassword);
 
        if (is_array($sSessionKey)) {
            echo $sSessionKey['status'];
            die();
        } else {
            // echo 'Retrieved session key'.'<br>';
            ;
        }
 
        $sessionkey = $sSessionKey;
        // performs some basic operation
 
        $rtdata["params"]["sessionkey"] = $sessionkey;
        $rtdata["params"]["questionID"] = $questionID;
 
        try {
 
            $surveyLanguage     = 'en';
            $questionSettings   = array();
 
            array_push($questionSettings,'available_answers')    ;
            array_push($questionSettings,'subquestions')    ;
            array_push($questionSettings,'attributes')    ;
            array_push($questionSettings,'attributes_lang')    ;
            array_push($questionSettings,'answeroptions')    ;
            array_push($questionSettings,'defaultvalue');
 
            /**            *
             * @access public
             * @param string $sSessionKey Auth credentials
             * @param int $iQuestionID Id of the question to get properties
             * @param array $aQuestionSettings The properties to get
             * @param string $sLanguage Optional parameter language for multilingual questions
             * @return array The requested values
             */
            $answerProperties = $limesurveyClient->get_question_properties(
                            $rtdata["params"]["sessionkey"],
                            $rtdata["params"]["questionID"],
                            $questionSettings,
                            $surveyLanguage);
 
            $limesurveyClient->release_session_key($sessionkey);
 
            if ($answerProperties) {
 
                if (!empty($answerProperties['available_answers'])) {
                    return $answerProperties['available_answers'];
                } else {
                    return [
                        'status' => 'No active data',
                        'message' => 'No active data'
                    ];
                }
 
            } else {
 
                return [
                    'status' => 'No active data',
                    'message' => 'No active data'
                ];
 
            }
 
        } catch (Exception $e) {
            return [
                'status' => 'Error Encountered',
                'message' => $e->getMessage()
            ];
        }
 
        return [
            'status' => 'Error Encountered',
            'message' => 'Unexpected Error'
        ];
 
 
    }
The following user(s) said Thank You: DenisChenu

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now