learndash_ques_multiple_answer_pts_each

apply_filters( 'learndash_ques_multiple_answer_pts_each',  int $point,  int|string $answer_index,  array $question_data,  mixed $correct_answer,  array $user_response )

Filters the points of each answer for multiple answer type question.


Description #


Parameters #

$point

(int) Points for the question.

$answer_index

(int|string) Index of the answer.

$question_data

(array) An array of question data.

$correct_answer

(mixed) Correct answer for the question.

$user_response

(array) An array of user response data.


Source #

File: includes/quiz/ld-quiz-pro.php


Examples #

 <?php
/**
 * Example usage for learndash_ques_multiple_answer_pts_each filter.
 */
add_filter(
	'learndash_ques_multiple_answer_pts_each',
	function( $points, $questionData, $answerIndex, $correctAnswer, $userResponse ) {
		// If the answer item is FALSE (not correct) but the student provided an answer. Then deduct the points.
		if ( ( $questionData['correct'][ $answerIndex ] == false ) && ( ! empty( $userResponse[ $answerIndex ] ) ) ) {
			if ( intval( $questionData['points'][ $answerIndex ] ) > 0 ) {
				$points -= intval( $questionData['points'][ $answerIndex ] );
			} else {
				$points -= 1;
			}
		}

		// Keep the points zero or greater.
		// First we get the key of the last answer items
		end( $questionData['correct'] );
		$last_key = key( $questionData['correct'] );

		// If we are at the last index and the points is less than zero we keep it from being negative.
		if ( ( $last_key == $answerIndex ) && ( $points < 0 ) ) {
			$points = 0;
		}

		// Always return $points
		return $points;
	},
	10,
	5
);

add_filter( 'learndash_ques_multiple_answer_pts_each', 'learndash_ques_multiple_answer_pts_each', 10, 5 );

function learndash_ques_multiple_answer_pts_each( $points, $questionData, $answerIndex, $correctAnswer, $userResponse ) {
	if ( empty( $correctAnswer ) && empty( $userResponse[ $answerIndex ] ) ) {
		$r[ $answerIndex ] = true;
		$points           += $questionData['points'][ $answerIndex ];
	}

	return $points;
}