• Resolved tonytool

    (@tonytool)


    Hi Dylan,

    hope you can give me a hint on this:
    In order to give difficult questions more “weight” when calculating the total score I like to add a weighing factor.

    I added a custom tab called “weighing factor” (German: “Wichtung”) to the question using the code you provided in the forum. Now here I can enter a number in the text field
    $field[“name”] = “weight”;

    The idea is: Currently each correct question is counted as 1 point, each wrong question gives 0 points. So, only correct answers need to be added to give the total score.

    How can I multiply this factor with each correct question? How and where do I read the variable into the js script where each question score is being calculated? I guess it needs to be something like “fieldInput” or so…

    I get it that’s not super easy (well, for me at least) because the weight belongs to an individual question, so it needs to be combined with the question ID.

    I was hoping you can help a little in case it’s not super difficult for you… 🙂

    Maybe you like the idea anyway and like to add the option to a future upgrade…

    Thanks and regards,
    Heiko

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Harmonic Design

    (@harmonic_design)

    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!

    Thread Starter tonytool

    (@tonytool)

    Hi Dylan,
    thank you so much for your support!
    I got back to you by e-mail.

    Regards,
    Heiko

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

The topic ‘Question weighing factor’ is closed to new replies.