From 31cbe3c0dc8686e29fd30af67323ce717fc78050 Mon Sep 17 00:00:00 2001 From: scmmishra Date: Mon, 15 Oct 2018 16:04:14 +0530 Subject: [PATCH] Added function to retrieve quiz data as JSON --- erpnext/education/utils.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/erpnext/education/utils.py b/erpnext/education/utils.py index d55bd1d316..809fa9e573 100644 --- a/erpnext/education/utils.py +++ b/erpnext/education/utils.py @@ -91,7 +91,7 @@ def add_activity(content_type, **kwargs): activity_does_not_exists, activity = check_entry_exists(kwargs.get('program')) if activity_does_not_exists: current_activity = frappe.get_doc({ - "doctype": "Student Course Activity", + "doctype": "Course Activity", "student_id": get_student_id(frappe.session.user), "program_name": kwargs.get('program'), "lms_activity": [{ @@ -131,13 +131,13 @@ def add_activity(content_type, **kwargs): def check_entry_exists(program): try: - activity_name = frappe.get_all("Student Course Activity", filters={"student_id": get_student_id(frappe.session.user), "program_name": program})[0] + activity_name = frappe.get_all("Course Activity", filters={"student_id": get_student_id(frappe.session.user), "program_name": program})[0] except IndexError: print("------ Got No Doc ------") return True, None else: print("------ Got A Doc ------") - return None, frappe.get_doc("Student Course Activity", activity_name) + return None, frappe.get_doc("Course Activity", activity_name) def get_student_id(email): """Returns Student ID, example EDU-STU-2018-00001 from email address @@ -147,4 +147,25 @@ def get_student_id(email): student = frappe.get_list('Student', filters={'student_email_id': email})[0].name return student except IndexError: - frappe.throw("Student Account with email:{0} does not exist".format(email)) \ No newline at end of file + frappe.throw("Student Account with email:{0} does not exist".format(email)) + +def get_quiz(content): + """Helper Function to get questions for a quiz + + :params content: name of a Content doctype with content_type quiz""" + try: + quiz_doc = frappe.get_doc("Content", content) + if quiz_doc.content_type == "Quiz": + import json + quiz = [frappe.get_doc("Question", item.question_link) for item in quiz_doc.questions] + data = [] + for question in quiz: + d = {} + d['Options'] = [{'option':item.option,'is_correct':item.is_correct} for item in quiz[0].options] + d['Question'] = question.question + data.append(d) + return json.dumps(data) + else: + frappe.throw("{0} is not a Quiz".format(content)) + except frappe.DoesNotExistError: + return None \ No newline at end of file