<?php
// Silence is golden.

	$settings = $this->get_settings_for_display();
    $id = $this->get_id();
    

    if( class_exists('SFWD_LMS') ) :
        $elementor_editor = \Elementor\Plugin::instance()->editor->is_edit_mode();
        if( $elementor_editor ) {
            $course_id = learndash_post_arr()[0];
        } else {
            $course_id = learndash_get_course_id();
        }

        if( is_user_logged_in() ) {
            $user_id = get_current_user_id();
        } else {
            $user_id = 0;
        }

        $has_access = sfwd_lms_has_access( $course_id );
        $course_status = learndash_course_status( $course_id, $user_id );
        $course_progress = get_user_meta( $user_id, '_sfwd-course_progress', true );


        $completed = 0;
        $total     = false;

        if ( ! empty( $user_id ) ) {

            $course_progress = get_user_meta( $user_id, '_sfwd-course_progress', true );

            $percentage = 0;
            $message    = '';

            if ( ( ! empty( $course_progress ) ) && ( isset( $course_progress[ $course_id ] ) ) && ( ! empty( $course_progress[ $course_id ] ) ) ) {
                if ( isset( $course_progress[ $course_id ]['completed'] ) ) {
                    $completed = absint( $course_progress[ $course_id ]['completed'] );
                }

                if ( isset( $course_progress[ $course_id ]['total'] ) ) {
                    $total = absint( $course_progress[ $course_id ]['total'] );
                }
            }
        }

        // If $total is still false we calculate the total from course steps.
        if ( false === $total ) {
            $total = learndash_get_course_steps_count( $course_id );
        }

        if ( $total > 0 ) {
            $percentage = intval( $completed * 100 / $total );
            $percentage = ( $percentage > 100 ) ? 100 : $percentage;
        } else {
            $percentage = 0;
        }

        $course_args     = array(
            'course_id'     => $course_id,
            'user_id'       => $user_id,
            'post_id'       => $course_id,
            'activity_type' => 'course',
        );

        $course_activity = learndash_get_user_activity( $course_args );

        if( $has_access && is_user_logged_in() ) :
        ?>
            <div class="ld-course-status">
                <div class="ld-course-status-top">
                    <div class="ld-course-progress-heading">
                        <h4>
                            <?php echo esc_html__('Steps Completed','widgetkit-pro'); ?>
                            <span><?php echo esc_html__(sprintf('%s of %s Steps',$completed,$total),'widgetkit-pro'); ?></span>
                        </h4>
                    </div>
                    <?php if( $course_activity ) : ?>
                    <div class="ld-course-progress-activity">
                        <p> <span></span> <?php echo esc_html__('Last activity on ','widgetkit-pro') . learndash_adjust_date_time_display( $course_activity->activity_updated ); ?></p>
                    </div>
                    <?php endif; ?>
                </div>
                <div class="ld-course-status-bottom">
                    <div class="ld-course-progress-bar">
                        <div class="ld-course-progress-bar-percentage" style="width:<?php echo esc_attr($percentage); ?>%;"></div>
                    </div>
                    <div class="ld-course-status-button">
                        <div class="ld-course-status-button-status"><?php echo esc_html($course_status); ?></div>
                    </div>
                </div>
            </div>
            
        <?php
        endif;
    else :
    ?>
        <div class="plugin-active-massage">
            <h3><?php esc_html_e('Please activate LearnDash LMS plugin first', 'widgetkit-pro');?></h3>
        </div>
    <?php
        endif;