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 | e94e9d2 | 2019-05-30 18:05:00 +0530 | [diff] [blame] | 8 | # 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 Mishra | 4991fca | 2019-05-30 16:37:15 +0530 | [diff] [blame] | 18 | |
Shivam Mishra | e94e9d2 | 2019-05-30 18:05:00 +0530 | [diff] [blame] | 19 | |
| 20 | # Check if user has access to the content |
Shivam Mishra | 4991fca | 2019-05-30 16:37:15 +0530 | [diff] [blame] | 21 | 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 Mishra | ae2871f | 2019-05-29 13:11:34 +0530 | [diff] [blame] | 25 | frappe.local.flags.redirect_location = '/lms' |
| 26 | raise frappe.Redirect |
| 27 | |
Shivam Mishra | e94e9d2 | 2019-05-30 18:05:00 +0530 | [diff] [blame] | 28 | |
| 29 | # Set context for content to be displayer |
Shivam Mishra | d1a2521 | 2019-06-03 12:57:38 +0530 | [diff] [blame] | 30 | context.content = frappe.get_doc(content_type, content).as_dict() |
Shivam Mishra | 4991fca | 2019-05-30 16:37:15 +0530 | [diff] [blame] | 31 | context.content_type = content_type |
Shivam Mishra | e94e9d2 | 2019-05-30 18:05:00 +0530 | [diff] [blame] | 32 | context.program = program |
| 33 | context.course = course |
| 34 | context.topic = topic |
Shivam Mishra | 4991fca | 2019-05-30 16:37:15 +0530 | [diff] [blame] | 35 | |
Shivam Mishra | c8c790a | 2019-05-21 12:04:50 +0530 | [diff] [blame] | 36 | topic = frappe.get_doc("Topic", topic) |
Shivam Mishra | d49b5e4 | 2019-06-06 17:19:53 +0530 | [diff] [blame] | 37 | content_list = [{'content_type':item.content_type, 'content':item.content} for item in topic.topic_content] |
Shivam Mishra | e94e9d2 | 2019-05-30 18:05:00 +0530 | [diff] [blame] | 38 | |
| 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 | |
| 47 | def get_next_content(content_list, current_index): |
Shivam Mishra | c8c790a | 2019-05-21 12:04:50 +0530 | [diff] [blame] | 48 | try: |
| 49 | return content_list[current_index + 1] |
| 50 | except IndexError: |
| 51 | return None |
| 52 | |
Shivam Mishra | e94e9d2 | 2019-05-30 18:05:00 +0530 | [diff] [blame] | 53 | def get_previous_content(content_list, current_index): |
Shivam Mishra | c8c790a | 2019-05-21 12:04:50 +0530 | [diff] [blame] | 54 | if current_index == 0: |
| 55 | return None |
| 56 | else: |
Shivam Mishra | 4991fca | 2019-05-30 16:37:15 +0530 | [diff] [blame] | 57 | return content_list[current_index - 1] |
| 58 | |
| 59 | def allowed_content_access(program, content, content_type): |
Shivam Mishra | d49b5e4 | 2019-06-06 17:19:53 +0530 | [diff] [blame] | 60 | 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 Mishra | 4991fca | 2019-05-30 16:37:15 +0530 | [diff] [blame] | 67 | |
| 68 | return (content, content_type) in contents_of_program |