diff --git a/classes/Quiz.php b/classes/Quiz.php index a66973e45b..0c600dea53 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; @@ -951,37 +952,26 @@ 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 ); - $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 ) ) { - $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; @@ -1265,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; } /**