Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show total time spent by student on a Course #7676

Open
Imran92 opened this issue Sep 4, 2024 · 0 comments
Open

Show total time spent by student on a Course #7676

Imran92 opened this issue Sep 4, 2024 · 0 comments
Labels
[Pri] Low Cosmetic issue, low impact, easy workaround [Type] Enhancement

Comments

@Imran92
Copy link
Contributor

Imran92 commented Sep 4, 2024

Is your feature request related to a problem? Please describe

Display the total hours and minutes that students have spent on a course.

Describe the solution you'd like

Find a proper place to show the asked value (for example, a report, or Courses -> [Course] -> Manage.

In case it helps, here's a hook that shows the total time spent by a student on all complete courses in the Reports -> Students table

add_filter( 'sensei_analysis_overview_columns', function( $columns, $instance ) {
	if ( ! $instance instanceof Sensei_Reports_Overview_List_Table_Students ) {
		return $columns;
	}

	$columns['sum_of_days'] = __( 'Total Days to Complete Courses', 'sensei-lms' );
	return $columns;
}, 10, 2 );

add_filter( 'sensei_analysis_overview_column_data', function( $column_data, $item, $instance ) {
	if ( ! $instance instanceof Sensei_Reports_Overview_List_Table_Students ) {
		return $column_data;
	}

	$completed_course_ids = Sensei_Utils::sensei_activity_ids(
		[
			'user_id' => $item->ID,
			'type'    => 'sensei_course_status',
			'status'  => 'complete',
		]
	);
	$column_data['sum_of_days'] = ( is_array( $completed_course_ids ) && count( $completed_course_ids ) > 0 ) ? get_sum_days_to_completion( $completed_course_ids, $item->ID ) : 0;
	return $column_data;
}, 10, 3 );

function get_sum_days_to_completion( array $course_ids, int $user_id ) : float {
    if ( empty( $course_ids ) ) {
        return 0;
    }
    global $wpdb;

    $query = "
	SELECT SUM( aggregated.days_to_completion )
    FROM (
        SELECT GREATEST( CEIL( TIMESTAMPDIFF(SECOND, STR_TO_DATE( {$wpdb->commentmeta}.meta_value, '%Y-%m-%d %H:%i:%s' ), {$wpdb->comments}.comment_date ) / 86400 ), 1 ) AS days_to_completion
        FROM {$wpdb->comments}
        LEFT JOIN {$wpdb->commentmeta} ON {$wpdb->comments}.comment_ID = {$wpdb->commentmeta}.comment_id
            AND {$wpdb->commentmeta}.meta_key = 'start'
        WHERE {$wpdb->comments}.comment_type = 'sensei_course_status'
            AND {$wpdb->comments}.comment_approved = 'complete'
            AND {$wpdb->comments}.comment_post_ID IN ( " . implode( ',', array_map( 'intval', $course_ids ) ) . " )
            AND {$wpdb->comments}.user_id = {$user_id}
        GROUP BY {$wpdb->comments}.comment_post_ID
    ) AS aggregated";

    // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.NoCaching -- Performance improvement.
    return (float) $wpdb->get_var( $query );
}

Additional context

p1725367444476299-slack-C07418EJ0

@donnapep donnapep removed the [Status] Needs Triage Issue needs to be triaged label Sep 4, 2024
@donnapep donnapep added the [Pri] Low Cosmetic issue, low impact, easy workaround label Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Pri] Low Cosmetic issue, low impact, easy workaround [Type] Enhancement
Projects
None yet
Development

No branches or pull requests

2 participants