BaseModel.php
11 months ago
BillingModel.php
1 year ago
CartModel.php
1 year ago
CouponModel.php
11 months ago
CourseModel.php
10 months ago
LessonModel.php
10 months ago
OrderActivitiesModel.php
1 year ago
OrderMetaModel.php
1 year ago
OrderModel.php
10 months ago
QuizModel.php
10 months ago
UserModel.php
1 year ago
WithdrawModel.php
1 year ago
UserModel.php
104 lines
| 1 | <?php |
| 2 | /** |
| 3 | * User Model |
| 4 | * |
| 5 | * @package Tutor\Models |
| 6 | * @author Themeum <support@themeum.com> |
| 7 | * @link https://themeum.com |
| 8 | * @since 3.0.0 |
| 9 | */ |
| 10 | |
| 11 | namespace Tutor\Models; |
| 12 | |
| 13 | use TUTOR\Course; |
| 14 | use Tutor\Helpers\QueryHelper; |
| 15 | |
| 16 | /** |
| 17 | * UserModel class |
| 18 | */ |
| 19 | class UserModel { |
| 20 | |
| 21 | /** |
| 22 | * Get user list |
| 23 | * |
| 24 | * @since 3.0.0 |
| 25 | * |
| 26 | * @see https://developer.wordpress.org/reference/classes/wp_user_query/ |
| 27 | * |
| 28 | * @param array $args args. |
| 29 | * |
| 30 | * @return \WP_User_Query |
| 31 | */ |
| 32 | public function get_users_list( array $args ) { |
| 33 | $default_args = array( |
| 34 | 'orderby' => 'ID', |
| 35 | 'order' => 'DESC', |
| 36 | 'number' => 10, |
| 37 | 'offset' => 0, |
| 38 | 'role' => '', |
| 39 | 'fields' => array( 'ID', 'user_login', 'display_name', 'user_email', 'user_url', 'registered' ), |
| 40 | ); |
| 41 | |
| 42 | $args = wp_parse_args( $args, $default_args ); |
| 43 | $args = apply_filters( 'tutor_user_list_args', $args ); |
| 44 | |
| 45 | return new \WP_User_Query( $args ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Get unenrolled users of a course/bundle |
| 50 | * |
| 51 | * @since 3.0.0 |
| 52 | * |
| 53 | * @param int $object_id Course/Bundle id. |
| 54 | * @param array $search_clause Search condition. |
| 55 | * @param integer $limit List limit. |
| 56 | * @param integer $offset Offset. |
| 57 | * |
| 58 | * @return array |
| 59 | */ |
| 60 | public function get_unenrolled_users( $object_id, $search_clause = array(), $limit = 10, $offset = 0 ) { |
| 61 | global $wpdb; |
| 62 | |
| 63 | $primary_table = "{$wpdb->users} AS u"; |
| 64 | $joining_tables = array( |
| 65 | array( |
| 66 | 'type' => 'LEFT', |
| 67 | 'table' => "{$wpdb->posts} p", |
| 68 | 'on' => "p.post_type = 'tutor_enrolled' AND p.post_parent = {$object_id} AND u.ID = p.post_author", |
| 69 | ), |
| 70 | ); |
| 71 | |
| 72 | $response = QueryHelper::get_joined_data( |
| 73 | $primary_table, |
| 74 | $joining_tables, |
| 75 | array( |
| 76 | 'distinct u.ID', |
| 77 | 'u.user_login', |
| 78 | 'u.user_email', |
| 79 | 'u.display_name', |
| 80 | 'CASE WHEN p.ID IS NOT NULL THEN 1 ELSE 0 END AS is_enrolled', |
| 81 | 'p.post_status', |
| 82 | ), |
| 83 | array(), |
| 84 | $search_clause, |
| 85 | 'ID', |
| 86 | $limit, |
| 87 | $offset |
| 88 | ); |
| 89 | |
| 90 | foreach ( $response['results'] as $result ) { |
| 91 | // Typecast `is_enrolled` to int. |
| 92 | $result->is_enrolled = (int) $result->is_enrolled; |
| 93 | |
| 94 | // Add enrollment status. |
| 95 | $result->enrollment_status = in_array( $result->post_status, array( 'cancel', 'cancelled', 'canceled' ) ) ? __( 'Cancelled', 'tutor' ) : ( 'completed' === $result->post_status ? __( 'Approved', 'tutor' ) : ( 'pending' === $result->post_status ? __( 'Pending', 'tutor' ) : $result->post_status ) ); |
| 96 | |
| 97 | // Add avatar URL for the user. |
| 98 | $result->avatar_url = get_avatar_url( $result->ID ); |
| 99 | } |
| 100 | |
| 101 | return $response; |
| 102 | } |
| 103 | } |
| 104 |