LLMS_Generator_Courses::handle_prerequisites()

Updates course and lesson prerequisites


Description Description

If the prerequisite was included in the import, updates to the new imported version.

If the prereq is not included but the source matches, leaves the prereq intact as long as the prereq exists.

Otherwise removes prerequisite data from the new course / lesson.

Removes prereq track associations if there’s no source or source doesn’t match or if the track doesn’t exist.


Top ↑

Return Return

(void)


Top ↑

Source Source

File: includes/class-llms-generator-courses.php

566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
protected function handle_prerequisites() {
 
    foreach ( array( 'course', 'lesson' ) as $obj_type ) {
 
        $ids = ! empty( $this->tempids[ $obj_type ] ) ? $this->tempids[ $obj_type ] : array();
 
        // Courses have two kinds of prereqs.
        $has_prereq_param = ( 'course' === $obj_type ) ? 'course' : null;
 
        // Loop through all then created lessons.
        foreach ( $ids as $old_id => $new_id ) {
 
            // Instantiate the new instance of the object.
            $obj = llms_get_post( $new_id );
 
            // If this is a course and there isn't a source or the source doesn't match the current site.
            // We should remove the track prerequisites.
            if ( 'course' === $obj_type && ( ! isset( $raw['_source'] ) || get_site_url() !== $raw['_source'] ) ) {
 
                // Remove prereq track settings.
                if ( $obj->has_prerequisite( 'course_track' ) ) {
                    $obj->set( 'prerequisite_track', 0 );
                    if ( ! $obj->has_prerequisite( 'course' ) ) {
                        $obj->set( 'has_prerequisite', 'no' );
                    }
                }
            }
 
            // If the object has a prereq.
            if ( $obj->has_prerequisite( $has_prereq_param ) ) {
 
                // Get the old preqeq's id.
                $old_prereq = $obj->get( 'prerequisite' );
 
                // If the old prereq is a key in the array of created objects.
                // We can replace it with the new id.
                if ( in_array( $old_prereq, array_keys( $ids ) ) ) {
 
                    $obj->set( 'prerequisite', $ids[ $old_prereq ] );
 
                } elseif ( ! isset( $raw['_source'] ) || get_site_url() !== $raw['_source'] ) {
 
                    $obj->set( 'has_prerequisite', 'no' );
                    $obj->set( 'prerequisite', 0 );
 
                } else {
                    $post = get_post( $old_prereq );
                    // Post doesn't exist or the post type doesn't match, get rid of it.
                    if ( ! $post || $obj_type !== $post->post_type ) {
 
                        $obj->set( 'has_prerequisite', 'no' );
                        $obj->set( 'prerequisite', 0 );
 
                    }
                }
            }
        }
    }
 
}


Top ↑

Changelog Changelog

Changelog
Version Description
3.3.0
3.24.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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