feat: added next/prev links to learn pages

This commit is contained in:
Anand Chitipothu
2021-05-14 15:29:44 +05:30
parent 49b41749e8
commit 8b657f2f40
6 changed files with 95 additions and 9 deletions

View File

@@ -22,3 +22,18 @@ class Lesson(Document):
s.contents = section.contents s.contents = section.contents
s.index = index s.index = index
return s 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.
"""

View File

@@ -190,3 +190,46 @@ class LMSCourse(Document):
{"chapter": chapter_name, "index_": lesson_index}, {"chapter": chapter_name, "index_": lesson_index},
"name") "name")
return lesson_name and frappe.get_doc("Lesson", lesson_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

View File

@@ -303,6 +303,6 @@ section.lightgray {
color: black !important; color: black !important;
} }
.lesson { .lesson-page {
margin: 20px 0px 20px 50px; margin: 20px 0px;
} }

View File

@@ -23,15 +23,31 @@
{% block content %} {% block content %}
{{ Sidebar(course.name, batch.name) }} {{ Sidebar(course.name, batch.name) }}
<div class="container lesson">
<h1>{{ lesson.title }} - {{ lesson.name }}</h1> <div class="container">
<div class="lesson-page">
<div class="lesson-pagination">
{% if prev_url %}
<a href="{{prev_url}}" class="btn">&larr; Prev</a>
{% endif %}
{% if next_url %}
<a href="{{next_url}}" class="btn pull-right">Next &rarr;</a>
{% endif %}
</div>
{% for s in lesson.get_sections() %} <h2>{{ lesson.title }}</h2>
<div class="section section-{{ s.type }}">
{{ render_section(s) }} {% for s in lesson.get_sections() %}
<div class="section section-{{ s.type }}">
{{ render_section(s) }}
</div>
{% endfor %}
<div class="lesson-pagination">
<a href="#" class="btn">&larr; Prev</a>
<a href="#" class="btn pull-right">Next &rarr;</a>
</div>
</div> </div>
{% endfor %}
</div> </div>
{% endblock %} {% endblock %}

View File

@@ -8,6 +8,7 @@ def get_context(context):
batch_name = frappe.form_dict["batch"] batch_name = frappe.form_dict["batch"]
chapter_index = frappe.form_dict.get("chapter") chapter_index = frappe.form_dict.get("chapter")
lesson_index = frappe.form_dict.get("lesson") lesson_index = frappe.form_dict.get("lesson")
lesson_number = f"{chapter_index}.{lesson_index}"
course = Course.find(course_name) course = Course.find(course_name)
if not course: if not course:
@@ -30,5 +31,16 @@ def get_context(context):
context.chapter_index = chapter_index context.chapter_index = chapter_index
context.livecode_url = get_livecode_url() 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(): def get_livecode_url():
return frappe.db.get_single_value("LMS Settings", "livecode_url") return frappe.db.get_single_value("LMS Settings", "livecode_url")

View File

@@ -34,7 +34,7 @@
{% if is_exercise %} {% if is_exercise %}
<button class="submit pull-right btn-primary">Submit</button> <button class="submit pull-right btn-primary">Submit</button>
{% if last_submitted %} {% if last_submitted %}
<span class="pull-right" style="padding-right: 10px;">Last submitted {{last_submitted}}</span> <span class="pull-right" style="padding-right: 10px;">Submitted <span class="human-time" data-timestamp="{{last_submitted}}">on {{last_submitted}}</span></span>
{% endif %} {% endif %}
{% endif %} {% endif %}
</div> </div>