From 8b657f2f400a76323dbde49efc31dbd71e775ff0 Mon Sep 17 00:00:00 2001 From: Anand Chitipothu Date: Fri, 14 May 2021 15:29:44 +0530 Subject: [PATCH] feat: added next/prev links to learn pages --- community/lms/doctype/lesson/lesson.py | 15 +++++++ .../lms/doctype/lms_course/lms_course.py | 43 +++++++++++++++++++ community/public/css/style.less | 4 +- community/www/courses/learn/index.html | 28 +++++++++--- community/www/courses/learn/index.py | 12 ++++++ community/www/macros/livecode.html | 2 +- 6 files changed, 95 insertions(+), 9 deletions(-) diff --git a/community/lms/doctype/lesson/lesson.py b/community/lms/doctype/lesson/lesson.py index 9d63a4b2..b1105240 100644 --- a/community/lms/doctype/lesson/lesson.py +++ b/community/lms/doctype/lesson/lesson.py @@ -22,3 +22,18 @@ class Lesson(Document): s.contents = section.contents s.index = index return s + + def get_next(self): + """Returns the number for the next lesson. + + The return value would be like 1.2, 2.1 etc. + It will be None if there is no next lesson. + """ + + + def get_prev(self): + """Returns the number for the prev lesson. + + The return value would be like 1.2, 2.1 etc. + It will be None if there is no next lesson. + """ diff --git a/community/lms/doctype/lms_course/lms_course.py b/community/lms/doctype/lms_course/lms_course.py index 56fc4001..914e5c90 100644 --- a/community/lms/doctype/lms_course/lms_course.py +++ b/community/lms/doctype/lms_course/lms_course.py @@ -190,3 +190,46 @@ class LMSCourse(Document): {"chapter": chapter_name, "index_": lesson_index}, "name") return lesson_name and frappe.get_doc("Lesson", lesson_name) + + def get_outline(self): + return CourseOutline(self) + +class CourseOutline: + def __init__(self, course): + self.course = course + self.chapters = self.get_chapters() + self.lessons = self.get_lessons() + + def get_next(self, current): + numbers = sorted(lesson['number'] for lesson in self.lessons) + try: + index = numbers.index(current) + return numbers[index+1] + except IndexError: + return None + + def get_prev(self, current): + numbers = sorted(lesson['number'] for lesson in self.lessons) + try: + index = numbers.index(current) + if index == 0: + return None + return numbers[index-1] + except IndexError: + return None + + def get_chapters(self): + return frappe.db.get_all("Chapter", + filters={"course": self.course.name}, + fields=["name", "title", "index_"]) + + def get_lessons(self): + chapters = [c['name'] for c in self.chapters] + lessons = frappe.db.get_all("Lesson", + filters={"chapter": ["IN", chapters]}, + fields=["name", "title", "chapter", "index_"]) + + chapter_numbers = {c['name']: c['index_'] for c in self.chapters} + for lesson in lessons: + lesson['number'] = "{}.{}".format(chapter_numbers[lesson['chapter']], lesson['index_']) + return lessons diff --git a/community/public/css/style.less b/community/public/css/style.less index 5bb00f30..b0800ff3 100644 --- a/community/public/css/style.less +++ b/community/public/css/style.less @@ -303,6 +303,6 @@ section.lightgray { color: black !important; } -.lesson { - margin: 20px 0px 20px 50px; +.lesson-page { + margin: 20px 0px; } diff --git a/community/www/courses/learn/index.html b/community/www/courses/learn/index.html index 8145e872..4804f508 100644 --- a/community/www/courses/learn/index.html +++ b/community/www/courses/learn/index.html @@ -23,15 +23,31 @@ {% block content %} {{ Sidebar(course.name, batch.name) }} -
-

{{ lesson.title }} - {{ lesson.name }}

+
+
+
+ {% if prev_url %} + ← Prev + {% endif %} + {% if next_url %} + Next → + {% endif %} +
- {% for s in lesson.get_sections() %} -
- {{ render_section(s) }} +

{{ lesson.title }}

+ + {% for s in lesson.get_sections() %} +
+ {{ render_section(s) }} +
+ {% endfor %} + +
- {% endfor %}
{% endblock %} diff --git a/community/www/courses/learn/index.py b/community/www/courses/learn/index.py index 36b25048..4aea3388 100644 --- a/community/www/courses/learn/index.py +++ b/community/www/courses/learn/index.py @@ -8,6 +8,7 @@ def get_context(context): batch_name = frappe.form_dict["batch"] chapter_index = frappe.form_dict.get("chapter") lesson_index = frappe.form_dict.get("lesson") + lesson_number = f"{chapter_index}.{lesson_index}" course = Course.find(course_name) if not course: @@ -30,5 +31,16 @@ def get_context(context): context.chapter_index = chapter_index context.livecode_url = get_livecode_url() + outline = course.get_outline() + next_ = outline.get_next(lesson_number) + prev_ = outline.get_prev(lesson_number) + context.next_url = get_learn_url(course_name, batch_name, next_) + context.prev_url = get_learn_url(course_name, batch_name, prev_) + +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_livecode_url(): return frappe.db.get_single_value("LMS Settings", "livecode_url") diff --git a/community/www/macros/livecode.html b/community/www/macros/livecode.html index de553e4d..94502e53 100644 --- a/community/www/macros/livecode.html +++ b/community/www/macros/livecode.html @@ -34,7 +34,7 @@ {% if is_exercise %} {% if last_submitted %} - Last submitted {{last_submitted}} + Submitted on {{last_submitted}} {% endif %} {% endif %}