blob: 97f59180d96686125613a4bf7cda6950f3e44b07 [file] [log] [blame]
Shivam Mishrac8c790a2019-05-21 12:04:50 +05301from __future__ import unicode_literals
Chillar Anand915b3432021-09-02 16:44:59 +05302
Shivam Mishrac8c790a2019-05-21 12:04:50 +05303import frappe
4
Chillar Anand915b3432021-09-02 16:44:59 +05305import erpnext.education.utils as utils
6
Shivam Mishrac8c790a2019-05-21 12:04:50 +05307no_cache = 1
8
9def get_context(context):
Shivam Mishrae94e9d22019-05-30 18:05:00 +053010 # Load Query Parameters
11 try:
12 program = frappe.form_dict['program']
13 content = frappe.form_dict['content']
14 content_type = frappe.form_dict['type']
15 course = frappe.form_dict['course']
16 topic = frappe.form_dict['topic']
17 except KeyError:
18 frappe.local.flags.redirect_location = '/lms'
19 raise frappe.Redirect
Shivam Mishra4991fca2019-05-30 16:37:15 +053020
Shivam Mishrae94e9d22019-05-30 18:05:00 +053021
22 # Check if user has access to the content
Shivam Mishra4991fca2019-05-30 16:37:15 +053023 has_program_access = utils.allowed_program_access(program)
24 has_content_access = allowed_content_access(program, content, content_type)
25
26 if frappe.session.user == "Guest" or not has_program_access or not has_content_access:
Shivam Mishraae2871f2019-05-29 13:11:34 +053027 frappe.local.flags.redirect_location = '/lms'
28 raise frappe.Redirect
29
Shivam Mishrae94e9d22019-05-30 18:05:00 +053030
31 # Set context for content to be displayer
Shivam Mishrad1a25212019-06-03 12:57:38 +053032 context.content = frappe.get_doc(content_type, content).as_dict()
Shivam Mishra4991fca2019-05-30 16:37:15 +053033 context.content_type = content_type
Shivam Mishrae94e9d22019-05-30 18:05:00 +053034 context.program = program
35 context.course = course
36 context.topic = topic
Shivam Mishra4991fca2019-05-30 16:37:15 +053037
Shivam Mishrac8c790a2019-05-21 12:04:50 +053038 topic = frappe.get_doc("Topic", topic)
Shivam Mishrad49b5e42019-06-06 17:19:53 +053039 content_list = [{'content_type':item.content_type, 'content':item.content} for item in topic.topic_content]
Shivam Mishrae94e9d22019-05-30 18:05:00 +053040
41 # Set context for progress numbers
42 context.position = content_list.index({'content': content, 'content_type': content_type})
43 context.length = len(content_list)
44
45 # Set context for navigation
46 context.previous = get_previous_content(content_list, context.position)
47 context.next = get_next_content(content_list, context.position)
48
49def 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
Shivam Mishrae94e9d22019-05-30 18:05:00 +053055def get_previous_content(content_list, current_index):
Shivam Mishrac8c790a2019-05-21 12:04:50 +053056 if current_index == 0:
57 return None
58 else:
Shivam Mishra4991fca2019-05-30 16:37:15 +053059 return content_list[current_index - 1]
60
61def allowed_content_access(program, content, content_type):
Shivam Mishrad49b5e42019-06-06 17:19:53 +053062 contents_of_program = frappe.db.sql("""select `tabTopic Content`.content, `tabTopic Content`.content_type
63 from `tabCourse Topic`,
64 `tabProgram Course`,
65 `tabTopic Content`
66 where `tabCourse Topic`.parent = `tabProgram Course`.course
67 and `tabTopic Content`.parent = `tabCourse Topic`.topic
68 and `tabProgram Course`.parent = %(program)s""", {'program': program})
Shivam Mishra4991fca2019-05-30 16:37:15 +053069
Ankush Menat4551d7d2021-08-19 13:41:10 +053070 return (content, content_type) in contents_of_program