Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 15 additions & 24 deletions classes/Quiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -951,37 +952,26 @@
}

// 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 );

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-picking the served set at submit depends on RAND(attempt_id) + LIMIT returning the same rows as at render. That stops being true if the instructor adds, removes or reorders questions, or changes questions_order, while an attempt is open, or if the result of tutor_filter_unsupported_quiz_question_types changes (e.g. Pro turned on or off). The student then gets scored on questions they never saw.

Saving the served question IDs on the attempt when it starts, and validating against that list here, would be more robust.

$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 );

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question_mark is decimal(9,2), so intval() drops the fraction (7.50 → 7) while earned marks stay decimal. The pass percentage comes out too high, and a perfect score can show above 100%. The old get_var() kept the decimal. Please use (float) here.

$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;
Expand Down Expand Up @@ -1265,7 +1255,8 @@
$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 ) );

Check failure on line 1258 in classes/Quiz.php

View workflow job for this annotation

GitHub Actions / WPCS

Using short ternaries is not allowed as they are rarely used correctly
exit;
}

/**
Expand Down
Loading