- Posts: 4
- Thank you received: 0
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Import responses via LimeSurvey RPC API
- ArtemPavlenko
- Topic Author
- Offline
- New Member
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?
- evently
- Offline
- Official LimeSurvey Partner
- Posts: 10
- Thank you received: 7
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!
- ArtemPavlenko
- Topic Author
- Offline
- New Member
- Posts: 4
- Thank you received: 0
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:
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.Error: Invalid Column names supplied: FHNUS1, FHNUS2, FHNUS3, FHNUS4, FHNUS5, FHNUS6, FHNUS7, FHNUS8, FHNUS9, FHNUS10, FHNUS11, ... , FHNUS90
Hope it's clear
- tpartner
- Away
- LimeSurvey Community Team
- Posts: 10103
- Thank you received: 3591
- 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.
- ArtemPavlenko
- Topic Author
- Offline
- New Member
- Posts: 4
- Thank you received: 0
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!
- DenisChenu
- Away
- LimeSurvey Community Team
- Posts: 13624
- Thank you received: 2490
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.
- tpartner
- Away
- LimeSurvey Community Team
- Posts: 10103
- Thank you received: 3591
<?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) && (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.
- tpartner
- Away
- LimeSurvey Community Team
- Posts: 10103
- Thank you received: 3591
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
- DenisChenu
- Away
- LimeSurvey Community Team
- Posts: 13624
- Thank you received: 2490
It must not, last tested it work.tpartner wrote: @DenisChenu, are there any dependencies for rcAddReponseCode?
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.
- ArtemPavlenko
- Topic Author
- Offline
- New Member
- Posts: 4
- Thank you received: 0
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?
- tpartner
- Away
- LimeSurvey Community Team
- Posts: 10103
- Thank you received: 3591
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.
- DenisChenu
- Away
- LimeSurvey Community Team
- Posts: 13624
- Thank you received: 2490
Seems that don't exist. tparner solution to set it to null is near the needed solution.ArtemPavlenko wrote: 2) Remove answers for specific survey and token from DB (???)
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.