| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class UserLessonModel |
| 5 |
* |
| 6 |
* @package LearnPress/Classes |
| 7 |
* @version 1.0.1 |
| 8 |
* @since 4.2.7.6 |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace LearnPress\Models\UserItems; |
| 12 |
|
| 13 |
use Exception; |
| 14 |
use LearnPress\Models\CourseModel; |
| 15 |
use LearnPress\Models\LessonPostModel; |
| 16 |
use LP_Datetime; |
| 17 |
use WP_Error; |
| 18 |
|
| 19 |
class UserLessonModel extends UserItemModel { |
| 20 |
/** |
| 21 |
* Item type Lesson |
| 22 |
* |
| 23 |
* @var string Item type |
| 24 |
*/ |
| 25 |
public $item_type = LP_LESSON_CPT; |
| 26 |
/** |
| 27 |
* Ref type Course |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
public $ref_type = LP_COURSE_CPT; |
| 32 |
|
| 33 |
public function __construct( $data = null ) { |
| 34 |
parent::__construct( $data ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Get lesson model |
| 39 |
* |
| 40 |
* @return bool|LessonPostModel |
| 41 |
* @since 4.2.5 |
| 42 |
* @version 1.0.1 |
| 43 |
*/ |
| 44 |
public function get_lesson_post_model() { |
| 45 |
return LessonPostModel::find( $this->item_id, true ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Get course model |
| 50 |
* |
| 51 |
* @return false|CourseModel |
| 52 |
* @since 4.2.7.6 |
| 53 |
* @version 1.0.0 |
| 54 |
*/ |
| 55 |
public function get_course_model() { |
| 56 |
return CourseModel::find( $this->ref_id, true ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Get user course model |
| 61 |
* |
| 62 |
* @return false|UserCourseModel |
| 63 |
* @since 4.2.7.6 |
| 64 |
* @version 1.0.0 |
| 65 |
*/ |
| 66 |
public function get_user_course_model() { |
| 67 |
return UserCourseModel::find( $this->user_id, $this->ref_id, true ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Complete lesson |
| 72 |
* |
| 73 |
* @throws Exception |
| 74 |
* @version 1.0.1 |
| 75 |
* @since 4.2.7.6 |
| 76 |
*/ |
| 77 |
public function set_complete() { |
| 78 |
if ( $this->get_status() === self::STATUS_COMPLETED ) { |
| 79 |
throw new Exception( __( 'Lesson is already completed.', 'learnpress' ) ); |
| 80 |
} |
| 81 |
|
| 82 |
$userCourseModel = $this->get_user_course_model(); |
| 83 |
if ( ! $userCourseModel ) { |
| 84 |
throw new Exception( __( 'You have not started course', 'learnpress' ) ); |
| 85 |
} |
| 86 |
|
| 87 |
$can_impact_lesson = $userCourseModel->can_impact_item(); |
| 88 |
if ( $can_impact_lesson instanceof WP_Error ) { |
| 89 |
throw new Exception( $can_impact_lesson->get_error_message() ); |
| 90 |
} |
| 91 |
|
| 92 |
$this->status = self::STATUS_COMPLETED; |
| 93 |
$this->end_time = gmdate( LP_Datetime::$format, time() ); |
| 94 |
$this->graduation = self::GRADUATION_PASSED; |
| 95 |
$this->save(); |
| 96 |
|
| 97 |
do_action( 'learn-press/user-completed-lesson', $this->item_id, $this->ref_id, $this->user_id ); |
| 98 |
do_action( 'learn-press/user-lesson/completed', $this ); |
| 99 |
} |
| 100 |
} |
| 101 |
|