Making WordPress.org

Changeset 13160


Ignore:
Timestamp:
01/31/2024 11:32:59 AM (3 years ago)
Author:
paulkevan
Message:

Learn: add https://github.com/WordPress/Learn/pull/2178

Github should now be at 433848db54f76e1c5fd065167b1e8cd30ba53d34

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-learn/inc/sensei.php

    r11584 r13160  
    164164}
    165165add_action( 'sensei_course_learning_mode_load_theme', __NAMESPACE__ . '\wporg_fix_learning_mode_header_space' );
     166
     167/**
     168 * Format a date query var into a DateTime object.
     169 */
     170function wporg_learn_get_date( $query_var ) {
     171        $date = sanitize_text_field( $_GET[ $query_var ] ?? '' );
     172
     173        return \DateTime::createFromFormat( 'Y-m-d', $date ?? '', new \DateTimeZone( 'UTC' ) );
     174}
     175
     176/**
     177 * Get the number of unique learners between two dates.
     178 *
     179 * @param \DateTime $from_date
     180 * @param \DateTime $to_date
     181 *
     182 * @return int
     183 */
     184function wporg_learn_get_student_count( $from_date, $to_date ) {
     185
     186        if ( ! $from_date || ! $to_date ) {
     187                return 0;
     188        }
     189
     190        global $wpdb;
     191
     192        return $wpdb->get_var(
     193                $wpdb->prepare(
     194                        "SELECT COUNT(DISTINCT user_id) FROM $wpdb->comments
     195                        WHERE comment_type = 'sensei_course_status'
     196                        AND comment_date_gmt >= %s
     197                        AND comment_date_gmt <= %s",
     198                        array(
     199                                $from_date->format( 'Y-m-d H:i:s' ),
     200                                $to_date->format( 'Y-m-d H:i:s' ),
     201                        )
     202                )
     203        );
     204}
     205
     206/**
     207 * Add script to count unique learners
     208 */
     209function wporg_learn_add_student_count_to_reports( $type ) {
     210        if ( 'users' !== $type ) {
     211                return; // Only show the count on the students report screen.
     212        }
     213
     214        $from_date = wporg_learn_get_date( 'from_date' );
     215        $to_date   = wporg_learn_get_date( 'to_date' );
     216
     217        $student_count = wporg_learn_get_student_count( $from_date, $to_date );
     218
     219        ?>
     220        <div class="actions bulkactions">
     221                <label><?php esc_html_e( 'Total number of students', 'wporg-learn' ); ?></label>
     222                <input
     223                                class="sensei-date-picker"
     224                                name="from_date"
     225                                type="text"
     226                                autocomplete="off"
     227                                placeholder="<?php echo esc_attr( __( 'From Date', 'wporg-learn' ) ); ?>"
     228                                value="<?php echo esc_attr( $from_date ? $from_date->format( 'Y-m-d' ) : '' ); ?>"
     229                />
     230                <input
     231                                class="sensei-date-picker"
     232                                name="to_date"
     233                                type="text"
     234                                autocomplete="off"
     235                                placeholder="<?php echo esc_attr( __( 'To Date', 'wporg-learn' ) ); ?>"
     236                                value="<?php echo esc_attr( $to_date ? $to_date->format( 'Y-m-d' ) : '' ); ?>"
     237                />
     238                <label>: <?php echo (int) $student_count; ?></label>
     239        </div>
     240        <br>
     241        <?php
     242}
     243add_action( 'sensei_reports_overview_before_top_filters', __NAMESPACE__ . '\wporg_learn_add_student_count_to_reports' );
Note: See TracChangeset for help on using the changeset viewer.