diff options
-rw-r--r-- | yaksh/models.py | 58 | ||||
-rw-r--r-- | yaksh/views.py | 23 |
2 files changed, 57 insertions, 24 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index 398f508..f4a3a7a 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -616,11 +616,11 @@ class Quiz(models.Model): ############################################################################### class QuestionPaperManager(models.Manager): - def _create_trial_from_questionpaper(self, original_quiz_id): + def _create_trial_from_questionpaper(self, original_quiz_id, fixed_que): """Creates a copy of the original questionpaper""" trial_questionpaper = self.get(quiz_id=original_quiz_id) - trial_questions = {"fixed_questions": trial_questionpaper - .fixed_questions.all(), + trial_questions = {"fixed_questions": fixed_que.get_fixed_questions( + trial_questionpaper), "random_questions": trial_questionpaper .random_questions.all() } @@ -635,7 +635,10 @@ class QuestionPaperManager(models.Manager): trial_questionpaper = self.create(quiz=trial_quiz, total_marks=10, ) - trial_questionpaper.fixed_questions.add(*questions_list) + fixed_que = FixedQuestions() + for q_id in questions_list: + que = Question.objects.get(id=q_id) + fixed_que.add_fixed_questions(trial_questionpaper, que) return trial_questionpaper def create_trial_paper_to_test_quiz(self, trial_quiz, original_quiz_id): @@ -643,11 +646,16 @@ class QuestionPaperManager(models.Manager): if self.filter(quiz=trial_quiz).exists(): trial_questionpaper = self.get(quiz=trial_quiz) else: + fixed_que = FixedQuestions() trial_questionpaper, trial_questions = \ - self._create_trial_from_questionpaper(original_quiz_id) + self._create_trial_from_questionpaper(original_quiz_id, + fixed_que + ) trial_questionpaper.quiz = trial_quiz - trial_questionpaper.fixed_questions\ - .add(*trial_questions["fixed_questions"]) + for question in trial_questions["fixed_questions"]: + fixed_que.add_fixed_questions(trial_questionpaper, + question + ) trial_questionpaper.random_questions\ .add(*trial_questions["random_questions"]) trial_questionpaper.save() @@ -661,9 +669,6 @@ class QuestionPaper(models.Model): # Question paper belongs to a particular quiz. quiz = models.ForeignKey(Quiz) - # Questions that will be mandatory in the quiz. - fixed_questions = models.ManyToManyField(Question) - # Questions that will be fetched randomly from the Question Set. random_questions = models.ManyToManyField("QuestionSet") @@ -678,7 +683,8 @@ class QuestionPaper(models.Model): def update_total_marks(self): """ Updates the total marks for the Question Paper""" marks = 0.0 - questions = self.fixed_questions.all() + fixed_ques = FixedQuestions() + questions = fixed_ques.get_fixed_questions(self) for question in questions: marks += question.points for question_set in self.random_questions.all(): @@ -687,7 +693,8 @@ class QuestionPaper(models.Model): def _get_questions_for_answerpaper(self): """ Returns fixed and random questions for the answer paper""" - questions = list(self.fixed_questions.filter(active=True)) + fixed_ques = FixedQuestions() + questions = fixed_ques.get_fixed_questions(self) for question_set in self.random_questions.all(): questions += question_set.get_random_questions() return questions @@ -705,8 +712,10 @@ class QuestionPaper(models.Model): ans_paper.question_paper = self ans_paper.save() questions = self._get_questions_for_answerpaper() - ans_paper.questions.add(*questions) - ans_paper.questions_unanswered.add(*questions) + for question in questions: + ans_paper.questions.add(question) + for question in questions: + ans_paper.questions_unanswered.add(question) return ans_paper def _is_questionpaper_passed(self, user): @@ -748,14 +757,33 @@ class QuestionPaper(models.Model): summary="Yaksh Demo Question", user=user) # add fixed set of questions to the question paper + fixed_que = FixedQuestions() for question in questions: - question_paper.fixed_questions.add(question) + fixed_que.add_fixed_questions(questionpaper, question) def __str__(self): return "Question Paper for " + self.quiz.description ############################################################################### +class FixedQuestions(models.Model): + questionpaper = models.ForeignKey(QuestionPaper) + question = models.ForeignKey(Question) + + def get_fixed_questions(self, qp): + fixed_q = FixedQuestions.objects.filter(questionpaper=qp).order_by('pk') + questions = [que.question for que in fixed_q + if que.question.active == True] + return questions + + def add_fixed_questions(self, qp, que): + FixedQuestions.objects.create(questionpaper=qp, question=que) + + def remove_fixed_questions(self, que_ids): + FixedQuestions.objects.filter(question_id__in=que_ids).delete() + + +############################################################################### class QuestionSet(models.Model): """Question set contains a set of questions from which random questions will be selected for the quiz. diff --git a/yaksh/views.py b/yaksh/views.py index 63653e6..9b6fc4e 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -25,7 +25,8 @@ import six # Local imports. from yaksh.models import get_model_class, Quiz, Question, QuestionPaper, QuestionSet, Course from yaksh.models import Profile, Answer, AnswerPaper, User, TestCase, FileUpload,\ - has_profile, StandardTestCase, McqTestCase, StdIOBasedTestCase, HookTestCase + has_profile, StandardTestCase, McqTestCase, StdIOBasedTestCase, HookTestCase,\ + FixedQuestions from yaksh.forms import UserRegisterForm, UserLoginForm, QuizForm,\ QuestionForm, RandomQuestionForm,\ QuestionFilterForm, CourseForm, ProfileForm, UploadFileForm,\ @@ -265,7 +266,8 @@ def show_all_questionpapers(request, questionpaper_id=None): else: qu_papers = QuestionPaper.objects.get(id=questionpaper_id) quiz = qu_papers.quiz - fixed_questions = qu_papers.fixed_questions.all() + fixed_ques = FixedQuestions() + fixed_questions = fixed_ques.get_fixed_questions(qu_papers) random_questions = qu_papers.random_questions.all() context = {'quiz': quiz, 'fixed_questions': fixed_questions, 'random_questions': random_questions} @@ -805,8 +807,9 @@ def _remove_already_present(questionpaper_id, questions): if questionpaper_id is None: return questions questionpaper = QuestionPaper.objects.get(pk=questionpaper_id) - questions = questions.exclude( - id__in=questionpaper.fixed_questions.values_list('id', flat=True)) + fixed_questions = FixedQuestions.objects.filter( + questionpaper=questionpaper).values_list('question_id', flat=True) + questions = questions.exclude(id__in=fixed_questions) for random_set in questionpaper.random_questions.all(): questions = questions.exclude( id__in=random_set.questions.values_list('id', flat=True)) @@ -824,6 +827,7 @@ def design_questionpaper(request, quiz_id, questionpaper_id=None): questions = None marks = None state = None + fixed_que = FixedQuestions() if questionpaper_id is None: question_paper = QuestionPaper.objects.get_or_create(quiz_id=quiz_id)[0] else: @@ -839,13 +843,14 @@ def design_questionpaper(request, quiz_id, questionpaper_id=None): state = request.POST.get('is_active', None) if 'add-fixed' in request.POST: - question_ids = request.POST.getlist('questions', None) - for question in Question.objects.filter(id__in=question_ids): - question_paper.fixed_questions.add(question) + question_ids = request.POST.get('checked_ques', None).split(',') + for q_id in question_ids: + que = Question.objects.get(id=q_id) + fixed_que.add_fixed_questions(question_paper, que) if 'remove-fixed' in request.POST: question_ids = request.POST.getlist('added-questions', None) - question_paper.fixed_questions.remove(*question_ids) + fixed_que.remove_fixed_questions(question_ids) if 'add-random' in request.POST: question_ids = request.POST.getlist('random_questions', None) @@ -872,7 +877,7 @@ def design_questionpaper(request, quiz_id, questionpaper_id=None): question_paper.update_total_marks() question_paper.save() random_sets = question_paper.random_questions.all() - fixed_questions = question_paper.fixed_questions.all() + fixed_questions = fixed_que.get_fixed_questions(question_paper) context = {'qpaper_form': qpaper_form, 'filter_form': filter_form, 'qpaper': question_paper, 'questions': questions, 'fixed_questions': fixed_questions, 'state': state, 'random_sets': random_sets} |