PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.10
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.10
4.4.10 4.4.9.1 4.4.9 4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 All 142 releases
← All changes | inc/user-item/class-lp-user-item-course.php +76 -42 4.1.6.9.2 → 4.4.10 View file →
@@ -1,10 +1,14 @@
1 1 <?php
2 +
3 +use LearnPress\Filters\UserItemsFilter;
4 +use LearnPress\Models\UserItems\UserCourseModel;
5 +
2 6 /**
3 7 * Class LP_User_Item_Course
4 8 */
5 9
6 -class LP_User_Item_Course extends LP_User_Item implements ArrayAccess {
10 +class LP_User_Item_Course extends LP_User_Item {
7 11 public $_item_type = LP_COURSE_CPT;
8 12 public $_ref_type = LP_ORDER_CPT;
9 13
10 14 /**
@@ -153,10 +157,14 @@
153 157 public function get_course_id() {
154 158 return $this->get_data( 'item_id' );
155 159 }
156 160
161 + /**
162 + * @deprecated 4.1.7.3
163 + */
157 164 public function get_finishing_type() {
158 - $type = $this->get_meta( 'finishing_type' );
165 + _deprecated_function( __METHOD__, '4.1.7.3' );
166 + /*$type = $this->get_meta( 'finishing_type' );
159 167
160 168 if ( ! $type ) {
161 169 $type = $this->is_exceeded() <= 0 ? 'exceeded' : 'click';
162 170
@@ -164,9 +172,9 @@
164 172
165 173 $this->set_meta( 'finishing_type', $type );
166 174 }
167 175
168 - return $type;
176 + return $type;*/
169 177 }
170 178
171 179 public function offsetSet( $offset, $value ) {
172 180 // $this->set_data( $offset, $value );
@@ -190,9 +198,9 @@
190 198 }
191 199
192 200 /**
193 201 * @return LP_User_Item|bool
194 - * @depecated 4.1.6.9.2
202 + * @deprecated 4.1.6.9.2
195 203 */
196 204 /*public function get_viewing_item() {
197 205 $item = LP_Global::course_item();
198 206 if ( $item ) {
@@ -201,15 +209,22 @@
201 209
202 210 return false;
203 211 }*/
204 212
205 - public function get_course( $return = '' ) {
206 - $cid = $this->get_data( 'item_id' );
213 + /**
214 + * Get course.
215 + *
216 + * @param string $return
217 + *
218 + * @return bool|LP_Course|int|mixed
219 + */
220 + public function get_course( string $return = '' ) {
221 + $course_id = $this->get_data( 'item_id', 0 );
207 222 if ( $return == '' ) {
208 - return $cid ? learn_press_get_course( $cid ) : false;
223 + return $course_id ? learn_press_get_course( $course_id ) : false;
209 224 }
210 225
211 - return $cid;
226 + return $course_id;
212 227 }
213 228
214 229 /**
215 230 * Get result/processing course
@@ -273,8 +288,31 @@
273 288 /**
274 289 * Calculate course result
275 290 */
276 291 public function calculate_course_results( bool $force_cache = false ) {
292 + $results = array(
293 + 'count_items' => 0,
294 + 'completed_items' => 0,
295 + 'items' => array(),
296 + 'evaluate_type' => '',
297 + 'pass' => 0,
298 + 'result' => 0,
299 + );
300 +
301 + try {
302 + $userCourseModel = UserCourseModel::find( $this->get_user_id(), $this->get_course_id(), true );
303 + if ( ! $userCourseModel instanceof UserCourseModel ) {
304 + return $results;
305 + }
306 +
307 + $results = $userCourseModel->calculate_course_results();
308 + } catch ( Throwable $e ) {
309 + LP_Debug::error_log( $e );
310 + }
311 +
312 + return $results;
313 +
314 + // Code old
277 315 $items = array();
278 316 $results = array(
279 317 'count_items' => 0,
280 318 'completed_items' => 0,
@@ -304,12 +342,19 @@
304 342 // Todo: tungnx - set cache
305 343 return LP_User_Items_Result_DB::instance()->get_result( $this->get_user_item_id() );
306 344 }
307 345
346 + if ( $this->get_status() !== LP_COURSE_ENROLLED ) {
347 + return $results;
348 + }
349 +
308 350 $count_items = $course->count_items();
309 351 $count_items_completed = $this->count_items_completed();
310 352
311 - $evaluate_type = $course->get_data( 'course_result', 'evaluate_lesson' );
353 + $evaluate_type = get_post_meta( $course->get_id(), '_lp_course_result', true );
354 + if ( ! $evaluate_type ) {
355 + $evaluate_type = 'evaluate_lesson';
356 + }
312 357 switch ( $evaluate_type ) {
313 358 case 'evaluate_lesson':
314 359 $results_evaluate = $this->evaluate_course_by_lesson( $count_items_completed, $course->count_items( LP_LESSON_CPT ) );
315 360 break;
@@ -364,9 +409,9 @@
364 409 );
365 410
366 411 LP_Cache::cache_load_first( 'set', $key_first_cache, $results );
367 412 } catch ( Throwable $e ) {
368 -
413 + error_log( __METHOD__ . ': ' . $e->getMessage() );
369 414 }
370 415
371 416 return $results;
372 417 }
@@ -376,10 +421,11 @@
376 421 *
377 422 * @param string $context
378 423 *
379 424 * @return string
425 + * @deprecated 4.1.6.9.3
380 426 */
381 - public function get_grade( string $context = 'display' ): string {
427 + public function get_grade( string $context = '' ): string {
382 428 $grade = $this->get_graduation() ?? '';
383 429
384 430 return $context == 'display' ? learn_press_course_grade_html( $grade, false ) : $grade;
385 431 }
@@ -384,15 +430,8 @@
384 430 return $context == 'display' ? learn_press_course_grade_html( $grade, false ) : $grade;
385 431 }
386 432
387 433 /**
388 - * @return bool
389 - */
390 - public function is_passed() {
391 - return $this->get_grade() == 'passed';
392 - }
393 -
394 - /**
395 434 * @param int $decimal
396 435 *
397 436 * @return int|string
398 437 */
@@ -628,9 +667,9 @@
628 667 continue;
629 668 }
630 669
631 670 if ( $item->type == LP_QUIZ_CPT ) {
632 - $total_questions += count( $itemObj->get_questions() );
671 + $total_questions += count( $itemObj->get_question_ids() );
633 672 $total_mark_question += $itemObj->get_mark();
634 673 }
635 674 }
636 675 }
@@ -656,10 +695,8 @@
656 695 }
657 696
658 697 /**
659 698 * Check course of use has enrolled
660 - *
661 - * @throws Exception
662 699 */
663 700 public function is_enrolled(): bool {
664 701 return $this->get_status() == LP_COURSE_ENROLLED;
665 702 }
@@ -665,10 +702,8 @@
665 702 }
666 703
667 704 /**
668 705 * Check course of use has purchased
669 - *
670 - * @throws Exception
671 706 * @author tungnx
672 707 * @since 4.1.3
673 708 * @version 1.0.0
674 709 */
@@ -696,9 +731,9 @@
696 731 return $level;
697 732 }
698 733
699 734 /**
700 - * @depecated 4.1.6.9.2
735 + * @deprecated 4.1.6.9.2
701 736 */
702 737 /*protected function _is_passed( $result ) {
703 738 $is_passed = LP_COURSE_GRADUATION_FAILED;
704 739 $result = round( $result, 2 );
@@ -772,9 +807,9 @@
772 807 }
773 808
774 809 if ( $type === $item_type ) {
775 810 if ( $item->get_status() == 'completed' ) {
776 - $completed ++;
811 + ++$completed;
777 812 }
778 813 $completed = apply_filters(
779 814 'learn-press/course-item/completed',
780 815 $completed,
@@ -781,9 +816,9 @@
781 816 $item,
782 817 $item->get_status()
783 818 );
784 819 // if ( ! $item->is_preview() ) {
785 - $total ++;
820 + ++$total;
786 821 // }
787 822 }
788 823 }
789 824 }
@@ -808,17 +843,15 @@
808 843 $count_items_completed = new stdClass();
809 844
810 845 try {
811 846 $course = learn_press_get_course( $this->get_course_id() );
812 -
813 847 if ( ! $course ) {
814 - throw new Exception( __FUNCTION__ . ': Course is invalid!' );
848 + throw new Exception( 'Course is invalid!' );
815 849 }
816 850
817 851 $user_course = $this->get_last_user_course();
818 -
819 852 if ( ! $user_course ) {
820 - throw new Exception();
853 + throw new Exception( 'User course is invalid!' );
821 854 }
822 855
823 856 $filter_count = new LP_User_Items_Filter();
824 857 $filter_count->parent_id = $user_course->user_item_id;
@@ -827,9 +860,9 @@
827 860 $filter_count->status = 'completed';
828 861 $filter_count->graduation = LP_COURSE_GRADUATION_PASSED;
829 862 $count_items_completed = $lp_user_items_db->count_items_of_course_with_status( $filter_count );
830 863 } catch ( Throwable $e ) {
831 -
864 + error_log( __METHOD__ . ': ' . $e->getMessage() );
832 865 }
833 866
834 867 return $count_items_completed;
835 868 }
@@ -880,9 +913,8 @@
880 913 /**
881 914 * Check course is completed or not.
882 915 *
883 916 * @return bool
884 - * @throws Exception
885 917 * @editor tungnx
886 918 * @modify 4.1.3
887 919 */
888 920 public function is_finished(): bool {
@@ -899,9 +931,9 @@
899 931 }
900 932
901 933 /**
902 934 * @return bool
903 - * @depecated 4.1.6.9.2
935 + * @deprecated 4.1.6.9.2
904 936 */
905 937 /*public function can_graduated() {
906 938 return $this->get_results( 'result' ) >= $this->get_passing_condition();
907 939 }*/
@@ -910,8 +942,10 @@
910 942 // TODO: Implement __unset() method.
911 943 }
912 944
913 945 /**
946 + * Get item user attend on course by item_id.
947 + *
914 948 * @param int $item_id
915 949 *
916 950 * @return bool|LP_User_Item
917 951 * @editor tungnx
@@ -987,9 +1021,9 @@
987 1021 * @return bool|int
988 1022 */
989 1023 public function increase_retake_count() {
990 1024 $count = $this->get_retaken_count();
991 - $count ++;
1025 + ++$count;
992 1026
993 1027 return $this->update_meta( '_lp_retaken_count', $count );
994 1028 }
995 1029
@@ -996,10 +1030,12 @@
996 1030 /**
997 1031 * Update course item and it's child.
998 1032 *
999 1033 * @TODO: tungnx - review to modify
1034 + * @deprecated 4.1.7.3
1000 1035 */
1001 1036 public function save() {
1037 + _deprecated_function( __METHOD__, '4.1.7.3' );
1002 1038 /**
1003 1039 * @var LP_User_Item $item
1004 1040 */
1005 1041 $this->update();
@@ -1008,18 +1044,16 @@
1008 1044 return false;
1009 1045 }
1010 1046
1011 1047 foreach ( $items as $item_id => $item ) {
1012 -
1013 1048 if ( ! $item->get_status() ) {
1014 1049 continue;
1015 1050 }
1016 1051
1017 1052 /**
1018 - * Auto fill the end-time if it isn't already set
1053 + * Autofill the end-time if it isn't already set
1019 1054 */
1020 1055 if ( in_array( $item->get_status(), array( 'completed', 'finished' ) ) ) {
1021 -
1022 1056 if ( ! $item->get_end_time() || $item->get_end_time()->is_null() ) {
1023 1057 $item->set_end_time( current_time( 'mysql', 1 ) );
1024 1058 }
1025 1059 }
@@ -1037,9 +1071,9 @@
1037 1071 * @param bool $with_total - Optional. Include total if TRUE
1038 1072 * @param int $section_id - Optional. Get in specific section
1039 1073 *
1040 1074 * @return array|bool|mixed
1041 - * @depecated 4.1.6.9
1075 + * @deprecated 4.1.6.9
1042 1076 */
1043 1077 public function get_passed_items( $type = '', $with_total = false, $section_id = 0 ) {
1044 1078 _deprecated_function( __FUNCTION__, '4.1.6.9' );
1045 1079 $this->read_items();
@@ -1083,9 +1117,9 @@
1083 1117 }
1084 1118
1085 1119 if ( $type === $item_type ) {
1086 1120 if ( $item->get_status( 'graduation' ) == 'passed' ) {
1087 - $passed ++;
1121 + ++$passed;
1088 1122 }
1089 1123 $passed = apply_filters(
1090 1124 'learn-press/course-item/passed',
1091 1125 $passed,
@@ -1092,9 +1126,9 @@
1092 1126 $item,
1093 1127 $item->get_status()
1094 1128 );
1095 1129 // if ( ! $item->is_preview() ) {
1096 - $total ++;
1130 + ++$total;
1097 1131 // }
1098 1132 }
1099 1133 }
1100 1134 }
@@ -1158,14 +1192,14 @@
1158 1192
1159 1193 /**
1160 1194 * Get courses only by course's user are learning
1161 1195 *
1162 - * @param LP_User_Items_Filter $filter
1196 + * @param LP_User_Items_Filter|UserItemsFilter $filter
1163 1197 * @param int $total_rows return total row query
1164 1198 *
1165 1199 * @return array|null|int|string
1166 1200 */
1167 - public static function get_user_courses( LP_User_Items_Filter $filter, int &$total_rows = 0 ) {
1201 + public static function get_user_courses( $filter, int &$total_rows = 0 ) {
1168 1202 try {
1169 1203 /*$key_cache = md5( json_encode( $filter ) );
1170 1204 $courses_cache = LP_Cache::instance()->get_cache( $key_cache );
1171 1205