From 943d2c1f6d0a1f99ffc6e48bc0c82249e2d4d08c Mon Sep 17 00:00:00 2001 From: adityacp Date: Fri, 12 Jan 2018 17:33:16 +0530 Subject: Change in models.py, views.py and urls.py - Add new model methods to calculate percent of module and course completion - Add new view function for displaying course status - Add new url to redirect to course status --- yaksh/models.py | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) (limited to 'yaksh/models.py') diff --git a/yaksh/models.py b/yaksh/models.py index d4a73fa..f37df3b 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -453,12 +453,16 @@ class LearningModule(models.Model): ordered_units = learning_module.learning_unit.order_by("order") status_list = [unit.get_completion_status(user, course) for unit in ordered_units] - if all([status == "completed" for status in status_list]): - return "completed" + + if not status_list: + default_status = "no units" + elif all([status == "completed" for status in status_list]): + default_status = "completed" elif "inprogress" in status_list: - return "inprogress" + default_status = "inprogress" else: - return "not attempted" + default_status = "not attempted" + return default_status def is_prerequisite_passed(self, user, course): """ Check if prerequisite module is completed """ @@ -481,6 +485,17 @@ class LearningModule(models.Model): def has_prerequisite(self): return self.check_prerequisite + def get_module_complete_percent(self, course, user): + units = self.get_learning_units() + if not units: + percent = 0 + else: + status_list = [unit.get_completion_status(user, course) + for unit in units] + count = status_list.count("completed") + percent = round((count / len(units)) * 100) + return percent + def __str__(self): return self.name @@ -681,6 +696,17 @@ class Course(models.Model): next_index = 0 return modules.get(id=module_ids[next_index]) + def percent_completed(self, user): + modules = self.get_learning_modules() + if not modules: + percent = 0 + else: + status_list = [module.get_module_complete_percent(self, user) + for module in modules] + count = sum(status_list) + percent = round((count / len(modules))) + return percent + def __str__(self): return self.name -- cgit