From c77835b81f4b96576efcc5d2e6342c3ac4efac6b Mon Sep 17 00:00:00 2001 From: Anand Chitipothu Date: Fri, 21 May 2021 13:13:34 +0530 Subject: [PATCH] feat: added page to see progress of a batch --- community/hooks.py | 1 + community/www/batch/progress.html | 52 +++++++++++++++++++++++++ community/www/batch/progress.py | 64 +++++++++++++++++++++++++++++++ community/www/macros/sidebar.html | 3 ++ 4 files changed, 120 insertions(+) create mode 100644 community/www/batch/progress.html create mode 100644 community/www/batch/progress.py diff --git a/community/hooks.py b/community/hooks.py index 62abd1ed..057b7b34 100644 --- a/community/hooks.py +++ b/community/hooks.py @@ -147,6 +147,7 @@ primary_rules = [ {"from_route": "/courses///members", "to_route": "batch/members"}, {"from_route": "/courses///discuss", "to_route": "batch/discuss"}, {"from_route": "/courses///about", "to_route": "batch/about"}, + {"from_route": "/courses///progress", "to_route": "batch/progress"} ] # Any frappe default URL is blocked by profile-rules, add it here to unblock it diff --git a/community/www/batch/progress.html b/community/www/batch/progress.html new file mode 100644 index 00000000..ebfde54f --- /dev/null +++ b/community/www/batch/progress.html @@ -0,0 +1,52 @@ +{% extends "templates/base.html" %} +{% from "www/macros/sidebar.html" import Sidebar %} +{% from "www/macros/livecode.html" import LiveCodeEditorJS, LiveCodeEditor with context %} +{% block title %}{{ course.title }} - Batch Dashboard{% endblock %} + +{% block head_include %} + + + + + + + + + + + + + + +{% endblock %} + + +{% block content %} +{{ Sidebar(course, batch) }} + +
+
+

Batch Progress

+ {% for exercise in report.exercises %} +
+

{{exercise.title}}

+ {% for s in report.get_submissions_of_exercise(exercise.name) %} +
+

{{s.owner.full_name}}

+
+ {{ LiveCodeEditor(name=s.name, code=s.solution, reset_code=s.solution) }} +
+
+ {% endfor %} +
+ {% endfor %} +
+
+{% endblock %} + + +{%- block script %} + {{ super() }} + {{ LiveCodeEditorJS() }} +{% endblock %} diff --git a/community/www/batch/progress.py b/community/www/batch/progress.py new file mode 100644 index 00000000..628b6233 --- /dev/null +++ b/community/www/batch/progress.py @@ -0,0 +1,64 @@ +import frappe +from community.lms.models import Course +from collections import defaultdict +from . import utils + +def get_context(context): + utils.get_common_context(context) + + exercise_name = frappe.form_dict.get("exercise") + if exercise_name: + exercise = frappe.get_doc("Exercise", exercise_name) + else: + exercise = None + + context.exercise = exercise + context.report = BatchReport(context.course, context.batch) + +class BatchReport: + def __init__(self, course, batch): + self.submissions = get_submissions(batch) + self.exercises = self.get_exercises(course.name) + + self.submissions_by_exercise = defaultdict(list) + for s in self.submissions: + self.submissions_by_exercise[s.exercise].append(s) + + def get_exercises(self, course_name): + return frappe.get_all("Exercise", {"course": course_name}, ["name", "title"]) + + def get_submissions_of_exercise(self, exercise_name): + return self.submissions_by_exercise[exercise_name] + +def get_submissions(batch): + students = batch.get_students() + students_map = {s['email']: s for s in students} + + names, values = nparams("s", students_map.keys()) + + sql = """ + select owner, exercise, name, solution, creation, image + from ( + select owner, exercise, name, solution, creation, image, + row_number() over (partition by owner, exercise order by creation desc) as ix + from `tabExercise Submission`) as t + where t.ix=1 and owner IN {} + """.format(names) + + data = frappe.db.sql(sql, values=values, as_dict=True) + + for row in data: + row['owner'] = students_map[row['owner']] + return data + +def nparams(name, values): + """Creates n paramters from a list of values for a db query. + + >>> nparams("name", ["a", "b]) + ("(%(name_1)s, %(name_2)s)", {"name_1": "a", "name_2": "b"}) + """ + keys = [f"{name}_{i}" for i, _ in enumerate(values, start=1)] + param_names = [f"%({k})s" for k in keys] + param_values = dict(zip(keys, values)) + joined_names = "(" + ", ".join(param_names) + ")" + return joined_names, param_values diff --git a/community/www/macros/sidebar.html b/community/www/macros/sidebar.html index 125962ec..5237544a 100644 --- a/community/www/macros/sidebar.html +++ b/community/www/macros/sidebar.html @@ -7,5 +7,8 @@ + {% if batch.is_member(frappe.session.user, member_type="Mentor") %} + + {% endif %} {% endmacro %}