• Resolved matias1234

    (@matias1234)


    Hi! Amazing plugin! I couldn’t find any answer to the following anywhere:

    Is there any way to show a final message in Knowledge Quizzes based on the user’s result?
    If it’s not, do any workarounds come to mind? I’d like users to see they got Level 1 if they scored from 0 to 5 out of 20; Level 2 if they scored from 6 to 10 out of 20 and so on.

    Also, is there any way to remove the Try it Again button?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @matias1234

    I hope you are doing well.

    Is there any way to show a final message in Knowledge Quizzes based on the user’s result?

    I am afraid it is not possible by default but can you try the solution shared on this URL? https://wpmudev.com/forums/topic/calculate-quiz-percentage-and-display-result-based-on-that/#post-3770227

    Also, is there any way to remove the Try it Again button?

    Can you please test this CSS on Forminator > Appearance > Custom CSS?

    .forminator-result--retake{
        display: none !important;
    }

    Best Regards
    Patrick Freitas

    Thread Starter matias1234

    (@matias1234)

    Thanks very much for the response. Regardin the first thing, I used the solution on that URL, activated the mu-plugin but now all responses return ‘fail’.
    This is the code I used

    
    <?php
    /**
    * Plugin Name: [Forminator Pro] - different quiz response based on score
    * Plugin URI: https://wpmudev.com/
    * Description:
    * Author: Maciej Palmowski @ WPMUDEV
    * Author URI: https://wpmudev.com/
    * Task: SLS-110
    * License: GPLv2 or later
    */
    
    add_filter(
    'forminator_quizzes_render_knowledge_result',
    function( $knowledge_result_html, $text, $model ) {
    $pattern = '/([0-9]+)(\/)([0-9]+)/';
    preg_match( $pattern, $text, $matches );
    
    $correct = (int) $matches[1];
    $all = (int) $matches[3];
    
    $percentage = $correct / $all * 100;
    
    $ret = '<div role="alert" class="forminator-quiz--summary">';
    if ( $percentage >= 80 ) {
    $ret .= 'success';
    } else {
    $ret .= 'fail';
    }
    $ret .= '</div>';
    
    return $ret;
    },
    10,
    3
    );

    I built a sample quiz at https://www.whitecastle.com.ar/booking-test-2 with 10 questions: C are correct and I are incorrect. All 10 combinations (1 correct 9 incorrect; 2 correct 8 incorrect; etc. return ‘fail’). The plugin is installed now.
    Should I change anything in the plugin?

    Thanks a lot in advance.

    • This reply was modified 3 years, 4 months ago by matias1234.
    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @matias1234

    I hope you are doing well.

    The code is only an example, you will need to extend it and replace the text using your custom text.

    For example, 80% correct = level 3:

    if ( $percentage >= 80 ) {
    $ret .= 'Level 3';
    }

    For multiple conditionals, you can use else if:
    https://www.php.net/manual/en/control-structures.elseif.php

    Let us know if you have any additional questions.
    Best Regards
    Patrick Freitas

    Thread Starter matias1234

    (@matias1234)

    Hi @wpmudevsupport12 ! Thanks very much for the reply and help!
    Yes, I know it’s just an example. I meant that it’s not working for me. That is, with the above code, all the possible combinations (all answers wrong, all answers right, even below 80% return a ‘success’ message).
    I have tried testing it with a sample quiz of 10 questions (https://www.whitecastle.com.ar/booking-test-2) and all possible combinations return ‘success’.
    Thanks a lot in advance again!

    Thread Starter matias1234

    (@matias1234)

    OK, I solved it! It seems in the Final Count Message I had deleted the %YourNum%/%Total% and that had to be there to parse the correct and total values.
    Just in case anyone needs it:

    /**
    * Plugin Name: [Forminator Pro] - different quiz response based on score
    * Plugin URI: https://wpmudev.com/
    * Description:
    * Author: Maciej Palmowski @ WPMUDEV
    * Author URI: https://wpmudev.com/
    * Task: SLS-110
    * License: GPLv2 or later
    */
    
    add_filter(
    'forminator_quizzes_render_knowledge_result',
    function( $knowledge_result_html, $text, $model ) {
    $pattern = '/([0-9]+)(\/)([0-9]+)/';
    preg_match( $pattern, $text, $matches );
    
    $correct = (int) $matches[1];
    $all = (int) $matches[3];
    
    $percentage = $correct / $all * 100;
    
    $ret = '<div role="alert" class="forminator-quiz--summary">';
     if ( $percentage >= 80 ) {
    $ret .= 'A';
    } elseif ( $percentage >= 70 ) {
    $ret .= 'B';
     } elseif ( $percentage >= 60 ) {
    $ret .= 'C';
     } elseif ( $percentage >= 40 ) {
    $ret .= 'D';
     }  elseif ( $percentage >= 20 ) {
    $ret .= 'E';
     }  elseif ( $percentage >= 10 ) {
    $ret .= 'F';
     }  else {
    $ret .= 'G';
    }
    $ret .= '</div>';
    
    return $ret;
    },
    10,
    3
    );

    So thanks very much for your help indeed!

    • This reply was modified 3 years, 4 months ago by matias1234.
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Final Count Message in Quiz based on quiz score’ is closed to new replies.