From 16b4129ede4795d8f2af09ce08e60ddbdda97a8b Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Wed, 5 Jun 2019 17:29:48 +0530 Subject: [PATCH] feat: added student profile page --- .../program_enrollment/program_enrollment.py | 23 -------- erpnext/education/utils.py | 53 +++++++++++++++--- erpnext/www/lms/course.py | 2 +- erpnext/www/lms/profile.html | 56 +++++++++++++++++++ erpnext/www/lms/profile.py | 24 ++++++++ erpnext/www/lms/program.py | 2 +- 6 files changed, 128 insertions(+), 32 deletions(-) create mode 100644 erpnext/www/lms/profile.html create mode 100644 erpnext/www/lms/profile.py diff --git a/erpnext/education/doctype/program_enrollment/program_enrollment.py b/erpnext/education/doctype/program_enrollment/program_enrollment.py index 22cca86fcf..d232e47245 100644 --- a/erpnext/education/doctype/program_enrollment/program_enrollment.py +++ b/erpnext/education/doctype/program_enrollment/program_enrollment.py @@ -96,29 +96,6 @@ class ProgramEnrollment(Document): quiz_progress.program = self.program return quiz_progress - def get_program_progress(self): - import math - program = frappe.get_doc("Program", self.program) - program_progress = {} - progress = [] - for course in program.get_all_children(): - course_progress = lms.get_student_course_details(course.course, self.program) - is_complete = False - if course_progress['flag'] == "Completed": - is_complete = True - progress.append({'course_name': course.course_name, 'name': course.course, 'is_complete': is_complete}) - - program_progress['progress'] = progress - program_progress['name'] = self.program - program_progress['program'] = frappe.get_value("Program", self.program, 'program_name') - - try: - program_progress['percentage'] = math.ceil((sum([item['is_complete'] for item in progress] * 100)/len(progress))) - except ZeroDivisionError: - program_progress['percentage'] = 0 - - return program_progress - @frappe.whitelist() def get_program_courses(doctype, txt, searchfield, start, page_len, filters): if filters.get('program'): diff --git a/erpnext/education/utils.py b/erpnext/education/utils.py index 3352b515f3..ac7294badb 100644 --- a/erpnext/education/utils.py +++ b/erpnext/education/utils.py @@ -239,7 +239,7 @@ def get_quiz(quiz_name, course): status, score, result = check_quiz_completion(quiz, course_enrollment) return {'questions': questions, 'activity': {'is_complete': status, 'score': score, 'result': result}} -def get_student_topic_details(topic, course_name, program): +def get_topic_progress(topic, course_name, program): """ Return the porgress of a course in a program as well as the content to continue from. :param topic_name: @@ -260,7 +260,7 @@ def get_student_topic_details(topic, course_name, program): elif count < len(progress): return {'completed': False, 'started': True} -def get_student_course_details(course, program): +def get_course_progress(course, program): """ Return the porgress of a course in a program as well as the content to continue from. :param topic_name: @@ -269,16 +269,14 @@ def get_student_course_details(course, program): course_progress = [] for course_topic in course.topics: topic = frappe.get_doc("Topic", course_topic.topic) - progress = get_student_topic_details(topic, course.name, program) + progress = get_topic_progress(topic, course.name, program) if progress: course_progress.append(progress) - if course_progress: number_of_completed_topics = sum([activity['completed'] for activity in course_progress]) total_topics = len(course_progress) - print("course_progress", course_progress) - print("number_of_completed_topics", number_of_completed_topics) - print("total_topics", total_topics) + if total_topics == 1: + return course_progress[0] if number_of_completed_topics == 0: return {'completed': False, 'started': False} if number_of_completed_topics == total_topics: @@ -288,6 +286,47 @@ def get_student_course_details(course, program): return None +def get_program_progress(program): + program_progress = [] + if not program.courses: + return None + for program_course in program.courses: + course = frappe.get_doc("Course", program_course.course) + progress = get_course_progress(course, program.name) + if progress: + progress['name'] = course.name + progress['course'] = course.course_name + program_progress.append(progress) + + if program_progress: + return program_progress + + return None + +def get_program_completion(program): + topics = frappe.db.sql("""select `tabcourse topic`.topic, `tabcourse topic`.parent + from `tabcourse topic`, + `tabprogram course` + where `tabcourse topic`.parent = `tabprogram course`.course + and `tabprogram course`.parent = '{0}'""".format(program.name)) + + progress = [] + for topic in topics: + topic_doc = frappe.get_doc('Topic', topic[0]) + topic_progress = get_topic_progress(topic_doc, topic[1], program.name) + if topic_progress: + progress.append(topic_progress) + + if progress: + number_of_completed_topics = sum([activity['completed'] for activity in progress if activity]) + total_topics = len(progress) + try: + return int((float(number_of_completed_topics)/total_topics)*100) + except ZeroDivisionError: + return 0 + + return 0 + def create_student_from_current_user(): user = frappe.get_doc("User", frappe.session.user) diff --git a/erpnext/www/lms/course.py b/erpnext/www/lms/course.py index f59c28cf7e..e7ed2e3ed6 100644 --- a/erpnext/www/lms/course.py +++ b/erpnext/www/lms/course.py @@ -15,5 +15,5 @@ def get_context(context): context.progress = get_topic_progress(context.topics, course, context.program) def get_topic_progress(topics, course, program): - progress = {topic.name: utils.get_student_topic_details(topic, course.name, program) for topic in topics} + progress = {topic.name: utils.get_topic_progress(topic, course.name, program) for topic in topics} return progress diff --git a/erpnext/www/lms/profile.html b/erpnext/www/lms/profile.html new file mode 100644 index 0000000000..c642265b63 --- /dev/null +++ b/erpnext/www/lms/profile.html @@ -0,0 +1,56 @@ +{% extends "templates/base.html" %} +{% block title %}Profile{% endblock %} +{% from "www/lms/macros/hero.html" import hero %} + +{% macro card(program) %} +
+ + + +
+{% endmacro %} + +{% block content %} +
+
+ +

{{ student.first_name }} {{ student.last_name or '' }}

+

{{ student.name }}

+
+
+
+ {% for program in progress %} + {{ card(program) }} + {% endfor %} +
+
+
+{% endblock %} \ No newline at end of file diff --git a/erpnext/www/lms/profile.py b/erpnext/www/lms/profile.py new file mode 100644 index 0000000000..1a0decce32 --- /dev/null +++ b/erpnext/www/lms/profile.py @@ -0,0 +1,24 @@ +from __future__ import unicode_literals +import erpnext.education.utils as utils +import frappe + +no_cache = 1 + +def get_context(context): + context.student = utils.get_current_student() + context.progress = get_program_progress(context.student.name) + +def get_program_progress(student): + enrolled_programs = frappe.get_all("Program Enrollment", filters={'student':student}, fields=['program']) + student_progress = [] + for list_item in enrolled_programs: + program = frappe.get_doc("Program", list_item.program) + progress = utils.get_program_progress(program) + completion = utils.get_program_completion(program) + student_progress.append({'program': program.program_name, 'name': program.name, 'progress':progress, 'completion': completion}) + + return student_progress + + + + diff --git a/erpnext/www/lms/program.py b/erpnext/www/lms/program.py index 1242336688..a92ec31082 100644 --- a/erpnext/www/lms/program.py +++ b/erpnext/www/lms/program.py @@ -18,5 +18,5 @@ def get_program(program_name): frappe.throw(_("Program {0} does not exist.".format(program_name))) def get_course_progress(courses, program): - progress = {course.name: utils.get_student_course_details(course, program) for course in courses} + progress = {course.name: utils.get_course_progress(course, program) for course in courses} return progress \ No newline at end of file