I came across an interesting situation this past week in The Village whereby another CCB user questioned about sharing form responses without logging in.

Personally, I’ve never thought there was a need to share form responses, or said another way, I’ve not encountered a need to be able to view form responses without logging in.

Then, the thought hit me about how would one go about sharing responses with a 3rd party that doesn’t have CCB access.

My initial answer would be to give them CCB access to a specific form(s) only, but then that now means you have a CCB profile created for the 3rd party.

Who cares to create and manage additional CCB profiles for 3rd parties?

More importantly, what if there was an external system that needed access to CCB to retrieve form responses for all forms or a specific form?

Is it possible to gain access or share CCB form responses, and not use the CCB web front-end interface?

Well, we’ll answer those questions in today’s tutorial about Sharing CCB Form Responses. Let’s get started.

A Word of Caution

Before we jump into the nitty gritty details of how to use CCB’s API to access form details, I want to be clear in that you DO NOT want to allow access or sharing of form data that contains sensitive information, such as the following:

  • Driver’s license
  • Bank account information
  • Social security
  • Credit card or other payment information
  • Prayer requests
  • Personal information

So, before you begin sharing data via the CCB API to 3rd party systems, be sure you ask the rights questions as well as review your form(s) to ensure that NO sensitive information is being shared or compromised.

Variables and Include Files

As we’ve done numerous times over, you’ll need to ensure that you setup the proper variables, file inclusions and comments when starting your tutorial file.

Go ahead and open the text editor of your choice (I use Sublime: Mastering Sublime), saving and naming the file form-responses.php.

Include the necessary file inclusions and comments, and define the following variables with their respective values:

  • apiService – Set the value equal to the form_responses CCB API Service.
  • formId – [optional value] Set the value to be the specific form id of the form you’re wanting to access.
  • individualId – [optional value] Not used in this tutorial, but could be specified to return form responses for a specific individual.
  • formModifyDate – [optional value] Returns form responses after a specific date created or modified using string format of YYYY-MM-DD.

Specify and define the variables you’ll use for your own form. 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
  • form_id => formId variable
  • modified_since => formModifyDate variable

Identify the form id and person id using CCB’s front-end web interface (review this tutorial).

<?PHP

/**
 * retrieve form responses
 */

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

$apiService = 'form_responses'; // CCB api service

$formId = 90;

$formModifyDate = '2015-01-01';

$urlData = array_filter( array(
    'srv' => "$apiService",
    'form_id' => "$formId",
    'modified_since' => "$formModifyDate"
    ) );

?>

Making the CCB API Call and Parse XML Response

It’s time for the heavy lifting to occur now in making the CCB API call.

To complete the API call, define a rss variable and set it equal to the ccbDetails function, passing get the function’s first argument and the urlData variable at its second argument.

Next, define a nodes variable and set its value to use XPATH query to parse the XML response.

Parsing the XML response containing form response(s) can be tricky. What’s tricky about it is you’ll need to know exactly what data you’re in search of.

It’s highly likely that each form is not uniform outside of the contact information, meaning each form is likely to have a multitude of question and answer types.

If you want to include all information in regards to the form response, including contact information, then you’ll want to use the XPATH query expression: //form_responses/form_response/.

If you’re strictly after the answers only and contact information is not a concern, then you’ll want to use the XPATH query expression //form_responses/form_response/answers/.

In this tutorial, I used the latter.

Next, define an empty response_object variable.

<?PHP

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

$nodes = $rss->xpath('//form_responses/form_response/answers');  // xpath for form response answers

$response_object = '';

?>

Now this next little section of code which uses a foreach statement can be a little tricky.

It’s definitely somewhat similar to previous tutorials, but has a new twist because of the variability of form response question and answer types.

It’s very likely that your form is not the same as my form. In fact, the form that I’m using for this tutorial is a child dedication form that captures the gender, name and birthdate of a child.

Share CCB Form Response

If you want to see all the answers for a given form response in array format, then you need to use a bit of JSON encode and decode magic.

Array of Shared CCB Form Response

I say this because it’s likely that you won’t know the actually field names your in search of.

So to find the field names, use the following code within the foreach statement to identify field names for the specific form response:

<?PHP

foreach ($nodes as $node)
{

	echo '<pre>';
	$node = @json_decode(json_encode($node), true);
	print_r($node);
	echo '</pre>';

}

?>

In this tutorial example, my form only has one response and not hundreds as would most church forms.

My desire is to capture the form response title and it’s respective answer. To do this, I’ll simply use a for statement which is like a foreach statement.

For is a bit more flexible than foreach, yet can be a performance nightmare when handling a large array dataset of any sort.

Since I know that there are only 3 answers I’m in search of from my form response, I simply use a for loop statement to return the title and answer values for each answer via a concatenation of the response_object variable.

Simply close the for and foreach statements, and it’s now time to test the code.

<?PHP

foreach ($nodes as $node)
{

	//echo '<pre>';
	$node = @json_decode(json_encode($node), true);
	//print_r($node);
	//echo '</pre>';
	//die();

	for($i=0;$i<sizeof($node['title']);$i++){

        $response_object .= $node['title'][$i].': '.$node['answer_value'][$i].'<br>';

       }
}

echo $response_object;

?>

Time to Bring it Together and Test

There’s not much to this tutorial although you will have to think about a few areas of it.

Remember, DO NOT use this tutorial for sharing of sensitive information within form responses or any other CCB API service that contains sensitive information.

Again, let me know if this tutorial has helped you by leaving a comment below.

<?PHP

/**
 * retrieve form responses
 */

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

$apiService = 'form_responses'; // CCB api service

$formId = 90;

$formModifyDate = '2015-01-01';

$urlData = array_filter( array(
    'srv' => "$apiService",
    'form_id' => "$formId",
    'modified_since' => "$formModifyDate"
    ) );

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

$nodes = $rss->xpath('//form_responses/form_response/answers');  // xpath for form response answers

$response_object = '';

foreach ($nodes as $node)
{

	//echo '<pre>';
	$node = @json_decode(json_encode($node), true);
	//print_r($node);
	//echo '</pre>';
	//die();

	for($i=0;$i<sizeof($node['title']);$i++){

        $response_object .= $node['title'][$i].': '.$node['answer_value'][$i].'<br>';

       }
}

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...