LLMS_Quiz_Attempt::init( int $quiz_id, int $lesson_id, mixed $student = null )

Initialize a new quiz attempt by quiz and lesson for a user

Description Description

If no user found throws an Exception.

Top ↑

Parameters Parameters


(int) (Required) WP Post ID of the quiz.


(int) (Required) WP Post ID of the lesson.


(mixed) (Optional) Accepts anything that can be passed to llms_get_student. If no user is passed the current user will be used. Default null.

Default value: null

Top ↑

Return Return


Top ↑

Source Source

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

	public static function init( $quiz_id, $lesson_id, $student = null ) {

		$student = llms_get_student( $student );
		if ( ! $student ) {
			throw new Exception( __( 'You must be logged in to take a quiz!', 'lifterlms' ) );

		// Initialize a new attempt.
		$attempt = new self();
		$attempt->set( 'quiz_id', $quiz_id );
		$attempt->set( 'lesson_id', $lesson_id );
		$attempt->set( 'student_id', $student->get_id() );
		$attempt->set_status( 'incomplete' );
		$attempt->set_questions( $attempt->get_new_questions() );

		$number = 1;

		$last_attempt = $student->quizzes()->get_last_attempt( $quiz_id );
		if ( $last_attempt ) {
			$number = absint( $last_attempt->get( 'attempt' ) ) + 1;
		$attempt->set( 'attempt', $number );

		return $attempt;


Top ↑

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.