LLMS_Table_Quizzes

Quizzes Reporting Table class


Source Source

File: includes/admin/reporting/tables/llms.table.quizzes.php

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
466
467
468
class LLMS_Table_Quizzes extends LLMS_Admin_Table {
 
    /**
     * Unique ID for the Table
     *
     * @var string
     */
    protected $id = 'quizzes';
 
    /**
     * 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';
 
    /**
     * Get HTML for buttons in the actions cell of the table
     *
     * @since 3.37.8
     * @since 4.2.0 Added a deep check on whether the quiz is associated to a lesson.
     * @since 6.0.0 Don't access `LLMS_Query_Quiz_Attempt` properties directly.
     *
     * @param LLMS_Quiz $quiz Quiz object.
     * @return string
     */
    private function get_actions_html( $quiz ) {
 
        if ( ! $quiz->is_orphan( true ) && $quiz->get_course() ) {
            return '';
        }
 
        // If there are quiz attempts for the quiz let the admin know they're going to delete the attempts also.
        $query = new LLMS_Query_Quiz_Attempt(
            array(
                'quiz_id'  => $quiz->get( 'id' ),
                'per_page' => 1,
            )
        );
 
        $msg  = $query->has_results() ? __( 'Are you sure you want to delete this quiz and all associated student attempts?', 'lifterlms' ) : __( 'Are you sure you want to delete this quiz?', 'lifterlms' );
        $msg .= ' ' . __( 'This action cannot be undone!', 'lifterlms' );
 
        ob_start();
        ?>
        <form action="" method="POST" style="display:inline;">
 
            <button type="submit" class="llms-button-danger small" id="llms-del-quiz-<?php echo $quiz->get( 'id' ); ?>" name="llms_del_quiz" value="<?php echo $quiz->get( 'id' ); ?>">
                <?php _e( 'Delete', 'lifterlms' ); ?>
                <i class="fa fa-trash" aria-hidden="true"></i>
            </button>
 
            <input type="hidden" name="_llms_quiz_actions_nonce" value="<?php echo wp_create_nonce( 'llms-quiz-actions' ); ?>">
 
        </form>
 
        <script>document.getElementById( 'llms-del-quiz-<?php echo $quiz->get( 'id' ); ?>' ).onclick = function( e ) {
            return window.confirm( '<?php echo esc_attr( $msg ); ?>' );
        };</script>
        <?php
        return ob_get_clean();
 
    }
 
    /**
     * Retrieve data for a cell
     *
     * @since 3.16.0
     * @since 3.24.0 Unknown.
     * @since 3.37.8 Add actions column that allows deletion of orphaned quizzes.
     *               ID column displays as plain text if the quiz is not editable and directs to the quiz within the course builder when it is.
     * @since 4.2.0 Added a deep check on whether the quiz is associated to a lesson.
     * @since 6.0.0 Don't access `LLMS_Query_Quiz_Attempt` properties directly.
     * @since 7.1.3 Added `round()` method for 'average' column values with precision from `llms_get_floats_rounding_precision()` helper.
     *
     * @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
     */
    protected function get_data( $key, $data ) {
 
        $quiz = llms_get_post( $data );
 
        switch ( $key ) {
 
            case 'actions':
                $value = $this->get_actions_html( $quiz );
                break;
 
            case 'attempts':
                $query = new LLMS_Query_Quiz_Attempt(
                    array(
                        'quiz_id'  => $quiz->get( 'id' ),
                        'per_page' => 1,
                    )
                );
 
                $url   = LLMS_Admin_Reporting::get_current_tab_url(
                    array(
                        'tab'     => 'quizzes',
                        'stab'    => 'attempts',
                        'quiz_id' => $quiz->get( 'id' ),
                    )
                );
                $value = '<a href="' . $url . '">' . $query->get_found_results() . '</a>';
 
                break;
 
            case 'average':
                $grade = 0;
                $query = new LLMS_Query_Quiz_Attempt(
                    array(
                        'quiz_id'  => $quiz->get( 'id' ),
                        'per_page' => 1000,
                    )
                );
 
                $attempts = $query->get_number_results();
 
                if ( ! $attempts ) {
                    $value = 0;
                } else {
 
                    foreach ( $query->get_attempts() as $attempt ) {
                        $grade += $attempt->get( 'grade' );
                    }
 
                    $value = round( $grade / $attempts, llms_get_floats_rounding_precision() ) . '%';
 
                }
 
                break;
 
            case 'course':
                $value  = '&mdash;';
                $course = $quiz->get_course();
                if ( $course ) {
                    $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;
 
            case 'id':
                $id    = $quiz->get( 'id' );
                $value = $id;
 
                $course = $quiz->get_course();
                if ( ! $quiz->is_orphan( true ) && $course ) {
 
                    $url = add_query_arg(
                        array(
                            'page'      => 'llms-course-builder',
                            'course_id' => $course->get( 'id' ),
                        ),
                        admin_url( 'admin.php' )
                    );
 
                    $url  .= sprintf( '#lesson:%d:quiz', $quiz->get( 'lesson_id' ) );
                    $value = '<a href="' . esc_url( $url ) . '">' . $id . '</a>';
                }
                break;
 
            case 'lesson':
                $value  = '&mdash;';
                $lesson = $quiz->get_lesson();
                if ( $lesson ) {
                    $value = $lesson->get( 'title' );
                }
                break;
 
            case 'title':
                $value = $quiz->get( 'title' );
                $url   = LLMS_Admin_Reporting::get_current_tab_url(
                    array(
                        'tab'     => 'quizzes',
                        'quiz_id' => $quiz->get( 'id' ),
                    )
                );
                $value = '<a href="' . esc_url( $url ) . '">' . $quiz->get( 'title' ) . '</a>';
                break;
 
            default:
                $value = $key;
 
        }
 
        return $this->filter_get_data( $value, $key, $data );
 
    }
 
    /**
     * Retrieve a list of Instructors to be used for Filtering
     *
     * @since 3.16.0
     *
     * @return array
     */
    private function get_instructor_filters() {
 
        $query = get_users(
            array(
                'fields'   => array( 'ID', 'display_name' ),
                'meta_key' => 'last_name', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
                '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
     *
     * @since 3.16.0
     * @since 3.36.1 Fixed an issue that allow instructors, who can only see their own reports,
     *               to see all the quizzes when they had no courses or courses with no lessons.
     *
     * @param array $args Array of query args.
     * @return void
     */
    public function get_results( $args = array() ) {
 
        $this->title = __( 'Quizzes', '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'    => 'publish',
            'post_type'      => 'llms_quiz',
            'posts_per_page' => $per,
        );
 
        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;
            }
 
            $lessons = array();
            $courses = $instructor->get_courses(
                array(
                    'posts_per_page' => -1,
                )
            );
            foreach ( $courses as $course ) {
                $lessons = array_merge( $lessons, $course->get_lessons( 'ids' ) );
            }
 
            if ( empty( $lessons ) ) {
                return;
            }
 
            $query_args['meta_query'] = array// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
                array(
                    'compare' => 'IN',
                    'key'     => '_llms_lesson_id',
                    'value'   => $lessons,
                ),
            );
 
            $query = new WP_Query( $query_args );
 
        } 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
     *
     * @since 3.16.0
     *
     * @return string
     */
    public function get_table_search_form_placeholder() {
 
        /**
         * Filter the placeholder used in the search input on the quizzes reporting table.
         *
         * @since 3.16.0
         *
         * @param string $placeholder The placeholder string.
         */
        return apply_filters( 'llms_table_get_quizzes_search_placeholder', __( 'Search quizzes...', 'lifterlms' ) );
 
    }
 
    /**
     * Define the structure of arguments used to pass to the get_results method
     *
     * @since 3.16.0
     *
     * @return array
     */
    public function set_args() {
        return array();
    }
 
    /**
     * Define the structure of the table
     *
     * @since 3.16.0
     * @since 3.16.10 Unknown.
     * @since 3.37.8 Added the 'actions' column.
     * @since 3.37.12 Fixed the 'actions' column name.
     *
     * @return array
     */
    protected function set_columns() {
        return array(
            'id'       => array(
                'exportable' => true,
                'title'      => __( 'ID', 'lifterlms' ),
                'sortable'   => true,
            ),
            'title'    => array(
                'exportable' => true,
                'title'      => __( 'Title', 'lifterlms' ),
                'sortable'   => true,
            ),
            'course'   => array(
                'exportable' => true,
                'title'      => __( 'Course', 'lifterlms' ),
                'sortable'   => false,
            ),
            'lesson'   => array(
                'exportable' => true,
                'title'      => __( 'Lesson', 'lifterlms' ),
                'sortable'   => false,
            ),
            'attempts' => array(
                'exportable' => true,
                'title'      => __( 'Total Attempts', 'lifterlms' ),
                'sortable'   => false,
            ),
            'average'  => array(
                'exportable' => true,
                'title'      => __( 'Average Grade', 'lifterlms' ),
                'sortable'   => false,
            ),
            'actions'  => array(
                'exportable' => false,
                'title'      => __( 'Actions', 'lifterlms' ),
                'sortable'   => false,
            ),
        );
    }


Top ↑

Methods Methods


Top ↑

Changelog Changelog

Changelog
Version Description
4.2.0 Added deep checks on whether the quiz is associated to a lesson.
3.37.8 Allow orphaned quizzes to be deleted. Output quiz IDs as plain text (no link) when they cannot be edited and link to the quiz within the course builder when they can.
3.37.12 Fixed the 'actions' column name.
3.36.1 Fixed an issue that allow instructors, who can only see their own reports, to see all the quizzes when they had no courses or courses with no lessons.
3.16.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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