Welcome to the LimeSurvey Community Forum

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

Import responses via LimeSurvey RPC API

  • ArtemPavlenko
  • ArtemPavlenko's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
5 years 1 week ago #182764 by ArtemPavlenko
Import responses via LimeSurvey RPC API was created by ArtemPavlenko
Hello!

Is it possible to import survey responses using LimeSurvey RPC API?
I currently can export responses by token, using api.limesurvey.org/classes/remotecontrol...t_responses_by_token but don't see a method to import them back.

Could you please help me?
The topic has been locked.
  • evently
  • evently's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
5 years 1 week ago #182766 by evently
Replied by evently on topic Import responses via LimeSurvey RPC API
Hi Artem,

not sure what you want to do but you can add responses or update responses

api.limesurvey.org/classes/remotecontrol...#method_add_response

api.limesurvey.org/classes/remotecontrol...thod_update_response

Best regards,
Stefan (stefan(at)evently.nl)
Evently
Looking for beta testers: interested in connecting Zapier to Limesurvey or
creating Telegram and Messenger bots from Limesurvey? Email me!
The topic has been locked.
  • ArtemPavlenko
  • ArtemPavlenko's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
5 years 1 week ago - 5 years 1 week ago #182768 by ArtemPavlenko
Replied by ArtemPavlenko on topic Import responses via LimeSurvey RPC API
Thanks for your response, evently!

Let me describe what issues I faced using those methods you mentioned.

1) I exported answers for specific surveys and token, using: export_responses_by_token($sessionKey, $surveyId, 'json', $token, null, 'all', 'code', 'long') and the API returned me following JSON:

[
{
"933": {
"id": "933",
"submitdate": "2019-03-22 12:46:23",
"lastpage": "1",
"startlanguage": "en",
"token": "9bbc23aa75b7f325ff92cc430acba801ce6",
"startdate": "2019-03-22 12:44:46",
"datestamp": "2019-03-22 12:46:23",
"ipaddr": "127.0.0.1",
"refurl": "",
"FHNUS1": "No",
"FHNUS2": "",
"FHNUS3": "",
"FHNUS4": "N\/A",
"FHNUS5": null,
"FHNUS6": null,
"FHNUS7": null,
"FHNUS8": "N\/A",
"FHNUS9": "",
"FHNUS10": "",
"FHNUS11": "N\/A",
....,
"FHNUS90": ""
}
}
]


2) I tried api.limesurvey.org/classes/remotecontrol...#method_add_response passing JSON from previous step as $aResponseData parameter. Method returns ID of new answer, but in DB in this survey I see that answer wasn't added correctly. In table lime_survey_SURVEYID for newly added answer all fields except id, startdate and datestamp are nulls. Which doesn't look correct.
3) I tried api.limesurvey.org/classes/remotecontrol...thod_update_response but in response got error message:

Error: Invalid Column names supplied: FHNUS1, FHNUS2, FHNUS3, FHNUS4, FHNUS5, FHNUS6, FHNUS7, FHNUS8, FHNUS9, FHNUS10, FHNUS11, ... , FHNUS90

I looked into the code on GitHub, and saw that this method expects real columns names ({SURVEY_ID}X{QUESTION}) instead of what export_responses_by_token API returns to us.

Hope it's clear :)
Last edit: 5 years 1 week ago by ArtemPavlenko.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 1 week ago #182771 by tpartner
Replied by tpartner on topic Import responses via LimeSurvey RPC API
I think you will need to use list_questions() to relate the question codes to SGQA values (column names).

- api.limesurvey.org/classes/remotecontrol...ethod_list_questions

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • ArtemPavlenko
  • ArtemPavlenko's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
5 years 1 week ago #182772 by ArtemPavlenko
Replied by ArtemPavlenko on topic Import responses via LimeSurvey RPC API
Thank you very much, tpartner!


Seems like it's a valid solution for me.

BTW, can you advice me on how to remove answers for specific survey and token?

Thanks in advance!
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 1 week ago #182793 by DenisChenu
Replied by DenisChenu on topic Import responses via LimeSurvey RPC API

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 topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 1 week ago - 5 years 1 week ago #182796 by tpartner
Replied by tpartner on topic Import responses via LimeSurvey RPC API
Denis may have a more efficient way of doing this but here is a short example of how to retrieve a response ID and then update that response (assuming you know the SGQA):

Code:
<?php
 
  require_once 'jsonRPCClient.php';
 
  define( 'LS_BASEURL', 'http://pathTo/limeSurvey');  
  define( 'LS_USER', 'admin' );
  define( 'LS_PASSWORD', '*******' );
 
  $iSurveyID = 885886;
  $sToken = '111';
 
 
  if(ctype_alnum($iSurveyID) &amp;&amp; (strlen($iSurveyID) == 5 || strlen($iSurveyID) == 6)) { // Valid SID format
 
    // Instantiate a new RPC client
    $myJSONRPCClient = new jsonRPCClient( LS_BASEURL.'/index.php/admin/remotecontrol' );
 
    // Get a session key
    $sSessionKey= $myJSONRPCClient->get_session_key( LS_USER, LS_PASSWORD );
 
    if(is_array($sSessionKey)) { // Invalid session
      echo $sSessionKey['status'];
    }
    else if($sSessionKey) { // Valid session
 
      // Get a response by token
      $tokenResponse = $myJSONRPCClient->export_responses_by_token($sSessionKey, $iSurveyID, 'json', $sToken, null, 'all', 'code', 'long');
 
      if(is_array($tokenResponse)) {
        // Oops, print any errors
        print_r($tokenResponse);
      }
      else {
        // There is a valid response...
        // Decode the retuned base-64 string and convert to an array
        $decodedString = base64_decode($tokenResponse);
        $tokenResponseArr = json_decode($decodedString, True);
        // Get the first item in the $tokenResponseArr['responses'][0] array
        $tokenResponseArr2 = reset($tokenResponseArr['responses'][0]);
        $responseID = $tokenResponseArr2['id'];
        echo $responseID;
        echo '<br /><br />';
 
        // Update a response
        // In this case, we update a multiple-choice sub-question 
        // and it's related comment with null values. 
        // The group ID is 330, the question ID is 5152, the sq-code is SQ001
        $aResponseData = array(
          'id' => $responseID,
          $iSurveyID.'X330X5152SQ001' => '',
          $iSurveyID.'X330X5152SQ001comment' => ''
        );
        $updatedResponse = $myJSONRPCClient->update_response($sSessionKey, $iSurveyID, $aResponseData);
 
        echo $updatedResponse;
      }
    }
 
    // Release the session key
    $myJSONRPCClient->release_session_key( $sSessionKey );
  }
  else { // Invalid SID format
    die( 'Invalid format!' );
  }
 
?>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 5 years 1 week ago by tpartner.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 1 week ago #182798 by tpartner
Replied by tpartner on topic Import responses via LimeSurvey RPC API
@DenisChenu, are there any dependencies for rcAddReponseCode?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 1 week ago #182807 by DenisChenu
Replied by DenisChenu on topic Import responses via LimeSurvey RPC API

tpartner wrote: @DenisChenu, are there any dependencies for rcAddReponseCode?

It must not, last tested it work.

See gitlab.com/SondagesPro/RemoteControl/rcA...ntrolHandler.php#L33 for the new function.

But in fact $emCode is not used (always true …)

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 topic has been locked.
  • ArtemPavlenko
  • ArtemPavlenko's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
5 years 1 week ago - 5 years 1 week ago #182821 by ArtemPavlenko
Replied by ArtemPavlenko on topic Import responses via LimeSurvey RPC API
Guys, thank you very much for the suggested solutions!

But I still need to understand how to delete answers for the specific survey and token.

I need to implement following flow:

1) Import answers for specific survey and token ( api.limesurvey.org/classes/remotecontrol...t_responses_by_token ).
2) Remove answers for specific survey and token from DB (???)
3) Export answers for specific survey and token ( api.limesurvey.org/classes/remotecontrol...#method_add_response ).

Can you advice me how to do 2nd step in my flow?
Last edit: 5 years 1 week ago by ArtemPavlenko.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 1 week ago #182824 by tpartner
Replied by tpartner on topic Import responses via LimeSurvey RPC API
I have already given an example of how to modify a response (in this case, set the answers to null).

Do you want to remove a complete response set?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 1 week ago #182842 by DenisChenu
Replied by DenisChenu on topic Import responses via LimeSurvey RPC API

ArtemPavlenko wrote: 2) Remove answers for specific survey and token from DB (???)

Seems that don't exist. tparner solution to set it to null is near the needed solution.

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 topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose