feat: add a new quiz
This commit is contained in:
@@ -258,6 +258,7 @@ def save_chapter(course, title, chapter_description, idx, chapter):
|
|||||||
|
|
||||||
return doc.name
|
return doc.name
|
||||||
|
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def save_lesson(title, body, chapter, preview, idx, lesson):
|
def save_lesson(title, body, chapter, preview, idx, lesson):
|
||||||
if lesson:
|
if lesson:
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import frappe
|
|||||||
from frappe.model.document import Document
|
from frappe.model.document import Document
|
||||||
import json
|
import json
|
||||||
from frappe import _
|
from frappe import _
|
||||||
|
from frappe.utils import cstr
|
||||||
|
|
||||||
class LMSQuiz(Document):
|
class LMSQuiz(Document):
|
||||||
def validate(self):
|
def validate(self):
|
||||||
@@ -77,3 +78,31 @@ def quiz_summary(quiz, results):
|
|||||||
}).save(ignore_permissions=True)
|
}).save(ignore_permissions=True)
|
||||||
|
|
||||||
return score
|
return score
|
||||||
|
|
||||||
|
|
||||||
|
@frappe.whitelist()
|
||||||
|
def save_quiz(quiz_title, questions):
|
||||||
|
doc = frappe.get_doc({
|
||||||
|
"doctype": "LMS Quiz",
|
||||||
|
"title": quiz_title
|
||||||
|
})
|
||||||
|
doc.save(ignore_permissions=True)
|
||||||
|
for index, row in enumerate(json.loads(questions)):
|
||||||
|
question_details = {
|
||||||
|
"doctype": "LMS Quiz Question",
|
||||||
|
"parent": doc.name,
|
||||||
|
"question": row["question"],
|
||||||
|
"parenttype": "LMS Quiz",
|
||||||
|
"parentfield": "questions",
|
||||||
|
"idx": index + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
for num in range(1,5):
|
||||||
|
question_details["option_" + cstr(num)] = row["option_" + cstr(num)]
|
||||||
|
question_details["explanation_" + cstr(num)] = row["explanation_" + cstr(num)]
|
||||||
|
question_details["is_correct_" + cstr(num)] = row["is_correct_" + cstr(num)]
|
||||||
|
|
||||||
|
question_doc = frappe.get_doc(question_details)
|
||||||
|
question_doc.save(ignore_permissions=True)
|
||||||
|
|
||||||
|
return doc.name
|
||||||
|
|||||||
@@ -135,9 +135,6 @@ const scroll_to_chapter_container = () => {
|
|||||||
const save_chapter = (e) => {
|
const save_chapter = (e) => {
|
||||||
let target = $(e.currentTarget);
|
let target = $(e.currentTarget);
|
||||||
let parent = target.closest(".chapter-parent");
|
let parent = target.closest(".chapter-parent");
|
||||||
console.log(parent)
|
|
||||||
console.log(parent.find(".chapter-description"))
|
|
||||||
debugger;
|
|
||||||
frappe.call({
|
frappe.call({
|
||||||
method: "lms.lms.doctype.lms_course.lms_course.save_chapter",
|
method: "lms.lms.doctype.lms_course.lms_course.save_chapter",
|
||||||
args: {
|
args: {
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ frappe.ready(() => {
|
|||||||
|
|
||||||
|
|
||||||
const add_question = (e) => {
|
const add_question = (e) => {
|
||||||
let add_after = $(".quiz-card").length ? $(".quiz-card") : $("#quiz-title");
|
let add_after = $(".quiz-card").length ? $(".quiz-card:last") : $("#quiz-title");
|
||||||
let question_template = `<div class="quiz-card">
|
let question_template = `<div class="quiz-card">
|
||||||
<div contenteditable="true" data-placeholder="${__("Question")}" class="question mb-4"></div>
|
<div contenteditable="true" data-placeholder="${__("Question")}" class="question mb-4"></div>
|
||||||
</div>`;
|
</div>`;
|
||||||
@@ -52,16 +52,14 @@ const save_question = (e) => {
|
|||||||
if (!$("#quiz-title").text()) {
|
if (!$("#quiz-title").text()) {
|
||||||
frappe.throw(__("Quiz Title is mandatory."));
|
frappe.throw(__("Quiz Title is mandatory."));
|
||||||
}
|
}
|
||||||
console.log(get_questions());
|
|
||||||
debugger;
|
|
||||||
frappe.call({
|
frappe.call({
|
||||||
method: lms.lms.doctype.lms_quiz.lms_quiz.save_quiz,
|
method: "lms.lms.doctype.lms_quiz.lms_quiz.save_quiz",
|
||||||
args: {
|
args: {
|
||||||
"quiz-title": $("#quiz-title"),
|
"quiz_title": $("#quiz-title").text(),
|
||||||
"questions": get_questions()
|
"questions": get_questions()
|
||||||
},
|
},
|
||||||
callback: (data) => {
|
callback: (data) => {
|
||||||
|
window.location.href = "/quizzes";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -70,22 +68,35 @@ const save_question = (e) => {
|
|||||||
const get_questions = () => {
|
const get_questions = () => {
|
||||||
let questions = [];
|
let questions = [];
|
||||||
|
|
||||||
$(".quiz-card").each((i, elem) => {
|
$(".quiz-card").each((i, el) => {
|
||||||
|
|
||||||
if (!$(elem).find(".question").text())
|
if (!$(el).find(".question").text())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
let question_details = {};
|
let details = {};
|
||||||
question_details["question"] = $(elem).find(".question").text();
|
let one_correct_option = false;
|
||||||
|
details["question"] = $(el).find(".question").text();
|
||||||
|
|
||||||
Array.from({length: 4}, (x, i) => {
|
Array.from({length: 4}, (x, i) => {
|
||||||
let num = i + 1;
|
let num = i + 1;
|
||||||
question_details[`option_${num}`] = $(`.option-${num} .option-input:first`).text();
|
details[`option_${num}`] = $(el).find(`.option-${num} .option-input:first`).text();
|
||||||
question_details[`explanation_${num}`] = $(`.option-${num} .option-input:last`).text();
|
details[`explanation_${num}`] = $(el).find(`.option-${num} .option-input:last`).text();
|
||||||
question_details[`is_correct_${num}`] = $(`.option-${num} .option-checkbox`).find("input").prop("checked");
|
|
||||||
|
let is_correct = $(el).find(`.option-${num} .option-checkbox`).find("input").prop("checked");
|
||||||
|
if (is_correct)
|
||||||
|
one_correct_option = true;
|
||||||
|
|
||||||
|
details[`is_correct_${num}`] = is_correct;
|
||||||
});
|
});
|
||||||
questions.push(question_details);
|
|
||||||
|
if (!details["option_1"] || !details["option_2"]) {
|
||||||
|
frappe.throw(__("Each question must have at least two options."))
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!one_correct_option)
|
||||||
|
frappe.throw(__("Each question must have at least one correct option."))
|
||||||
|
questions.push(details);
|
||||||
});
|
});
|
||||||
|
|
||||||
return questions
|
return questions;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user