From a0d3d83485949b38a8e2525ceebebf1bb18176e2 Mon Sep 17 00:00:00 2001 From: ren <18050944+renintw@users.noreply.github.com> Date: Thu, 25 Jul 2024 07:29:25 +0900 Subject: [PATCH 1/4] Render Back to lesson when next_lesson_url has no value --- includes/course-theme/class-sensei-course-theme-quiz.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/includes/course-theme/class-sensei-course-theme-quiz.php b/includes/course-theme/class-sensei-course-theme-quiz.php index 787c3887c9..554b924b64 100644 --- a/includes/course-theme/class-sensei-course-theme-quiz.php +++ b/includes/course-theme/class-sensei-course-theme-quiz.php @@ -140,7 +140,12 @@ private function maybe_add_quiz_results_notice() { if ( in_array( $quiz_progress->get_status(), array( 'graded', 'passed' ), true ) ) { $prev_next_urls = sensei_get_prev_next_lessons( $lesson_id ); $next_lesson_url = $prev_next_urls['next']['url'] ?? null; - $actions[] = Sensei_Quiz::get_primary_button_html( __( 'Continue to next lesson', 'sensei-lms' ), $next_lesson_url ); + if ( $next_lesson_url ) { + $actions[] = Sensei_Quiz::get_primary_button_html( __( 'Continue to next lesson', 'sensei-lms' ), $next_lesson_url ); + } else { + $lesson_url = get_permalink( $lesson_id ); + $actions[] = Sensei_Quiz::get_primary_button_html( __( 'Back to lesson', 'sensei-lms' ), $lesson_url ); + } } // "Restart Quiz" button. From 0b69c375ac0cc975cb05040f51df4bfe904dd5ce Mon Sep 17 00:00:00 2001 From: Dmitry Merkushin Date: Sun, 28 Jul 2024 12:56:46 -0600 Subject: [PATCH 2/4] Update unit tests --- .../factories/class-sensei-factory.php | 5 ++ .../test-class-sensei-course-theme-quiz.php | 47 +++++++++++++++---- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/tests/framework/factories/class-sensei-factory.php b/tests/framework/factories/class-sensei-factory.php index 7cc081a39f..80d1d6c94f 100644 --- a/tests/framework/factories/class-sensei-factory.php +++ b/tests/framework/factories/class-sensei-factory.php @@ -263,6 +263,11 @@ public function get_course_with_lessons( $args = array() ) { $this->attach_lessons_multiple_questions( $multiple_question_count, $lesson_id, $args['multiple_question_args'], $args['quiz_args'] ); } + if ( $course_id && count( $lesson_ids ) ) { + $admin = new Sensei_Admin(); + $admin->save_lesson_order( implode( ',', $lesson_ids ), $course_id ); + } + return array( 'course_id' => $course_id, 'lesson_ids' => $lesson_ids, diff --git a/tests/unit-tests/course-theme/test-class-sensei-course-theme-quiz.php b/tests/unit-tests/course-theme/test-class-sensei-course-theme-quiz.php index 3e843c28b8..11a0aab012 100644 --- a/tests/unit-tests/course-theme/test-class-sensei-course-theme-quiz.php +++ b/tests/unit-tests/course-theme/test-class-sensei-course-theme-quiz.php @@ -18,20 +18,22 @@ public function setUp(): void { $this->factory = new Sensei_Factory(); } - public function testInit_QuizPassed_AddsNotice(): void { + public function testInit_QuizPassedAndTheLessonWasntTheLast_AddsNoticeWithExpectedMessage(): void { /* Arrange. */ global $post; - $notices = \Sensei_Context_Notices::instance( 'course_theme_quiz_grade' ); - $lesson_id = $this->factory->lesson->create(); - $quiz = $this->factory->quiz->create_and_get( array( 'post_parent' => $lesson_id ) ); - $user = $this->factory->user->create_and_get(); - $post = $quiz; + $course_data = $this->factory->get_course_with_lessons( array( 'lesson_count' => 2 ) ); + $lesson_ids = $course_data['lesson_ids']; + $quiz_ids = $course_data['quiz_ids']; + $user = $this->factory->user->create_and_get(); + $post = get_post( $quiz_ids[0] ); wp_set_current_user( $user->ID ); + + $notices = \Sensei_Context_Notices::instance( 'course_theme_quiz_grade' ); $notices->remove_notice( 'course-theme-quiz-grade' ); - Sensei()->lesson_progress_repository->create( $lesson_id, $user->ID ); - $quiz_progress = Sensei()->quiz_progress_repository->create( $quiz->ID, $user->ID ); + Sensei()->lesson_progress_repository->create( $lesson_ids[0], $user->ID ); + $quiz_progress = Sensei()->quiz_progress_repository->create( $quiz_ids[0], $user->ID ); $quiz_progress->pass(); Sensei()->quiz_progress_repository->save( $quiz_progress ); @@ -45,6 +47,35 @@ public function testInit_QuizPassed_AddsNotice(): void { $this->assertStringContainsString( 'Continue to next lesson', $notices_html ); } + public function testInit_QuizPassedAndTheLessonWasTheLast_AddsNoticeWithExpectedMessage(): void { + /* Arrange. */ + global $post; + + $course_data = $this->factory->get_course_with_lessons( array( 'lesson_count' => 2 ) ); + $lesson_ids = $course_data['lesson_ids']; + $quiz_ids = $course_data['quiz_ids']; + $user = $this->factory->user->create_and_get(); + $post = get_post( $quiz_ids[1] ); + wp_set_current_user( $user->ID ); + + $notices = \Sensei_Context_Notices::instance( 'course_theme_quiz_grade' ); + $notices->remove_notice( 'course-theme-quiz-grade' ); + + Sensei()->lesson_progress_repository->create( $lesson_ids[1], $user->ID ); + $quiz_progress = Sensei()->quiz_progress_repository->create( $quiz_ids[1], $user->ID ); + $quiz_progress->pass(); + Sensei()->quiz_progress_repository->save( $quiz_progress ); + + $course_theme_quiz = \Sensei_Course_Theme_Quiz::instance(); + + /* Act. */ + $course_theme_quiz->init(); + + /* Assert. */ + $notices_html = $notices->get_notices_html( 'course-theme/lesson-quiz-notice.php' ); + $this->assertStringContainsString( 'Back to lesson', $notices_html ); + } + public function testInit_QuizInProgress_DoesntAddNotice(): void { /* Arrange. */ global $post; From ac54abcdf70ef9ee8ebd910bc4722f35e7c3e02f Mon Sep 17 00:00:00 2001 From: Dmitry Merkushin Date: Sun, 28 Jul 2024 13:06:54 -0600 Subject: [PATCH 3/4] Fix Psalm issue: string or null accpepted --- .../course-theme/class-sensei-course-theme-quiz.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/includes/course-theme/class-sensei-course-theme-quiz.php b/includes/course-theme/class-sensei-course-theme-quiz.php index 554b924b64..9d9c330696 100644 --- a/includes/course-theme/class-sensei-course-theme-quiz.php +++ b/includes/course-theme/class-sensei-course-theme-quiz.php @@ -108,11 +108,10 @@ private function maybe_add_quiz_results_notice() { if ( $quiz_graded_email && 'publish' === $quiz_graded_email->post_status ) { $email_enabled = true; } - } else { // Old quiz graded email. - if ( isset( Sensei()->settings->settings['email_learners'] ) && - in_array( 'learner-graded-quiz', (array) Sensei()->settings->settings['email_learners'], true ) ) { - $email_enabled = true; - } + } elseif ( isset( Sensei()->settings->settings['email_learners'] ) && + in_array( 'learner-graded-quiz', (array) Sensei()->settings->settings['email_learners'], true ) ) { + // Old quiz graded email. + $email_enabled = true; } if ( $email_enabled ) { @@ -144,6 +143,7 @@ private function maybe_add_quiz_results_notice() { $actions[] = Sensei_Quiz::get_primary_button_html( __( 'Continue to next lesson', 'sensei-lms' ), $next_lesson_url ); } else { $lesson_url = get_permalink( $lesson_id ); + $lesson_url = empty( $lesson_url ) ? null : $lesson_url; $actions[] = Sensei_Quiz::get_primary_button_html( __( 'Back to lesson', 'sensei-lms' ), $lesson_url ); } } From a5a6da02de62a309a1a6ecfa2b56ff2bf5afbbc0 Mon Sep 17 00:00:00 2001 From: Dmitry Merkushin Date: Sun, 28 Jul 2024 13:08:09 -0600 Subject: [PATCH 4/4] Add changelog entry --- changelog/fix-continue-to-next-lesson-not-react | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 changelog/fix-continue-to-next-lesson-not-react diff --git a/changelog/fix-continue-to-next-lesson-not-react b/changelog/fix-continue-to-next-lesson-not-react new file mode 100644 index 0000000000..1cc148e7e3 --- /dev/null +++ b/changelog/fix-continue-to-next-lesson-not-react @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +Show Back to lesson in a quiz when the lesson is the last in the course