From 4991fca5ccc81f130b93bf6669e41ccd50313e4a Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Thu, 30 May 2019 16:37:15 +0530 Subject: [PATCH] feat: enabled per student access --- erpnext/www/lms/content.py | 36 +++++++++++++++++++++++++++++++----- erpnext/www/lms/course.py | 4 +++- erpnext/www/lms/program.py | 4 ++-- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/erpnext/www/lms/content.py b/erpnext/www/lms/content.py index 78b02e2778..67e3508df5 100644 --- a/erpnext/www/lms/content.py +++ b/erpnext/www/lms/content.py @@ -5,19 +5,27 @@ import frappe no_cache = 1 def get_context(context): - if frappe.session.user == "Guest": + program = frappe.form_dict['program'] + content = frappe.form_dict['content'] + content_type = frappe.form_dict['type'] + + has_program_access = utils.allowed_program_access(program) + has_content_access = allowed_content_access(program, content, content_type) + + if frappe.session.user == "Guest" or not has_program_access or not has_content_access: frappe.local.flags.redirect_location = '/lms' raise frappe.Redirect + context.content = frappe.get_doc(content_type, content).as_dict() + context.content_type = content_type + context.course = frappe.form_dict['course'] context.topic = frappe.form_dict['topic'] - content = frappe.form_dict['content'] - context.content_type = frappe.form_dict['type'] - context.content = frappe.get_doc(context.content_type, content).as_dict() context.previous = get_previous_content(context.topic, context.course, context.content, context.content_type) context.next = get_next_content(context.topic, context.course, context.content, context.content_type) + def get_next_content(topic, course, content, content_type): if frappe.session.user == "Guest": return None @@ -38,4 +46,22 @@ def get_previous_content(topic, course, content, content_type): if current_index == 0: return None else: - return content_list[current_index - 1] \ No newline at end of file + return content_list[current_index - 1] + +def allowed_content_access(program, content, content_type): + # Get all content in program + + # Using ORM + # course_in_program = [course.course for course in frappe.get_all('Program Course', fields=['course'], filters={'parent': program})] + # topics_in_course = [topic.topic for topic in frappe.get_all("Course Topic", fields=['topic'], filters=[['parent','in', course_in_program]])] + # contents_of_program = [[c.content, c.content_type] for c in frappe.get_all('Topic Content', fields=['content', 'content_type'], filters=[['parent','in', topics_in_course]])] + + contents_of_program = frappe.db.sql("""select `tabtopic content`.content, `tabtopic content`.content_type + from `tabcourse topic`, + `tabprogram course`, + `tabtopic content` + where `tabcourse topic`.parent = `tabprogram course`.course + and `tabtopic content`.parent = `tabcourse topic`.topic + and `tabprogram course`.parent = '{0}'""".format(program)) + + return (content, content_type) in contents_of_program \ No newline at end of file diff --git a/erpnext/www/lms/course.py b/erpnext/www/lms/course.py index d8670e6977..b9aff5c5bd 100644 --- a/erpnext/www/lms/course.py +++ b/erpnext/www/lms/course.py @@ -7,5 +7,7 @@ no_cache = 1 def get_context(context): context.education_settings = frappe.get_single("Education Settings") course = frappe.get_doc('Course', frappe.form_dict['name']) + context.program = frappe.form_dict['program'] context.course = course - context.topics = course.get_topics() \ No newline at end of file + context.topics = course.get_topics() + context.has_access = utils.allowed_program_access(context.program) \ No newline at end of file diff --git a/erpnext/www/lms/program.py b/erpnext/www/lms/program.py index 827b11a4ab..4c3a3fdf66 100644 --- a/erpnext/www/lms/program.py +++ b/erpnext/www/lms/program.py @@ -6,8 +6,8 @@ no_cache = 1 def get_context(context): context.education_settings = frappe.get_single("Education Settings") - context.program = get_program(frappe.form_dict['name']) - context.is_enrolled = utils.get_enrollment_status(program) + context.program = get_program(frappe.form_dict['program']) + context.has_access = utils.allowed_program_access(frappe.form_dict['program']) def get_program(program_name): try: