From 6cf7eb9f1a69f596ea0dcc418c1b90d3fb8ec513 Mon Sep 17 00:00:00 2001 From: Prabhu Ramachandran Date: Wed, 9 Nov 2011 03:05:03 +0530 Subject: ENH: Saving answers and added quit page/button. Cleaned up the models so the answers submitted are all saved. Also added a quit button to each question page so a user can easily quit the exam in order that another may start. --- exam/models.py | 29 +++++++++++++++++++---------- exam/urls.py | 1 + exam/views.py | 30 ++++++++++++++++++++---------- 3 files changed, 40 insertions(+), 20 deletions(-) (limited to 'exam') diff --git a/exam/models.py b/exam/models.py index 3a8121b..226146a 100644 --- a/exam/models.py +++ b/exam/models.py @@ -1,13 +1,13 @@ from django.db import models from django.contrib.auth.models import User -# Create your models here. class Profile(models.Model): - """Profile for a user to store roll number etc.""" + """Profile for a user to store roll number and other details.""" user = models.ForeignKey(User) roll_number = models.CharField(max_length=20) + class Question(models.Model): """A question in the database.""" # An optional one-line summary of the question. @@ -16,7 +16,7 @@ class Question(models.Model): description = models.TextField() # Number of points for the question. - points = models.IntegerField() + points = models.IntegerField(default=1) # Test cases for the question in the form of code that is run. # This is simple Python code. @@ -24,33 +24,42 @@ class Question(models.Model): def __unicode__(self): return self.summary - + class Answer(models.Model): """Answers submitted by users. """ + # The question for which we are an answer. question = models.ForeignKey(Question) + # The last answer submitted by the user. answer = models.TextField() - attempts = models.IntegerField() - - # Is the question correct. - correct = models.BooleanField() - # Marks obtained. - marks = models.IntegerField() + # Is the answer correct. + correct = models.BooleanField(default=False) + def __unicode__(self): return self.answer + class Quiz(models.Model): """A quiz for a student. """ + # The user taking this quiz. user = models.ForeignKey(User) + # User's IP which is logged. user_ip = models.CharField(max_length=15) + # Unused currently. key = models.CharField(max_length=10) + + # The questions (a list of ids separated by '|') questions = models.CharField(max_length=128) + # The questions successfully answered (a list of ids separated by '|') questions_answered = models.CharField(max_length=128) + # All the submitted answers. + answers = models.ManyToManyField(Answer) + def current_question(self): """Returns the current active question to display.""" qs = self.questions.split('|') diff --git a/exam/urls.py b/exam/urls.py index 7922255..cfb4764 100644 --- a/exam/urls.py +++ b/exam/urls.py @@ -3,6 +3,7 @@ from django.conf.urls.defaults import patterns, include, url urlpatterns = patterns('exam.views', url(r'^$', 'index'), url(r'^start/$', 'start'), + url(r'^quit/$', 'quit'), url(r'^complete/$', 'complete'), url(r'^(?P\d+)/$', 'question'), url(r'^(?P\d+)/check/$', 'check'), diff --git a/exam/views.py b/exam/views.py index 69d7838..0dc02cc 100644 --- a/exam/views.py +++ b/exam/views.py @@ -8,19 +8,13 @@ from django.contrib.auth.models import User from django.contrib.auth import login, logout, authenticate from django.shortcuts import render_to_response, get_object_or_404, redirect from django.template import RequestContext -from exam.models import Question, Quiz, Profile +from exam.models import Question, Quiz, Profile, Answer from exam.forms import UserRegisterForm def gen_key(no_of_chars): + """Generate a random key of the number of characters.""" allowed_chars = string.digits+string.uppercase return ''.join([random.choice(allowed_chars) for i in range(no_of_chars)]) - -def index_old(request): - """The start page. - """ - question_list = Question.objects.all() - context = {'question_list': question_list} - return render_to_response('exam/index.html', context) def index(request): """The start page. @@ -64,6 +58,7 @@ def index(request): context_instance=RequestContext(request)) def show_question(request, q_id): + """Show a question if possible.""" if len(q_id) == 0: return redirect("/exam/complete") else: @@ -137,6 +132,11 @@ def check(request, q_id): retry = False err = 'Correct answer' finally: + # Add the answer submitted. + new_answer = Answer(question=question, answer=answer.strip()) + new_answer.correct = not retry + new_answer.save() + quiz.answers.add(new_answer) del tb ci = RequestContext(request) @@ -148,8 +148,18 @@ def check(request, q_id): else: next_q = quiz.answered_question(question.id) return show_question(request, next_q) + +def quit(request): + return render_to_response('exam/quit.html', + context_instance=RequestContext(request)) def complete(request): - logout(request) - return render_to_response('exam/complete.html') + yes = True + if request.method == 'POST': + yes = request.POST.get('yes', None) + if yes: + logout(request) + return render_to_response('exam/complete.html') + else: + return redirect('/exam/') -- cgit