Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

API: Invite participants to a survey

  • jltrussart11
  • jltrussart11's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
7 years 1 week ago #149690 by jltrussart11
API: Invite participants to a survey was created by jltrussart11
Hi,


This php json script works well to add a participant to the survey 318848.
I'm trying to automatically send the invitation at the end.
Any idea why this routine does'nt work?

Thanks for your precious help!



require_once 'application/libraries/jsonRPCClient.php';

define( 'LS_BASEURL', ' www.vecteursleviers.com/conseil ');
define( 'LS_USER', '***' );
define( 'LS_PASSWORD', '***' );
define( 'LS_DB_HOST', 'localhost' );
define( 'LS_DB_NAME', 'limesurvey' );
define( 'LS_DB_USER', 'root' );
define( 'LS_DB_PASS', '' );
define( 'LS_DB_TABLEPREFIX', 'lime_' );

$iSurveyID = 318848;
$token = 'ABCDE';
$email = 'test@vecteursleviers.com';
$LastNameAPI = 'Nom famille' ;
$FirstNameAPI = 'Prenom' ;


// 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"=>'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 Invitations routine: invite_participants(string $sSessionKey, int $iSurveyID, array $aTokenIds = false, bool $bEmail = true) : array
invite_participants(string $sSessionKey, int $iSurveyID, array $aTokenIds = $newToken, bool $bEmail = true) : array

// Release the session key
$myJSONRPCClient->release_session_key( $sSessionKey );
?>
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 1 week ago - 7 years 1 week ago #149782 by tpartner
Replied by tpartner on topic API: Invite participants to a survey
This will create a token and send an invitation to that token:

Code:
<?php
 
  require_once 'application/libraries/jsonRPCClient.php';
 
  define( 'LS_BASEURL', ' www.pathTo/limesurvey ');
  define( 'LS_USER', '***' );
  define( 'LS_PASSWORD', '***' );
 
  $iSurveyID = 318848;
  $token = 'ABCDE';
  $email = 'example@example.com';
  $LastNameAPI = 'Nom famille' ;
  $FirstNameAPI = 'Prenom' ;
 
 
  // 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"=>'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]['tid'].'</li>'
  .'<li>Token - '.$newToken[0]['token'].'</li>'
  .'</ul>';
 
  // Send an invitation to the created token
  $tokenIDs = array($newToken[0]['tid']);
  $newMail = $myJSONRPCClient->invite_participants($sSessionKey, $iSurveyID, $tokenIDs, true);
 
  // Print returned results
  if($newMail[$newToken[0]['tid']]['status'] == 'OK') {
    echo 'Invitation sent to:'
    .'<ul>'
    .'<li>Name - '.$newMail[$newToken[0]['tid']]['name'].'</li>'
    .'<li>Email - '.$newMail[$newToken[0]['tid']]['email'].'</li>'
    .'</ul>';
  }
  else {
    echo 'Error - no invitation sent!';
  }
 
  // Release the session key
  $myJSONRPCClient->release_session_key( $sSessionKey );
?>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 7 years 1 week ago by tpartner.
The following user(s) said Thank You: Ben_V, jltrussart11
The topic has been locked.
  • jltrussart11
  • jltrussart11's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
7 years 1 week ago #149803 by jltrussart11
Replied by jltrussart11 on topic API: Invite participants to a survey
Great! Works like a charm! Thanks a lot for your precious help!
The topic has been locked.
More
6 years 9 months ago #155257 by first
Replied by first on topic API: Invite participants to a survey

Survey Designer and Programmer
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 9 months ago #155269 by tpartner
Replied by tpartner on topic API: Invite participants to a survey
Such a demand for help in another post is unlikely to get you any.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
More
6 years 9 months ago #155270 by first
Replied by first on topic API: Invite participants to a survey
Hi Tony - Thanks for reply. Just wanted to link related posts. Is this not ok?

Survey Designer and Programmer
The topic has been locked.
More
6 years 8 months ago #156157 by orenh
Replied by orenh on topic API: Invite participants to a survey
Hi,

I am using the invite, and it works but instead of sending the invite to one token its sends it to all the tokens in the survey...

I send to the api the session, the survey id and an array of tids, and it still sends the invite to all the tokens.
How do I make the api send the invite to only one token?

Thank you.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 8 months ago #156187 by tpartner
Replied by tpartner on topic API: Invite participants to a survey
The API should only send invitations to the token IDs that you provide in the $aTokenIds argument - api.limesurvey.org/classes/remotecontrol..._invite_participants

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
More
6 years 8 months ago #156191 by orenh
Replied by orenh on topic API: Invite participants to a survey
Well, I know the API should send to only the ids I tell him, but it actually sends to all the tokens.

Here is the scenario:
1. I created a survey
2. Added 5 new tokens to the survey
3. Using the API I wanted to send an invite only to one of the tokens: here is the code:

// Instantiate a new RPC client
$oRPCClient = new jsonRPCClient( LIME_URL );

// Get a session key
$sessionKey= $oRPCClient->get_session_key( LIME_USER, LIME_PASSWORD );

// Send an invitation the selected token
$surveyID = 751954
$tIDs = array(38);
$res = $oRPCClient->invite_participants($sessionKey, $surveyID, $tIDs, true);


The result of this API call is that it actually sends the invites to all the 5 tokens.

The version of LimeSurvey: Version 2.6.4-lts
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 8 months ago #156203 by tpartner
Replied by tpartner on topic API: Invite participants to a survey
This works for me and only sends to token ID 38 (not token value 38!).

Code:
<?php
 
  require_once 'jsonRPCClient.php';
 
  define( 'LS_BASEURL', 'http://pathTo/limeSurvey/');  
  define( 'LS_USER', 'admin' );
  define( 'LS_PASSWORD', 'password' );
 
  $iSurveyID = 123456;
 
 
  // 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 );
 
  // Send an invitation to a token
  $tokenIDs = array(38);
  $newMail = $myJSONRPCClient->invite_participants($sSessionKey, $iSurveyID, $tokenIDs, true);
 
  // Release the session key
  $myJSONRPCClient->release_session_key( $sSessionKey );
?>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
More
6 years 8 months ago #156374 by first
Replied by first on topic API: Invite participants to a survey
My findings:
Same code works fine in : Version 2.67.1+170626
But send emails to all in : 2.06lts.

@Orenh: Can you log a bug?

Survey Designer and Programmer
The topic has been locked.
More
6 years 8 months ago #156380 by first
Replied by first on topic API: Invite participants to a survey
On further review I realized that it is also not a bug . Instead it seems to be additional feature with latest version of LS.

2.06lts:


latest ls:


Not survey if they are adding or removing features in lts version.

Survey Designer and Programmer
The following user(s) said Thank You: tpartner
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose