how to export (RPC) a given answer by its ID?

More
4 years 6 months ago - 4 years 6 months ago #93428 by bebs
bebs created the topic: how to export (RPC) a given answer by its ID?
Hello,
I can export the survey answers via RPC, thanks for the feature.

How do I export only 1 answer that interests me? I know it's ID, but of course not its "position" in the answers table as they may not match.

current export_responses function allows to use from & to parameters, but I have no way to convert the answerID to the position of the record in the table.

My current workaround is to export all answers (in csv) and then find the only one that interests me (in php)... not very efficient with many (loooong) answers.

Any idea would be appreciated.
B.


fyi my current "export, convert & check" code is below, any improvement suggestion is welcome, as I'm no great php coder.
/** turn limesurvey csv export answers into an array of format 
 
	[0]{code=>answer}
	[1]{code=>answer} (only if second answer set was included),...
 
 inspired by http://stackoverflow.com/questions/2276626/is-there-a-way-to-access-a-string-as-a-filehandle-in-php 
 and https://gist.github.com/385876 
*/
function str_getcsvarray($input, $delimiter = ",", $enclosure = '"', $escape = "\\")  {
	$header = NULL;
	$data = array();
	$fp = fopen("php://memory", 'r+');
    	fputs($fp, $input);
    	rewind($fp);
 
    	while (($row = fgetcsv($fp, 0, $delimiter)) !== FALSE)	{
			if(!$header)
				$header = $row;
			else
				$data[] = array_combine($header, $row);
		}
	fclose($fp);
	return $data;
}
 
// the survey & answer to process
	$survey_id=$_POST['sid'];
	$answer_id=$_POST['aid'];	
	$response=null; // I want my answer stored here as php array
 
	// get answer from limesurvey
	$myJSONRPCClient = new jsonRPCClient( LS_BASEURL.'admin/remotecontrol' );
	$sessionKey= $myJSONRPCClient->get_session_key( LS_USER, LS_PASSWORD );
 
	//find right answer in answers table.
	$found = false;
	$more = true;
	$index = 1;
	while($more && !$found) {
		$csvresponses = $myJSONRPCClient->export_responses( $sessionKey, $survey_id, 'csv', null, 'all', 'code', 'short', $index, $index+10); // export by 10 to ensure no timeout on server
		// turn csv answers to php array
		$decoded = base64_decode($csvresponses);
		$all =  str_getcsvarray($decoded); 
 
		//find interesting answer in this portion, if it's there
		foreach ($all as $row) {
			if ($row['id']==$answer_id) {
				$found=true;				
				$response = $row;
				break;
			}
		}
 
		//not found
		$more = (count($all)==10);
		$index = $index + 9; // increase by 1 less, to be sure that there is at least 1 answer in next export, it's not efficient but prevents rpc export_responses exception on no answer found. that I cant handle yet.
 
	}
 
  	$myJSONRPCClient->release_session_key( $sessionKey );  
 
	if (!found) {
		echo "\nno answer $answer_id found in survey $survey_id\n";		
		return;
	}
 
//if you get here, then $response is set to the wanted array.
Last Edit: 4 years 6 months ago by bebs. Reason: added php code, for inspiration.

Please Log in to join the conversation.

More
4 years 4 months ago #95748 by bebs
bebs replied the topic: how to export (RPC) a given answer by its ID?
bump: How do I export (using remote control) only 1 answer that interests me? I know it's ID, but of course not its "position" in the answers table as they may not match.

Please Log in to join the conversation.

More
4 years 4 months ago #95965 by Mazi
Mazi replied the topic: how to export (RPC) a given answer by its ID?
I think it makes sense to have a similar function like "export_single_response" which accepts a token or data set ID.

Feel free to file a feature request.


Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)survey-consulting.com'"

Please Log in to join the conversation.

More
4 years 4 months ago #96235 by bebs
bebs replied the topic: how to export (RPC) a given answer by its ID?
added feature request 7857 , thanks.
B.

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now