From 764f35870803a5645299714dd22eeb75cb688f83 Mon Sep 17 00:00:00 2001 From: Jannat Patel Date: Fri, 28 Jun 2024 12:21:07 +0530 Subject: [PATCH] feat: limit questions in quiz --- frontend/src/components/Quiz.vue | 3 +++ lms/hooks.py | 19 --------------- lms/lms/doctype/lms_quiz/lms_quiz.json | 22 ++++++++--------- lms/lms/doctype/lms_quiz/lms_quiz.py | 24 +++++++++++++++++-- .../doctype/lms_settings/lms_settings.json | 10 +------- lms/lms/utils.py | 4 ---- lms/overrides/user.py | 14 ----------- lms/plugins.py | 11 --------- 8 files changed, 36 insertions(+), 71 deletions(-) diff --git a/frontend/src/components/Quiz.vue b/frontend/src/components/Quiz.vue index 5774a8f9..cec001b1 100644 --- a/frontend/src/components/Quiz.vue +++ b/frontend/src/components/Quiz.vue @@ -270,6 +270,9 @@ const quiz = createResource({ if (data.shuffle_questions) { data.questions = data.questions.sort(() => Math.random() - 0.5) } + if (data.limit_questions_to) { + data.questions = data.questions.slice(0, data.limit_questions_to) + } }, }) diff --git a/lms/hooks.py b/lms/hooks.py index f7a30b5e..ea9ff348 100644 --- a/lms/hooks.py +++ b/lms/hooks.py @@ -242,25 +242,6 @@ has_website_permission = { "LMS Certificate": "lms.lms.doctype.lms_certificate.lms_certificate.has_website_permission", } -profile_mandatory_fields = [ - "first_name", - "last_name", - "user_image", - "bio", - "linkedin", - "education", - "skill", - "preferred_functions", - "preferred_industries", - "dream_companies", - "attire", - "collaboration", - "role", - "location_preference", - "time", - "company_type", -] - ## Markdown Macros for Lessons lms_markdown_macro_renderers = { "Exercise": "lms.plugins.exercise_renderer", diff --git a/lms/lms/doctype/lms_quiz/lms_quiz.json b/lms/lms/doctype/lms_quiz/lms_quiz.json index d93a089a..9c1b8e95 100644 --- a/lms/lms/doctype/lms_quiz/lms_quiz.json +++ b/lms/lms/doctype/lms_quiz/lms_quiz.json @@ -8,10 +8,11 @@ "engine": "InnoDB", "field_order": [ "title", - "passing_percentage", - "column_break_gaac", "max_attempts", + "limit_questions_to", + "column_break_gaac", "total_marks", + "passing_percentage", "section_break_hsiv", "show_answers", "column_break_rocd", @@ -23,8 +24,7 @@ "section_break_3", "lesson", "column_break_5", - "course", - "time" + "course" ], "fields": [ { @@ -54,13 +54,6 @@ "fieldtype": "Int", "label": "Max Attempts" }, - { - "default": "0", - "fieldname": "time", - "fieldtype": "Int", - "hidden": 1, - "label": "Time Per Question (in Seconds)" - }, { "fetch_from": "lesson.course", "fieldname": "course", @@ -131,11 +124,16 @@ "fieldname": "shuffle_questions", "fieldtype": "Check", "label": "Shuffle Questions" + }, + { + "fieldname": "limit_questions_to", + "fieldtype": "Int", + "label": "Limit Questions To" } ], "index_web_pages_for_search": 1, "links": [], - "modified": "2024-04-24 12:37:20.578041", + "modified": "2024-06-27 22:03:48.576489", "modified_by": "Administrator", "module": "LMS", "name": "LMS Quiz", diff --git a/lms/lms/doctype/lms_quiz/lms_quiz.py b/lms/lms/doctype/lms_quiz/lms_quiz.py index ebebd0ce..ef7f6243 100644 --- a/lms/lms/doctype/lms_quiz/lms_quiz.py +++ b/lms/lms/doctype/lms_quiz/lms_quiz.py @@ -18,7 +18,8 @@ from lms.lms.utils import ( class LMSQuiz(Document): def validate(self): self.validate_duplicate_questions() - self.total_marks = set_total_marks(self.name, self.questions) + self.validate_limit() + self.calculate_total_marks() def validate_duplicate_questions(self): questions = [row.question for row in self.questions] @@ -28,6 +29,25 @@ class LMSQuiz(Document): _("Rows {0} have the duplicate questions.").format(frappe.bold(comma_and(rows))) ) + def validate_limit(self): + if self.limit_questions_to and self.limit_questions_to >= len(self.questions): + frappe.throw( + _("Limit cannot be greater than or equal to the number of questions in the quiz.") + ) + + if self.limit_questions_to and self.limit_questions_to < len(self.questions): + marks = [question.marks for question in self.questions] + if len(set(marks)) > 1: + frappe.throw(_("All questions should have the same marks if the limit is set.")) + + def calculate_total_marks(self): + if self.limit_questions_to: + self.total_marks = sum( + question.marks for question in self.questions[: self.limit_questions_to] + ) + else: + self.total_marks = sum(question.marks for question in self.questions) + def autoname(self): if not self.name: self.name = generate_slug(self.title, "LMS Quiz") @@ -50,7 +70,7 @@ class LMSQuiz(Document): return result[0] -def set_total_marks(quiz, questions): +def set_total_marks(questions): marks = 0 for question in questions: marks += question.get("marks") diff --git a/lms/lms/doctype/lms_settings/lms_settings.json b/lms/lms/doctype/lms_settings/lms_settings.json index 1f954b66..5d0af7a3 100644 --- a/lms/lms/doctype/lms_settings/lms_settings.json +++ b/lms/lms/doctype/lms_settings/lms_settings.json @@ -6,7 +6,6 @@ "engine": "InnoDB", "field_order": [ "default_home", - "force_profile_completion", "is_onboarding_complete", "column_break_zdel", "unsplash_access_key", @@ -104,13 +103,6 @@ "fieldtype": "Data", "label": "Course List Search Bar Placeholder" }, - { - "default": "0", - "fieldname": "force_profile_completion", - "fieldtype": "Check", - "hidden": 1, - "label": "Force users to complete their Profile" - }, { "default": "0", "fieldname": "terms_of_use", @@ -431,7 +423,7 @@ "index_web_pages_for_search": 1, "issingle": 1, "links": [], - "modified": "2024-05-31 20:17:07.362088", + "modified": "2024-06-27 21:57:02.193336", "modified_by": "Administrator", "module": "LMS", "name": "LMS Settings", diff --git a/lms/lms/utils.py b/lms/lms/utils.py index 2ac801db..066f1133 100644 --- a/lms/lms/utils.py +++ b/lms/lms/utils.py @@ -776,10 +776,6 @@ def get_lesson_count(course): return lesson_count -def check_profile_restriction(): - return frappe.db.get_single_value("LMS Settings", "force_profile_completion") - - def get_restriction_details(): user = frappe.db.get_value( "User", frappe.session.user, ["profile_complete", "username"], as_dict=True diff --git a/lms/overrides/user.py b/lms/overrides/user.py index f667c18d..b7659493 100644 --- a/lms/overrides/user.py +++ b/lms/overrides/user.py @@ -15,7 +15,6 @@ class CustomUser(User): def validate(self): super().validate() self.validate_username_duplicates() - self.validate_completion() def validate_username_duplicates(self): while not self.username or self.username_exists(): @@ -38,19 +37,6 @@ class CustomUser(User): else: frappe.throw(_("Skills must be unique")) - def validate_completion(self): - if frappe.db.get_single_value("LMS Settings", "force_profile_completion"): - all_fields_have_value = True - profile_mandatory_fields = frappe.get_hooks("profile_mandatory_fields") - docfields = frappe.get_meta(self.doctype).fields - - for field in profile_mandatory_fields: - if not self.get(field): - all_fields_have_value = False - break - - self.profile_complete = all_fields_have_value - def get_batch_count(self) -> int: """Returns the number of batches authored by this user.""" return frappe.db.count( diff --git a/lms/plugins.py b/lms/plugins.py index 76278ce3..852d5587 100644 --- a/lms/plugins.py +++ b/lms/plugins.py @@ -91,17 +91,6 @@ class LiveCodeExtension(PageExtension): return frappe.render_template("templates/livecode/extension_footer.html", context) -def set_mandatory_fields_for_profile(): - profile_form = frappe.get_doc("Web Form", "profile") - profile_mandatory_fields = frappe.get_hooks("profile_mandatory_fields") - for field in profile_form.web_form_fields: - field.reqd = 0 - if field.fieldname in profile_mandatory_fields: - field.reqd = 1 - - profile_form.save() - - def quiz_renderer(quiz_name): if frappe.session.user == "Guest": return "
" + _(