Skip to content

Instantly share code, notes, and snippets.

@lucanos
Forked from ebinnion/gist:2346560
Last active March 22, 2016 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucanos/c886043e8eacad6b32ca to your computer and use it in GitHub Desktop.
Save lucanos/c886043e8eacad6b32ca to your computer and use it in GitHub Desktop.
Gravity Forms Pre-Submission and Notification
// http://www.gravityhelp.com/forums/topic/simple-calculations
// change the 41 here to your form ID
add_action('gform_pre_submission_41', 'ch_awesomeness_rating');
function ch_awesomeness_rating($form) {
// set up one array for each step of the form
// each array contains the input IDs of the fields we want to sum on each page
// IDs do not need to be consecutive using this method
$step_groups = array(
38 => array( 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 ) ,
39 => array( 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 ) ,
40 => array( 27 , 28 , 29 , 20 , 31 , 32 , 33 , 34 , 35 , 36 )
);
$score_total_key = 41;
// loop through inputs for each step individually
foreach( $step_groups as $subtotal_key => $field_keys ){
foreach( $field_keys as $idx => $field_key ){
$_POST['input_'.$subtotal_key] = ( !$idx ? 0 : $_POST['input_'.$subtotal_key] ) + rgpost( 'input_'.$field_key );
}
}
$_POST['input_'.$score_total_key] = 0;
foreach( $step_groups as $subtotal_key => $v ){
$_POST['input_'.$score_total_key] += $_POST['input_'.$subtotal_key];
}
// be sure to return the form when we're done
return $form;
}
// http://www.gravityhelp.com/forums/topic/simple-calculations
// change the 41 here to your form ID
add_filter('gform_confirmation_41', 'ch_courage_confirmation', 10, 4);
function ch_courage_confirmation($confirmation, $form, $lead, $ajax) {
// beginning of the confirmation message
$confirmation = "<a name='gf_41' class='gform_anchor' ></a><div id='gforms_confirmation_message' class='gform_confirmation_message_41' style='text-align:left;'>";
// set the "lowest score" message as a default and change it if a higher score is achieved
$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.';
// reset the confirmation message based on the overall score, checking for lowest scores first
// this will bump the message up if a higher overall score is found
// this could have been done with an if, else if statement as well
if($lead[41] > 75)
$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?';
if($lead[41] > 150)
$grading = 'Your organization is well on the way to being courageous! You can freely concentrate on the areas that scored lower than others.';
if($lead[41] > 225)
$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.';
// append this conditional information to the confirmation text entered in the form builder
$confirmation .= "<strong>Overall score: " . $lead[41] .".</strong> ". $grading;
// display the subtotal score on the confirmation page as well
// 38 is courage, 39 is process, 40 is behavior
$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>';
// return the confirmation
return $confirmation;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment