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 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)