/
llms.table.courses.php
330 lines (273 loc) · 7.18 KB
/
llms.table.courses.php
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
/**
* Courses Reporting Table
*
* @package LifterLMS/Admin/Reporting/Tables/Classes
*
* @since 3.15.0
* @version 3.16.14
*/
defined( 'ABSPATH' ) || exit;
/**
* Courses Reporting Table class
*
* @since 3.15.0
* @version 3.16.14
*/
class LLMS_Table_Courses extends LLMS_Admin_Table {
/**
* Unique ID for the Table
*
* @var string
*/
protected $id = 'courses';
/**
* Value of the field being filtered by
* Only applicable if $filterby is set
*
* @var string
*/
protected $filter = 'any';
/**
* Field results are filtered by
*
* @var string
*/
protected $filterby = 'instructor';
/**
* Is the Table Exportable?
*
* @var boolean
*/
protected $is_exportable = true;
/**
* Determine if the table is filterable
*
* @var boolean
*/
protected $is_filterable = true;
/**
* If true, tfoot will add ajax pagination links
*
* @var boolean
*/
protected $is_paginated = true;
/**
* Determine of the table is searchable
*
* @var boolean
*/
protected $is_searchable = true;
/**
* Results sort order
* 'ASC' or 'DESC'
* Only applicable of $orderby is not set
*
* @var string
*/
protected $order = 'ASC';
/**
* Field results are sorted by
*
* @var string
*/
protected $orderby = 'title';
/**
* Retrieve data for a cell
*
* @param string $key the column id / key
* @param mixed $data object / array of data that the function can use to extract the data
* @return mixed
* @since 3.15.0
* @version 3.15.0
*/
protected function get_data( $key, $data ) {
$course = llms_get_post( $data );
switch ( $key ) {
case 'grade':
$value = $course->get( 'average_grade' ) . '%';
break;
case 'id':
$value = $this->get_post_link( $course->get( 'id' ) );
break;
case 'instructors':
$data = array();
foreach ( $course->get_instructors() as $info ) {
$instructor = llms_get_instructor( $info['id'] );
if ( $instructor ) {
$data[] = sprintf( '%1$s (%2$s)', $instructor->get( 'display_name' ), $info['label'] );
}
}
$value = implode( ', ', $data );
break;
case 'progress':
$value = $this->get_progress_bar_html( $course->get( 'average_progress' ) );
break;
case 'students':
$value = number_format_i18n( $course->get_student_count(), 0 );
break;
case 'title':
$url = LLMS_Admin_Reporting::get_current_tab_url(
array(
'tab' => 'courses',
'course_id' => $course->get( 'id' ),
)
);
$value = '<a href="' . esc_url( $url ) . '">' . $course->get( 'title' ) . '</a>';
break;
default:
$value = $key;
}// End switch().
return $value;
}
/**
* Retrieve a list of Instructors to be used for Filtering
*
* @return array
* @since 3.15.0
* @version 3.15.0
*/
private function get_instructor_filters() {
$query = get_users(
array(
'fields' => array( 'ID', 'display_name' ),
'meta_key' => 'last_name',
'orderby' => 'meta_value',
'role__in' => array( 'administrator', 'lms_manager', 'instructor', 'instructors_assistant' ),
)
);
$instructors = wp_list_pluck( $query, 'display_name', 'ID' );
return $instructors;
}
/**
* Execute a query to retrieve results from the table
*
* @param array $args array of query args
* @return void
* @since 3.15.0
* @version 3.16.14
*/
public function get_results( $args = array() ) {
$this->title = __( 'Courses', 'lifterlms' );
$args = $this->clean_args( $args );
if ( isset( $args['page'] ) ) {
$this->current_page = absint( $args['page'] );
}
$per = apply_filters( 'llms_reporting_' . $this->id . '_per_page', 25 );
$this->order = isset( $args['order'] ) ? $args['order'] : $this->order;
$this->orderby = isset( $args['orderby'] ) ? $args['orderby'] : $this->orderby;
$this->filter = isset( $args['filter'] ) ? $args['filter'] : $this->get_filter();
$this->filterby = isset( $args['filterby'] ) ? $args['filterby'] : $this->get_filterby();
$query_args = array(
'order' => $this->order,
'orderby' => $this->orderby,
'paged' => $this->current_page,
'post_status' => array( 'publish', 'private' ),
'post_type' => 'course',
'posts_per_page' => $per,
);
if ( 'any' !== $this->filter ) {
$serialized_id = serialize(
array(
'id' => absint( $this->filter ),
)
);
$serialized_id = str_replace( array( 'a:1:{', '}' ), '', $serialized_id );
$query_args['meta_query'] = array(
array(
'compare' => 'LIKE',
'key' => '_llms_instructors',
'value' => $serialized_id,
),
);
}
if ( 'progress' === $this->orderby ) {
$query_args['meta_key'] = '_llms_average_progress';
$query_args['orderby'] = 'meta_value_num';
} elseif ( 'grade' === $this->orderby ) {
$query_args['meta_key'] = '_llms_average_progress';
$query_args['orderby'] = 'meta_value_num';
}
if ( isset( $args['search'] ) ) {
$query_args['s'] = sanitize_text_field( $args['search'] );
}
// If you can view others reports, make a regular query.
if ( current_user_can( 'view_others_lifterlms_reports' ) ) {
$query = new WP_Query( $query_args );
// User can only see their own reports, get a list of their students.
} elseif ( current_user_can( 'view_lifterlms_reports' ) ) {
$instructor = llms_get_instructor();
if ( ! $instructor ) {
return;
}
$query = $instructor->get_courses( $query_args, 'query' );
} else {
return;
}
$this->max_pages = $query->max_num_pages;
if ( $this->max_pages > $this->current_page ) {
$this->is_last_page = false;
}
$this->tbody_data = $query->posts;
}
/**
* Get the Text to be used as the placeholder in a searchable tables search input
*
* @return string
* @since 3.2.0
* @version 3.2.0
*/
public function get_table_search_form_placeholder() {
return apply_filters( 'llms_table_get_' . $this->id . '_search_placeholder', __( 'Search courses...', 'lifterlms' ) );
}
/**
* Define the structure of arguments used to pass to the get_results method
*
* @return array
* @since 3.15.0
* @version 3.15.0
*/
public function set_args() {
return array();
}
/**
* Define the structure of the table
*
* @return array
* @since 3.15.0
* @version 3.15.0
*/
protected function set_columns() {
return array(
'id' => array(
'exportable' => true,
'title' => __( 'ID', 'lifterlms' ),
'sortable' => true,
),
'title' => array(
'exportable' => true,
'title' => __( 'Title', 'lifterlms' ),
'sortable' => true,
),
'instructors' => array(
'exportable' => true,
'filterable' => current_user_can( 'view_others_lifterlms_reports' ) ? $this->get_instructor_filters() : false,
'title' => __( 'Instructors', 'lifterlms' ),
),
'students' => array(
'exportable' => true,
'title' => __( 'Students', 'lifterlms' ),
),
'progress' => array(
'exportable' => true,
'title' => __( 'Average Progress', 'lifterlms' ),
'sortable' => true,
),
'grade' => array(
'exportable' => true,
'title' => __( 'Average Grade', 'lifterlms' ),
'sortable' => true,
),
);
}
}