Advertisement
chrishajer

Grade a 20 question quiz and provide percentage correct

Aug 21st, 2012
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.19 KB | None | 0 0
  1. // Chris Hajer 21 August 2012
  2. // reference http://www.gravityhelp.com/forums/topic/gravity-forms-quiz-help-needed#post-71088
  3. add_action('gform_pre_submission', 'social_media_quiz_score');
  4. function social_media_quiz_score ($form) {
  5.         // change the number 2 here to your form ID
  6.         // if this is not form 2, don't do any processing
  7.         if($form['id'] != 2)
  8.                 return $form;
  9.  
  10.         // otherwise ...
  11.         $score = 0;
  12.  
  13.         // my radio button inputs are numbered 1 to 20.  Change the beginning
  14.         // and ending number in this loop to match your actual input numbers
  15.         for($i=1; $i<=20; $i++) {
  16.                 // add the value of the $_POST value for input 1 to input 20
  17.                 $input  = 'input_' . $i;
  18.                 $score += rgpost($input);
  19.         }
  20.  
  21.         // update the hidden 'Score' field with our calculated score
  22.         $_POST['input_25'] = (int)($score * 5);
  23.         switch($score) {
  24.                 // my "admin only" Rating field to hold the display message based on the score is input_24
  25.                 case 0:
  26.                 case 1:
  27.                 case 2:
  28.                 case 3:
  29.                 case 4:
  30.                 case 5:
  31.                         $_POST['input_24'] = '5 or fewer correct answers: You really don\'t know much about social media.';
  32.                         break;
  33.                 case 6:
  34.                 case 7:
  35.                 case 8:
  36.                 case 9:
  37.                 case 10:
  38.                         $_POST['input_24'] = '6 to 10 correct answers: I guess you know a little bit about social media.';
  39.                         break;
  40.                 case 11:
  41.                 case 12:
  42.                 case 13:
  43.                 case 14:
  44.                 case 15:
  45.                         $_POST['input_24'] = '11 to 15 correct answers: You seem to know what you\'re talking about when it comes to social media.';
  46.                         break;
  47.                 default:
  48.                         $_POST['input_24'] = 'More than 15 correct answers: You might possibly know as much as I do about social media.  Maybe I should hire you.';
  49.         }
  50.         // return the form object
  51.         return $form;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement