diff options
author | Prabhu Ramachandran | 2011-11-17 16:40:05 +0530 |
---|---|---|
committer | Prabhu Ramachandran | 2011-11-17 16:40:05 +0530 |
commit | 78e18eae416a37fdb2e27d4dde7ed9675692ac7d (patch) | |
tree | 4c403239be513d8259dd3810326bbfcdc0adaf23 /exam/views.py | |
parent | e2b3d315c3cd3eab79ad74224436b681cbf84371 (diff) | |
download | online_test-78e18eae416a37fdb2e27d4dde7ed9675692ac7d.tar.gz online_test-78e18eae416a37fdb2e27d4dde7ed9675692ac7d.tar.bz2 online_test-78e18eae416a37fdb2e27d4dde7ed9675692ac7d.zip |
ENH: Added a Quiz model and time based papers
Added a model called Quiz which allows the admin to define a quiz on a
date which can be active or deactivated. Users can take a quiz if one
is active and are given a fixed duration to complete the quiz. They may
login as many times as needed to finish it but the total time is logged.
Javascript code updates a timer on the page.
Diffstat (limited to 'exam/views.py')
-rw-r--r-- | exam/views.py | 60 |
1 files changed, 40 insertions, 20 deletions
diff --git a/exam/views.py b/exam/views.py index 0c48f08..ad8a04f 100644 --- a/exam/views.py +++ b/exam/views.py @@ -3,11 +3,12 @@ import string import os import stat from os.path import dirname, pardir, abspath, join, exists +import datetime 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, QuestionPaper, Profile, Answer +from exam.models import Quiz, Question, QuestionPaper, Profile, Answer from exam.forms import UserRegisterForm, UserLoginForm from exam.xmlrpc_clients import python_server @@ -26,7 +27,6 @@ def get_user_dir(user): def index(request): """The start page. """ - # Largely copied from Nishanth's QuestionPaper app. user = request.user if user.is_authenticated(): return redirect("/exam/start/") @@ -82,25 +82,25 @@ def user_login(request): return render_to_response('exam/login.html', context, 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: - return question(request, q_id) - def start(request): user = request.user try: - old_paper = QuestionPaper.objects.get(user=user) - if not old_paper.is_active: - return redirect("/exam/complete/") + # 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) p = old_paper.current_question() return redirect('/exam/%s'%p) except QuestionPaper.DoesNotExist: ip = request.META['REMOTE_ADDR'] key = gen_key(10) - new_paper = QuestionPaper(user=user, user_ip=ip, key=key) + new_paper = QuestionPaper(user=user, user_ip=ip, key=key, quiz=quiz) + new_paper.start_time = datetime.datetime.now() # Make user directory. user_dir = get_user_dir(user) @@ -130,11 +130,25 @@ def question(request, q_id): paper = QuestionPaper.objects.get(user=request.user) except QuestionPaper.DoesNotExist: redirect('/exam/start') - context = {'question': q, 'paper': paper, 'user': user} + time_left = paper.time_left() + if time_left == 0: + return complete(request, reason='Your time is up!') + quiz_name = paper.quiz.description + context = {'question': q, 'paper': paper, 'user': user, + 'quiz_name': quiz_name, + 'time_left': time_left} ci = RequestContext(request) return render_to_response('exam/question.html', context, context_instance=ci) +def show_question(request, q_id): + """Show a question if possible.""" + if len(q_id) == 0: + msg = 'Congratulations! You have successfully completed the quiz.' + return complete(request, msg) + else: + return question(request, q_id) + def check(request, q_id): user = request.user question = get_object_or_404(Question, pk=q_id) @@ -164,8 +178,14 @@ def check(request, q_id): ci = RequestContext(request) if not success: + time_left = paper.time_left() + if time_left == 0: + return complete(request, reason='Your time is up!') + context = {'question': question, 'error_message': err_msg, - 'paper': paper, 'last_attempt': answer} + 'paper': paper, 'last_attempt': answer, + 'time_left': time_left} + return render_to_response('exam/question.html', context, context_instance=ci) else: @@ -176,17 +196,17 @@ def quit(request): return render_to_response('exam/quit.html', context_instance=RequestContext(request)) -def complete(request): +def complete(request, reason=None): user = request.user yes = True + message = reason or 'The quiz has been completed. Thank you.' if request.method == 'POST': yes = request.POST.get('yes', None) if yes: - paper = QuestionPaper.objects.get(user=user) - paper.is_active = False - paper.save() + # Logout the user and quit with the message given. logout(request) - return render_to_response('exam/complete.html') + context = {'message': message} + return render_to_response('exam/complete.html', context) else: return redirect('/exam/') |