Advertisement
Guest User

gravity forms quiz

a guest
Jul 14th, 2011
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.96 KB | None | 0 0
  1. // INSTRUCTIONS
  2. // ensure you have two hidden fields labeled 'quiz' and 'correct', they can be placed anywhere in the form
  3. // in each quiz question check 'enable values' and set the value of the correct answer to 1, others set to 0. don't use other point systems
  4.  
  5. // processes a quiz and total up the correct answers
  6. function sb_quiz_process($form){
  7.     if ( ! sb_is_quiz( $form ) ) return;   
  8.    
  9.     $total_value = 0; $questions_num = 0;
  10.     foreach($form['fields'] as $field){
  11.         //if ( strtolower($field['label']) == 'quiz') $is_quiz = true;
  12.         if ( strtolower($field['label']) == 'correct') $total_field_id = $field['id'];
  13.         $value = $_POST['input_' . $field['id']];
  14.         if ( $value === '1' || $value === '0' ) {
  15.             $total_value = $total_value + $value;
  16.             $questions_num++;
  17.         }
  18.     }
  19.     if ( $total_field_id && $questions_num ) {
  20.         $percent_correct = $total_value / $questions_num; // assuming each correct value is 1 and not more!
  21.         $_POST['input_' . $total_field_id] = sprintf("%.2f", $percent_correct );
  22.     }
  23. }
  24. add_action("gform_pre_submission", "sb_quiz_process");
  25.  
  26.  
  27. //create our own confirmation
  28. function sb_quiz_submission( $lead, $form ) {
  29.  
  30.     if ( ! sb_is_quiz( $form ) ) return;   
  31.    
  32.     echo '<div id="gforms_confirmation_message">';
  33.     echo '<p>Thanks for taking the quiz! Your results are stored and you can always check your status to see what quizzes you have finished and which still need to be done.</p>';
  34.  
  35.     foreach($form["fields"] as $field){
  36.         $field_label = esc_html(GFCommon::get_label($field));
  37.         if ( strtolower($field['label']) == 'quiz') continue;
  38.         $field_value = RGFormsModel::get_lead_field_value($lead, $field);
  39.         $field_value_display = GFCommon::get_lead_field_display($field, $field_value, false, true);
  40.        
  41.         if ( strtolower($field['label']) == 'correct' )  {
  42.              echo "<p class='sb_quiz_total'><strong>Your Score: ". $field_value * 100 ."%</strong>";
  43.              unset($_POST['input_'.$field['id']]);
  44.         } elseif ( is_numeric($field_value) ) {
  45.             echo "<p class='sb_quiz_result' style='text-align:left;margin-left: 20px; '><strong>$field_label</strong><br>";
  46.             if ( $field_value === '1' ){
  47.                 echo "<span style='color:green'>CORRECT - $field_value_display</span>";
  48.             } else {
  49.                 echo "<span style='color:red'>INCORRECT - try again</span>";
  50.                 unset($_POST['input_'.$field['id']]);
  51.             }
  52.             echo "</p>";
  53.         }
  54.     }
  55.  
  56.     echo '</div>';
  57. }
  58. add_action( 'gform_post_submission', 'sb_quiz_submission', 10, 2 );
  59.  
  60.  
  61.  
  62. //disable default confirmation
  63. function sb_quiz_confirmation( $confirmation, $form, $lead, $ajax ) {
  64.     if ( sb_is_quiz( $form ) )
  65.         return '';
  66.     else
  67.         return $confirmation;
  68. }
  69. add_filter( 'gform_confirmation', 'sb_quiz_confirmation', 10, 4 );
  70.  
  71.  
  72. // is this form a quiz?
  73. function sb_is_quiz( $form ) {
  74.     foreach($form['fields'] as $field){
  75.         if ( strtolower($field['label'] ) == 'quiz' )
  76.             $is_quiz = true;
  77.     }
  78.     return $is_quiz;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement