From 102fa9c0a853b5b650b784cfbdf712e057b49094 Mon Sep 17 00:00:00 2001 From: Anand Chitipothu Date: Mon, 29 Nov 2021 17:33:45 +0530 Subject: [PATCH] feat: added a page to list cohorts of a course - added a page /courses//manage that lists the active cohorts - only accessible to mentors and staff - also added a "manage the course" button on course page for mentors/staff Issue #271 --- school/hooks.py | 1 + school/lms/doctype/lms_course/lms_course.py | 18 +++++++++++ school/www/cohorts/base.html | 34 +++++++++++++++++++++ school/www/cohorts/index.html | 34 +++++++++++++++++++++ school/www/cohorts/index.py | 30 ++++++++++++++++++ school/www/cohorts/utils.py | 17 +++++++++++ school/www/courses/course.html | 7 +++++ 7 files changed, 141 insertions(+) create mode 100644 school/www/cohorts/base.html create mode 100644 school/www/cohorts/index.html create mode 100644 school/www/cohorts/index.py create mode 100644 school/www/cohorts/utils.py diff --git a/school/hooks.py b/school/hooks.py index 01d6d866..b04c9da9 100644 --- a/school/hooks.py +++ b/school/hooks.py @@ -141,6 +141,7 @@ website_route_rules = [ {"from_route": "/courses//learn/.", "to_route": "batch/learn"}, {"from_route": "/courses//progress", "to_route": "batch/progress"}, {"from_route": "/courses//join", "to_route": "batch/join"}, + {"from_route": "/courses//manage", "to_route": "cohorts"}, {"from_route": "/users", "to_route": "profiles/profile"} ] diff --git a/school/lms/doctype/lms_course/lms_course.py b/school/lms/doctype/lms_course/lms_course.py index b9993cbe..6815dd6e 100644 --- a/school/lms/doctype/lms_course/lms_course.py +++ b/school/lms/doctype/lms_course/lms_course.py @@ -210,6 +210,24 @@ class LMSCourse(Document): visibility="Public") return batches + def get_cohorts(self): + return find_all("Cohort", course=self.name, order_by="creation") + + def is_cohort_staff(self, user_email): + """Returns True if the user is either a mentor or a staff for one or more active cohorts of this course. + """ + q1 = { + "doctype": "Cohort Staff", + "course": self.name, + "email": user_email + } + q2 = { + "doctype": "Cohort Mentor", + "course": self.name, + "email": user_email + } + return frappe.db.exists(q1) or frappe.db.exists(q2) + def get_lesson_index(self, lesson_name): """Returns the {chapter_index}.{lesson_index} for the lesson. """ diff --git a/school/www/cohorts/base.html b/school/www/cohorts/base.html new file mode 100644 index 00000000..f0177dc4 --- /dev/null +++ b/school/www/cohorts/base.html @@ -0,0 +1,34 @@ +{% extends "templates/base.html" %} + +{% macro render_nav(nav) %} + + +{% endmacro %} + +{% block title %}Cohorts{% endblock %} +{% block head_include %} + + +{% endblock %} + + +{% block content %} +
+
+ {{ render_nav(nav | default([])) }} + + {% block page_content %} + Hello, world! + {% endblock %} +
+
+{% endblock %} + + diff --git a/school/www/cohorts/index.html b/school/www/cohorts/index.html new file mode 100644 index 00000000..dcb4c5d8 --- /dev/null +++ b/school/www/cohorts/index.html @@ -0,0 +1,34 @@ +{% extends "www/cohorts/base.html" %} +{% block title %}Manage {{ course.title }}{% endblock %} + +{% block page_content %} +
+
+ {% if cohorts %} +

Cohorts

+
+ {% for cohort in cohorts %} +
+ {{ render_cohort(course, cohort) }} +
+ {% endfor %} + {% else %} +

Permission Denied

+

You don't have permission to manage this course.

+ {% endif %} +
+
+
+{% endblock %} + +{% macro render_cohort(course, cohort) %} +
+
+
{{cohort.title}}
+
{{cohort.begin_date}} - {{cohort.end_date}}
+ + Manage +
+
+ +{% endmacro %} diff --git a/school/www/cohorts/index.py b/school/www/cohorts/index.py new file mode 100644 index 00000000..5f27ef2f --- /dev/null +++ b/school/www/cohorts/index.py @@ -0,0 +1,30 @@ +import frappe +from .utils import get_course, add_nav + +def get_context(context): + context.no_cache = 1 + context.course = get_course() + if frappe.session.user == "Guest": + frappe.local.flags.redirect_location = "/login?redirect-to=" + frappe.request.path + raise frappe.Redirect() + + if not context.course: + context.template = "www/404.html" + return + + context.cohorts = get_cohorts(context.course) + + add_nav(context, "All Courses", "/courses") + add_nav(context, context.course.title, "/courses/" + context.course.name) + +def get_cohorts(course): + if "System Manager" in frappe.get_roles(): + return course.get_cohorts() + + staff_roles = frappe.get_all("Cohort Staff", filters={"course": course.name}, fields=["cohort"]) + mentor_roles = frappe.get_all("Cohort Mentor", filters={"course": course.name}, fields=["cohort"]) + roles = staff_roles + mentor_roles + print(roles) + names = {role.cohort for role in roles} + print(names) + return [frappe.get_doc("Cohort", name) for name in names] diff --git a/school/www/cohorts/utils.py b/school/www/cohorts/utils.py new file mode 100644 index 00000000..001d1e4d --- /dev/null +++ b/school/www/cohorts/utils.py @@ -0,0 +1,17 @@ +import frappe + +def get_course(course_name=None): + course_name = course_name or frappe.form_dict["course"] + return course_name and get_doc("LMS Course", course_name) + +def get_doc(doctype, name): + try: + return frappe.get_doc(doctype, name) + except frappe.exceptions.DoesNotExistError: + return + +def add_nav(context, title, href): + """Adds a breadcrumb to the navigation. + """ + nav = context.setdefault("nav", []) + nav.append({"title": title, "href": href}) diff --git a/school/www/courses/course.html b/school/www/courses/course.html index 364fdeec..e457ee58 100644 --- a/school/www/courses/course.html +++ b/school/www/courses/course.html @@ -70,6 +70,13 @@ {% endif %} + + {% if course.is_cohort_staff(frappe.session.user) %} + + Manage the course + + {% endif %}