From 214d8696fafee4e38c6bf039315aac37e4cd2e2b Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Tue, 21 Jul 2015 18:02:56 +0530 Subject: Add filters to question display --- testapp/exam/forms.py | 13 ++++ testapp/exam/static/exam/js/question_filter.js | 69 ++++++++++++++++++++++ .../exam/templates/exam/ajax_question_filter.html | 15 +++++ testapp/exam/templates/exam/showquestions.html | 23 ++++++++ testapp/exam/urls.py | 2 + testapp/exam/views.py | 42 +++++++++++-- 6 files changed, 160 insertions(+), 4 deletions(-) create mode 100644 testapp/exam/static/exam/js/question_filter.js create mode 100644 testapp/exam/templates/exam/ajax_question_filter.html diff --git a/testapp/exam/forms.py b/testapp/exam/forms.py index b7625be..1c61816 100644 --- a/testapp/exam/forms.py +++ b/testapp/exam/forms.py @@ -240,5 +240,18 @@ class RandomQuestionForm(forms.Form): (choices=(('select', 'Select Marks'),))) shuffle_questions = forms.BooleanField(required=False) + +class QuestionFilterForm(forms.Form): + questions = Question.objects.all() + points_list = questions.values_list('points', flat=True).distinct() + points_options = [(i, i) for i in points_list] + + language = forms.CharField(max_length=8, widget=forms.Select\ + (choices=languages)) + question_type = forms.CharField(max_length=8, widget=forms.Select\ + (choices=question_types)) + marks = forms.FloatField(widget=forms.Select(choices=points_options)) + + TestCaseFormSet = inlineformset_factory(Question, TestCase,\ can_order=False, can_delete=False, extra=1) diff --git a/testapp/exam/static/exam/js/question_filter.js b/testapp/exam/static/exam/js/question_filter.js new file mode 100644 index 0000000..7dc92a6 --- /dev/null +++ b/testapp/exam/static/exam/js/question_filter.js @@ -0,0 +1,69 @@ +$(document).ready(function(){ + $question_type = $("#id_question_type"); + $marks = $("#id_marks"); + $language = $("#id_language"); + + $question_type.change(function() { + $.ajax({ + url: "/exam/ajax/questions/filter/", + type: "POST", + data: { + question_type: $question_type.val(), + marks: $marks.val(), + language: $language.val() + }, + dataType: "html", + success: function(output) { + var questions = $(output).filter("#questions").html(); + $("#filtered-questions").html(questions); + } + }); + }); + + $language.change(function() { + $.ajax({ + url: "/exam/ajax/questions/filter/", + type: "POST", + data: { + question_type: $question_type.val(), + marks: $marks.val(), + language: $language.val() + }, + dataType: "html", + success: function(output) { + var questions = $(output).filter("#questions").html(); + $("#filtered-questions").html(questions); + } + }); + }); + + $marks.change(function() { + $.ajax({ + url: "/exam/ajax/questions/filter/", + type: "POST", + data: { + question_type: $question_type.val(), + marks: $marks.val(), + language: $language.val() + }, + dataType: "html", + success: function(output) { + var questions = $(output).filter("#questions").html(); + $("#filtered-questions").html(questions); + } + }); + }); + + $("#checkall").live("click", function(){ + if($(this).attr("checked")) { + $("#filtered-questions input:checkbox").each(function(index, element) { + $(this).attr('checked','checked'); + }); + } + else { + $("#filtered_questions input:checkbox").each(function(index, element) { + $(this).removeAttr('checked'); + }); + } + }); +}); \ No newline at end of file diff --git a/testapp/exam/templates/exam/ajax_question_filter.html b/testapp/exam/templates/exam/ajax_question_filter.html new file mode 100644 index 0000000..11bf660 --- /dev/null +++ b/testapp/exam/templates/exam/ajax_question_filter.html @@ -0,0 +1,15 @@ +
+ {% if questions %} +
Select All
+ {% endif %} + +
diff --git a/testapp/exam/templates/exam/showquestions.html b/testapp/exam/templates/exam/showquestions.html index 62b40e4..d593ab8 100644 --- a/testapp/exam/templates/exam/showquestions.html +++ b/testapp/exam/templates/exam/showquestions.html @@ -4,15 +4,38 @@ {% block subtitle %}List of Questions {% endblock %} {% block script %} + + {% endblock %} {% block manage %}
{% csrf_token %} +
+
Filters
+
+ {{ form.question_type }} +
+
+ {{ form.language }} +
+
+ {{ form.marks }} +
+
+
+
+
+ +
+
+
+
{% for i in questions %}   {{ i }}
{% endfor %} +

      diff --git a/testapp/exam/urls.py b/testapp/exam/urls.py index 43970ce..c87df9a 100644 --- a/testapp/exam/urls.py +++ b/testapp/exam/urls.py @@ -47,4 +47,6 @@ urlpatterns = patterns('testapp.exam.views', url(r'^manage/designquestionpaper/manual/(?P\d+)/$',\ 'manual_questionpaper'), url(r'^ajax/questionpaper/(?P.+)/$', 'ajax_questionpaper'), + url(r'^ajax/questions/filter/$', 'ajax_questions_filter'), ##@@ + ) diff --git a/testapp/exam/views.py b/testapp/exam/views.py index 69d16d7..e1c9dc5 100644 --- a/testapp/exam/views.py +++ b/testapp/exam/views.py @@ -19,7 +19,8 @@ import json from testapp.exam.models import Quiz, Question, QuestionPaper, QuestionSet from testapp.exam.models import Profile, Answer, AnswerPaper, User, TestCase from testapp.exam.forms import UserRegisterForm, UserLoginForm, QuizForm,\ - QuestionForm, RandomQuestionForm, TestCaseFormSet + QuestionForm, RandomQuestionForm, TestCaseFormSet,\ + QuestionFilterForm from testapp.exam.xmlrpc_clients import code_server from settings import URL_ROOT from testapp.exam.models import AssignmentUpload @@ -1249,6 +1250,30 @@ def show_all_quiz(request): context_instance=ci) +@csrf_exempt +def ajax_questions_filter(request): + """Ajax call made when filtering displayed questions.""" + + filter_dict = {} + question_type = request.POST.get('question_type') + marks = request.POST.get('marks') + language = request.POST.get('language') + + if question_type != "select": + filter_dict['type'] = str(question_type) + + if marks != "": + filter_dict['points'] = marks + + if language != "select": + filter_dict['language'] = str(language) + + questions = list(Question.objects.filter(**filter_dict)) + + return my_render_to_response('exam/ajax_question_filter.html', + {'questions': questions}) + + def show_all_questions(request): """Show a list of all the questions currently in the databse.""" @@ -1261,18 +1286,24 @@ def show_all_questions(request): data = request.POST.getlist('question') if data is None: questions = Question.objects.all() + form = QuestionFilterForm() context = {'papers': [], 'question': None, - 'questions': questions} + 'questions': questions, + 'form': form + } return my_render_to_response('exam/showquestions.html', context, context_instance=ci) else: for i in data: question = Question.objects.get(id=i).delete() questions = Question.objects.all() + form = QuestionFilterForm() context = {'papers': [], 'question': None, - 'questions': questions} + 'questions': questions, + 'form': form + } return my_render_to_response('exam/showquestions.html', context, context_instance=ci) elif request.method == 'POST' and request.POST.get('edit') == 'edit': @@ -1312,9 +1343,12 @@ def show_all_questions(request): context_instance=ci) else: questions = Question.objects.all() + form = QuestionFilterForm() context = {'papers': [], 'question': None, - 'questions': questions} + 'questions': questions, + 'form': form + } return my_render_to_response('exam/showquestions.html', context, context_instance=ci) -- cgit