Import responses via LimeSurvey RPC API

Plus d'informations
il y a 2 semaines 4 jours #182764 par ArtemPavlenko
COM_KUNENA_MESSAGE_CREATED_NEW
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?

Connexion ou Créer un compte pour participer à la conversation.

LimeSurvey Partners
Plus d'informations
il y a 2 semaines 4 jours #182766 par evently
COM_KUNENA_MESSAGE_REPLIED_NEW
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!

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 2 semaines 4 jours - il y a 2 semaines 4 jours #182768 par ArtemPavlenko
COM_KUNENA_MESSAGE_REPLIED_NEW
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: il y a 2 semaines 4 jours by ArtemPavlenko.

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 2 semaines 4 jours #182771 par tpartner
COM_KUNENA_MESSAGE_REPLIED_NEW
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.

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 2 semaines 4 jours #182772 par ArtemPavlenko
COM_KUNENA_MESSAGE_REPLIED_NEW
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!

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 2 semaines 4 jours #182793 par DenisChenu
COM_KUNENA_MESSAGE_REPLIED_NEW

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 2 semaines 4 jours - il y a 2 semaines 4 jours #182796 par tpartner
COM_KUNENA_MESSAGE_REPLIED_NEW
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):

<?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.
Last edit: il y a 2 semaines 4 jours by tpartner.

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 2 semaines 4 jours #182798 par tpartner
COM_KUNENA_MESSAGE_REPLIED_NEW
@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.

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 2 semaines 3 jours #182807 par DenisChenu
COM_KUNENA_MESSAGE_REPLIED_NEW

tpartner écrit: @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 (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 2 semaines 3 jours - il y a 2 semaines 3 jours #182821 par ArtemPavlenko
COM_KUNENA_MESSAGE_REPLIED_NEW
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: il y a 2 semaines 3 jours by ArtemPavlenko.

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 2 semaines 3 jours #182824 par tpartner
COM_KUNENA_MESSAGE_REPLIED_NEW
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.

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 2 semaines 3 jours #182842 par DenisChenu
COM_KUNENA_MESSAGE_REPLIED_NEW

ArtemPavlenko écrit: 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 (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .

Connexion ou Créer un compte pour participer à la conversation.

Commencez dès maintenant !

Créez simplement un compte et commencez à utiliser LimeSurvey dès aujourd'hui.

Inscrivez-vous maintenant

Inscrivez-vous à notre Newsletter!