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
3 changes: 3 additions & 0 deletions assets/src/js/front/course/_spotlight-quiz.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ window.jQuery(document).ready($ => {
*/

function tutor_quiz_validation($question_wrap, validated) {
if ($question_wrap.attr('data-revealed') === '1') {
return validated;
}

var $required_answer_wrap = $question_wrap.find('.quiz-answer-required');

Expand Down
10 changes: 1 addition & 9 deletions classes/Quiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -2043,15 +2043,7 @@ private function apply_quiz_answer_review( int $attempt_id, $attempt_answer, str
* @return float
*/
private function calculate_attempt_earned_marks( int $attempt_id ): float {
$marks = QueryHelper::query(
'tutor_quiz_attempt_answers',
array(
'select' => 'SUM(achieved_mark) AS earned_marks',
'where' => array( 'quiz_attempt_id' => $attempt_id ),
)
);

return max( 0.0, (float) ( $marks[0]->earned_marks ?? 0.0 ) );
return QuizModel::get_attempt_answers_stats( $attempt_id )->total_earned_marks;
}

/**
Expand Down
12 changes: 9 additions & 3 deletions classes/QuizBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -942,15 +942,21 @@ private function set_current_quiz_questions_answer_ids( int $quiz_id ) {
public function is_valid_quiz_question_answer_payload( array $payload ): bool {
$payload_question_ids = wp_list_pluck( $payload, 'question_id' );
$payload_question_answers = wp_list_pluck( $payload, 'question_answers', 'question_id' );
$is_cb_question = wp_list_pluck( $payload, 'is_cb_question', 'question_id' );

$is_cb_question = array();
foreach ( $payload as $question ) {
if ( isset( $question['question_id'] ) ) {
$is_cb_question[ $question['question_id'] ] = ! empty( $question['is_cb_question'] );
}
}

// Remove content bank answers.
$payload_question_answers = array_filter( $payload_question_answers, fn( $question_id ) => ! $is_cb_question[ $question_id ], ARRAY_FILTER_USE_KEY );
$payload_question_answers = array_filter( $payload_question_answers, fn( $question_id ) => empty( $is_cb_question[ $question_id ] ), ARRAY_FILTER_USE_KEY );
$payload_question_answers = array_filter( array_values( $payload_question_answers ) ); // Filter out empty array.
$payload_answer_ids = wp_list_pluck( array_merge( ...$payload_question_answers ), 'answer_id' );

// Remove the hash id.
$payload_question_ids = array_filter( $payload_question_ids, fn( $id ) => is_numeric( $id ) && ! $is_cb_question[ $id ] );
$payload_question_ids = array_filter( $payload_question_ids, fn( $id ) => is_numeric( $id ) && empty( $is_cb_question[ $id ] ) );
$payload_answer_ids = array_filter( $payload_answer_ids, fn( $id ) => is_numeric( $id ) );

// For the new quiz current_quiz is null, in this there should not be any numeric question or answer id.
Expand Down
7 changes: 3 additions & 4 deletions helpers/QueryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1049,9 +1049,9 @@ public static function prepare_value( $value ) {
if ( is_int( $value ) ) {
$escaped_value = $wpdb->prepare( '%d', $value );
} elseif ( is_float( $value ) ) {
list( $whole, $decimal ) = explode( '.', $value );
$expression = '%.' . strlen( $decimal ) . 'f';
$escaped_value = $wpdb->prepare( $expression, $value );//phpcs:ignore
$value_parts = explode( '.', (string) $value );
$expression = '%.' . strlen( $value_parts[1] ?? '' ) . 'f';
$escaped_value = $wpdb->prepare( $expression, $value );//phpcs:ignore
} else {
$escaped_value = $wpdb->prepare( '%s', $value );
}
Expand Down Expand Up @@ -1509,5 +1509,4 @@ public static function get_table_schema( $table_name ) {

return $result;
}

}
3 changes: 2 additions & 1 deletion templates/learning-area/quiz/attempt.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ class="tutor-quiz-questions"
do_action( 'tutor_quiz/body/before', $tutor_is_started_quiz->quiz_id, $quiz_attempt_info );
foreach ( $questions as $index => $question ) {
$question_settings = maybe_unserialize( $question->question_settings );
$answer_required = isset( $question_settings['answer_required'] ) && '1' === $question_settings['answer_required'];
$is_committed_question = ! empty( $committed_attempt_answers[ (int) $question->question_id ] );
$answer_required = ! $is_committed_question && isset( $question_settings['answer_required'] ) && '1' === $question_settings['answer_required'];
$question_index = $index + 1;
$question->committed_answer = $committed_attempt_answers[ (int) $question->question_id ] ?? null;
?>
Expand Down
9 changes: 4 additions & 5 deletions templates/learning-area/quiz/question.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,11 @@
$question_id = (int) ( $question->question_id ?? 0 );
$field_name_base = sprintf( 'attempt[%d][quiz_question][%d]', $attempt_id, $question_id );

$answer_is_required = isset( $question_settings['answer_required'] ) && '1' === $question_settings['answer_required'];
$committed_answer = $committed_answer ?? ( $question->committed_answer ?? null );
$is_revealed = ! empty( $committed_answer );
$answer_is_required = ! $is_revealed && isset( $question_settings['answer_required'] ) && '1' === $question_settings['answer_required'];
$required_message = __( 'The answer for this question is required', 'tutor' );

$committed_answer = $committed_answer ?? ( $question->committed_answer ?? null );
$is_revealed = ! empty( $committed_answer );
$reveal_result = '';
$reveal_result = '';
if ( $is_revealed ) {
if ( '1' === (string) $committed_answer->is_correct ) {
$reveal_result = 'correct';
Expand Down
2 changes: 1 addition & 1 deletion templates/single/quiz/parts/choice-box.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
}
?>

<div class="quiz-question-ans-choice-area tutor-mt-40 question-type-<?php echo esc_attr( $question_type ); ?> <?php echo $answer_required ? 'quiz-answer-required' : ''; ?>" <?php echo $is_committed ? 'style="pointer-events: none;"' : ''; ?>>
<div class="quiz-question-ans-choice-area tutor-mt-40 question-type-<?php echo esc_attr( $question_type ); ?> <?php echo ( $answer_required && ! $is_committed ) ? 'quiz-answer-required' : ''; ?>" <?php echo $is_committed ? 'style="pointer-events: none;"' : ''; ?>>
<div id="<?php echo esc_attr( $id ); ?>" class="<?php echo esc_attr( $class ); ?> tutor-quiz-wrap">
<div class="tutor-row">
<?php if ( count( $answers ) ) : ?>
Expand Down
2 changes: 1 addition & 1 deletion templates/single/quiz/parts/question.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class="quiz-attempt-single-question quiz-attempt-single-question-<?php echo esc_

$answers = \Tutor\Models\QuizModel::get_answers_by_quiz_question( $question->question_id, $rand_choice );
$show_question_mark = (bool) tutor_utils()->avalue_dot( 'show_question_mark', $question_settings );
$answer_required = (bool) tutor_utils()->array_get( 'answer_required', $question_settings );
$answer_required = ! $is_question_committed && (bool) tutor_utils()->array_get( 'answer_required', $question_settings );
echo wp_kses(
'<div class="quiz-question-title tutor-fs-4 tutor-fw-medium tutor-color-black tutor-mb-20">',
array(
Expand Down
Loading