| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Course Progress Widget. |
| 5 |
* |
| 6 |
* @author ThimPress |
| 7 |
* @category Widgets |
| 8 |
* @package Learnpress/Widgets |
| 9 |
* @version 4.0.0 |
| 10 |
* @extends LP_Widget |
| 11 |
*/ |
| 12 |
|
| 13 |
defined( 'ABSPATH' ) || exit(); |
| 14 |
|
| 15 |
if ( ! class_exists( 'LP_Widget_Course_Progress' ) ) { |
| 16 |
|
| 17 |
/** |
| 18 |
* Class LP_Widget_Course_Progress |
| 19 |
*/ |
| 20 |
class LP_Widget_Course_Progress extends LP_Widget { |
| 21 |
|
| 22 |
public function __construct() { |
| 23 |
$this->widget_cssclass = 'learnpress widget_course_progress'; |
| 24 |
$this->widget_description = esc_html__( 'Display the Course Progress', 'learnpress' ); |
| 25 |
$this->widget_id = 'learnpress_widget_course_progress'; |
| 26 |
$this->widget_name = esc_html__( 'LearnPress - Course Progress', 'learnpress' ); |
| 27 |
$this->settings = array( |
| 28 |
'title' => array( |
| 29 |
'label' => esc_html__( 'Title', 'learnpress' ), |
| 30 |
'type' => 'text', |
| 31 |
'std' => esc_html__( 'Course Progress', 'learnpress' ), |
| 32 |
), |
| 33 |
'course_id' => array( |
| 34 |
'label' => esc_html__( 'Select Course', 'learnpress' ), |
| 35 |
'type' => 'autocomplete', |
| 36 |
'post_type' => LP_COURSE_CPT, |
| 37 |
'std' => '', |
| 38 |
), |
| 39 |
'css_class' => array( |
| 40 |
'label' => esc_html__( 'CSS Class', 'learnpress' ), |
| 41 |
'type' => 'text', |
| 42 |
'std' => '', |
| 43 |
), |
| 44 |
); |
| 45 |
|
| 46 |
parent::__construct(); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* @throws Exception |
| 51 |
*/ |
| 52 |
public function lp_rest_api_content( $instance, $params ) { |
| 53 |
$user_id = get_current_user_id(); |
| 54 |
|
| 55 |
if ( empty( $user_id ) ) { |
| 56 |
return new WP_Error( 'no_user', esc_html__( 'You need to log in to view the Course Progress', 'learnpress' ) ); |
| 57 |
} |
| 58 |
|
| 59 |
if ( empty( $instance['course_id'] ) ) { |
| 60 |
return new WP_Error( 'no_course', esc_html__( 'Please choose a course!', 'learnpress' ) ); |
| 61 |
} |
| 62 |
|
| 63 |
$course = learn_press_get_course( $instance['course_id'] ); |
| 64 |
if ( ! $course ) { |
| 65 |
return new WP_Error( 'no_course', esc_html__( 'The course is invalid!', 'learnpress' ) ); |
| 66 |
} |
| 67 |
|
| 68 |
$user = learn_press_get_user( $user_id ); |
| 69 |
|
| 70 |
$course_data = $user->get_course_data( $course->get_id() ); |
| 71 |
if ( ! $course_data ) { |
| 72 |
return new WP_Error( 'no_enroll', sprintf( esc_html__( 'You haven\'t started %s', 'learnpress' ), $course->get_title() ) ); |
| 73 |
} |
| 74 |
|
| 75 |
if ( ! $user->has_enrolled_or_finished( $instance['course_id'] ) ) { |
| 76 |
return new WP_Error( 'no_enroll', sprintf( esc_html__( 'You haven\'t started %s', 'learnpress' ), $course->get_title() ) ); |
| 77 |
} |
| 78 |
|
| 79 |
$course_results = $course_data->get_result(); |
| 80 |
|
| 81 |
$instance['css_class'] = $instance['css_class'] ?? ''; |
| 82 |
|
| 83 |
return learn_press_get_template_content( |
| 84 |
'widgets/course-progress', |
| 85 |
compact( 'user', 'course', 'instance', 'course_data', 'course_results' ) |
| 86 |
); |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|