diff options
Diffstat (limited to 'yaksh/models.py')
-rw-r--r-- | yaksh/models.py | 62 |
1 files changed, 47 insertions, 15 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index fea8fda..9a406c3 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -1,4 +1,4 @@ -from __future__ import unicode_literals +from __future__ import unicode_literals, division from datetime import datetime, timedelta import json import random @@ -464,12 +464,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 """ @@ -492,6 +496,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.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 @@ -692,6 +707,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.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 @@ -1181,13 +1207,14 @@ class QuestionPaper(models.Model): return ans_paper - def _is_attempt_allowed(self, user): + def _is_attempt_allowed(self, user, course_id): attempts = AnswerPaper.objects.get_total_attempt(questionpaper=self, - user=user) + user=user, + course_id=course_id) return attempts != self.quiz.attempts_allowed def can_attempt_now(self, user, course_id): - if self._is_attempt_allowed(user): + if self._is_attempt_allowed(user, course_id): last_attempt = AnswerPaper.objects.get_user_last_attempt( user=user, questionpaper=self, course_id=course_id ) @@ -1265,10 +1292,11 @@ class QuestionSet(models.Model): ############################################################################### class AnswerPaperManager(models.Manager): - def get_all_questions(self, questionpaper_id, attempt_number, + def get_all_questions(self, questionpaper_id, attempt_number, course_id, status='completed'): ''' Return a dict of question id as key and count as value''' papers = self.filter(question_paper_id=questionpaper_id, + course_id=course_id, attempt_number=attempt_number, status=status) all_questions = list() questions = list() @@ -1279,9 +1307,10 @@ class AnswerPaperManager(models.Manager): return Counter(questions) def get_all_questions_answered(self, questionpaper_id, attempt_number, - status='completed'): + course_id, status='completed'): ''' Return a dict of answered question id as key and count as value''' papers = self.filter(question_paper_id=questionpaper_id, + course_id=course_id, attempt_number=attempt_number, status=status) questions_answered = list() for paper in papers: @@ -1319,15 +1348,17 @@ class AnswerPaperManager(models.Manager): ).count() def get_question_statistics(self, questionpaper_id, attempt_number, - status='completed'): + course_id, status='completed'): ''' Return dict with question object as key and list as value The list contains two value, first the number of times a question was answered correctly, and second the number of times a question appeared in a quiz''' question_stats = {} questions_answered = self.get_all_questions_answered(questionpaper_id, - attempt_number) - questions = self.get_all_questions(questionpaper_id, attempt_number) + attempt_number, + course_id) + questions = self.get_all_questions(questionpaper_id, attempt_number, + course_id) all_questions = Question.objects.filter( id__in=set(questions), active=True @@ -1376,8 +1407,9 @@ class AnswerPaperManager(models.Manager): def get_user_answerpapers(self, user): return self.filter(user=user) - def get_total_attempt(self, questionpaper, user): - return self.filter(question_paper=questionpaper, user=user).count() + def get_total_attempt(self, questionpaper, user, course_id): + return self.filter(question_paper=questionpaper, user=user, + course_id=course_id).count() def get_users_for_questionpaper(self, questionpaper_id, course_id): return self._get_answerpapers_for_quiz(questionpaper_id, course_id, |