diff --git a/community/lms/api.py b/community/lms/api.py index 3a7bd1b0..9cf2fc9d 100644 --- a/community/lms/api.py +++ b/community/lms/api.py @@ -34,3 +34,21 @@ def submit_solution(exercise, code): return doc = ex.submit(code) return {"name": doc.name, "creation": doc.creation} + +@frappe.whitelist() +def save_current_lesson(batch_name, lesson_name): + """Saves the current lesson for a student/mentor. + """ + name = frappe.get_value( + doctype="LMS Batch Membership", + filters={ + "batch": batch_name, + "member_email": frappe.session.user + }, + fieldname="name") + if not name: + return + doc = frappe.get_doc("LMS Batch Membership", name) + doc.current_lesson = lesson_name + doc.save(ignore_permissions=True) + return {"current_lesson": doc.current_lesson} diff --git a/community/lms/doctype/lms_batch/lms_batch.py b/community/lms/doctype/lms_batch/lms_batch.py index 5deb770f..6be6e6e6 100644 --- a/community/lms/doctype/lms_batch/lms_batch.py +++ b/community/lms/doctype/lms_batch/lms_batch.py @@ -69,6 +69,24 @@ class LMSBatch(Document): message.is_author = True return messages + def get_membership(self, email): + """Returns the membership document of given user. + """ + name = frappe.get_value( + doctype="LMS Batch Membership", + filters={ + "batch": self.name, + "member": email + }, + fieldname="name") + return frappe.get_doc("LMS Batch Membership", name) + + def get_current_lesson(self, user): + """Returns the name of the current lesson for the given user. + """ + membership = self.get_membership(user) + return membership and membership.current_lesson + @frappe.whitelist() def save_message(message, batch): doc = frappe.get_doc({ diff --git a/community/lms/doctype/lms_course/lms_course.py b/community/lms/doctype/lms_course/lms_course.py index 501f5341..f72264ba 100644 --- a/community/lms/doctype/lms_course/lms_course.py +++ b/community/lms/doctype/lms_course/lms_course.py @@ -150,6 +150,13 @@ class LMSCourse(Document): "name") return lesson_name and frappe.get_doc("Lesson", lesson_name) + def get_lesson_index(self, lesson_name): + """Returns the {chapter_index}.{lesson_index} for the lesson. + """ + lesson = frappe.get_doc("Lesson", lesson_name) + chapter = frappe.get_doc("Chapter", lesson.chapter) + return f"{chapter.index_}.{lesson.index_}" + def get_outline(self): return CourseOutline(self) diff --git a/community/www/batch/learn.html b/community/www/batch/learn.html index 31750946..19699202 100644 --- a/community/www/batch/learn.html +++ b/community/www/batch/learn.html @@ -84,49 +84,16 @@ {{ super() }} {{ LiveCodeEditorJS() }} - - + + {%- endblock %} diff --git a/community/www/batch/learn.py b/community/www/batch/learn.py index 538fa26c..d42e60d9 100644 --- a/community/www/batch/learn.py +++ b/community/www/batch/learn.py @@ -12,13 +12,14 @@ def get_context(context): batch_name = context.batch.name if not chapter_index or not lesson_index: - frappe.local.flags.redirect_location = f"/courses/{course_name}/{batch_name}/learn/1.1" + index_ = get_lesson_index(context.course, context.batch, frappe.session.user) or "1.1" + frappe.local.flags.redirect_location = get_learn_url(course_name, batch_name, index_) raise frappe.Redirect context.lesson = context.course.get_lesson(chapter_index, lesson_index) context.lesson_index = lesson_index context.chapter_index = chapter_index - print(context.lesson) + outline = context.course.get_outline() next_ = outline.get_next(lesson_number) prev_ = outline.get_prev(lesson_number) @@ -29,3 +30,9 @@ def get_learn_url(course_name, batch_name, lesson_number): if not lesson_number: return return f"/courses/{course_name}/{batch_name}/learn/{lesson_number}" + +def get_lesson_index(course, batch, user): + lesson = batch.get_current_lesson(user) + return lesson and course.get_lesson_index(lesson) + +