Search code examples
phpjqueryxml-rpcjson-rpclimesurvey

LimeSurvey RemoteControl2 API - Any add_response PHP examples?


I have a survey I've setup on a third-party server by hand (HTML/JS/CSS/PHP) with some additional features not available in LimeSurvey (i.e. jQuery Mobile, slicker dseign, etc) and set it up to have the same form element name/value pairs as my actual survey as presented and coded in LimeSurvey.

What I'm trying to do to submit the form remotely via the JSON-RPC API is as follows (in PHP, based on their example for retrieving basic Survey data only for add_response):

<?php
include "jsonRPCClient.php";
/* using LimeSurvey (LS) RemoteControly API version 2's JSON-RPC method */
define("LS_BASEURL", 'http://'.$host.$path);  // adjust this one to your actual LimeSurvey URL
define("LS_USER", $username);
define("LS_PASSWORD", $password);

//instantiate a new client
$jsonRPC = new jsonRPCClient(LS_BASEURL);

//receive session key
$sessionKey = $jsonRPC->get_session_key(LS_USER, LS_PASSWORD);

//get response data from FORM input values
$response_data = $_POST; 

//submit survey response data
$groups = $jsonRPC->add_response($sessionKey, $survey_id, $response_data);

//release the session key
$jsonRPC->release_session_key($sessionKey);
?>

I was hoping it would be as easy as posting all my Form's inputs to this script called "limesurvey.collector.php" and simply giving the Form elements the same names (and being careful not to change them further so they get new names/codes assigned).

And I thought it was working as a new record shows up for the proper Survey ID, however the values are not getting sent in, here's what I see on the LimeSurvey side: LimeSurvey results for Survey (NOTE: the first response is a proper one, manually submitted directly from the Survey, the second one is the erroneous one that comes in from the API without any values being set)

I don't think it matters much but here's my jQuery to perform the POST inside the remote FORM:

var POST_DATA = { 
    java527317X1X11 : q1, 
    "527317X1X11" : q1, 
    java527317X1X12 : q2, 
    "527317X1X12" : q2, 
    java527317X1X13 : q3, 
    "527317X1X13" : q3, 
    java527317X1X14 : q4, 
    "527317X1X14" : q4, 
    java527317X1X15 : q5, 
    "527317X1X15" : q5, 
    java527317X1X16 : q6, 
    "527317X1X16" : q6, 
    java527317X1X17 : q7, 
    "527317X1X17" : q7, 
    java527317X1X18 : q8, 
    "527317X1X18" : q8, 
    java527317X1X19 : q9, 
    "527317X1X19" : q9, 
    java527317X1X110 : q10, 
    "527317X1X110" : q10, 
    java527317X1X111 : q11, 
    "527317X1X111" : q11, 
    java527317X1X112 : q12, 
    "527317X1X112" : q12, 
    java527317X1X113 : q13, 
    "527317X1X113" : q13, 
    java527317X1X114 : q14, 
    "527317X1X114" : q14, 
    java527317X1X115 : q15, 
    "527317X1X115" : q15, 
    java527317X1X116 : q16, 
    "527317X1X116" : q16,       
    java527317X1X38 : gender, 
    "527317X1X38" : gender,             
    java527317X1X39 : yearsWork, 
    "527317X1X39" : yearsWork,          
    java527317X1X40 : specialty, 
    "527317X1X40" : specialty,          
    java527317X1X41 : workHours, 
    "527317X1X41" : workHours, 
    lastgroup : "527317X1", 
    relevance1 : "1", 
    relevance38 : "1", 
    relevance39 : "1", 
    relevance40 : "1", 
    relevance41 : "1", 
    relevanceG0 : "1", 
    movesubmit : "qmovesubmit", 
    thisstep : "1", 
    sid : "527317", 
    start_time : "1397651769", 
    LEMpostKey : "145856491"
  };

$.ajax({
  url: PROXY_URL+"http://localhost/survey/limesurvey.collector.php",
  type: "POST",
  data: POST_DATA,
  complete: function(xmlHttp) {
    if(xmlHttp.status.toString() == '200') {                
        $("#framediv").css({'display':'block'});
    } else { 
        window.open(xmlHttp.getResponseHeader('Location'));
        //DEBUG:  
                    console.log("Status: " + xmlHttp.status);
    }
  }

All jQuery value are being set correctly (as I see in my Console logs) using simple jQuery value access: $('#fieldname').val()

Anything else that might be missing? Is maybe just a plaintext POST value not sufficient for the add_response API call? Possibly I need every single response value to be manually formatted into JSON-RPC style individual name/value pairs but if so, which label to use for each response value? The "527317X1X11" or the "java527317X1X11" type one? Or, is it something else more like XML-RPC's <param><value> lists?

EDIT: I do realize it may be significantly easier to simply edit the LimeSurvey CSS/JS templates to add the mobile survey design stuff in, however, this Survey in particular (and others like it) I need to be in full control of not just the custom styles just for the survey but also the domain redirection that happens and do additional processing, such as also submitting the responses to another Medical server hosting a separate (non Lime-Survey) Survey data collection service.

UPDATE: (that PHP code at the top was incomplete, this code works for what I was trying to do):

<?php
include "jsonRPCClient.php";
/* using LimeSurvey (LS) RemoteControly API version 2's JSON-RPC method */
define("LS_BASEURL", 'http://'.$host.$path);  // adjust this one to your actual LimeSurvey URL
define("LS_USER", $username);
define("LS_PASSWORD", $password);

//instantiate a new client
$jsonRPC = new jsonRPCClient(LS_BASEURL);

//receive session key
$sessionKey = $jsonRPC->get_session_key(LS_USER, LS_PASSWORD);


//get response data from FORM input values
$response_data = array(); 
foreach ($_POST as $key => $value) {
  $response_data[$key] = $value;
}  

//submit survey response data
$groups = $jsonRPC->add_response($sessionKey, $survey_id, $response_data);

//release the session key
$jsonRPC->release_session_key($sessionKey);

?>

Solution

  • The $aResponseData must be the array of answer to set in table, looking at your example, to have the same 3 answer for Q1_1,Q1_2,q1_3, you can use

        $response_data = array(
            "527317X1X11" => "3", 
            "527317X1X12" => "2", 
            "527317X1X13" => "3", 
        );
    

    Just test with this first. You can use startdate to set the start date ans submltdate for the submitted date