Delete participant by survey access token

More
1 year 3 weeks ago #204483 by TonyMonast
Hi,

I'm trying to find the best solution to connect the platform to our event ticketing application.

Each time a customer goes to purchase an event ticket, they will be allowed to complete a survey later. To access the survey, they will need to use the ticket serial number.

1. When purchasing, we can easily add the participant with add_participants.

2. When canceling the ticket in our app, the participant must be deleted in Lime Survey. However, the API function delete_participants needs the participant's tid (not the token). This gives me the impression that I must first retrieve the list of all survey participants with the API, find the participant with the token linked to the ticket serial number, retrieve the tid, then call the function delete_participants with the tid.

Isn't there a more efficient way to sync all this?

Note : I could also periodically remove all unused participants from the survey, then send the new list of participants, but I would like to be able to do the synchronization with each transaction so that it is as much as possible in real time and using the minimum of ressources.

Any idea is welcome.

Thank you
The topic has been locked.
More
1 year 3 weeks ago - 1 year 3 weeks ago #204485 by tpartner
Yes, you will need to make two API calls.

The list_participants() function can be filtered by the token value:

<?php
 
	require_once 'jsonRPCClient.php';
 
	define( 'LS_BASEURL', 'http://pathTo/limeSurvey');  
	define( 'LS_USER', 'admin' );
	define( 'LS_PASSWORD', 'password' );
 
	$iSurveyID = 123456;
 
 
	if(ctype_alnum($iSurveyID) && (strlen($iSurveyID) == 5 || strlen($iSurveyID) == 6)) { // Valid SID format
 
		// Instantiate a new RPC client
		$myJSONRPCClient = new jsonRPCClient( LS_BASEURL.'/index.php/admin/remotecontrol' );
 
		// Get a session key
		$sSessionKey= $myJSONRPCClient->get_session_key( LS_USER, LS_PASSWORD );
 
		if(is_array($sSessionKey)) { // Invalid session
			echo $sSessionKey['status'];
		}
		else if($sSessionKey) { // Valid session
 
			// Filter by token value
			$aConditions = array('token'=>'123456789');
 
			// Get the filtered participants
			$participants = $myJSONRPCClient->list_participants($sSessionKey, $iSurveyID, 0, 10, false, false, $aConditions);
 
			// Print the results
			echo '<ul>';
			foreach($participants as $key => $value) {
				echo '<li>';
					print_r($value);
				echo '</li>';
			}
			echo '</ul>';
		}
 
		// Release the session key
		$myJSONRPCClient->release_session_key( $sSessionKey );
	}
	else { // Invalid SID format
		die( 'Invalid format!' );
	}
 
?>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 1 year 3 weeks ago by tpartner.
The following user(s) said Thank You: TonyMonast
The topic has been locked.
More
1 year 3 weeks ago - 1 year 3 weeks ago #204486 by tpartner
...or, for your use-case:

<?php
 
	require_once 'jsonRPCClient.php';
 
	define( 'LS_BASEURL', 'http://pathTo/limeSurvey');  
	define( 'LS_USER', 'admin' );
	define( 'LS_PASSWORD', 'password' );
 
	$iSurveyID = 123456;
	$token = 123456789;
 
	if(ctype_alnum($iSurveyID) && (strlen($iSurveyID) == 5 || strlen($iSurveyID) == 6)) { // Valid SID format
 
		// Instantiate a new RPC client
		$myJSONRPCClient = new jsonRPCClient( LS_BASEURL.'/index.php/admin/remotecontrol' );
 
		// Get a session key
		$sSessionKey= $myJSONRPCClient->get_session_key( LS_USER, LS_PASSWORD );
 
		if(is_array($sSessionKey)) { // Invalid session
			echo $sSessionKey['status'];
		}
		else if($sSessionKey) { // Valid session
 
			// Filter by token value
			$aConditions = array('token'=>$token);
 
			// Get the filtered participants
			$participants = $myJSONRPCClient->list_participants($sSessionKey, $iSurveyID, 0, 10, false, false, $aConditions);
 
			// Now, delete the first returned participant
			$aTokenIDs = array($participants[0]['tid']);
			$deleteParticipants = $myJSONRPCClient->delete_participants($sSessionKey, $iSurveyID, $aTokenIDs);
		}
 
		// Release the session key
		$myJSONRPCClient->release_session_key( $sSessionKey );
	}
	else { // Invalid SID format
		die( 'Invalid format!' );
	}
 
?>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 1 year 3 weeks ago by tpartner.
The topic has been locked.
More
1 year 3 weeks ago #204488 by TonyMonast
Interesting! I suppose the filter by token value allow you to only get one participant? Is there any way to get a list of participants from a list of tokens? The idea is if a user cancel 100 event tickets, I don't want to do this operation 100 times, but just one.

I suppose I need to get all the participants from Lime Survey then filter it on the application side, right?
The topic has been locked.
More
1 year 3 weeks ago - 1 year 3 weeks ago #204489 by tpartner

I suppose the filter by token value allow you to only get one participant?

Yes, since tokens are unique, the "token" condition can only match one result.

I suppose I need to get all the participants from Lime Survey then filter it on the application side, right?

Not sure I understand this. With filtering on the API call, you will only get one participant

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 1 year 3 weeks ago by tpartner.
The topic has been locked.
More
1 year 3 weeks ago #204490 by tpartner
Oh, wait, the list_participants() function accepts an array for token values in the $aConditions parameter:

$tokens = array('1234', '5678', 'abcd');			
$aConditions = array('token'=>$tokens);

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
1 year 3 weeks ago #204521 by TonyMonast
Hi tpartner,

Are you sure that list_participants() function accepts an array for token values in the $aConditions parameter? When I try it, I get an Illegal operator error message.

When I look at the source code of the api, I see that :
foreach ($aConditions as $columnName => $valueOrTuple) {
                        if (is_array($valueOrTuple)) {
                            /** @var string[] List of operators allowed in query. */
                            $allowedOperators = ['<', '>', '>=', '<=', '=', '<>', 'LIKE'];
                            /** @var string */
                            $operator = $valueOrTuple[0];
                            if (!in_array($operator, $allowedOperators)) {
                                return array('status' => 'Illegal operator: ' . $operator);
                            } elseif ($operator === 'LIKE') {
                                /** @var mixed */
                                $value = $valueOrTuple[1];
                                $oCriteria->addSearchCondition($columnName, $value);
                            } else {
                                /** @var mixed */
                                $value = $valueOrTuple[1];
                                $oCriteria->compare($columnName, $operator . $value);
                            }
                        } elseif (is_string($valueOrTuple)) {
                            if (in_array($columnName, $aConditionFields)) {
                                $aAttributeValues[$columnName] = $valueOrTuple;
                            }
                        } else {
                            // Silent ignore?
                        }
                    }

If the value of the condition token is an array, it will look for an operator at array position 0 (ex.: <', '>', '>=', '<=', '=', '<>', 'LIKE'), then the value at array position 1. I don't see any operators in your example.
The topic has been locked.
More
1 year 3 weeks ago - 1 year 3 weeks ago #204528 by tpartner
This works in version 3.22:

<?php
 
	require_once 'jsonRPCClient.php';
 
	define( 'LS_BASEURL', 'http://pathTo/limeSurvey');  
	define( 'LS_USER', 'admin' );
	define( 'LS_PASSWORD', 'password' );
 
	$iSurveyID = 429116;
 
 
	if(ctype_alnum($iSurveyID) && (strlen($iSurveyID) == 5 || strlen($iSurveyID) == 6)) { // Valid SID format
 
		// Instantiate a new RPC client
		$myJSONRPCClient = new jsonRPCClient( LS_BASEURL.'/index.php/admin/remotecontrol' );
 
		// Get a session key
		$sSessionKey= $myJSONRPCClient->get_session_key( LS_USER, LS_PASSWORD );
 
		if(is_array($sSessionKey)) { // Invalid session
			echo $sSessionKey['status'];
		}
		else if($sSessionKey) { // Valid session
 
			$aAttributes = array('attribute_1', 'attribute_2');
			$tokens = array('1111', '2');			
			$aConditions = array('token'=>$tokens);	
 
			$participants = $myJSONRPCClient->list_participants($sSessionKey, $iSurveyID, 0, 10, false, $aAttributes, $aConditions);
 
			// Print the results
			echo '<ul>';
			foreach($participants as $key => $value) {
				echo '<li>';
					print_r($value);
				echo '</li>';
			}
			echo '</ul>';
			echo '<br /><br />';
		}
 
		// Release the session key
		$myJSONRPCClient->release_session_key( $sSessionKey );
	}
	else { // Invalid SID format
		die( 'Invalid format!' );
	}
 
?>


Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
Last edit: 1 year 3 weeks ago by tpartner.
The topic has been locked.
More
1 year 3 weeks ago #204530 by TonyMonast
I use the Version 4.3.10+200812. I compared both versions and the code is very different for list_participants. I suppose the multiple tokens it's not supported anymore by list_participants()
The topic has been locked.
More
1 year 3 weeks ago #204532 by tpartner
I would not use 4.x for any production surveys. It is still too buggy.

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
1 year 3 weeks ago #204536 by tpartner

I suppose the multiple tokens it's not supported anymore by list_participants()

This appears to be the case. I consider this to be a serious regression. Please file a bug report and give a link here.

I have commented on the feature commit - github.com/LimeSurvey/LimeSurvey/commit/...26bd81895f848ba471ad

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: TonyMonast
The topic has been locked.
More
1 year 3 weeks ago #204537 by TonyMonast
Well we are new to LimeSurvey so we took the latest version available. We don't need to be backward compatible with anything and we want to be up to date from the start. They also suggest version 4 on their download page.

I agree that there are bugs. I have also reported a few, including a fairly serious one concerning duplicate tokens. This will be fixed in the next version.

I'll take your advice and report this problem too.

Thanks for your help!
The topic has been locked.
More
1 year 3 weeks ago #204539 by TonyMonast
The following user(s) said Thank You: tpartner
The topic has been locked.

Start now!

Just create your account and start using Limesurvey today.

Register now