• Resolved mubasher99

    (@mubasher99)


    Hello The current result display is like: 2 / 5 – 40%

    Is there anyway that I can show it like:
    correct Answers: 2
    Incorrect Answers: 2
    Unanswered Questions: 1
    Percentage: 40%

    PS. Around 5 years ago i had designed a site for my client and had asked the same question from you. you developed the following code to put in functions.php. I had saved that with me. But somehow this code is not working..! it is not displaying the result i want. instead it is showing the same result as (2 / 5 – 40%). Code was:


    function hdq_before_mubasher99($quizID)
    {
    ?>
    <script>
    function hdq_custom_submit(){
    let data = {};
    let no_answers = 0;

    // mark each question with string on whether answer was right, wrong, or unanswered
    jQuery("#hdq_<?php echo $quizID; ?> .hdq_question").each(function(){
    let data = "";
    if(!this.classList.contains("hdq_question_title") && !this.classList.contains("hdq_results_wrapper")){
    let sel = jQuery(this).find(".hdq_correct");
    if(sel.length > 0){
    data = "Correct Answers";
    } else {
    sel = jQuery(this).find(".hdq_wrong");
    if(sel.length > 0){
    data = "Incorrect Answers";
    } else {
    data = "No Answers Selected";
    no_answers ++;
    }
    }
    }

    jQuery("<div class='online-result-status'><h1>" + data +"</h1></div>").prependTo(this);
    });

    // hdq_score is array of score (score/total questions)
    let c = hdq_score[0];
    let t = hdq_score[1];
    let q = parseFloat(t)-parseFloat(c)-no_answers;
    q = q.toFixed(0);
    let p = (parseFloat(c) / parseFloat(t)) * 100;
    p = p.toFixed(2);
    let d =
    <br> <br> <br> <p class="Online-Test-result-sheet"><br> <br> <br> Correct Answers: <strong>${c}</strong><br/><br> Incorrect Answers: <strong>${q}</strong><br/><br> Unanswered Questions: <strong>${no_answers}</strong><br/><br> Percentage: <strong>${p}%</strong><br> </p><br>;
    let results_section = document.querySelectorAll(".hdq_result_fail")[0];
    results_section.insertAdjacentHTML("beforebegin", d);
    return JSON.stringify(data); // expects a json string to be returned
    }
    </script>
    <?php
    }
    add_action('hdq_before', 'hdq_before_mubasher99');

    function hdq_submit_mubasher99($quizOptions)
    {
    array_push($quizOptions->hdq_submit, "hdq_custom_submit");
    return $quizOptions;
    }
    add_action('hdq_submit', 'hdq_submit_mubasher99');


Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Dylan – Harmonic Design

    (@dylanharmonicdesign)

    Hi mubasher99,
    as you can imagine, HD Quiz has changed a lot in the past 5 years, including a major rewrite 7 months ago. The following code should replace everything I previously gave you.

    NOTES:

    • Requires you to have full marking enabled (“Show correct answers on completion”).
    • Not compatible with all question types (Example: Text based answers will always show as being answered since marking fills in the values automatically)
    • Will not work with an upcoming new feature for Weighted Answers.
    • Feel free to edit the STEP 3. Print the result onto the page part of the code to modify the HTML output however you feel works best for you.
    • Based on the code you referenced, it looks like you only wanted this data to print if the user does not pass the quiz?
    function hdq_before_mubasher99($quizID)
    {
    ?>
    <script>
    function hdq_custom_submit() {
    // STEP 1. Figure out how many questions were not answered
    let unanswered = 0;
    const questions = document.getElementsByClassName("hdq_question");
    for (let i = 0; i < questions.length; i++) {
    if (parseInt(questions[i].getAttribute("data-weight")) == 0) {
    continue; // make sure this is a markable question
    }

    const answeredWrong = questions[i].getElementsByClassName("hdq_wrong");
    const answeredCorrect = questions[i].getElementsByClassName("hdq_correct");
    if (answeredWrong.length === 0) {
    if (answeredCorrect.length === 0) {
    unanswered = unanswered + 1;
    }
    }
    }

    // STEP 2. Calculate each paremeter
    const correct = HDQ.VARS.hdq_score[0]; // answers that were correct
    const incorrect = HDQ.VARS.hdq_score[1] - HDQ.VARS.hdq_score[0]; // total questions - correct answers
    let p = (parseFloat(HDQ.VARS.hdq_score[0]) / parseFloat(HDQ.VARS.hdq_score[1])) * 100;
    p = p.toFixed(2);
    console.log("Correct: " + correct)
    console.log("Incorrect:" + incorrect)
    console.log("Unanswered: " + unanswered)

    // STEP 3. Print the result onto the page
    const html =
    <p class="Online-Test-result-sheet"><br> Correct Answers: <strong>${correct}</strong><br/><br> Incorrect Answers: <strong>${incorrect}</strong><br/><br> Unanswered Questions: <strong>${unanswered}</strong><br/><br> Percentage: <strong>${p}%</strong><br></p>;
    let results_section = document.getElementsByClassName("hdq_result_fail")[0];
    results_section.insertAdjacentHTML("beforebegin", html);

    return JSON.stringify('{}'); // expects a JSON string to be returned
    }
    </script>
    <?php
    }
    add_action('hdq_before', 'hdq_before_mubasher99');

    function hdq_submit_mubasher99($quizOptions)
    {
    array_push($quizOptions->hdq_submit, "hdq_custom_submit");
    return $quizOptions;
    }
    add_action('hdq_submit', 'hdq_submit_mubasher99');

    Thread Starter mubasher99

    (@mubasher99)

    Thank You so much. It is working as i needed.
    You are indeed a great developer owning a great plugin.. <3

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Coding to Display Result’ is closed to new replies.