From 456da9935993b646f34a487c14059fda4147eb24 Mon Sep 17 00:00:00 2001 From: Evans Benjamin Date: Tue, 23 Jan 2024 16:31:11 +0900 Subject: [PATCH 1/3] Add script to count unique learnersin Sensei Adds the script provided by @m1r0 in #2111 to wporg-learn plugin --- wp-content/plugins/wporg-learn/inc/sensei.php | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/wp-content/plugins/wporg-learn/inc/sensei.php b/wp-content/plugins/wporg-learn/inc/sensei.php index b832e0e74..538a082ab 100644 --- a/wp-content/plugins/wporg-learn/inc/sensei.php +++ b/wp-content/plugins/wporg-learn/inc/sensei.php @@ -163,3 +163,60 @@ function wporg_fix_learning_mode_header_space() { wp_add_inline_style( 'learning-mode-header-fix', $custom_styles ); } add_action( 'sensei_course_learning_mode_load_theme', __NAMESPACE__ . '\wporg_fix_learning_mode_header_space' ); + +/** + * Add script to count unique learners + */ +function wporg_learn_add_student_count_to_reports( $type ) { + if ( 'users' !== $type ) { + return; // Only show the count on the students report screen. + } + + $from_date = \DateTime::createFromFormat( 'Y-m-d', $_GET['from_date'] ?? '', new \DateTimeZone( 'UTC' ) ); + $to_date = \DateTime::createFromFormat( 'Y-m-d', $_GET['to_date'] ?? '', new \DateTimeZone( 'UTC' ) ); + + global $wpdb; + $query = "SELECT COUNT(DISTINCT user_id) +FROM $wpdb->comments +WHERE comment_type = 'sensei_course_status'"; + + if ( $from_date ) { + $from_date->setTime( 0, 0, 0 ); + + $query .= " AND comment_date_gmt >= '" . $from_date->format( 'Y-m-d H:i:s' ) . "'"; + } + + if ( $to_date ) { + $to_date->setTime( 23, 59, 59 ); + + $query .= " AND comment_date_gmt <= '" . $to_date->format( 'Y-m-d H:i:s' ) . "'"; + } + + $student_count = $wpdb->get_var( $query ); //phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared + + ?> +
+ + + + +
+
+ Date: Tue, 23 Jan 2024 16:37:23 +0900 Subject: [PATCH 2/3] Fixing lint errors --- wp-content/plugins/wporg-learn/inc/sensei.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/wp-content/plugins/wporg-learn/inc/sensei.php b/wp-content/plugins/wporg-learn/inc/sensei.php index 538a082ab..24fefd803 100644 --- a/wp-content/plugins/wporg-learn/inc/sensei.php +++ b/wp-content/plugins/wporg-learn/inc/sensei.php @@ -182,13 +182,13 @@ function wporg_learn_add_student_count_to_reports( $type ) { if ( $from_date ) { $from_date->setTime( 0, 0, 0 ); - + $query .= " AND comment_date_gmt >= '" . $from_date->format( 'Y-m-d H:i:s' ) . "'"; } if ( $to_date ) { $to_date->setTime( 23, 59, 59 ); - + $query .= " AND comment_date_gmt <= '" . $to_date->format( 'Y-m-d H:i:s' ) . "'"; } @@ -202,7 +202,7 @@ class="sensei-date-picker" name="from_date" type="text" autocomplete="off" - placeholder="" + placeholder="" value="format( 'Y-m-d' ) : '' ); ?>" /> From ac6c83716b7937d7c1d5e49cd8eaaa20b15dc2d5 Mon Sep 17 00:00:00 2001 From: Ben Evans <49054298+bsanevans@users.noreply.github.com> Date: Fri, 26 Jan 2024 11:08:39 +0900 Subject: [PATCH 3/3] Apply suggestions from code review --- wp-content/plugins/wporg-learn/inc/sensei.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wp-content/plugins/wporg-learn/inc/sensei.php b/wp-content/plugins/wporg-learn/inc/sensei.php index 24fefd803..4c0fd801f 100644 --- a/wp-content/plugins/wporg-learn/inc/sensei.php +++ b/wp-content/plugins/wporg-learn/inc/sensei.php @@ -177,8 +177,8 @@ function wporg_learn_add_student_count_to_reports( $type ) { global $wpdb; $query = "SELECT COUNT(DISTINCT user_id) -FROM $wpdb->comments -WHERE comment_type = 'sensei_course_status'"; + FROM $wpdb->comments + WHERE comment_type = 'sensei_course_status'"; if ( $from_date ) { $from_date->setTime( 0, 0, 0 );