From 7233bdf6c310782bb48c264bad6794c85a424d8a Mon Sep 17 00:00:00 2001 From: Miroslav Mitev Date: Tue, 12 Dec 2023 23:55:53 +0200 Subject: [PATCH] Fix quiz marked as passed when the lesson is completed --- ...omments-based-quiz-progress-repository.php | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/includes/internal/student-progress/quiz-progress/repositories/class-comments-based-quiz-progress-repository.php b/includes/internal/student-progress/quiz-progress/repositories/class-comments-based-quiz-progress-repository.php index 3f192ee85b..b4c2b3f46c 100644 --- a/includes/internal/student-progress/quiz-progress/repositories/class-comments-based-quiz-progress-repository.php +++ b/includes/internal/student-progress/quiz-progress/repositories/class-comments-based-quiz-progress-repository.php @@ -307,16 +307,31 @@ public function find( array $args ): array { * @return Comments_Based_Quiz_Progress The course progress. */ private function create_progress_from_comment( WP_Comment $comment, ?int $quiz_id = null ): Comments_Based_Quiz_Progress { - $comment_date = new DateTime( $comment->comment_date, wp_timezone() ); - $meta_start = get_comment_meta( (int) $comment->comment_ID, 'start', true ); - $started_at = ! empty( $meta_start ) ? new DateTime( $meta_start, wp_timezone() ) : current_datetime(); - - if ( in_array( $comment->comment_approved, [ 'complete', 'passed', 'graded' ], true ) ) { + $status = $comment->comment_approved; + $comment_date = new DateTime( $comment->comment_date, wp_timezone() ); + $meta_start = get_comment_meta( (int) $comment->comment_ID, 'start', true ); + $started_at = ! empty( $meta_start ) ? new DateTime( $meta_start, wp_timezone() ) : current_datetime(); + $grade = get_comment_meta( (int) $comment->comment_ID, 'grade', true ); + $is_graded = is_numeric( $grade ); + $questions_asked = get_comment_meta( (int) $comment->comment_ID, 'questions_asked', true ); + $is_submitted = ! empty( $questions_asked ); + + if ( in_array( $status, [ 'complete', 'passed', 'graded' ], true ) ) { $completed_at = $comment_date; } else { $completed_at = null; } + if ( in_array( $status, [ 'passed' ], true ) ) { + if ( ! $is_submitted ) { + // If the lesson is completed without submitting the quiz, set the quiz status to 'in-progress'. + $status = Quiz_Progress_Interface::STATUS_IN_PROGRESS; + } elseif ( ! $is_graded ) { + // If the lesson is completed and the quiz is not graded, set the quiz status to 'ungraded'. + $status = Quiz_Progress_Interface::STATUS_UNGRADED; + } + } + if ( is_null( $quiz_id ) ) { $quiz_id = Sensei()->lesson->lesson_quizzes( $comment->comment_post_ID ); } @@ -325,7 +340,7 @@ private function create_progress_from_comment( WP_Comment $comment, ?int $quiz_i (int) $comment->comment_ID, (int) $quiz_id, (int) $comment->user_id, - $comment->comment_approved, + $status, $started_at, $completed_at, $comment_date,