@DenisChenu, I think badronald needs to create a token in a second survey.
@badronald, If you need the respondent to register for a first survey, complete it, and then use the API to create the same token in a second survey and redirect the respondent, follow these steps.
1) Add
jsonRPCClient.php and
jsonRPCServer.php to your template directory.
2) Add a file called
api_redirect.php, containing the following code, to your template directory.
- Modify the constants in lines 5-7
- Modify
$iSurveyID
- This is just a quick example so you may want to add some validation/security
Code:
<?php
require_once 'jsonRPCClient.php';
define( 'LS_BASEURL', 'https://path/to/LimeSurvey/');
define( 'LS_USER', 'admin' );
define( 'LS_PASSWORD', 'yourPassword' );
$iSurveyID = '123456'; // The ID of the second survey
$token = $_GET["token"];
$email = $_GET["email"];
$FirstNameAPI = $_GET["firstname"];
$LastNameAPI = $_GET["lastname"];
$language = 'en';
// Instantiate a new RPC client
$myJSONRPCClient = new jsonRPCClient( LS_BASEURL.'/admin/remotecontrol' );
// Get a session key
$sSessionKey= $myJSONRPCClient->get_session_key( LS_USER, LS_PASSWORD );
// Define the token params
$tokenParams = array("email"=>$email,"lastname"=>$LastNameAPI,"firstname"=>$FirstNameAPI,"token"=>$token,"language"=>$language,"emailstatus"=>"OK");
$aParticipantData=array($tokenParams);
$bCreateToken = false;
// Create the token in survey 2
$newToken = $myJSONRPCClient->add_participants( $sSessionKey, $iSurveyID, $aParticipantData, $bCreateToken);
// Release the session key
$myJSONRPCClient->release_session_key( $sSessionKey );
// If token successfully created, redirect to survey
if(isset($newToken[0]['tid']) && isset($newToken[0]['token'])) {
$newURL = LS_BASEURL.'index.php/'.$iSurveyID.'?lang='.$language.'&token='.$newToken[0]['token'];
header('Location: '.$newURL);
die();
}
// Otherwise, display errors
else if (isset($newToken['status'])) {
echo $newToken['status'];
}
else {
echo '<br />';
echo 'Error: '.$newToken[0]['errors']['token'][0];
}
?>
3) Activate and initiate tokens in both surveys.
4) In Survey 1, enable "
Automatically load URL when survey complete?" and insert an End URL like this:
Code:
{TEMPLATEURL}api_redirect.php?token={TOKEN:TOKEN}&email={TOKEN:EMAIL}&firstname={TOKEN:FIRSTNAME}&lastname={TOKEN:LASTNAME}
Sample template and surveys attached: