Contact Jan Thomas Consulting

January 12, 2010

Use Cforms in WordPress to create a short quiz or questionnaire

Filed under: HomePage,WordPress Techniques — AdminJan @ 8:22 pm

Do you want to vary the CFormsII success message? Maybe you want to create a short quiz that can be scored and reported upon completion. CFormsII provides the ability to alter the success message before the form is processed. For something simpler, create a questionnaire with no scoring involved. Learn how to set these up.

[The quiz and survey solutions require knowledge of php programming for scoring. After being submitted, the form is scored in a php function.]

View the Questionnaire Demo

View the Quiz Demo (Scoring: one correct answer)

View the Health Survey Demo (Scoring: answers weighted from 0 to 3)

Questionnaire Instructions: No Scoring

  1. Create a Form in CformsII. Add the form to a WordPress page or post. Click for more information on creating a CFormsII form.

    Here’s what the demo Questionnaire looks like in the CForms Designer: WebsiteSurvey
  2. The survey results will be sent to the admin’s email account and the user’s email with the CC option.

Quiz Instructions: Scoring is Involved

  1. Create a Form in CformsII. Add the form to a WordPress page or post. Click for more information.

    Remember the form number and field numbers.

    This form below is form #8 and the quiz includes fields 4 through 8.

    Each of the radio button answers are set up with a value = 1 for the correct answer and a value = 0 for the incorrect answer.

    tvquestionnaire

  2. Edit my-functions.php .
    • Use the editor of your choice to edit wp-content/plugins/cforms/my-functions.php.
      OR
      Go into Plugins->Editor. Select plugin to edit: cforms. Click the cforms/my-functions.php file near the bottom of the long list of files.
    • The my-functions.php file has sample code for numerous actions. For example setting the redirection link, or changing an email address. By default, these functions are commented out.
    • Define my_cforms_logic to change the success message. In this example, the values for fields 4, 5, 6, 7, 8 are totaled. Correct answers have a value of 1 and incorrect answers have a value of 0 so the total represents the score.
    • Within the my_cforms_logic function, there are several ways to access the field data. In this example, the field value is referenced using it’s field number when combined with $$$.
    • To see the variables that hold field data use the @email function. The results will be sent to email.
  3. Save Changes to the server
  4. Run the form.

Here’s the code:

 
function my_cforms_logic($cformsdata,$oldvalue,$setting) {
 
   // Form number 7 is the Health Quiz:  the radio button value is weighted from 0 to 3.  This value gets added for a total score.
    if (( $cformsdata['id']==7 ) && ( $setting == "successMessage" )) {
         ### note: '$$$mypick' references the ID of the HTML element and has been assigned
         ### to the drop down field in the form configuration, with [id:mypick] !
	 $form   = $cformsdata['data'];
 
	 $total = $max = 0;
         $ptsPossible = 3;
 
	 for ($i = 4; $i <= 8; $i++) {
	 	$curField = '$$$' . $i;
	 	$total += $form[$form[$curField]];
                $max += $ptsPossible;
	 }
 
         $yourScore = "<p>You scored " . $total . " out of " . $max . "</p>";
	 if ( $total >= 12 )
	        return $yourScore . ' <p>Good Work!  Keep up your healthy eating habits</p> ' . $oldvalue ;
         else if ($total >=8)
                return $yourScore . ' <p>Your score indicates a need for improvement.  Try adding some healthier elements to your diet.</p> ' . $oldvalue ;
         else 
               return $yourScore . ' <p>Your score shows a need for improvement.  Try adding some healthier elements to your diet:  Whole wheat cereals and breads are found to reduce heart disease.  Fish such as salmon and halibut have large quantities of omega-3 fatty acids.  These can reduce cholesterol.  USDA recommends 20% of your total daily intake of fat.  Especially the unsaturated fats found in plants.</p> ' . $oldvalue;
 
//@mail('your@email.com', 'cforms my_filter_nonAjax test', 'Form data array (nonAjax):'.print_r($cformsdata,1), 'From: your@email.com');
	}
 
         // Form number 8 is the TV Questionnaire:  the radio button value: 0 = wrong, 1 = correct.  This value gets added for a total score.
         if (( $cformsdata['id']==8 ) && ( $setting == "successMessage" )) {
 
		### note: '$$$mypick' references the ID of the HTML element and has been assigned
                ### to the drop down field in the form configuration, with [id:mypick] !
		$form   = $cformsdata['data'];
 
		$totalCorrect = 0;
		$total = 0;
 		for ($i = 4; $i <= 8; $i++) {
		         $curField = '$$$' . $i;
		         $totalCorrect += $form[$form[$curField]];
                         $total++;
 		}
 
                $percent = ($totalCorrect / $total) * 100;
                $yourScore = "<p>You scored " . $totalCorrect . " out of " . $total . ".  That's " . $percent . "%!</p>";
		if ( $total >= 4 )
	               return $yourScore . ' <p>Good Work!</p>' . $oldvalue ;
		else
		      return $yourScore . ' <p>Better Luck Next Time!</p>';
 
		//@mail('your@email.com', 'cforms my_filter_nonAjax test', 'Form data array (nonAjax):'.print_r($cformsdata,1), 'From: your@email.com');
	}
	return $oldvalue;
}

2 Comments »

  1. How could I create a quiz that will display the answers as the user is taking the test?

    Comment by Todd — September 10, 2010 @ 12:35 pm

  2. Hi !

    Thank you very much for your code !
    It is very helpfull and works like a charm. Great !

    Have a nice day,
    Remi (France)

    Comment by remi — August 13, 2011 @ 10:46 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

© 2011 JanBThomas Last Updated January 2011. Privacy Policy

Powered by WordPress