fix: Align question types and add Previous button to assessment

This commit is contained in:
Ruben Ramirez 2025-04-04 08:15:40 -05:00
parent 21204d2682
commit 38bf3e4b4d
2 changed files with 35 additions and 3 deletions

View file

@ -38,6 +38,7 @@
var $questionsContainer = $('#quiztech-questions-container');
var $questionContainers = $questionsContainer.find('.quiztech-question-container');
var $timerDisplay = $('#quiztech-timer');
var $prevButton = $('#quiztech-prev-question'); // ADDED SELECTOR
var $nextButton = $('#quiztech-next-question');
var $submitAssessmentButton = $('#quiztech-submit-assessment');
var $completionMessage = $('#quiztech-completion-message');
@ -98,6 +99,7 @@
// Handle case with no questions if needed
showMessage($assessmentMessages, 'No questions found for this assessment.', true);
$timerDisplay.hide();
$prevButton.hide(); // ADDED
$nextButton.hide();
$submitAssessmentButton.hide();
}
@ -201,8 +203,13 @@
}
}
} else if ($input.is(':checkbox')) {
// Example for checkboxes (if added later) - might need array handling
answer = $input.is(':checked') ? $input.val() : ''; // Simplistic, adjust if multiple checkboxes per question
// Collect values from all checked checkboxes with the same name
var groupName = $input.attr('name');
var checkedValues = [];
$assessmentForm.find('input[name="' + groupName + '"]:checked').each(function() {
checkedValues.push($(this).val());
});
answer = JSON.stringify(checkedValues); // Store as JSON string
} else {
answer = $input.val(); // For textarea, text input, select
}
@ -256,7 +263,7 @@
if ($input.is(':radio')) {
var groupName = $input.attr('name');
value = $assessmentForm.find('input[name="' + groupName + '"]:checked').val();
} else {
} else { // Includes textarea, text, number, etc.
value = $input.val();
}
if (!value) {
@ -264,6 +271,8 @@
// Maybe add a visual cue near the input?
}
});
// Note: Checkbox groups might not have 'required' on individual inputs,
// validation logic might need adjustment if a checkbox answer is mandatory.
if (!isValid) {
showMessage($assessmentMessages, 'Please answer the current question before proceeding.', true);
@ -281,12 +290,34 @@
}
});
// ADDED: Previous Button Handler
$prevButton.on('click', function() {
if (currentQuestionIndex > 0) {
clearMessage($assessmentMessages); // Clear any messages
// Hide current, show previous
$questionContainers.eq(currentQuestionIndex).addClass('quiztech-question-hidden');
currentQuestionIndex--;
$questionContainers.eq(currentQuestionIndex).removeClass('quiztech-question-hidden');
updateNavigationButtons();
}
});
function updateNavigationButtons() {
if (totalQuestions <= 0) {
$prevButton.hide();
$nextButton.hide();
$submitAssessmentButton.hide();
return;
}
// Previous Button Visibility
if (currentQuestionIndex > 0) {
$prevButton.show();
} else {
$prevButton.hide();
}
// Next/Submit Button Visibility
if (currentQuestionIndex >= totalQuestions - 1) {
// Last question
$nextButton.hide();

View file

@ -240,6 +240,7 @@ $show_pre_screening = ! empty( $pre_screening_questions ) && is_array( $pre_scre
</div> <!-- #quiztech-questions-container -->
<div class="quiztech-navigation">
<button type="button" id="quiztech-prev-question" style="display: none;"><?php esc_html_e( 'Previous Question', 'quiztech' ); ?></button> <?php // ADDED BUTTON ?>
<button type="button" id="quiztech-next-question"><?php esc_html_e( 'Next Question', 'quiztech' ); ?></button>
<button type="button" id="quiztech-submit-assessment" style="display: none;"><?php esc_html_e( 'Submit Assessment', 'quiztech' ); ?></button>
</div>