blob: 0c0484536259b9968396e8214fd6cae8c5319061 [file] [log] [blame]
Shivam Mishrac8c790a2019-05-21 12:04:50 +05301from __future__ import unicode_literals
2import erpnext.education.utils as utils
3import frappe
4
5no_cache = 1
6
7def get_context(context):
Shivam Mishrae94e9d22019-05-30 18:05:00 +05308 # Load Query Parameters
9 try:
10 program = frappe.form_dict['program']
11 content = frappe.form_dict['content']
12 content_type = frappe.form_dict['type']
13 course = frappe.form_dict['course']
14 topic = frappe.form_dict['topic']
15 except KeyError:
16 frappe.local.flags.redirect_location = '/lms'
17 raise frappe.Redirect
Shivam Mishra4991fca2019-05-30 16:37:15 +053018
Shivam Mishrae94e9d22019-05-30 18:05:00 +053019
20 # 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:
Shivam Mishraae2871f2019-05-29 13:11:34 +053025 frappe.local.flags.redirect_location = '/lms'
26 raise frappe.Redirect
27
Shivam Mishrae94e9d22019-05-30 18:05:00 +053028
29 # Set context for content to be displayer
Shivam Mishrad1a25212019-06-03 12:57:38 +053030 context.content = frappe.get_doc(content_type, content).as_dict()
Shivam Mishra4991fca2019-05-30 16:37:15 +053031 context.content_type = content_type
Shivam Mishrae94e9d22019-05-30 18:05:00 +053032 context.program = program
33 context.course = course
34 context.topic = topic
Shivam Mishra4991fca2019-05-30 16:37:15 +053035
Shivam Mishrac8c790a2019-05-21 12:04:50 +053036 topic = frappe.get_doc("Topic", topic)
Shivam Mishrad49b5e42019-06-06 17:19:53 +053037 content_list = [{'content_type':item.content_type, 'content':item.content} for item in topic.topic_content]
Shivam Mishrae94e9d22019-05-30 18:05:00 +053038
39 # Set context for progress numbers
40 context.position = content_list.index({'content': content, 'content_type': content_type})
41 context.length = len(content_list)
42
43 # Set context for navigation
44 context.previous = get_previous_content(content_list, context.position)
45 context.next = get_next_content(content_list, context.position)
46
47def get_next_content(content_list, current_index):
Shivam Mishrac8c790a2019-05-21 12:04:50 +053048 try:
49 return content_list[current_index + 1]
50 except IndexError:
51 return None
52
Shivam Mishrae94e9d22019-05-30 18:05:00 +053053def get_previous_content(content_list, current_index):
Shivam Mishrac8c790a2019-05-21 12:04:50 +053054 if current_index == 0:
55 return None
56 else:
Shivam Mishra4991fca2019-05-30 16:37:15 +053057 return content_list[current_index - 1]
58
59def allowed_content_access(program, content, content_type):
Shivam Mishrad49b5e42019-06-06 17:19:53 +053060 contents_of_program = frappe.db.sql("""select `tabTopic Content`.content, `tabTopic Content`.content_type
61 from `tabCourse Topic`,
62 `tabProgram Course`,
63 `tabTopic Content`
64 where `tabCourse Topic`.parent = `tabProgram Course`.course
65 and `tabTopic Content`.parent = `tabCourse Topic`.topic
66 and `tabProgram Course`.parent = %(program)s""", {'program': program})
Shivam Mishra4991fca2019-05-30 16:37:15 +053067
68 return (content, content_type) in contents_of_program