1: <?php
2: if ( ! defined( 'ABSPATH' ) ) exit;
3:
4: 5: 6: 7: 8: 9: 10: 11:
12: class Sensei_Analysis_User_Profile_List_Table extends Sensei_List_Table {
13: public $user_id;
14: public $page_slug = 'sensei_analysis';
15:
16: 17: 18: 19: 20:
21: public function __construct ( $user_id = 0 ) {
22: $this->user_id = intval( $user_id );
23:
24:
25: parent::__construct( 'analysis_user_profile' );
26:
27:
28: add_action( 'sensei_before_list_table', array( $this, 'data_table_header' ) );
29: add_action( 'sensei_after_list_table', array( $this, 'data_table_footer' ) );
30:
31: add_filter( 'sensei_list_table_search_button_text', array( $this, 'search_button' ) );
32: }
33:
34: 35: 36: 37: 38:
39: function get_columns() {
40: $columns = array(
41: 'title' => __( 'Course', 'woothemes-sensei' ),
42: 'started' => __( 'Date Started', 'woothemes-sensei' ),
43: 'completed' => __( 'Date Completed', 'woothemes-sensei' ),
44: 'status' => __( 'Status', 'woothemes-sensei' ),
45:
46: 'percent' => __( 'Percent Complete', 'woothemes-sensei' ),
47: );
48: $columns = apply_filters( 'sensei_analysis_user_profile_columns', $columns );
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:
64: 'percent' => array( 'percent', false )
65: );
66: $columns = apply_filters( 'sensei_analysis_user_profile_columns_sortable', $columns );
67: return $columns;
68: }
69:
70: 71: 72: 73: 74:
75: public function prepare_items() {
76: global $per_page;
77:
78:
79: $orderby = '';
80: if ( !empty( $_GET['orderby'] ) ) {
81: if ( array_key_exists( esc_html( $_GET['orderby'] ), $this->get_sortable_columns() ) ) {
82: $orderby = esc_html( $_GET['orderby'] );
83: }
84: }
85:
86:
87: $order = 'ASC';
88: if ( !empty( $_GET['order'] ) ) {
89: $order = ( 'ASC' == strtoupper($_GET['order']) ) ? 'ASC' : 'DESC';
90: }
91:
92:
93: $search = false;
94: if ( !empty( $_GET['s'] ) ) {
95: $search = esc_html( $_GET['s'] );
96: }
97: $this->search = $search;
98:
99: $per_page = $this->get_items_per_page( 'sensei_comments_per_page' );
100: $per_page = apply_filters( 'sensei_comments_per_page', $per_page, 'sensei_comments' );
101:
102: $paged = $this->get_pagenum();
103: $offset = 0;
104: if ( !empty($paged) ) {
105: $offset = $per_page * ( $paged - 1 );
106: }
107:
108: $args = array(
109: 'number' => $per_page,
110: 'offset' => $offset,
111: 'orderby' => $orderby,
112: 'order' => $order,
113: );
114: if ( $this->search ) {
115: $args['search'] = $this->search;
116: }
117:
118: $this->items = $this->get_course_statuses( $args );
119:
120: $total_items = $this->total_items;
121: $total_pages = ceil( $total_items / $per_page );
122: $this->set_pagination_args( array(
123: 'total_items' => $total_items,
124: 'total_pages' => $total_pages,
125: 'per_page' => $per_page
126: ) );
127: }
128:
129: 130: 131: 132: 133:
134: public function generate_report( $report ) {
135:
136: $data = array();
137:
138: $this->csv_output = true;
139:
140:
141: $orderby = '';
142: if ( !empty( $_GET['orderby'] ) ) {
143: if ( array_key_exists( esc_html( $_GET['orderby'] ), $this->get_sortable_columns() ) ) {
144: $orderby = esc_html( $_GET['orderby'] );
145: }
146: }
147:
148:
149: $order = 'ASC';
150: if ( !empty( $_GET['order'] ) ) {
151: $order = ( 'ASC' == strtoupper($_GET['order']) ) ? 'ASC' : 'DESC';
152: }
153:
154:
155: $search = false;
156: if ( !empty( $_GET['s'] ) ) {
157: $search = esc_html( $_GET['s'] );
158: }
159: $this->search = $search;
160:
161: $args = array(
162: 'orderby' => $orderby,
163: 'order' => $order,
164: );
165: if ( $this->search ) {
166: $args['search'] = $this->search;
167: }
168:
169:
170: $column_headers = array();
171: $columns = $this->get_columns();
172: foreach( $columns AS $key => $title ) {
173: $column_headers[] = $title;
174: }
175: $data[] = $column_headers;
176:
177: $this->items = $this->get_course_statuses( $args );
178:
179:
180: foreach( $this->items AS $item) {
181: $data[] = $this->get_row_data( $item );
182: }
183:
184: return $data;
185: }
186:
187: 188: 189: 190: 191:
192: protected function get_row_data( $item ) {
193:
194: $course_title = get_the_title( $item->comment_post_ID );
195: $course_percent = get_comment_meta( $item->comment_ID, 'percent', true );
196: $course_start_date = get_comment_meta( $item->comment_ID, 'start', true );
197: $course_end_date = '';
198:
199: if( 'complete' == $item->comment_approved ) {
200:
201: $status = __( 'Completed', 'woothemes-sensei' );
202: $status_class = 'graded';
203:
204: $course_end_date = $item->comment_date;
205:
206: } else {
207:
208: $status = __( 'In Progress', 'woothemes-sensei' );
209: $status_class = 'in-progress';
210:
211: }
212:
213:
214: if ( !$this->csv_output ) {
215: $url = add_query_arg( array( 'page' => $this->page_slug, 'user_id' => $this->user_id, 'course_id' => $item->comment_post_ID ), admin_url( 'admin.php' ) );
216:
217: $course_title = '<strong><a class="row-title" href="' . esc_url( $url ) . '">' . $course_title . '</a></strong>';
218: $status = sprintf( '<span class="%s">%s</span>', $status_class, $status );
219: if ( is_numeric($course_percent) ) {
220: $course_percent .= '%';
221: }
222: }
223: $column_data = apply_filters( 'sensei_analysis_user_profile_column_data', array( 'title' => $course_title,
224: 'started' => $course_start_date,
225: 'completed' => $course_end_date,
226: 'status' => $status,
227: 'percent' => $course_percent,
228: ), $item );
229:
230: return $column_data;
231: }
232:
233: 234: 235: 236: 237:
238: private function get_course_statuses( $args ) {
239:
240: $activity_args = array(
241: 'user_id' => $this->user_id,
242: 'type' => 'sensei_course_status',
243: 'number' => $args['number'],
244: 'offset' => $args['offset'],
245: 'orderby' => $args['orderby'],
246: 'order' => $args['order'],
247: 'status' => 'any',
248: );
249:
250: $activity_args = apply_filters( 'sensei_analysis_user_profile_filter_statuses', $activity_args );
251:
252:
253: $this->total_items = Sensei_Utils::sensei_check_for_activity( array_merge( $activity_args, array('count' => true, 'offset' => 0, 'number' => 0) ) );
254:
255:
256: if ( $this->total_items < $activity_args['offset'] ) {
257:
258: $new_paged = floor( $this->total_items / $activity_args['number'] );
259: $activity_args['offset'] = $new_paged * $activity_args['number'];
260:
261: }
262: $statuses = Sensei_Utils::sensei_check_for_activity( $activity_args, true );
263:
264:
265: if ( !is_array($statuses) ) {
266: $statuses = array( $statuses );
267: }
268:
269: return $statuses;
270: }
271:
272: 273: 274: 275: 276: 277:
278: public function no_items() {
279: echo __( 'No courses found.', 'woothemes-sensei' );
280: }
281:
282: 283: 284: 285: 286:
287: public function data_table_header() {
288: echo '<strong>' . __( 'Courses', 'woothemes-sensei' ) . '</strong>';
289: }
290:
291: 292: 293: 294: 295:
296: public function data_table_footer() {
297: $user = get_user_by( 'id', $this->user_id );
298: $report = sanitize_title( $user->display_name ) . '-course-overview';
299: $url = add_query_arg( array( 'page' => $this->page_slug, 'user_id' => $this->user_id, 'sensei_report_download' => $report ), admin_url( 'admin.php' ) );
300: 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>';
301: }
302:
303: 304: 305: 306: 307:
308: public function search_button( $text = '' ) {
309: return __( 'Search Courses', 'woothemes-sensei' );
310: }
311:
312: }
313:
314: 315: 316: 317: 318:
319: class WooThemes_Sensei_Analysis_User_Profile_List_Table extends WooThemes_Sensei_List_Table {}
320: