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 +84 -90 4.1.6.9.1 → 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,10 +198,11 @@
190 198 }
191 199
192 200 /**
193 201 * @return LP_User_Item|bool
202 + * @deprecated 4.1.6.9.2
194 203 */
195 - public function get_viewing_item() {
204 + /*public function get_viewing_item() {
196 205 $item = LP_Global::course_item();
197 206 if ( $item ) {
198 207 return $this[ $item->get_id() ];
199 208 }
@@ -198,17 +207,24 @@
198 207 return $this[ $item->get_id() ];
199 208 }
200 209
201 210 return false;
202 - }
211 + }*/
203 212
204 - public function get_course( $return = '' ) {
205 - $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 );
206 222 if ( $return == '' ) {
207 - return $cid ? learn_press_get_course( $cid ) : false;
223 + return $course_id ? learn_press_get_course( $course_id ) : false;
208 224 }
209 225
210 - return $cid;
226 + return $course_id;
211 227 }
212 228
213 229 /**
214 230 * Get result/processing course
@@ -256,15 +272,9 @@
256 272 'course-results'
257 273 );
258 274
259 275 if ( $results === false ) {
260 - // $course_result = $course->get_evaluation_results_method();
261 -
262 - // $results = LP_User_Items_Result_DB::instance()->get_result( $this->get_user_item_id() );
263 -
264 - // if ( is_array( $results ) && array_key_exists( 'result', $results ) ) {
265 276 $results = $this->calculate_course_results();
266 - // }
267 277
268 278 LP_Object_Cache::set(
269 279 'course-' . $this->get_item_id() . '-' . $this->get_user_id(),
270 280 $results,
@@ -278,8 +288,31 @@
278 288 /**
279 289 * Calculate course result
280 290 */
281 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
282 315 $items = array();
283 316 $results = array(
284 317 'count_items' => 0,
285 318 'completed_items' => 0,
@@ -285,8 +318,9 @@
285 318 'completed_items' => 0,
286 319 'items' => array(),
287 320 'evaluate_type' => '',
288 321 'pass' => 0,
322 + 'result' => 0,
289 323 );
290 324
291 325 try {
292 326 $course = learn_press_get_course( $this->get_course_id() );
@@ -308,12 +342,19 @@
308 342 // Todo: tungnx - set cache
309 343 return LP_User_Items_Result_DB::instance()->get_result( $this->get_user_item_id() );
310 344 }
311 345
346 + if ( $this->get_status() !== LP_COURSE_ENROLLED ) {
347 + return $results;
348 + }
349 +
312 350 $count_items = $course->count_items();
313 351 $count_items_completed = $this->count_items_completed();
314 352
315 - $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 + }
316 357 switch ( $evaluate_type ) {
317 358 case 'evaluate_lesson':
318 359 $results_evaluate = $this->evaluate_course_by_lesson( $count_items_completed, $course->count_items( LP_LESSON_CPT ) );
319 360 break;
@@ -328,8 +369,9 @@
328 369 $results_evaluate = $this->evaluate_course_by_question( $evaluate_type );
329 370 break;
330 371 default:
331 372 $results_evaluate = apply_filters( 'learn-press/evaluate_passed_conditions', $results, $evaluate_type, $this );
373 + break;
332 374 }
333 375
334 376 if ( ! is_array( $results_evaluate ) ) {
335 377 $results_evaluate = array(
@@ -357,15 +399,8 @@
357 399 $results_evaluate,
358 400 compact( 'count_items', 'completed_items', 'items', 'evaluate_type' )
359 401 );
360 402
361 - /*
362 - $graduation = '';
363 -
364 - if ( ! $this->is_purchased() ) {
365 - $graduation = $this->is_finished() ? $this->_is_passed( $results['result'] ) : 'in-progress';
366 - }*/
367 -
368 403 $results = apply_filters(
369 404 'learn-press/update-course-results',
370 405 $results,
371 406 $this->get_item_id(),
@@ -374,9 +409,9 @@
374 409 );
375 410
376 411 LP_Cache::cache_load_first( 'set', $key_first_cache, $results );
377 412 } catch ( Throwable $e ) {
378 -
413 + error_log( __METHOD__ . ': ' . $e->getMessage() );
379 414 }
380 415
381 416 return $results;
382 417 }
@@ -386,10 +421,11 @@
386 421 *
387 422 * @param string $context
388 423 *
389 424 * @return string
425 + * @deprecated 4.1.6.9.3
390 426 */
391 - public function get_grade( string $context = 'display' ): string {
427 + public function get_grade( string $context = '' ): string {
392 428 $grade = $this->get_graduation() ?? '';
393 429
394 430 return $context == 'display' ? learn_press_course_grade_html( $grade, false ) : $grade;
395 431 }
@@ -394,15 +430,8 @@
394 430 return $context == 'display' ? learn_press_course_grade_html( $grade, false ) : $grade;
395 431 }
396 432
397 433 /**
398 - * @return bool
399 - */
400 - public function is_passed() {
401 - return $this->get_grade() == 'passed';
402 - }
403 -
404 - /**
405 434 * @param int $decimal
406 435 *
407 436 * @return int|string
408 437 */
@@ -638,9 +667,9 @@
638 667 continue;
639 668 }
640 669
641 670 if ( $item->type == LP_QUIZ_CPT ) {
642 - $total_questions += count( $itemObj->get_questions() );
671 + $total_questions += count( $itemObj->get_question_ids() );
643 672 $total_mark_question += $itemObj->get_mark();
644 673 }
645 674 }
646 675 }
@@ -666,10 +695,8 @@
666 695 }
667 696
668 697 /**
669 698 * Check course of use has enrolled
670 - *
671 - * @throws Exception
672 699 */
673 700 public function is_enrolled(): bool {
674 701 return $this->get_status() == LP_COURSE_ENROLLED;
675 702 }
@@ -675,10 +702,8 @@
675 702 }
676 703
677 704 /**
678 705 * Check course of use has purchased
679 - *
680 - * @throws Exception
681 706 * @author tungnx
682 707 * @since 4.1.3
683 708 * @version 1.0.0
684 709 */
@@ -706,42 +731,11 @@
706 731 return $level;
707 732 }
708 733
709 734 /**
710 - * Evaluate course result by final quiz.
711 - *
712 - * @return array
735 + * @deprecated 4.1.6.9.2
713 736 */
714 - protected function _evaluate_course_by_final_quiz() {
715 - $cache_key = 'user-course-' . $this->get_user_id() . '-' . $this->get_id();
716 - $cached_data = LP_Object_Cache::get( $cache_key, 'learn-press/course-results' );
717 -
718 - if ( false === $cached_data || ! array_key_exists( 'final-quiz', $cached_data ) ) {
719 - $course = $this->get_course();
720 - $final_quiz = $course->get_final_quiz();
721 - $user_quiz = $this->get_item( $final_quiz );
722 - $result = false;
723 -
724 - if ( $user_quiz ) {
725 - $result = $user_quiz->get_results( false );
726 - }
727 -
728 - $percent = $result ? $result['result'] : 0;
729 - $data = array(
730 - 'result' => $percent,
731 - 'status' => $this->get_status(),
732 - );
733 -
734 - settype( $cached_data, 'array' );
735 - $cached_data['final-quiz'] = $data;
736 -
737 - LP_Object_Cache::set( $cache_key, $cached_data, 'learn-press/course-results' );
738 - }
739 -
740 - return isset( $cached_data['final-quiz'] ) ? $cached_data['final-quiz'] : array();
741 - }
742 -
743 - protected function _is_passed( $result ) {
737 + /*protected function _is_passed( $result ) {
744 738 $is_passed = LP_COURSE_GRADUATION_FAILED;
745 739 $result = round( $result, 2 );
746 740
747 741 if ( $result >= $this->get_passing_condition() ) {
@@ -748,9 +742,9 @@
748 742 $is_passed = LP_COURSE_GRADUATION_PASSED;
749 743 }
750 744
751 745 return apply_filters( 'learnpress/user/course/is-passed', $is_passed, $result );
752 - }
746 + }*/
753 747
754 748 /**
755 749 * Get completed items.
756 750 *
@@ -813,9 +807,9 @@
813 807 }
814 808
815 809 if ( $type === $item_type ) {
816 810 if ( $item->get_status() == 'completed' ) {
817 - $completed ++;
811 + ++$completed;
818 812 }
819 813 $completed = apply_filters(
820 814 'learn-press/course-item/completed',
821 815 $completed,
@@ -822,9 +816,9 @@
822 816 $item,
823 817 $item->get_status()
824 818 );
825 819 // if ( ! $item->is_preview() ) {
826 - $total ++;
820 + ++$total;
827 821 // }
828 822 }
829 823 }
830 824 }
@@ -849,17 +843,15 @@
849 843 $count_items_completed = new stdClass();
850 844
851 845 try {
852 846 $course = learn_press_get_course( $this->get_course_id() );
853 -
854 847 if ( ! $course ) {
855 - throw new Exception( __FUNCTION__ . ': Course is invalid!' );
848 + throw new Exception( 'Course is invalid!' );
856 849 }
857 850
858 851 $user_course = $this->get_last_user_course();
859 -
860 852 if ( ! $user_course ) {
861 - throw new Exception();
853 + throw new Exception( 'User course is invalid!' );
862 854 }
863 855
864 856 $filter_count = new LP_User_Items_Filter();
865 857 $filter_count->parent_id = $user_course->user_item_id;
@@ -868,9 +860,9 @@
868 860 $filter_count->status = 'completed';
869 861 $filter_count->graduation = LP_COURSE_GRADUATION_PASSED;
870 862 $count_items_completed = $lp_user_items_db->count_items_of_course_with_status( $filter_count );
871 863 } catch ( Throwable $e ) {
872 -
864 + error_log( __METHOD__ . ': ' . $e->getMessage() );
873 865 }
874 866
875 867 return $count_items_completed;
876 868 }
@@ -921,9 +913,8 @@
921 913 /**
922 914 * Check course is completed or not.
923 915 *
924 916 * @return bool
925 - * @throws Exception
926 917 * @editor tungnx
927 918 * @modify 4.1.3
928 919 */
929 920 public function is_finished(): bool {
@@ -940,12 +931,13 @@
940 931 }
941 932
942 933 /**
943 934 * @return bool
935 + * @deprecated 4.1.6.9.2
944 936 */
945 - public function can_graduated() {
937 + /*public function can_graduated() {
946 938 return $this->get_results( 'result' ) >= $this->get_passing_condition();
947 - }
939 + }*/
948 940
949 941 function __destruct() {
950 942 // TODO: Implement __unset() method.
951 943 }
@@ -950,8 +942,10 @@
950 942 // TODO: Implement __unset() method.
951 943 }
952 944
953 945 /**
946 + * Get item user attend on course by item_id.
947 + *
954 948 * @param int $item_id
955 949 *
956 950 * @return bool|LP_User_Item
957 951 * @editor tungnx
@@ -1027,9 +1021,9 @@
1027 1021 * @return bool|int
1028 1022 */
1029 1023 public function increase_retake_count() {
1030 1024 $count = $this->get_retaken_count();
1031 - $count ++;
1025 + ++$count;
1032 1026
1033 1027 return $this->update_meta( '_lp_retaken_count', $count );
1034 1028 }
1035 1029
@@ -1036,10 +1030,12 @@
1036 1030 /**
1037 1031 * Update course item and it's child.
1038 1032 *
1039 1033 * @TODO: tungnx - review to modify
1034 + * @deprecated 4.1.7.3
1040 1035 */
1041 1036 public function save() {
1037 + _deprecated_function( __METHOD__, '4.1.7.3' );
1042 1038 /**
1043 1039 * @var LP_User_Item $item
1044 1040 */
1045 1041 $this->update();
@@ -1048,18 +1044,16 @@
1048 1044 return false;
1049 1045 }
1050 1046
1051 1047 foreach ( $items as $item_id => $item ) {
1052 -
1053 1048 if ( ! $item->get_status() ) {
1054 1049 continue;
1055 1050 }
1056 1051
1057 1052 /**
1058 - * Auto fill the end-time if it isn't already set
1053 + * Autofill the end-time if it isn't already set
1059 1054 */
1060 1055 if ( in_array( $item->get_status(), array( 'completed', 'finished' ) ) ) {
1061 -
1062 1056 if ( ! $item->get_end_time() || $item->get_end_time()->is_null() ) {
1063 1057 $item->set_end_time( current_time( 'mysql', 1 ) );
1064 1058 }
1065 1059 }
@@ -1077,9 +1071,9 @@
1077 1071 * @param bool $with_total - Optional. Include total if TRUE
1078 1072 * @param int $section_id - Optional. Get in specific section
1079 1073 *
1080 1074 * @return array|bool|mixed
1081 - * @depecated 4.1.6.9
1075 + * @deprecated 4.1.6.9
1082 1076 */
1083 1077 public function get_passed_items( $type = '', $with_total = false, $section_id = 0 ) {
1084 1078 _deprecated_function( __FUNCTION__, '4.1.6.9' );
1085 1079 $this->read_items();
@@ -1123,9 +1117,9 @@
1123 1117 }
1124 1118
1125 1119 if ( $type === $item_type ) {
1126 1120 if ( $item->get_status( 'graduation' ) == 'passed' ) {
1127 - $passed ++;
1121 + ++$passed;
1128 1122 }
1129 1123 $passed = apply_filters(
1130 1124 'learn-press/course-item/passed',
1131 1125 $passed,
@@ -1132,9 +1126,9 @@
1132 1126 $item,
1133 1127 $item->get_status()
1134 1128 );
1135 1129 // if ( ! $item->is_preview() ) {
1136 - $total ++;
1130 + ++$total;
1137 1131 // }
1138 1132 }
1139 1133 }
1140 1134 }
@@ -1198,14 +1192,14 @@
1198 1192
1199 1193 /**
1200 1194 * Get courses only by course's user are learning
1201 1195 *
1202 - * @param LP_User_Items_Filter $filter
1196 + * @param LP_User_Items_Filter|UserItemsFilter $filter
1203 1197 * @param int $total_rows return total row query
1204 1198 *
1205 1199 * @return array|null|int|string
1206 1200 */
1207 - 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 ) {
1208 1202 try {
1209 1203 /*$key_cache = md5( json_encode( $filter ) );
1210 1204 $courses_cache = LP_Cache::instance()->get_cache( $key_cache );
1211 1205