Advertisement
chrishajer

calculations for confirmation and entry

Sep 15th, 2011
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.83 KB | None | 0 0
  1. // http://www.gravityhelp.com/forums/topic/simple-calculations
  2. // change the 41 here to your form ID
  3. add_action('gform_pre_submission_41', 'ch_awesomeness_rating');
  4. function ch_awesomeness_rating($form) {
  5.  
  6.     // set up one array for each step of the form
  7.     // each array contains the input IDs of the fields we want to sum on each page
  8.     // IDs do not need to be consecutive using this method
  9.     $step_1_fields = array('input_2',  'input_3',  'input_4',  'input_5',  'input_6',  'input_7',  'input_8',  'input_9',  'input_10', 'input_11');
  10.     $step_2_fields = array('input_14', 'input_15', 'input_16', 'input_17', 'input_18', 'input_19', 'input_20', 'input_21', 'input_22', 'input_23');
  11.     $step_3_fields = array('input_27', 'input_28', 'input_29', 'input_30', 'input_31', 'input_32', 'input_33', 'input_34', 'input_35', 'input_36');
  12.  
  13.     // loop through inputs for each step individually
  14.     $culture = 0;
  15.     foreach($step_1_fields as $value)
  16.         // add each value to $step1_score
  17.         $culture += rgpost($value);
  18.  
  19.     $process = 0;
  20.     foreach($step_2_fields as $value)
  21.         // do the same for step 2
  22.         $process += rgpost($value);
  23.  
  24.     $behavior = 0;
  25.     foreach($step_3_fields as $value)
  26.         // and also for step 3
  27.         $behavior += rgpost($value);
  28.  
  29.     // total of the subtotals for each step
  30.     $overall = $culture + $process + $behavior;
  31.  
  32.     // submit these calculated values to the form so they are stored with the entry and can be used in the confirmation
  33.     $_POST['input_38'] = $culture;
  34.     $_POST['input_39'] = $process;
  35.     $_POST['input_40'] = $behavior;
  36.     $_POST['input_41'] = $overall; 
  37.  
  38.     // be sure to return the form when we're done
  39.     return $form;
  40. }
  41.  
  42. // http://www.gravityhelp.com/forums/topic/simple-calculations
  43. // change the 41 here to your form ID
  44. add_filter('gform_confirmation_41', 'ch_courage_confirmation', 10, 4);
  45. function ch_courage_confirmation($confirmation, $form, $lead, $ajax) {
  46.  
  47.     // beginning of the confirmation message
  48.     $confirmation = "<a name='gf_41' class='gform_anchor' ></a><div id='gforms_confirmation_message' class='gform_confirmation_message_41' style='text-align:left;'>";
  49.  
  50.     // set the "lowest score" message as a default and change it if a higher score is achieved
  51.     $grading = 'This score assumes that you probably have "lack of courage" issues in all three areas of culture, process, and behavior. You’ll need to pick your battles and figure out where you can concentrate your efforts at first.';
  52.  
  53.     // reset the confirmation message based on the overall score, checking for lowest scores first
  54.     // this will bump the message up if a higher overall score is found
  55.     // this could have been done with an if, else if statement as well
  56.     if($lead[41] > 75)
  57.         $grading = 'Not a bad start. Pay close attention to which of the three areas (or particular questions) scored highest and lowest. Can you do more of the bright spots? Can you scrap some things that are completely not courageous?';
  58.     if($lead[41] > 150)
  59.         $grading = 'Your organization is well on the way to being courageous! You can freely concentrate on the areas that scored lower than others.';
  60.     if($lead[41] > 225)
  61.         $grading = 'If you have more than about 240 points, please call us, because we want to feature you on the blog as an example of a courageous organization. Nice job.';
  62.  
  63.     // append this conditional information to the confirmation text entered in the form builder
  64.     $confirmation .= "<strong>Overall score: " . $lead[41] .".</strong> ". $grading;
  65.  
  66.     // display the subtotal score on the confirmation page as well
  67.     // 38 is courage, 39 is process, 40 is behavior
  68.     $confirmation .= '<h4>Here are your totals for each section</h4><ul class="step_scores" style="padding-left:20px;"><li><strong>Courage:</strong> ' .$lead[38]. '</li><li><strong>Process:</strong> ' .$lead[39]. '</li><li><strong>Behavior:</strong> ' .$lead[40]. '</li></ul></div>';
  69.  
  70.     // return the confirmation 
  71.     return $confirmation;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement