summaryrefslogtreecommitdiff
path: root/yaksh
diff options
context:
space:
mode:
authorPrabhu Ramachandran2017-03-20 18:51:47 +0530
committerGitHub2017-03-20 18:51:47 +0530
commit6a09816d66f9f24c0dba275d0dd3aaf7289eb73b (patch)
treeca1bbe5db8a96c7444a11f26400612601cde4757 /yaksh
parent23ecd8fa33e7fa2e953aa9715ae45a2869a044a0 (diff)
parent81a70c486823149b009627eb0194618ceeeb2369 (diff)
downloadonline_test-6a09816d66f9f24c0dba275d0dd3aaf7289eb73b.tar.gz
online_test-6a09816d66f9f24c0dba275d0dd3aaf7289eb73b.tar.bz2
online_test-6a09816d66f9f24c0dba275d0dd3aaf7289eb73b.zip
Merge pull request #228 from adityacp/fixes
Ordering Questions while creating Question Paper
Diffstat (limited to 'yaksh')
-rw-r--r--yaksh/models.py32
-rw-r--r--yaksh/static/yaksh/js/question_paper_creation.js14
-rw-r--r--yaksh/templates/yaksh/design_questionpaper.html8
-rw-r--r--yaksh/test_models.py71
-rw-r--r--yaksh/test_views.py2
-rw-r--r--yaksh/views.py30
6 files changed, 112 insertions, 45 deletions
diff --git a/yaksh/models.py b/yaksh/models.py
index 50ecb1c..42e8714 100644
--- a/yaksh/models.py
+++ b/yaksh/models.py
@@ -631,8 +631,8 @@ class QuestionPaperManager(models.Manager):
def _create_trial_from_questionpaper(self, original_quiz_id):
"""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(),
+ fixed_ques = trial_questionpaper.get_ordered_questions()
+ trial_questions = {"fixed_questions": fixed_ques,
"random_questions": trial_questionpaper
.random_questions.all()
}
@@ -647,6 +647,7 @@ class QuestionPaperManager(models.Manager):
trial_questionpaper = self.create(quiz=trial_quiz,
total_marks=10,
)
+ trial_questionpaper.fixed_question_order = ",".join(questions_list)
trial_questionpaper.fixed_questions.add(*questions_list)
return trial_questionpaper
@@ -685,6 +686,9 @@ class QuestionPaper(models.Model):
# Total marks for the question paper.
total_marks = models.FloatField(default=0.0, blank=True)
+ # Sequence or Order of fixed questions
+ fixed_question_order = models.CharField(max_length=255, blank=True)
+
objects = QuestionPaperManager()
def update_total_marks(self):
@@ -699,7 +703,7 @@ 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))
+ questions = self.get_ordered_questions()
for question_set in self.random_questions.all():
questions += question_set.get_random_questions()
return questions
@@ -717,8 +721,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):
@@ -759,9 +765,21 @@ class QuestionPaper(models.Model):
questions = Question.objects.filter(active=True,
summary="Yaksh Demo Question",
user=user)
+ q_order = [str(que.id) for que in questions]
+ question_paper.fixed_question_order = ",".join(q_order)
+ question_paper.save()
# add fixed set of questions to the question paper
- for question in questions:
- question_paper.fixed_questions.add(question)
+ question_paper.fixed_questions.add(*questions)
+
+ def get_ordered_questions(self):
+ ques = []
+ if self.fixed_question_order:
+ que_order = self.fixed_question_order.split(',')
+ for que_id in que_order:
+ ques.append(self.fixed_questions.get(id=que_id))
+ else:
+ ques = self.fixed_questions.all()
+ return ques
def __str__(self):
return "Question Paper for " + self.quiz.description
diff --git a/yaksh/static/yaksh/js/question_paper_creation.js b/yaksh/static/yaksh/js/question_paper_creation.js
index 898e491..430ec4b 100644
--- a/yaksh/static/yaksh/js/question_paper_creation.js
+++ b/yaksh/static/yaksh/js/question_paper_creation.js
@@ -46,4 +46,18 @@ $(document).ready(function(){
$("#random").click();
});
+ var checked_vals = [];
+ $('input:checkbox[name="questions"]').click(function() {
+ if($(this).prop("checked") == true){
+ checked_vals.push(parseInt($(this).val()));
+ }
+ else{
+ checked_vals.pop(parseInt($(this).val()));
+ }
+ });
+ $('#design_q').submit(function(eventObj) {
+ $(this).append('<input type="hidden" name="checked_ques" value='+checked_vals+'>');
+ return true;
+});
+
});//document
diff --git a/yaksh/templates/yaksh/design_questionpaper.html b/yaksh/templates/yaksh/design_questionpaper.html
index 4418c27..829e27f 100644
--- a/yaksh/templates/yaksh/design_questionpaper.html
+++ b/yaksh/templates/yaksh/design_questionpaper.html
@@ -23,7 +23,7 @@ select
{% block content %}
<input type=hidden id="url_root" value={{ URL_ROOT }}>
-<form action="{{ URL_ROOT }}/exam/manage/designquestionpaper/{{ qpaper.quiz.id }}/{{ qpaper.id }}/" method="POST">
+<form action="{{ URL_ROOT }}/exam/manage/designquestionpaper/{{ qpaper.quiz.id }}/{{ qpaper.id }}/" method="POST" id="design_q">
<input class ="btn primary small" type="submit" name="back" id="back" value="Cancel">
{% csrf_token %}
<input type=hidden name="is_active" id="is_active" value="{{ state }}">
@@ -95,8 +95,10 @@ select
{% for question in fixed_questions %}
<li>
<label>
- <input type="checkbox" name="added-questions" data-qid="{{question.id}}" value={{question.id}}>
- <span> {{ question.summary }} </span> <span> {{ question.points }} </span>
+ <input type="checkbox" name="added-questions"
+ data-qid="{{question.id}}" value={{question.id}}>
+ <span> {{ question.summary }} </span>
+ <span> {{ question.points }} </span>
</label>
</li>
{% endfor %}
diff --git a/yaksh/test_models.py b/yaksh/test_models.py
index cd66aed..6812212 100644
--- a/yaksh/test_models.py
+++ b/yaksh/test_models.py
@@ -324,9 +324,13 @@ class QuestionPaperTestCases(unittest.TestCase):
shuffle_questions=True
)
+ self.question_paper.fixed_question_order = "{0}, {1}".format(
+ self.questions[3].id, self.questions[5].id
+ )
# add fixed set of questions to the question paper
self.question_paper.fixed_questions.add(self.questions[3],
- self.questions[5])
+ self.questions[5]
+ )
# create two QuestionSet for random questions
# QuestionSet 1
self.question_set_1 = QuestionSet.objects.create(marks=2,
@@ -368,14 +372,15 @@ class QuestionPaperTestCases(unittest.TestCase):
# For Trial case
self.questions_list = [self.questions[3].id, self.questions[5].id]
- trial_course = Course.objects.create_trial_course(self.user)
- trial_quiz = Quiz.objects.create_trial_quiz(trial_course, self.user)
+ self.trial_course = Course.objects.create_trial_course(self.user)
+ self.trial_quiz = Quiz.objects.create_trial_quiz(self.trial_course, self.user)
def test_questionpaper(self):
""" Test question paper"""
self.assertEqual(self.question_paper.quiz.description, 'demo quiz')
self.assertSequenceEqual(self.question_paper.fixed_questions.all(),
- [self.questions[3], self.questions[5]])
+ [self.questions[3], self.questions[5]]
+ )
self.assertTrue(self.question_paper.shuffle_questions)
def test_update_total_marks(self):
@@ -423,29 +428,41 @@ class QuestionPaperTestCases(unittest.TestCase):
# test can_attempt_now(self):
self.assertFalse(self.question_paper.can_attempt_now(self.user))
- def test_create_trial_paper_to_test_quiz(self):
- trial_paper = QuestionPaper.objects.create_trial_paper_to_test_quiz\
- (trial_quiz,
- self.question_paper.id
- )
- self.assertEqual(trial_paper.quiz, trial_quiz)
- self.assertEqual(trial_paper.fixed_questions.all(),
- self.question_paper.fixed_questions.all()
- )
- self.assertEqual(trial_paper.random_questions.all(),
- self.question_paper.random_questions.all()
- )
-
- def test_create_trial_paper_to_test_questions(self):
- trial_paper = QuestionPaper.objects.\
- create_trial_paper_to_test_questions(
- trial_quiz, self.questions_list
- )
- self.assertEqual(trial_paper.quiz, trial_quiz)
- self.assertEqual(self.questions_list,
- self.question_paper.fixed_questions
- .values_list("id", flat=True)
- )
+ def test_create_trial_paper_to_test_quiz(self):
+ qu_list = [str(self.questions_list[0]), str(self.questions_list[1])]
+ trial_paper = QuestionPaper.objects.create_trial_paper_to_test_quiz\
+ (self.trial_quiz,
+ self.quiz.id
+ )
+ trial_paper.random_questions.add(self.question_set_1)
+ trial_paper.random_questions.add(self.question_set_2)
+ trial_paper.fixed_question_order = ",".join(qu_list)
+ self.assertEqual(trial_paper.quiz, self.trial_quiz)
+ self.assertSequenceEqual(trial_paper.get_ordered_questions(),
+ self.question_paper.get_ordered_questions()
+ )
+ trial_paper_ran = [q_set.id for q_set in
+ trial_paper.random_questions.all()]
+ qp_ran = [q_set.id for q_set in
+ self.question_paper.random_questions.all()]
+
+ self.assertSequenceEqual(trial_paper_ran, qp_ran)
+
+ def test_create_trial_paper_to_test_questions(self):
+ qu_list = [str(self.questions_list[0]), str(self.questions_list[1])]
+ trial_paper = QuestionPaper.objects.\
+ create_trial_paper_to_test_questions(
+ self.trial_quiz, qu_list
+ )
+ self.assertEqual(trial_paper.quiz, self.trial_quiz)
+ fixed_q = self.question_paper.fixed_questions.values_list(
+ 'id', flat=True)
+ self.assertSequenceEqual(self.questions_list, fixed_q)
+
+ def test_fixed_order_questions(self):
+ fixed_ques = self.question_paper.get_ordered_questions()
+ actual_ques = [self.questions[3], self.questions[5]]
+ self.assertSequenceEqual(fixed_ques, actual_ques)
###############################################################################
diff --git a/yaksh/test_views.py b/yaksh/test_views.py
index 7757f70..aa6561a 100644
--- a/yaksh/test_views.py
+++ b/yaksh/test_views.py
@@ -1161,7 +1161,6 @@ class TestViewAnswerPaper(TestCase):
self.question_paper = QuestionPaper.objects.create(quiz=self.quiz,
total_marks=1.0)
-
self.question_paper.fixed_questions.add(self.question)
self.question_paper.save()
@@ -1446,7 +1445,6 @@ class TestGrader(TestCase):
self.question_paper = QuestionPaper.objects.create(quiz=self.quiz,
total_marks=1.0)
-
self.question_paper.fixed_questions.add(self.question)
self.question_paper.save()
diff --git a/yaksh/views.py b/yaksh/views.py
index daa81eb..a752ec2 100644
--- a/yaksh/views.py
+++ b/yaksh/views.py
@@ -25,6 +25,7 @@ import six
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
+
from yaksh.forms import UserRegisterForm, UserLoginForm, QuizForm,\
QuestionForm, RandomQuestionForm,\
QuestionFilterForm, CourseForm, ProfileForm, UploadFileForm,\
@@ -264,7 +265,7 @@ 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_questions = qu_papers.get_ordered_questions()
random_questions = qu_papers.random_questions.all()
context = {'quiz': quiz, 'fixed_questions': fixed_questions,
'random_questions': random_questions}
@@ -356,7 +357,8 @@ def start(request, questionpaper_id=None, attempt_num=None):
msg = 'Quiz not found, please contact your '\
'instructor/administrator.'
return complete(request, msg, attempt_num, questionpaper_id=None)
- if not quest_paper.fixed_questions.all() and not quest_paper.random_questions.all():
+ if not quest_paper.get_ordered_questions() and not \
+ quest_paper.random_questions.all():
msg = 'Quiz does not have Questions, please contact your '\
'instructor/administrator.'
return complete(request, msg, attempt_num, questionpaper_id=None)
@@ -838,12 +840,28 @@ 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)
+ if question_paper.fixed_question_order:
+ ques_order = question_paper.fixed_question_order.split(",") +\
+ question_ids.split(",")
+ questions_order = ",".join(ques_order)
+ else:
+ questions_order = question_ids
+ questions = Question.objects.filter(id__in=question_ids.split(','))
+ question_paper.fixed_question_order = questions_order
+ question_paper.save()
+ question_paper.fixed_questions.add(*questions)
if 'remove-fixed' in request.POST:
question_ids = request.POST.getlist('added-questions', None)
+ que_order = question_paper.fixed_question_order.split(",")
+ for qid in question_ids:
+ que_order.remove(qid)
+ if que_order:
+ question_paper.fixed_question_order = ",".join(que_order)
+ else:
+ question_paper.fixed_question_order = ""
+ question_paper.save()
question_paper.fixed_questions.remove(*question_ids)
if 'add-random' in request.POST:
@@ -871,7 +889,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 = question_paper.get_ordered_questions()
context = {'qpaper_form': qpaper_form, 'filter_form': filter_form, 'qpaper':
question_paper, 'questions': questions, 'fixed_questions': fixed_questions,
'state': state, 'random_sets': random_sets}