From 94b3ccd3d9952d2261368a6c517a4ff399ce3043 Mon Sep 17 00:00:00 2001 From: Anand Chitipothu Date: Thu, 1 Jul 2021 17:29:02 +0530 Subject: [PATCH] feat: added a utililty to switch a student from one batch to another --- community/lms/doctype/lms_batch/lms_batch.py | 35 ++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/community/lms/doctype/lms_batch/lms_batch.py b/community/lms/doctype/lms_batch/lms_batch.py index 8ed2b990..6b870ff4 100644 --- a/community/lms/doctype/lms_batch/lms_batch.py +++ b/community/lms/doctype/lms_batch/lms_batch.py @@ -71,3 +71,38 @@ def save_message(message, batch): "message": message }) doc.save(ignore_permissions=True) + +def switch_batch(course_name, email, batch_name): + """Switches the user from the current batch of the course to a new batch. + """ + membership = frappe.get_last_doc( + "LMS Batch Membership", + filters={"course": course_name, "member": email}) + + batch = frappe.get_doc("LMS Batch", batch_name) + if not batch: + raise ValueError(f"Invalid Batch: {batch_name}") + + if batch.course != course_name: + raise ValueError("Can not switch batches across courses") + + if batch.is_member(email): + print(f"{email} is already a member of {batch.title}") + return + + old_batch = frappe.get_doc("LMS Batch", membership.batch) + + print("updating membership", membership.name) + membership.batch = batch_name + membership.save() + + # update exercise submissions + filters = { + "owner": email, + "batch": old_batch.name + } + for name in frappe.db.get_all("Exercise Submission", filters=filters, pluck='name'): + doc = frappe.get_doc("Exercise Submission", name) + print("updating exercise submission", name) + doc.batch = batch_name + doc.save()