LLMS_Table_Achievements

Display the student achievements reporting table.


Source Source

File: includes/admin/reporting/tables/llms.table.achievements.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
class LLMS_Table_Achievements extends LLMS_Admin_Table {
 
    use LLMS_Trait_Earned_Engagement_Reporting_Table;
 
    /**
     * Unique ID for the Table.
     *
     * @var string
     */
    protected $id = 'achievements';
 
    /**
     * Instance of LLMS_Student.
     *
     * @var null
     */
    protected $student = null;
 
    /**
     * If true, tfoot will add ajax pagination links.
     *
     * @var boolean
     */
    protected $is_paginated = true;
 
    /**
     * Get HTML for buttons in the actions cell of the table.
     *
     * @since 3.18.0
     * @since 6.0.0 Show a button to edit earned achievements.
     *
     * @param int $achievement_id WP Post ID of the achievement post.
     * @return void
     */
    private function get_actions_html( $achievement_id ) {
        ob_start();
        ?>
        <?php if ( get_edit_post_link( $achievement_id ) ) : ?>
        <a class="llms-button-secondary small" href="<?php echo esc_url( get_edit_post_link( $achievement_id ) ); ?>">
            <?php _e( 'Edit', 'lifterlms' ); ?>
            <i class="fa fa-pencil" aria-hidden="true"></i>
        </a>
        <?php endif; ?>
        <form action="" method="POST" style="display:inline;">
 
            <button type="submit" class="llms-button-danger small" id="llms_delete_achievement" name="llms_delete_achievement">
                <?php _e( 'Delete', 'lifterlms' ); ?>
                <i class="fa fa-trash" aria-hidden="true"></i>
            </button>
 
            <input type="hidden" name="achievement_id" value="<?php echo absint( $achievement_id ); ?>">
            <?php wp_nonce_field( 'llms-achievement-actions', '_llms_achievement_actions_nonce' ); ?>
 
        </form>
 
        <script>document.getElementById( 'llms_delete_achievement' ).onclick = function( e ) {
            return window.confirm( '<?php esc_attr_e( 'Are you sure you want to delete this achievement? This action cannot be undone!', 'lifterlms' ); ?>' );
        };</script>
        <?php
        return ob_get_clean();
    }
 
    /**
     * Retrieve data for the columns.
     *
     * @since 3.2.0
     * @since 3.18.0 Unknown.
     * @since 6.0.0 Retrieve earned date using the LLMS_User_Achievement model.
     *
     * @param  string                $key         The column id / key.
     * @param  LLMS_User_Achievement $achievement Object of achievement data.
     * @return mixed
     */
    public function get_data( $key, $achievement ) {
 
        // Handle old object being passed in.
        if ( ! is_a( $achievement, 'LLMS_User_Achievement' ) && property_exists( $achievement, 'achievement_id' ) ) {
            $achievement = new LLMS_User_Achievement( $achievement->certificate_id );
        }
 
        switch ( $key ) {
 
            case 'actions':
                $value = $this->get_actions_html( $achievement->get( 'id' ) );
                break;
 
            case 'related':
                if ( $achievement->get( 'related' ) && 'llms_achievement' !== get_post_type( $achievement->get( 'related' ) ) ) {
                    if ( is_numeric( $achievement->get( 'related' ) ) ) {
                        $value = $this->get_post_link( $achievement->get( 'related' ), get_the_title( $achievement->get( 'related' ) ) );
                    } else {
                        $value = $achievement->get( 'related' );
                    }
                } else {
                    $value = '&ndash;';
                }
                break;
 
            case 'earned':
                $value = $achievement->get_earned_date();
                $value = 'future' === $achievement->get( 'status' ) ? $value . ' ' . __( '(scheduled)', 'lifterlms' ) : $value;
                break;
 
            case 'id':
                $value = $achievement->get( 'id' );
                break;
 
            case 'image':
                $src   = $achievement->get_image( array( 32, 32 ) );
                $value = '<img src="' . esc_url( $src ) . '" alt="' . $achievement->get( 'title' ) . '" width="32" height="32">';
                break;
 
            case 'template_id':
                // Prior to 3.2 this data wasn't recorded.
                $template = $achievement->get( 'parent' );
                if ( $template ) {
                    $value = $this->get_post_link( $template );
                } else {
                    $value = '&ndash;';
                }
                break;
 
            case 'name':
                $value = $achievement->get( 'title' );
                break;
 
            default:
                $value = $key;
 
        }
 
        // Pass the "legacy" object to the filter.
        $backwards_compat_obj = array(
            'post_id'        => $achievement->get( 'related' ),
            'achievement_id' => $achievement->get( 'id' ),
            'earned_date'    => $achievement->get_earned_date(),
        );
 
        return $this->filter_get_data( $value, $key, (object) $backwards_compat_obj );
 
    }
 
    /**
     * Get table results.
     *
     * @since Unknown
     * @since 6.0.0 Don't use deprecated signature for retrieving achievements.
     *              Paginate results.
     *
     * @param array $args Table query arguments.
     * @return void
     */
    public function get_results( $args = array() ) {
 
        $args = $this->clean_args( $args );
 
        if ( is_numeric( $args['student'] ) ) {
            $args['student'] = new LLMS_Student( $args['student'] );
        }
 
        $this->student = $args['student'];
 
        if ( isset( $args['page'] ) ) {
            $this->current_page = absint( $args['page'] );
        }
 
        $query = $this->student->get_achievements(
            array(
                'per_page' => 10,
                'status'   => array( 'publish', 'future' ),
                'paged'    => $this->current_page,
                'sort'     => array(
                    'date' => 'ASC',
                    'ID'   => 'ASC',
                ),
            )
        );
 
        $this->max_pages = $query->get_max_pages();
 
        if ( $this->max_pages > $this->current_page ) {
            $this->is_last_page = false;
        }
 
        $this->tbody_data = $query->get_awards();
 
    }
 
    /**
     * Define the structure of arguments used to pass to the get_results method
     *
     * @since 2.3.0
     * @since 3.35.0 Get student ID more reliably.
     *
     * @return array
     */
    public function set_args() {
 
        $student = false;
        if ( ! empty( $this->student ) ) {
            $student = $this->student->get_id();
        } elseif ( ! empty( $_GET['student_id'] ) ) {
            $student = llms_filter_input( INPUT_GET, 'student_id', FILTER_SANITIZE_NUMBER_INT );
        }
 
        return array(
            'student' => $student,
        );
    }
 
    /**
     * Define the structure of the table
     *
     * @return   array
     * @since    3.2.0
     * @version  3.18.0
     */
    protected function set_columns() {
        return array(
            'id'          => __( 'ID', 'lifterlms' ),
            'template_id' => __( 'Template ID', 'lifterlms' ),
            'name'        => __( 'Achievement Title', 'lifterlms' ),
            'image'       => __( 'Image', 'lifterlms' ),
            'earned'      => __( 'Earned Date', 'lifterlms' ),
            'related'     => __( 'Related Post', 'lifterlms' ),
            'actions'     => '',
        );
    }
 
    /**
     * Empty message displayed when no results are found
     *
     * @return   string
     * @since    3.2.0
     * @version  3.2.0
     */
    protected function set_empty_message() {
        return __( 'This student has not yet earned any achievements.', 'lifterlms' );
    }
 
}


Top ↑

Methods Methods


Top ↑

Changelog Changelog

Changelog
Version Description
6.0.0 Allow pagination.
3.2.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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