LLMS_Question::grade( array[] $answer )

Attempt to grade a question


Parameters Parameters

$answer

(array[]) (Required) Selected answer(s).


Top ↑

Return Return

(string|null) Returns null if the question cannot be automatically graded. Returns yes for correct answers and no for incorrect answers.


Top ↑

Source Source

File: includes/models/model.llms.question.php

	 * @param array[] $answer Selected answer(s).
	 * @return string|null Returns `null` if the question cannot be automatically graded.
	 *                     Returns `yes` for correct answers and `no` for incorrect answers.
	 */
	public function grade( $answer ) {

		$question_type = $this->get( 'question_type' );

		/**
		 * Use this filter to bypass core grading for a given question type.
		 *
		 * If the filter returns a non-null value core grading is bypassed.
		 *
		 * The dynamic portion of this hook, `$question_type`, refers to the type of question being graded.
		 *
		 * @since 3.16.0
		 *
		 * @param null|string   $grade    Defaults to `null` which signifies that LifterLMS should attempt to grade the answer.
		 *                                Return `yes` (correct) or `no` (incorrect) to bypass core grading methods.
		 * @param string[]      $answer   User-submitted answers.
		 * @param LLMS_Question $question Question object.
		 */
		$grade = apply_filters( "llms_{$question_type}_question_pre_grade", null, $answer, $this );

		if ( is_null( $grade ) && $this->get( 'points' ) >= 1 ) {

			$grading_type = $this->get_auto_grade_type();

			if ( 'choices' === $grading_type ) {

				sort( $answer );
				$grade = ( $answer === $this->get_correct_choice() ) ? 'yes' : 'no';

			} elseif ( 'conditional' === $grading_type ) {

				$correct = $this->get_conditional_correct_value();

				/**
				 * Filter whether or not conditionally graded question answers are treated as a case-sensitive
				 *
				 * By default, case sensitivity is disabled.
				 *
				 * @since 3.16.15
				 *
				 * @param boolean       $case_sensitive Whether or not answers are treated as case-sensitive.
				 * @param string[]      $answer         User-submitted answers.
				 * @param string[]      $correct        Correct answers.
				 * @param LLMS_Question $question       Question object.
				 */
				if ( false === apply_filters( 'llms_quiz_grading_case_sensitive', false, $answer, $correct, $this ) ) {

					$answer  = array_map( 'strtolower', $answer );
					$correct = array_map( 'strtolower', $correct );

				}

				$grade = ( $answer === $correct ) ? 'yes' : 'no';

			}
		}

		/**
		 * Filter the grading result of an answer for a given question type.
		 *
		 * The dynamic portion of this hook, `$question_type`, refers to the type of question being graded.
		 *
		 * @since 3.16.0
		 *
		 * @param null|string   $grade    Defaults to `null` which signifies that LifterLMS should attempt to grade the answer.
		 *                                Return `yes` (correct) or `no` (incorrect) to bypass core grading methods.
		 * @param string[]      $answer   User-submitted answers.
		 * @param LLMS_Question $question Question object.


Top ↑

Changelog Changelog

Changelog
Version Description
4.4.0 Combined nested if statements into a single condition.
3.16.15 Unknown.
3.16.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.