-
Notifications
You must be signed in to change notification settings - Fork 212
Migrate learner management start date to progress repositories #7933
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
Changes from all commits
5ad58bd
6f16c89
dbf1ade
fc51a1f
0d5fda1
ee4e923
c93921d
761bc7b
c1d0987
b467a7e
7a71c5d
9edf462
471eddf
90150b8
1938d21
55a937a
ed41b7b
79e5018
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: minor | ||
| Type: changed | ||
|
|
||
| Migrate student start date editing to use progress repositories instead of direct comment meta access. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -468,85 +468,120 @@ public function edit_date_started() { | |
| check_ajax_referer( 'edit_date_nonce', 'security' ); | ||
|
|
||
| if ( ! empty( $_POST['data']['post_id'] ) && is_numeric( $_POST['data']['post_id'] ) ) { | ||
| $post_id = (int) sanitize_key( $_POST['data']['post_id'] ); | ||
| $post_id = absint( wp_unslash( $_POST['data']['post_id'] ) ); | ||
| } else { | ||
| exit( '' ); | ||
| exit; | ||
|
donnapep marked this conversation as resolved.
|
||
| } | ||
|
|
||
| $post = get_post( $post_id ); | ||
|
|
||
| if ( empty( $post ) || ! is_a( $post, 'WP_Post' ) ) { | ||
| exit( '' ); | ||
| exit; | ||
| } | ||
|
|
||
| if ( ! empty( $_POST['data']['comment_id'] ) && is_numeric( $_POST['data']['comment_id'] ) ) { | ||
| $comment_id = (int) sanitize_key( $_POST['data']['comment_id'] ); | ||
| if ( ! empty( $_POST['data']['user_id'] ) && is_numeric( $_POST['data']['user_id'] ) ) { | ||
| $user_id = absint( wp_unslash( $_POST['data']['user_id'] ) ); | ||
| } else { | ||
| exit( '' ); | ||
| exit; | ||
| } | ||
|
|
||
| $comment = get_comment( $comment_id ); | ||
| $post_type = get_post_type( $post_id ); | ||
|
|
||
| if ( empty( $comment ) ) { | ||
| exit( '' ); | ||
| if ( ! in_array( $post_type, array( 'course', 'lesson' ), true ) ) { | ||
| exit; | ||
| } | ||
|
|
||
| $post_type = get_post_type( $post_id ); | ||
|
|
||
| if ( 'lesson' === $post_type ) { | ||
| $can_edit_date = $this->can_user_manage_students( (int) Sensei()->lesson->get_course_id( $post_id ), intval( $post->post_author ) ); | ||
| } else { | ||
| $can_edit_date = $this->can_user_manage_students( $post_id, intval( $post->post_author ) ); | ||
| } | ||
|
|
||
| if ( ! $can_edit_date ) { | ||
| exit( '' ); | ||
| exit; | ||
| } | ||
|
|
||
| if ( ! empty( $_POST['data']['new_dates']['start-date'] ) ) { | ||
| $date_string = sanitize_text_field( wp_unslash( $_POST['data']['new_dates']['start-date'] ) ); | ||
| } else { | ||
| exit( '' ); | ||
| exit; | ||
| } | ||
|
|
||
| if ( empty( $date_string ) ) { | ||
| exit( '' ); | ||
| exit; | ||
| } | ||
|
|
||
| $date_started = get_comment_meta( $comment_id, 'start', true ); | ||
|
|
||
| $expected_date_format = 'Y-m-d H:i:s'; | ||
| if ( false === strpos( $date_string, ' ' ) ) { | ||
| $expected_date_format = 'Y-m-d'; | ||
| } | ||
|
|
||
| $date = DateTimeImmutable::createFromFormat( $expected_date_format, $date_string ); | ||
| $date = DateTimeImmutable::createFromFormat( $expected_date_format, $date_string, wp_timezone() ); | ||
| if ( false === $date ) { | ||
| exit( '' ); | ||
| exit; | ||
| } | ||
|
|
||
| $utc_date = $date->setTimezone( new \DateTimeZone( 'UTC' ) ); | ||
|
|
||
| $repository = 'course' === $post_type | ||
| ? Sensei()->course_progress_repository_factory->create() | ||
| : Sensei()->lesson_progress_repository_factory->create(); | ||
|
Comment on lines
+526
to
+528
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A tiny possible improvement here could be to hide this logic behind a factory method like:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. I think I'm going to leave this for now for those 2 instances and re-assess if it grows. |
||
|
|
||
| $progress = $repository->get( $post_id, $user_id ); | ||
| if ( ! $progress ) { | ||
| exit; | ||
| } | ||
|
|
||
| $old_started_at = $progress->get_started_at(); | ||
| $started_at_changed = ( null === $old_started_at || $old_started_at->getTimestamp() !== $utc_date->getTimestamp() ); | ||
|
|
||
| if ( $started_at_changed ) { | ||
| $progress->set_started_at( $utc_date ); | ||
|
|
||
| /** | ||
| * The repository and progress are created from the same factory, so their types | ||
| * always match, but Psalm cannot correlate the two union types. | ||
| * | ||
| * @psalm-suppress PossiblyInvalidArgument | ||
| */ | ||
| $repository->save( $progress ); | ||
| } | ||
|
|
||
| $formatted_date = gmdate( 'Y-m-d H:i:s', $date->getTimestamp() ); | ||
| $updated = (bool) update_comment_meta( $comment_id, 'start', $formatted_date, $date_started ); | ||
| /** | ||
| * Filters whether a student row was updated on the Students screen. | ||
| * | ||
| * Lets extensions persist additional date changes (e.g. course access dates) when a | ||
| * student's dates are edited and report whether a change occurred so the row refreshes. | ||
| * | ||
| * @since $$next-version$$ | ||
| * | ||
| * @hook sensei_students_row_updated | ||
| * | ||
| * @param {bool} $started_at_changed Whether the start date was changed. | ||
| * @param {int} $post_id Course or lesson ID. | ||
| * @param {int} $user_id Student user ID. | ||
| * @param {string} $post_type Post type ('course' or 'lesson'). | ||
| * @return {bool} Whether the student row was updated. | ||
| */ | ||
| $updated = (bool) apply_filters( 'sensei_students_row_updated', $started_at_changed, $post_id, $user_id, $post_type ); | ||
|
|
||
| /** | ||
| * Filter sensei_learners_learner_updated | ||
| * | ||
| * This filter should return false if there was no update in the learner row. | ||
| * @deprecated $$next-version$$ Use sensei_students_row_updated instead. | ||
| * | ||
| * @hook sensei_learners_learner_updated | ||
| * | ||
| * @param {bool} $updated A flag indicating if there was an update in the learner row. | ||
| * @param {bool} $updated A flag indicating if there was an update in the student row. | ||
| * @param {int} $post_id Lesson or course id. | ||
| * @param {int} $comment_id The comment id which tracks the progress of the learner. | ||
| * @param {int} $comment_id The comment id which tracks the progress of the student. | ||
| * @return {bool} False if there were no updates. | ||
| */ | ||
| $updated = apply_filters( 'sensei_learners_learner_updated', $updated, $post_id, $comment_id ); | ||
| $updated = (bool) apply_filters_deprecated( 'sensei_learners_learner_updated', array( $updated, $post_id, $progress->get_id() ), '$$next-version$$', 'sensei_students_row_updated' ); | ||
|
|
||
| if ( false === $updated ) { | ||
| exit( '' ); | ||
| if ( ! $updated ) { | ||
| wp_die(); | ||
| } | ||
|
|
||
| exit( esc_html( $formatted_date ) ); | ||
| wp_die( esc_html( $date->format( 'Y-m-d H:i:s' ) ) ); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -536,8 +536,16 @@ protected function get_row_data( $item ) { | |
| $actions[] = $edit_start_date_form; | ||
| } | ||
|
|
||
| $date_started = get_comment_meta( $user_activity->comment_ID, 'start', true ); | ||
| $date_input = '<input class="edit-date-date-picker" data-name="start-date" type="text" value="' . esc_attr( $date_started ) . '">'; | ||
| $student_progress = null; | ||
| if ( $post_id ) { | ||
| $progress_repository = 'course' === $post_type | ||
| ? Sensei()->course_progress_repository_factory->create() | ||
| : Sensei()->lesson_progress_repository_factory->create(); | ||
|
Comment on lines
+541
to
+543
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No surprise, that factory method could be used here as well.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #7933 (comment). |
||
| $student_progress = $progress_repository->get( (int) $post_id, $user_activity->user_id ); | ||
| } | ||
| $student_started_at = $student_progress ? $student_progress->get_started_at() : null; | ||
| $date_started = $student_started_at ? (string) wp_date( 'Y-m-d H:i:s', $student_started_at->getTimestamp() ) : ''; | ||
| $date_input = '<input class="edit-date-date-picker" data-name="start-date" type="text" value="' . esc_attr( $date_started ) . '">'; | ||
|
|
||
| /** | ||
| * Filter sensei_learners_main_column_data | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.