Creating a custom spiritual gifts assessment questionnaire – PART V

Featured in Alltop

Welcome back for the final install of our month long series focused on creating a custom spiritual gifts assessment questionnaire.

If you’ve been following along for the last month or so, then you’ve likely wondered when CCB was going to be introduced to the custom spiritual gifts assessment questionnaire.

Well today is the day.  I’ll take you step-by-step and show you the following:

  • how to take the custom spiritual gifts assessment questionnaire results
  • update a CCB profile’s fit with the top 5 spiritual gifts

As a quick recap, review the following tutorials if you’re just dropping in on us:

We have no time to waste today, so let’s get started.

Setup includes, sanitization, validation and variables

Open a text editor of your choice, and name and save the following file: updateCCBFit.php.  Make sure this file is in the same directory as the reports.php file.

Just as we’ve executed in previous tutorials, include the config.php file.

As explained and executed in Part IV, define a file variable and set it equal to the clean_input function with $_REQUEST[‘file’] as its argument.

Next, use an if statement to verify the file variable is empty or the actual file doesn’t exist.

If either are true, then redirect the user to the index.php using PHP’s built-in header method.  Read Part V for more in depth information.

Next, define a giftingRating variable and set its value equal to 15.  I choose 15 because the top 5 spiritual gifting rankings when totaled were equal to or greater than 15.  Feel free to change this value to your own liking. Again, I prefer the top 5 spiritual gifts to be updated in CCB for the respective person’s profile.

Last but not least, define a personID variable and set it equal to a person’s CCB ID.  For the sake of this example, I’ve chosen to hardcode the value.  You could easily run a person lookup to retrieve the person’s id from CCB with a little extra elbow grease.

Retrieve the person’s current fit selections

Before updating a person’s respective fit selections with new selections, ensure you retrieve and store the current selections.

If not, you’ll overwrite a person’s current fit selections simply updating their fit with new fit or spiritual gifting selections.

To retrieve the person’s current fit selections, define a personFitArray variable and set it equal to the getPersonFitArray function, passing it the personID variable as the function argument.

The function simply returns the fit or spiritual gifting name and its respective id as a key-value pair array.

Retrieve the person’s top 5 spiritual gifts

To retrieve a person’s top 5 spiritual gifts, define a personSGAFitArray variable and set it equal to the getPersonSGAFitArray function, passing it the file variable as the function argument.

This function simply reads the actual csv file and returns an array of only the spiritual gifts with a total value greater than or equal to the giftingRating variable defined at the beginning of this tutorial.

Retrieve the default list of spiritual gifts

Since we don’t have the id of each spiritual gift return as an array stored as the personSGAFitArray variable, then we must retrieve the default list of spiritual gifts with their respective ids.

To retrieve this list, define a giftingListingArray variable and set it equal to the getGiftingList function.

Time for a bit of tricky PHP heavy lifting and logic

We’re about to become if, if else, and foreach nested happy in the following lines of code in this section.  It’ll be a bit more complicated than in previous weeks, but hang in there.  You’ll make it! 🙂

Before we move too far into the heavy lifting, define the follow two variables as empty arrays:

  • newGiftingArray variable will contain list that will be updated in CCB profile
  • lastGifting variable is the last known gifting used to keep us from having duplicate gifts captured in the newGiftingArray variable

The first step in this heavy lifting logic and process is to parse each of the array values stored in the personSGAFitArray, also known as the top 5 spiritual gifts for a person.  To do this, execute a foreach statement as shown below.

Within this foreach statement, find the integer id value of each spiritual gift’s string value.  To do this, execute an additional foreach statement parsing the giftingListArray variable’s array value into key=>pair values as shown below.

Within the giftingListArray foreach statement, create an if statement for when the default gift list string is equal to that of the respective top 5 gifting of the person, meaning the gifting has been matched or found.

Within this if statement, execute an if else statement to ensure there’s a defined value for the personFitArray variable.  

If a value is defined, then we know that a person has current fit or spiritual gift selections checked in CCB.  If a value is not defined, then we know that person did not have current fit or spiritual gift selections checked in CCB.

Within this if statement, execute a foreach statement that parses each personFitArray array values as key=>value pairs. 

Within the foreach statement, execute an if else statement used to rule out duplicates.  There is nothing more to do within the if of this if else statement.  In the else section of the if else statement, execute another if statement to ensure the gifting currently checked is not in the lastGifting array.  This simply rules out duplicates.  If the new gifting is not found in the lastGifting array, then add the new gifting string and respective id to the personFitArray array.

Now we’re ready to traverse the nest back up a few levels to the if statement for the personFitArray, rather write logic for the else section in the if personFitArray variable statement.

This else statement is basically written for a personFitArray not having current fit or spiritual gift selections checked in CCB initially.  So, basically copy and paste the last three lines of code and paste lines into the else section of the if statement for the personFitArray.  Yes, just that easy.

Updating a person’s fit with new selections

Okay, this tutorial is not for the faint of heart, but we’re almost to the finish line. 🙂  

It’s time to update the person’s fit in CCB with new selections.

To do so, define a newPersonFitArray variable and set it equal to an empty array.

Next, create a foreach statement using the personFitArray.  Parse each array value to retrieve the id for each respective fit selection to be updated in CCB for the person.

Within the foreach statement define the newPersonFitArray array variable and set it equal to the newGiftingData variable zero index (i.e., the fit id).  Close the foreach statement.

Finally, set a response_object variable and set it equal to the updatePersonGifting function, passing to it the personID as the first argument and the newPersonFitArray as the second argument.

Finally, echo response_object variable so you can see which spiritual gifts have been updated.

This is what the code sections, in their entirety, above displays when executing and viewing script from a web browser.

Bring it together and time to test

WHEW!  What a tutorial this was today and this month!  It’s taken quite a bit of time to prepare and model this tutorial.  We might light on tutorials over the next month. 🙂

Nevertheless, your code should now look like the code below in its entirety.  Don’t forget to save the functions in the general.php file.

Again, with a little extra work, you can make this code dynamic by executing a person search and CCB id return instead of hardcoding the personID as we’ve executed in this tutorial.

If you would like additional help, please don’t hesitate to contact us to implement and integrate this tutorial for your church’s CCB account.  We’d love too!

That’s it for today and this month.  See you back here next week for a new tutorial to kick off a new month.

prepareFitOutput function

This functions parses an array of spiritual gift values and prepares a titled HTML list of spiritual gift values

updatePersonGifting function

This function uses the update_individual_fit CCB API service to update person’s spiritual gifts or fit selections in CCB.

getGiftingList function

This function uses the gift_list CCB API service to retrieve an key=>value pair array listing of default spiriting gifts and their respective ids.

getPersonSGAFitArray function

This function parses the spiritual gifts assessment results file and returns an string value array of the top 5 spiritual gifts that are equal to or greater than the giftingRating global variable.

getPersonFitArray function

This function uses the individual_fit CCB API service to retrieve an key=>value pair array listing of selected spiriting gifts and their respective ids for a given person.

 


Please comment if this tutorial has helped you.