LLMS_Table_Course_Students

LLMS_Table_Course_Students class


Source Source

File: includes/admin/reporting/tables/llms.table.course.students.php

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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
class LLMS_Table_Course_Students extends LLMS_Admin_Table {
 
    /**
     * Unique ID for the Table
     *
     * @var  string
     */
    protected $id = 'course-students';
 
    /**
     * 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 = 'status';
 
    /**
     * 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 = 'name';
 
    /**
     * Post ID for the current table
     *
     * @var  int
     */
    public $course_id = null;
 
    /**
     * Retrieve data for the columns
     *
     * @since 3.15.0
     * @since 3.17.2 Unknown.
     *
     * @param string       $key     The column id / key.
     * @param LLMS_Student $student Student object.
     * @return mixed
     */
    public function get_data( $key, $student ) {
 
        $value = '';
 
        switch ( $key ) {
 
            case 'completed':
                $date  = $student->get_completion_date( $this->course_id );
                $value = $date ? $date : '—';
                break;
 
            case 'enrolled':
                $value = $student->get_enrollment_date( $this->course_id, 'updated' );
                break;
 
            case 'grade':
                $value = $student->get_grade( $this->course_id );
                if ( is_numeric( $value ) ) {
                    $value .= '%';
                }
                break;
 
            case 'id':
                $id = $student->get_id();
                if ( current_user_can( 'edit_users', $id ) ) {
                    $value = '<a href="' . esc_url( get_edit_user_link( $id ) ) . '">' . $id . '</a>';
                } else {
                    $value = $id;
                }
                break;
 
            case 'last_lesson':
                $lid = $student->get_last_completed_lesson( $this->course_id );
                if ( $lid ) {
                    $value = $this->get_post_link( $lid, llms_trim_string( get_the_title( $lid ), 30 ) );
                } else {
                    $value = '&ndash;';
                }
                break;
 
            case 'name':
                $first = $student->get( 'first_name' );
                $last  = $student->get( 'last_name' );
 
                if ( ! $first || ! $last ) {
                    $value = $student->get( 'display_name' );
                } else {
                    $value = $last . ', ' . $first;
                }
 
                $url   = add_query_arg(
                    array(
                        'page'       => 'llms-reporting',
                        'tab'        => 'students',
                        'student_id' => $student->get_id(),
                        'stab'       => 'courses',
                        'course_id'  => $this->course_id,
                    ),
                    admin_url( 'admin.php' )
                );
                $value = '<a href="' . esc_url( $url ) . '">' . $value . '</a>';
 
                break;
 
            case 'progress':
                $value = $this->get_progress_bar_html( $student->get_progress( $this->course_id ) );
                break;
 
            case 'status':
                $value = llms_get_enrollment_status_name( $student->get_enrollment_status( $this->course_id ) );
                break;
 
            default:
                $value = $key;
 
        }// End switch().
 
        return $this->filter_get_data( $value, $key, $student );
 
    }
 
    /**
     * Retrieve data for a cell in an export file
     * Should be overridden in extending classes
     *
     * @param    string $key        the column id / key
     * @param    obj    $student    Instance of the LLMS_Student
     * @return   mixed
     * @since    3.15.0
     * @version  3.15.0
     */
    public function get_export_data( $key, $student ) {
 
        switch ( $key ) {
 
            case 'id':
                $value = $student->get_id();
                break;
 
            case 'email':
                $value = $student->get( 'user_email' );
                break;
 
            case 'name_first':
                $value = $student->get( 'first_name' );
                break;
 
            case 'name_last':
                $value = $student->get( 'last_name' );
                break;
 
            case 'progress':
                $value = $student->get_progress( $this->course_id ) . '%';
                break;
 
            default:
                $value = $this->get_data( $key, $student );
 
        }// End switch().
 
        return $this->filter_get_data( $value, $key, $student, 'export' );
 
    }
 
 
    /**
     * Get a lock key unique to the table & user for locking the table during export generation
     *
     * @return   string
     * @since    3.15.0
     * @version  3.15.0
     */
    public function get_export_lock_key() {
        $args = $this->get_args();
        return sprintf( '%1$s:%2$d:%3$d', $this->id, get_current_user_id(), $args['course_id'] );
    }
 
    /**
     * Allow customization of the title for export files
     *
     * @param    array $args   optional arguments passed from table to csv processor
     * @return   string
     * @since    3.15.0
     * @version  3.15.0
     */
    public function get_export_title( $args = array() ) {
        $title = $this->get_title();
        if ( isset( $args['course_id'] ) ) {
            $title = get_the_title( $args['course_id'] ) . ' ' . $title;
        }
        return apply_filters( 'llms_table_get_' . $this->id . '_export_title', $title );
    }
 
    /**
     * Get the Text to be used as the placeholder in a searchable tables search input
     *
     * @return   string
     * @since    3.15.0
     * @version  3.15.0
     */
    public function get_table_search_form_placeholder() {
        return apply_filters( 'llms_reporting_get_' . $this->id . '_search_placeholder', __( 'Search students by name or email...', 'lifterlms' ) );
    }
 
    /**
     * Execute a query to retrieve results from the table.
     *
     * @since 3.15.0
     * @since 5.10.0 Add ability to sort by completion date.
     * @since 6.0.0 Don't access `LLMS_Student_Query` properties directly.
     *
     * @param array $args Array of query args.
     * @return void
     */
    public function get_results( $args = array() ) {
 
        $this->title = __( 'Students', 'lifterlms' );
 
        if ( ! $args ) {
            $args = $this->get_args();
        }
 
        $args = $this->clean_args( $args );
 
        $this->course_id = $args['course_id'];
 
        if ( isset( $args['page'] ) ) {
            $this->current_page = absint( $args['page'] );
        }
 
        $this->filter   = isset( $args['filter'] ) ? $args['filter'] : $this->get_filter();
        $this->filterby = isset( $args['filterby'] ) ? $args['filterby'] : $this->get_filterby();
 
        $this->order   = isset( $args['order'] ) ? $args['order'] : $this->get_order();
        $this->orderby = isset( $args['orderby'] ) ? $args['orderby'] : $this->get_orderby();
 
        $sort = array();
        switch ( $this->get_orderby() ) {
 
            case 'completed':
                $sort = array(
                    'completed'  => $this->get_order(),
                    'last_name'  => 'ASC',
                    'first_name' => 'ASC',
                    'id'         => 'ASC',
                );
                break;
 
            case 'enrolled':
                $sort = array(
                    'date'       => $this->get_order(),
                    'last_name'  => 'ASC',
                    'first_name' => 'ASC',
                    'id'         => 'ASC',
                );
                break;
 
            case 'id':
                $sort = array(
                    'id' => $this->get_order(),
                );
                break;
 
            case 'name':
                $sort = array(
                    'last_name'  => $this->get_order(),
                    'first_name' => 'ASC',
                    'id'         => 'ASC',
                );
                break;
 
            case 'status':
                $sort = array(
                    'status'     => $this->get_order(),
                    'last_name'  => 'ASC',
                    'first_name' => 'ASC',
                    'id'         => 'ASC',
                );
                break;
 
        }
 
        $query_args = array(
            'page'     => $this->get_current_page(),
            'post_id'  => $args['course_id'],
            'per_page' => apply_filters( 'llms_' . $this->id . '_table_students_per_page', 25 ),
            'sort'     => $sort,
        );
 
        if ( 'status' === $this->get_filterby() && 'any' !== $this->get_filter() ) {
 
            $query_args['statuses'] = array( $this->get_filter() );
 
        }
 
        if ( isset( $args['search'] ) ) {
 
            $this->search         = $args['search'];
            $query_args['search'] = $this->get_search();
 
        }
 
        $query = new LLMS_Student_Query( $query_args );
 
        $this->max_pages    = $query->get_max_pages();
        $this->is_last_page = $query->is_last_page();
 
        $this->tbody_data = $query->get_students();
 
    }
 
 
    /**
     * 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() {
 
        if ( ! $this->course_id ) {
            $this->course_id = ! empty( $_GET['course_id'] ) ? absint( $_GET['course_id'] ) : null;
        }
 
        return array(
            'course_id' => $this->course_id,
        );
 
    }
 
    /**
     * Define the structure of the table
     *
     * @return   array
     * @since    3.15.0
     * @version  3.16.11
     */
    public function set_columns() {
        $cols = array(
            'id'          => array(
                'exportable' => true,
                'sortable'   => true,
                'title'      => __( 'ID', 'lifterlms' ),
            ),
            'name'        => array(
                'sortable' => true,
                'title'    => __( 'Name', 'lifterlms' ),
            ),
            'name_last'   => array(
                'exportable'  => true,
                'export_only' => true,
                'title'       => __( 'Last Name', 'lifterlms' ),
            ),
            'name_first'  => array(
                'exportable'  => true,
                'export_only' => true,
                'title'       => __( 'First Name', 'lifterlms' ),
            ),
            'email'       => array(
                'exportable'  => true,
                'export_only' => true,
                'title'       => __( 'Email', 'lifterlms' ),
            ),
            'status'      => array(
                'exportable' => true,
                'filterable' => llms_get_enrollment_statuses(),
                'sortable'   => true,
                'title'      => __( 'Status', 'lifterlms' ),
            ),
            'enrolled'    => array(
                'exportable' => true,
                'sortable'   => true,
                'title'      => __( 'Enrollment Updated', 'lifterlms' ),
            ),
            'completed'   => array(
                'exportable' => true,
                'sortable'   => true,
                'title'      => __( 'Completed', 'lifterlms' ),
            ),
            'progress'    => array(
                'exportable' => true,
                'sortable'   => false,
                'title'      => __( 'Progress', 'lifterlms' ),
            ),
            'grade'       => array(
                'exportable' => true,
                'sortable'   => false,
                'title'      => __( 'Grade', 'lifterlms' ),
            ),
            'last_lesson' => array(
                'sortable' => false,
                'title'    => __( 'Last Lesson', 'lifterlms' ),
            ),
        );
 
        return $cols;
 
    }
 
}


Top ↑

Methods Methods

  • get_data — Retrieve data for the columns
  • get_export_data — Retrieve data for a cell in an export file Should be overridden in extending classes
  • get_export_lock_key — Get a lock key unique to the table & user for locking the table during export generation
  • get_export_title — Allow customization of the title for export files
  • get_results — Execute a query to retrieve results from the table.
  • get_table_search_form_placeholder — Get the Text to be used as the placeholder in a searchable tables search input
  • set_args — Define the structure of arguments used to pass to the get_results method
  • set_columns — Define the structure of the table

Top ↑

Changelog Changelog

Changelog
Version Description
3.15.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.