PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.9
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.9
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 4.1.7.3.1 4.1.7.3.2 All 140 releases
← All changes | inc/user/abstract-lp-user.php +172 -1104 4.1.6.9.2 → 4.4.9 View file →
@@ -10,8 +10,11 @@
10 10
11 11 /**
12 12 * Prevent loading this file directly
13 13 */
14 +
15 +use LearnPress\Models\UserItems\UserCourseModel;
16 +
14 17 defined( 'ABSPATH' ) || exit();
15 18
16 19 if ( ! class_exists( 'LP_Abstract_User' ) ) {
17 20
@@ -79,9 +82,8 @@
79 82
80 83 if ( $this->get_id() > 0 ) {
81 84 $this->load();
82 85 }
83 -
84 86 }
85 87
86 88 /**
87 89 * Load user data from curd
@@ -95,14 +97,14 @@
95 97 *
96 98 * @param int $course_id .
97 99 *
98 100 * @return LP_User_Item_Course|bool
99 - * @version 3.1.3
100 - * @editor tungnx
101 + * @version 3.1.4
101 102 * @modify 4.1.3
102 103 */
103 104 public function get_course_data( int $course_id = 0 ) {
104 - $lp_user_items_db = LP_User_Items_DB::getInstance();
105 + $lp_user_items_db = LP_User_Items_DB::getInstance();
106 + $object_course_data = false;
105 107
106 108 try {
107 109 if ( ! $course_id ) {
108 110 $course_id = get_the_ID();
@@ -107,21 +109,14 @@
107 109 if ( ! $course_id ) {
108 110 $course_id = get_the_ID();
109 111 }
110 112
111 - /*if ( $this instanceof LP_User_Guest ) {
112 - throw new Exception( 'User is Guest' );
113 - }*/
114 -
115 - $filter = new LP_User_Items_Filter();
116 - $filter->item_id = $course_id;
117 - $filter->user_id = $this->get_id();
118 - $last_user_course = $lp_user_items_db->get_last_user_course( $filter );
119 -
120 - if ( $last_user_course ) {
121 - $object_course_data = new LP_User_Item_Course( $last_user_course );
113 + $userCourseModel = UserCourseModel::find( $this->get_id(), $course_id, true );
114 + if ( $userCourseModel ) {
115 + $userCourseArr = (array) $userCourseModel;
116 + $userCourseArr['user_item_id'] = $userCourseModel->get_user_item_id();
117 + $object_course_data = new LP_User_Item_Course( $userCourseArr );
122 118 } else {
123 - $object_course_data = false;
124 119 /**
125 120 * Todo: some themes still not check false, so still use below code.\
126 121 * @editor tungnx 4.1.6.9
127 122 */
@@ -134,8 +129,28 @@
134 129 return $object_course_data;
135 130 }
136 131
137 132 /**
133 + * Get course attend of user not Guest.
134 + * Replace get_course_data method.
135 + *
136 + * @param int $course_id
137 + * @return false|UserCourseModel
138 + * @since 4.2.5
139 + * @version 1.0.1
140 + */
141 + public function get_course_attend( int $course_id = 0 ) {
142 + if ( $this instanceof LP_User_Guest ) {
143 + return false;
144 + }
145 +
146 + $filter = new LP_User_Items_Filter();
147 + $filter->item_id = $course_id;
148 + $filter->user_id = $this->get_id();
149 + return UserCourseModel::get_user_item_model_from_db( $filter );
150 + }
151 +
152 + /**
138 153 * Get user course item.
139 154 *
140 155 * @param int $item_id
141 156 * @param int $course_id
@@ -337,28 +352,8 @@
337 352 return $records;
338 353 }
339 354
340 355 /**
341 - * Count number of rows for an item in user-items
342 - *
343 - * @param int $item_id
344 - * @param int $course_id
345 - *
346 - * @return int
347 - * @editor tungnx
348 - * @modify 4.1.3 - comment - not user
349 - */
350 - /*public function count_item_archive( $item_id, $course_id = 0 ) {
351 - $count = 0;
352 -
353 - if ( $items = $this->get_item_archive( $item_id, $course_id ) ) {
354 - $count = sizeof( $items );
355 - }
356 -
357 - return $count;
358 - }*/
359 -
360 - /**
361 356 * Check quiz can retake?
362 357 *
363 358 * @param [type] $quiz_id
364 359 * @param [type] $course_id
@@ -470,18 +465,16 @@
470 465 $status = '';
471 466
472 467 if ( ! $course_id ) {
473 468 $course_id = get_the_ID();
469 + if ( ! $course_id ) {
470 + return $status;
471 + }
474 472 }
475 473
476 - if ( ! $course_id ) {
477 - return $status;
478 - }
479 -
480 474 $item = $this->get_item( $item_id, $course_id, true );
481 -
482 - if ( false !== $item ) {
483 - $status = $item['status'];
475 + if ( $item instanceof LP_User_Item ) {
476 + $status = $item->get_status();
484 477 }
485 478
486 479 return apply_filters( 'learn-press/user-item-status', $status, $item_id, $this->get_id(), $course_id );
487 480 }
@@ -486,153 +479,8 @@
486 479 return apply_filters( 'learn-press/user-item-status', $status, $item_id, $this->get_id(), $course_id );
487 480 }
488 481
489 482 /**
490 - * To rewrite get_item_status on abstract-lp-user.
491 - *
492 - * @throws Exception
493 - * @author tungnx
494 - */
495 - /*public function getItemStatus( $item_id, $course_id ) {
496 - $status = LP_User_Items_DB::getInstance()->get_item_status( $item_id, $course_id );
497 -
498 - return $status;
499 - }*/
500 -
501 - /**
502 - * Update viewing item data into database.
503 - *
504 - * @param int $item_id
505 - * @param int $course_id
506 - *
507 - * @return bool
508 - * @since 3.0.0
509 - * @version 4.0.1
510 - * @depecated 4.1.6.9
511 - */
512 - /*public function maybe_update_item( $item_id, $course_id ) {
513 - $return = false;
514 -
515 - try {
516 - $course_data = $this->get_course_data( $course_id );
517 -
518 - if ( $course_data ) {
519 - $item = $course_data->get_item( $item_id );
520 -
521 - if ( ! $item ) {
522 - $item = LP_User_Item::get_item_object( $item_id );
523 -
524 - if ( ! $item ) {
525 - return $return;
526 - }
527 -
528 - if ( $item instanceof LP_User_Item_Quiz ) {
529 - return $return;
530 - }
531 -
532 - $item->set_ref_id( $course_id );
533 - $item->set_parent_id( $course_data->get_user_item_id() );
534 -
535 - $return = $item->update();
536 - }
537 - }
538 - } catch ( Throwable $e ) {
539 - error_log( $e->getMessage() );
540 - }
541 -
542 - return $return;
543 - }*/
544 -
545 - /**
546 - * Get item user has accessed in last time.
547 - *
548 - * @param int $course_id
549 - * @param bool $permalink - Optional. TRUE will return permalink instead of ID.
550 - *
551 - * @return mixed
552 - * @depecated 4.1.6.9
553 - */
554 - public function get_current_item( $course_id, $permalink = false ) {
555 - _deprecated_function( __FUNCTION__, '4.1.6.9' );
556 - return 0;
557 - /*$course_data = $this->get_course_data( $course_id );
558 - if ( ! $course_data ) {
559 - return false;
560 - }
561 -
562 - $course = learn_press_get_course( $course_id );
563 - $id = learn_press_get_user_item_meta( $course_data->get_user_item_id(), '_current_item' );
564 - if ( ! $id || $this->has_completed_item( $id, $course_id ) ) {
565 - $items = $course->get_items( '', false );
566 - if ( $items ) {
567 - foreach ( $items as $item_id ) {
568 - if ( ! $this->has_completed_item( $item_id, $course_id ) ) {
569 - $id = $item_id;
570 - break;
571 - }
572 - }
573 -
574 - if ( ! $id ) {
575 - $id = reset( $items );
576 - }
577 - }
578 -
579 - if ( $id ) {
580 - learn_press_update_user_item_meta( $course_data->get_user_item_id(), '_current_item', $id );
581 - }
582 - }
583 -
584 - if ( $permalink && $id ) {
585 - return apply_filters(
586 - 'learn-press/current-course-item-permalink',
587 - $course->get_item_link( $id ),
588 - $course_id,
589 - $this->get_id()
590 - );
591 - } else {
592 - return apply_filters( 'learn-press/current-course-item', $id, $course_id, $this->get_id() );
593 - }*/
594 - }
595 -
596 - /**
597 - * Get current question's ID/Permalink inside quiz.
598 - *
599 - * @param int $quiz_id
600 - * @param int $course_id
601 - * @param bool $permalink
602 - *
603 - * @return bool|int|string
604 - * @depecated 4.1.6.9
605 - */
606 - /*public function get_current_question( $quiz_id, $course_id, $permalink = false ) {
607 - _deprecated_function( sprintf( '%s::%s', __CLASS__, __FUNCTION__ ), '4.0.0' );
608 - }*/
609 -
610 - /**
611 - * Get previous Question
612 - *
613 - * @param null $quiz_id
614 - * @param int $course_id
615 - * @param false $permalink
616 - * @depecated 4.1.6.9
617 - */
618 - /*public function get_prev_question( $quiz_id = null, $course_id = 0, $permalink = false ) {
619 - _deprecated_function( sprintf( '%s::%s', __CLASS__, __FUNCTION__ ), '4.0.0' );
620 - }*/
621 -
622 - /**
623 - * Get next Question
624 - *
625 - * @param null $quiz_id
626 - * @param int $course_id
627 - * @param false $permalink
628 - * @depecated 4.1.6.9
629 - */
630 - /*public function get_next_question( $quiz_id = null, $course_id = 0, $permalink = false ) {
631 - _deprecated_function( sprintf( '%s::%s', __CLASS__, __FUNCTION__ ), '4.0.0' );
632 - }*/
633 -
634 - /**
635 483 * Checks if has status of a quiz for user
636 484 *
637 485 * @param string|array $statuses
638 486 * @param int $quiz_id
@@ -690,293 +538,8 @@
690 538 return $result;
691 539 }
692 540
693 541 /**
694 - * Mark question that user has checked.
695 - *
696 - * @param int $question_id
697 - * @param int $quiz_id
698 - * @param int $course_id
699 - *
700 - * @return WP_Error|mixed
701 - * @since 3.0.0
702 - * @editor tungnx
703 - * @modify 4.1.4.1 - comment - not use
704 - */
705 - /*public function check_question( $question_id, $quiz_id, $course_id ) {
706 - if ( ! $course = learn_press_get_course( $course_id ) ) {
707 - return false;
708 - }
709 -
710 - if ( ! $course->has_item( $quiz_id ) ) {
711 - return false;
712 - }
713 -
714 - $quiz = $course->get_item( $quiz_id );
715 -
716 - if ( ! $quiz->has_question( $question_id ) ) {
717 - return false;
718 - }
719 -
720 - $quiz_data = $this->get_item_data( $quiz_id, $course_id );
721 -
722 - return $quiz_data->check_question( $question_id );
723 - }*/
724 -
725 - /**
726 - * Mark question that user has checked.
727 - *
728 - * @param int $question_id
729 - * @param int $quiz_id
730 - * @param int $course_id
731 - *
732 - * @return WP_Error|mixed
733 - * @since 3.0.0
734 - */
735 - public function hint( $question_id, $quiz_id, $course_id ) {
736 - $course = learn_press_get_course( $course_id );
737 - if ( ! $course ) {
738 - return false;
739 - }
740 -
741 - if ( ! $course->has_item( $quiz_id ) ) {
742 - return false;
743 - }
744 -
745 - /**
746 - * @var $quiz LP_Quiz
747 - */
748 - $quiz = $course->get_item( $quiz_id );
749 - if ( ! $quiz instanceof LP_Course_Item_Quiz ) {
750 - return false;
751 - }
752 -
753 - if ( ! $quiz->has_question( $question_id ) ) {
754 - return false;
755 - }
756 -
757 - $quiz_data = $this->get_item_data( $quiz_id, $course_id );
758 - $remain = $quiz_data->hint( $question_id );
759 - if ( false === $remain ) {
760 - return new WP_Error( 1001, __( 'You can not hint question.', 'learnpress' ) );
761 - }
762 -
763 - return $remain;
764 - }
765 -
766 - /**
767 - * Return true if check answer is enabled.
768 - *
769 - * @param int $quiz_id
770 - * @param int $course_id
771 - *
772 - * @return bool
773 - * @deprecated 4.1.4.1
774 - */
775 - /*public function can_check_answer( $quiz_id, $course_id = 0 ) {
776 - _deprecated_function( __FUNCTION__, '4.1.4.1' );
777 - if ( ! $course_id ) {
778 - $course_id = get_the_ID();
779 - }
780 -
781 - if ( $quiz_data = $this->get_item_data( $quiz_id, $course_id ) ) {
782 - return $quiz_data->can_check_answer();
783 - }
784 -
785 - return false;
786 - }*/
787 -
788 - /**
789 - * Return true if check answer is enabled.
790 - *
791 - * @param int $quiz_id
792 - * @param int $course_id
793 - *
794 - * @return bool
795 - * @depecated 4.1.6.9
796 - */
797 - /*public function can_hint_answer( $quiz_id, $course_id = 0 ) {
798 -
799 - if ( ! $course_id ) {
800 - $course_id = get_the_ID();
801 - }
802 -
803 - if ( $quiz_data = $this->get_item_data( $quiz_id, $course_id ) ) {
804 - return $quiz_data->can_hint_answer();
805 - }
806 -
807 - return false;
808 - }*/
809 -
810 -
811 - public function get_quiz_last_results( $quiz_id ) {
812 - $results = $this->get_course_info( $quiz_id );
813 - if ( $results ) {
814 - $results = reset( $results );
815 - }
816 -
817 - return apply_filters( 'learn_press_user_quiz_last_results', $results, $quiz_id, $this );
818 - }
819 -
820 - /**
821 - * Get history of a quiz for an user
822 - *
823 - * @param int $quiz_id
824 - * @param int $course_id
825 - * @param int $history_id
826 - * @param bool $force
827 - *
828 - * @return mixed|null|void
829 - * @editor tungnx
830 - * @modify 4.1.4.1 - comment - not use
831 - */
832 - /*public function get_quiz_history( $quiz_id, $course_id = 0, $history_id = null, $force = false ) {
833 - $course_id = $this->_get_course( $course_id );
834 -
835 - $course = learn_press_get_course( $course_id );
836 - if ( $course ) {
837 - $quizzes = $course->get_quizzes( 'ID' );
838 - } else {
839 - $quizzes = array();
840 - }
841 - $key = $this->get_id() . '-' . $course_id . '-' . $quiz_id;
842 -
843 - $cached = LP_Cache::get_quiz_history( false, array() );
844 -
845 - if ( ( ! array_key_exists( $key, $cached ) || $force ) && $quizzes && in_array( $quiz_id, $quizzes ) ) {
846 - global $wpdb;
847 - $t1 = $wpdb->learnpress_user_items;
848 - $t2 = $wpdb->learnpress_user_itemmeta;
849 - $in = array_fill( 0, sizeof( $quizzes ), '%d' );
850 - $prepare_params = array_merge(
851 - array( 'lp_quiz', $this->get_id(), $course_id ),
852 - $quizzes
853 - );
854 - $query = $wpdb->prepare(
855 - "
856 - SELECT *
857 - FROM $t1 uq
858 - WHERE uq.item_type = %s
859 - AND uq.user_id = %d
860 - AND uq.ref_id = %d
861 - AND uq.item_id IN(" . join( ',', $in ) . ')
862 - ORDER BY uq.user_item_id DESC
863 - ',
864 - $prepare_params
865 - );
866 -
867 - $history = array();
868 - foreach ( $quizzes as $_quiz_id ) {
869 - $history[ $this->get_id() . '-' . $course_id . '-' . $_quiz_id ] = array();
870 - }
871 - if ( $results = $wpdb->get_results( $query ) ) {
872 - $item_ids = array();
873 - foreach ( $results as $result ) {
874 - $item_ids[] = $result->user_item_id;
875 - $cache_key = $this->get_id() . '-' . $course_id . '-' . $result->item_id;
876 - if ( empty( $history[ $cache_key ] ) ) {
877 - $history[ $cache_key ] = array();
878 - }
879 - // limit newest 10 items
880 - if ( sizeof( $history[ $cache_key ] ) >= 10 ) {
881 - // break;
882 - }
883 -
884 - $history[ $cache_key ][ $result->user_item_id ] = (object) array(
885 - 'history_id' => $result->user_item_id,
886 - 'start' => $result->start_time,
887 - 'end' => $result->end_time,
888 - 'status' => $result->status,
889 - 'question' => '',
890 - 'questions' => array(),
891 - 'question_answers' => array(),
892 - );
893 - }
894 - if ( $item_ids && $meta = $this->_get_quiz_meta( $item_ids ) ) {
895 - $maps = array(
896 - 'questions' => 'questions',
897 - 'current_question' => 'question',
898 - 'question_answers' => 'question_answers',
899 - 'question_checked' => 'question_checked',
900 - );
901 - foreach ( $meta as $k => $v ) {
902 - $_key = $this->get_id() . '-' . $course_id . '-' . $v->item_id;
903 - if ( empty( $history[ $_key ] ) ) {
904 - continue;
905 - }
906 - if ( empty( $history[ $_key ][ $v->user_item_id ] ) ) {
907 - continue;
908 - }
909 - $obj_key = ! empty( $maps[ $v->meta_key ] ) ? $maps[ $v->meta_key ] : $v->meta_key;
910 - if ( ! $obj_key ) {
911 - continue;
912 - }
913 - $history[ $_key ][ $v->user_item_id ]->{$obj_key} = LP_Helper::maybe_unserialize( $v->meta_value );
914 - }
915 - }
916 - }
917 -
918 - if ( $history ) {
919 - foreach ( $history as $k1 => $v1 ) {
920 - if ( empty( $cached[ $k1 ] ) ) {
921 - $cached[ $k1 ] = $v1;
922 - continue;
923 - }
924 - foreach ( $v1 as $k2 => $v2 ) {
925 - $cached[ $k1 ][ $k2 ] = $v2;
926 - }
927 - }
928 - }
929 - LP_Cache::set_quiz_history( $cached );
930 - }
931 -
932 - return apply_filters(
933 - 'learn_press_user_quiz_history',
934 - isset( $cached[ $key ] ) ? $cached[ $key ] : array(),
935 - $this,
936 - $quiz_id
937 - );
938 - }*/
939 -
940 - /**
941 - * @editor tungnx
942 - * @modify 4.1.4.1 - comment - not use
943 - */
944 - /*private function _get_quiz_meta( $user_item_id ) {
945 - global $wpdb;
946 - settype( $user_item_id, 'array' );
947 - $in = array_fill( 0, sizeof( $user_item_id ), '%d' );
948 -
949 - $query = $wpdb->prepare(
950 - "
951 - SELECT learnpress_user_item_id as user_item_id, meta_key, meta_value, item_id
952 - FROM {$wpdb->prefix}learnpress_user_itemmeta im
953 - INNER JOIN {$wpdb->prefix}learnpress_user_items i ON i.user_item_id = im.learnpress_user_item_id
954 - WHERE learnpress_user_item_id IN(" . join( ',', $in ) . ')
955 - ',
956 - $user_item_id
957 - );
958 -
959 - return $wpdb->get_results( $query );
960 - }*/
961 -
962 - /**
963 - * @editor tungnx
964 - * @modify 4.1.4.1 comment - not use
965 - */
966 - /*public function get_current_results( $quiz_id, $course_id = 0 ) {
967 - $course_id = $this->_get_course( $course_id );
968 -
969 - $history = $this->get_quiz_history( $quiz_id, $course_id );
970 - $current = false;
971 - if ( $history ) {
972 - $current = reset( $history );
973 - }
974 -
975 - return $current;
976 - }*/
977 -
978 - /**
979 542 * Check if user has at least one role.
980 543 *
981 544 * @param array|string $roles
982 545 *
@@ -1008,30 +571,8 @@
1008 571
1009 572 return $is;
1010 573 }
1011 574
1012 - /**
1013 - *
1014 - * Check what the user can do
1015 - *
1016 - * @param $role
1017 - *
1018 - * @return mixed
1019 - * @throws Exception
1020 - */
1021 - public function can( $role ) {
1022 - _deprecated_function( __FUNCTION__, '3.0.8' );
1023 - $args = func_get_args();
1024 - unset( $args[0] );
1025 - $method = 'can_' . preg_replace( '!-!', '_', $role );
1026 - $callback = array( $this, $method );
1027 - if ( is_callable( $callback ) ) {
1028 - return call_user_func_array( $callback, $args );
1029 - } else {
1030 - throw new Exception( sprintf( __( 'The role %s for user doesn\'t exist', 'learnpress' ), $role ) );
1031 - }
1032 - }
1033 -
1034 575 public function can_edit_item( $item_id, $course_id = 0 ) {
1035 576 $return = $this->is_admin();
1036 577
1037 578 if ( ! $return ) {
@@ -1046,24 +587,8 @@
1046 587 return apply_filters( 'learn_press_user_can_edit_item', $return, $item_id, $course_id, $this->get_id() );
1047 588 }
1048 589
1049 590 /**
1050 - * Check if user can finish course by getting current progress
1051 - * and compares with course passing condition.
1052 - *
1053 - * @param int $course_id
1054 - *
1055 - * @return bool
1056 - * @editor tungnx
1057 - * @modify 4.1.3
1058 - */
1059 - public function can_finish_course( int $course_id ) {
1060 - _deprecated_function( __FUNCTION__, '4.1.3', 'is_course_finished' );
1061 -
1062 - return true;
1063 - }
1064 -
1065 - /**
1066 591 * Check if course has any passed status for an user.
1067 592 * Statuses: depending on value of column `status` in user_items.
1068 593 * - purchased: bought and order is completed, `start_date` and `end_date` is null
1069 594 * - enrolled: value of column `status` in user_items is enrolled
@@ -1088,17 +613,8 @@
1088 613 return false;
1089 614 }
1090 615
1091 616 /**
1092 - * @depecated 4.1.6.9.1
1093 - */
1094 - /*public function get_completed_items( $course_id ) {
1095 - $this->_curd->get_user_items( $this->get_id(), $course_id );
1096 -
1097 - return $this->_curd->get_user_completed_items( $this->get_id(), $course_id );
1098 - }*/
1099 -
1100 - /**
1101 617 * Finish course
1102 618 *
1103 619 * @param int $course_id
1104 620 *
@@ -1109,8 +625,11 @@
1109 625 $course = learn_press_get_course( $course_id );
1110 626
1111 627 if ( $course ) {
1112 628 $user_course = $this->get_course_data( $course_id );
629 + if ( ! $user_course ) {
630 + return $return;
631 + }
1113 632
1114 633 $result = $user_course->calculate_course_results();
1115 634
1116 635 // Save result for course
@@ -1122,13 +641,24 @@
1122 641 $graduation = LP_COURSE_GRADUATION_FAILED;
1123 642 }
1124 643
1125 644 $user_course->set_graduation( $graduation );
1126 - $user_course->save();
645 + //$user_course->save();
646 + $return = $user_course->complete( LP_COURSE_FINISHED );
1127 647
1128 - $return = $user_course->complete( 'finished' );
648 + if ( $return ) {
649 + /**
650 + * LP_User_Item::complete() writes straight to the table, so it never runs
651 + * UserCourseModel::clean_caches() the way UserItemModel::save() does. The
652 + * cached model still reports the enrollment as in progress, and that cache
653 + * outlives the request wherever a persistent object cache is installed - the
654 + * course then reads back unfinished and keeps offering "Finish course".
655 + */
656 + $userCourseModel = UserCourseModel::find( $this->get_id(), $course_id );
657 + if ( $userCourseModel instanceof UserCourseModel ) {
658 + $userCourseModel->clean_caches();
659 + }
1129 660
1130 - if ( $return ) {
1131 661 do_action( 'learn-press/user-course-finished', $course_id, $this->get_id(), $return );
1132 662 }
1133 663 }
1134 664
@@ -1174,22 +704,8 @@
1174 704 public function is_author_of( $post_id ) {
1175 705 return absint( get_post_field( 'post_author', $post_id ) ) === $this->get_id();
1176 706 }
1177 707
1178 - public function has( $role ) {
1179 - _deprecated_function( __FUNCTION__, '3.0.8' );
1180 -
1181 - $args = func_get_args();
1182 - unset( $args[0] );
1183 - $method = 'has_' . preg_replace( '!-!', '_', $role );
1184 - $callback = array( $this, $method );
1185 - if ( is_callable( $callback ) ) {
1186 - return call_user_func_array( $callback, $args );
1187 - } else {
1188 - throw new Exception( sprintf( __( 'The role %s for user doesn\'t exist', 'learnpress' ), $role ) );
1189 - }
1190 - }
1191 -
1192 708 public function get( $role ) {
1193 709 $args = func_get_args();
1194 710 unset( $args[0] );
1195 711 $method = 'get_' . preg_replace( '!-!', '_', $role );
@@ -1196,9 +712,9 @@
1196 712 $callback = array( $this, $method );
1197 713 if ( is_callable( $callback ) ) {
1198 714 return call_user_func_array( $callback, $args );
1199 715 } else {
1200 - throw new Exception( sprintf( __( 'The role %s for user doesn\'t exist', 'learnpress' ), $role ) );
716 + throw new Exception( sprintf( __( 'The role %s for the user doesn\'t exist', 'learnpress' ), $role ) );
1201 717 }
1202 718 }
1203 719
1204 720 /**
@@ -1210,13 +726,9 @@
1210 726 */
1211 727 public function has_passed_course( $course_id ) {
1212 728 $user_course = $this->get_course_data( $course_id );
1213 729
1214 - if ( $user_course && $user_course->is_passed() ) {
1215 - return true;
1216 - } else {
1217 - return false;
1218 - }
730 + return $user_course && $user_course->is_passed();
1219 731 }
1220 732
1221 733 /**
1222 734 * Checks if user has started a quiz
@@ -1242,9 +754,9 @@
1242 754 * @param int $quiz_id
1243 755 * @param int $course_id
1244 756 *
1245 757 * @return bool
1246 - * @depecated 4.1.6.9
758 + * @deprecated 4.1.6.9
1247 759 */
1248 760 /*public function has_completed_quiz( $quiz_id, $course_id = 0 ): bool {
1249 761 return $this->get_item_status( $quiz_id, $course_id ) == 'completed';
1250 762 }*/
@@ -1253,48 +765,43 @@
1253 765 * Mark a lesson is completed for user
1254 766 *
1255 767 * @param int $lesson_id
1256 768 * @param int $course_id
1257 - * @param bool $return_wp_error
1258 769 *
1259 770 * @return bool|WP_Error
1260 771 */
1261 - public function complete_lesson( $lesson_id, $course_id = 0, $return_wp_error = true ) {
772 + public function complete_lesson( $lesson_id = 0, $course_id = 0 ) {
773 + $result = true;
774 +
1262 775 try {
1263 - $course_id = $this->_get_course( $course_id );
776 + $course = learn_press_get_course( $course_id );
777 + if ( ! $course ) {
778 + throw new Exception( __( 'Invalid course', 'learnpress' ) );
779 + }
1264 780
1265 781 $course_data = $this->get_course_data( $course_id );
782 + if ( ! $course_data ) {
783 + throw new Exception( __( 'You must enroll course!', 'learnpress' ) );
784 + }
1266 785
1267 - $result = false;
1268 -
1269 786 /**
1270 787 * If user has stared a lesson, get user lesson information
1271 788 */
1272 789 $item = $course_data->get_item( $lesson_id );
1273 - if ( $item ) {
1274 - if ( $item->is_completed() ) {
1275 - throw new Exception(
1276 - __( 'You have already completed this lesson.', 'learnpress' ),
1277 - LP_COMPLETE_ITEM_FAIL
1278 - );
1279 - }
790 + if ( ! $item ) {
791 + throw new Exception( __( 'Invalid lesson', 'learnpress' ) );
792 + }
1280 793
1281 - $item->set_end_time( current_time( 'mysql', 1 ) );
1282 - $item->set_status( 'completed' );
1283 - $item->set_graduation( 'passed' );
794 + if ( $item->is_completed() ) {
795 + throw new Exception( __( 'You have already completed this lesson.', 'learnpress' ) );
796 + }
1284 797
1285 - $updated = $item->update();
798 + $item->set_graduation( 'passed' );
799 + $updated = $item->complete();
1286 800
1287 - if ( is_wp_error( $updated ) ) {
1288 - return $return_wp_error ? $updated : false;
1289 - } else {
1290 - $result = true;
1291 - }
1292 - }
1293 -
1294 801 do_action( 'learn-press/user-completed-lesson', $lesson_id, $course_id, $this->get_id() );
1295 - } catch ( Exception $ex ) {
1296 - $result = $return_wp_error ? new WP_Error( $ex->getCode(), $ex->getMessage() ) : false;
802 + } catch ( Throwable $e ) {
803 + $result = new WP_Error( 'error_lesson_complete', $e->getMessage() );
1297 804 }
1298 805
1299 806 return $result;
1300 807 }
@@ -1305,9 +812,9 @@
1305 812 * @param int $lesson_id Lesson id.
1306 813 * @param null $course_id Course id.
1307 814 *
1308 815 * @return bool
1309 - * @depecated 4.1.6.9
816 + * @deprecated 4.1.6.9
1310 817 */
1311 818 public function has_completed_lesson( $lesson_id = 0, $course_id = null ): bool {
1312 819 return 'completed' === $this->get_item_status( $lesson_id, $course_id );
1313 820 }
@@ -1333,9 +840,9 @@
1333 840 /**
1334 841 * @param $course_id
1335 842 *
1336 843 * @return int
1337 - * @depecated 4.1.6.9
844 + * @deprecated 4.1.6.9
1338 845 */
1339 846 public function get_course_history_id( $course_id ) {
1340 847 $history = $this->get_course_info( $course_id );
1341 848
@@ -1370,216 +877,8 @@
1370 877 return apply_filters( 'learn-press/user-course-status', $status, $course_id, $this->get_id() );
1371 878 }
1372 879
1373 880 /**
1374 - * Controls what this user can do with a course.
1375 - *
1376 - * 0 => No accessible
1377 - * 10 => Normal users (like not logged in)
1378 - * 20 => Author of course
1379 - * 30 => Admin site
1380 - * 35 => No require enrollment
1381 - * 40 => Ordered but not completed
1382 - * 50 => Order is completed but not enrolled
1383 - * 60 => User has already enrolled course
1384 - * 70 => User has already finished course
1385 - *
1386 - * @param int $course_id
1387 - *
1388 - * @return int
1389 - * @since 3.1.0
1390 - * @editor tungnx
1391 - * @modify 4.1.3 - comment - not use
1392 - */
1393 - /*public function get_course_access_level( $course_id ) {
1394 - $access_level = LP_Object_Cache::get(
1395 - 'course-' . $course_id . '-' . $this->get_id(),
1396 - 'learn-press/course-access-levels'
1397 - );
1398 -
1399 - if ( false === $access_level ) {
1400 - $course = learn_press_get_course( $course_id );
1401 -
1402 - if ( ! $course ) {
1403 - $access_level = LP_COURSE_ACCESS_LEVEL_0;
1404 - } elseif ( $this->is_admin() ) {
1405 - $access_level = LP_COURSE_ACCESS_LEVEL_30;
1406 - } elseif ( $this->is_author_of( $course_id ) ) {
1407 - $access_level = LP_COURSE_ACCESS_LEVEL_20;
1408 - } else {
1409 - $access_level = LP_COURSE_ACCESS_LEVEL_10;
1410 - }
1411 -
1412 - // Default level
1413 - $access_level = apply_filters(
1414 - 'learn-press/course-access-level-default',
1415 - $access_level,
1416 - $course_id,
1417 - $this->get_id()
1418 - );
1419 - $course_data = $this->get_course_data( $course_id );
1420 -
1421 - if ( $course_data && $course_data->get_user_item_id() ) {
1422 - // if ( $course_data->get_access_level() >= 50 ) {
1423 - switch ( $course_data->get_status() ) {
1424 - case 'completed':
1425 - case 'failed':
1426 - $access_level = LP_COURSE_ACCESS_LEVEL_60;
1427 - break;
1428 - case 'in-progress':
1429 - case 'enrolled':
1430 - $access_level = LP_COURSE_ACCESS_LEVEL_70;
1431 - break;
1432 - }
1433 - // }
1434 - } else {
1435 - $order = $this->get_course_order( $course_id );
1436 -
1437 - if ( $order ) {
1438 - switch ( $order->get_status() ) {
1439 - case 'completed':
1440 - $access_level = LP_COURSE_ACCESS_LEVEL_50;
1441 - break;
1442 - default:
1443 - $access_level = LP_COURSE_ACCESS_LEVEL_40;
1444 - }
1445 - }
1446 - }
1447 -
1448 - LP_Object_Cache::set(
1449 - 'course-' . $course_id . '-' . $this->get_id(),
1450 - $access_level,
1451 - 'learn-press/course-access-levels'
1452 - );
1453 - }
1454 -
1455 - return apply_filters( 'learn-press/course-access-level', $access_level, $course_id, $this->get_id() );
1456 - }*/
1457 -
1458 - /**
1459 - * @editor tungnx
1460 - * @reason comment - not use
1461 - * @modify 4.1.2
1462 - */
1463 - /*public function get_item_access_level( $item_id, $course_id ) {
1464 - $access_level = 0;
1465 -
1466 - if ( $course = learn_press_get_course( $course_id ) ) {
1467 - if ( $course->has_item( $item_id ) ) {
1468 - if ( 10 < $this->get_course_access_level( $course_id ) ) {
1469 - $access_level = 10;
1470 - } else {
1471 - $item = $course->get_item( $item_id );
1472 - if ( $item->is_preview() ) {
1473 - $access_level = 10;
1474 - }
1475 - }
1476 - }
1477 - }
1478 -
1479 - return apply_filters(
1480 - 'learn-press/course-item-access-level',
1481 - $access_level,
1482 - $item_id,
1483 - $course_id,
1484 - $this->get_id()
1485 - );
1486 - }*/
1487 -
1488 - /**
1489 - * Set new access-level of an user with a course.
1490 - *
1491 - * @param int $access_level
1492 - * @param int $course_id
1493 - *
1494 - * @return mixed
1495 - * @since 3.1.0
1496 - * @editor tungnx
1497 - * @reason comment - not use
1498 - * @modify 4.1.2
1499 - */
1500 - /*public function set_course_access_level( $access_level, $course_id ) {
1501 - if ( $access_level !== $this->get_course_access_level( $course_id ) ) {
1502 - LP_Object_Cache::set(
1503 - 'course-' . $course_id . '-' . $this->get_id(),
1504 - $access_level,
1505 - 'learn-press/course-access-levels'
1506 - );
1507 - }
1508 -
1509 - return $access_level;
1510 - }*/
1511 -
1512 - /**
1513 - * Check if user have an access-level.
1514 - * Consider the passed access-level is max level user have.
1515 - *
1516 - * @param int[] $access_level
1517 - * @param int $course_id
1518 - * @param string $compare
1519 - *
1520 - * @return bool
1521 - * @since 3.1.0
1522 - * @editor tungnx
1523 - * @modify 4.1.3 - not - use
1524 - */
1525 - /*public function has_course_access_level( $access_level, $course_id, $compare = '<=' ) {
1526 - $user_access_level = $this->get_course_access_level( $course_id );
1527 -
1528 - switch ( $compare ) {
1529 - case 'any':
1530 - settype( $access_level, 'array' );
1531 - $has = in_array( $user_access_level, $access_level );
1532 - break;
1533 - default:
1534 - $has = version_compare( $user_access_level, $access_level );
1535 - }
1536 -
1537 - return $has;
1538 - }*/
1539 -
1540 - /**
1541 - * Check if user has an access-level with a course.
1542 - *
1543 - * @param int $access_level
1544 - * @param int $course_id
1545 - *
1546 - * @return bool
1547 - * @since 3.1.0
1548 - * @editor tungnx
1549 - * @modify 4.1.2
1550 - * @reason comment - not use
1551 - */
1552 - /*public function is_access_level( $access_level, $course_id ) {
1553 - $user_access_level = $this->get_course_access_level( $course_id );
1554 -
1555 - return $user_access_level === $access_level;
1556 - }*/
1557 -
1558 - /**
1559 - * Check if user is already ordered a course.
1560 - *
1561 - * @param int $course_id
1562 - *
1563 - * @return mixed|LP_Order
1564 - * @editor tungnx
1565 - * @modify 4.1.3 - comment - not use
1566 - */
1567 - /*public function has_ordered_course( $course_id ) {
1568 - $return = apply_filters(
1569 - 'learn-press/user-has-ordered-course',
1570 - $this->get_course_order( $course_id ),
1571 - $course_id,
1572 - $this->get_id()
1573 - );
1574 -
1575 - // Deprecated since 3.0.0
1576 - $return = apply_filters( 'learn_press_user_has_ordered_course', $return, $course_id, $this->get_id() );
1577 -
1578 - return $return;
1579 - }*/
1580 -
1581 - /**
1582 881 * Get order status of a course.
1583 882 *
1584 883 * @param int $course_id
1585 884 *
@@ -1606,16 +905,19 @@
1606 905 return $order_status;
1607 906 }
1608 907
1609 908 /**
909 + * Check item completed.
910 + *
1610 911 * @param $item
1611 912 * @param int $course_id
1612 913 * @param bool $force
1613 914 *
1614 915 * @return mixed|void
916 + * @version 3.0.1
917 + * @since 3.0.0
1615 918 */
1616 919 public function has_completed_item( $item, $course_id = 0, $force = false ) {
1617 -
1618 920 $course_id = $this->_get_course( $course_id );
1619 921
1620 922 $return = false;
1621 923 $item_id = 0;
@@ -1626,21 +928,11 @@
1626 928 if ( ! empty( $item['ID'] ) ) {
1627 929 $item_id = absint( $item['ID'] );
1628 930 }
1629 931 }
932 +
1630 933 if ( $item_id ) {
1631 - if ( empty( $item['item_type'] ) ) {
1632 - $type = learn_press_get_post_type( $item_id );
1633 - } else {
1634 - $type = $item['item_type'];
1635 - }
1636 - if ( ! $type ) {
1637 - $type = learn_press_get_post_type( $item_id );
1638 - }
1639 -
1640 - if ( $type == 'lp_lesson' || $type == 'lp_quiz' ) {
1641 - $return = 'completed' === $this->get_item_status( $item_id, $course_id );
1642 - }
934 + $return = 'completed' === $this->get_item_status( $item_id, $course_id );
1643 935 }
1644 936
1645 937 return apply_filters( 'learn_press_user_has_completed_item', $return, $item );
1646 938 }
@@ -1645,29 +937,8 @@
1645 937 return apply_filters( 'learn_press_user_has_completed_item', $return, $item );
1646 938 }
1647 939
1648 940 /**
1649 - * Get the remaining time of a course for the user.
1650 - *
1651 - * @param int $course_id
1652 - *
1653 - * @return bool|int|string
1654 - */
1655 - public function get_course_remaining_time( $course_id ) {
1656 - $course = learn_press_get_course( $course_id );
1657 - $remain = false;
1658 -
1659 - if ( $course && $course->get_id() ) {
1660 - $course_data = $this->get_course_data( $course_id, true );
1661 - if ( $course_data ) {
1662 - $remain = $course_data->is_exceeded();
1663 - }
1664 - }
1665 -
1666 - return $remain > 0 ? learn_press_seconds_to_weeks( $remain ) : false;
1667 - }
1668 -
1669 - /**
1670 941 * Get the order that contains the course.
1671 942 *
1672 943 * @param int $course_id
1673 944 *
@@ -1689,142 +960,8 @@
1689 960 return $lp_order;
1690 961 }
1691 962
1692 963 /**
1693 - * Enroll this user to a course.
1694 - *
1695 - * @param int $course_id
1696 - * @param int $order_id - Optional. An user can be enrolled to a course
1697 - *
1698 - * @return bool|WP_Error
1699 - * @throws Exception
1700 - * @since 3.3.0
1701 - * @editor tungnx
1702 - * @version 3.3.1
1703 - * @modify 4.1.3 - comment - not use
1704 - */
1705 - // public function enroll_course( int $course_id = 0, int $order_id = 0 ) {
1706 - // $lp_user_items_db = LP_User_Items_DB::getInstance();
1707 - //
1708 - // try {
1709 - // /*$user_item_api = new LP_User_Item_CURD();
1710 - // $find_query = array(
1711 - // 'item_id' => $course_id,
1712 - // 'user_id' => $this->get_id(),
1713 - // );
1714 - //
1715 - // if ( $order_id ) {
1716 - // $find_query['ref_id'] = $order_id;
1717 - // }*/
1718 - //
1719 - // $filter = new LP_User_Items_Filter();
1720 - // $filter->user_id = get_current_user_id();
1721 - // $filter->item_id = $course_id;
1722 - // $course_item = $lp_user_items_db->get_last_user_course( $filter );
1723 - //
1724 - // if ( ! $course_item ) {
1725 - // $course_item = LP_User_Item::get_empty_item();
1726 - // } else {
1727 - // $course_item = (array) $course_item;
1728 - // }
1729 - //
1730 - // $user_id = $this->get_id();
1731 - //
1732 - // $course_item['user_id'] = $user_id;
1733 - // $course_item['item_id'] = $course_id;
1734 - // $course_item['item_type'] = learn_press_get_post_type( $course_id );
1735 - // $course_item['ref_id'] = $order_id;
1736 - // $course_item['ref_type'] = ( $order_id != 0 ) ? learn_press_get_post_type( $order_id ) : '';
1737 - // $course_item['start_time'] = current_time( 'mysql', true );
1738 - // $course_item['access_level'] = 50;
1739 - //
1740 - // /**
1741 - // * @editor tungnx
1742 - // * @fixed: case no auto enroll
1743 - // */
1744 - // if ( 'yes' == LP_Settings::get_option( 'auto_enroll' ) ) {
1745 - // $course_item['graduation'] = 'in-progress';
1746 - // }
1747 - //
1748 - // $user_course = new LP_User_Item_Course( $course_item );
1749 - // $user_course->set_status( LP_COURSE_PURCHASED );
1750 - //
1751 - // if ( ! $user_course->update( true ) ) {
1752 - // throw new Exception( __( 'Update user item error.', 'learnpress' ) );
1753 - // }
1754 - //
1755 - // /*$user_id = is_user_logged_in() ? $this->get_id() : 0;
1756 - //
1757 - // global $wpdb;
1758 - // $query = $wpdb->prepare(
1759 - // "
1760 - // UPDATE {$wpdb->learnpress_user_items}
1761 - // SET access_level = %d
1762 - // WHERE user_id = %d
1763 - // AND item_id = %d
1764 - // AND user_item_id NOT IN(%d)
1765 - // ",
1766 - // 0,
1767 - // $user_id,
1768 - // $course_id,
1769 - // $user_course->get_user_item_id()
1770 - // );
1771 - // $wpdb->query( $query );*/
1772 - //
1773 - // $return = $user_course->get_user_item_id();
1774 - // } catch ( Exception $ex ) {
1775 - // error_log( $ex->getMessage() );
1776 - // return false;
1777 - // }
1778 - //
1779 - // return $return;
1780 - // }
1781 -
1782 - /**
1783 - * Enroll this user to a course.
1784 - *
1785 - * @param int $course_id
1786 - * @param int $order_id
1787 - * @param bool $force - Optional. Force create db record for preview quiz case
1788 - * @param bool $wp_error - Optional. TRUE will return WP_Error object if there is an error.
1789 - * @editor tungnx - comment - not use
1790 - * @return bool|mixed|WP_Error
1791 - */
1792 - /*public function enroll( $course_id, $order_id, $force = false, $wp_error = false ) {
1793 - global $wpdb;
1794 -
1795 - _deprecated_function( __FUNCTION__, '4.1.0' );
1796 -
1797 - try {
1798 - $course = learn_press_get_course( $course_id );
1799 - $user_id = $this->get_id();
1800 -
1801 - if ( $course->is_required_enroll() && ! $force ) {
1802 - $order = learn_press_get_order( $order_id );
1803 -
1804 - if ( ! $order ) {
1805 - throw new Exception( __( 'Failed to enroll course.', 'learnpress' ), 10000 );
1806 - }
1807 -
1808 - if ( ! $this->can_enroll_course( $course_id ) ) {
1809 - throw new Exception( __( 'Failed to enroll course.', 'learnpress' ), 10001 );
1810 - }
1811 -
1812 - if ( ! $this->get_id() ) {
1813 - throw new Exception( __( 'Please login to enroll course.', 'learnpress' ), 10002 );
1814 - }
1815 - }
1816 -
1817 - $return = $this->enroll_course( $course_id, $order_id, false, $wp_error );
1818 -
1819 - return $return;
1820 -
1821 - } catch ( Exception $ex ) {
1822 - return new WP_Error( $ex->getCode(), $ex->getMessage() );
1823 - }
1824 - }*/
1825 -
1826 - /**
1827 964 * @param $question_id
1828 965 *
1829 966 * @return null|string
1830 967 */
@@ -1843,89 +980,8 @@
1843 980 return $wpdb->get_var( $query );
1844 981 }
1845 982
1846 983 /**
1847 - * @param $question_id
1848 - * @param null $quiz_id
1849 - *
1850 - * @return bool
1851 - */
1852 - public function get_answer_results( $question_id, $quiz_id = null ) {
1853 -
1854 - _deprecated_function( __CLASS__ . '::' . __FUNCTION__, '3.0.0' );
1855 -
1856 - $data = false;
1857 - if ( ! $quiz_id ) {
1858 - $quiz_id = $this->get_quiz_by_question( $question_id );
1859 - }
1860 - if ( $quiz_id ) {
1861 - $question = LP_Question::get_question( $question_id );
1862 -
1863 - if ( $question ) {
1864 - $quiz_results = $this->get_quiz_results( $quiz_id );
1865 - if ( ! empty( $quiz_results->question_answers ) ) {
1866 - $question_answer = array_key_exists(
1867 - $question_id,
1868 - $quiz_results->question_answers
1869 - ) ? $quiz_results->question_answers[ $question_id ] : null;
1870 - $data = $question->check( $question_answer );
1871 - }
1872 - }
1873 - }
1874 -
1875 - return $data;
1876 - }
1877 -
1878 - /**
1879 - * @param $question_id
1880 - * @param null $quiz_id
1881 - *
1882 - * @return bool
1883 - */
1884 - public function is_answered_question( $question_id, $quiz_id = null ) {
1885 - if ( empty( $this->answered_questions ) ) {
1886 - $this->answered_questions = array();
1887 - }
1888 - if ( ! $quiz_id ) {
1889 - $quiz_id = $this->get_quiz_by_question( $question_id );
1890 - }
1891 -
1892 - $results = $this->get_quiz_results( $quiz_id );
1893 - $answered = ! empty( $results->question_answers ) ? $results->question_answers : array();
1894 -
1895 - return $answered ? array_key_exists( $question_id, $answered ) : false;
1896 - }
1897 -
1898 - /**
1899 - * @param array $args
1900 - *
1901 - * @return LP_Query_List_Table
1902 - * @editor tungnx
1903 - * @deprecated 4.1.6
1904 - */
1905 - /*public function get_purchased_courses( array $args = array() ): LP_Query_List_Table {
1906 - $filter = new LP_User_Items_Filter();
1907 - $filter->fields = array( 'item_id' );
1908 - $filter->user_id = $this->get_id();
1909 - $filter->status = $args['status'] ?? '';
1910 - $filter->page = $args['paged'] ?? 1;
1911 - $filter->limit = $args['limit'] ?? $filter->limit;
1912 - $total_rows = 0;
1913 - $result_courses = LP_User_Item_Course::get_user_courses( $filter, $total_rows );
1914 -
1915 - $course_ids = LP_Course::get_course_ids( $result_courses, 'item_id' );
1916 -
1917 - $courses = array(
1918 - 'total' => $total_rows,
1919 - 'paged' => $filter->page,
1920 - 'limit' => $filter->limit,
1921 - 'items' => $course_ids,
1922 - );
1923 -
1924 - return new LP_Query_List_Table( $courses );
1925 - }*/
1926 -
1927 - /**
1928 984 * @return array
1929 985 */
1930 986 public function get_roles() {
1931 987 return (array) $this->get_data( 'roles' );
@@ -1948,25 +1004,8 @@
1948 1004 return $quiz_data ? $quiz_data->has_checked_question( $question_id ) : false;
1949 1005 }
1950 1006
1951 1007 /**
1952 - * @param $question_id
1953 - * @param $quiz_id
1954 - * @param int $course_id
1955 - *
1956 - * @return bool
1957 - */
1958 - public function has_hinted_answer( $question_id, $quiz_id, $course_id = 0 ) {
1959 - if ( ! $course_id ) {
1960 - $course_id = get_the_ID();
1961 - }
1962 -
1963 - $quiz_data = $this->get_item_data( $quiz_id, $course_id );
1964 -
1965 - return $quiz_data ? $quiz_data->has_hinted_question( $question_id ) : false;
1966 - }
1967 -
1968 - /**
1969 1008 * Return TRUE if user is already exists.
1970 1009 *
1971 1010 * @return bool
1972 1011 */
@@ -1990,13 +1029,33 @@
1990 1029 * @param mixed $size
1991 1030 *
1992 1031 * @return string
1993 1032 */
1994 - public function get_upload_profile_src( $size = '' ) {
1995 - return LP_Profile::instance( $this->get_id() )->get_upload_profile_src( $size );
1033 + public function get_upload_profile_src() {
1034 + return LP_Profile::instance( $this->get_id() )->get_upload_profile_src();
1996 1035 }
1997 1036
1998 1037 /**
1038 + * Get profile avatar url
1039 + *
1040 + * @return string
1041 + * @since 4.2.7.2
1042 + * @version 1.0.0
1043 + */
1044 + public function get_profile_avatar_url(): string {
1045 + $avatar_url = $this->get_upload_profile_src();
1046 + if ( empty( $avatar_url ) ) {
1047 + $args = learn_press_get_avatar_thumb_size();
1048 + $avatar_url = get_avatar_url( $this->get_id(), $args );
1049 + if ( empty( $avatar_url ) ) {
1050 + $avatar_url = LP_PLUGIN_URL . 'assets/images/avatar-default.png';
1051 + }
1052 + }
1053 +
1054 + return $avatar_url;
1055 + }
1056 +
1057 + /**
1999 1058 * @param string $type
2000 1059 * @param int $size
2001 1060 * @param bool $src_only
2002 1061 *
@@ -2007,11 +1066,13 @@
2007 1066 }
2008 1067
2009 1068 /**
2010 1069 * Get links socials of use on Profile page
1070 + * Icon is font awesome
2011 1071 *
2012 1072 * @param int $user_id
2013 1073 * @return array
1074 + * @deprecated 4.2.3
2014 1075 */
2015 1076 public function get_profile_socials( int $user_id = 0 ): array {
2016 1077 $socials = array();
2017 1078 $extra_info = learn_press_get_user_extra_profile_info( $user_id );
@@ -2023,21 +1084,24 @@
2023 1084 }
2024 1085
2025 1086 switch ( $k ) {
2026 1087 case 'facebook':
2027 - $i = '<i class="fab fa-facebook-f"></i>';
1088 + $i = '<i class="lp-icon-facebook-f"></i>';
2028 1089 break;
2029 1090 case 'twitter':
2030 - $i = '<i class="fab fa-twitter"></i>';
1091 + $i = '<i class="lp-icon-twitter"></i>';
2031 1092 break;
2032 1093 case 'googleplus':
2033 - $i = '<i class="fab fa-google-plus-g"></i>';
1094 + $i = '<i class="lp-icon-google-plus"></i>';
2034 1095 break;
2035 1096 case 'youtube':
2036 - $i = '<i class="fab fa-youtube"></i>';
1097 + $i = '<i class="lp-icon-youtube-play"></i>';
2037 1098 break;
1099 + case 'linkedin':
1100 + $i = '<i class="lp-icon-linkedin"></i>';
1101 + break;
2038 1102 default:
2039 - $i = sprintf( '<i class="fab fa-%s"></i>', $k );
1103 + $i = sprintf( '<i class="lp-icon-%s"></i>', $k );
2040 1104 }
2041 1105
2042 1106 $icon = apply_filters(
2043 1107 'learn-press/user-profile-social-icon',
@@ -2053,37 +1117,57 @@
2053 1117 return apply_filters( 'learn-press/user-profile-socials', $socials, $this->get_id(), $this );
2054 1118 }
2055 1119
2056 1120 /**
2057 - * Check if user can access to a course.
1121 + * Get links socials of use on Profile page
1122 + * Icon is svg
2058 1123 *
2059 - * @param int $course_id
2060 - *
2061 - * @return mixed
2062 - * @editor tungnx
2063 - * @modify 4.1.3 - comment - not use
1124 + * @param int $user_id
1125 + * @return array
1126 + * @since 4.2.3
1127 + * @version 1.0.0
2064 1128 */
2065 - /*public function can_access_course( $course_id ) {
1129 + public function get_profile_social( int $user_id = 0 ): array {
1130 + $socials = array();
1131 + $extra_info = learn_press_get_user_extra_profile_info( $user_id );
2066 1132
2067 - $accessible = $this->has_course_access_level(
2068 - array(
2069 - LP_COURSE_ACCESS_LEVEL_60,
2070 - LP_COURSE_ACCESS_LEVEL_70,
2071 - ),
2072 - $course_id,
2073 - 'any'
2074 - );
1133 + if ( $extra_info ) {
1134 + foreach ( $extra_info as $k => $v ) {
1135 + if ( empty( $v ) ) {
1136 + continue;
1137 + }
2075 1138
2076 - $accessible = apply_filters(
2077 - 'learn-press/user-can-access-course',
2078 - $accessible,
2079 - $course_id,
2080 - $this->get_id()
2081 - );
1139 + switch ( $k ) {
1140 + case 'facebook':
1141 + $i = '<i class="lp-user-ico lp-icon-facebook"></i>';
1142 + break;
1143 + case 'twitter':
1144 + $i = '<i class="lp-user-ico lp-icon-twitter"></i>';
1145 + break;
1146 + case 'linkedin':
1147 + $i = '<i class="lp-user-ico lp-icon-linkedin"></i>';
1148 + break;
1149 + case 'youtube':
1150 + $i = '<i class="lp-user-ico lp-icon-youtube-play"></i>';
1151 + break;
1152 + default:
1153 + $i = sprintf( '<i class="lp-user-ico lp-icon-%s"></i>', esc_attr( $k ) );
1154 + }
2082 1155
2083 - return $accessible;
2084 - }*/
1156 + $icon = apply_filters(
1157 + 'learn-press/user-profile-social-icon',
1158 + $i,
1159 + $k,
1160 + $this->get_id(),
1161 + $this
1162 + );
1163 + $socials[ $k ] = sprintf( '<a href="%s">%s</a>', esc_url_raw( $v ), $icon );
1164 + }
1165 + }
2085 1166
1167 + return apply_filters( 'learn-press/user-profile-socials', $socials, $this->get_id(), $this );
1168 + }
1169 +
2086 1170 /**
2087 1171 * Check course of user has graduation is in-progress
2088 1172 *
2089 1173 * @param $course_id
@@ -2093,9 +1177,9 @@
2093 1177 public function is_course_in_progress( $course_id ): bool {
2094 1178 $user = learn_press_get_user( $this->get_id() );
2095 1179 $user_course = $user->get_course_data( $course_id );
2096 1180
2097 - return $user_course && LP_COURSE_GRADUATION_IN_PROGRESS === $user_course->get_graduation();
1181 + return $user_course && LP_COURSE_GRADUATION_IN_PROGRESS === $user_course->get_graduation();
2098 1182 }
2099 1183
2100 1184 /**
2101 1185 * Return TRUE if user can do a quiz
@@ -2108,13 +1192,10 @@
2108 1192 */
2109 1193 public function can_do_quiz( $quiz_id, $course_id = 0 ) {
2110 1194 $course = learn_press_get_course( $course_id );
2111 1195
2112 - if ( $course->is_required_enroll() ) {
2113 - $can = $this->has_course_status(
2114 - $course_id,
2115 - array( 'enrolled' )
2116 - ) && ! $this->has_started_quiz( $quiz_id, $course_id );
1196 + if ( ! $course->is_no_required_enroll() ) {
1197 + $can = $this->has_course_status( $course_id, array( 'enrolled' ) ) && ! $this->has_started_quiz( $quiz_id, $course_id );
2117 1198 } else {
2118 1199 $can = ! $this->has_started_quiz( $quiz_id, $course_id );
2119 1200 }
2120 1201
@@ -2121,28 +1202,8 @@
2121 1202 return apply_filters( 'learn_press_user_can_do_quiz', $can, $quiz_id, $this->get_id(), $course_id );
2122 1203 }
2123 1204
2124 1205 /**
2125 - * @depecated 4.1.6.9.2
2126 - */
2127 - /*public function evaluate_course_results( $course_id ) {
2128 - $user_course = $this->get_course_data( $course_id );
2129 -
2130 - return isset( $user_course ) ? $user_course->get_results( 'result' ) : 0;
2131 - }*/
2132 -
2133 - /**
2134 - * @editor tungnx
2135 - * @modify 4.1.4.1 - comment - not use
2136 - */
2137 - /*public function has_reached_passing_condition( $course_id ) {
2138 - $course = learn_press_get_course( $course_id );
2139 - $result = $this->evaluate_course_results( $course_id );
2140 -
2141 - return $return = $result >= $course->get_passing_condition();
2142 - }*/
2143 -
2144 - /**
2145 1206 * Check if all items in course completed.
2146 1207 *
2147 1208 * @return boolean
2148 1209 * @author Nhamdv <[email protected]>
@@ -2160,10 +1221,17 @@
2160 1221
2161 1222 return $course_results['completed_items'] >= $course->count_items();
2162 1223 }
2163 1224
1225 + /**
1226 + * Get role of user.
1227 + *
1228 + * @return string
1229 + * @since 3.0.0
1230 + * @version 1.0.1
1231 + */
2164 1232 public function get_role() {
2165 - return $this->is_admin() ? 'admin' : ( $this->is_instructor() ? 'instructor' : 'user' );
1233 + return $this->get_data( 'role' );
2166 1234 }
2167 1235
2168 1236 /**
2169 1237 * Get user course's grade.
@@ -2192,9 +1260,9 @@
2192 1260 *
2193 1261 * @return bool
2194 1262 */
2195 1263 public function is_guest(): bool {
2196 - return ! $this->get_id() || ! get_user_by( 'id', $this->get_id() );
1264 + return $this instanceof LP_User_Guest;
2197 1265 }
2198 1266
2199 1267 /**
2200 1268 * Check if user can edit a post.
@@ -2234,9 +1302,9 @@
2234 1302 *
2235 1303 * @return string
2236 1304 */
2237 1305 public function get_description(): string {
2238 - return $this->get_data( 'description', '' );
1306 + return get_the_author_meta( 'description', $this->get_id() );
2239 1307 }
2240 1308
2241 1309 /**
2242 1310 * Return user first name.