diff --git a/lms/www/batch/quiz.html b/lms/www/batch/quiz.html index 9eacd460..626d5530 100644 --- a/lms/www/batch/quiz.html +++ b/lms/www/batch/quiz.html @@ -37,7 +37,7 @@
{% if question.question %} {{ question.question }} {% endif %}
- {% for option in ["Choices", "User Input"] %} {% endfor %} @@ -46,11 +46,10 @@ {% for i in range(1,5) %} {% set num = frappe.utils.cstr(i) %} - {% if question.type == "Choices" %} {% set option = question["option_" + num] %} {% set explanation = question["explanation_" + num] %} -
+
- {% else %} {% set possible_answer = question["possibility_" + num] %} -
+
@@ -77,7 +75,6 @@
- {% endif %} {% endfor %}
{% endfor %} diff --git a/lms/www/batch/quiz.js b/lms/www/batch/quiz.js index e2065612..2077d90f 100644 --- a/lms/www/batch/quiz.js +++ b/lms/www/batch/quiz.js @@ -15,15 +15,25 @@ frappe.ready(() => { frappe.utils.copy_to_clipboard($(e.currentTarget).data("name")); }); + $(document).on("change", ".type", function () { + toggle_form($(this)); + }); + get_questions(); }); -const add_question = () => { - /* if ($(".new-quiz-card").length) { - scroll_to_question_container(); - return; - } */ +const toggle_form = (el) => { + let type = el.val(); + if (type === "Choices") { + el.siblings(".option-group").removeClass("hide"); + el.siblings(".possibility-group").addClass("hide"); + } else if (type === "User Input") { + el.siblings(".option-group").addClass("hide"); + el.siblings(".possibility-group").removeClass("hide"); + } +}; +const add_question = () => { let add_after = $(".quiz-card").length ? $(".quiz-card:last") : $("#quiz-title"); @@ -31,7 +41,11 @@ const add_question = () => {
-
`; + +
`; $(question_template).insertAfter(add_after); get_question_template(); $(".btn-save-question").removeClass("hide"); @@ -40,11 +54,31 @@ const add_question = () => { const get_question_template = () => { Array.from({ length: 4 }, (x, num) => { let option_template = get_option_template(num + 1); + let add_after = $(".quiz-card:last .option-group").length ? $(".quiz-card:last .option-group").last() - : $(".question:last"); + : $(".type:last"); question_template = $(option_template).insertAfter(add_after); }); + + Array.from({ length: 4 }, (x, num) => { + let possibility_template = get_possibility_template(num + 1); + let add_after = $(".quiz-card:last .possibility-group").length + ? $(".quiz-card:last .possibility-group").last() + : $(".quiz-card:last .option-group:last"); + question_template = $(possibility_template).insertAfter(add_after); + }); +}; + +const get_possibility_template = (num) => { + return `
+ +
+
+
+
+
+
`; }; const get_option_template = (num) => { @@ -95,6 +129,7 @@ const get_questions = () => { let correct_options = 0; let possibilities = 0; + details["element"] = el; details["question"] = $(el).find(".question").text(); details["question_name"] = $(el).find(".question").data("question") || ""; @@ -138,16 +173,21 @@ const get_questions = () => { const validate_mandatory = (details, correct_options, possibilities) => { if (details["type"] == "Choices") { - if (!details["option_1"] || !details["option_2"]) + if (!details["option_1"] || !details["option_2"]) { + scroll_to_element(details["element"]); frappe.throw(__("Each question must have at least two options.")); + } - if (!correct_options) + if (!correct_options) { + scroll_to_element(details["element"]); frappe.throw( __( "Question with choices must have at least one correct option." ) ); + } } else if (!possibilities) { + scroll_to_element(details["element"]); frappe.throw( __( "Question with user input must have at least one possible answer." @@ -157,11 +197,16 @@ const validate_mandatory = (details, correct_options, possibilities) => { }; const scroll_to_question_container = () => { - $([document.documentElement, document.body]).animate( - { - scrollTop: $(".new-quiz-card").offset().top, - }, - 1000 - ); + scroll_to_element(".new-quiz-card:last"); $(".new-quiz-card").find(".question").focus(); }; + +const scroll_to_element = (element) => { + if ($(element).length) + $([document.documentElement, document.body]).animate( + { + scrollTop: $(element).offset().top, + }, + 1000 + ); +};