LLMS_Quiz_Attempt::answer_question( int $question_id, string[] $answer )

Answer a question


Description Description

Records the selected option and whether or not the selected option was the correct option.

Automatically updates & saves the attempt to the database


Top ↑

Parameters Parameters

$question_id

(int) (Required) WP_Post ID of the LLMS_Question.

$answer

(string[]) (Required) Array of selected choice IDs (for core question types) or an array containing the user-submitted answer(s).


Top ↑

Return Return

(LLMS_Quiz_Attempt) Instance of the current attempt.


Top ↑

Source Source

File: includes/models/model.llms.quiz.attempt.php

	public function answer_question( $question_id, $answer ) {

		$questions = $this->get_questions();

		foreach ( $questions as $key => $data ) {

			if ( absint( $question_id ) !== absint( $data['id'] ) ) {
				continue;
			}

			$question                     = llms_get_post( $question_id );
			$graded                       = $question->grade( $answer );
			$questions[ $key ]['answer']  = $answer;
			$questions[ $key ]['correct'] = $graded;
			$questions[ $key ]['earned']  = llms_parse_bool( $graded ) ? $questions[ $key ]['points'] : 0;

			break;
		}

		$this->set_questions( $questions )->save();

		return $this;

	}


Top ↑

Changelog Changelog

Changelog
Version Description
4.0.0 Explicitly set earned points to 0 when answering incorrectly. Exit the loop as soon as we find our question. Use strict comparison for IDs.
3.9.0
3.16.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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