Shivam Mishra | c8c790a | 2019-05-21 12:04:50 +0530 | [diff] [blame] | 1 | import frappe |
| 2 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 3 | import erpnext.education.utils as utils |
| 4 | |
Shivam Mishra | c8c790a | 2019-05-21 12:04:50 +0530 | [diff] [blame] | 5 | no_cache = 1 |
| 6 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 7 | |
Shivam Mishra | c8c790a | 2019-05-21 12:04:50 +0530 | [diff] [blame] | 8 | def get_context(context): |
Shivam Mishra | e94e9d2 | 2019-05-30 18:05:00 +0530 | [diff] [blame] | 9 | # Load Query Parameters |
| 10 | try: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 11 | program = frappe.form_dict["program"] |
| 12 | content = frappe.form_dict["content"] |
| 13 | content_type = frappe.form_dict["type"] |
| 14 | course = frappe.form_dict["course"] |
| 15 | topic = frappe.form_dict["topic"] |
Shivam Mishra | e94e9d2 | 2019-05-30 18:05:00 +0530 | [diff] [blame] | 16 | except KeyError: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 17 | frappe.local.flags.redirect_location = "/lms" |
Shivam Mishra | e94e9d2 | 2019-05-30 18:05:00 +0530 | [diff] [blame] | 18 | raise frappe.Redirect |
Shivam Mishra | 4991fca | 2019-05-30 16:37:15 +0530 | [diff] [blame] | 19 | |
Shivam Mishra | e94e9d2 | 2019-05-30 18:05:00 +0530 | [diff] [blame] | 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: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 25 | frappe.local.flags.redirect_location = "/lms" |
Shivam Mishra | ae2871f | 2019-05-29 13:11:34 +0530 | [diff] [blame] | 26 | raise frappe.Redirect |
| 27 | |
Shivam Mishra | e94e9d2 | 2019-05-30 18:05:00 +0530 | [diff] [blame] | 28 | # Set context for content to be displayer |
Shivam Mishra | d1a2521 | 2019-06-03 12:57:38 +0530 | [diff] [blame] | 29 | context.content = frappe.get_doc(content_type, content).as_dict() |
Shivam Mishra | 4991fca | 2019-05-30 16:37:15 +0530 | [diff] [blame] | 30 | context.content_type = content_type |
Shivam Mishra | e94e9d2 | 2019-05-30 18:05:00 +0530 | [diff] [blame] | 31 | context.program = program |
| 32 | context.course = course |
| 33 | context.topic = topic |
Shivam Mishra | 4991fca | 2019-05-30 16:37:15 +0530 | [diff] [blame] | 34 | |
Shivam Mishra | c8c790a | 2019-05-21 12:04:50 +0530 | [diff] [blame] | 35 | topic = frappe.get_doc("Topic", topic) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 36 | content_list = [ |
| 37 | {"content_type": item.content_type, "content": item.content} for item in topic.topic_content |
| 38 | ] |
Shivam Mishra | e94e9d2 | 2019-05-30 18:05:00 +0530 | [diff] [blame] | 39 | |
| 40 | # Set context for progress numbers |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 41 | context.position = content_list.index({"content": content, "content_type": content_type}) |
Shivam Mishra | e94e9d2 | 2019-05-30 18:05:00 +0530 | [diff] [blame] | 42 | context.length = len(content_list) |
| 43 | |
| 44 | # Set context for navigation |
| 45 | context.previous = get_previous_content(content_list, context.position) |
| 46 | context.next = get_next_content(content_list, context.position) |
| 47 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 48 | |
Shivam Mishra | e94e9d2 | 2019-05-30 18:05:00 +0530 | [diff] [blame] | 49 | def get_next_content(content_list, current_index): |
Shivam Mishra | c8c790a | 2019-05-21 12:04:50 +0530 | [diff] [blame] | 50 | try: |
| 51 | return content_list[current_index + 1] |
| 52 | except IndexError: |
| 53 | return None |
| 54 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 55 | |
Shivam Mishra | e94e9d2 | 2019-05-30 18:05:00 +0530 | [diff] [blame] | 56 | def get_previous_content(content_list, current_index): |
Shivam Mishra | c8c790a | 2019-05-21 12:04:50 +0530 | [diff] [blame] | 57 | if current_index == 0: |
| 58 | return None |
| 59 | else: |
Shivam Mishra | 4991fca | 2019-05-30 16:37:15 +0530 | [diff] [blame] | 60 | return content_list[current_index - 1] |
| 61 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 62 | |
Shivam Mishra | 4991fca | 2019-05-30 16:37:15 +0530 | [diff] [blame] | 63 | def allowed_content_access(program, content, content_type): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 64 | contents_of_program = frappe.db.sql( |
| 65 | """select `tabTopic Content`.content, `tabTopic Content`.content_type |
Shivam Mishra | d49b5e4 | 2019-06-06 17:19:53 +0530 | [diff] [blame] | 66 | from `tabCourse Topic`, |
| 67 | `tabProgram Course`, |
| 68 | `tabTopic Content` |
| 69 | where `tabCourse Topic`.parent = `tabProgram Course`.course |
| 70 | and `tabTopic Content`.parent = `tabCourse Topic`.topic |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 71 | and `tabProgram Course`.parent = %(program)s""", |
| 72 | {"program": program}, |
| 73 | ) |
Shivam Mishra | 4991fca | 2019-05-30 16:37:15 +0530 | [diff] [blame] | 74 | |
Ankush Menat | 4551d7d | 2021-08-19 13:41:10 +0530 | [diff] [blame] | 75 | return (content, content_type) in contents_of_program |