From 712f11df006e8100aeea8bbe771ba0c136732f5e Mon Sep 17 00:00:00 2001 From: Tom Walker Date: Fri, 25 Jul 2014 23:23:13 +0100 Subject: [PATCH] added Essay question to admin, changed templates so they dont show previous question if it was an essay question since it is not marked automatically, updated sitting_detail view to show the answers that the user gave for each question. next up - allow teacher to mark questions in this view, resulting in updated score etc --- essay/models.py | 2 +- essay/tests.py | 2 +- quiz/admin.py | 10 ++++- quiz/models.py | 7 +-- quiz/templates/correct_answer.html | 57 +++++++++++++------------ quiz/templates/question.html | 11 +++-- quiz/templates/quiz/sitting_detail.html | 6 ++- quiz/templates/result.html | 2 +- quiz/tests.py | 4 ++ quiz/views.py | 21 +++++---- 10 files changed, 72 insertions(+), 50 deletions(-) diff --git a/essay/models.py b/essay/models.py index 1a077c9..96a6b43 100644 --- a/essay/models.py +++ b/essay/models.py @@ -3,7 +3,7 @@ from quiz.models import Question class Essay_Question(Question): - def check_if_correct(self): + def check_if_correct(self, guess): return False def get_answers(self): diff --git a/essay/tests.py b/essay/tests.py index 6f56d07..c261a45 100644 --- a/essay/tests.py +++ b/essay/tests.py @@ -9,7 +9,7 @@ class TestEssayQuestionModel(TestCase): explanation="Wow!") def test_always_false(self): - self.assertEqual(self.essay.check_if_correct(), False) + self.assertEqual(self.essay.check_if_correct('spam'), False) self.assertEqual(self.essay.get_answers(), False) self.assertEqual(self.essay.get_answers_list(), False) diff --git a/quiz/admin.py b/quiz/admin.py index 7760b9f..1ba051f 100644 --- a/quiz/admin.py +++ b/quiz/admin.py @@ -5,7 +5,7 @@ from django.contrib.admin.widgets import FilteredSelectMultiple from .models import Quiz, Category, Progress, Question from multichoice.models import MCQuestion, Answer from true_false.models import TF_Question - +from essay.models import Essay_Question class AnswerInline(admin.TabularInline): model = Answer @@ -82,8 +82,16 @@ class TFQuestionAdmin(admin.ModelAdmin): search_fields = ('content', 'explanation') filter_horizontal = ('quiz',) +class EssayQuestionAdmin(admin.ModelAdmin): + list_display = ('content', 'category', ) + list_filter = ('category',) + fields = ('content', 'category', 'quiz', 'explanation', ) + search_fields = ('content', 'explanation') + filter_horizontal = ('quiz',) + admin.site.register(Quiz, QuizAdmin) admin.site.register(Category, CategoryAdmin) admin.site.register(MCQuestion, MCQuestionAdmin) admin.site.register(Progress, ProgressAdmin) admin.site.register(TF_Question, TFQuestionAdmin) +admin.site.register(Essay_Question, EssayQuestionAdmin) diff --git a/quiz/models.py b/quiz/models.py index 93e0b05..97d64f0 100644 --- a/quiz/models.py +++ b/quiz/models.py @@ -67,7 +67,8 @@ class Quiz(models.Model): default=False, help_text="If yes, the result of each" " attempt by a user will be" - " stored.") + " stored. Necessary for" + " marking.") single_attempt = models.BooleanField(blank=False, default=False, @@ -421,12 +422,12 @@ class Sitting(models.Model): current = json.loads(self.user_answers) current[question.id] = guess self.user_answers = json.dumps(current) + self.save() def questions_with_user_answers(self): output = {} user_answers = json.loads(self.user_answers) - questions = self.quiz.question_set.all().select_subclasses() - for question in questions: + for question in self.quiz.question_set.all().select_subclasses(): output[question] = user_answers[unicode(question.id)] return output diff --git a/quiz/templates/correct_answer.html b/quiz/templates/correct_answer.html index b24eeb8..49f7c36 100644 --- a/quiz/templates/correct_answer.html +++ b/quiz/templates/correct_answer.html @@ -1,28 +1,31 @@ -{% if user_was_incorrect %} -
- You answered the above question incorrectly -
-{% endif %} +{% if previous.answers %} + + {% if user_was_incorrect %} +
+ You answered the above question incorrectly +
+ {% endif %} - - - {% for answer in previous.answers %} - {% if answer.correct %} - - - - {% else %} - - - - {% endif %} - - {% endfor %} - -
{{ answer.content }}This is the correct answer
{{ answer.content }} - {% if previous.question_type.MCQuestion %} - {% if answer.id|add:"0" == previous.previous_answer|add:"0" %} - This was your answer. - {% endif %} - {% endif %} -
+ + + {% for answer in previous.answers %} + {% if answer.correct %} + + + + {% else %} + + + + {% endif %} + + {% endfor %} + +
{{ answer.content }}This is the correct answer
{{ answer.content }} + {% if previous.question_type.MCQuestion %} + {% if answer.id|add:"0" == previous.previous_answer|add:"0" %} + This was your answer. + {% endif %} + {% endif %} +
+{% endif %} diff --git a/quiz/templates/question.html b/quiz/templates/question.html index 6890965..b980c74 100644 --- a/quiz/templates/question.html +++ b/quiz/templates/question.html @@ -53,12 +53,11 @@ diff --git a/quiz/templates/quiz/sitting_detail.html b/quiz/templates/quiz/sitting_detail.html index 65e8fc7..a685b9a 100644 --- a/quiz/templates/quiz/sitting_detail.html +++ b/quiz/templates/quiz/sitting_detail.html @@ -16,15 +16,17 @@ Result of {{ sitting.quiz.title }} for {{ sitting.user }} Question + User answer - +n -{% for question in questions %} +{% for question, user_name in questions.items %} {{ question.content }} + {{ user_name }} {% if question.id in incorrect %}

incorrect

diff --git a/quiz/templates/result.html b/quiz/templates/result.html index efb8cb8..4ed25ff 100644 --- a/quiz/templates/result.html +++ b/quiz/templates/result.html @@ -7,7 +7,7 @@ {% block content %} - {% if previous %} + {% if previous.answers %}

The previous question:

{{ previous.previous_question }}

diff --git a/quiz/tests.py b/quiz/tests.py index 195c5e3..0e4a2f7 100644 --- a/quiz/tests.py +++ b/quiz/tests.py @@ -399,6 +399,9 @@ class TestQuestionMarking(TestCase): sitting2.complete = True sitting2.save() + sitting1.add_user_answer(self.question1, '123') + # sitting1.add_user_answer(self.question2, '456') + def test_paper_marking_list_view(self): response = self.client.get('/marking/') self.assertRedirects(response, 'accounts/login/?next=/marking/', @@ -449,6 +452,7 @@ class TestQuestionMarking(TestCase): def test_paper_marking_detail_view(self): self.client.login(username='yoda', password='use_d@_force') response = self.client.get('/marking/1/') + self.assertContains(response, 'test quiz 1') self.assertContains(response, 'squawk') self.assertContains(response, 'incorrect') diff --git a/quiz/views.py b/quiz/views.py index fafc504..aba7d5e 100644 --- a/quiz/views.py +++ b/quiz/views.py @@ -8,7 +8,8 @@ from django.views.generic import DetailView, ListView, TemplateView, FormView from .forms import QuestionForm, EssayForm from .models import Quiz, Category, Progress, Sitting, Question -from Essay_Question.models import Essay_Question +from essay.models import Essay_Question + class QuizMarkerMixin(object): @method_decorator(login_required) @@ -100,7 +101,8 @@ class QuizMarkingDetail(QuizMarkerMixin, DetailView): def get_context_data(self, **kwargs): context = super(QuizMarkingDetail, self).get_context_data(**kwargs) - context['questions'] = context['object'].quiz.question_set.all() + # context['questions'] = context['object'].quiz.question_set.all() + context['questions'] = context['object'].questions_with_user_answers() context['incorrect'] = context['object'].get_incorrect_questions return context @@ -113,27 +115,29 @@ class QuizTake(FormView): self.quiz = get_object_or_404(Quiz, url=self.kwargs['quiz_name']) if request.user.is_authenticated() is True: - self.sitting = user_sitting(self.request, self.quiz) + self.sitting = user_sitting(request, self.quiz) else: - self.sitting = anon_load_sitting(self.request, self.quiz) + self.sitting = anon_load_sitting(request, self.quiz) if self.sitting is False: return render(request, 'single_complete.html') + return super(QuizTake, self).dispatch(request, *args, **kwargs) + + def get_form(self, form_class): if self.request.user.is_authenticated() is True: self.question = self.sitting.get_first_question() else: self.question = anon_next_question(self) - return super(QuizTake, self).dispatch(self.request, *args, **kwargs) - - def get_form(self, form_class): if self.question.__class__ is Essay_Question: form_class = EssayForm + return form_class(**self.get_form_kwargs()) def get_form_kwargs(self): kwargs = super(QuizTake, self).get_form_kwargs() + return dict(kwargs, question=self.question) def form_valid(self, form): @@ -205,6 +209,7 @@ def form_valid_user(self, form): else: self.previous = {} + self.sitting.add_user_answer(self.question, guess) self.sitting.remove_first_question() @@ -295,7 +300,7 @@ def form_valid_anon(self, form): else: self.previous = {} self.request.session[self.quiz.anon_q_list()] =\ - (self.request.session[self.quiz.anon_q_list()][1:]) + self.request.session[self.quiz.anon_q_list()][1:] def anon_session_score(session, to_add=0, possible=0): -- 2.39.5