View difference between Paste ID: ruVWvET8 and WKKND4Yc
SHOW: | | - or go back to the newest paste.
1
// QUIZ
2
3
// INSTRUCTIONS
4
// ensure you have two hidden fields labeled 'quiz' and 'correct', they can be placed anywhere in the form
5
// 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
6
7
// processes a quiz and total up the correct answers
8
function sb_quiz_process($form){
9
	if ( ! sb_is_quiz( $form ) ) return;	
10
	
11
	$total_value = 0; $questions_num = 0;
12
	foreach($form['fields'] as $field){
13
		//if ( strtolower($field['label']) == 'quiz') $is_quiz = true;
14
		if ( strtolower($field['label']) == 'correct') $total_field_id = $field['id'];
15
		$value = $_POST['input_' . $field['id']];
16
		if ( $value === '1' || $value === '0' ) {
17
			$total_value = $total_value + $value;
18
			$questions_num++;
19
		}
20
	}
21
	if ( $total_field_id && $questions_num ) {
22
		$percent_correct = $total_value / $questions_num; // assuming each correct value is 1 and not more! 
23
		$_POST['input_' . $total_field_id] = sprintf("%.2f", $percent_correct );
24
	}
25
}
26
add_action("gform_pre_submission", "sb_quiz_process");
27
28
29
//create our own confirmation
30
function sb_quiz_submission( $lead, $form ) {
31
32
	if ( ! sb_is_quiz( $form ) ) return;	
33
	
34
	//echo GFCommon::get_submitted_fields($form, $lead, false, true);
35
	echo '<div id="gforms_confirmation_message">';
36
	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>';
37
38
    foreach($form["fields"] as $field){
39
        $field_label = esc_html(GFCommon::get_label($field));
40
        if ( strtolower($field['label']) == 'quiz') continue;
41
        $field_value = RGFormsModel::get_lead_field_value($lead, $field);
42
        $field_value_display = GFCommon::get_lead_field_display($field, $field_value, false, true);
43
        
44
        if ( strtolower($field['label']) == 'correct' )  {
45
        	 echo "<p class='sb_quiz_total'><strong>Your Score: ". $field_value * 100 ."%</strong>";
46
        	 unset($_POST['input_'.$field['id']]);
47
        } elseif ( is_numeric($field_value) ) {
48
	        echo "<p class='sb_quiz_result' style='text-align:left;margin-left: 20px; '><strong>$field_label</strong><br>";
49
	        if ( $field_value === '1' ){
50
	        	echo "<span style='color:green'>CORRECT - $field_value_display</span>";
51
	        } else {
52
	        	echo "<span style='color:red'>INCORRECT - try again</span>";
53
	        	unset($_POST['input_'.$field['id']]);
54
	        }
55
	        echo "</p>";
56
        }
57
    }
58
59
    echo '</div>';
60
}
61
add_action( 'gform_post_submission', 'sb_quiz_submission', 10, 2 );
62
63
64
65
//disable default confirmation
66
function sb_quiz_confirmation( $confirmation, $form, $lead, $ajax ) {
67
	if ( sb_is_quiz( $form ) )
68
		return '';
69
	else
70
		return $confirmation;
71
}
72
add_filter( 'gform_confirmation', 'sb_quiz_confirmation', 10, 4 );
73
74
75
// is this form a quiz?
76
function sb_is_quiz( $form ) {
77
	foreach($form['fields'] as $field){
78
		if ( strtolower($field['label'] ) == 'quiz' ) 
79-
}
79+
80
	}
81
	return $is_quiz;
82
}
83
84