Skip to content

Commit

Permalink
Merge pull request #7642 from Automattic/fix/continue-to-next-lesson-…
Browse files Browse the repository at this point in the history
…not-react

Render Back to lesson when next_lesson_url has no value
  • Loading branch information
merkushin authored Jul 28, 2024
2 parents a4a6647 + a5a6da0 commit eb1630b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 14 deletions.
4 changes: 4 additions & 0 deletions changelog/fix-continue-to-next-lesson-not-react
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Show Back to lesson in a quiz when the lesson is the last in the course
17 changes: 11 additions & 6 deletions includes/course-theme/class-sensei-course-theme-quiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -140,7 +139,13 @@ 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 );
$lesson_url = empty( $lesson_url ) ? null : $lesson_url;
$actions[] = Sensei_Quiz::get_primary_button_html( __( 'Back to lesson', 'sensei-lms' ), $lesson_url );
}
}

// "Restart Quiz" button.
Expand Down
5 changes: 5 additions & 0 deletions tests/framework/factories/class-sensei-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand All @@ -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;
Expand Down

0 comments on commit eb1630b

Please sign in to comment.