Welcome to the LimeSurvey Community Forum

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

RPC API, list_participants and filter result by custom attribute

  • TonyMonast
  • TonyMonast's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 7 months 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 :
Code:
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
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 7 months ago - 3 years 7 months 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.

Code:
<?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) &amp;&amp; (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.
Last edit: 3 years 7 months ago by tpartner.
The following user(s) said Thank You: TonyMonast
The topic has been locked.
  • TonyMonast
  • TonyMonast's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 7 months 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!
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 7 months 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' => 'info@example.com') 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.
The topic has been locked.
  • TonyMonast
  • TonyMonast's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 7 months ago #203486 by TonyMonast
Yes, it's clear enough! Thank you!
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose