summaryrefslogtreecommitdiff
path: root/yaksh/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'yaksh/models.py')
-rw-r--r--yaksh/models.py57
1 files changed, 47 insertions, 10 deletions
diff --git a/yaksh/models.py b/yaksh/models.py
index 00bca5b..a475493 100644
--- a/yaksh/models.py
+++ b/yaksh/models.py
@@ -321,7 +321,6 @@ class Lesson(models.Model):
lesson_files = self.get_files()
new_lesson = self
new_lesson.id = None
- new_lesson.name = "Copy of {0}".format(self.name)
new_lesson.creator = user
new_lesson.save()
for _file in lesson_files:
@@ -600,7 +599,6 @@ class Quiz(models.Model):
question_papers = self.questionpaper_set.all()
new_quiz = self
new_quiz.id = None
- new_quiz.description = "Copy of {0}".format(self.description)
new_quiz.creator = user
new_quiz.save()
for qp in question_papers:
@@ -846,12 +844,13 @@ class LearningModule(models.Model):
percent = round((count / units.count()) * 100)
return percent
- def _create_module_copy(self, user, module_name):
+ def _create_module_copy(self, user, module_name=None):
learning_units = self.learning_unit.order_by("order")
new_module = self
new_module.id = None
- new_module.name = module_name
new_module.creator = user
+ if module_name:
+ new_module.name = module_name
new_module.save()
for unit in learning_units:
new_unit = unit._create_unit_copy(user)
@@ -957,8 +956,8 @@ class Course(models.Model):
copy_course_name = "Copy Of {0}".format(self.name)
new_course = self._create_duplicate_instance(user, copy_course_name)
for module in learning_modules:
- copy_module_name = "Copy of {0}".format(module.name)
- new_module = module._create_module_copy(user, copy_module_name)
+ copy_module_name = module.name
+ new_module = module._create_module_copy(user)
new_course.learning_module.add(new_module)
return new_course
@@ -1754,7 +1753,8 @@ class QuestionPaperManager(models.Manager):
def create_trial_paper_to_test_quiz(self, trial_quiz, original_quiz_id):
"""Creates a trial question paper to test quiz."""
- trial_quiz.questionpaper_set.all().delete()
+ if self.filter(quiz=trial_quiz).exists():
+ self.get(quiz=trial_quiz).delete()
trial_questionpaper, trial_questions = \
self._create_trial_from_questionpaper(original_quiz_id)
trial_questionpaper.quiz = trial_quiz
@@ -1993,6 +1993,32 @@ class AnswerPaperManager(models.Manager):
questions.append(question.id)
return Counter(questions)
+ def get_per_answer_stats(self, questionpaper_id, attempt_number,
+ course_id, status='completed'):
+ papers = self.filter(question_paper_id=questionpaper_id,
+ course_id=course_id,
+ attempt_number=attempt_number, status=status)
+ questions = Question.objects.filter(
+ questions__id__in=papers,
+ ).distinct()
+
+ stats = {}
+ for question in questions:
+ answers = Answer.objects.filter(
+ answerpaper__id__in=papers, question=question.id
+ ).values('answer', 'question__id', 'answerpaper__id')
+ question_ans_count = {}
+ answerpaper_count = []
+ for ans in answers:
+ if ans.get('answerpaper__id'):
+ if ans.get('answer') not in question_ans_count:
+ question_ans_count[ans.get('answer')] = 1
+ else:
+ question_ans_count[ans.get('answer')] += 1
+ answerpaper_count.append(ans.get('answerpaper__id'))
+ stats[question] = question_ans_count
+ return stats
+
def get_all_questions_answered(self, questionpaper_id, attempt_number,
course_id, status='completed'):
''' Return a dict of answered question id as key and count as value'''
@@ -2046,16 +2072,27 @@ class AnswerPaperManager(models.Manager):
course_id)
questions = self.get_all_questions(questionpaper_id, attempt_number,
course_id)
+ per_answer_stats = self.get_per_answer_stats(
+ questionpaper_id, attempt_number, course_id
+ )
all_questions = Question.objects.filter(
id__in=set(questions),
active=True
).order_by('type')
for question in all_questions:
if question.id in questions_answered:
- question_stats[question] = [questions_answered[question.id],
- questions[question.id]]
+ question_stats[question] = {
+ 'answered': [questions_answered[question.id],
+ questions[question.id]],
+ 'per_answer': per_answer_stats[question],
+ }
+
else:
- question_stats[question] = [0, questions[question.id]]
+ question_stats[question] = {
+ 'answered': [0, questions[question.id]],
+ 'per_answer': per_answer_stats[question],
+ }
+
return question_stats
def _get_answerpapers_for_quiz(self, questionpaper_id, course_id,