fix: quiz enhancements and tests
This commit is contained in:
@@ -46,34 +46,30 @@ class LMSQuiz(Document):
|
||||
return result[0]
|
||||
|
||||
@frappe.whitelist()
|
||||
def submit(quiz, result):
|
||||
def quiz_summary(quiz, results):
|
||||
score = 0
|
||||
answer_map = {
|
||||
"is_correct_1": "option_1",
|
||||
"is_correct_2": "option_2",
|
||||
"is_correct_3": "option_3",
|
||||
"is_correct_4": "option_4"
|
||||
}
|
||||
result = json.loads(result)
|
||||
quiz_details = frappe.get_doc("LMS Quiz", quiz)
|
||||
results = json.loads(results)
|
||||
|
||||
for response in result:
|
||||
match = list(filter(lambda x: x.question == response.get("question"), quiz_details.questions))[0]
|
||||
correct_options = quiz_details.get_correct_options(match)
|
||||
correct_answers = [ match.get(answer_map[option]) for option in correct_options ]
|
||||
for result in results:
|
||||
correct = result["is_correct"][0]
|
||||
result["question"] = frappe.db.get_value("LMS Quiz Question",
|
||||
{"parent": quiz, "idx": result["question_index"]},
|
||||
["question"])
|
||||
|
||||
if response.get("answer") == correct_answers:
|
||||
response["result"] = "Right"
|
||||
score += 1
|
||||
else:
|
||||
response["result"] = "Wrong"
|
||||
response["answer"] = ("").join([ ans if idx == len(response.get("answer")) -1 else ans + ", " for idx, ans in enumerate(response.get("answer")) ])
|
||||
for point in result["is_correct"]:
|
||||
correct = correct and point
|
||||
|
||||
result["result"] = "Right" if correct else "Wrong"
|
||||
score += correct
|
||||
|
||||
del result["is_correct"]
|
||||
del result["question_index"]
|
||||
|
||||
frappe.get_doc({
|
||||
"doctype": "LMS Quiz Submission",
|
||||
"quiz": quiz,
|
||||
"result": result,
|
||||
"result": results,
|
||||
"score": score
|
||||
}).save(ignore_permissions=True)
|
||||
update_progress(quiz_details.lesson)
|
||||
|
||||
return score
|
||||
|
||||
@@ -3,6 +3,39 @@
|
||||
|
||||
# import frappe
|
||||
import unittest
|
||||
import frappe
|
||||
|
||||
class TestLMSQuiz(unittest.TestCase):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls) -> None:
|
||||
frappe.get_doc({
|
||||
"doctype": "LMS Quiz",
|
||||
"title": "Test Quiz"
|
||||
}).save()
|
||||
|
||||
def test_with_multiple_options(self):
|
||||
quiz = frappe.get_doc("LMS Quiz", "Test Quiz")
|
||||
quiz.append("questions", {
|
||||
"question": "Question multiple",
|
||||
"option_1": "Option 1",
|
||||
"is_correct_1": 1,
|
||||
"option_2": "Option 2",
|
||||
"is_correct_2": 1
|
||||
})
|
||||
quiz.save()
|
||||
self.assertTrue(quiz.questions[0].multiple)
|
||||
|
||||
def test_with_no_correct_option(self):
|
||||
quiz = frappe.get_doc("LMS Quiz", "Test Quiz")
|
||||
quiz.append("questions", {
|
||||
"question": "Question no correct option",
|
||||
"option_1": "Option 1",
|
||||
"option_2": "Option 2",
|
||||
})
|
||||
self.assertRaises(frappe.ValidationError, quiz.save)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls) -> None:
|
||||
frappe.db.delete("LMS Quiz", "Test Quiz")
|
||||
frappe.db.delete("LMS Quiz Question", {"parent": "Test Quiz"})
|
||||
|
||||
@@ -333,7 +333,7 @@ input[type=checkbox] {
|
||||
|
||||
.card-divider {
|
||||
border: 1px solid #F4F5F6;
|
||||
margin-bottom: 16px;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.card-divider-dark {
|
||||
@@ -487,23 +487,30 @@ input[type=checkbox] {
|
||||
--star-fill: #74808B;
|
||||
}
|
||||
|
||||
div.custom-checkbox>label>input {
|
||||
.custom-checkbox {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.custom-checkbox>label>input {
|
||||
visibility: hidden;
|
||||
}
|
||||
|
||||
div.custom-checkbox>label>img {
|
||||
height: 20px;
|
||||
width: 20px;
|
||||
.custom-checkbox>label>.empty-checkbox {
|
||||
height: 1.5rem;
|
||||
width: 1.5rem;
|
||||
border: 1px solid black;
|
||||
border-radius: 5px;
|
||||
}
|
||||
|
||||
div.custom-checkbox>label>input:checked+img {
|
||||
background: url(/assets/community/images/Vector.png);
|
||||
.custom-checkbox>label>input:checked+.empty-checkbox {
|
||||
background: url(/assets/community/icons/tick.svg);
|
||||
background-repeat: no-repeat;
|
||||
background-position: center center;
|
||||
background-size: 15px 15px;
|
||||
object-fit: contain;
|
||||
}
|
||||
|
||||
.quiz-label {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.course-card-wide {
|
||||
@@ -1013,8 +1020,24 @@ div.custom-checkbox>label>input:checked+img {
|
||||
color: red;
|
||||
}
|
||||
|
||||
.quiz-footer {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
}
|
||||
|
||||
.question {
|
||||
flex-direction: column;
|
||||
width: 688px;
|
||||
margin: auto;
|
||||
}
|
||||
|
||||
.question p {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.active-question .card-divider {
|
||||
margin-top: 1rem;
|
||||
}
|
||||
|
||||
.dark-links {
|
||||
@@ -1057,7 +1080,7 @@ div.custom-checkbox>label>input:checked+img {
|
||||
}
|
||||
|
||||
.course-content-parent .course-home-headings {
|
||||
margin: 0px 0px 16px;
|
||||
margin: 0px 0px 1rem;
|
||||
}
|
||||
|
||||
.lesson-pagination {
|
||||
|
||||
4
community/public/icons/minus-circle-green.svg
Normal file
4
community/public/icons/minus-circle-green.svg
Normal file
@@ -0,0 +1,4 @@
|
||||
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||||
<path d="M12 22C17.5228 22 22 17.5228 22 12C22 6.47715 17.5228 2 12 2C6.47715 2 2 6.47715 2 12C2 17.5228 6.47715 22 12 22Z" fill="#68D391" stroke="#68D391" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
<path d="M8 12H16" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 425 B |
1
community/public/icons/minus-circle.svg
Normal file
1
community/public/icons/minus-circle.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-minus-circle"><circle cx="12" cy="12" r="10"></circle><line x1="8" y1="12" x2="16" y2="12"></line></svg>
|
||||
|
After Width: | Height: | Size: 309 B |
@@ -1,3 +1 @@
|
||||
<svg class="icon">
|
||||
<use href="#icon-tick"></use>
|
||||
</svg>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-check"><polyline points="20 6 9 17 4 12"></polyline></svg>
|
||||
|
Before Width: | Height: | Size: 58 B After Width: | Height: | Size: 262 B |
3
community/public/icons/wrong.svg
Normal file
3
community/public/icons/wrong.svg
Normal file
@@ -0,0 +1,3 @@
|
||||
<svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||||
<path fill-rule="evenodd" clip-rule="evenodd" d="M10 20C15.5228 20 20 15.5228 20 10C20 4.47715 15.5228 0 10 0C4.47715 0 0 4.47715 0 10C0 15.5228 4.47715 20 10 20ZM6.0429 6.04289C6.43342 5.65237 7.06659 5.65237 7.45711 6.04289L10.0011 8.58692L12.5451 6.04294C12.9356 5.65242 13.5688 5.65242 13.9593 6.04294C14.3499 6.43347 14.3499 7.06663 13.9593 7.45716L11.4154 10.0011L13.9593 12.5451C14.3499 12.9356 14.3499 13.5688 13.9593 13.9593C13.5688 14.3499 12.9357 14.3499 12.5451 13.9593L10.0011 11.4154L7.45711 13.9594C7.06659 14.3499 6.43342 14.3499 6.0429 13.9594C5.65237 13.5689 5.65237 12.9357 6.0429 12.5452L8.58693 10.0011L6.0429 7.45711C5.65237 7.06658 5.65237 6.43342 6.0429 6.04289Z" fill="#F56B6B"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 808 B |
@@ -1,33 +1,59 @@
|
||||
{% set last_submission = quiz.get_last_submission_details() %}
|
||||
{% if last_submission %}
|
||||
<div class="pull-right">
|
||||
<div class="muted-text">Last Submitted On: {{ frappe.utils.pretty_date(last_submission.creation) }}</div>
|
||||
<div class="muted-text">Last Submission Score: {{ last_submission.score }}</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
<div id="title" class="course-home-headings">{{ quiz.title }}</div>
|
||||
<form id="quiz-form">
|
||||
{% for question in quiz.questions %}
|
||||
<div class="question mb-5" data-question="{{ question.question }}"
|
||||
data-multi="{{ question.multiple_correct_answers}}">
|
||||
<p> {{ loop.index }}. {{ question.question }}</p>
|
||||
{% set options = [question.option_1, question.option_2, question.option_3, question.option_4] %}
|
||||
{% for option in options %}
|
||||
{% if option %}
|
||||
<div class="custom-checkbox mb-2">
|
||||
<label>
|
||||
<input {% if question.multiple %} type="checkbox" {% else %} type="radio"
|
||||
name="{{ question.question | urlencode }}" {% endif %} class="option" value="{{ option | urlencode }}">
|
||||
<img />
|
||||
</label>
|
||||
<span class="label-area">{{ option }}</span>
|
||||
<div id="quiz-title" class="course-home-headings">{{ quiz.title }}</div>
|
||||
|
||||
<div class="card-divider"></div>
|
||||
|
||||
<div class="mt-5">
|
||||
<form id="quiz-form">
|
||||
<div class="questions">
|
||||
{% for question in quiz.questions %}
|
||||
<div class="question {% if loop.index == 1 %} active-question {% else %} hide {% endif %}"
|
||||
data-question="{{ question.question }}"data-multi="{{ question.multiple}}" data-qt-index="{{ loop.index }}">
|
||||
<p>{{ question.question }}</p>
|
||||
|
||||
{% if question.multiple %}
|
||||
<small class="font-weight-bold">Choose all answers that apply:</small>
|
||||
{% else %}
|
||||
<small class="font-weight-bold">Choose 1 answer:</small>
|
||||
{% endif %}
|
||||
|
||||
<div class="card-divider"></div>
|
||||
|
||||
{% set options = [question.option_1, question.option_2, question.option_3, question.option_4] %}
|
||||
|
||||
{% for option in options %}
|
||||
{% if option %}
|
||||
|
||||
<div class="custom-checkbox">
|
||||
<label class="quiz-label">
|
||||
<input class="option" value="{{ option | urlencode }}"
|
||||
data-correct="{{ question['is_correct_' + loop.index | string] }}"
|
||||
{% if question.multiple %} type="checkbox"
|
||||
{% else %} type="radio" name="{{ question.question | urlencode }}" {% endif %}>
|
||||
<img class="empty-checkbox mr-3"/>
|
||||
</label>
|
||||
<span class="label-area">{{ frappe.utils.md_to_html(option) }}</span>
|
||||
</div>
|
||||
|
||||
{% set explanation = question['explanation_' + loop.index | string] %}
|
||||
{% if explanation %}
|
||||
<small class="explanation muted-text hide">{{ explanation }}</small>
|
||||
{% endif %}
|
||||
|
||||
<div class="card-divider"></div>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endfor %}
|
||||
<button class="btn btn-secondary hide mb-5" id="try-again">Try Again</button>
|
||||
<button class="btn btn-primary" id="submit-quiz">Submit</button>
|
||||
<h4 class="success-message"></h4>
|
||||
<h5 class="score text-muted"></h5>
|
||||
</form>
|
||||
<div class="quiz-footer">
|
||||
<span class="font-weight-bold"> <span class="current-question">1</span> of {{ quiz.questions | length }}</span>
|
||||
<button class="btn btn-primary pull-right" id="check" disabled>Check</button>
|
||||
<button class="btn btn-primary hide" id="next">Next</button>
|
||||
<button class="btn btn-primary hide" id="summary">Summary</button>
|
||||
</div>
|
||||
<div class="button is-secondary pull-right hide" id="try-again">Try Again</div>
|
||||
<h4 class="success-message"></h4>
|
||||
<h5 class="score text-muted"></h5>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
@@ -1,13 +1,27 @@
|
||||
frappe.ready(() => {
|
||||
|
||||
localStorage.removeItem($("#quiz-title").text());
|
||||
|
||||
save_current_lesson();
|
||||
|
||||
$(".option").click((e) => {
|
||||
enable_check(e);
|
||||
})
|
||||
|
||||
$("#progress").click((e) => {
|
||||
mark_progress(e);
|
||||
});
|
||||
|
||||
$("#submit-quiz").click((e) => {
|
||||
submit_quiz(e);
|
||||
$("#summary").click((e) => {
|
||||
quiz_summary(e);
|
||||
});
|
||||
|
||||
$("#check").click((e) => {
|
||||
check_answer(e);
|
||||
});
|
||||
|
||||
$("#next").click((e) => {
|
||||
mark_active_question(e);
|
||||
});
|
||||
|
||||
$("#try-again").click((e) => {
|
||||
@@ -25,6 +39,27 @@ var save_current_lesson = () => {
|
||||
}
|
||||
}
|
||||
|
||||
var enable_check = (e) => {
|
||||
if ($(".option:checked").length && $("#check").attr("disabled")) {
|
||||
$("#check").removeAttr("disabled");
|
||||
}
|
||||
}
|
||||
|
||||
var mark_active_question = (e = undefined) => {
|
||||
var current_index;
|
||||
var next_index = 1;
|
||||
if (e) {
|
||||
e.preventDefault();
|
||||
current_index = $(".active-question").attr("data-qt-index");
|
||||
next_index = parseInt(current_index) + 1;
|
||||
}
|
||||
$(".question").addClass("hide").removeClass("active-question");
|
||||
$(`.question[data-qt-index='${next_index}']`).removeClass("hide").addClass("active-question");
|
||||
$(".current-question").text(`${next_index}`);
|
||||
$("#check").removeClass("hide").attr("disabled", true);
|
||||
$("#next").addClass("hide");
|
||||
}
|
||||
|
||||
var mark_progress = (e) => {
|
||||
var status = $(e.currentTarget).attr("data-progress");
|
||||
frappe.call({
|
||||
@@ -56,47 +91,77 @@ var change_progress_indicators = (status, e) => {
|
||||
$(e.currentTarget).text(label).attr("data-progress", data_progress);
|
||||
}
|
||||
|
||||
var submit_quiz = (e) => {
|
||||
var quiz_summary = (e) => {
|
||||
e.preventDefault();
|
||||
var result = [];
|
||||
$('.question').each((i, element) => {
|
||||
var options = $(element).find(".option");
|
||||
var answers = [];
|
||||
options.filter((i, op) => $(op).prop("checked")).each((i, elem) => answers.push(decodeURIComponent(elem.value)));
|
||||
result.push({
|
||||
"question": element.dataset.question,
|
||||
"answer": answers
|
||||
});
|
||||
});
|
||||
var quiz_name = $("#quiz-title").text();
|
||||
var total_questions = $(".question").length;
|
||||
|
||||
frappe.call({
|
||||
method: "community.lms.doctype.lms_quiz.lms_quiz.submit",
|
||||
method: "community.lms.doctype.lms_quiz.lms_quiz.quiz_summary",
|
||||
args: {
|
||||
quiz: $("#title").text(),
|
||||
result: result
|
||||
"quiz": quiz_name,
|
||||
"results": localStorage.getItem(quiz_name)
|
||||
},
|
||||
callback: (data) => {
|
||||
$("#submit-quiz").addClass("hide");
|
||||
var message = data.message == total_questions ? "Excellent Work" : "You were almost there."
|
||||
$(".question").addClass("hide");
|
||||
$(".quiz-footer").addClass("hide");
|
||||
$("#quiz-form").parent().prepend(
|
||||
`<div class="text-center summary"><h2>${message} 👏 </h2>
|
||||
<div class="font-weight-bold">${data.message}/${total_questions} correct.</div></div>`);
|
||||
$("#try-again").removeClass("hide");
|
||||
$(":input[type='checkbox']").prop("disabled", true);
|
||||
$(":input[type='radio']").prop("disabled", true);
|
||||
if (data.message == result.length) {
|
||||
$(".success-message").text("Congratulations, you cleared the quiz!");
|
||||
}
|
||||
else {
|
||||
$(".success-message").text("Some of your answers weren't correct. You can give it another shot.");
|
||||
}
|
||||
$(".score").text(`Score: ${data.message}/${result.length}`);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
var try_quiz_again = (e) => {
|
||||
e.preventDefault();
|
||||
$(":input[type='checkbox']").prop("disabled", false);
|
||||
$(":input[type='radio']").prop("disabled", false);
|
||||
$("#quiz-form").trigger("reset");
|
||||
$(".success-message").text("");
|
||||
$(".score").text("");
|
||||
$("#submit-quiz").removeClass("hide");
|
||||
$("#try-again").addClass("hide");
|
||||
window.location.reload();
|
||||
}
|
||||
|
||||
var check_answer = (e) => {
|
||||
e.preventDefault();
|
||||
|
||||
var quiz_name = $("#quiz-title").text();
|
||||
var total_questions = $(".question").length;
|
||||
var current_index = $(".active-question").attr("data-qt-index");
|
||||
|
||||
$(".explanation").removeClass("hide");
|
||||
$("#check").addClass("hide");
|
||||
current_index == total_questions ? $("#summary").removeClass("hide") : $("#next").removeClass("hide");
|
||||
|
||||
var [answer, is_correct] = parse_options();
|
||||
add_to_local_storage(quiz_name, current_index, answer, is_correct)
|
||||
}
|
||||
|
||||
var parse_options = () => {
|
||||
var answer = [];
|
||||
var is_correct = [];
|
||||
$(".active-question input").each((i, element) => {
|
||||
var correct = parseInt($(element).attr("data-correct"));
|
||||
if ($(element).prop("checked")) {
|
||||
answer.push(decodeURIComponent($(element).val()));
|
||||
correct && is_correct.push(1);
|
||||
correct ? add_icon(element, "check") : add_icon(element, "wrong");
|
||||
}
|
||||
else {
|
||||
correct && is_correct.push(0);
|
||||
correct ? add_icon(element, "minus-circle-green") : add_icon(element, "minus-circle");
|
||||
}
|
||||
})
|
||||
return [answer, is_correct];
|
||||
}
|
||||
|
||||
var add_icon = (element, icon) => {
|
||||
$(element).parent().empty().html(`<img class="mr-3" src="/assets/community/icons/${icon}.svg">`);
|
||||
}
|
||||
|
||||
var add_to_local_storage = (quiz_name, current_index, answer, is_correct) => {
|
||||
var quiz_stored = JSON.parse(localStorage.getItem(quiz_name));
|
||||
var quiz_obj = {
|
||||
"question_index": current_index,
|
||||
"answer": answer.join(),
|
||||
"is_correct": is_correct
|
||||
}
|
||||
quiz_stored ? quiz_stored.push(quiz_obj) : quiz_stored = [quiz_obj]
|
||||
localStorage.setItem(quiz_name, JSON.stringify(quiz_stored))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user