From 84ab9caad22204c6c1f949d53b8ca9e337ebea35 Mon Sep 17 00:00:00 2001 From: "Md.Harun-Ur-Rashid" Date: Wed, 2 Dec 2026 16:19:45 +0600 Subject: [PATCH 1/3] fix:Quiz Passing Can Be Manipulated --- classes/Quiz.php | 58 +++++++++++++++++++++++++++----------------- models/QuizModel.php | 48 ++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 22 deletions(-) diff --git a/classes/Quiz.php b/classes/Quiz.php index a66973e45b..4e2a584aa0 100644 --- a/classes/Quiz.php +++ b/classes/Quiz.php @@ -19,6 +19,7 @@ use Tutor\Components\Modal; use Tutor\Components\SvgIcon; use Tutor\Components\Table; +use Tutor\Core\DB; use Tutor\Core\Input; use Tutor\Helpers\HttpHelper; use Tutor\Helpers\QueryHelper; @@ -129,6 +130,7 @@ public function __construct( $register_hooks = true ) { add_action( 'template_redirect', array( $this, 'start_the_quiz' ) ); add_action( 'template_redirect', array( $this, 'answering_quiz' ) ); add_action( 'template_redirect', array( $this, 'finishing_quiz_attempt' ) ); + add_action( 'tutor_quiz/start/after', array( $this, 'store_served_questions' ), 10, 3 ); /** * Instructor quiz review and feedback Ajax API. @@ -812,6 +814,30 @@ public static function quiz_attempt( int $course_id, int $quiz_id, int $user_id, } } + /** + * Store served questions + * + * @since 4.3.0 + * + * @param int $quiz_id quiz id. + * @param int $user_id user id. + * @param int $attempt_id attempt id. + * + * @return void + */ + public function store_served_questions( int $quiz_id, int $user_id, int $attempt_id ) { + $served_questions = tutor_utils()->get_random_questions_by_quiz( $quiz_id ); + if ( ! empty( $served_questions ) ) { + $attempt = tutor_utils()->get_attempt( $attempt_id ); + $attempt_info = maybe_unserialize( $attempt->attempt_info ); + + $attempt_info['served_questions'] = $served_questions; + $data = array( 'attempt_info' => maybe_serialize( $attempt_info ) ); + + DB::table( 'tutor_quiz_attempts' )->where( 'attempt_id', $attempt_id )->update( $data ); + } + } + /** * Answering quiz * @@ -951,37 +977,25 @@ public static function manage_attempt_answers( $attempt_answers, $attempt, $atte } // Get total marks of all question comes. - $question_ids = tutor_utils()->avalue_dot( 'quiz_question_ids', $attempt_answer ); - $question_ids = array_filter( $question_ids, fn ( $id ) => is_numeric( $id ) && intval( $id ) > 0 ); + $question_ids = QuizModel::get_served_question_ids( $attempt ); $quiz_answers = tutor_utils()->avalue_dot( 'quiz_question', $attempt_answer ); // Calculate and set the total marks in attempt table for this question. if ( tutor_utils()->count( $question_ids ) ) { - $question_ids_string = QueryHelper::prepare_in_clause( $question_ids ); - // Get total marks of the questions from question table. - //phpcs:disable - $query = $wpdb->prepare( - "SELECT SUM(question_mark) - FROM {$wpdb->prefix}tutor_quiz_questions - WHERE 1 = %d - AND quiz_id = %d - AND question_id IN({$question_ids_string}); - ", - 1, - $attempt->quiz_id - ); - $total_question_marks = $wpdb->get_var( $query ); - //phpcs:enable + $total_question_marks = DB::table( 'tutor_quiz_questions' ) + ->select_raw( 'SUM(question_mark) as total_marks' ) + ->where( 'quiz_id', $attempt->quiz_id ) + ->where_in( 'question_id', $question_ids ) + ->first(); + $total_question_marks = intval( $total_question_marks ? $total_question_marks->total_marks : 0 ); $total_question_marks = apply_filters( 'tutor_filter_update_before_question_mark', $total_question_marks, $question_ids, $user_id, $attempt_id ); // Set the the total mark in the attempt table for the question. - $wpdb->update( - $wpdb->prefix . 'tutor_quiz_attempts', - array( 'total_marks' => $total_question_marks ), - array( 'attempt_id' => $attempt_id ) - ); + DB::table( 'tutor_quiz_attempts' ) + ->where( 'attempt_id', $attempt_id ) + ->update( array( 'total_marks' => $total_question_marks ) ); } $total_marks = 0; diff --git a/models/QuizModel.php b/models/QuizModel.php index fd88311e02..0ae4055178 100644 --- a/models/QuizModel.php +++ b/models/QuizModel.php @@ -2047,4 +2047,52 @@ public static function get_manual_overrides_map( $attempt_info ): array { return is_array( $overrides_map ) ? $overrides_map : array(); } + + /** + * Get the served questions from attempt info. + * + * Consistent extraction for the served_questions map stored inside + * attempt_info, keyed by question ID. + * + * @since 4.1.0 + * + * @param int|object $attempt attempt id or object. + * + * @return array Served questions. + */ + public static function get_served_questions_by_attempt( $attempt ) { + if ( is_numeric( $attempt ) ) { + $attempt = tutor_utils()->get_attempt( $attempt ); + } + + if ( ! is_object( $attempt ) ) { + return array(); + } + + $attempt_info = maybe_unserialize( $attempt->attempt_info ?? '' ); + $served_questions = is_array( $attempt_info ) ? ( $attempt_info['served_questions'] ?? array() ) : array(); + + return is_array( $served_questions ) ? $served_questions : array(); + } + + /** + * Get the served question ids from attempt. + * + * @since 4.3.0 + * + * @param int|object $attempt attempt id or object. + * + * @return array Served question ids. + */ + public static function get_served_question_ids( $attempt ) : array { + $served_questions = self::get_served_questions_by_attempt( $attempt ); + $question_ids = array(); + foreach ( $served_questions as $question ) { + if ( isset( $question->question_id ) && is_numeric( $question->question_id ) ) { + $question_ids[] = (int) $question->question_id; + } + } + + return $question_ids; + } } From 59e7d5e53f16e45ff7b75d0293ee3d99f53fb348 Mon Sep 17 00:00:00 2001 From: "Md.Harun-Ur-Rashid" Date: Thu, 3 Dec 2026 12:28:52 +0600 Subject: [PATCH 2/3] refactor: remove stored quiz served questions and calculate dynamically during evaluation --- classes/Quiz.php | 30 +++------------------------ models/QuizModel.php | 48 -------------------------------------------- 2 files changed, 3 insertions(+), 75 deletions(-) diff --git a/classes/Quiz.php b/classes/Quiz.php index 4e2a584aa0..3112ef0d83 100644 --- a/classes/Quiz.php +++ b/classes/Quiz.php @@ -130,7 +130,6 @@ public function __construct( $register_hooks = true ) { add_action( 'template_redirect', array( $this, 'start_the_quiz' ) ); add_action( 'template_redirect', array( $this, 'answering_quiz' ) ); add_action( 'template_redirect', array( $this, 'finishing_quiz_attempt' ) ); - add_action( 'tutor_quiz/start/after', array( $this, 'store_served_questions' ), 10, 3 ); /** * Instructor quiz review and feedback Ajax API. @@ -814,30 +813,6 @@ public static function quiz_attempt( int $course_id, int $quiz_id, int $user_id, } } - /** - * Store served questions - * - * @since 4.3.0 - * - * @param int $quiz_id quiz id. - * @param int $user_id user id. - * @param int $attempt_id attempt id. - * - * @return void - */ - public function store_served_questions( int $quiz_id, int $user_id, int $attempt_id ) { - $served_questions = tutor_utils()->get_random_questions_by_quiz( $quiz_id ); - if ( ! empty( $served_questions ) ) { - $attempt = tutor_utils()->get_attempt( $attempt_id ); - $attempt_info = maybe_unserialize( $attempt->attempt_info ); - - $attempt_info['served_questions'] = $served_questions; - $data = array( 'attempt_info' => maybe_serialize( $attempt_info ) ); - - DB::table( 'tutor_quiz_attempts' )->where( 'attempt_id', $attempt_id )->update( $data ); - } - } - /** * Answering quiz * @@ -977,8 +952,9 @@ public static function manage_attempt_answers( $attempt_answers, $attempt, $atte } // Get total marks of all question comes. - $question_ids = QuizModel::get_served_question_ids( $attempt ); - $quiz_answers = tutor_utils()->avalue_dot( 'quiz_question', $attempt_answer ); + $served_questions = tutor_utils()->get_random_questions_by_quiz( $attempt->quiz_id ); + $question_ids = wp_list_pluck( (array) $served_questions, 'question_id' ); + $quiz_answers = tutor_utils()->avalue_dot( 'quiz_question', $attempt_answer ); // Calculate and set the total marks in attempt table for this question. if ( tutor_utils()->count( $question_ids ) ) { diff --git a/models/QuizModel.php b/models/QuizModel.php index 0ae4055178..fd88311e02 100644 --- a/models/QuizModel.php +++ b/models/QuizModel.php @@ -2047,52 +2047,4 @@ public static function get_manual_overrides_map( $attempt_info ): array { return is_array( $overrides_map ) ? $overrides_map : array(); } - - /** - * Get the served questions from attempt info. - * - * Consistent extraction for the served_questions map stored inside - * attempt_info, keyed by question ID. - * - * @since 4.1.0 - * - * @param int|object $attempt attempt id or object. - * - * @return array Served questions. - */ - public static function get_served_questions_by_attempt( $attempt ) { - if ( is_numeric( $attempt ) ) { - $attempt = tutor_utils()->get_attempt( $attempt ); - } - - if ( ! is_object( $attempt ) ) { - return array(); - } - - $attempt_info = maybe_unserialize( $attempt->attempt_info ?? '' ); - $served_questions = is_array( $attempt_info ) ? ( $attempt_info['served_questions'] ?? array() ) : array(); - - return is_array( $served_questions ) ? $served_questions : array(); - } - - /** - * Get the served question ids from attempt. - * - * @since 4.3.0 - * - * @param int|object $attempt attempt id or object. - * - * @return array Served question ids. - */ - public static function get_served_question_ids( $attempt ) : array { - $served_questions = self::get_served_questions_by_attempt( $attempt ); - $question_ids = array(); - foreach ( $served_questions as $question ) { - if ( isset( $question->question_id ) && is_numeric( $question->question_id ) ) { - $question_ids[] = (int) $question->question_id; - } - } - - return $question_ids; - } } From 0dc3adee2c6cc41381cd54144ce12bc38d701646 Mon Sep 17 00:00:00 2001 From: "Md.Harun-Ur-Rashid" Date: Thu, 3 Dec 2026 13:22:51 +0600 Subject: [PATCH 3/3] fix: use wp_safe_redirect and exit after quiz submission --- classes/Quiz.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/classes/Quiz.php b/classes/Quiz.php index 3112ef0d83..0c600dea53 100644 --- a/classes/Quiz.php +++ b/classes/Quiz.php @@ -1255,7 +1255,8 @@ public function finishing_quiz_attempt() { $wpdb->update( $wpdb->prefix . 'tutor_quiz_attempts', $attempt_info, array( 'attempt_id' => $attempt_id ) ); do_action( 'tutor_quiz_finished', $attempt_id, $quiz_id, $attempt->user_id ); - wp_redirect( tutor_utils()->input_old( '_wp_http_referer' ) ); + wp_safe_redirect( wp_get_referer() ?: get_permalink( $quiz_id ) ); + exit; } /**