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.