Shivam Mishra | c8c790a | 2019-05-21 12:04:50 +0530 | [diff] [blame] | 1 | from __future__ import unicode_literals |
| 2 | import erpnext.education.utils as utils |
| 3 | import frappe |
| 4 | |
| 5 | no_cache = 1 |
| 6 | |
| 7 | def get_context(context): |
Shivam Mishra | 4991fca | 2019-05-30 16:37:15 +0530 | [diff] [blame] | 8 | 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 Mishra | ae2871f | 2019-05-29 13:11:34 +0530 | [diff] [blame] | 16 | frappe.local.flags.redirect_location = '/lms' |
| 17 | raise frappe.Redirect |
| 18 | |
Shivam Mishra | 1257961 | 2019-05-30 17:19:11 +0530 | [diff] [blame^] | 19 | context.content = frappe.get_doc(content_type, content) |
Shivam Mishra | 4991fca | 2019-05-30 16:37:15 +0530 | [diff] [blame] | 20 | context.content_type = content_type |
| 21 | |
Shivam Mishra | c8c790a | 2019-05-21 12:04:50 +0530 | [diff] [blame] | 22 | context.course = frappe.form_dict['course'] |
| 23 | context.topic = frappe.form_dict['topic'] |
Shivam Mishra | c8c790a | 2019-05-21 12:04:50 +0530 | [diff] [blame] | 24 | |
Shivam Mishra | c8c790a | 2019-05-21 12:04:50 +0530 | [diff] [blame] | 25 | context.previous = get_previous_content(context.topic, context.course, context.content, context.content_type) |
Shivam Mishra | c8c790a | 2019-05-21 12:04:50 +0530 | [diff] [blame] | 26 | context.next = get_next_content(context.topic, context.course, context.content, context.content_type) |
| 27 | |
Shivam Mishra | 4991fca | 2019-05-30 16:37:15 +0530 | [diff] [blame] | 28 | |
Shivam Mishra | c8c790a | 2019-05-21 12:04:50 +0530 | [diff] [blame] | 29 | def 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 | |
| 40 | def 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 Mishra | 4991fca | 2019-05-30 16:37:15 +0530 | [diff] [blame] | 49 | return content_list[current_index - 1] |
| 50 | |
| 51 | def 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 |