blob: 99462ceeee5a616376d1b1eb3a32d289ce967345 [file] [log] [blame]
Shivam Mishrac8c790a2019-05-21 12:04:50 +05301import frappe
2
Chillar Anand915b3432021-09-02 16:44:59 +05303import erpnext.education.utils as utils
4
Shivam Mishrac8c790a2019-05-21 12:04:50 +05305no_cache = 1
6
Ankush Menat494bd9e2022-03-28 18:52:46 +05307
Shivam Mishrac8c790a2019-05-21 12:04:50 +05308def get_context(context):
Shivam Mishrae94e9d22019-05-30 18:05:00 +05309 # Load Query Parameters
10 try:
Ankush Menat494bd9e2022-03-28 18:52:46 +053011 program = frappe.form_dict["program"]
12 content = frappe.form_dict["content"]
13 content_type = frappe.form_dict["type"]
14 course = frappe.form_dict["course"]
15 topic = frappe.form_dict["topic"]
Shivam Mishrae94e9d22019-05-30 18:05:00 +053016 except KeyError:
Ankush Menat494bd9e2022-03-28 18:52:46 +053017 frappe.local.flags.redirect_location = "/lms"
Shivam Mishrae94e9d22019-05-30 18:05:00 +053018 raise frappe.Redirect
Shivam Mishra4991fca2019-05-30 16:37:15 +053019
Shivam Mishrae94e9d22019-05-30 18:05:00 +053020 # Check if user has access to the content
Shivam Mishra4991fca2019-05-30 16:37:15 +053021 has_program_access = utils.allowed_program_access(program)
22 has_content_access = allowed_content_access(program, content, content_type)
23
24 if frappe.session.user == "Guest" or not has_program_access or not has_content_access:
Ankush Menat494bd9e2022-03-28 18:52:46 +053025 frappe.local.flags.redirect_location = "/lms"
Shivam Mishraae2871f2019-05-29 13:11:34 +053026 raise frappe.Redirect
27
Shivam Mishrae94e9d22019-05-30 18:05:00 +053028 # Set context for content to be displayer
Shivam Mishrad1a25212019-06-03 12:57:38 +053029 context.content = frappe.get_doc(content_type, content).as_dict()
Shivam Mishra4991fca2019-05-30 16:37:15 +053030 context.content_type = content_type
Shivam Mishrae94e9d22019-05-30 18:05:00 +053031 context.program = program
32 context.course = course
33 context.topic = topic
Shivam Mishra4991fca2019-05-30 16:37:15 +053034
Shivam Mishrac8c790a2019-05-21 12:04:50 +053035 topic = frappe.get_doc("Topic", topic)
Ankush Menat494bd9e2022-03-28 18:52:46 +053036 content_list = [
37 {"content_type": item.content_type, "content": item.content} for item in topic.topic_content
38 ]
Shivam Mishrae94e9d22019-05-30 18:05:00 +053039
40 # Set context for progress numbers
Ankush Menat494bd9e2022-03-28 18:52:46 +053041 context.position = content_list.index({"content": content, "content_type": content_type})
Shivam Mishrae94e9d22019-05-30 18:05:00 +053042 context.length = len(content_list)
43
44 # Set context for navigation
45 context.previous = get_previous_content(content_list, context.position)
46 context.next = get_next_content(content_list, context.position)
47
Ankush Menat494bd9e2022-03-28 18:52:46 +053048
Shivam Mishrae94e9d22019-05-30 18:05:00 +053049def get_next_content(content_list, current_index):
Shivam Mishrac8c790a2019-05-21 12:04:50 +053050 try:
51 return content_list[current_index + 1]
52 except IndexError:
53 return None
54
Ankush Menat494bd9e2022-03-28 18:52:46 +053055
Shivam Mishrae94e9d22019-05-30 18:05:00 +053056def get_previous_content(content_list, current_index):
Shivam Mishrac8c790a2019-05-21 12:04:50 +053057 if current_index == 0:
58 return None
59 else:
Shivam Mishra4991fca2019-05-30 16:37:15 +053060 return content_list[current_index - 1]
61
Ankush Menat494bd9e2022-03-28 18:52:46 +053062
Shivam Mishra4991fca2019-05-30 16:37:15 +053063def allowed_content_access(program, content, content_type):
Ankush Menat494bd9e2022-03-28 18:52:46 +053064 contents_of_program = frappe.db.sql(
65 """select `tabTopic Content`.content, `tabTopic Content`.content_type
Shivam Mishrad49b5e42019-06-06 17:19:53 +053066 from `tabCourse Topic`,
67 `tabProgram Course`,
68 `tabTopic Content`
69 where `tabCourse Topic`.parent = `tabProgram Course`.course
70 and `tabTopic Content`.parent = `tabCourse Topic`.topic
Ankush Menat494bd9e2022-03-28 18:52:46 +053071 and `tabProgram Course`.parent = %(program)s""",
72 {"program": program},
73 )
Shivam Mishra4991fca2019-05-30 16:37:15 +053074
Ankush Menat4551d7d2021-08-19 13:41:10 +053075 return (content, content_type) in contents_of_program