[Forminator] Customization help needed please

I have 2 customization questions I’d like some help with if possible:

1) Is there any way to show a percentage of who got the quiz correct after they answer. Example: “56% answered today’s question correctly.”?

2) I need a counter to show how many Total times people have taken the quiz; is there an easy way to do this? For example, here is the quiz: http://dev-+++++++++.pantheonsite.io/ – and I need that “36” to go up every time the quiz is taken. Is this possible with forminator?

  • Ash
    • WordPress Hacker

    Hello Aly

    This could be quite complex. I have talked to our SLS team and they wanted to take a look.

    We have sent your issue to our second level support team. If this is possible within our support scope, they will definitely help. But if it needs too much custom code, you might need to end up hiring a developer. Please note that, they deal with more complex issues, so there might be little delay. We appreciate your patience on this.

    Have a nice day!

    Cheers,

    Ash

  • Konstantinos Xenos
    • Rubber Duck Debugger

    Hey Aly ,

    I've created 2 shortcodes to help you with this. One of them will output the "total" submissions of the quiz and the other will calculate the correct answers.

    You will be able to use them anywhere within your WP by adding these shortcodes:

    [quiz_show_success_percentage id="88"]
    [quiz_count_entries id="88"]

    Simply change the "88" with the Quiz ID that you want to count :slight_smile: .

    How to install:

    Always make sure to keep a backup of your site before changing/adding custom code.

    1] Navigate to your /wp-content/ directory and create a new one named mu-plugins if it doesn't exist.

    2] Inside the mu-plugins folder create a file named frmt-quiz-shortcodes.php

    3] Edit the file and copy / paste this code snippet inside.

    <?php

    // Adds the [quiz_count_entries id="#"] Shortcode
    add_shortcode(
    'quiz_count_entries',
    function( $atts ) {

    $atts = shortcode_atts(
    array(
    'id' => '',
    ),
    $atts
    );

    if ( ! empty( $atts['id'] ) ) {
    $entries = Forminator_API::get_entries( intval( $atts['id'] ) );
    $count = count( $entries );

    // Edit this to change the output message.
    $message = 'Total entries count: ' . $count;
    } else {
    // Edit this to change the output message.
    $message = 'Please enter an ID.';
    }

    return $message;

    }
    );

    // Adds the [quiz_show_success_percentage id="#"] Shortcode
    add_shortcode(
    'quiz_show_success_percentage',
    function( $atts ) {

    $atts = shortcode_atts(
    array(
    'id' => '',
    ),
    $atts
    );

    $total = 0;
    $correct = 0;

    if ( ! empty( $atts['id'] ) ) {
    $entries = Forminator_API::get_entries( intval( $atts['id'] ) );

    // Edit this to change the output message.
    foreach ( $entries as $entry ) {
    foreach ( $entry->meta_data['entry']['value'] as $val ) {
    $total++;

    if ( $val['isCorrect'] ) {
    $correct++;
    }
    }
    }

    if ( $total > 0 ) {
    $percent = round( $correct / ( $total / 100 ), 2 );
    } else {
    $percent = 0;
    }

    // Edit this to change the output message.
    $message = 'Percentage of correct answers: ' . $percent . '% ( Total: ' . $total . ', Correct: ' . $correct . ')';
    } else {
    // Edit this to change the output message.
    $message = 'Please enter an ID.';
    }

    return $message;

    }
    );

    4] Save and close the file.

    5] The final path should look like /wp-content/mu-plugins/frmt-quiz-shortcodes.php

    You can edit the code given to change the output messages or stylize them as well.

    As an example if you just use the code as it is you can do something like this:

    I hope this helps!

    Regards,

    Konstantinos

  • Konstantinos Xenos
    • Rubber Duck Debugger

    Hey Aly ,

    Yes the percentage takes account “all” of the submissions & answers and calculates the correct ones :slight_smile:, so it’s not a user specific percentage.

    I’m not really sure about the “that day” though, the code above will calculate everything for the Quiz that you input it’s ID. What does this mean:

    If “each day” you create a new quiz so the ID is different then your statistics will be for “that quiz only” hence “that day”. If you just change the questions / answers on the same quiz though it’s ID won’t change so the code will continue reading “all of the submissions” that have ever been done into this Quiz. Does this make sense? I’m just trying to make this clear to avoid any confusion :slight_smile: .

    In essence let’s leave aside the “day” to not get confused, the code is only interested for a unique Quiz ID and the submissions that have been made to that Quiz :slight_smile: .

    Tell me if you need any further clarifications or updates so I can help you out as much as possible.

    Regards,

    Konstantinos

    • Aly
      • WPMU DEV Initiate

      Thanks Konstantinos! That is very helpful. We were also wondering if there is a way to refresh the page completely when someone answers the quiz. Right now it redirects to /entries/104/ but the actual page doesn’t seem to refresh. Is there a way we can make this happen? Thanks!

  • Konstantinos Xenos
    • Rubber Duck Debugger

    Hey Aly ,

    Unfortunately a “refresh” isn’t viable as the url changes for Forminator to do it’s queries and calculate the results :slight_smile: . If we do an actual refresh then that page will be “broken”.

    But ! We can adjust our code a little bit to add these custom shortoce only into the actual “results” so the /entries/104/ page for example.

    To do that you can update the code to this:

    <?php

    // filter the Quiz result to add the custom shortcodes
    add_filter(
    'forminator_quizzes_render_knowledge_result',
    function( $html ) {
    $new_html = $html . '<p>' . do_shortcode( '[quiz_count_entries id="88"]' ) . '</p><p>' . do_shortcode( '[quiz_show_success_percentage id="88"]' ) . '</p>';

    return $new_html;
    }
    );

    // Adds the [quiz_count_entries id="#"] Shortcode
    add_shortcode(
    'quiz_count_entries',
    function( $atts ) {

    $atts = shortcode_atts(
    array(
    'id' => '',
    ),
    $atts
    );

    if ( ! empty( $atts['id'] ) ) {
    $entries = Forminator_API::get_entries( intval( $atts['id'] ) );
    $count = count( $entries );

    // Edit this to change the output message.
    $message = 'Total entries count: ' . $count;
    } else {
    // Edit this to change the output message.
    $message = 'Please enter an ID.';
    }

    return $message;

    }
    );

    // Adds the [quiz_show_success_percentage id="#"] Shortcode
    add_shortcode(
    'quiz_show_success_percentage',
    function( $atts ) {

    $atts = shortcode_atts(
    array(
    'id' => '',
    ),
    $atts
    );

    $total = 0;
    $correct = 0;

    if ( ! empty( $atts['id'] ) ) {
    $entries = Forminator_API::get_entries( intval( $atts['id'] ) );

    // Edit this to change the output message.
    foreach ( $entries as $entry ) {
    foreach ( $entry->meta_data['entry']['value'] as $val ) {
    $total++;

    if ( $val['isCorrect'] ) {
    $correct++;
    }
    }
    }

    if ( $total > 0 ) {
    $percent = round( $correct / ( $total / 100 ), 2 );
    } else {
    $percent = 0;
    }

    // Edit this to change the output message.
    $message = 'Percentage of correct answers: ' . $percent . '% ( Total: ' . $total . ', Correct: ' . $correct . ')';
    } else {
    // Edit this to change the output message.
    $message = 'Please enter an ID.';
    }

    return $message;

    }
    );

    As you see I added an extra “filter” at the top to add the 2 shortcodes on the html produced as a result.

    This means that the shortcodes will show up after this message:

    This is my result page as an example ( note that I have actually removed the shortcodes from the page itself and I’m just using the extra filter from the code on this screenshot ):

    I hope this helps!

    Regards,

    Konstantinos

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.