Learndash Custom Course Pages

<?php

//load course data
$course_id = learndash_get_course_id($id);
$course_title = get_the_title($course_id);
$course = get_post($course_id);
$lessons = learndash_get_course_lessons_list($course);

//load user data
$user_id = get_current_user_id();
$guest = true;
if ( is_user_logged_in() ) {
    $guest = false;
}

//load course content
if(!empty($lessons)) {
    $lesson_open = true;
    foreach($lessons as $lesson) {
        
        //load lesson variables
        $lesson_id = $lesson['post']->ID;
        $is_lesson_completed = ($course_lesson["status"]=='completed')?true:false;
        $lesson_url = get_permalink( $lesson_id );
        $lesson_featured_image = get_the_post_thumbnail( $lesson_id, array( 120, 120) );
        
        //output lesson info
        echo '<div class = "no-topic-lesson-display">';
        echo '<div class = "no-topic-lesson-display-inner">';
        if ($lesson_open) {
            echo '<div class="lesson_open">';
            echo '<a href="' . $lesson_url . '">';
            echo $lesson_featured_image;
            echo '<h2>' . $lesson["post"]->post_title . '</h2>';
            echo '</a>';
            echo '</div>';
        } else {
            echo '<div class="lesson_closed">';
            echo $lesson_featured_image;
            echo '<h2>' . $lesson["post"]->post_title . '</h2>';
            echo '</div>';
        }
        
        //load quizzes
        $lesson_quiz_list = learndash_get_lesson_quiz_list( $lesson_id, get_current_user_id() );
        
        //loop through quizzes and output progress bars
        $lesson_quiz_list_count = [];
        $count = 0;
        foreach ($lesson_quiz_list as $lesson_quiz) {
            //see if we have any quizzes and get compltetion info
            $count += 1;
            $quiz_completed = learndash_is_quiz_complete( get_current_user_id(), $lesson_quiz['post']->ID );
            $completed_class = empty( $quiz_completed ) ? 'not-completed' : 'completed';

            $lesson_quiz_list_count[$count] = $completed_class;
        }
        if ($count > 0) {
            //if there are quizzes print to page
            echo '<table class= "topic-progress"><tr>';
            foreach ($lesson_quiz_list_count as $key => $value) {
                echo '<td class="' . $value. '"></td>';
            }
            echo '</tr></table>';
        }
        //should the next lesson be open
        if ($is_lesson_completed) {
            $lesson_open = true;
        } else {
            $lesson_open = false;
        }
        echo '</div></div>';
    }
}
?>
<!-- load users next uncompleted test in this course -->
<?php 
//loop through course lessons
foreach($lessons as $lesson) {
    $lesson_id = $lesson['post']->ID;
    $lesson_quiz_list = learndash_get_lesson_quiz_list( $lesson_id, get_current_user_id() );
    
    //loop through each quiz attached to each lesson
    foreach ($lesson_quiz_list as $lesson_quiz) {
        $quiz_id = $lesson_quiz["post"]->ID;
        $quiz_title = $lesson_quiz["post"]->post_title;
        $quiz_url = get_permalink( $quiz_id );
        $quiz_featured_image = '<img src = "' . get_template_directory_uri() . '/images/icons/paper-11-plus-verbal-reasoning.png">';
        $quiz_completed = learndash_is_quiz_complete( $user_id, $quiz_id  );
        
        //output details for first uncompleted quiz found
        if ($quiz_completed == false) {
            echo '<div class= "course-info-box">';
            echo '<h3>Next Activity</h3>';
            echo '<div class= "course-info-box-inner">';
            echo '<a href ="' . $quiz_url . '">';
            echo $quiz_featured_image;
            echo '<p>' . $quiz_title . '</p>';
            echo '</a>';
            echo '<a href ="' . $quiz_url . '" class="g-button">Start</a>';
            echo'</div>';
            echo '</div>';
            break 2;
        }
    }    
}
?>
<!-- generate and output course progress bar -->
<div class= "course-info-box">
    <h3>Course Progress</h3>
    <div class= "course-info-box-inner">
    <?php
    
    //create variables
    $course_started = false;
    $quiz_count = 0;
    $quiz_completed_count = 0;
    $unit_count = 0;
    $unit_completed_count = 0;
    
    // if lessons then start lesson loop
    if(!empty($lessons)) {
        foreach($lessons as $lesson) {
        
            // count lessons
            $unit_count += 1;
            $lesson_id = $lesson['post']->ID;
            
            // count completed lessons
            $is_lesson_completed = ($lesson_id["status"]=='completed')?true:false;
            if($is_lesson_completed) {
                $unit_completed_count += 1;
            }
            
            //load quizzes
            $lesson_quiz_list = learndash_get_lesson_quiz_list( $lesson_id, get_current_user_id() );
            
            // loop through quizzes and count
            foreach ($lesson_quiz_list as $lesson_quiz) {
                $quiz_id = $lesson_quiz["post"]->ID;
                $quiz_count += 1;
                
                //count completed quizzes
                $quiz_completed = learndash_is_quiz_complete( $user_id, $quiz_id  );
                if ($quiz_completed) {
                    $course_started = true;
                    $quiz_completed_count += 1;
                }
            }    
        }
    }
    
    //calculate course progress
    $progress = round($quiz_completed_count / $quiz_count * 100);
    
    //output results
    echo '<div class="course-progress-box">';
        echo get_the_post_thumbnail( $course_id, array( 60, 60) );
        echo '<div class="course-progress-bar"><div class="course-progress-bar-inner" style="width:' . $progress . '%"></div></div>';
        echo '<p>' . $unit_completed_count . ' out of ' . $unit_count . ' units completed</p>';
    echo '</div>';
echo '</div>';
?>