Group.php
5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
/**
* This class provides the easy way to operate a group.
*
* @since 4.6.0
*
* @package LearnDash\Core
*/
/** NOTICE: This code is currently under development and may not be stable.
* Its functionality, behavior, and interfaces may change at any time without notice.
* Please refrain from using it in production or other critical systems.
* By using this code, you assume all risks and liabilities associated with its use.
* Thank you for your understanding and cooperation.
**/
namespace LearnDash\Core\Models;
use LDLMS_Post_Types;
use LearnDash\Core\Models\Traits\Has_Materials;
use WP_User;
/**
* Group model class.
*
* @since 4.6.0
*/
class Group extends Post implements Interfaces\Product {
use Has_Materials;
/**
* Returns allowed post types.
*
* @since 4.6.0
*
* @return string[]
*/
public static function get_allowed_post_types(): array {
return array(
LDLMS_Post_Types::get_post_type_slug( LDLMS_Post_Types::GROUP ),
);
}
/**
* Returns a product model based on the group.
*
* @since 4.6.0
*
* @return Product
*/
public function get_product(): Product {
/**
* Filters a group product.
*
* @since 4.6.0
*
* @param Product $product Product model.
* @param Group $group Group model.
*
* @ignore
*/
return apply_filters(
'learndash_model_group_product',
$this->memoize(
function(): Product {
$product = Product::create_from_post( $this->get_post() );
if ( $this->memoization_is_enabled() ) {
$product->enable_memoization();
}
return $product;
}
),
$this
);
}
/**
* Returns instructors.
*
* @since 4.6.0
*
* @return Instructor[]
*/
public function get_instructors(): array {
/**
* Filters group instructors.
*
* @since 4.6.0
*
* @param Instructor[] $instructors Instructors.
* @param Group $group Group model.
*
* @ignore
*/
return apply_filters(
'learndash_model_group_instructors',
$this->memoize(
function(): array {
$instructors = [];
$limit = 20;
$offset = 0;
do {
$courses = $this->get_courses( $limit, $offset );
foreach ( $courses as $course ) {
foreach ( $course->get_instructors() as $instructor ) {
$instructors[ $instructor->get_id() ] = $instructor;
}
}
$offset += $limit;
} while ( ! empty( $courses ) );
return array_values( $instructors );
}
),
$this
);
}
/**
* Returns related courses models.
*
* @since 4.6.0
*
* @param int $limit Optional. Limit. Default is 0 which will be changed with LD settings.
* @param int $offset Optional. Offset. Default 0.
*
* @return Course[]
*/
public function get_courses( int $limit = 0, int $offset = 0 ): array {
$query_args = [
'offset' => $offset,
];
if ( $limit !== 0 ) {
$query_args['per_page'] = $limit;
}
/**
* Filters group courses.
*
* @since 4.6.0
*
* @param Course[] $courses Courses.
* @param Group $group Group model.
*
* @ignore
*/
return apply_filters(
'learndash_model_group_courses',
$this->memoize(
function() use ( $query_args ): array {
return Course::find_many(
learndash_get_group_courses_list( $this->get_id(), $query_args )
);
}
),
$this
);
}
/**
* Returns the total number of related courses.
*
* @since 4.6.0
*
* @return int
*/
public function get_courses_number(): int {
/**
* Filters group courses number.
*
* @since 4.6.0
*
* @param int $number Number of courses.
* @param Group $group Group model.
*
* @ignore
*/
return apply_filters(
'learndash_model_group_courses_number',
$this->memoize(
function(): int {
return count(
learndash_group_enrolled_courses( $this->get_id() )
);
}
),
$this
);
}
/**
* Returns a certificate link for a user.
*
* @since 4.6.0
*
* @param WP_User $user User.
*
* @return string
*/
public function get_certificate_link( WP_User $user ): string {
/**
* Filters a group certificate link.
*
* @since 4.6.0
*
* @param string $url Group certificate link.
* @param Group $group Group model.
* @param WP_User $user User.
*
* @return string Group certificate link.
*
* @ignore
*/
return apply_filters(
'learndash_model_group_certificate_link',
$this->memoize(
function() use ( $user ): string {
return learndash_get_group_certificate_link( $this->get_id(), $user->ID );
}
),
$this,
$user
);
}
/**
* Returns a status slug for a user.
*
* @since 4.6.0
*
* @param WP_User $user User.
*
* @return string
*/
public function get_status_slug( WP_User $user ): string {
/**
* Filters a group status slug.
*
* @since 4.6.0
*
* @param string $slug Group status slug.
* @param Group $group Group model.
* @param WP_User $user User.
*
* @return string Course status slug.
*
* @ignore
*/
return apply_filters(
'learndash_model_group_status_slug',
$this->memoize(
function() use ( $user ): string {
return learndash_get_user_group_status( $this->get_id(), $user->ID, true );
}
),
$this,
$user
);
}
}