Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added option to navigate quiz using keyboard #2465

Merged
merged 2 commits into from
Feb 12, 2024
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 css/common.css
Original file line number Diff line number Diff line change
Expand Up @@ -636,4 +636,7 @@ body .ui-tooltip.ui-widget-content {
padding: 1rem;
border-radius: 0.25rem;
margin-bottom: 1rem;
}
.qsm-question-wrapper.qsm-active-question{
zoom: 105%;
}
79 changes: 70 additions & 9 deletions js/qsm-quiz.js
Original file line number Diff line number Diff line change
Expand Up @@ -1895,15 +1895,76 @@ jQuery(document).on( 'click', '.qsm-quiz-container', function() {
jQuery('.qsm-quiz-container').removeClass('qsm-recently-active');
jQuery(this).addClass('qsm-recently-active');
});
jQuery(document).keydown(function(e) {
if (e.keyCode === 39) {
jQuery('.qsm-quiz-container.qsm-recently-active').find('.mlw_next:visible').click();
}
if (e.keyCode === 37) {
jQuery('.qsm-quiz-container.qsm-recently-active').find('.mlw_previous:visible').click();
}
if (e.keyCode === 13) {
jQuery('.qsm-quiz-container.qsm-recently-active').find('.qsm-submit-btn:visible').click();
jQuery(document).keydown(function(event) {
if (jQuery('.qsm-quiz-container.qsm-recently-active').length) {
jQuery(document).trigger('qsm_keyboard_quiz_action_start', event);

if ([39, 37, 13, 9].includes(event.keyCode)) {
event.preventDefault();
}
if (event.keyCode === 39) {
jQuery('.qsm-quiz-container.qsm-recently-active').find('.mlw_next:visible').click();
}
if (event.keyCode === 37) {
jQuery('.qsm-quiz-container.qsm-recently-active').find('.mlw_previous:visible').click();
}
if (event.keyCode === 13 && jQuery('textarea:focus').length === 0) {
jQuery('.qsm-quiz-container.qsm-recently-active').find('.qsm-submit-btn:visible').click();
jQuery('.qsm-quiz-container.qsm-recently-active').find('.mlw_next:visible').click();
}
if (event.keyCode === 40 && jQuery('.qsm-quiz-container.qsm-recently-active .qsm-question-wrapper.qsm-active-question .qmn_radio_answers:not(.qsm_multiple_grid_answers)').length) {
event.preventDefault();
let checkedInputs = jQuery('.qsm-quiz-container.qsm-recently-active .qsm-question-wrapper.qsm-active-question .qmn_radio_answers .qmn_mc_answer_wrap input:checked, .qsm-quiz-container.qsm-recently-active .qsm-question-wrapper.qsm-active-question .qmn_radio_answers .mlw_horizontal_choice input:checked');
if (checkedInputs.length === 0) {
jQuery('.qsm-quiz-container.qsm-recently-active .qsm-question-wrapper.qsm-active-question .qmn_radio_answers').find('input:first').click();
} else {
let nextInput = checkedInputs.closest('.qmn_mc_answer_wrap, .mlw_horizontal_choice').next('.qmn_mc_answer_wrap, .mlw_horizontal_choice').find('input[type="radio"]');
if (nextInput.length) {
nextInput.click();
} else {
jQuery('.qsm-quiz-container.qsm-recently-active .qsm-question-wrapper.qsm-active-question .qmn_radio_answers').find('input:first').click();
}
}
}
if (event.keyCode === 38 && jQuery('.qsm-quiz-container.qsm-recently-active .qsm-question-wrapper.qsm-active-question .qmn_radio_answers:not(.qsm_multiple_grid_answers)').length) {
event.preventDefault();
let checkedInputs = jQuery('.qsm-quiz-container.qsm-recently-active .qsm-question-wrapper.qsm-active-question .qmn_radio_answers .qmn_mc_answer_wrap input:checked, .qsm-quiz-container.qsm-recently-active .qsm-question-wrapper.qsm-active-question .qmn_radio_answers .mlw_horizontal_choice input:checked');
if (checkedInputs.length === 0) {
jQuery('.qsm-quiz-container.qsm-recently-active .qsm-question-wrapper.qsm-active-question .qmn_radio_answers').find('.qmn_mc_answer_wrap input:last, .mlw_horizontal_choice input:last').click();
} else {
let prevInput = checkedInputs.closest('.qmn_mc_answer_wrap, .mlw_horizontal_choice').prev('.qmn_mc_answer_wrap, .mlw_horizontal_choice').find('input[type="radio"]');
if (prevInput.length) {
prevInput.click();
} else {
jQuery('.qsm-quiz-container.qsm-recently-active .qsm-question-wrapper.qsm-active-question .qmn_radio_answers').find('.qmn_mc_answer_wrap input:last, .mlw_horizontal_choice input:last').click();
}
}
}
if (event.shiftKey && event.keyCode === 9) {
let active_question = jQuery('.qsm-quiz-container.qsm-recently-active .qsm-question-wrapper.qsm-active-question');
if (active_question.length) {
jQuery('.qsm-quiz-container.qsm-recently-active .qsm-question-wrapper').removeClass("qsm-active-question");
active_question.prev('.qsm-question-wrapper:visible').addClass("qsm-active-question");
} else {
jQuery(".qsm-quiz-container.qsm-recently-active .qsm-question-wrapper:visible:first-child").addClass("qsm-active-question");
}
} else if (event.keyCode === 9) {
let active_question = jQuery('.qsm-quiz-container.qsm-recently-active .qsm-question-wrapper.qsm-active-question');
if (active_question.length) {
jQuery('.qsm-quiz-container.qsm-recently-active .qsm-question-wrapper').removeClass("qsm-active-question");
active_question.next('.qsm-question-wrapper:visible').addClass("qsm-active-question");
} else {
jQuery(".qsm-quiz-container.qsm-recently-active .qsm-question-wrapper:visible:first-child").addClass("qsm-active-question");
}
}
if (event.keyCode === 9) {
let active_question = jQuery('.qsm-quiz-container.qsm-recently-active .qsm-question-wrapper.qsm-active-question');
if (active_question.length) {
jQuery('.qsm-quiz-container.qsm-recently-active .qsm-question-wrapper.qsm-active-question input:first').focus();
}
}
jQuery(document).trigger('qsm_keyboard_quiz_action_end', event);
}
});


8 changes: 4 additions & 4 deletions php/admin/settings-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ public function qsm_global_limit_number_of_questions() {
<input class="small-text" type="number" step="1" min="0" id="qsm-question-from-total-input" name="qsm-quiz-settings[question_from_total]" value="<?php echo esc_attr( $qsm_question_from_total ); ?>">
<?php esc_html_e( 'Maximum question limit', 'quiz-master-next'); ?>
</fieldset>
<fieldset class="buttonset buttonset-hide" data-hide="1" id="qsm-question-per-category" style="">
<fieldset class="buttonset buttonset-hide" data-hide="1" id="qsm-question-per-category">
<input class="small-text" type="number" step="1" min="0" id="qsm-question-per-category-input" name="qsm-quiz-settings[question_per_category]" value="<?php echo esc_attr( $qsm_question_per_category ); ?>">
<span class="qsm-opt-tr">
<?php esc_html_e( 'Limit number of questions per category', 'quiz-master-next'); ?>
Expand Down Expand Up @@ -873,10 +873,10 @@ public function default_answers(){
*/
public function qsm_global_enable_comments() {
global $globalQuizsetting;
$qsm_comment_section = ( isset( $globalQuizsetting['comment_section'] ) && '' !== $globalQuizsetting['comment_section'] ? $globalQuizsetting['comment_section'] : '0' );
$qsm_comment_section = ( isset( $globalQuizsetting['comment_section'] ) && '' !== $globalQuizsetting['comment_section'] ? $globalQuizsetting['comment_section'] : 0 );
?>
<label for="comment_section-0">
<input type="checkbox" id="comment_section-0" name="qsm-quiz-settings[comment_section]" value="0" <?php checked( $qsm_comment_section, 0 ); ?> >
<label class="qsm-opt-tr" for="qsm-comment-section">
<input type="checkbox" id="qsm-comment-section" name="qsm-quiz-settings[comment_section]" value="0" <?php checked( $qsm_comment_section, 0 ); ?> >
<?php esc_html_e( 'Allow users to post comments after completion', 'quiz-master-next' ); ?>
</label>
<?php
Expand Down
1 change: 0 additions & 1 deletion php/classes/class-qsm-install.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ public function register_default_settings() {
'option_tab' => 'general',
);
$mlwQuizMasterNext->pluginHelper->register_quiz_setting( $field_array, 'quiz_options' );

/* ===== Generat tab end ======== */
/* ===== Submission tab start ======== */
$field_array = array(
Expand Down