Creating a custom spiritual gifts assessment questionnaire – PART II

Another week has passed and it’s time to continue our journey in creating a custom spiritual gifts assessment questionnaire.

Picking up from where we left off from last week’s tutorial, we currently completed the following:

  • Created a php form
  • Created an excel/csv spreadsheet of spiritual gift questions and a CCB spiritual gifts key
  • Parsed excel/csv spreadsheet of display questions for form viewing via a web browser

Today’s tutorial focuses on the following three items:

  • Submitting and validating form data
  • Excluding certain question categories based on category key
  • Summing, grouping, sorting and storing results via an excel/csv spreadsheet

We don’t have time to waste, so let’s jump head over heels in and get started with the next install of this tutorial series.

Setting up initialization variables

To start, set up showNoForm and msg variables before the formFieldData variable used in the last tutorial (as shown below).

The showNoForm variable allows for the form to not be displayed upon successful completion and submission when its value is set to true.

The msg variable is used to display both pass and fail text for form validation and submission.

Both variables are covered in more detail later in the tutorial.

Submitting the form and validating form data

You currently have your php form, form questions, and a submit button based on completing the previous tutorial.

Knowing this, to capture the data from a form submission, we must capture the form’s submit button trigger.

To do so, we’ll use an if else statement, meaning if the submit button is pressed or triggered, then we’ll proceed with capturing and validating the form data.

If not, then we simply display the form questions as if the form was never submitted.

Within the if part of the if else statement, we first make sure the form button exists using PHP’s built-in isset function, then we make sure the submit button’s value is equal to a case-sensitive text Submit.

Next, it’s time to capture the form data for the following fields: First Name, Last Name, Email, and each survey answer.

Create the aforementioned variables and set each equal to the form field name using PHP’s super global variable $_REQUEST.

Notice that the cleanInput function is used to sanitize the form input. It’s ALWAYS important to sanitize form input submitted by users. NEVER trust or ASSUME a user to enter the correct information.

After all, the user could be a misfit of some sort attempting to delete your database, take your website down, or hack your website to redirect it to a high risk website.

Nevertheless, although the cleanInput function is used, it’s recommended you use PHP’s built in filters to ensure sanitize and validation of your application receiving the correct input.

Finally, set the else statement and the formFieldData variable from the previous tutorial within it, closing the else statement.

Capturing, summing, grouping, and sorting answers

Okay, it’s time for a bit more heavy lifting. The form has been submitted, and form fields captured, validated, and sanitized.

Next, we must total the values for each spiritual gifting and rank/sort the spiritual gifting in descending order based on their summed value.

The next step in the process is to capture the answers in an array. Create the arrAnsKey variable and set it equal to the prepareAnswerKeyArray function, passing to this function the answer variable as its argument.

In short, the prepareAnswerKeyArray function creates and returns a multi-dimensional array of category and answer bindings using the question-key.csv file to retrieve the category and the form field answer arrays.

Next, create the arrGroupSumSort variable and set it equal to the prepareGroupSumSortArrray function, passing to this function the arrAnsKey variable.

In short, the prepareGroupSumSortArray function groups and sums each spiritual gifting category based on the answers provided for each categorized question ranking of a 1 to 5 value.

Once grouped and summed, the array is sorted in descending order, according to each groups (or spiritual gifting) summed value, using PHP’s built-in arsort function.

Now that we have a ranking for how well a person scored in each spiritual gifting area, let’s discuss how to exclude certain giftings from being viewed via the report that we’ll cover in the coming weeks.

Excluding certain question categories based on category key

Here’s where things get a bit hairy or dicey in conversation. The reason I make this statement is that assessing spiritual gifts can be a tricky conversation to be had in general.

Mix in denominations, bible versions and commentaries, and spiritual maturity or lack of at a personal or corporate level, and we have the making of a war on our hands to say the least.

It’s quite a hot button topic and QUICKER than quick is how we arrive.

Nevertheless, there may be certain spiritual gifts that you or church desire a person to answer but you don’t desire to show or reveal this information to the person immediately.

For instance, Prophecy tends to be a hot button topic. A person could believe they have the gift of prophecy, yet their definition is that they are a Prophet.

See how quick things can go off the rail with such a statement? 🙂

Nevertheless, you may choose not to show a specific category by preparing the survey results to exclude the category and its results.

This is what the prepareResultsData function does. Be sure to set the response_data variable equal to the prepareResultsData function.

Whatever categories are listed in the Excludes column of the question-gift-excludes.csv file used within the prepareGiftExclusions function found in the prepareResultsData function, then those categories are excluded when the data is being prepared to be written to the excel/csv file for reporting.

Storing results via an excel/csv spreadsheet

Okay, you’ve made it this far and you’re almost home free. Hang in there…

From the previous section, pass the firstname, lastname, and response_data variables as arguments in their respective order to the createDataFile function.

This function simply creates and writes data to a csv file (e.g. alvin-brown_sga-resluts_123847263.csv) containing the grouped, summed, and sorted spiritual gifting results prepared in the previous section.

Displaying the success or failure message

Last but not least, create a showNoForm variable and set it equal to true. Then create a msg variable with the following text string: Thank you for completing the spiritual gifts assessment.

Of course, you could easily turn the string into a variable if you wanted to use this same form format for another type of survey (hint, hint). 😉

And before testing, you’ll need to echo the msg variable inside the form, as well as create a if statement for showing and hiding the form based on the presence of the showNoForm variable, just before the H1 tag.

And don’t for get to close the showNoForm variable if statement just before the closing form tag and after the submit button.

Time to save and test your technical prowess

Well, you made it to the end of this tutorial. 😉 As for what’s next…

Next week, I’ll show you how to complete the following:

  • Create a page to view a list of completed spiritual gifts assessments
  • Email a summary to download spiritual gifts excel/csv file

For now, you’re code should look like the code below in its entirety.

BY THE WAY, I RECOMMEND PLACING ALL FUNCTIONS INTO A SINGLE FILE INCLUDE MUCH LIKE THE PREVIOUS TUTORIALS USE THE GENERAL.PHP FILE.
THIS MAKES FOR A CLEANER AND MANAGEABLE CODEBASE. 😉

Save your file and you’re now ready to open a web browser for viewing and submitting a spiritual gifts assessment.

Rest up, and be ready to expand your knowledge next week. See you next week.

createDataFile Function

This function creates and stores the spiritual assessment data for unique individuals in a excel/csv file.

prepareResultsData Function

This function prepares the data to be written to excel/csv file, using the prepareGiftExclusions function to not include categories excluded from reporting.

prepareGiftExclusions Function

This function uses the question-gift-excludes.csv to return an array of categories to exclude from reporting.

prepareGroupSumSortArray Function

This function sums each category grouping of questions and sorts the array groupings in descending or by value.

prepareAnswerKeyArray Function

This function binds each questions answer value to the category label assigned to each question as a multi-dimensional array.

cleanInput Function

This function was previously named clean_input function, and was changed to cleanInput function to remain consistent in naming convention.  This function sanitizes form input submitted by user.  I recommend using PHP’s built-in filters for sanitization and validation of form data.

 


Please comment if this tutorial has helped you.