blob: 67e3508df59690053acb5f30213358f502174413 [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 Mishra4991fca2019-05-30 16:37:15 +05308 program = frappe.form_dict['program']
9 content = frappe.form_dict['content']
10 content_type = frappe.form_dict['type']
11
12 has_program_access = utils.allowed_program_access(program)
13 has_content_access = allowed_content_access(program, content, content_type)
14
15 if frappe.session.user == "Guest" or not has_program_access or not has_content_access:
Shivam Mishraae2871f2019-05-29 13:11:34 +053016 frappe.local.flags.redirect_location = '/lms'
17 raise frappe.Redirect
18
Shivam Mishra4991fca2019-05-30 16:37:15 +053019 context.content = frappe.get_doc(content_type, content).as_dict()
20 context.content_type = content_type
21
Shivam Mishrac8c790a2019-05-21 12:04:50 +053022 context.course = frappe.form_dict['course']
23 context.topic = frappe.form_dict['topic']
Shivam Mishrac8c790a2019-05-21 12:04:50 +053024
Shivam Mishrac8c790a2019-05-21 12:04:50 +053025 context.previous = get_previous_content(context.topic, context.course, context.content, context.content_type)
Shivam Mishrac8c790a2019-05-21 12:04:50 +053026 context.next = get_next_content(context.topic, context.course, context.content, context.content_type)
27
Shivam Mishra4991fca2019-05-30 16:37:15 +053028
Shivam Mishrac8c790a2019-05-21 12:04:50 +053029def get_next_content(topic, course, content, content_type):
30 if frappe.session.user == "Guest":
31 return None
32 topic = frappe.get_doc("Topic", topic)
33 content_list = [{'content_type':item.doctype, 'content':item.name} for item in topic.get_contents()]
34 current_index = content_list.index({'content': content.name, 'content_type': content_type})
35 try:
36 return content_list[current_index + 1]
37 except IndexError:
38 return None
39
40def get_previous_content(topic, course, content, content_type):
41 if frappe.session.user == "Guest":
42 return None
43 topic = frappe.get_doc("Topic", topic)
44 content_list = [{'content_type':item.doctype, 'content':item.name} for item in topic.get_contents()]
45 current_index = content_list.index({'content': content.name, 'content_type': content_type})
46 if current_index == 0:
47 return None
48 else:
Shivam Mishra4991fca2019-05-30 16:37:15 +053049 return content_list[current_index - 1]
50
51def allowed_content_access(program, content, content_type):
52 # Get all content in program
53
54 # Using ORM
55 # course_in_program = [course.course for course in frappe.get_all('Program Course', fields=['course'], filters={'parent': program})]
56 # topics_in_course = [topic.topic for topic in frappe.get_all("Course Topic", fields=['topic'], filters=[['parent','in', course_in_program]])]
57 # 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]])]
58
59 contents_of_program = frappe.db.sql("""select `tabtopic content`.content, `tabtopic content`.content_type
60 from `tabcourse topic`,
61 `tabprogram course`,
62 `tabtopic content`
63 where `tabcourse topic`.parent = `tabprogram course`.course
64 and `tabtopic content`.parent = `tabcourse topic`.topic
65 and `tabprogram course`.parent = '{0}'""".format(program))
66
67 return (content, content_type) in contents_of_program