RPC API, list_participants and filter result by custom attribute

More
1 month 3 weeks ago #203363 by TonyMonast
Hi,

I want to use the RPC API to get all the participants of a survey with a specific custom attribute value. I found this :
list_participants(string $sSessionKey,integer $iSurveyID,integer $iStart,integer $iLimit = 10,boolean $bUnused = false,boolean|array $aAttributes = false,array $aConditions = array()): array


I'm able to call it and get all the participants, but when I try to use the conditions argument, I always get null as a result.

1. Is that possible to filter the participants by attribute by using the conditions argument of list_participants?
2. What is the syntax?

Thank you

Please Log in to join the conversation.

LimeSurvey Partners
More
1 month 2 weeks ago - 1 month 2 weeks ago #203392 by tpartner
Here is a PHP example. In this case, all tokens with a last name of 'Doe' and an attribute_1 value of 'xyz' will be returned.

Also, since $aAttributes is defined, the values for attribute_1 and attribute_2 will be returned for each of those tokens.

<?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
 
			// Define the additional attributes to be returned
			$aAttributes = array('attribute_1', 'attribute_2');
			// Define filters for participants
			$aConditions = array('lastname'=>'Doe', 'attribute_1'=>'xyz');
 
			// Get the filtered participants
			$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>';
		}
 
		// 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.
Official LimeSurvey Partner - partnersurveys.com
Last edit: 1 month 2 weeks ago by tpartner.
The following user(s) said Thank You: TonyMonast

Please Log in to join the conversation.

More
1 month 2 weeks ago #203394 by TonyMonast
There is no indication in the documentation that the conditions argument is an associative array (arrays with named keys). It would be relevant to mention it.

Thank you tpartner for your help! It works!

Please Log in to join the conversation.

More
1 month 2 weeks ago #203463 by DenisChenu

TonyMonast wrote: There is no indication in the documentation that the conditions argument is an associative array (arrays with named keys). It would be relevant to mention it.

1. wiki and code can be improved by user …
2. If it's really unclear : all doc come from phpDoc
3. Then you can create pull request : github.com/LimeSurvey/LimeSurvey/blob/cd...rol_handle.php#L2071

Seems clear enough here ?
«Optional conditions to limit the list, e.g. with array('email' => 'This email address is being protected from spambots. You need JavaScript enabled to view it.') or array('validuntil' => array('>', '2019-01-01 00:00:00'))»

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.

Please Log in to join the conversation.

More
1 month 2 weeks ago #203486 by TonyMonast
Yes, it's clear enough! Thank you!

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now