Skip to content
Merged
Show file tree
Hide file tree
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
62 changes: 49 additions & 13 deletions classes/Quiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ public function finishing_quiz_attempt() {
}

/**
* Get quiz total marks.
* Get total marks for a quiz
*
* @since 3.0.0
*
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -1848,16 +1882,18 @@ public static function render_quiz_summary( $total_questions, $quiz_item_readabl
);
}

$quiz_summary[] = array(
'columns' => array(
array(
'content' => '<div class="tutor-flex tutor-gap-3 tutor-items-center">
' . SvgIcon::make()->name( Icon::PRIME_CHECK_CIRCLE )->size( 20 )->get() . __( 'Total Marks', 'tutor' ) . '
</div>',
if ( ! empty( $total_marks ) ) {
$quiz_summary[] = array(
'columns' => array(
array(
'content' => '<div class="tutor-flex tutor-gap-3 tutor-items-center">
' . SvgIcon::make()->name( Icon::PRIME_CHECK_CIRCLE )->size( 20 )->get() . __( 'Total Marks', 'tutor' ) . '
</div>',
),
array( 'content' => (float) $total_marks ),
),
array( 'content' => $total_marks ),
),
);
);
}

$quiz_summary[] = array(
'columns' => array(
Expand Down
13 changes: 7 additions & 6 deletions templates/learning-area/quiz/content.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -25,19 +25,20 @@
$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;
$time_units = Quiz::quiz_time_units();
$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';
Expand Down
Loading