LLMS_Quiz_Attempt::answer_question( int $question_id, int $answer )

Answer a question records the selected option and whether or not the selected option was the correct option Automatically updates & saves the attempt to the database


Description Description


Parameters Parameters

$question_id

(int) (Required) WP_Post ID of the LLMS_Question

$answer

(int) (Required) index/key of the selected answer option as found in the array of options retrieved by LLMS_Question->get_options()


Top ↑

Return Return

($this)


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 ( $question_id != $data['id'] ) {
				continue;
			}
			$question = llms_get_post( $question_id );
			$graded = $question->grade( $answer );
			$questions[ $key ]['answer'] = $answer;
			$questions[ $key ]['correct'] = $graded;
			if ( llms_parse_bool( $graded ) ) {
				$questions[ $key ]['earned'] = $questions[ $key ]['points'];
			}
			break;
		}

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

		return $this;

	}

Top ↑

Changelog Changelog

Changelog
Version Description
3.9.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

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





Permalink: