Hi Tony,
first, let’s just rip the bandaid off. What you are asking for is hard to do. There is a lot of things you’ll need to do in order to modify HD Quiz this way.
First, as you’ve already done, is you need to extend questions to allow you to enter in a custom value for the “weight” of the question. I’m glad to see that you’ve already done this 🙂
Next, we need to actually use this value somewhere. I’d recommend using the hdq_before action. The function you pass will contain the quiz ID. You can then use this ID to loop through all of the questions in the quiz to extract your custom data which can then be localized. Unfortunately, this is not the most performant method, but this will ensure the best compatibility.
EXAMPLE:
function hdq_tonytool_custom_meta_loop($quiz_ID){
// NOTE: you will need to extend this if using WP pagination
$args = array(
'post_type' => array('post_type_questionna'),
'tax_query' => array(
array(
'taxonomy' => 'quiz',
'terms' => $quiz_ID,
),
)
);
$query = new WP_Query($args);
$hdq_question_weight = array(); // array to store the data
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$question_ID = get_the_ID();
$question = get_hdq_question($question_ID);
$weight = 1; // default weight to 1
if(isset($question["weight"])){
// make sure that you have a weight value entered for this question
if($question["weight"]["value"][0] != ""){
$weight = intval($question["weight"]["value"][0]);
}
}
array_push(
$hdq_question_weight,
array(
"question_ID" => $question_ID,
"weight" => $weight
)
);
}
}
wp_localize_script('hdq_admin_script', 'hdq_question_weight', $hdq_question_weight);
}
add_action('hdq_before', "hdq_tonytool_custom_meta_loop");
Ok, so we’ve now have access to what the “weight” of each question should be worth. But this has been the easy part.
The way that HD Quiz calculates the score is easy. The formula is correct questions / total questions. But you need to replace both of those to something much more complicated like sum of correct total questions weight / sum of all questions weight. So now it’s best that you hook into the quiz submit event and override to calculate everything yourself. The above code will place an array of objects {question_ID: 0, weight: 1} which you can use to lookup how much to increase the score by. You can also loop through it to add all of the “weights” up to get what the “sum of all questions weight” amount is.
The above should be more than enough to get you started, so good luck!
Hi Dylan,
thank you so much for your support!
I got back to you by e-mail.
Regards,
Heiko