def design_questionpaper(request): user = request.user ci = RequestContext(request) if not user.is_authenticated() or not is_moderator(user): raise Http404('You are not allowed to view this page!') if request.method == 'POST': fixed_questions = request.POST.getlist('fixed') random_questions = request.POST.getlist('random') random_number = request.POST.getlist('number') is_shuffle = request.POST.get('shuffle_questions', False) if is_shuffle == 'on': is_shuffle = True question_paper = QuestionPaper(shuffle_questions=is_shuffle) quiz = Quiz.objects.order_by("-id")[0] tot_marks = 0 question_paper.quiz = quiz question_paper.total_marks = tot_marks question_paper.save() if fixed_questions: fixed_questions_ids = ",".join(fixed_questions) fixed_questions_ids_list = fixed_questions_ids.split(',') for question_id in fixed_questions_ids_list: question_paper.fixed_questions.add(question_id) if random_questions: for random_question, num in zip(random_questions, random_number): question = Question.objects.get(id=random_question[0]) marks = question.points question_set = QuestionSet(marks=marks, num_questions=num) question_set.save() for question_id in random_question.split(','): question_set.questions.add(question_id) question_paper.random_questions.add(question_set) question_paper.update_total_marks() question_paper.save() return my_redirect('/exam/manage/showquiz') else: form = RandomQuestionForm() context = {'form': form} return my_render_to_response('exam/design_questionpaper.html', context, context_instance=ci)
def start(request): user = request.user try: # Right now the app is designed so there is only one active quiz # at a particular time. quiz = Quiz.objects.get(active=True) except Quiz.DoesNotExist: msg = 'No active quiz found, please contact your '\ 'instructor/administrator. Please login again thereafter.' return complete(request, reason=msg) try: old_paper = QuestionPaper.objects.get(user=user, quiz=quiz) q = old_paper.current_question() return show_question(request, q) except QuestionPaper.DoesNotExist: ip = request.META['REMOTE_ADDR'] key = gen_key(10) try: profile = user.get_profile() except Profile.DoesNotExist: msg = 'You do not have a profile and cannot take the quiz!' raise Http404(msg) new_paper = QuestionPaper(user=user, user_ip=ip, key=key, quiz=quiz, profile=profile) new_paper.start_time = datetime.datetime.now() # Make user directory. user_dir = get_user_dir(user) questions = [ str(_.id) for _ in Question.objects.filter(active=True) ] random.shuffle(questions) new_paper.questions = "|".join(questions) new_paper.save() # Show the user the intro page. context = {'user': user} ci = RequestContext(request) return my_render_to_response('exam/intro.html', context, context_instance=ci)
def start(request): user = request.user try: # Right now the app is designed so there is only one active test # at a particular time. test = Test.objects.get(active=True) except Test.DoesNotExist: msg = 'No active test found, please contact your '\ 'instructor/administrator. Please login again thereafter.' return complete(request, reason=msg) try: old_paper = QuestionPaper.objects.get(user=user, test=test) q = old_paper.current_question() return show_question(request, q) except QuestionPaper.DoesNotExist: ip = request.META['REMOTE_ADDR'] key = gen_key(10) try: profile = user.get_profile() except Profile.DoesNotExist: msg = 'You do not have a profile and cannot take the test!' raise Http404(msg) new_paper = QuestionPaper(user=user, user_ip=ip, key=key, test=test, profile=profile) new_paper.start_time = datetime.datetime.now() # Make user directory. user_dir = get_user_dir(user) questions = [ str(_.id) for _ in Question.objects.filter(active=True) ] random.shuffle(questions) new_paper.questions = "|".join(questions) new_paper.save() old_paper = QuestionPaper.objects.get(user=user, test=test) q = old_paper.current_question() return show_question(request, q)
def start(request): user = request.user try: # Right now the app is designed so there is only one active quiz # at a particular time. quiz = Quiz.objects.get(active=True) except Quiz.DoesNotExist: msg = "No active quiz found, please contact your " "instructor/administrator. Please login again thereafter." return complete(request, reason=msg) try: old_paper = QuestionPaper.objects.get(user=user, quiz=quiz) q = old_paper.current_question() return show_question(request, q) except QuestionPaper.DoesNotExist: ip = request.META["REMOTE_ADDR"] key = gen_key(10) try: profile = user.get_profile() except Profile.DoesNotExist: msg = "You do not have a profile and cannot take the quiz!" raise Http404(msg) new_paper = QuestionPaper(user=user, user_ip=ip, key=key, quiz=quiz, profile=profile) new_paper.start_time = datetime.datetime.now() # Make user directory. user_dir = get_user_dir(user) questions = [str(_.id) for _ in Question.objects.filter(active=True)] random.shuffle(questions) new_paper.questions = "|".join(questions) new_paper.save() # Show the user the intro page. context = {"user": user} ci = RequestContext(request) return my_render_to_response("exam/intro.html", context, context_instance=ci)
class AnswerPaperTestCases(unittest.TestCase): @classmethod def setUpClass(self): self.ip = '101.0.0.1' self.user = User.objects.get(id=1) self.profile = self.user.profile self.quiz = Quiz.objects.get(pk=1) self.question_paper = QuestionPaper(quiz=self.quiz, total_marks=3) self.question_paper.save() # create answerpaper self.answerpaper = AnswerPaper(user=self.user, profile=self.profile, questions='1|2|3', question_paper=self.question_paper, start_time='2014-06-13 12:20:19.791297', end_time='2014-06-13 12:50:19.791297', user_ip=self.ip) self.answerpaper.questions_answered = '1' self.answerpaper.save() # answers for the Answer Paper self.answer_right = Answer(question=Question.objects.get(id=1), answer="Demo answer", correct=True, marks=1) self.answer_wrong = Answer(question=Question.objects.get(id=2), answer="My answer", correct=False, marks=0) self.answer_right.save() self.answer_wrong.save() self.answerpaper.answers.add(self.answer_right) self.answerpaper.answers.add(self.answer_wrong) def test_answerpaper(self): """ Test Answer Paper""" self.assertEqual(self.answerpaper.user.username, 'demo_user') self.assertEqual(self.answerpaper.profile_id, 1) self.assertEqual(self.answerpaper.user_ip, self.ip) questions = self.answerpaper.questions num_questions = len(questions.split('|')) self.assertEqual(questions, '1|2|3') self.assertEqual(num_questions, 3) self.assertEqual(self.answerpaper.question_paper, self.question_paper) self.assertEqual(self.answerpaper.start_time, '2014-06-13 12:20:19.791297') self.assertEqual(self.answerpaper.end_time, '2014-06-13 12:50:19.791297') def test_current_question(self): """ Test current_question() method of Answer Paper""" current_question = self.answerpaper.current_question() self.assertEqual(current_question, '2') def test_completed_question(self): """ Test completed_question() method of Answer Paper""" question = self.answerpaper.completed_question(1) self.assertEqual(self.answerpaper.questions_left(), 2) def test_questions_left(self): """ Test questions_left() method of Answer Paper""" self.assertEqual(self.answerpaper.questions_left(), 2) def test_skip(self): """ Test skip() method of Answer Paper""" next_question_id = self.answerpaper.skip() self.assertTrue(next_question_id is not None) self.assertEqual(next_question_id, '3') def test_answered_str(self): """ Test answered_str() method of Answer Paper""" answered_question = self.answerpaper.get_answered_str() self.assertEqual(answered_question, '1') def test_update_marks_obtained(self): """ Test get_marks_obtained() method of Answer Paper""" self.answerpaper.update_marks_obtained() self.assertEqual(self.answerpaper.marks_obtained, 1.0) def test_update_percent(self): """ Test update_percent() method of Answerpaper""" self.answerpaper.update_percent() self.assertEqual(self.answerpaper.percent, 33.33) def test_update_passed(self): """ Test update_passed method of AnswerPaper""" self.answerpaper.update_passed() self.assertFalse(self.answerpaper.passed) def test_get_question_answer(self): """ Test get_question_answer() method of Answer Paper""" answered = self.answerpaper.get_question_answers() first_answer = answered.values()[0][0] self.assertEqual(first_answer.answer, 'Demo answer') self.assertTrue(first_answer.correct) self.assertEqual(len(answered), 2)
def manual_questionpaper(request, questionpaper_id=None): user = request.user ci = RequestContext(request) if not user.is_authenticated() or not is_moderator(user): raise Http404('You are not allowed to view this page!') if questionpaper_id is None: if request.method == "POST": if request.POST.get('save') == 'save': questions = request.POST.getlist('questions') quest_paper = QuestionPaper() quiz = Quiz.objects.order_by("-id")[0] tot_marks = 0 for quest in questions: q = Question.objects.get(id=quest) tot_marks += q.points quest_paper.quiz = quiz quest_paper.total_marks = tot_marks quest_paper.save() for i in questions: q = Question.objects.get(id=i) quest_paper.questions.add(q) return my_redirect('/exam/manage/showquiz') else: fetched_questions = fetch_questions(request) n = len(fetched_questions) msg = '' if (n == 0): msg = 'No matching Question found...' tags = Tag.objects.all() context = {'data': {'questions': fetched_questions, 'tags': tags, 'msg': msg}} return my_render_to_response('exam/manual_questionpaper.html', context, context_instance=ci) else: tags = Tag.objects.all() context = {'data': {'tags': tags}} return my_render_to_response('exam/manual_questionpaper.html', context, context_instance=ci) else: if request.method == "POST": if request.POST.get('save') == 'save': quest_paper = QuestionPaper.objects.get(id=questionpaper_id) questions = request.POST.getlist('questions') tot_marks = quest_paper.total_marks for quest in questions: q = Question.objects.get(id=quest) tot_marks += q.points quest_paper.total_marks = tot_marks quest_paper.save() for i in questions: q = Question.objects.get(id=i) quest_paper.questions.add(q) return my_redirect('/exam/manage/showquiz') else: fetched_questions = fetch_questions(request) n = len(fetched_questions) msg = '' if (n == 0): msg = 'No matching Question found...' tags = Tag.objects.all() context = {'data': {'questions': fetched_questions, 'tags': tags, 'msg': msg}} return my_render_to_response('exam/manual_questionpaper.html', context, context_instance=ci) else: tags = Tag.objects.all() context = {'data': {'tags': tags}} return my_render_to_response('exam/manual_questionpaper.html', context, context_instance=ci)
def automatic_questionpaper(request, questionpaper_id=None): """Generate automatic question paper for a particular quiz""" user = request.user ci = RequestContext(request) if not user.is_authenticated() or not is_moderator(user): raise Http404('You are not allowed to view this page!') if questionpaper_id is None: if request.method == "POST": if request.POST.get('save') == 'save': quiz = Quiz.objects.order_by("-id")[0] quest_paper = QuestionPaper() questions = request.POST.getlist('questions') tot_marks = 0 for quest in questions: q = Question.objects.get(id=quest) tot_marks += q.points quest_paper.quiz = quiz quest_paper.total_marks = tot_marks quest_paper.save() for quest in questions: q = Question.objects.get(id=quest) quest_paper.fixed_questions.add(q) return my_redirect('/exam/manage/showquiz') else: no_questions = int(request.POST.get('num_questions')) fetched_questions = fetch_questions(request) n = len(fetched_questions) msg = '' if (no_questions < n): i = n - no_questions for i in range(0, i): fetched_questions.pop() elif (no_questions > n): msg = 'The given Criteria does not satisfy the number\ of Questions...' tags = Tag.objects.all() context = {'data': {'questions': fetched_questions, 'tags': tags, 'msg': msg}} return my_render_to_response( 'exam/automatic_questionpaper.html', context, context_instance=ci) else: tags = Tag.objects.all() context = {'data': {'tags': tags}} return my_render_to_response('exam/automatic_questionpaper.html', context, context_instance=ci) else: if request.method == "POST": if request.POST.get('save') == 'save': quest_paper = QuestionPaper.objects.get(id=questionpaper_id) questions = request.POST.getlist('questions') tot_marks = quest_paper.total_marks for quest in questions: q = Question.objects.get(id=quest) tot_marks += q.points quest_paper.total_marks = tot_marks quest_paper.save() for quest in questions: q = Question.objects.get(id=quest) quest_paper.questions.add(q) return my_redirect('/exam/manage/showquiz') else: no_questions = int(request.POST.get('num_questions')) fetched_questions = fetch_questions(request) n = len(fetched_questions) msg = '' if(no_questions < n): i = n - no_questions for i in range(0, i): fetched_questions.pop() elif(no_questions > n): msg = 'The given Criteria does not satisfy the number of \ Questions...' tags = Tag.objects.all() context = {'data': {'questions': fetched_questions, 'tags': tags, 'msg': msg}} return my_render_to_response( 'exam/automatic_questionpaper.html', context, context_instance=ci) else: tags = Tag.objects.all() context = {'data': {'tags': tags}} return my_render_to_response('exam/automatic_questionpaper.html', context, context_instance=ci)