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 | ae2871f | 2019-05-29 13:11:34 +0530 | [diff] [blame] | 8 | if frappe.session.user == "Guest": |
| 9 | frappe.local.flags.redirect_location = '/lms' |
| 10 | raise frappe.Redirect |
| 11 | |
Shivam Mishra | c8c790a | 2019-05-21 12:04:50 +0530 | [diff] [blame] | 12 | context.course = frappe.form_dict['course'] |
| 13 | context.topic = frappe.form_dict['topic'] |
| 14 | content = frappe.form_dict['content'] |
| 15 | context.content_type = frappe.form_dict['type'] |
| 16 | |
| 17 | context.content = frappe.get_doc(context.content_type, content).as_dict() |
Shivam Mishra | c8c790a | 2019-05-21 12:04:50 +0530 | [diff] [blame] | 18 | 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] | 19 | context.next = get_next_content(context.topic, context.course, context.content, context.content_type) |
| 20 | |
| 21 | def get_next_content(topic, course, content, content_type): |
| 22 | if frappe.session.user == "Guest": |
| 23 | return None |
| 24 | topic = frappe.get_doc("Topic", topic) |
| 25 | content_list = [{'content_type':item.doctype, 'content':item.name} for item in topic.get_contents()] |
| 26 | current_index = content_list.index({'content': content.name, 'content_type': content_type}) |
| 27 | try: |
| 28 | return content_list[current_index + 1] |
| 29 | except IndexError: |
| 30 | return None |
| 31 | |
| 32 | def get_previous_content(topic, course, content, content_type): |
| 33 | if frappe.session.user == "Guest": |
| 34 | return None |
| 35 | topic = frappe.get_doc("Topic", topic) |
| 36 | content_list = [{'content_type':item.doctype, 'content':item.name} for item in topic.get_contents()] |
| 37 | current_index = content_list.index({'content': content.name, 'content_type': content_type}) |
| 38 | if current_index == 0: |
| 39 | return None |
| 40 | else: |
| 41 | return content_list[current_index - 1] |