llms_is_post_restricted_by_prerequisite( int $post_id, int|null $user_id = null )

Determine if a lesson/quiz is restricted by a prerequisite lesson.


Parameters Parameters

$post_id

(int) (Required) WP Post ID of a lesson or quiz.

$user_id

(int|null) (Optional) WP User ID (will use get_current_user_id() if none supplied). Default null.

Default value: null


Top ↑

Return Return

(array|false) False if the post is not restricted or the user has completed the prereq associative array with prereq type and prereq id array( type => [course|course_track|lesson] id => int (object id) ).


Top ↑

Source Source

File: includes/functions/llms.functions.access.php

function llms_is_post_restricted_by_prerequisite( $post_id, $user_id = null ) {

	$post_type = get_post_type( $post_id );

	switch ( $post_type ) {
		// If we're on a lesson, lesson id is the post id.
		case 'lesson':
			$lesson_id = $post_id;
			break;
		case 'llms_quiz':
			$quiz      = llms_get_post( $post_id );
			$lesson_id = $quiz->get( 'lesson_id' );
			if ( ! $lesson_id ) {
				return false;
			}
			break;
		default: // Don't pass other post types.
			return false;
	}

	$lesson = llms_get_post( $lesson_id );
	$course = $lesson->get_course();

	if ( ! $course ) {
		return false;
	}

	// Get an array of all possible prerequisites.
	$prerequisites = array();

	if ( $course->has_prerequisite( 'course' ) ) {
		$prerequisites[] = array(
			'id'   => $course->get_prerequisite_id( 'course' ),
			'type' => 'course',
		);
	}

	if ( $course->has_prerequisite( 'course_track' ) ) {
		$prerequisites[] = array(
			'id'   => $course->get_prerequisite_id( 'course_track' ),
			'type' => 'course_track',
		);
	}

	if ( $lesson->has_prerequisite() ) {
		$prerequisites[] = array(
			'id'   => $lesson->get_prerequisite(),
			'type' => 'lesson',
		);
	}

	// Prerequisites exist and user is not logged in, return the first prereq id.
	if ( $prerequisites && ! $user_id ) {

		return array_shift( $prerequisites );

		// If incomplete, send the prereq id.
	} else {

		$student = new LLMS_Student( $user_id );
		foreach ( $prerequisites as $prereq ) {
			if ( ! $student->is_complete( $prereq['id'], $prereq['type'] ) ) {
				return $prereq;
			}
		}
	}

	// Otherwise return false: no prerequisite.
	return false;

}


Top ↑

Changelog Changelog

Changelog
Version Description
6.5.0 Improve code readability turning if-elseif into a switch-case.
3.16.11 Unknown.
3.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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