Get all questions right before redirect
Building a new site for someone and they want a training section where the user watches a video and then has to answer 3 true or false questions and then get redirected to next page and video. I installed the program and does everything I need but forwards regardless on how many they got right. The customer wants them to have 100% correct before going to next page. Did I miss something or is this not possible with the program.
Hi Rich,
the feature states “If you want to automatically redirect to another page on quiz completion.”, so the redirect happens no matter the score.
However, HD Quiz is very extendable, and I have actions and hooks that allow you to modify just about all aspects of the system to customize it however you need.
If you add the following code to your theme’s functions.php file (BACKUP THE FILE BEFORE MAKING ANY MODIFICATIONS), a new function will run whenever a quiz completes. This function checks to see what the score is, and if the score is not 100%, then it sets the redirect URL to blank. This way the redirect will only happen if the user gets all questions correct.
function hdq_before_Rich($quizID)
{
?>
<script>
function hdq_custom_submit_Rich() {
// check the score. Only redirect is quiz score is 100%
if (HDQ.VARS.hdq_score[0] != HDQ.VARS.hdq_score[1]) {
HDQ.VARS.quiz.quiz_redirect_url = "";
}
return JSON.stringify('{}'); // expects a JSON string to be returned
}
</script>
<?php
}
add_action('hdq_before', 'hdq_before_Rich');
// Tell HD Quiz to run a custom JS function on quiz completion
function hdq_submit_Rich($quizOptions)
{
array_push($quizOptions->hdq_submit, "hdq_custom_submit_Rich");
return $quizOptions;
}
add_action('hdq_submit', 'hdq_submit_Rich');OK this code works perfectly, but leaves the user at a standstill. Not able to edit or anything or try again. This is not a super serious quiz but we want the user to be engaged and paying attention. Suggestions on how to let them try again?
Thanks for your great support!
Easy!
Reloading page = fresh new start.
function hd_results_after_content($quiz)
{
?>
<p style="text-align: right;"><a href="<?php echo get_the_permalink(); ?>">Retry Quiz</a></p>
<?php
}
add_action("hdq_results_after_content", "hd_results_after_content", 10, 1);This will automatically add custom content to all results. In our case, we are adding a link that simply links back to the same page. You’ll probably want to change the class in the link to whatever your site uses for buttons.
I will try this to see if I like it better. I ended up just adding some text in the quiz fail content with a message that said “Almost, click here to try again” with a link back to the same page which refreshes the quiz.
This thread has either been marked as complete or has been automatically closed due to inactivity. Please consider opening a new support thread for help.