feat: update quiz

This commit is contained in:
Jannat Patel
2022-08-19 18:22:43 +05:30
parent 7397bf389e
commit 5aba690318
9 changed files with 92 additions and 61 deletions

View File

@@ -29,7 +29,7 @@ class CourseLesson(Document):
e = frappe.get_doc(doctype_map[section], name)
e.lesson = self.name
e.index_ = index
e.save()
e.save(ignore_permissions=True)
index += 1
self.update_orphan_documents(doctype_map[section], documents)

View File

@@ -5,7 +5,7 @@ from __future__ import unicode_literals
import frappe
from frappe.model.document import Document
import json
from ...utils import slugify
from ...utils import generate_slug
from frappe.utils import flt, cint
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.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):
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):
return f"<Course#{self.name}>"

View File

@@ -2,7 +2,6 @@
"actions": [],
"allow_import": 1,
"allow_rename": 1,
"autoname": "field:title",
"creation": "2021-06-07 10:50:17.893625",
"doctype": "DocType",
"editable_grid": 1,
@@ -18,7 +17,9 @@
{
"fieldname": "title",
"fieldtype": "Data",
"in_list_view": 1,
"label": "Title",
"reqd": 1,
"unique": 1
},
{
@@ -49,11 +50,10 @@
],
"index_web_pages_for_search": 1,
"links": [],
"modified": "2022-05-16 14:47:55.364743",
"modified": "2022-08-19 17:54:41.685182",
"modified_by": "Administrator",
"module": "LMS",
"name": "LMS Quiz",
"naming_rule": "By fieldname",
"owner": "Administrator",
"permissions": [
{

View File

@@ -6,8 +6,15 @@ from frappe.model.document import Document
import json
from frappe import _
from frappe.utils import cstr
from lms.lms.utils import generate_slug
class LMSQuiz(Document):
def autoname(self):
if not self.name:
self.name = generate_slug(self.title, "LMS Quiz")
def validate(self):
self.validate_correct_answers()
@@ -20,7 +27,7 @@ class LMSQuiz(Document):
question.multiple = 1
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):
@@ -81,28 +88,42 @@ def quiz_summary(quiz, results):
@frappe.whitelist()
def save_quiz(quiz_title, questions):
doc = frappe.get_doc({
"doctype": "LMS Quiz",
def save_quiz(quiz_title, questions, quiz):
if quiz:
doc = frappe.get_doc("LMS Quiz", quiz)
else:
doc = frappe.get_doc({
"doctype": "LMS Quiz",
})
doc.update({
"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
}
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",
"parent": doc.name,
"parenttype": "LMS Quiz",
"parentfield": "questions",
"idx": index + 1
})
question_doc.update({
"question": row["question"]
})
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.update({
"option_" + cstr(num): row["option_" + 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)
return doc.name

View File

@@ -33,6 +33,15 @@ def slugify(title, used_slugs=[]):
return new_slug
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):
filters = {
"member": member,

View File

@@ -21,7 +21,7 @@
{% macro BreadCrumb(quiz) %}
<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">
<span class="breadcrumb-destination">{{ quiz.title if quiz.title else _("New Quiz") }}</span>
</div>
@@ -32,42 +32,45 @@
<div style="width: 60%;">
<div class="course-home-headings mb-2" data-placeholder="{{ _('Quiz Title') }}" id="quiz-title"
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.name %} data-name="{{ quiz.name }}" {% endif %}
contenteditable="true" >{% if quiz.title %}{{ quiz.title }}{% endif %}</div>
{% if quiz.question %}
{% if quiz.questions %}
{% for question in quiz.questions %}
<div class="quiz-card">
<div contenteditable="true" data-placeholder="{{ _('Question') }}"
class="mb-4">{% if question.question %} {{ question.question }} {% endif %}</div>
<div contenteditable="true" data-placeholder="{{ _('Question') }}" data-question="{{ question.name }}"
class="question mb-4">{% if question.question %} {{ question.question }} {% endif %}</div>
{% for num in range(1,5) %}
{% set option = question["option_" + frappe.utils.cstr(num)] %}
{% set explanation = question["explanation_" + frappe.utils.cstr(num)] %}
{% for i in range(1,5) %}
{% set num = frappe.utils.cstr(i) %}
{% set option = question["option_" + num] %}
{% set explanation = question["explanation_" + num] %}
<div class="mt-4">
<label class=""> {{ _("Option") }} {{ frappe.utils.cstr(num) }} </label>
<div class="d-flex justify-content-between">
<label class=""> {{ _("Option") }} {{ num }} </label>
<div class="d-flex justify-content-between option-{{ num }}">
<div contenteditable="true" data-placeholder="{{ _('Option') }}"
class="option-input">{% if option %}{{ option }}{% endif %}</div>
<div contenteditable="true" data-placeholder="{{ _('Explanation') }}"
class="option-input">{% if explanation %}{{ explanation }}{% endif %}</div>
<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>
</div>
</div>
</div>
{% endfor %}
</div>
{% endfor %}
{% 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>
{% endmacro %}

View File

@@ -8,6 +8,8 @@ frappe.ready(() => {
save_question(e);
});
get_questions()
});
@@ -56,7 +58,8 @@ const save_question = (e) => {
method: "lms.lms.doctype.lms_quiz.lms_quiz.save_quiz",
args: {
"quiz_title": $("#quiz-title").text(),
"questions": get_questions()
"questions": get_questions(),
"quiz": $("#quiz-title").data("name") || ""
},
callback: (data) => {
window.location.href = "/quizzes";
@@ -69,16 +72,17 @@ const get_questions = () => {
let questions = [];
$(".quiz-card").each((i, el) => {
if (!$(el).find(".question").text())
return;
let details = {};
let one_correct_option = false;
details["question"] = $(el).find(".question").text();
details["question_name"] = $(el).find(".question").data("question") || "";
Array.from({length: 4}, (x, i) => {
let num = i + 1;
details[`option_${num}`] = $(el).find(`.option-${num} .option-input:first`).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;
});
if (!details["option_1"] || !details["option_2"]) {
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);
});

View File

@@ -8,13 +8,14 @@ def get_context(context):
context.quiz = frappe._dict()
context.quiz.edit_mode = 1
else:
fields_arr = []
fields_arr = ["name","question"]
for num in range(1,5):
fields_arr.append("option_" + cstr(num))
fields_arr.append("is_correct_" + 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", {
"parent": quizname
}, fields_arr)
}, fields_arr,
order_by="idx")

View File

@@ -17,13 +17,17 @@
<div class="course-home-headings"> {{ _("Quiz List") }} </div>
<div class="common-card-style">
<table class="table">
<tr>
<td style="width: 5%;"> {{ _("No.") }} </td>
<td> {{ _("Quiz") }} </td>
<tr style="background-color: var(--fg-hover-color); font-weight: bold">
<td style="width: 10%;"> {{ _("No.") }} </td>
<td style="width: 45%;"> {{ _("Title") }} </td>
<td> {{ _("ID") }} </td>
</tr>
{% for quiz in quiz_list %}
<tr style="position: relative; color: var(--text-color);">
<td> {{ loop.index }} </td>
<td>
<a class="button-links" href="/quizzes/{{ quiz.name }}">{{ quiz.title }}</a>
</td>
<td>
<a class="button-links" href="/quizzes/{{ quiz.name }}">{{ quiz.name }}</a>
</td>