Skip to content

Use progress storage for course completion report values - #8341

Merged
donnapep merged 8 commits into
trunkfrom
hpps-reports-course-completions
Oct 19, 2026
Merged

donnapep merged 8 commits into
trunkfrom
hpps-reports-course-completions

Conversation

@donnapep

@donnapep donnapep commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

Proposed Changes

The Courses overview previously read per-course completions through the comments-era activity helper, so its Completions and Completion Rate values could disagree with HPPS progress. Read these values from the selected progress backend and resolve translated course IDs to their stored progress IDs. The overview header and CSV now agree with the rows. Remove Completions sorting because the previous sort used a different count and sorting by the new count required an expensive query across all courses.

Testing Instructions

  • With comments storage, create two published courses. Give one course a completed student and an in-progress student; leave the other without completions. In Sensei > Reports > Courses, verify the first course shows Completions 1 and Completion Rate 50%, and that the header agrees when viewing that course.
  • Confirm the Completions column has no sort control, Title sorting still works, and the Courses CSV contains the displayed counts and rates.
  • Repeat the course row, header, and CSV checks with HPPS enabled.
  • Create a guest user and a preview user, and give both completed progress in the first course (use WP-CLI if the student picker hides them). With each storage backend selected, verify that the course still shows 2 enrolled, 1 completion, and a 50% completion rate; the header totals do not increase, and the CSV matches the row.

Deprecated Code

  • sensei_analysis_course_completions is deprecated. Use Sensei_Reports_Overview_Service_Courses::get_completion_counts_by_course() for report-ready completion counts.

Related to SEN-83.

Copilot AI balanced review requested due to automatic review settings October 19, 2026 13:27
@donnapep donnapep self-assigned this Oct 19, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@donnapep donnapep added Deprecation This change introduces a deprecation. Hooks This change adds or modifies one or more hooks. labels Oct 19, 2026
@donnapep donnapep added this to the 4.27.0 milestone Oct 19, 2026
@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

WordPress Playground Preview

The changes in this pull request can previewed and tested using a WordPress Playground instance.

Open WordPress Playground Preview

@donnapep
donnapep force-pushed the hpps-reports-course-completions branch from 59f00c0 to 820e55c Compare October 19, 2026 13:42
@donnapep

Copy link
Copy Markdown
Member Author

@claude Please review.

@github-actions

Copy link
Copy Markdown
Contributor

Code Review

Reviewed the diff for PR #8341 (base trunk, head hpps-reports-course-completions). The refactor to source Completions/Completion Rate from the progress aggregation service is sound, and the translated-course / deprecated-filter test coverage is solid. Two findings worth considering before merge:

1. (Medium) New "sort by Completions" path adds extra full-catalog queries and a linearly-growing CASE expression

includes/reports/overview/data-provider/class-sensei-reports-overview-data-provider-courses.php:114-133 and :183-197

When orderby === 'count_of_completions', get_items() now:

  1. Runs an unpaginated get_posts() over all matching courses to get candidate IDs (lines 114-133),
  2. Runs get_completion_counts_by_course() over that full candidate set,
  3. Builds a CASE {$wpdb->posts}.ID WHEN %d THEN %d ... END expression in add_orderby_custom_field_to_query() with one WHEN clause per course that has at least one completion (lines 189-193), and
  4. Runs the real paginated query using that CASE expression for ORDER BY.

This replaces the previous single-query, SQL-side sort (a plain count_of_completions ASC/DESC on a column already selected via add_days_to_completion_to_courses_clauses()) with three queries plus a query string whose size scales with the number of courses that have completions. Because get_items() with orderby=count_of_completions can be invoked multiple times in one request (header totals via get_all_item_ids() in get_columns(), the paginated prepare_items() call, and the full unpaginated generate_report() CSV export), the extra cost is multiplied per page load. For stores with large course catalogs this could get slow or produce very large ORDER BY clauses. Worth considering pushing the count+sort entirely into SQL (e.g. ORDER BY a correlated subquery/derived column) instead of materializing all IDs and counts in PHP first.

As an aside, since count_of_completions is now always overwritten in PHP from completion_counts (line 166), the SQL-computed count_of_completions/days_to_completion columns added by add_days_to_completion_to_courses_query() (still active on every query) appear to be dead weight for this report now - the per-course LEFT JOIN + GROUP BY runs but its count_of_completions output is discarded immediately, and days_to_completion is not read anywhere else in this file (get_average_days_to_completion_by_course() is queried separately). Not introduced by this diff, but worth a follow-up cleanup since it compounds the cost above.

2. (Low) Deprecation notice can fire multiple times per request

includes/reports/overview/services/class-sensei-reports-overview-service-courses.php, get_completion_counts_by_course() (around lines 144-164, specifically the has_filter()/_deprecated_hook() block near line 149)

get_completion_counts_by_course() checks has_filter( 'sensei_analysis_course_completions' ) and calls _deprecated_hook() on every invocation. The method is called from several places per report render (row priming, the new completions-sort candidate counting, and full CSV export), so sites that still have a callback on this filter will get the same _deprecated_hook notice logged multiple times per request. Consider firing it once (e.g. with a static/instance guard) to avoid log noise for affected sites.

No other correctness issues found - the translated-course resolution, zero-completion handling, and post-status eligibility changes all look correct and are covered by the new tests.

@donnapep

Copy link
Copy Markdown
Member Author

Thanks for the review. The sorting concern is addressed in 2f3d032: the Completions column is no longer sortable, and the full-catalog lookup and growing CASE expression have been removed. In our local 1,600-course benchmark, the old PR sort took about 18 ms on warm runs versus 4–5 ms on trunk, so we chose to remove that sort instead of adding another SQL path. The existing Days to Completion clauses still supply fields on the provider's returned course objects; changing those separately would affect the provider's existing output and is outside this PR.

We left the deprecation notice as-is. With sorting removed, a normal Courses page or CSV export calls the count method twice, regardless of the number of rows. It emits a notice only when a callback is registered on the deprecated filter, and WordPress logs it only when deprecation logging is enabled. Two notices per request are minor log noise, so we did not add state solely to suppress the second one.

@donnapep
donnapep merged commit 98ba9ae into trunk Oct 19, 2026
23 checks passed
@donnapep
donnapep deleted the hpps-reports-course-completions branch October 19, 2026 18:33
@donnapep donnapep added Hooks This change adds or modifies one or more hooks. HPPS and removed Hooks This change adds or modifies one or more hooks. labels Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Deprecation This change introduces a deprecation. Hooks This change adds or modifies one or more hooks. HPPS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants