Create token on the fly

More
1 month 3 weeks ago #158147 by DenisChenu
DenisChenu replied the topic: Create token on the fly

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .

Please Log in to join the conversation.

More
1 month 3 weeks ago - 1 month 3 weeks ago #158158 by tpartner
tpartner replied the topic: Create token on the fly
@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

<?php
 
	require_once 'jsonRPCClient.php';
 
	define( 'LS_BASEURL', 'http://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:
{TEMPLATEURL}api_redirect.php?token={TOKEN:TOKEN}&email={TOKEN:EMAIL}&firstname={TOKEN:FIRSTNAME}&lastname={TOKEN:LASTNAME}

Sample template and surveys attached:

File Attachment:

File Name: API_Redire...ey_1.lss
File Size:12 KB

File Attachment:

File Name: API_Redire...ey_2.lss
File Size:13 KB

File Attachment:

File Name: Test_API_Redirect.zip
File Size:92 KB

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
Last Edit: 1 month 3 weeks ago by tpartner.

Please Log in to join the conversation.

More
1 month 2 weeks ago #158161 by DenisChenu
DenisChenu replied the topic: Create token on the fly
My english is surely lesser good than yours (and after rereading : i'm sure you're right).

Else, RC is a good idea and your code is great. Personnaly i surely make a plugin for the same purpose (in fact i already have a part of a plugin : creating token if don't exist with beforeSurveyPage event).

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand (or search sondages pro).
An error happen ? Before make a new topic : remind the Debug mode .

Please Log in to join the conversation.

More
1 month 2 weeks ago #158162 by badronald
badronald replied the topic: Create token on the fly
Thank you. Trying it now. I really appreciate your help.

Please Log in to join the conversation.

More
1 month 2 weeks ago #158166 by badronald
badronald replied the topic: Create token on the fly
I am getting this error:


Fatal error: Uncaught exception 'Exception' with message 'Incorrect response id (request id: 1, response id: )' in /home/nhs2/public_html/limesurvey206/upload/templates/citronadeNHS3MOD9/jsonRPCClient.php:154 Stack trace: #0 /home/nhs2/public_html/limesurvey206/upload/templates/citronadeNHS3MOD9/api_redirect.php(21): jsonRPCClient->__call('get_session_key', Array) #1 /home/nhs2/public_html/limesurvey206/upload/templates/citronadeNHS3MOD9/api_redirect.php(21): jsonRPCClient->get_session_key('****', '****') #2 {main} thrown in /home/nhs2/public_html/limesurvey206/upload/templates/citronadeNHS3MOD9/jsonRPCClient.php on line 154

Please Log in to join the conversation.

More
1 month 2 weeks ago #158175 by badronald
badronald replied the topic: Create token on the fly
I got it working. Thank you.

Any way to have it check if token exists in second survey and have it resume rather than say token already exists?

Please Log in to join the conversation.

More
1 month 2 weeks ago - 1 month 2 weeks ago #158184 by tpartner
tpartner replied the topic: Create token on the fly
You should be able to use the list_participants() method to check for a pre-existing token something like this:

<?php
 
	require_once 'jsonRPCClient.php';
 
	define( 'LS_BASEURL', 'http://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 );
 
	// Check if token already exists
	$tokenExists = false;
	$aConditions = array("completed"=>"N");
	$listParticipants = $myJSONRPCClient->list_participants($sSessionKey, $iSurveyID, 0, 100000, false, false, $aConditions);
	foreach ($listParticipants as $value) {
		if($value['token'] == $token) {
			$tokenExists = true;
		}
	}
 
	// Token already exists in survey 2, so redirect to survey
	if($tokenExists == true) {
		// Release the session key
		$myJSONRPCClient->release_session_key( $sSessionKey );
 
		// Redirect to survey
		$newURL =  LS_BASEURL.'index.php/'.$iSurveyID.'?lang='.$language.'&token='.$token;
		header('Location: '.$newURL);
		die();
	}
	// No existing token in survey 2, so we'll create one
	else {
 
		// 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];
		}
	}
?>

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last Edit: 1 month 2 weeks ago by tpartner.

Please Log in to join the conversation.

More
1 month 2 weeks ago #158191 by badronald
badronald replied the topic: Create token on the fly
Thank you! Works great.

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now