| @@ -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,135 +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 | 542 | * Check if user has at least one role. |
| 822 | 543 | * |
| 823 | 544 | * @param array|string $roles |
| 824 | 545 | * |
| @@ -850,30 +571,8 @@ | ||
| 850 | 571 | |
| 851 | 572 | return $is; |
| 852 | 573 | } |
| 853 | 574 | |
| 854 | - /** | |
| 855 | - * | |
| 856 | - * Check what the user can do | |
| 857 | - * | |
| 858 | - * @param $role | |
| 859 | - * | |
| 860 | - * @return mixed | |
| 861 | - * @throws Exception | |
| 862 | - */ | |
| 863 | - public function can( $role ) { | |
| 864 | - _deprecated_function( __FUNCTION__, '3.0.8' ); | |
| 865 | - $args = func_get_args(); | |
| 866 | - unset( $args[0] ); | |
| 867 | - $method = 'can_' . preg_replace( '!-!', '_', $role ); | |
| 868 | - $callback = array( $this, $method ); | |
| 869 | - if ( is_callable( $callback ) ) { | |
| 870 | - return call_user_func_array( $callback, $args ); | |
| 871 | - } else { | |
| 872 | - throw new Exception( sprintf( __( 'The role %s for user doesn\'t exist', 'learnpress' ), $role ) ); | |
| 873 | - } | |
| 874 | - } | |
| 875 | - | |
| 876 | 575 | public function can_edit_item( $item_id, $course_id = 0 ) { |
| 877 | 576 | $return = $this->is_admin(); |
| 878 | 577 | |
| 879 | 578 | if ( ! $return ) { |
| @@ -888,24 +587,8 @@ | ||
| 888 | 587 | return apply_filters( 'learn_press_user_can_edit_item', $return, $item_id, $course_id, $this->get_id() ); |
| 889 | 588 | } |
| 890 | 589 | |
| 891 | 590 | /** |
| 892 | - * Check if user can finish course by getting current progress | |
| 893 | - * and compares with course passing condition. | |
| 894 | - * | |
| 895 | - * @param int $course_id | |
| 896 | - * | |
| 897 | - * @return bool | |
| 898 | - * @editor tungnx | |
| 899 | - * @modify 4.1.3 | |
| 900 | - */ | |
| 901 | - public function can_finish_course( int $course_id ) { | |
| 902 | - _deprecated_function( __FUNCTION__, '4.1.3', 'is_course_finished' ); | |
| 903 | - | |
| 904 | - return true; | |
| 905 | - } | |
| 906 | - | |
| 907 | - /** | |
| 908 | 591 | * Check if course has any passed status for an user. |
| 909 | 592 | * Statuses: depending on value of column `status` in user_items. |
| 910 | 593 | * - purchased: bought and order is completed, `start_date` and `end_date` is null |
| 911 | 594 | * - enrolled: value of column `status` in user_items is enrolled |
| @@ -930,17 +613,8 @@ | ||
| 930 | 613 | return false; |
| 931 | 614 | } |
| 932 | 615 | |
| 933 | 616 | /** |
| 934 | - * @depecated 4.1.6.9.1 | |
| 935 | - */ | |
| 936 | - /*public function get_completed_items( $course_id ) { | |
| 937 | - $this->_curd->get_user_items( $this->get_id(), $course_id ); | |
| 938 | - | |
| 939 | - return $this->_curd->get_user_completed_items( $this->get_id(), $course_id ); | |
| 940 | - }*/ | |
| 941 | - | |
| 942 | - /** | |
| 943 | 617 | * Finish course |
| 944 | 618 | * |
| 945 | 619 | * @param int $course_id |
| 946 | 620 | * |
| @@ -951,8 +625,11 @@ | ||
| 951 | 625 | $course = learn_press_get_course( $course_id ); |
| 952 | 626 | |
| 953 | 627 | if ( $course ) { |
| 954 | 628 | $user_course = $this->get_course_data( $course_id ); |
| 629 | + if ( ! $user_course ) { | |
| 630 | + return $return; | |
| 631 | + } | |
| 955 | 632 | |
| 956 | 633 | $result = $user_course->calculate_course_results(); |
| 957 | 634 | |
| 958 | 635 | // Save result for course |
| @@ -964,12 +641,11 @@ | ||
| 964 | 641 | $graduation = LP_COURSE_GRADUATION_FAILED; |
| 965 | 642 | } |
| 966 | 643 | |
| 967 | 644 | $user_course->set_graduation( $graduation ); |
| 968 | - $user_course->save(); | |
| 645 | + //$user_course->save(); | |
| 646 | + $return = $user_course->complete( LP_COURSE_FINISHED ); | |
| 969 | 647 | |
| 970 | - $return = $user_course->complete( 'finished' ); | |
| 971 | - | |
| 972 | 648 | if ( $return ) { |
| 973 | 649 | do_action( 'learn-press/user-course-finished', $course_id, $this->get_id(), $return ); |
| 974 | 650 | } |
| 975 | 651 | } |
| @@ -1016,22 +692,8 @@ | ||
| 1016 | 692 | public function is_author_of( $post_id ) { |
| 1017 | 693 | return absint( get_post_field( 'post_author', $post_id ) ) === $this->get_id(); |
| 1018 | 694 | } |
| 1019 | 695 | |
| 1020 | - public function has( $role ) { | |
| 1021 | - _deprecated_function( __FUNCTION__, '3.0.8' ); | |
| 1022 | - | |
| 1023 | - $args = func_get_args(); | |
| 1024 | - unset( $args[0] ); | |
| 1025 | - $method = 'has_' . 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 | 696 | public function get( $role ) { |
| 1035 | 697 | $args = func_get_args(); |
| 1036 | 698 | unset( $args[0] ); |
| 1037 | 699 | $method = 'get_' . preg_replace( '!-!', '_', $role ); |
| @@ -1038,9 +700,9 @@ | ||
| 1038 | 700 | $callback = array( $this, $method ); |
| 1039 | 701 | if ( is_callable( $callback ) ) { |
| 1040 | 702 | return call_user_func_array( $callback, $args ); |
| 1041 | 703 | } else { |
| 1042 | - throw new Exception( sprintf( __( 'The role %s for user doesn\'t exist', 'learnpress' ), $role ) ); | |
| 704 | + throw new Exception( sprintf( __( 'The role %s for the user doesn\'t exist', 'learnpress' ), $role ) ); | |
| 1043 | 705 | } |
| 1044 | 706 | } |
| 1045 | 707 | |
| 1046 | 708 | /** |
| @@ -1080,9 +742,9 @@ | ||
| 1080 | 742 | * @param int $quiz_id |
| 1081 | 743 | * @param int $course_id |
| 1082 | 744 | * |
| 1083 | 745 | * @return bool |
| 1084 | - * @depecated 4.1.6.9 | |
| 746 | + * @deprecated 4.1.6.9 | |
| 1085 | 747 | */ |
| 1086 | 748 | /*public function has_completed_quiz( $quiz_id, $course_id = 0 ): bool { |
| 1087 | 749 | return $this->get_item_status( $quiz_id, $course_id ) == 'completed'; |
| 1088 | 750 | }*/ |
| @@ -1091,48 +753,43 @@ | ||
| 1091 | 753 | * Mark a lesson is completed for user |
| 1092 | 754 | * |
| 1093 | 755 | * @param int $lesson_id |
| 1094 | 756 | * @param int $course_id |
| 1095 | - * @param bool $return_wp_error | |
| 1096 | 757 | * |
| 1097 | 758 | * @return bool|WP_Error |
| 1098 | 759 | */ |
| 1099 | - public function complete_lesson( $lesson_id, $course_id = 0, $return_wp_error = true ) { | |
| 760 | + public function complete_lesson( $lesson_id = 0, $course_id = 0 ) { | |
| 761 | + $result = true; | |
| 762 | + | |
| 1100 | 763 | try { |
| 1101 | - $course_id = $this->_get_course( $course_id ); | |
| 764 | + $course = learn_press_get_course( $course_id ); | |
| 765 | + if ( ! $course ) { | |
| 766 | + throw new Exception( __( 'Invalid course', 'learnpress' ) ); | |
| 767 | + } | |
| 1102 | 768 | |
| 1103 | 769 | $course_data = $this->get_course_data( $course_id ); |
| 770 | + if ( ! $course_data ) { | |
| 771 | + throw new Exception( __( 'You must enroll course!', 'learnpress' ) ); | |
| 772 | + } | |
| 1104 | 773 | |
| 1105 | - $result = false; | |
| 1106 | - | |
| 1107 | 774 | /** |
| 1108 | 775 | * If user has stared a lesson, get user lesson information |
| 1109 | 776 | */ |
| 1110 | 777 | $item = $course_data->get_item( $lesson_id ); |
| 1111 | - if ( $item ) { | |
| 1112 | - if ( $item->is_completed() ) { | |
| 1113 | - throw new Exception( | |
| 1114 | - __( 'You have already completed this lesson.', 'learnpress' ), | |
| 1115 | - LP_COMPLETE_ITEM_FAIL | |
| 1116 | - ); | |
| 1117 | - } | |
| 778 | + if ( ! $item ) { | |
| 779 | + throw new Exception( __( 'Invalid lesson', 'learnpress' ) ); | |
| 780 | + } | |
| 1118 | 781 | |
| 1119 | - $item->set_end_time( current_time( 'mysql', 1 ) ); | |
| 1120 | - $item->set_status( 'completed' ); | |
| 1121 | - $item->set_graduation( 'passed' ); | |
| 782 | + if ( $item->is_completed() ) { | |
| 783 | + throw new Exception( __( 'You have already completed this lesson.', 'learnpress' ) ); | |
| 784 | + } | |
| 1122 | 785 | |
| 1123 | - $updated = $item->update(); | |
| 786 | + $item->set_graduation( 'passed' ); | |
| 787 | + $updated = $item->complete(); | |
| 1124 | 788 | |
| 1125 | - if ( is_wp_error( $updated ) ) { | |
| 1126 | - return $return_wp_error ? $updated : false; | |
| 1127 | - } else { | |
| 1128 | - $result = true; | |
| 1129 | - } | |
| 1130 | - } | |
| 1131 | - | |
| 1132 | 789 | do_action( 'learn-press/user-completed-lesson', $lesson_id, $course_id, $this->get_id() ); |
| 1133 | - } catch ( Exception $ex ) { | |
| 1134 | - $result = $return_wp_error ? new WP_Error( $ex->getCode(), $ex->getMessage() ) : false; | |
| 790 | + } catch ( Throwable $e ) { | |
| 791 | + $result = new WP_Error( 'error_lesson_complete', $e->getMessage() ); | |
| 1135 | 792 | } |
| 1136 | 793 | |
| 1137 | 794 | return $result; |
| 1138 | 795 | } |
| @@ -1143,9 +800,9 @@ | ||
| 1143 | 800 | * @param int $lesson_id Lesson id. |
| 1144 | 801 | * @param null $course_id Course id. |
| 1145 | 802 | * |
| 1146 | 803 | * @return bool |
| 1147 | - * @depecated 4.1.6.9 | |
| 804 | + * @deprecated 4.1.6.9 | |
| 1148 | 805 | */ |
| 1149 | 806 | public function has_completed_lesson( $lesson_id = 0, $course_id = null ): bool { |
| 1150 | 807 | return 'completed' === $this->get_item_status( $lesson_id, $course_id ); |
| 1151 | 808 | } |
| @@ -1171,9 +828,9 @@ | ||
| 1171 | 828 | /** |
| 1172 | 829 | * @param $course_id |
| 1173 | 830 | * |
| 1174 | 831 | * @return int |
| 1175 | - * @depecated 4.1.6.9 | |
| 832 | + * @deprecated 4.1.6.9 | |
| 1176 | 833 | */ |
| 1177 | 834 | public function get_course_history_id( $course_id ) { |
| 1178 | 835 | $history = $this->get_course_info( $course_id ); |
| 1179 | 836 | |
| @@ -1208,216 +865,8 @@ | ||
| 1208 | 865 | return apply_filters( 'learn-press/user-course-status', $status, $course_id, $this->get_id() ); |
| 1209 | 866 | } |
| 1210 | 867 | |
| 1211 | 868 | /** |
| 1212 | - * Controls what this user can do with a course. | |
| 1213 | - * | |
| 1214 | - * 0 => No accessible | |
| 1215 | - * 10 => Normal users (like not logged in) | |
| 1216 | - * 20 => Author of course | |
| 1217 | - * 30 => Admin site | |
| 1218 | - * 35 => No require enrollment | |
| 1219 | - * 40 => Ordered but not completed | |
| 1220 | - * 50 => Order is completed but not enrolled | |
| 1221 | - * 60 => User has already enrolled course | |
| 1222 | - * 70 => User has already finished course | |
| 1223 | - * | |
| 1224 | - * @param int $course_id | |
| 1225 | - * | |
| 1226 | - * @return int | |
| 1227 | - * @since 3.1.0 | |
| 1228 | - * @editor tungnx | |
| 1229 | - * @modify 4.1.3 - comment - not use | |
| 1230 | - */ | |
| 1231 | - /*public function get_course_access_level( $course_id ) { | |
| 1232 | - $access_level = LP_Object_Cache::get( | |
| 1233 | - 'course-' . $course_id . '-' . $this->get_id(), | |
| 1234 | - 'learn-press/course-access-levels' | |
| 1235 | - ); | |
| 1236 | - | |
| 1237 | - if ( false === $access_level ) { | |
| 1238 | - $course = learn_press_get_course( $course_id ); | |
| 1239 | - | |
| 1240 | - if ( ! $course ) { | |
| 1241 | - $access_level = LP_COURSE_ACCESS_LEVEL_0; | |
| 1242 | - } elseif ( $this->is_admin() ) { | |
| 1243 | - $access_level = LP_COURSE_ACCESS_LEVEL_30; | |
| 1244 | - } elseif ( $this->is_author_of( $course_id ) ) { | |
| 1245 | - $access_level = LP_COURSE_ACCESS_LEVEL_20; | |
| 1246 | - } else { | |
| 1247 | - $access_level = LP_COURSE_ACCESS_LEVEL_10; | |
| 1248 | - } | |
| 1249 | - | |
| 1250 | - // Default level | |
| 1251 | - $access_level = apply_filters( | |
| 1252 | - 'learn-press/course-access-level-default', | |
| 1253 | - $access_level, | |
| 1254 | - $course_id, | |
| 1255 | - $this->get_id() | |
| 1256 | - ); | |
| 1257 | - $course_data = $this->get_course_data( $course_id ); | |
| 1258 | - | |
| 1259 | - if ( $course_data && $course_data->get_user_item_id() ) { | |
| 1260 | - // if ( $course_data->get_access_level() >= 50 ) { | |
| 1261 | - switch ( $course_data->get_status() ) { | |
| 1262 | - case 'completed': | |
| 1263 | - case 'failed': | |
| 1264 | - $access_level = LP_COURSE_ACCESS_LEVEL_60; | |
| 1265 | - break; | |
| 1266 | - case 'in-progress': | |
| 1267 | - case 'enrolled': | |
| 1268 | - $access_level = LP_COURSE_ACCESS_LEVEL_70; | |
| 1269 | - break; | |
| 1270 | - } | |
| 1271 | - // } | |
| 1272 | - } else { | |
| 1273 | - $order = $this->get_course_order( $course_id ); | |
| 1274 | - | |
| 1275 | - if ( $order ) { | |
| 1276 | - switch ( $order->get_status() ) { | |
| 1277 | - case 'completed': | |
| 1278 | - $access_level = LP_COURSE_ACCESS_LEVEL_50; | |
| 1279 | - break; | |
| 1280 | - default: | |
| 1281 | - $access_level = LP_COURSE_ACCESS_LEVEL_40; | |
| 1282 | - } | |
| 1283 | - } | |
| 1284 | - } | |
| 1285 | - | |
| 1286 | - LP_Object_Cache::set( | |
| 1287 | - 'course-' . $course_id . '-' . $this->get_id(), | |
| 1288 | - $access_level, | |
| 1289 | - 'learn-press/course-access-levels' | |
| 1290 | - ); | |
| 1291 | - } | |
| 1292 | - | |
| 1293 | - return apply_filters( 'learn-press/course-access-level', $access_level, $course_id, $this->get_id() ); | |
| 1294 | - }*/ | |
| 1295 | - | |
| 1296 | - /** | |
| 1297 | - * @editor tungnx | |
| 1298 | - * @reason comment - not use | |
| 1299 | - * @modify 4.1.2 | |
| 1300 | - */ | |
| 1301 | - /*public function get_item_access_level( $item_id, $course_id ) { | |
| 1302 | - $access_level = 0; | |
| 1303 | - | |
| 1304 | - if ( $course = learn_press_get_course( $course_id ) ) { | |
| 1305 | - if ( $course->has_item( $item_id ) ) { | |
| 1306 | - if ( 10 < $this->get_course_access_level( $course_id ) ) { | |
| 1307 | - $access_level = 10; | |
| 1308 | - } else { | |
| 1309 | - $item = $course->get_item( $item_id ); | |
| 1310 | - if ( $item->is_preview() ) { | |
| 1311 | - $access_level = 10; | |
| 1312 | - } | |
| 1313 | - } | |
| 1314 | - } | |
| 1315 | - } | |
| 1316 | - | |
| 1317 | - return apply_filters( | |
| 1318 | - 'learn-press/course-item-access-level', | |
| 1319 | - $access_level, | |
| 1320 | - $item_id, | |
| 1321 | - $course_id, | |
| 1322 | - $this->get_id() | |
| 1323 | - ); | |
| 1324 | - }*/ | |
| 1325 | - | |
| 1326 | - /** | |
| 1327 | - * Set new access-level of an user with a course. | |
| 1328 | - * | |
| 1329 | - * @param int $access_level | |
| 1330 | - * @param int $course_id | |
| 1331 | - * | |
| 1332 | - * @return mixed | |
| 1333 | - * @since 3.1.0 | |
| 1334 | - * @editor tungnx | |
| 1335 | - * @reason comment - not use | |
| 1336 | - * @modify 4.1.2 | |
| 1337 | - */ | |
| 1338 | - /*public function set_course_access_level( $access_level, $course_id ) { | |
| 1339 | - if ( $access_level !== $this->get_course_access_level( $course_id ) ) { | |
| 1340 | - LP_Object_Cache::set( | |
| 1341 | - 'course-' . $course_id . '-' . $this->get_id(), | |
| 1342 | - $access_level, | |
| 1343 | - 'learn-press/course-access-levels' | |
| 1344 | - ); | |
| 1345 | - } | |
| 1346 | - | |
| 1347 | - return $access_level; | |
| 1348 | - }*/ | |
| 1349 | - | |
| 1350 | - /** | |
| 1351 | - * Check if user have an access-level. | |
| 1352 | - * Consider the passed access-level is max level user have. | |
| 1353 | - * | |
| 1354 | - * @param int[] $access_level | |
| 1355 | - * @param int $course_id | |
| 1356 | - * @param string $compare | |
| 1357 | - * | |
| 1358 | - * @return bool | |
| 1359 | - * @since 3.1.0 | |
| 1360 | - * @editor tungnx | |
| 1361 | - * @modify 4.1.3 - not - use | |
| 1362 | - */ | |
| 1363 | - /*public function has_course_access_level( $access_level, $course_id, $compare = '<=' ) { | |
| 1364 | - $user_access_level = $this->get_course_access_level( $course_id ); | |
| 1365 | - | |
| 1366 | - switch ( $compare ) { | |
| 1367 | - case 'any': | |
| 1368 | - settype( $access_level, 'array' ); | |
| 1369 | - $has = in_array( $user_access_level, $access_level ); | |
| 1370 | - break; | |
| 1371 | - default: | |
| 1372 | - $has = version_compare( $user_access_level, $access_level ); | |
| 1373 | - } | |
| 1374 | - | |
| 1375 | - return $has; | |
| 1376 | - }*/ | |
| 1377 | - | |
| 1378 | - /** | |
| 1379 | - * Check if user has an access-level with a course. | |
| 1380 | - * | |
| 1381 | - * @param int $access_level | |
| 1382 | - * @param int $course_id | |
| 1383 | - * | |
| 1384 | - * @return bool | |
| 1385 | - * @since 3.1.0 | |
| 1386 | - * @editor tungnx | |
| 1387 | - * @modify 4.1.2 | |
| 1388 | - * @reason comment - not use | |
| 1389 | - */ | |
| 1390 | - /*public function is_access_level( $access_level, $course_id ) { | |
| 1391 | - $user_access_level = $this->get_course_access_level( $course_id ); | |
| 1392 | - | |
| 1393 | - return $user_access_level === $access_level; | |
| 1394 | - }*/ | |
| 1395 | - | |
| 1396 | - /** | |
| 1397 | - * Check if user is already ordered a course. | |
| 1398 | - * | |
| 1399 | - * @param int $course_id | |
| 1400 | - * | |
| 1401 | - * @return mixed|LP_Order | |
| 1402 | - * @editor tungnx | |
| 1403 | - * @modify 4.1.3 - comment - not use | |
| 1404 | - */ | |
| 1405 | - /*public function has_ordered_course( $course_id ) { | |
| 1406 | - $return = apply_filters( | |
| 1407 | - 'learn-press/user-has-ordered-course', | |
| 1408 | - $this->get_course_order( $course_id ), | |
| 1409 | - $course_id, | |
| 1410 | - $this->get_id() | |
| 1411 | - ); | |
| 1412 | - | |
| 1413 | - // Deprecated since 3.0.0 | |
| 1414 | - $return = apply_filters( 'learn_press_user_has_ordered_course', $return, $course_id, $this->get_id() ); | |
| 1415 | - | |
| 1416 | - return $return; | |
| 1417 | - }*/ | |
| 1418 | - | |
| 1419 | - /** | |
| 1420 | 869 | * Get order status of a course. |
| 1421 | 870 | * |
| 1422 | 871 | * @param int $course_id |
| 1423 | 872 | * |
| @@ -1476,29 +925,8 @@ | ||
| 1476 | 925 | return apply_filters( 'learn_press_user_has_completed_item', $return, $item ); |
| 1477 | 926 | } |
| 1478 | 927 | |
| 1479 | 928 | /** |
| 1480 | - * Get the remaining time of a course for the user. | |
| 1481 | - * | |
| 1482 | - * @param int $course_id | |
| 1483 | - * | |
| 1484 | - * @return bool|int|string | |
| 1485 | - */ | |
| 1486 | - public function get_course_remaining_time( $course_id ) { | |
| 1487 | - $course = learn_press_get_course( $course_id ); | |
| 1488 | - $remain = false; | |
| 1489 | - | |
| 1490 | - if ( $course && $course->get_id() ) { | |
| 1491 | - $course_data = $this->get_course_data( $course_id, true ); | |
| 1492 | - if ( $course_data ) { | |
| 1493 | - $remain = $course_data->is_exceeded(); | |
| 1494 | - } | |
| 1495 | - } | |
| 1496 | - | |
| 1497 | - return $remain > 0 ? learn_press_seconds_to_weeks( $remain ) : false; | |
| 1498 | - } | |
| 1499 | - | |
| 1500 | - /** | |
| 1501 | 929 | * Get the order that contains the course. |
| 1502 | 930 | * |
| 1503 | 931 | * @param int $course_id |
| 1504 | 932 | * |
| @@ -1520,142 +948,8 @@ | ||
| 1520 | 948 | return $lp_order; |
| 1521 | 949 | } |
| 1522 | 950 | |
| 1523 | 951 | /** |
| 1524 | - * Enroll this user to a course. | |
| 1525 | - * | |
| 1526 | - * @param int $course_id | |
| 1527 | - * @param int $order_id - Optional. An user can be enrolled to a course | |
| 1528 | - * | |
| 1529 | - * @return bool|WP_Error | |
| 1530 | - * @throws Exception | |
| 1531 | - * @since 3.3.0 | |
| 1532 | - * @editor tungnx | |
| 1533 | - * @version 3.3.1 | |
| 1534 | - * @modify 4.1.3 - comment - not use | |
| 1535 | - */ | |
| 1536 | - // public function enroll_course( int $course_id = 0, int $order_id = 0 ) { | |
| 1537 | - // $lp_user_items_db = LP_User_Items_DB::getInstance(); | |
| 1538 | - // | |
| 1539 | - // try { | |
| 1540 | - // /*$user_item_api = new LP_User_Item_CURD(); | |
| 1541 | - // $find_query = array( | |
| 1542 | - // 'item_id' => $course_id, | |
| 1543 | - // 'user_id' => $this->get_id(), | |
| 1544 | - // ); | |
| 1545 | - // | |
| 1546 | - // if ( $order_id ) { | |
| 1547 | - // $find_query['ref_id'] = $order_id; | |
| 1548 | - // }*/ | |
| 1549 | - // | |
| 1550 | - // $filter = new LP_User_Items_Filter(); | |
| 1551 | - // $filter->user_id = get_current_user_id(); | |
| 1552 | - // $filter->item_id = $course_id; | |
| 1553 | - // $course_item = $lp_user_items_db->get_last_user_course( $filter ); | |
| 1554 | - // | |
| 1555 | - // if ( ! $course_item ) { | |
| 1556 | - // $course_item = LP_User_Item::get_empty_item(); | |
| 1557 | - // } else { | |
| 1558 | - // $course_item = (array) $course_item; | |
| 1559 | - // } | |
| 1560 | - // | |
| 1561 | - // $user_id = $this->get_id(); | |
| 1562 | - // | |
| 1563 | - // $course_item['user_id'] = $user_id; | |
| 1564 | - // $course_item['item_id'] = $course_id; | |
| 1565 | - // $course_item['item_type'] = learn_press_get_post_type( $course_id ); | |
| 1566 | - // $course_item['ref_id'] = $order_id; | |
| 1567 | - // $course_item['ref_type'] = ( $order_id != 0 ) ? learn_press_get_post_type( $order_id ) : ''; | |
| 1568 | - // $course_item['start_time'] = current_time( 'mysql', true ); | |
| 1569 | - // $course_item['access_level'] = 50; | |
| 1570 | - // | |
| 1571 | - // /** | |
| 1572 | - // * @editor tungnx | |
| 1573 | - // * @fixed: case no auto enroll | |
| 1574 | - // */ | |
| 1575 | - // if ( 'yes' == LP_Settings::get_option( 'auto_enroll' ) ) { | |
| 1576 | - // $course_item['graduation'] = 'in-progress'; | |
| 1577 | - // } | |
| 1578 | - // | |
| 1579 | - // $user_course = new LP_User_Item_Course( $course_item ); | |
| 1580 | - // $user_course->set_status( LP_COURSE_PURCHASED ); | |
| 1581 | - // | |
| 1582 | - // if ( ! $user_course->update( true ) ) { | |
| 1583 | - // throw new Exception( __( 'Update user item error.', 'learnpress' ) ); | |
| 1584 | - // } | |
| 1585 | - // | |
| 1586 | - // /*$user_id = is_user_logged_in() ? $this->get_id() : 0; | |
| 1587 | - // | |
| 1588 | - // global $wpdb; | |
| 1589 | - // $query = $wpdb->prepare( | |
| 1590 | - // " | |
| 1591 | - // UPDATE {$wpdb->learnpress_user_items} | |
| 1592 | - // SET access_level = %d | |
| 1593 | - // WHERE user_id = %d | |
| 1594 | - // AND item_id = %d | |
| 1595 | - // AND user_item_id NOT IN(%d) | |
| 1596 | - // ", | |
| 1597 | - // 0, | |
| 1598 | - // $user_id, | |
| 1599 | - // $course_id, | |
| 1600 | - // $user_course->get_user_item_id() | |
| 1601 | - // ); | |
| 1602 | - // $wpdb->query( $query );*/ | |
| 1603 | - // | |
| 1604 | - // $return = $user_course->get_user_item_id(); | |
| 1605 | - // } catch ( Exception $ex ) { | |
| 1606 | - // error_log( $ex->getMessage() ); | |
| 1607 | - // return false; | |
| 1608 | - // } | |
| 1609 | - // | |
| 1610 | - // return $return; | |
| 1611 | - // } | |
| 1612 | - | |
| 1613 | - /** | |
| 1614 | - * Enroll this user to a course. | |
| 1615 | - * | |
| 1616 | - * @param int $course_id | |
| 1617 | - * @param int $order_id | |
| 1618 | - * @param bool $force - Optional. Force create db record for preview quiz case | |
| 1619 | - * @param bool $wp_error - Optional. TRUE will return WP_Error object if there is an error. | |
| 1620 | - * @editor tungnx - comment - not use | |
| 1621 | - * @return bool|mixed|WP_Error | |
| 1622 | - */ | |
| 1623 | - /*public function enroll( $course_id, $order_id, $force = false, $wp_error = false ) { | |
| 1624 | - global $wpdb; | |
| 1625 | - | |
| 1626 | - _deprecated_function( __FUNCTION__, '4.1.0' ); | |
| 1627 | - | |
| 1628 | - try { | |
| 1629 | - $course = learn_press_get_course( $course_id ); | |
| 1630 | - $user_id = $this->get_id(); | |
| 1631 | - | |
| 1632 | - if ( $course->is_required_enroll() && ! $force ) { | |
| 1633 | - $order = learn_press_get_order( $order_id ); | |
| 1634 | - | |
| 1635 | - if ( ! $order ) { | |
| 1636 | - throw new Exception( __( 'Failed to enroll course.', 'learnpress' ), 10000 ); | |
| 1637 | - } | |
| 1638 | - | |
| 1639 | - if ( ! $this->can_enroll_course( $course_id ) ) { | |
| 1640 | - throw new Exception( __( 'Failed to enroll course.', 'learnpress' ), 10001 ); | |
| 1641 | - } | |
| 1642 | - | |
| 1643 | - if ( ! $this->get_id() ) { | |
| 1644 | - throw new Exception( __( 'Please login to enroll course.', 'learnpress' ), 10002 ); | |
| 1645 | - } | |
| 1646 | - } | |
| 1647 | - | |
| 1648 | - $return = $this->enroll_course( $course_id, $order_id, false, $wp_error ); | |
| 1649 | - | |
| 1650 | - return $return; | |
| 1651 | - | |
| 1652 | - } catch ( Exception $ex ) { | |
| 1653 | - return new WP_Error( $ex->getCode(), $ex->getMessage() ); | |
| 1654 | - } | |
| 1655 | - }*/ | |
| 1656 | - | |
| 1657 | - /** | |
| 1658 | 952 | * @param $question_id |
| 1659 | 953 | * |
| 1660 | 954 | * @return null|string |
| 1661 | 955 | */ |
| @@ -1674,89 +968,8 @@ | ||
| 1674 | 968 | return $wpdb->get_var( $query ); |
| 1675 | 969 | } |
| 1676 | 970 | |
| 1677 | 971 | /** |
| 1678 | - * @param $question_id | |
| 1679 | - * @param null $quiz_id | |
| 1680 | - * | |
| 1681 | - * @return bool | |
| 1682 | - */ | |
| 1683 | - public function get_answer_results( $question_id, $quiz_id = null ) { | |
| 1684 | - | |
| 1685 | - _deprecated_function( __CLASS__ . '::' . __FUNCTION__, '3.0.0' ); | |
| 1686 | - | |
| 1687 | - $data = false; | |
| 1688 | - if ( ! $quiz_id ) { | |
| 1689 | - $quiz_id = $this->get_quiz_by_question( $question_id ); | |
| 1690 | - } | |
| 1691 | - if ( $quiz_id ) { | |
| 1692 | - $question = LP_Question::get_question( $question_id ); | |
| 1693 | - | |
| 1694 | - if ( $question ) { | |
| 1695 | - $quiz_results = $this->get_quiz_results( $quiz_id ); | |
| 1696 | - if ( ! empty( $quiz_results->question_answers ) ) { | |
| 1697 | - $question_answer = array_key_exists( | |
| 1698 | - $question_id, | |
| 1699 | - $quiz_results->question_answers | |
| 1700 | - ) ? $quiz_results->question_answers[ $question_id ] : null; | |
| 1701 | - $data = $question->check( $question_answer ); | |
| 1702 | - } | |
| 1703 | - } | |
| 1704 | - } | |
| 1705 | - | |
| 1706 | - return $data; | |
| 1707 | - } | |
| 1708 | - | |
| 1709 | - /** | |
| 1710 | - * @param $question_id | |
| 1711 | - * @param null $quiz_id | |
| 1712 | - * | |
| 1713 | - * @return bool | |
| 1714 | - */ | |
| 1715 | - public function is_answered_question( $question_id, $quiz_id = null ) { | |
| 1716 | - if ( empty( $this->answered_questions ) ) { | |
| 1717 | - $this->answered_questions = array(); | |
| 1718 | - } | |
| 1719 | - if ( ! $quiz_id ) { | |
| 1720 | - $quiz_id = $this->get_quiz_by_question( $question_id ); | |
| 1721 | - } | |
| 1722 | - | |
| 1723 | - $results = $this->get_quiz_results( $quiz_id ); | |
| 1724 | - $answered = ! empty( $results->question_answers ) ? $results->question_answers : array(); | |
| 1725 | - | |
| 1726 | - return $answered ? array_key_exists( $question_id, $answered ) : false; | |
| 1727 | - } | |
| 1728 | - | |
| 1729 | - /** | |
| 1730 | - * @param array $args | |
| 1731 | - * | |
| 1732 | - * @return LP_Query_List_Table | |
| 1733 | - * @editor tungnx | |
| 1734 | - * @deprecated 4.1.6 | |
| 1735 | - */ | |
| 1736 | - /*public function get_purchased_courses( array $args = array() ): LP_Query_List_Table { | |
| 1737 | - $filter = new LP_User_Items_Filter(); | |
| 1738 | - $filter->fields = array( 'item_id' ); | |
| 1739 | - $filter->user_id = $this->get_id(); | |
| 1740 | - $filter->status = $args['status'] ?? ''; | |
| 1741 | - $filter->page = $args['paged'] ?? 1; | |
| 1742 | - $filter->limit = $args['limit'] ?? $filter->limit; | |
| 1743 | - $total_rows = 0; | |
| 1744 | - $result_courses = LP_User_Item_Course::get_user_courses( $filter, $total_rows ); | |
| 1745 | - | |
| 1746 | - $course_ids = LP_Course::get_course_ids( $result_courses, 'item_id' ); | |
| 1747 | - | |
| 1748 | - $courses = array( | |
| 1749 | - 'total' => $total_rows, | |
| 1750 | - 'paged' => $filter->page, | |
| 1751 | - 'limit' => $filter->limit, | |
| 1752 | - 'items' => $course_ids, | |
| 1753 | - ); | |
| 1754 | - | |
| 1755 | - return new LP_Query_List_Table( $courses ); | |
| 1756 | - }*/ | |
| 1757 | - | |
| 1758 | - /** | |
| 1759 | 972 | * @return array |
| 1760 | 973 | */ |
| 1761 | 974 | public function get_roles() { |
| 1762 | 975 | return (array) $this->get_data( 'roles' ); |
| @@ -1779,25 +992,8 @@ | ||
| 1779 | 992 | return $quiz_data ? $quiz_data->has_checked_question( $question_id ) : false; |
| 1780 | 993 | } |
| 1781 | 994 | |
| 1782 | 995 | /** |
| 1783 | - * @param $question_id | |
| 1784 | - * @param $quiz_id | |
| 1785 | - * @param int $course_id | |
| 1786 | - * | |
| 1787 | - * @return bool | |
| 1788 | - */ | |
| 1789 | - public function has_hinted_answer( $question_id, $quiz_id, $course_id = 0 ) { | |
| 1790 | - if ( ! $course_id ) { | |
| 1791 | - $course_id = get_the_ID(); | |
| 1792 | - } | |
| 1793 | - | |
| 1794 | - $quiz_data = $this->get_item_data( $quiz_id, $course_id ); | |
| 1795 | - | |
| 1796 | - return $quiz_data ? $quiz_data->has_hinted_question( $question_id ) : false; | |
| 1797 | - } | |
| 1798 | - | |
| 1799 | - /** | |
| 1800 | 996 | * Return TRUE if user is already exists. |
| 1801 | 997 | * |
| 1802 | 998 | * @return bool |
| 1803 | 999 | */ |
| @@ -1821,13 +1017,33 @@ | ||
| 1821 | 1017 | * @param mixed $size |
| 1822 | 1018 | * |
| 1823 | 1019 | * @return string |
| 1824 | 1020 | */ |
| 1825 | - public function get_upload_profile_src( $size = '' ) { | |
| 1826 | - return LP_Profile::instance( $this->get_id() )->get_upload_profile_src( $size ); | |
| 1021 | + public function get_upload_profile_src() { | |
| 1022 | + return LP_Profile::instance( $this->get_id() )->get_upload_profile_src(); | |
| 1827 | 1023 | } |
| 1828 | 1024 | |
| 1829 | 1025 | /** |
| 1026 | + * Get profile avatar url | |
| 1027 | + * | |
| 1028 | + * @return string | |
| 1029 | + * @since 4.2.7.2 | |
| 1030 | + * @version 1.0.0 | |
| 1031 | + */ | |
| 1032 | + public function get_profile_avatar_url(): string { | |
| 1033 | + $avatar_url = $this->get_upload_profile_src(); | |
| 1034 | + if ( empty( $avatar_url ) ) { | |
| 1035 | + $args = learn_press_get_avatar_thumb_size(); | |
| 1036 | + $avatar_url = get_avatar_url( $this->get_id(), $args ); | |
| 1037 | + if ( empty( $avatar_url ) ) { | |
| 1038 | + $avatar_url = LP_PLUGIN_URL . 'assets/images/avatar-default.png'; | |
| 1039 | + } | |
| 1040 | + } | |
| 1041 | + | |
| 1042 | + return $avatar_url; | |
| 1043 | + } | |
| 1044 | + | |
| 1045 | + /** | |
| 1830 | 1046 | * @param string $type |
| 1831 | 1047 | * @param int $size |
| 1832 | 1048 | * @param bool $src_only |
| 1833 | 1049 | * |
| @@ -1838,11 +1054,13 @@ | ||
| 1838 | 1054 | } |
| 1839 | 1055 | |
| 1840 | 1056 | /** |
| 1841 | 1057 | * Get links socials of use on Profile page |
| 1058 | + * Icon is font awesome | |
| 1842 | 1059 | * |
| 1843 | 1060 | * @param int $user_id |
| 1844 | 1061 | * @return array |
| 1062 | + * @deprecated 4.2.3 | |
| 1845 | 1063 | */ |
| 1846 | 1064 | public function get_profile_socials( int $user_id = 0 ): array { |
| 1847 | 1065 | $socials = array(); |
| 1848 | 1066 | $extra_info = learn_press_get_user_extra_profile_info( $user_id ); |
| @@ -1854,21 +1072,24 @@ | ||
| 1854 | 1072 | } |
| 1855 | 1073 | |
| 1856 | 1074 | switch ( $k ) { |
| 1857 | 1075 | case 'facebook': |
| 1858 | - $i = '<i class="fab fa-facebook-f"></i>'; | |
| 1076 | + $i = '<i class="lp-icon-facebook-f"></i>'; | |
| 1859 | 1077 | break; |
| 1860 | 1078 | case 'twitter': |
| 1861 | - $i = '<i class="fab fa-twitter"></i>'; | |
| 1079 | + $i = '<i class="lp-icon-twitter"></i>'; | |
| 1862 | 1080 | break; |
| 1863 | 1081 | case 'googleplus': |
| 1864 | - $i = '<i class="fab fa-google-plus-g"></i>'; | |
| 1082 | + $i = '<i class="lp-icon-google-plus"></i>'; | |
| 1865 | 1083 | break; |
| 1866 | 1084 | case 'youtube': |
| 1867 | - $i = '<i class="fab fa-youtube"></i>'; | |
| 1085 | + $i = '<i class="lp-icon-youtube-play"></i>'; | |
| 1868 | 1086 | break; |
| 1087 | + case 'linkedin': | |
| 1088 | + $i = '<i class="lp-icon-linkedin"></i>'; | |
| 1089 | + break; | |
| 1869 | 1090 | default: |
| 1870 | - $i = sprintf( '<i class="fab fa-%s"></i>', $k ); | |
| 1091 | + $i = sprintf( '<i class="lp-icon-%s"></i>', $k ); | |
| 1871 | 1092 | } |
| 1872 | 1093 | |
| 1873 | 1094 | $icon = apply_filters( |
| 1874 | 1095 | 'learn-press/user-profile-social-icon', |
| @@ -1884,37 +1105,57 @@ | ||
| 1884 | 1105 | return apply_filters( 'learn-press/user-profile-socials', $socials, $this->get_id(), $this ); |
| 1885 | 1106 | } |
| 1886 | 1107 | |
| 1887 | 1108 | /** |
| 1888 | - * Check if user can access to a course. | |
| 1109 | + * Get links socials of use on Profile page | |
| 1110 | + * Icon is svg | |
| 1889 | 1111 | * |
| 1890 | - * @param int $course_id | |
| 1891 | - * | |
| 1892 | - * @return mixed | |
| 1893 | - * @editor tungnx | |
| 1894 | - * @modify 4.1.3 - comment - not use | |
| 1112 | + * @param int $user_id | |
| 1113 | + * @return array | |
| 1114 | + * @since 4.2.3 | |
| 1115 | + * @version 1.0.0 | |
| 1895 | 1116 | */ |
| 1896 | - /*public function can_access_course( $course_id ) { | |
| 1117 | + public function get_profile_social( int $user_id = 0 ): array { | |
| 1118 | + $socials = array(); | |
| 1119 | + $extra_info = learn_press_get_user_extra_profile_info( $user_id ); | |
| 1897 | 1120 | |
| 1898 | - $accessible = $this->has_course_access_level( | |
| 1899 | - array( | |
| 1900 | - LP_COURSE_ACCESS_LEVEL_60, | |
| 1901 | - LP_COURSE_ACCESS_LEVEL_70, | |
| 1902 | - ), | |
| 1903 | - $course_id, | |
| 1904 | - 'any' | |
| 1905 | - ); | |
| 1121 | + if ( $extra_info ) { | |
| 1122 | + foreach ( $extra_info as $k => $v ) { | |
| 1123 | + if ( empty( $v ) ) { | |
| 1124 | + continue; | |
| 1125 | + } | |
| 1906 | 1126 | |
| 1907 | - $accessible = apply_filters( | |
| 1908 | - 'learn-press/user-can-access-course', | |
| 1909 | - $accessible, | |
| 1910 | - $course_id, | |
| 1911 | - $this->get_id() | |
| 1912 | - ); | |
| 1127 | + switch ( $k ) { | |
| 1128 | + case 'facebook': | |
| 1129 | + $i = '<i class="lp-user-ico lp-icon-facebook"></i>'; | |
| 1130 | + break; | |
| 1131 | + case 'twitter': | |
| 1132 | + $i = '<i class="lp-user-ico lp-icon-twitter"></i>'; | |
| 1133 | + break; | |
| 1134 | + case 'linkedin': | |
| 1135 | + $i = '<i class="lp-user-ico lp-icon-linkedin"></i>'; | |
| 1136 | + break; | |
| 1137 | + case 'youtube': | |
| 1138 | + $i = '<i class="lp-user-ico lp-icon-youtube-play"></i>'; | |
| 1139 | + break; | |
| 1140 | + default: | |
| 1141 | + $i = sprintf( '<i class="lp-user-ico lp-icon-%s"></i>', esc_attr( $k ) ); | |
| 1142 | + } | |
| 1913 | 1143 | |
| 1914 | - return $accessible; | |
| 1915 | - }*/ | |
| 1144 | + $icon = apply_filters( | |
| 1145 | + 'learn-press/user-profile-social-icon', | |
| 1146 | + $i, | |
| 1147 | + $k, | |
| 1148 | + $this->get_id(), | |
| 1149 | + $this | |
| 1150 | + ); | |
| 1151 | + $socials[ $k ] = sprintf( '<a href="%s">%s</a>', esc_url_raw( $v ), $icon ); | |
| 1152 | + } | |
| 1153 | + } | |
| 1916 | 1154 | |
| 1155 | + return apply_filters( 'learn-press/user-profile-socials', $socials, $this->get_id(), $this ); | |
| 1156 | + } | |
| 1157 | + | |
| 1917 | 1158 | /** |
| 1918 | 1159 | * Check course of user has graduation is in-progress |
| 1919 | 1160 | * |
| 1920 | 1161 | * @param $course_id |
| @@ -1924,9 +1165,9 @@ | ||
| 1924 | 1165 | public function is_course_in_progress( $course_id ): bool { |
| 1925 | 1166 | $user = learn_press_get_user( $this->get_id() ); |
| 1926 | 1167 | $user_course = $user->get_course_data( $course_id ); |
| 1927 | 1168 | |
| 1928 | - return $user_course && LP_COURSE_GRADUATION_IN_PROGRESS === $user_course->get_graduation(); | |
| 1169 | + return $user_course && LP_COURSE_GRADUATION_IN_PROGRESS === $user_course->get_graduation(); | |
| 1929 | 1170 | } |
| 1930 | 1171 | |
| 1931 | 1172 | /** |
| 1932 | 1173 | * Return TRUE if user can do a quiz |
| @@ -1939,13 +1180,10 @@ | ||
| 1939 | 1180 | */ |
| 1940 | 1181 | public function can_do_quiz( $quiz_id, $course_id = 0 ) { |
| 1941 | 1182 | $course = learn_press_get_course( $course_id ); |
| 1942 | 1183 | |
| 1943 | - if ( $course->is_required_enroll() ) { | |
| 1944 | - $can = $this->has_course_status( | |
| 1945 | - $course_id, | |
| 1946 | - array( 'enrolled' ) | |
| 1947 | - ) && ! $this->has_started_quiz( $quiz_id, $course_id ); | |
| 1184 | + if ( ! $course->is_no_required_enroll() ) { | |
| 1185 | + $can = $this->has_course_status( $course_id, array( 'enrolled' ) ) && ! $this->has_started_quiz( $quiz_id, $course_id ); | |
| 1948 | 1186 | } else { |
| 1949 | 1187 | $can = ! $this->has_started_quiz( $quiz_id, $course_id ); |
| 1950 | 1188 | } |
| 1951 | 1189 | |
| @@ -1952,28 +1190,8 @@ | ||
| 1952 | 1190 | return apply_filters( 'learn_press_user_can_do_quiz', $can, $quiz_id, $this->get_id(), $course_id ); |
| 1953 | 1191 | } |
| 1954 | 1192 | |
| 1955 | 1193 | /** |
| 1956 | - * @depecated 4.1.6.9.2 | |
| 1957 | - */ | |
| 1958 | - /*public function evaluate_course_results( $course_id ) { | |
| 1959 | - $user_course = $this->get_course_data( $course_id ); | |
| 1960 | - | |
| 1961 | - return isset( $user_course ) ? $user_course->get_results( 'result' ) : 0; | |
| 1962 | - }*/ | |
| 1963 | - | |
| 1964 | - /** | |
| 1965 | - * @editor tungnx | |
| 1966 | - * @modify 4.1.4.1 - comment - not use | |
| 1967 | - */ | |
| 1968 | - /*public function has_reached_passing_condition( $course_id ) { | |
| 1969 | - $course = learn_press_get_course( $course_id ); | |
| 1970 | - $result = $this->evaluate_course_results( $course_id ); | |
| 1971 | - | |
| 1972 | - return $return = $result >= $course->get_passing_condition(); | |
| 1973 | - }*/ | |
| 1974 | - | |
| 1975 | - /** | |
| 1976 | 1194 | * Check if all items in course completed. |
| 1977 | 1195 | * |
| 1978 | 1196 | * @return boolean |
| 1979 | 1197 | * @author Nhamdv <email@email.com> |
| @@ -1991,10 +1209,17 @@ | ||
| 1991 | 1209 | |
| 1992 | 1210 | return $course_results['completed_items'] >= $course->count_items(); |
| 1993 | 1211 | } |
| 1994 | 1212 | |
| 1213 | + /** | |
| 1214 | + * Get role of user. | |
| 1215 | + * | |
| 1216 | + * @return string | |
| 1217 | + * @since 3.0.0 | |
| 1218 | + * @version 1.0.1 | |
| 1219 | + */ | |
| 1995 | 1220 | public function get_role() { |
| 1996 | - return $this->is_admin() ? 'admin' : ( $this->is_instructor() ? 'instructor' : 'user' ); | |
| 1221 | + return $this->get_data( 'role' ); | |
| 1997 | 1222 | } |
| 1998 | 1223 | |
| 1999 | 1224 | /** |
| 2000 | 1225 | * Get user course's grade. |
| @@ -2023,9 +1248,9 @@ | ||
| 2023 | 1248 | * |
| 2024 | 1249 | * @return bool |
| 2025 | 1250 | */ |
| 2026 | 1251 | public function is_guest(): bool { |
| 2027 | - return ! $this->get_id() || ! get_user_by( 'id', $this->get_id() ); | |
| 1252 | + return $this instanceof LP_User_Guest; | |
| 2028 | 1253 | } |
| 2029 | 1254 | |
| 2030 | 1255 | /** |
| 2031 | 1256 | * Check if user can edit a post. |
| @@ -2065,9 +1290,9 @@ | ||
| 2065 | 1290 | * |
| 2066 | 1291 | * @return string |
| 2067 | 1292 | */ |
| 2068 | 1293 | public function get_description(): string { |
| 2069 | - return $this->get_data( 'description', '' ); | |
| 1294 | + return get_the_author_meta( 'description', $this->get_id() ); | |
| 2070 | 1295 | } |
| 2071 | 1296 | |
| 2072 | 1297 | /** |
| 2073 | 1298 | * Return user first name. |