Created
January 27, 2023 11:19
-
-
Save adczk/89b54b64514f61162b730c5e197af8fd to your computer and use it in GitHub Desktop.
Forminator - customize knowledge quiz result message depending on score
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Plugin Name: [Forminator] Set minimum number of words limit for textarea field | |
| * Plugin URI: https://premium.wpmudev.org/ | |
| * Description: Sets minium number of words for textarea field | |
| * Author: adczk | |
| * Author URI: https://premium.wpmudev.org/ | |
| * License: GPLv2 or later | |
| * | |
| * Tested with Forminator 1.22.1 | |
| * | |
| * | |
| * {CUSTOM_RESULT} - use this placeholder in quiz behavior settings inside "Final Count Message" content | |
| * | |
| * | |
| * config explained in code comment | |
| * | |
| */ | |
| add_filter ('forminator_replace_quiz_form_data', 'forminator_custom_replace_quiz_form_data', 11, 3); | |
| function forminator_custom_replace_quiz_form_data( $content, $quiz, $prepared ) { | |
| # CONFIGURATION # | |
| $quiz_ids = array( 2045 ); // list of quiz IDs for this to work with, comma separated if multiple | |
| /* define your results below in form of array where key is score and value is message | |
| that will be used if score is highter than this; | |
| in example below if | |
| result is | |
| >3 it will say "Awesome, X is great result" where X will be actual score | |
| >1 (so 2 and 3 score) it will say "it's fine, X is resanable" | |
| otherwise it will use default message set below | |
| */ | |
| $quiz_messages = array( | |
| '3' => "Awesome, CSR_SCORE is great result!", | |
| '1' => "It's fine, CSR_SCORE out of CSR_ANSWERS is reasonable" | |
| ); | |
| $msg = "You got CSR_SCORE out of CSR_ANSWERS in quiz ID CSR_ID"; | |
| /* note: you can use following "placeholders" that will be automatically replaced | |
| - CSR_SCORE - is a quiz result achieved | |
| - CSR_ANSWERS - is a total number of answers | |
| - CSR_ID = is a quiz ID number | |
| */ | |
| # CONFIGURATIN END # | |
| $quiz_id = $prepared['form_id']; // get quiz ID; | |
| if ( in_array( $quiz_id, $quiz_ids ) ) { | |
| // prepare some data about results | |
| $quiz_answers = $prepared['answers']; | |
| $quiz_answers_count = count($quiz_answers); | |
| $quiz_result = $prepared['final_result']; | |
| // select predefined response if not | |
| ksort( $quiz_messages ); | |
| foreach ( $quiz_messages as $score=>$result_msg ) { | |
| if ( $quiz_result > $score ) $msg = $result_msg; | |
| } | |
| // do replacements | |
| $msg = str_replace( 'CSR_SCORE', $quiz_result, $msg ); | |
| $msg = str_replace( 'CSR_ANSWERS', $quiz_answers_count, $msg ); | |
| $msg = str_replace( 'CSR_ID', $quiz_id, $msg ); | |
| // and finally custom message into final result response | |
| $content = str_replace( "{CUSTOM_RESULT}", $msg, $content ); | |
| } | |
| return $content; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment