1: <?php
2: if ( ! defined( 'ABSPATH' ) ) exit;
3:
4: 5: 6: 7: 8: 9: 10: 11:
12: class Sensei_Analysis_Lesson_List_Table extends WooThemes_Sensei_List_Table {
13: public $lesson_id;
14: public $course_id;
15: public $page_slug = 'sensei_analysis';
16:
17: 18: 19: 20:
21: public function __construct ( $lesson_id = 0 ) {
22: $this->lesson_id = intval( $lesson_id );
23: $this->course_id = intval( get_post_meta( $this->lesson_id, '_lesson_course', true ) );
24:
25:
26: parent::__construct( 'analysis_lesson' );
27:
28:
29: add_action( 'sensei_before_list_table', array( $this, 'data_table_header' ) );
30: add_action( 'sensei_after_list_table', array( $this, 'data_table_footer' ) );
31:
32: add_filter( 'sensei_list_table_search_button_text', array( $this, 'search_button' ) );
33: }
34:
35: 36: 37: 38: 39:
40: function get_columns() {
41: $columns = array(
42: 'title' => __( 'Learner', 'woothemes-sensei' ),
43: 'started' => __( 'Date Started', 'woothemes-sensei' ),
44: 'completed' => __( 'Date Completed', 'woothemes-sensei' ),
45: 'status' => __( 'Status', 'woothemes-sensei' ),
46: 'grade' => __( 'Grade', 'woothemes-sensei' ),
47: );
48: $columns = apply_filters( 'sensei_analysis_lesson_columns', $columns, $this );
49: return $columns;
50: }
51:
52: 53: 54: 55: 56:
57: function get_sortable_columns() {
58: $columns = array(
59: 'title' => array( 'title', false ),
60: 'started' => array( 'started', false ),
61: 'completed' => array( 'completed', false ),
62: 'status' => array( 'status', false ),
63: 'grade' => array( 'grade', false ),
64: );
65: $columns = apply_filters( 'sensei_analysis_lesson_columns_sortable', $columns, $this );
66: return $columns;
67: }
68:
69: 70: 71: 72: 73:
74: public function prepare_items() {
75: global $per_page;
76:
77:
78: $orderby = '';
79: if ( !empty( $_GET['orderby'] ) ) {
80: if ( array_key_exists( esc_html( $_GET['orderby'] ), $this->get_sortable_columns() ) ) {
81: $orderby = esc_html( $_GET['orderby'] );
82: }
83: }
84:
85:
86: $order = 'ASC';
87: if ( !empty( $_GET['order'] ) ) {
88: $order = ( 'ASC' == strtoupper($_GET['order']) ) ? 'ASC' : 'DESC';
89: }
90:
91:
92: $search = false;
93: if ( !empty( $_GET['s'] ) ) {
94: $search = esc_html( $_GET['s'] );
95: }
96: $this->search = $search;
97:
98: $per_page = $this->get_items_per_page( 'sensei_comments_per_page' );
99: $per_page = apply_filters( 'sensei_comments_per_page', $per_page, 'sensei_comments' );
100:
101: $paged = $this->get_pagenum();
102: $offset = 0;
103: if ( !empty($paged) ) {
104: $offset = $per_page * ( $paged - 1 );
105: }
106:
107: $args = array(
108: 'number' => $per_page,
109: 'offset' => $offset,
110: 'orderby' => $orderby,
111: 'order' => $order,
112: );
113: if ( $this->search ) {
114: $args['search'] = $this->search;
115: }
116:
117: $this->items = $this->get_lesson_statuses( $args );
118:
119: $total_items = $this->total_items;
120: $total_pages = ceil( $total_items / $per_page );
121: $this->set_pagination_args( array(
122: 'total_items' => $total_items,
123: 'total_pages' => $total_pages,
124: 'per_page' => $per_page
125: ) );
126: }
127:
128: 129: 130: 131: 132:
133: public function generate_report( $report ) {
134:
135: $data = array();
136:
137: $this->csv_output = true;
138:
139:
140: $orderby = '';
141: if ( !empty( $_GET['orderby'] ) ) {
142: if ( array_key_exists( esc_html( $_GET['orderby'] ), $this->get_sortable_columns() ) ) {
143: $orderby = esc_html( $_GET['orderby'] );
144: }
145: }
146:
147:
148: $order = 'ASC';
149: if ( !empty( $_GET['order'] ) ) {
150: $order = ( 'ASC' == strtoupper($_GET['order']) ) ? 'ASC' : 'DESC';
151: }
152:
153:
154: $search = false;
155: if ( !empty( $_GET['s'] ) ) {
156: $search = esc_html( $_GET['s'] );
157: }
158: $this->search = $search;
159:
160: $args = array(
161: 'orderby' => $orderby,
162: 'order' => $order,
163: );
164: if ( $this->search ) {
165: $args['search'] = $this->search;
166: }
167:
168:
169: $column_headers = array();
170: $columns = $this->get_columns();
171: foreach( $columns AS $key => $title ) {
172: $column_headers[] = $title;
173: }
174: $data[] = $column_headers;
175:
176: $this->items = $this->get_lesson_statuses( $args );
177:
178:
179: foreach( $this->items AS $item) {
180: $data[] = $this->get_row_data( $item );
181: }
182:
183: return $data;
184: }
185:
186: 187: 188: 189: 190: 191:
192: protected function get_row_data( $item ) {
193:
194: $user_start_date = get_comment_meta( $item->comment_ID, 'start', true );
195: $user_end_date = $item->comment_date;
196: $status_class = $grade = '';
197:
198: if( 'complete' == $item->comment_approved ) {
199: $status = __( 'Completed', 'woothemes-sensei' );
200: $status_class = 'graded';
201:
202: $grade = __( 'No Grade', 'woothemes-sensei' );
203: }
204: elseif( 'graded' == $item->comment_approved ) {
205: $status = __( 'Graded', 'woothemes-sensei' ) ;
206: $status_class = 'graded';
207:
208: $grade = get_comment_meta( $item->comment_ID, 'grade', true);
209: }
210: elseif( 'passed' == $item->comment_approved ) {
211: $status = __( 'Passed', 'woothemes-sensei' );
212: $status_class = 'graded';
213:
214: $grade = get_comment_meta( $item->comment_ID, 'grade', true);
215: }
216: elseif( 'failed' == $item->comment_approved ) {
217: $status = __( 'Failed', 'woothemes-sensei' );
218: $status_class = 'failed';
219:
220: $grade = get_comment_meta( $item->comment_ID, 'grade', true);
221: }
222: elseif( 'ungraded' == $item->comment_approved ) {
223: $status = __( 'Ungraded', 'woothemes-sensei' );
224: $status_class = 'ungraded';
225:
226: }
227: else {
228: $status = __( 'In Progress', 'woothemes-sensei' );
229: $user_end_date = '';
230: }
231:
232:
233: $user_name = Sensei_Learner::get_full_name( $item->user_id );
234:
235: if ( !$this->csv_output ) {
236: $url = add_query_arg( array( 'page' => $this->page_slug, 'user_id' => $item->user_id, 'course_id' => $this->course_id ), admin_url( 'admin.php' ) );
237:
238: $user_name = '<strong><a class="row-title" href="' . esc_url( $url ) . '">' . $user_name . '</a></strong>';
239: $status = sprintf( '<span class="%s">%s</span>', $item->comment_approved, $status );
240: if ( is_numeric($grade) ) {
241: $grade .= '%';
242: }
243: }
244: $column_data = apply_filters( 'sensei_analysis_lesson_column_data', array( 'title' => $user_name,
245: 'started' => $user_start_date,
246: 'completed' => $user_end_date,
247: 'status' => $status,
248: 'grade' => $grade,
249: ), $item, $this );
250:
251: return $column_data;
252: }
253:
254: 255: 256: 257: 258:
259: private function get_lesson_statuses( $args ) {
260:
261: $activity_args = array(
262: 'post_id' => $this->lesson_id,
263: 'type' => 'sensei_lesson_status',
264: 'number' => $args['number'],
265: 'offset' => $args['offset'],
266: 'orderby' => $args['orderby'],
267: 'order' => $args['order'],
268: 'status' => 'any',
269: );
270:
271:
272: if ( $this->search ) {
273: $user_args = array(
274: 'search' => '*' . $this->search . '*',
275: 'fields' => 'ID',
276: );
277:
278: $user_args = apply_filters( 'sensei_analysis_lesson_search_users', $user_args );
279: if ( !empty( $user_args ) ) {
280: $learners_search = new WP_User_Query( $user_args );
281:
282: $activity_args['user_id'] = (array) $learners_search->get_results();
283: }
284: }
285:
286: $activity_args = apply_filters( 'sensei_analysis_lesson_filter_statuses', $activity_args );
287:
288:
289: $this->total_items = Sensei_Utils::sensei_check_for_activity( array_merge( $activity_args, array('count' => true, 'offset' => 0, 'number' => 0) ) );
290:
291:
292: if ( $this->total_items < $activity_args['offset'] ) {
293: $new_paged = floor( $total_statuses / $activity_args['number'] );
294: $activity_args['offset'] = $new_paged * $activity_args['number'];
295: }
296: $statuses = Sensei_Utils::sensei_check_for_activity( $activity_args, true );
297:
298: if ( !is_array($statuses) ) {
299: $statuses = array( $statuses );
300: }
301: return $statuses;
302: }
303:
304: 305: 306: 307: 308: 309:
310: public function no_items() {
311: _e( 'No learners found.', 'woothemes-sensei' );
312: }
313:
314: 315: 316: 317: 318:
319: public function data_table_header() {
320: echo '<strong>' . __( 'Learners taking this Lesson', 'woothemes-sensei' ) . '</strong>';
321: }
322:
323: 324: 325: 326: 327:
328: public function data_table_footer() {
329: $lesson = get_post( $this->lesson_id );
330: $report = sanitize_title( $lesson->post_title ) . '-learners-overview';
331: $url = add_query_arg( array( 'page' => $this->page_slug, 'lesson_id' => $this->lesson_id, 'sensei_report_download' => $report ), admin_url( 'admin.php' ) );
332: echo '<a class="button button-primary" href="' . esc_url( wp_nonce_url( $url, 'sensei_csv_download-' . $report, '_sdl_nonce' ) ) . '">' . __( 'Export all rows (CSV)', 'woothemes-sensei' ) . '</a>';
333: }
334:
335: 336: 337: 338: 339:
340: public function search_button( $text = '' ) {
341:
342: $text = __( 'Search Learners', 'woothemes-sensei' );
343:
344: return $text;
345:
346: }
347: }
348:
349:
350: 351: 352: 353: 354: 355: 356:
357: class WooThemes_Sensei_Analysis_Lesson_List_Table extends Sensei_Analysis_Lesson_List_Table {}
358: