feat: badges

This commit is contained in:
Jannat Patel
2024-04-30 19:25:07 +05:30
parent 0f64da69c0
commit 7355be2a8b
16 changed files with 453 additions and 2 deletions

View File

@@ -97,6 +97,11 @@ override_doctype_class = {
# Hook on document methods and events
doc_events = {
"*": {
"on_change": [
"lms.lms.doctype.lms_badge.lms_badge.process_badges",
]
},
"Discussion Reply": {"after_insert": "lms.lms.utils.handle_notifications"},
}

View File

@@ -359,3 +359,18 @@ def get_certified_participants():
participant_details.append(details)
return participant_details
@frappe.whitelist()
def get_assigned_badges(member):
assigned_badges = frappe.get_all(
"LMS Badge Assignment",
{"member": member},
["badge"],
as_dict=1,
)
for badge in assigned_badges:
badge.update(
frappe.db.get_value("LMS Badge", badge.badge, ["name", "title", "image"])
)

View File

View File

@@ -0,0 +1,8 @@
// Copyright (c) 2024, Frappe and contributors
// For license information, please see license.txt
// frappe.ui.form.on("LMS Badge", {
// refresh(frm) {
// },
// });

View File

@@ -0,0 +1,113 @@
{
"actions": [],
"allow_rename": 1,
"autoname": "field:title",
"creation": "2024-04-30 11:29:53.548647",
"doctype": "DocType",
"engine": "InnoDB",
"field_order": [
"enabled",
"title",
"description",
"image",
"reference_doctype",
"column_break_wgum",
"grant_only_once",
"event",
"field_to_check",
"condition"
],
"fields": [
{
"fieldname": "title",
"fieldtype": "Data",
"in_list_view": 1,
"label": "Title",
"reqd": 1,
"unique": 1
},
{
"fieldname": "image",
"fieldtype": "Attach Image",
"label": "Image",
"reqd": 1
},
{
"fieldname": "column_break_wgum",
"fieldtype": "Column Break"
},
{
"fieldname": "reference_doctype",
"fieldtype": "Link",
"in_list_view": 1,
"in_standard_filter": 1,
"label": "Reference Document Type",
"options": "DocType",
"reqd": 1
},
{
"fieldname": "event",
"fieldtype": "Select",
"in_list_view": 1,
"label": "Event",
"options": "New\nValue Change",
"reqd": 1
},
{
"fieldname": "condition",
"fieldtype": "Code",
"label": "Condition"
},
{
"depends_on": "eval:doc.event == 'Value Change'",
"fieldname": "field_to_check",
"fieldtype": "Select",
"label": "Field To Check"
},
{
"default": "0",
"fieldname": "grant_only_once",
"fieldtype": "Check",
"label": "Grant only once"
},
{
"default": "0",
"fieldname": "enabled",
"fieldtype": "Check",
"label": "Enabled",
"options": "1"
},
{
"fieldname": "description",
"fieldtype": "Small Text",
"label": "Description",
"reqd": 1
}
],
"index_web_pages_for_search": 1,
"links": [],
"modified": "2024-04-30 17:16:17.725459",
"modified_by": "Administrator",
"module": "LMS",
"name": "LMS Badge",
"naming_rule": "By fieldname",
"owner": "Administrator",
"permissions": [
{
"create": 1,
"delete": 1,
"email": 1,
"export": 1,
"print": 1,
"read": 1,
"report": 1,
"role": "System Manager",
"share": 1,
"write": 1
}
],
"sort_field": "creation",
"sort_order": "DESC",
"states": [],
"title_field": "title"
}

View File

@@ -0,0 +1,72 @@
# Copyright (c) 2024, Frappe and contributors
# For license information, please see license.txt
import frappe
from frappe.model.document import Document
class LMSBadge(Document):
def apply(self, doc):
if self.rule_condition_satisfied(doc):
print("rule satisfied")
self.award(doc)
def rule_condition_satisfied(self, doc):
doc_before_save = doc.get_doc_before_save()
if self.event == "New" and doc_before_save != None:
return False
print("its new")
if self.event == "Value Change":
field_to_check = self.field_to_check
if not self.field_to_check:
return False
if doc_before_save and doc_before_save.get(field_to_check) == doc.get(
field_to_check
):
return False
if self.condition:
print("found condition")
print(self.eval_condition(doc))
return self.eval_condition(doc)
return False
def award(self, doc):
if self.grant_only_once:
if frappe.db.exists(
"LMS Badge Assignment",
{"badge": self.name, "user": frappe.session.user},
):
return
assignment = frappe.new_doc("LMS Badge Assignment")
assignment.update(
{
"badge": self.name,
"user": frappe.session.user,
"issued_on": frappe.utils.now(),
}
)
assignment.save()
def eval_condition(self, doc):
return self.condition and frappe.safe_eval(
self.condition, None, {"doc": doc.as_dict()}
)
def process_badges(doc, state):
if (
frappe.flags.in_patch
or frappe.flags.in_install
or frappe.flags.in_migrate
or frappe.flags.in_import
or frappe.flags.in_setup_wizard
):
return
for d in frappe.cache_manager.get_doctype_map(
"LMS Badge", doc.doctype, dict(reference_doctype=doc.doctype, enabled=1)
):
frappe.get_doc("LMS Badge", d.get("name")).apply(doc)

View File

@@ -0,0 +1,9 @@
# Copyright (c) 2024, Frappe and Contributors
# See license.txt
# import frappe
from frappe.tests.utils import FrappeTestCase
class TestLMSBadge(FrappeTestCase):
pass

View File

@@ -0,0 +1,14 @@
// Copyright (c) 2024, Frappe and contributors
// For license information, please see license.txt
frappe.ui.form.on("LMS Badge Assignment", {
refresh(frm) {
frm.set_query("member", function (doc) {
return {
filters: {
ignore_user_type: 1,
},
};
});
},
});

View File

@@ -0,0 +1,113 @@
{
"actions": [],
"allow_rename": 1,
"creation": "2024-04-30 11:58:44.096879",
"doctype": "DocType",
"engine": "InnoDB",
"field_order": [
"member",
"issued_on",
"column_break_ugix",
"badge",
"badge_image",
"badge_description"
],
"fields": [
{
"fieldname": "member",
"fieldtype": "Link",
"in_list_view": 1,
"label": "Member",
"options": "User",
"reqd": 1
},
{
"fieldname": "badge",
"fieldtype": "Link",
"in_list_view": 1,
"in_standard_filter": 1,
"label": "Badge",
"options": "LMS Badge",
"reqd": 1
},
{
"fieldname": "issued_on",
"fieldtype": "Date",
"in_list_view": 1,
"label": "Issued On",
"options": "Today",
"reqd": 1
},
{
"fetch_from": "badge.image",
"fieldname": "badge_image",
"fieldtype": "Attach",
"label": "Badge Image",
"read_only": 1,
"reqd": 1
},
{
"fieldname": "column_break_ugix",
"fieldtype": "Column Break"
},
{
"fetch_from": "badge.description",
"fieldname": "badge_description",
"fieldtype": "Small Text",
"label": "Badge Description",
"read_only": 1,
"reqd": 1
}
],
"index_web_pages_for_search": 1,
"links": [],
"modified": "2024-04-30 17:19:39.554248",
"modified_by": "Administrator",
"module": "LMS",
"name": "LMS Badge Assignment",
"owner": "Administrator",
"permissions": [
{
"create": 1,
"delete": 1,
"email": 1,
"export": 1,
"print": 1,
"read": 1,
"report": 1,
"role": "System Manager",
"share": 1,
"write": 1
},
{
"create": 1,
"delete": 1,
"email": 1,
"export": 1,
"print": 1,
"read": 1,
"report": 1,
"role": "Moderator",
"share": 1,
"write": 1
},
{
"create": 1,
"delete": 1,
"email": 1,
"export": 1,
"if_owner": 1,
"print": 1,
"read": 1,
"report": 1,
"role": "LMS Student",
"share": 1,
"write": 1
}
],
"show_title_field_in_link": 1,
"sort_field": "creation",
"sort_order": "DESC",
"states": [],
"title_field": "member"
}

View File

@@ -0,0 +1,9 @@
# Copyright (c) 2024, Frappe and contributors
# For license information, please see license.txt
# import frappe
from frappe.model.document import Document
class LMSBadgeAssignment(Document):
pass

View File

@@ -0,0 +1,9 @@
# Copyright (c) 2024, Frappe and Contributors
# See license.txt
# import frappe
from frappe.tests.utils import FrappeTestCase
class TestLMSBadgeAssignment(FrappeTestCase):
pass