- Posts: 30
- Thank you received: 0
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Create token on the fly
- badronald
- Topic Author
- Offline
- Junior Member
Less
More
6 years 10 months ago #155474
by badronald
Replied by badronald on topic Create token on the fly
Thank you!
The topic has been locked.
- badronald
- Topic Author
- Offline
- Junior Member
Less
More
- Posts: 30
- Thank you received: 0
6 years 10 months ago - 6 years 10 months ago #155568
by badronald
Replied by badronald on topic Create token on the fly
I currently am using ls 2.06
I need to add tokens as people do the survey, I used to do this via the newtoken.php workaround
I have 2 surveys.....that need to to be connected via a token
I am trying to figure out the remote control feature but I am totally lost.
I have setup survey one to require registration, which creates a token, I would like then to pass this token to survey2 .
Is this possible? usnig end url something like ....
<?php
require_once 'jsonRPCClient.php';
define( 'LS_BASEURL', '/index.php/');
define( 'LS_USER', '****' );
define( 'LS_PASSWORD', '****' );
$iSurveyID = 375394;
$token = 'ABCDE';
// 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("token"=>$token,"language"=>'fr',"emailstatus"=>"OK");
$aParticipantData=array($tokenParams);
$bCreateToken = true;
// Create the tokens
$newToken = $myJSONRPCClient->add_participants( $sSessionKey, $iSurveyID, $aParticipantData, $bCreateToken);
// Print returned results
echo '<hr><br><h1>Limesurvey</h1><br>New token created in survey '.$iSurveyID.':'
.'<ul>'
.'<li>TID - '.$newToken[0].'</li>'
.'<li>Token - '.$newToken[0].'</li>'
.'</ul>';
// Send an invitation to the created token
$tokenIDs = array($newToken[0]);
$newMail = $myJSONRPCClient->invite_participants($sSessionKey, $iSurveyID, $tokenIDs, true);
// Print returned results
if($newMail[$newToken[0]] == 'OK') {
echo 'Invitation sent to:'
.'<ul>'
.'<li>Name - '.$newMail[$newToken[0]].'</li>'
.'<li>Email - '.$newMail[$newToken[0]].'</li>'
.'</ul>';
}
else {
echo 'Error - no invitation sent!';
}
// Release the session key
$myJSONRPCClient->release_session_key( $sSessionKey );
?>
I need to add tokens as people do the survey, I used to do this via the newtoken.php workaround
I have 2 surveys.....that need to to be connected via a token
I am trying to figure out the remote control feature but I am totally lost.
I have setup survey one to require registration, which creates a token, I would like then to pass this token to survey2 .
Is this possible? usnig end url something like ....
<?php
require_once 'jsonRPCClient.php';
define( 'LS_BASEURL', '/index.php/');
define( 'LS_USER', '****' );
define( 'LS_PASSWORD', '****' );
$iSurveyID = 375394;
$token = 'ABCDE';
// 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("token"=>$token,"language"=>'fr',"emailstatus"=>"OK");
$aParticipantData=array($tokenParams);
$bCreateToken = true;
// Create the tokens
$newToken = $myJSONRPCClient->add_participants( $sSessionKey, $iSurveyID, $aParticipantData, $bCreateToken);
// Print returned results
echo '<hr><br><h1>Limesurvey</h1><br>New token created in survey '.$iSurveyID.':'
.'<ul>'
.'<li>TID - '.$newToken[0].'</li>'
.'<li>Token - '.$newToken[0].'</li>'
.'</ul>';
// Send an invitation to the created token
$tokenIDs = array($newToken[0]);
$newMail = $myJSONRPCClient->invite_participants($sSessionKey, $iSurveyID, $tokenIDs, true);
// Print returned results
if($newMail[$newToken[0]] == 'OK') {
echo 'Invitation sent to:'
.'<ul>'
.'<li>Name - '.$newMail[$newToken[0]].'</li>'
.'<li>Email - '.$newMail[$newToken[0]].'</li>'
.'</ul>';
}
else {
echo 'Error - no invitation sent!';
}
// Release the session key
$myJSONRPCClient->release_session_key( $sSessionKey );
?>
Last edit: 6 years 10 months ago by badronald.
The topic has been locked.
- badronald
- Topic Author
- Offline
- Junior Member
Less
More
- Posts: 30
- Thank you received: 0
6 years 7 months ago #158141
by badronald
Replied by badronald on topic Create token on the fly
I am still horribly lost with creating tokens.
I need to register people, then have them complete a survey.
I need it all connected via a token. Ideally, to insert same token into registration table and survey.
I can make a registration form. I can get the remotecontrol API add participants to show and insert a token.
I am having trouble connecting these 2 pieces.... that is....register someone, use the API, and redirect to new survey.
I need to register people, then have them complete a survey.
I need it all connected via a token. Ideally, to insert same token into registration table and survey.
I can make a registration form. I can get the remotecontrol API add participants to show and insert a token.
I am having trouble connecting these 2 pieces.... that is....register someone, use the API, and redirect to new survey.
The topic has been locked.
- DenisChenu
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 13648
- Thank you received: 2491
6 years 7 months ago #158147
by DenisChenu
Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
Replied by DenisChenu on 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 , plugin development .
I don't answer to private message.
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10109
- Thank you received: 3595
6 years 7 months ago - 6 years 7 months ago #158158
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on 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
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:
Sample template and surveys attached:
@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', '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:
Code:
{TEMPLATEURL}api_redirect.php?token={TOKEN:TOKEN}&email={TOKEN:EMAIL}&firstname={TOKEN:FIRSTNAME}&lastname={TOKEN:LASTNAME}
Sample template and surveys attached:
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 6 years 7 months ago by tpartner.
The topic has been locked.
- DenisChenu
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 13648
- Thank you received: 2491
6 years 7 months ago #158161
by DenisChenu
Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
Replied by DenisChenu on 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).
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 , plugin development .
I don't answer to private message.
The topic has been locked.
- badronald
- Topic Author
- Offline
- Junior Member
Less
More
- Posts: 30
- Thank you received: 0
6 years 7 months ago #158162
by badronald
Replied by badronald on topic Create token on the fly
Thank you. Trying it now. I really appreciate your help.
The topic has been locked.
- badronald
- Topic Author
- Offline
- Junior Member
Less
More
- Posts: 30
- Thank you received: 0
6 years 7 months ago #158166
by badronald
Replied by badronald on 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
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
The topic has been locked.
- badronald
- Topic Author
- Offline
- Junior Member
Less
More
- Posts: 30
- Thank you received: 0
6 years 7 months ago #158175
by badronald
Replied by badronald on 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?
Any way to have it check if token exists in second survey and have it resume rather than say token already exists?
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10109
- Thank you received: 3595
6 years 7 months ago - 6 years 7 months ago #158184
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on 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:
Code:
<?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: 6 years 7 months ago by tpartner.
The topic has been locked.
- badronald
- Topic Author
- Offline
- Junior Member
Less
More
- Posts: 30
- Thank you received: 0
6 years 7 months ago #158191
by badronald
Replied by badronald on topic Create token on the fly
Thank you! Works great.
The topic has been locked.
- jinformatique
- Offline
- New Member
Less
More
- Posts: 13
- Thank you received: 1
5 years 11 months ago #168805
by jinformatique
Replied by jinformatique on topic Create token on the fly
Hi, I'm trying to connect to the API with limesurvey3.7.3+180516. I activated JSON-RPC in the UI.
What am I missing?
Here is the result I got. It stops at the 1st var_dump and doesn't show the 2nd.
What am I missing?
Code:
<?php include_once 'vendor/autoload.php'; // include_once '/alfred/vendor/weberhofer/jsonrpcphp/src/org/jsonrpcphp/jsonRPCClient.php'; define('LS_BASEURL', 'https://www.fnak.fr/limesurvey/index.php'); // adjust this one to your actual LimeSurvey URL define('LS_USER', 'user'); define('LS_PASSWORD', 'password'); $iSurveyID = 616685; echo '<pre>'; // Instantiate a new RPC client $myJSONRPCClient = new \org\jsonrpcphp\JsonRPCClient(LS_BASEURL . '/admin/remotecontrol'); var_dump($myJSONRPCClient); // Get a session key $sessionKey = $myJSONRPCClient->get_session_key(LS_USER, LS_PASSWORD); var_dump($sessionKey); // Release the session key $myJSONRPCClient->release_session_key($sessionKey); echo '</pre>';
Here is the result I got. It stops at the 1st var_dump and doesn't show the 2nd.
Code:
object(org\jsonrpcphp\JsonRPCClient)#3 (6) { ["debug":"org\jsonrpcphp\JsonRPCClient":private]=> bool(false) ["url":"org\jsonrpcphp\JsonRPCClient":private]=> string(60) "https://www.fnak.fr/limesurvey/index.php/admin/remotecontrol" ["id":"org\jsonrpcphp\JsonRPCClient":private]=> int(1) ["notification":"org\jsonrpcphp\JsonRPCClient":private]=> bool(false) ["enableCurl":"org\jsonrpcphp\JsonRPCClient":private]=> bool(true) ["proxy"]=> string(0) "" }
The topic has been locked.