Welcome, Guest
Username: Password: Remember me

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

how to export (RPC) a given answer by its ID? 1 year 5 months ago #93428

  • bebs
  • bebs's Avatar
  • OFFLINE
  • Senior Lime
  • Posts: 48
  • Thank you received: 2
  • Karma: 0
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: 1 year 5 months ago by bebs. Reason: added php code, for inspiration.
The administrator has disabled public write access.

how to export (RPC) a given answer by its ID? 1 year 3 months ago #95748

  • bebs
  • bebs's Avatar
  • OFFLINE
  • Senior Lime
  • Posts: 48
  • Thank you received: 2
  • Karma: 0
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.
The administrator has disabled public write access.

how to export (RPC) a given answer by its ID? 1 year 3 months ago #95965

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5324
  • Thank you received: 294
  • Karma: 249
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)limesurvey.org'"
The administrator has disabled public write access.

how to export (RPC) a given answer by its ID? 1 year 3 months ago #96235

  • bebs
  • bebs's Avatar
  • OFFLINE
  • Senior Lime
  • Posts: 48
  • Thank you received: 2
  • Karma: 0
added feature request 7857, thanks.
B.
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.172 seconds
Donation Image