summaryrefslogtreecommitdiff
path: root/exam/models.py
diff options
context:
space:
mode:
authorPrabhu Ramachandran2011-11-17 16:40:05 +0530
committerPrabhu Ramachandran2011-11-17 16:40:05 +0530
commit78e18eae416a37fdb2e27d4dde7ed9675692ac7d (patch)
tree4c403239be513d8259dd3810326bbfcdc0adaf23 /exam/models.py
parente2b3d315c3cd3eab79ad74224436b681cbf84371 (diff)
downloadonline_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/models.py')
-rw-r--r--exam/models.py48
1 files changed, 45 insertions, 3 deletions
diff --git a/exam/models.py b/exam/models.py
index 1e8ba69..ea60f17 100644
--- a/exam/models.py
+++ b/exam/models.py
@@ -1,13 +1,14 @@
+import datetime
from django.db import models
from django.contrib.auth.models import User
-
+################################################################################
class Profile(models.Model):
"""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.
@@ -26,6 +27,7 @@ class Question(models.Model):
return self.summary
+################################################################################
class Answer(models.Model):
"""Answers submitted by users.
"""
@@ -41,19 +43,50 @@ class Answer(models.Model):
def __unicode__(self):
return self.answer
+################################################################################
+class Quiz(models.Model):
+ """A quiz that students will participate in. One can think of this
+ as the "examination" event.
+ """
+
+ # The starting/ending date of the quiz.
+ start_date = models.DateField("Date of the quiz")
+ # This is always in minutes.
+ duration = models.IntegerField("Duration of quiz in minutes", default=20)
+
+ # Is the quiz active. The admin should deactivate the quiz once it is
+ # complete.
+ active = models.BooleanField(default=True)
+
+ # Description of quiz.
+ description = models.CharField(max_length=256)
+
+ def __unicode__(self):
+ desc = self.description or 'Quiz'
+ return '%s: on %s for %d minutes'%(desc, self.start_date, self.duration)
+
+
+################################################################################
class QuestionPaper(models.Model):
"""A question paper for a student -- one per student typically.
"""
# The user taking this question paper.
user = models.ForeignKey(User)
+
+ # The Quiz to which this question paper is attached to.
+ quiz = models.ForeignKey(Quiz)
+
+ # The time when this paper was started by the user.
+ start_time = models.DateTimeField()
+
# User's IP which is logged.
user_ip = models.CharField(max_length=15)
# Unused currently.
key = models.CharField(max_length=10)
# used to allow/stop a user from retaking the question paper.
- is_active = models.BooleanField(default = True)
+ active = models.BooleanField(default = True)
# The questions (a list of ids separated by '|')
questions = models.CharField(max_length=128)
@@ -108,7 +141,16 @@ class QuestionPaper(models.Model):
self.questions = '|'.join(qs)
self.save()
return qs[0]
+
+ def time_left(self):
+ """Return the time remaining for the user in seconds."""
+ dt = datetime.datetime.now() - self.start_time
+ secs = dt.total_seconds()
+ total = self.quiz.duration*60.0
+ remain = max(total - secs, 0)
+ return int(remain)
def __unicode__(self):
u = self.user
return u'Question paper for {0} {1}'.format(u.first_name, u.last_name)
+