llms_is_post_restricted_by_prerequisite( int $post_id,  $user_id = null )

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


Description Description


Parameters Parameters

$post_id

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


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 );

	if ( 'lesson' === $post_type ) {
		$lesson_id = $post_id;
	} elseif ( 'llms_quiz' === $post_type ) {
		$quiz = llms_get_post( $post_id );
		$lesson_id = $quiz->get( 'lesson_id' );
		if ( ! $lesson_id ) {
			return false;
		}
	} else {
		return false;
	}

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

	if ( ! $course ) {
		return false;
	}

	// get an array of all possible prereqs
	$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',
		);
	}

	// prereqs 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 prereq
	return false;

}

Top ↑

Changelog Changelog

Changelog
Version Description
3.0.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

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





Permalink: