diff --git a/classes/Quiz.php b/classes/Quiz.php index 7e063734ed..8ce8e8d9d0 100644 --- a/classes/Quiz.php +++ b/classes/Quiz.php @@ -1050,7 +1050,7 @@ public function finishing_quiz_attempt() { } /** - * Get quiz total marks. + * Get total marks for a quiz * * @since 3.0.0 * @@ -1061,16 +1061,50 @@ public function finishing_quiz_attempt() { public static function get_quiz_total_marks( $quiz_id ) { global $wpdb; + if ( ! $quiz_id ) { + return 0; + } + + $max_questions = (int) tutor_utils()->get_quiz_option( $quiz_id, 'max_questions_for_answer' ); + $questions_order = tutor_utils()->get_quiz_option( $quiz_id, 'questions_order', 'rand' ); + + $order_by_map = array( + 'asc' => 'question_id ASC', + 'desc' => 'question_id DESC', + 'sorting' => 'question_order ASC', + ); + $order_by = $order_by_map[ $questions_order ] ?? 'question_order ASC'; + + $limit = $max_questions > 0 ? $max_questions : PHP_INT_MAX; + + // phpcs:disable $total_marks = $wpdb->get_var( $wpdb->prepare( - "SELECT SUM(question_mark) total_marks + "SELECT CASE + WHEN %s = 'rand' AND %d > 0 AND COUNT(question_id) > %d THEN 0 + ELSE ( + SELECT SUM(questions.question_mark) FROM ( + SELECT question_mark + FROM {$wpdb->prefix}tutor_quiz_questions + WHERE quiz_id = %d + ORDER BY {$order_by} + LIMIT %d + ) AS questions + ) + END FROM {$wpdb->prefix}tutor_quiz_questions - WHERE quiz_id=%d", + WHERE quiz_id = %d", + $questions_order, + $max_questions, + $max_questions, + $quiz_id, + $limit, $quiz_id ) ); + // phpcs:enable - return floatval( $total_marks ); + return (float) $total_marks; } /** @@ -1848,16 +1882,18 @@ public static function render_quiz_summary( $total_questions, $quiz_item_readabl ); } - $quiz_summary[] = array( - 'columns' => array( - array( - 'content' => '
- ' . SvgIcon::make()->name( Icon::PRIME_CHECK_CIRCLE )->size( 20 )->get() . __( 'Total Marks', 'tutor' ) . ' -
', + if ( ! empty( $total_marks ) ) { + $quiz_summary[] = array( + 'columns' => array( + array( + 'content' => '
+ ' . SvgIcon::make()->name( Icon::PRIME_CHECK_CIRCLE )->size( 20 )->get() . __( 'Total Marks', 'tutor' ) . ' +
', + ), + array( 'content' => (float) $total_marks ), ), - array( 'content' => $total_marks ), - ), - ); + ); + } $quiz_summary[] = array( 'columns' => array( diff --git a/templates/learning-area/quiz/content.php b/templates/learning-area/quiz/content.php index a716e2f3ae..8179fd1654 100644 --- a/templates/learning-area/quiz/content.php +++ b/templates/learning-area/quiz/content.php @@ -13,7 +13,7 @@ use Tutor\Helpers\UrlHelper; use TUTOR\Quiz; -use TUTOR\Models\QuizModel; +use Tutor\Models\QuizModel; global $tutor_current_post, $tutor_course_id; @@ -25,7 +25,6 @@ $quiz_id = $quiz->ID; $total_questions = (int) tutor_utils()->total_questions_for_student_by_quiz( $quiz_id ); $quiz_options = tutor_utils()->get_quiz_option( $quiz_id ); -$total_marks = Quiz::get_quiz_total_marks( $quiz_id ); $passing_grade = (int) ( $quiz_options['passing_grade'] ?? 0 ); $quiz_time = $quiz_options['time_limit'] ?? null; $has_time_limit = is_array( $quiz_time ) && ! empty( $quiz_time['time_value'] ) && (int) $quiz_time['time_value'] > 0; @@ -33,11 +32,13 @@ $quiz_item_readable = $has_time_limit ? $quiz_time['time_value'] . ' ' . $time_units[ $quiz_time['time_type'] ] : null; $quiz_attempt = ( new QuizModel() )->get_quiz_attempt( $quiz_id, $user_id ?? get_current_user_id() ); $earned_marks = 0; +$total_marks = 0; -if ( $quiz_attempt && $total_marks > 0 ) { - $earned_marks = (float) $quiz_attempt->earned_marks; - $total = (float) $total_marks; - $earned_marks = number_format( ( $earned_marks / $total_marks ) * 100, 2 ); +if ( is_object( $quiz_attempt ) && (float) ( $quiz_attempt->total_marks ?? 0 ) > 0 ) { + $total_marks = (float) $quiz_attempt->total_marks; + $earned_marks = QuizModel::calculate_attempt_earned_percentage( $quiz_attempt ); +} else { + $total_marks = Quiz::get_quiz_total_marks( $quiz_id ); } $limit_attempts = (int) $quiz_options['limit_attempts_allowed'] ?? 0; $allowed_attempts = $limit_attempts ? $quiz_options['attempts_allowed'] ?? '' : '1';