blob: 61de093c57f554617f8d581f2a333e6baa09ce90 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
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.
summary = models.CharField(max_length=256)
# The question text.
description = models.TextField()
# Number of points for the question.
points = models.IntegerField(default=1)
# Test cases for the question in the form of code that is run.
# This is simple Python code.
test = models.TextField()
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()
# 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)
# used to allow/stop a user from retaking the quiz
is_active = models.BooleanField(default = True)
# 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('|')
if len(qs) > 0:
return qs[0]
else:
return ''
def questions_left(self):
"""Returns the number of questions left."""
qs = self.questions
if len(qs) == 0:
return 0
else:
return qs.count('|') + 1
def answered_question(self, question_id):
"""Removes the question from the list of questions and returns
the next."""
qa = self.questions_answered
if len(qa) > 0:
self.questions_answered = '|'.join([qa, str(question_id)])
else:
self.questions_answered = str(question_id)
qs = self.questions.split('|')
qs.remove(unicode(question_id))
self.questions = '|'.join(qs)
self.save()
if len(qs) == 0:
return ''
else:
return qs[0]
def skip(self):
"""Skip the current question and return the next available question."""
qs = self.questions.split('|')
if len(qs) == 0:
return ''
else:
# Put head at the end.
head = qs.pop(0)
qs.append(head)
self.questions = '|'.join(qs)
self.save()
return qs[0]
def __unicode__(self):
u = self.user
return u'Quiz for {0} {1}'.format(u.first_name, u.last_name)
|