Issues (896)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

class-sensei-analysis-overview-list-table.php (24 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 12 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
3
4
/**
5
 * Analysis Overview Data Table.
6
 *
7
 * @package Analytics
8
 * @author Automattic
9
 *
10
 * @since 1.2.0
11
 */
12
class Sensei_Analysis_Overview_List_Table extends WooThemes_Sensei_List_Table {
13
	public $type;
14
	public $page_slug = 'sensei_analysis';
15
16
	/**
17
	 * Constructor
18
	 * @since  1.2.0
19
	 * @return  void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
20
	 */
21
	public function __construct ( $type = 'users' ) {
22
		$this->type = in_array( $type, array( 'courses', 'lessons', 'users' ) ) ? $type : 'users';
23
24
		// Load Parent token into constructor
25
		parent::__construct( 'analysis_overview' );
26
27
		// Actions
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
	} // End __construct()
33
34
	/**
35
	 * Define the columns that are going to be used in the table
36
	 * @since  1.7.0
37
	 * @return array $columns, the array of columns to use with the table
38
	 */
39
	function get_columns() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
40
41
		switch( $this->type ) {
42 View Code Duplication
			case 'courses':
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
				$columns = array(
44
					'title' => __( 'Course', 'woothemes-sensei' ),
45
					'students' => __( 'Learners', 'woothemes-sensei' ),
46
					'lessons' => __( 'Lessons', 'woothemes-sensei' ),
47
					'completions' => __( 'Completed', 'woothemes-sensei' ),
48
					'average_percent' => __( 'Average Percentage', 'woothemes-sensei' ),
49
				);
50
				break;
51
52 View Code Duplication
			case 'lessons':
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
				$columns = array(
54
					'title' => __( 'Lesson', 'woothemes-sensei' ),
55
					'course' => __( 'Course', 'woothemes-sensei' ),
56
					'students' => __( 'Learners', 'woothemes-sensei' ),
57
					'completions' => __( 'Completed', 'woothemes-sensei' ),
58
					'average_grade' => __( 'Average Grade', 'woothemes-sensei' ),
59
				);
60
				break;
61
62
			case 'users':
63 View Code Duplication
			default:
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
				$columns = array(
65
					'title' => __( 'Learner', 'woothemes-sensei' ),
66
					'registered' => __( 'Date Registered', 'woothemes-sensei' ),
67
					'active_courses' => __( 'Active Courses', 'woothemes-sensei' ),
68
					'completed_courses' => __( 'Completed Courses', 'woothemes-sensei' ),
69
					'average_grade' => __( 'Average Grade', 'woothemes-sensei' ),
70
				);
71
				break;
72
		}
73
		// Backwards compatible filter name, moving forward should have single filter name
74
		$columns = apply_filters( 'sensei_analysis_overview_' . $this->type . '_columns', $columns, $this );
75
		$columns = apply_filters( 'sensei_analysis_overview_columns', $columns, $this );
76
		return $columns;
77
	}
78
79
	/**
80
	 * Define the columns that are going to be used in the table
81
	 * @since  1.7.0
82
	 * @return array $columns, the array of columns to use with the table
83
	 */
84
	function get_sortable_columns() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
85
86
		switch( $this->type ) {
87 View Code Duplication
			case 'courses':
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
				$columns = array(
89
					'title' => array( 'title', false ),
90
					'students' => array( 'students', false ),
91
					'lessons' => array( 'lessons', false ),
92
					'completions' => array( 'completions', false ),
93
					'average_percent' => array( 'average_percent', false ),
94
				);
95
				break;
96
97 View Code Duplication
			case 'lessons':
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
				$columns = array(
99
					'title' => array( 'title', false ),
100
					'course' => array( 'course', false ),
101
					'students' => array( 'students', false ),
102
					'completions' => array( 'completions', false ),
103
					'average_grade' => array( 'average_grade', false ),
104
				);
105
				break;
106
107
			case 'users':
108
			default:
109
				$columns = array(
110
					'title' => array( 'user_login', false ),
111
					'registered' => array( 'registered', false ),
112
					'active_courses' => array( 'active_courses', false ),
113
					'completed_courses' => array( 'completed_courses', false ),
114
					'average_grade' => array( 'average_grade', false )
115
				);
116
				break;
117
		}
118
		// Backwards compatible filter name, moving forward should have single filter name
119
		$columns = apply_filters( 'sensei_analysis_overview_' . $this->type . '_columns_sortable', $columns, $this );
120
		$columns = apply_filters( 'sensei_analysis_overview_columns_sortable', $columns, $this );
121
		return $columns;
122
	}
123
124
	/**
125
	 * Prepare the table with different parameters, pagination, columns and table elements
126
	 * @since  1.7.0
127
	 * @return void
128
	 */
129
	public function prepare_items() {
130
		global $per_page;
131
132
		// Handle orderby
133
		$orderby = '';
134
		if ( !empty( $_GET['orderby'] ) ) {
135
			if ( array_key_exists( esc_html( $_GET['orderby'] ), $this->get_sortable_columns() ) ) {
136
				$orderby = esc_html( $_GET['orderby'] );
137
			} // End If Statement
138
		}
139
140
		// Handle order
141
		$order = 'ASC';
142
		if ( !empty( $_GET['order'] ) ) {
143
			$order = ( 'ASC' == strtoupper($_GET['order']) ) ? 'ASC' : 'DESC';
144
		}
145
146
		$per_page = $this->get_items_per_page( 'sensei_comments_per_page' );
147
		$per_page = apply_filters( 'sensei_comments_per_page', $per_page, 'sensei_comments' );
148
149
		$paged = $this->get_pagenum();
150
		$offset = 0;
151
		if ( !empty($paged) ) {
152
			$offset = $per_page * ( $paged - 1 );
153
		} // End If Statement
154
155
		$args = array(
156
			'number' => $per_page,
157
			'offset' => $offset,
158
			'orderby' => $orderby,
159
			'order' => $order,
160
		);
161
162
        // Handle search
163 View Code Duplication
        if ( isset( $_GET['s'] ) && !empty( $_GET['s'] ) ) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
164
            $args['search'] = esc_html( $_GET['s'] );
165
        }
166
167 View Code Duplication
		switch ( $this->type ) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
168
			case 'courses':
169
				$this->items = $this->get_courses( $args );
170
				break;
171
172
			case 'lessons':
173
				$this->items = $this->get_lessons( $args );
174
				break;
175
176
			case 'users':
177
			default :
178
				$this->items = $this->get_learners( $args );
179
				break;
180
		}
181
182
		$total_items = $this->total_items;
183
		$total_pages = ceil( $total_items / $per_page );
184
		$this->set_pagination_args( array(
185
			'total_items' => $total_items,
186
			'total_pages' => $total_pages,
187
			'per_page' => $per_page
188
		) );
189
	}
190
191
	/**
192
	 * Generate a csv report with different parameters, pagination, columns and table elements
193
	 * @since  1.7.0
194
	 * @return data
195
	 */
196
	public function generate_report( $report ) {
197
198
		$data = array();
199
200
		$this->csv_output = true;
201
202
		// Handle orderby
203
		$orderby = '';
204
		if ( !empty( $_GET['orderby'] ) ) {
205
			if ( array_key_exists( esc_html( $_GET['orderby'] ), $this->get_sortable_columns() ) ) {
206
				$orderby = esc_html( $_GET['orderby'] );
207
			} // End If Statement
208
		}
209
210
		// Handle order
211
		$order = 'ASC';
212
		if ( !empty( $_GET['order'] ) ) {
213
			$order = ( 'ASC' == strtoupper($_GET['order']) ) ? 'ASC' : 'DESC';
214
		}
215
216
		$args = array(
217
			'orderby' => $orderby,
218
			'order' => $order,
219
		);
220
221
222
        // Handle search
223 View Code Duplication
        if ( isset( $_GET['s'] ) && !empty( $_GET['s'] ) ) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
224
            $args['search'] = esc_html( $_GET['s'] );
225
        }
226
227
228
		// Start the csv with the column headings
229
		$column_headers = array();
230
		$columns = $this->get_columns();
231
		foreach( $columns AS $key => $title ) {
232
			$column_headers[] = $title;
233
		}
234
		$data[] = $column_headers;
235
236 View Code Duplication
		switch ( $this->type ) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
237
			case 'courses':
238
				$this->items = $this->get_courses( $args );
239
				break;
240
241
			case 'lessons':
242
				$this->items = $this->get_lessons( $args );
243
				break;
244
245
			case 'users':
246
			default :
247
				$this->items = $this->get_learners( $args );
248
				break;
249
		}
250
251
		// Process each row
252
		foreach( $this->items AS $item) {
253
			$data[] = $this->get_row_data( $item );
254
		}
255
256
		return $data;
257
	}
258
259
	/**
260
	 * Generates the overall array for a single item in the display
261
	 * @since  1.7.0
262
	 * @param object $item The current item
263
     * @return array $column_data;
264
	 */
265
	protected function get_row_data( $item ) {
266
267
		switch( $this->type ) {
268
			case 'courses' :
269
				// Get Learners (i.e. those who have started)
270
				$course_args = array( 
271
						'post_id' => $item->ID,
272
						'type' => 'sensei_course_status',
273
						'status' => 'any',
274
					);
275
				$course_students = Sensei_Utils::sensei_check_for_activity( apply_filters( 'sensei_analysis_course_learners', $course_args, $item ) );
276
277
				// Get Course Completions
278
				$course_args = array( 
279
						'post_id' => $item->ID,
280
						'type' => 'sensei_course_status',
281
						'status' => 'complete',
282
					);
283
				$course_completions = Sensei_Utils::sensei_check_for_activity( apply_filters( 'sensei_analysis_course_completions', $course_args, $item ) );
284
285
				// Course Lessons
286
				$course_lessons = Sensei()->lesson->lesson_count( array('publish', 'private'), $item->ID );
0 ignored issues
show
array('publish', 'private') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
287
288
				// Get Percent Complete
289
				$grade_args = array( 
290
						'post_id' => $item->ID,
291
						'type' => 'sensei_course_status',
292
						'status' => 'any',
293
						'meta_key' => 'percent',
294
					);
295
296
				$percent_count = count( Sensei_Utils::sensei_check_for_activity( apply_filters( 'sensei_analysis_course_percentage', $grade_args, $item ), true ) );
297
				$percent_total = Sensei_Grading::get_course_users_grades_sum( $item->ID );
298
299
                $course_average_percent = 0;
300
                if( $percent_count > 0 && $percent_total > 0 ){
301
                    $course_average_percent = abs( round( doubleval( $percent_total / $percent_count ), 2 ) );
302
                }
303
304
305
				// Output course data
306 View Code Duplication
				if ( $this->csv_output ) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
307
					$course_title = apply_filters( 'the_title', $item->post_title, $item->ID );
308
				}
309
				else {
310
					$url = add_query_arg( array( 'page' => $this->page_slug, 'course_id' => $item->ID ), admin_url( 'admin.php' ) );
311
312
					$course_title = '<strong><a class="row-title" href="' . esc_url( $url ) . '">' . apply_filters( 'the_title', $item->post_title, $item->ID ) . '</a></strong>';
313
					$course_average_percent .= '%';
314
				} // End If Statement
315
				$column_data = apply_filters( 'sensei_analysis_overview_column_data', array( 'title' => $course_title,
316
												'students' => $course_students,
317
												'lessons' => $course_lessons,
318
												'completions' => $course_completions,
319
												'average_percent' => $course_average_percent,
320
											), $item, $this );
321
				break;
322
323
			case 'lessons' :
324
				// Get Learners (i.e. those who have started)
325
				$lesson_args = array( 
326
						'post_id' => $item->ID,
327
						'type' => 'sensei_lesson_status',
328
						'status' => 'any',
329
					);
330
				$lesson_students = Sensei_Utils::sensei_check_for_activity( apply_filters( 'sensei_analysis_lesson_learners', $lesson_args, $item ) );
331
332
				// Get Course Completions
333
				$lesson_args = array( 
334
						'post_id' => $item->ID,
335
						'type' => 'sensei_lesson_status',
336
						'status' => array( 'complete', 'graded', 'passed', 'failed' ),
337
						'count' => true,
338
					);
339
				$lesson_completions = Sensei_Utils::sensei_check_for_activity( apply_filters( 'sensei_analysis_lesson_completions', $lesson_args, $item ) );
340
341
				// Course 
342
				$course_id = get_post_meta( $item->ID, '_lesson_course', true );
343
				$course_title = $course_id ? get_the_title( $course_id ) : '';
344
345
				$lesson_average_grade = __('n/a', 'woothemes-sensei');
346
				if ( false != get_post_meta($item->ID, '_quiz_has_questions', true) ) {
347
					// Get Percent Complete
348
					$grade_args = array( 
349
							'post_id' => $item->ID,
350
							'type' => 'sensei_lesson_status',
351
							'status' => array( 'graded', 'passed', 'failed' ),
352
							'meta_key' => 'grade',
353
						);
354
355
					$grade_count = count( Sensei_Utils::sensei_check_for_activity( apply_filters( 'sensei_analysis_lesson_grades', $grade_args, $item ), true ));
356
					$grade_total = Sensei_Grading::get_lessons_users_grades_sum( $item->ID );
357
358
                    $lesson_average_grade = 0;
359 View Code Duplication
                    if( $grade_total > 0 && $grade_count > 0 ){
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
360
                        $lesson_average_grade = abs( round( doubleval( $grade_total / $grade_count ), 2 ) );
361
                    }
362
363
                }
364
				// Output lesson data
365
				if ( $this->csv_output ) {
366
					$lesson_title = apply_filters( 'the_title', $item->post_title, $item->ID );
367
				}
368
				else {
369
					$url = add_query_arg( array( 'page' => $this->page_slug, 'lesson_id' => $item->ID ), admin_url( 'admin.php' ) );
370
					$lesson_title = '<strong><a class="row-title" href="' . esc_url( $url ) . '">' . apply_filters( 'the_title', $item->post_title, $item->ID ) . '</a></strong>';
371
372
					if ( $course_id ) {
373
						$url = add_query_arg( array( 'page' => $this->page_slug, 'course_id' => $course_id ), admin_url( 'admin.php' ) );
374
						$course_title = '<a href="' . esc_url( $url ) . '">' . $course_title . '</a>';
375
					}
376
					else {
377
						$course_title = __('n/a', 'woothemes-sensei');
378
					}
379
					if ( is_numeric( $lesson_average_grade ) ) {
380
						$lesson_average_grade .= '%';
381
					}
382
				} // End If Statement
383
				$column_data = apply_filters( 'sensei_analysis_overview_column_data', array( 'title' => $lesson_title,
384
												'course' => $course_title,
385
												'students' => $lesson_students,
386
												'completions' => $lesson_completions,
387
												'average_grade' => $lesson_average_grade,
388
											), $item, $this );
389
				break;
390
391
			case 'users' :
392
			default:
393
				// Get Started Courses
394
				$course_args = array( 
395
						'user_id' => $item->ID,
396
						'type' => 'sensei_course_status',
397
						'status' => 'any',
398
					);
399
				$user_courses_started = Sensei_Utils::sensei_check_for_activity( apply_filters( 'sensei_analysis_user_courses_started', $course_args, $item ) );
400
401
				// Get Completed Courses
402
				$course_args = array( 
403
						'user_id' => $item->ID,
404
						'type' => 'sensei_course_status',
405
						'status' => 'complete',
406
					);
407
				$user_courses_ended = Sensei_Utils::sensei_check_for_activity( apply_filters( 'sensei_analysis_user_courses_ended', $course_args, $item ) );
408
409
				// Get Quiz Grades
410
				$grade_args = array( 
411
						'user_id' => $item->ID,
412
						'type' => 'sensei_lesson_status',
413
						'status' => 'any',
414
						'meta_key' => 'grade',
415
					);
416
417
				$grade_count = count( Sensei_Utils::sensei_check_for_activity( apply_filters( 'sensei_analysis_user_lesson_grades', $grade_args, $item ), true ));
418
				$grade_total = Sensei_Grading::get_user_graded_lessons_sum( $item->ID );
419
420
                $user_average_grade = 0;
421 View Code Duplication
                if( $grade_total > 0 && $grade_count > 0 ){
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
422
                    $user_average_grade = abs( round( doubleval( $grade_total / $grade_count ), 2 ) );
423
                }
424
425
				// Output the users data
426
				if ( $this->csv_output ) {
427
                    $user_name = Sensei_Learner::get_full_name( $item->ID );
428
                }
429
				else {
430
					$url = add_query_arg( array( 'page' => $this->page_slug, 'user_id' => $item->ID ), admin_url( 'admin.php' ) );
431
					$user_name = '<strong><a class="row-title" href="' . esc_url( $url ) . '">' . $item->display_name . '</a></strong>';
432
					$user_average_grade .= '%';
433
				} // End If Statement
434
				$column_data = apply_filters( 'sensei_analysis_overview_column_data', array( 'title' => $user_name,
435
												'registered' => $item->user_registered,
436
												'active_courses' => ( $user_courses_started - $user_courses_ended ),
437
												'completed_courses' => $user_courses_ended,
438
												'average_grade' => $user_average_grade,
439
											), $item, $this );
440
				break;
441
		} // end switch
442
		return $column_data;
443
	}
444
445
	/**
446
	 * Return array of course
447
	 * @since  1.7.0
448
	 * @return array courses
449
	 */
450 View Code Duplication
	private function get_courses( $args ) {
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
451
		$course_args = array(
452
			'post_type' => 'course',
453
			'post_status' => array('publish', 'private'),
454
			'posts_per_page' => $args['number'],
455
			'offset' => $args['offset'],
456
			'orderby' => $args['orderby'],
457
			'order' => $args['order'],
458
			'suppress_filters' => 0,
459
		);
460
461
		if ( $this->csv_output ) {
462
			$course_args['posts_per_page'] = '-1';
463
		}
464
465
		if( isset( $args['search'] ) ) {
466
			$course_args['s'] = $args['search'];
467
		}
468
469
		// Using WP_Query as get_posts() doesn't support 'found_posts'
470
		$courses_query = new WP_Query( apply_filters( 'sensei_analysis_overview_filter_courses', $course_args ) );
471
		$this->total_items = $courses_query->found_posts;
472
		return $courses_query->posts;
473
474
	} // End get_courses()
475
476
	/**
477
	 * Return array of lessons
478
	 * @since  1.7.0
479
	 * @return array lessons
480
	 */
481 View Code Duplication
	private function get_lessons( $args ) {
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
482
		$lessons_args = array(
483
			'post_type' => 'lesson',
484
			'post_status' => array('publish', 'private'),
485
			'posts_per_page' => $args['number'],
486
			'offset' => $args['offset'],
487
			'orderby' => $args['orderby'],
488
			'order' => $args['order'],
489
			'suppress_filters' => 0,
490
		);
491
492
		if ( $this->csv_output ) {
493
			$lessons_args['posts_per_page'] = '-1';
494
		}
495
496
		if( isset( $args['search'] ) ) {
497
			$lessons_args['s'] = $args['search'];
498
		}
499
500
		// Using WP_Query as get_posts() doesn't support 'found_posts'
501
		$lessons_query = new WP_Query( apply_filters( 'sensei_analysis_overview_filter_lessons', $lessons_args ) );
502
		$this->total_items = $lessons_query->found_posts;
503
		return $lessons_query->posts;
504
	} // End get_lessons()
505
506
	/**
507
	 * Return array of learners
508
	 * @since  1.7.0
509
	 * @return array learners
510
	 */
511
	private function get_learners( $args ) {
512
513
		if ( !empty($args['search']) ) {
514
			$args = array(
515
				'search' => '*' . trim( $args['search'], '*' ) . '*',
516
			);
517
		}
518
519
		// This stops the full meta data of each user being loaded
520
		$args['fields'] = array( 'ID', 'user_login', 'user_email', 'user_registered', 'display_name' );
521
522
        /**
523
         * Filter the WP_User_Query arguments
524
         * @since 1.6.0
525
         * @param $args
526
         */
527
        $args = apply_filters( 'sensei_analysis_overview_filter_users', $args );
528
		$wp_user_search = new WP_User_Query( $args );
529
        $learners = $wp_user_search->get_results();
530
		$this->total_items = $wp_user_search->get_total();
531
532
        return $learners;
533
534
	} // End get_learners()
535
536
	/**
537
	 * Sets the stats boxes to render
538
	 * @since  1.2.0
539
	 * @return array $stats_to_render of stats boxes and values
540
	 */
541
	public function stats_boxes () {
542
543
		// Get the data required
544
		$user_count = count_users();
545
		$user_count = apply_filters( 'sensei_analysis_total_users', $user_count['total_users'], $user_count );
546
		$total_courses = Sensei()->course->course_count( array('publish', 'private') );
0 ignored issues
show
array('publish', 'private') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
547
		$total_lessons = Sensei()->lesson->lesson_count( array('publish', 'private') );
0 ignored issues
show
array('publish', 'private') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
548
549
        /**
550
         * filter the analysis tot grades query args
551
         */
552
		$grade_args = apply_filters( 'sensei_analysis_total_quiz_grades', array(
0 ignored issues
show
$grade_args is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
553
				'type' => 'sensei_lesson_status',
554
				'status' => 'any',
555
556
                'meta_key' => 'grade'
557
        ));
558
559
		$total_grade_count = Sensei_Grading::get_graded_lessons_count();
560
		$total_grade_total = Sensei_Grading::get_graded_lessons_sum();
561
		$total_average_grade = 0;
562
		if( $total_grade_total > 0 &&  $total_grade_count >0   ){
563
			$total_average_grade = abs( round( doubleval( $total_grade_total / $total_grade_count ), 2 ) );
564
		}
565
566
567
		$course_args = array( 
568
				'type' => 'sensei_course_status',
569
				'status' => 'any',
570
			);
571
		$total_courses_started = Sensei_Utils::sensei_check_for_activity( apply_filters( 'sensei_analysis_total_courses_started', $course_args ) );
572
		$course_args = array( 
573
				'type' => 'sensei_course_status',
574
				'status' => 'complete',
575
			);
576
		$total_courses_ended = Sensei_Utils::sensei_check_for_activity( apply_filters( 'sensei_analysis_total_courses_ended', $course_args ) );
577
		$average_courses_per_learner = abs( round( doubleval( $total_courses_started / $user_count ), 2 ) );
578
579
		// Setup the boxes to render
580
		$stats_to_render = array( 
581
								__( 'Total Courses', 'woothemes-sensei' ) => $total_courses,
582
								__( 'Total Lessons', 'woothemes-sensei' ) => $total_lessons,
583
								__( 'Total Learners', 'woothemes-sensei' ) => $user_count,
584
								__( 'Average Courses per Learner', 'woothemes-sensei' ) => $average_courses_per_learner,
585
								__( 'Average Grade', 'woothemes-sensei' ) => $total_average_grade . '%',
586
								__( 'Total Completed Courses', 'woothemes-sensei' ) => $total_courses_ended,
587
							);
588
		return apply_filters( 'sensei_analysis_stats_boxes', $stats_to_render );
589
	} // End stats_boxes()
590
591
	/**
592
	 * Sets output when no items are found
593
	 * Overloads the parent method
594
	 * @since  1.2.0
595
	 * @return void
596
	 */
597
	public function no_items() {
598
		if( ! $this->view || 'users' == $this->view ) {
599
			$type = 'learners';
600
		} else {
601
			$type = $this->view;
602
		}
603
		echo  sprintf( __( '%1$sNo %2$s found%3$s', 'woothemes-sensei' ), '<em>', $type, '</em>' );
604
	} // End no_items()
605
606
	/**
607
	 * Output for table heading
608
	 * @since  1.2.0
609
	 * @return void
610
	 */
611
	public function data_table_header() {
612
		$menu = array();
613
614
		$query_args = array(
615
			'page' => $this->page_slug,
616
		);
617
		$learners_class = $courses_class = $lessons_class = '';
618
		switch( $this->type ) {
619
			case 'courses':
620
				$courses_class = 'current';
621
				break;
622
623
			case 'lessons':
624
				$lessons_class = 'current';
625
				break;
626
627
			default:
628
				$learners_class = 'current';
629
				break;
630
		}
631
		$learner_args = $lesson_args = $courses_args = $query_args;
632
		$learner_args['view'] = 'users';
633
		$lesson_args['view'] = 'lessons';
634
		$courses_args['view'] = 'courses';
635
636
		$menu['learners'] = '<a class="' . $learners_class . '" href="' . esc_url( add_query_arg( $learner_args, admin_url( 'admin.php' ) ) ). '">' . __( 'Learners', 'woothemes-sensei' ) . '</a>';
637
		$menu['courses'] = '<a class="' . $courses_class . '" href="' . esc_url ( add_query_arg( $courses_args, admin_url( 'admin.php' ) ) ) . '">' . __( 'Courses', 'woothemes-sensei' ) . '</a>';
638
		$menu['lessons'] = '<a class="' . $lessons_class . '" href="' . esc_url( add_query_arg( $lesson_args, admin_url( 'admin.php' ) ) ) . '">' . __( 'Lessons', 'woothemes-sensei' ) . '</a>';
639
640
		$menu = apply_filters( 'sensei_analysis_overview_sub_menu', $menu );
641 View Code Duplication
		if ( !empty($menu) ) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
642
			echo '<ul class="subsubsub">' . "\n";
643
			foreach ( $menu as $class => $item ) {
644
				$menu[ $class ] = "\t<li class='$class'>$item";
645
			}
646
			echo implode( " |</li>\n", $menu ) . "</li>\n";
647
			echo '</ul>' . "\n";
648
		}
649
	} // End data_table_header()
650
651
	/**
652
	 * Output for table footer
653
	 * @since  1.7.0
654
	 * @return void
655
	 */
656
	public function data_table_footer() {
657
		switch ( $this->type ) {
658
			case 'courses':
659
				$report = 'courses-overview';
660
				break;
661
662
			case 'lessons':
663
				$report = 'lessons-overview';
664
				break;
665
666
			case 'users':
667
			default :
668
				$report = 'user-overview';
669
			break;
670
		} // End Switch Statement
671
		$url = add_query_arg( array( 'page' => $this->page_slug, 'view' => $this->type, 'sensei_report_download' => $report ), admin_url( 'admin.php' ) );
672
		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>';
673
	} // End data_table_footer()
674
675
	/**
676
	 * The text for the search button
677
	 * @since  1.7.0
678
	 * @return string $text
679
	 */
680 View Code Duplication
	public function search_button( $text = '' ) {
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
681
		switch( $this->type ) {
682
			case 'courses':
683
				$text = __( 'Search Courses', 'woothemes-sensei' );
684
			break;
685
686
			case 'lessons':
687
				$text = __( 'Search Lessons', 'woothemes-sensei' );
688
			break;
689
690
			case 'users':
691
			default:
692
				$text = __( 'Search Learners', 'woothemes-sensei' );
693
			break;
694
		} // End Switch Statement
695
696
		return $text;
697
	}
698
699
} // End Class
700
701
/**
702
 * Class WooThemes_Sensei_Analysis_Overview_List_Table
703
 * @ignore only for backward compatibility
704
 * @since 1.9.0
705
 */
706
class WooThemes_Sensei_Analysis_Overview_List_Table extends Sensei_Analysis_Overview_List_Table {}
707