feat: update quiz
This commit is contained in:
@@ -29,7 +29,7 @@ class CourseLesson(Document):
|
|||||||
e = frappe.get_doc(doctype_map[section], name)
|
e = frappe.get_doc(doctype_map[section], name)
|
||||||
e.lesson = self.name
|
e.lesson = self.name
|
||||||
e.index_ = index
|
e.index_ = index
|
||||||
e.save()
|
e.save(ignore_permissions=True)
|
||||||
index += 1
|
index += 1
|
||||||
self.update_orphan_documents(doctype_map[section], documents)
|
self.update_orphan_documents(doctype_map[section], documents)
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ from __future__ import unicode_literals
|
|||||||
import frappe
|
import frappe
|
||||||
from frappe.model.document import Document
|
from frappe.model.document import Document
|
||||||
import json
|
import json
|
||||||
from ...utils import slugify
|
from ...utils import generate_slug
|
||||||
from frappe.utils import flt, cint
|
from frappe.utils import flt, cint
|
||||||
from lms.lms.utils import get_chapters
|
from lms.lms.utils import get_chapters
|
||||||
|
|
||||||
@@ -59,22 +59,11 @@ class LMSCourse(Document):
|
|||||||
frappe.enqueue(method=frappe.sendmail, queue='short', timeout=300, is_async=True, **email_args)
|
frappe.enqueue(method=frappe.sendmail, queue='short', timeout=300, is_async=True, **email_args)
|
||||||
frappe.db.set_value("LMS Course Interest", user.name, "email_sent", True)
|
frappe.db.set_value("LMS Course Interest", user.name, "email_sent", True)
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def find(name):
|
|
||||||
"""Returns the course with specified name.
|
|
||||||
"""
|
|
||||||
return find("LMS Course", published=True, name=name)
|
|
||||||
|
|
||||||
def autoname(self):
|
def autoname(self):
|
||||||
if not self.name:
|
if not self.name:
|
||||||
self.name = self.generate_slug(title=self.title)
|
self.name = generate_slug(self.title, "LMS Course")
|
||||||
|
|
||||||
def generate_slug(self, title):
|
|
||||||
result = frappe.get_all(
|
|
||||||
'LMS Course',
|
|
||||||
fields=['name'])
|
|
||||||
slugs = set([row['name'] for row in result])
|
|
||||||
return slugify(title, used_slugs=slugs)
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f"<Course#{self.name}>"
|
return f"<Course#{self.name}>"
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
"actions": [],
|
"actions": [],
|
||||||
"allow_import": 1,
|
"allow_import": 1,
|
||||||
"allow_rename": 1,
|
"allow_rename": 1,
|
||||||
"autoname": "field:title",
|
|
||||||
"creation": "2021-06-07 10:50:17.893625",
|
"creation": "2021-06-07 10:50:17.893625",
|
||||||
"doctype": "DocType",
|
"doctype": "DocType",
|
||||||
"editable_grid": 1,
|
"editable_grid": 1,
|
||||||
@@ -18,7 +17,9 @@
|
|||||||
{
|
{
|
||||||
"fieldname": "title",
|
"fieldname": "title",
|
||||||
"fieldtype": "Data",
|
"fieldtype": "Data",
|
||||||
|
"in_list_view": 1,
|
||||||
"label": "Title",
|
"label": "Title",
|
||||||
|
"reqd": 1,
|
||||||
"unique": 1
|
"unique": 1
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -49,11 +50,10 @@
|
|||||||
],
|
],
|
||||||
"index_web_pages_for_search": 1,
|
"index_web_pages_for_search": 1,
|
||||||
"links": [],
|
"links": [],
|
||||||
"modified": "2022-05-16 14:47:55.364743",
|
"modified": "2022-08-19 17:54:41.685182",
|
||||||
"modified_by": "Administrator",
|
"modified_by": "Administrator",
|
||||||
"module": "LMS",
|
"module": "LMS",
|
||||||
"name": "LMS Quiz",
|
"name": "LMS Quiz",
|
||||||
"naming_rule": "By fieldname",
|
|
||||||
"owner": "Administrator",
|
"owner": "Administrator",
|
||||||
"permissions": [
|
"permissions": [
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -6,8 +6,15 @@ from frappe.model.document import Document
|
|||||||
import json
|
import json
|
||||||
from frappe import _
|
from frappe import _
|
||||||
from frappe.utils import cstr
|
from frappe.utils import cstr
|
||||||
|
from lms.lms.utils import generate_slug
|
||||||
|
|
||||||
class LMSQuiz(Document):
|
class LMSQuiz(Document):
|
||||||
|
|
||||||
|
def autoname(self):
|
||||||
|
if not self.name:
|
||||||
|
self.name = generate_slug(self.title, "LMS Quiz")
|
||||||
|
|
||||||
|
|
||||||
def validate(self):
|
def validate(self):
|
||||||
self.validate_correct_answers()
|
self.validate_correct_answers()
|
||||||
|
|
||||||
@@ -20,7 +27,7 @@ class LMSQuiz(Document):
|
|||||||
question.multiple = 1
|
question.multiple = 1
|
||||||
|
|
||||||
if not len(correct_options):
|
if not len(correct_options):
|
||||||
frappe.throw(_("At least one answer must be correct for this question: {0}").format(frappe.bold(question.question)))
|
frappe.throw(_("At least one option must be correct for this question: {0}").format(frappe.bold(question.question)))
|
||||||
|
|
||||||
|
|
||||||
def get_correct_options(self, question):
|
def get_correct_options(self, question):
|
||||||
@@ -81,28 +88,42 @@ def quiz_summary(quiz, results):
|
|||||||
|
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def save_quiz(quiz_title, questions):
|
def save_quiz(quiz_title, questions, quiz):
|
||||||
|
if quiz:
|
||||||
|
doc = frappe.get_doc("LMS Quiz", quiz)
|
||||||
|
else:
|
||||||
doc = frappe.get_doc({
|
doc = frappe.get_doc({
|
||||||
"doctype": "LMS Quiz",
|
"doctype": "LMS Quiz",
|
||||||
|
})
|
||||||
|
|
||||||
|
doc.update({
|
||||||
"title": quiz_title
|
"title": quiz_title
|
||||||
})
|
})
|
||||||
doc.save(ignore_permissions=True)
|
doc.save(ignore_permissions=True)
|
||||||
|
|
||||||
for index, row in enumerate(json.loads(questions)):
|
for index, row in enumerate(json.loads(questions)):
|
||||||
question_details = {
|
if row["question_name"]:
|
||||||
|
question_doc = frappe.get_doc("LMS Quiz Question", row["question_name"])
|
||||||
|
else:
|
||||||
|
question_doc = frappe.get_doc({
|
||||||
"doctype": "LMS Quiz Question",
|
"doctype": "LMS Quiz Question",
|
||||||
"parent": doc.name,
|
"parent": doc.name,
|
||||||
"question": row["question"],
|
|
||||||
"parenttype": "LMS Quiz",
|
"parenttype": "LMS Quiz",
|
||||||
"parentfield": "questions",
|
"parentfield": "questions",
|
||||||
"idx": index + 1
|
"idx": index + 1
|
||||||
}
|
})
|
||||||
|
|
||||||
|
question_doc.update({
|
||||||
|
"question": row["question"]
|
||||||
|
})
|
||||||
|
|
||||||
for num in range(1,5):
|
for num in range(1,5):
|
||||||
question_details["option_" + cstr(num)] = row["option_" + cstr(num)]
|
question_doc.update({
|
||||||
question_details["explanation_" + cstr(num)] = row["explanation_" + cstr(num)]
|
"option_" + cstr(num): row["option_" + cstr(num)],
|
||||||
question_details["is_correct_" + cstr(num)] = row["is_correct_" + cstr(num)]
|
"explanation_" + cstr(num): row["explanation_" + cstr(num)],
|
||||||
|
"is_correct_" + cstr(num): row["is_correct_" + cstr(num)]
|
||||||
|
})
|
||||||
|
|
||||||
question_doc = frappe.get_doc(question_details)
|
|
||||||
question_doc.save(ignore_permissions=True)
|
question_doc.save(ignore_permissions=True)
|
||||||
|
|
||||||
return doc.name
|
return doc.name
|
||||||
|
|||||||
@@ -33,6 +33,15 @@ def slugify(title, used_slugs=[]):
|
|||||||
return new_slug
|
return new_slug
|
||||||
count = count+1
|
count = count+1
|
||||||
|
|
||||||
|
|
||||||
|
def generate_slug(title, doctype):
|
||||||
|
result = frappe.get_all(
|
||||||
|
doctype,
|
||||||
|
fields=['name'])
|
||||||
|
slugs = set([row['name'] for row in result])
|
||||||
|
return slugify(title, used_slugs=slugs)
|
||||||
|
|
||||||
|
|
||||||
def get_membership(course, member, batch=None):
|
def get_membership(course, member, batch=None):
|
||||||
filters = {
|
filters = {
|
||||||
"member": member,
|
"member": member,
|
||||||
|
|||||||
@@ -21,7 +21,7 @@
|
|||||||
|
|
||||||
{% macro BreadCrumb(quiz) %}
|
{% macro BreadCrumb(quiz) %}
|
||||||
<div class="breadcrumb">
|
<div class="breadcrumb">
|
||||||
<a class="dark-links" href="/courses">{{ _("Quizzes") }}</a>
|
<a class="dark-links" href="/quizzes">{{ _("Quizzes") }}</a>
|
||||||
<img class="ml-1 mr-1" src="/assets/lms/icons/chevron-right.svg">
|
<img class="ml-1 mr-1" src="/assets/lms/icons/chevron-right.svg">
|
||||||
<span class="breadcrumb-destination">{{ quiz.title if quiz.title else _("New Quiz") }}</span>
|
<span class="breadcrumb-destination">{{ quiz.title if quiz.title else _("New Quiz") }}</span>
|
||||||
</div>
|
</div>
|
||||||
@@ -32,42 +32,45 @@
|
|||||||
<div style="width: 60%;">
|
<div style="width: 60%;">
|
||||||
|
|
||||||
<div class="course-home-headings mb-2" data-placeholder="{{ _('Quiz Title') }}" id="quiz-title"
|
<div class="course-home-headings mb-2" data-placeholder="{{ _('Quiz Title') }}" id="quiz-title"
|
||||||
contenteditable="true">{% if quiz.title %}{{ quiz.title }}{% endif %}</div>
|
{% if quiz.name %} data-name="{{ quiz.name }}" {% endif %}
|
||||||
|
contenteditable="true" >{% if quiz.title %}{{ quiz.title }}{% endif %}</div>
|
||||||
<div class="mt-4">
|
|
||||||
<button class="btn btn-secondary btn-sm btn-question"> {{ _("New Question") }} </button>
|
|
||||||
<button class="btn btn-primary btn-sm btn-save-question ml-2 hide"> {{ _("Save") }} </button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
|
|
||||||
{% if quiz.question %}
|
{% if quiz.questions %}
|
||||||
{% for question in quiz.questions %}
|
{% for question in quiz.questions %}
|
||||||
<div class="quiz-card">
|
<div class="quiz-card">
|
||||||
<div contenteditable="true" data-placeholder="{{ _('Question') }}"
|
<div contenteditable="true" data-placeholder="{{ _('Question') }}" data-question="{{ question.name }}"
|
||||||
class="mb-4">{% if question.question %} {{ question.question }} {% endif %}</div>
|
class="question mb-4">{% if question.question %} {{ question.question }} {% endif %}</div>
|
||||||
|
|
||||||
{% for num in range(1,5) %}
|
{% for i in range(1,5) %}
|
||||||
{% set option = question["option_" + frappe.utils.cstr(num)] %}
|
{% set num = frappe.utils.cstr(i) %}
|
||||||
{% set explanation = question["explanation_" + frappe.utils.cstr(num)] %}
|
{% set option = question["option_" + num] %}
|
||||||
|
{% set explanation = question["explanation_" + num] %}
|
||||||
|
|
||||||
<div class="mt-4">
|
<div class="mt-4">
|
||||||
<label class=""> {{ _("Option") }} {{ frappe.utils.cstr(num) }} </label>
|
<label class=""> {{ _("Option") }} {{ num }} </label>
|
||||||
<div class="d-flex justify-content-between">
|
<div class="d-flex justify-content-between option-{{ num }}">
|
||||||
<div contenteditable="true" data-placeholder="{{ _('Option') }}"
|
<div contenteditable="true" data-placeholder="{{ _('Option') }}"
|
||||||
class="option-input">{% if option %}{{ option }}{% endif %}</div>
|
class="option-input">{% if option %}{{ option }}{% endif %}</div>
|
||||||
<div contenteditable="true" data-placeholder="{{ _('Explanation') }}"
|
<div contenteditable="true" data-placeholder="{{ _('Explanation') }}"
|
||||||
class="option-input">{% if explanation %}{{ explanation }}{% endif %}</div>
|
class="option-input">{% if explanation %}{{ explanation }}{% endif %}</div>
|
||||||
<div class="option-checkbox">
|
<div class="option-checkbox">
|
||||||
<input type="checkbox" {% if question['is_correct_' + frappe.utils.cstr(num)] %} checked {% endif %}>
|
<input type="checkbox" {% if question['is_correct_' + num] %} checked {% endif %}>
|
||||||
<label class="mb-0"> {{ _("Is Correct") }} </label>
|
<label class="mb-0"> {{ _("Is Correct") }} </label>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
|
||||||
|
<div class="mt-4">
|
||||||
|
<button class="btn btn-secondary btn-sm btn-question"> {{ _("New Question") }} </button>
|
||||||
|
<button class="btn btn-primary btn-sm btn-save-question ml-2
|
||||||
|
{% if not quiz.name %} hide {% endif %}"> {{ _("Save") }} </button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{% endmacro %}
|
{% endmacro %}
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ frappe.ready(() => {
|
|||||||
save_question(e);
|
save_question(e);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
get_questions()
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
@@ -56,7 +58,8 @@ const save_question = (e) => {
|
|||||||
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").text(),
|
"quiz_title": $("#quiz-title").text(),
|
||||||
"questions": get_questions()
|
"questions": get_questions(),
|
||||||
|
"quiz": $("#quiz-title").data("name") || ""
|
||||||
},
|
},
|
||||||
callback: (data) => {
|
callback: (data) => {
|
||||||
window.location.href = "/quizzes";
|
window.location.href = "/quizzes";
|
||||||
@@ -69,16 +72,17 @@ const get_questions = () => {
|
|||||||
let questions = [];
|
let questions = [];
|
||||||
|
|
||||||
$(".quiz-card").each((i, el) => {
|
$(".quiz-card").each((i, el) => {
|
||||||
|
|
||||||
if (!$(el).find(".question").text())
|
if (!$(el).find(".question").text())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
let details = {};
|
let details = {};
|
||||||
let one_correct_option = false;
|
let one_correct_option = false;
|
||||||
details["question"] = $(el).find(".question").text();
|
details["question"] = $(el).find(".question").text();
|
||||||
|
details["question_name"] = $(el).find(".question").data("question") || "";
|
||||||
|
|
||||||
Array.from({length: 4}, (x, i) => {
|
Array.from({length: 4}, (x, i) => {
|
||||||
let num = i + 1;
|
let num = i + 1;
|
||||||
|
|
||||||
details[`option_${num}`] = $(el).find(`.option-${num} .option-input:first`).text();
|
details[`option_${num}`] = $(el).find(`.option-${num} .option-input:first`).text();
|
||||||
details[`explanation_${num}`] = $(el).find(`.option-${num} .option-input:last`).text();
|
details[`explanation_${num}`] = $(el).find(`.option-${num} .option-input:last`).text();
|
||||||
|
|
||||||
@@ -89,12 +93,12 @@ const get_questions = () => {
|
|||||||
details[`is_correct_${num}`] = is_correct;
|
details[`is_correct_${num}`] = is_correct;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!details["option_1"] || !details["option_2"]) {
|
if (!details["option_1"] || !details["option_2"])
|
||||||
frappe.throw(__("Each question must have at least two options."))
|
frappe.throw(__("Each question must have at least two options."))
|
||||||
}
|
|
||||||
|
|
||||||
if (!one_correct_option)
|
if (!one_correct_option)
|
||||||
frappe.throw(__("Each question must have at least one correct option."))
|
frappe.throw(__("Each question must have at least one correct option."))
|
||||||
|
|
||||||
questions.push(details);
|
questions.push(details);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -8,13 +8,14 @@ def get_context(context):
|
|||||||
context.quiz = frappe._dict()
|
context.quiz = frappe._dict()
|
||||||
context.quiz.edit_mode = 1
|
context.quiz.edit_mode = 1
|
||||||
else:
|
else:
|
||||||
fields_arr = []
|
fields_arr = ["name","question"]
|
||||||
for num in range(1,5):
|
for num in range(1,5):
|
||||||
fields_arr.append("option_" + cstr(num))
|
fields_arr.append("option_" + cstr(num))
|
||||||
fields_arr.append("is_correct_" + cstr(num))
|
fields_arr.append("is_correct_" + cstr(num))
|
||||||
fields_arr.append("explanation_" + cstr(num))
|
fields_arr.append("explanation_" + cstr(num))
|
||||||
fields_arr.append("question")
|
|
||||||
context.quiz = frappe.db.get_value("LMS Quiz", quizname, ["title"], as_dict=1)
|
context.quiz = frappe.db.get_value("LMS Quiz", quizname, ["title", "name"], as_dict=1)
|
||||||
context.quiz.questions = frappe.get_all("LMS Quiz Question", {
|
context.quiz.questions = frappe.get_all("LMS Quiz Question", {
|
||||||
"parent": quizname
|
"parent": quizname
|
||||||
}, fields_arr)
|
}, fields_arr,
|
||||||
|
order_by="idx")
|
||||||
|
|||||||
@@ -17,13 +17,17 @@
|
|||||||
<div class="course-home-headings"> {{ _("Quiz List") }} </div>
|
<div class="course-home-headings"> {{ _("Quiz List") }} </div>
|
||||||
<div class="common-card-style">
|
<div class="common-card-style">
|
||||||
<table class="table">
|
<table class="table">
|
||||||
<tr>
|
<tr style="background-color: var(--fg-hover-color); font-weight: bold">
|
||||||
<td style="width: 5%;"> {{ _("No.") }} </td>
|
<td style="width: 10%;"> {{ _("No.") }} </td>
|
||||||
<td> {{ _("Quiz") }} </td>
|
<td style="width: 45%;"> {{ _("Title") }} </td>
|
||||||
|
<td> {{ _("ID") }} </td>
|
||||||
</tr>
|
</tr>
|
||||||
{% for quiz in quiz_list %}
|
{% for quiz in quiz_list %}
|
||||||
<tr style="position: relative; color: var(--text-color);">
|
<tr style="position: relative; color: var(--text-color);">
|
||||||
<td> {{ loop.index }} </td>
|
<td> {{ loop.index }} </td>
|
||||||
|
<td>
|
||||||
|
<a class="button-links" href="/quizzes/{{ quiz.name }}">{{ quiz.title }}</a>
|
||||||
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<a class="button-links" href="/quizzes/{{ quiz.name }}">{{ quiz.name }}</a>
|
<a class="button-links" href="/quizzes/{{ quiz.name }}">{{ quiz.name }}</a>
|
||||||
</td>
|
</td>
|
||||||
|
|||||||
Reference in New Issue
Block a user