Removing a Person From a CCB Group

It’s tutorial time once more. If you’ve been following along with us, then you know we spent nearly a month creating a report for persons that are overdue in a respective queue.

The report was created, emailed as html report, and saved as a file and attached to an email. It was quite the tutorial series and I highly recommend reviewing it.

As for today, we’re getting back to the basic building blocks with a simple tutorial focused on removing a person from a group using the Church Community Builder’s (CCB) API Service.

Without further ado, let’s get started with today’s tutorial.

Setting Up and Configuring Necessary Variables

Of course, you’re going to have to perform a bit of reading if you’re not quite familiar with how we roll here at CCBTutorials.com when it comes to the setup and configuration of a new tutorial.

I won’t go into much, if any, explanation. You’ll have to review previous tutorials, such as How To Search For Individuals, to get an understanding and deep explantation for why and how we setup the tutorials.

Nevertheless, crack open a text editor of your choice, saving and naming the file group-remove-person.php.

Be sure to include the necessary file inclusions and comments as shown in the code example below.

Next, define the following variables with their respective values:

  • apiService – Set the value to be the remove_individual_from_group CCB API service.
  • groupID – Set the value to be the group id of the group the person is currently associated too.
  • personID – Set the value to be the individual id of the person you’re wanting to remove from a specific group.

Once the variables above have been defined and their respective values set, then define the urlData variable, passing and binding the following variables to their respective key fields as an array, using the built-in PHP array_filter method without a callback:

  • srv => apiService variable
  • id => personID variable
  • group_id => groupID variable

You want to identify the group id and person id using CCB’s front-end web interface (review this tutorial).

<?PHP

/**
 * remove individual from group
 */

// contains resusable globals and functions
include("includes/config.php");

$apiService = 'remove_individual_from_group'; // CCB api service
$groupID = '25'; // Group ID
$personID = '14'; // Person ID

$urlData = array_filter( array(
	'srv' => "$apiService",
    'id' => "$personID",
    'group_id' => "$groupID"
	) );

?>

Make the API and Parse XML Response

Now that the necessary variables have been defined and values configured, it’s time to make the CCB API call using the ccbDetails function as the value of the rss variable.

Simply pass get as the first argument and the urlData variable as the second argument, and we’re off to the races making a curl call.

To parse the XML response of the curl call, define a nodes variable and set its value to use XPATH query expression //participants/participant via rss variable, using the built-in PHP xpath function.

Next, set an empty variable named response_object and set another variable equal to 0, naming it i.

To parse the array return from the XML response via the XPATH query, use a foreach statement, passing to it the nodes variable array as an argument.

Within the foreach statement, identify if the person exists in the group as the code iterates through each group participant.

To do this, use a simple if statement, comparing the node[‘id’] to the personID variable.

If the ids are equal, then the message “Person still exists in group.” is actively set via the concatenation of the response_object variable, and the i variable is incremented.

If the ids are not equal, then the only the i is incremented until there are no more persons to iterate through.

And because 0 was initially set as i’s value, this means that i does not exist and we must use an if statement to determine if the i variable has been incremented. Said another way, we make one last check to ensure that persons are not listed in group as a participant.

Finally, echo the response_object variable to the screen for display of whether or not the person was successfully removed from their respective group.

<?PHP

$rss = ccbDetails('get',$urlData); // transform to XML

$nodes = $rss->xpath('//participants/participant');  // xpath for process queues

$response_object = '';
$i = 0;

foreach ($nodes as $node)
{
	if($node['id'] == $personID)
			$response_object .= 'Person still exists in group.';
	$i++;
}

if(!$i)
	$response_object = 'Person removed from group.';

echo $response_object;

?>

Putting It All Together and Testing

And that’s all there is to it. Now put it all together, save the code, and fire up a web browser and execute the file. It’s time to test your process.

I told you today’s tutorial wouldn’t be as complex as the previous weeks.

In fact, you’re likely to use this same code for other removal API services.

Of course, you must replace the API service with the respective service name, and account for its required and optional parameters.

Well, that’s it for this week. I hope this tutorial helps you achieve removing persons from groups.

If this tutorial has helped or if you have questions, then please drop me a comment below. See you next week!

<?PHP

/**
 * remove individual from group
 */

// contains resusable globals and functions
include("includes/config.php");

$apiService = 'remove_individual_from_group'; // CCB api service
$groupID = '25'; // Group ID
$personID = '14'; // Person ID

$urlData = array_filter( array(
	'srv' => "$apiService",
    'id' => "$personID",
    'group_id' => "$groupID"
	) );

$rss = ccbDetails('get',$urlData); // transform to XML

$nodes = $rss->xpath('//participants/participant');  // xpath for process queues

$response_object = '';
$i = 0;

foreach ($nodes as $node)
{
	if($node['id'] == $personID)
			$response_object .= 'Person still exists in group.';
	$i++;
}

if(!$i)
	$response_object = 'Person removed from group.';

echo $response_object;

?>

 

Related Posts

Subscribe and receive the following...

  • Inside CCB tips and tricks
  • Instant CCB tutorial alerts and updates
  • CCB How To's, Videos, Webinars and more...