2019-05-21 06:34:50 +00:00
|
|
|
import frappe
|
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
import erpnext.education.utils as utils
|
|
|
|
|
2019-05-21 06:34:50 +00:00
|
|
|
no_cache = 1
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-05-21 06:34:50 +00:00
|
|
|
def get_context(context):
|
2019-05-30 12:35:00 +00:00
|
|
|
# Load Query Parameters
|
|
|
|
try:
|
2022-03-28 13:22:46 +00:00
|
|
|
program = frappe.form_dict["program"]
|
|
|
|
content = frappe.form_dict["content"]
|
|
|
|
content_type = frappe.form_dict["type"]
|
|
|
|
course = frappe.form_dict["course"]
|
|
|
|
topic = frappe.form_dict["topic"]
|
2019-05-30 12:35:00 +00:00
|
|
|
except KeyError:
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.local.flags.redirect_location = "/lms"
|
2019-05-30 12:35:00 +00:00
|
|
|
raise frappe.Redirect
|
|
|
|
|
|
|
|
# Check if user has access to the content
|
2019-05-30 11:07:15 +00:00
|
|
|
has_program_access = utils.allowed_program_access(program)
|
|
|
|
has_content_access = allowed_content_access(program, content, content_type)
|
|
|
|
|
|
|
|
if frappe.session.user == "Guest" or not has_program_access or not has_content_access:
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.local.flags.redirect_location = "/lms"
|
2019-05-29 07:41:34 +00:00
|
|
|
raise frappe.Redirect
|
|
|
|
|
2019-05-30 12:35:00 +00:00
|
|
|
# Set context for content to be displayer
|
2019-06-03 07:27:38 +00:00
|
|
|
context.content = frappe.get_doc(content_type, content).as_dict()
|
2019-05-30 11:07:15 +00:00
|
|
|
context.content_type = content_type
|
2019-05-30 12:35:00 +00:00
|
|
|
context.program = program
|
|
|
|
context.course = course
|
|
|
|
context.topic = topic
|
2019-05-30 11:07:15 +00:00
|
|
|
|
2019-05-30 12:35:00 +00:00
|
|
|
topic = frappe.get_doc("Topic", topic)
|
2022-03-28 13:22:46 +00:00
|
|
|
content_list = [
|
|
|
|
{"content_type": item.content_type, "content": item.content} for item in topic.topic_content
|
|
|
|
]
|
2019-05-21 06:34:50 +00:00
|
|
|
|
2019-05-30 12:35:00 +00:00
|
|
|
# Set context for progress numbers
|
2022-03-28 13:22:46 +00:00
|
|
|
context.position = content_list.index({"content": content, "content_type": content_type})
|
2019-05-30 12:35:00 +00:00
|
|
|
context.length = len(content_list)
|
2019-05-21 06:34:50 +00:00
|
|
|
|
2019-05-30 12:35:00 +00:00
|
|
|
# Set context for navigation
|
|
|
|
context.previous = get_previous_content(content_list, context.position)
|
|
|
|
context.next = get_next_content(content_list, context.position)
|
2019-05-30 11:07:15 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-05-30 12:35:00 +00:00
|
|
|
def get_next_content(content_list, current_index):
|
2019-05-21 06:34:50 +00:00
|
|
|
try:
|
|
|
|
return content_list[current_index + 1]
|
|
|
|
except IndexError:
|
|
|
|
return None
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-05-30 12:35:00 +00:00
|
|
|
def get_previous_content(content_list, current_index):
|
2019-05-21 06:34:50 +00:00
|
|
|
if current_index == 0:
|
|
|
|
return None
|
|
|
|
else:
|
2019-05-30 11:07:15 +00:00
|
|
|
return content_list[current_index - 1]
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-05-30 11:07:15 +00:00
|
|
|
def allowed_content_access(program, content, content_type):
|
2022-03-28 13:22:46 +00:00
|
|
|
contents_of_program = frappe.db.sql(
|
|
|
|
"""select `tabTopic Content`.content, `tabTopic Content`.content_type
|
2019-06-06 11:49:53 +00:00
|
|
|
from `tabCourse Topic`,
|
|
|
|
`tabProgram Course`,
|
|
|
|
`tabTopic Content`
|
|
|
|
where `tabCourse Topic`.parent = `tabProgram Course`.course
|
|
|
|
and `tabTopic Content`.parent = `tabCourse Topic`.topic
|
2022-03-28 13:22:46 +00:00
|
|
|
and `tabProgram Course`.parent = %(program)s""",
|
|
|
|
{"program": program},
|
|
|
|
)
|
2019-05-30 11:07:15 +00:00
|
|
|
|
2021-08-19 08:11:10 +00:00
|
|
|
return (content, content_type) in contents_of_program
|