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

377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
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.