added correct field to user_answer, revert utf8 fix
utf8 does not seem to make troubles on windows, testing this on linux
This commit is contained in:
parent
41cf0891b5
commit
8340621ce9
3 changed files with 33 additions and 13 deletions
|
@ -16,7 +16,7 @@ class Setting(models.Model):
|
||||||
logo = models.CharField(max_length=256, null=True)
|
logo = models.CharField(max_length=256, null=True)
|
||||||
active = models.BooleanField(unique=True, default=False)
|
active = models.BooleanField(unique=True, default=False)
|
||||||
|
|
||||||
def __unicode__(self):
|
def __str__(self):
|
||||||
return self.title
|
return self.title
|
||||||
|
|
||||||
|
|
||||||
|
@ -26,17 +26,16 @@ class Question(models.Model):
|
||||||
text = models.TextField(null=True)
|
text = models.TextField(null=True)
|
||||||
explanation = models.TextField(null=True)
|
explanation = models.TextField(null=True)
|
||||||
|
|
||||||
def __unicode__(self):
|
def __str__(self):
|
||||||
return self.title
|
return self.title
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Option(models.Model):
|
class Option(models.Model):
|
||||||
text = models.CharField(max_length=100, null=True)
|
text = models.CharField(max_length=100, null=True)
|
||||||
correct = models.BooleanField(null=False, default=False)
|
correct = models.BooleanField(null=False, default=False)
|
||||||
question = models.ForeignKey(Question, on_delete=models.CASCADE, null=False)
|
question = models.ForeignKey(Question, on_delete=models.CASCADE, null=False)
|
||||||
|
|
||||||
def __unicode__(self):
|
def __str__(self):
|
||||||
return self.text
|
return self.text
|
||||||
|
|
||||||
|
|
||||||
|
@ -50,15 +49,16 @@ class User(models.Model):
|
||||||
name = models.CharField(max_length=100, null=False)
|
name = models.CharField(max_length=100, null=False)
|
||||||
last_seen = models.DateTimeField(null=True)
|
last_seen = models.DateTimeField(null=True)
|
||||||
|
|
||||||
def __unicode__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
|
||||||
class UserAnswer(models.Model):
|
class UserAnswer(models.Model):
|
||||||
user = models.ForeignKey(User, on_delete=models.CASCADE, null=False)
|
user = models.ForeignKey(User, on_delete=models.CASCADE, null=False)
|
||||||
question = models.ForeignKey(Question, on_delete=models.CASCADE, null=False)
|
question = models.ForeignKey(Question, on_delete=models.CASCADE, null=False)
|
||||||
|
correct = models.NullBooleanField(null=True)
|
||||||
|
|
||||||
def __unicode__(self):
|
def __str__(self):
|
||||||
return str(self.user) + " - " + str(self.question)
|
return str(self.user) + " - " + str(self.question)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|
|
@ -42,11 +42,15 @@
|
||||||
<tr class="table_headline">
|
<tr class="table_headline">
|
||||||
<td>title</td>
|
<td>title</td>
|
||||||
<td>answers</td>
|
<td>answers</td>
|
||||||
|
<td>correct</td>
|
||||||
|
<td>correct percentage</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% for question in questions %}
|
{% for question in questions %}
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{ question.title }}</td>
|
<td>{{ question.title }}</td>
|
||||||
<td>{{ question.answers }}</td>
|
<td>{{ question.answers }}</td>
|
||||||
|
<td>{{ question.correct_answers }}</td>
|
||||||
|
<td>{{ question.correct_answers_percentage }}</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</table>
|
</table>
|
||||||
|
|
|
@ -60,12 +60,33 @@ def get_answer(request):
|
||||||
user = User.objects.get(token=request_token)
|
user = User.objects.get(token=request_token)
|
||||||
log_last_seen(user)
|
log_last_seen(user)
|
||||||
|
|
||||||
|
correct_options = Option.objects.filter(question=question.id, correct=True)
|
||||||
|
correct_option_checklist = []
|
||||||
|
for option in correct_options:
|
||||||
|
correct_option_checklist.append(option.id)
|
||||||
|
|
||||||
|
options_id = []
|
||||||
|
for option in correct_options:
|
||||||
|
options_id.append(option.id)
|
||||||
|
options_id = json.dumps(options_id)
|
||||||
|
|
||||||
user_answer = get_user_answer(question, user)
|
user_answer = get_user_answer(question, user)
|
||||||
|
|
||||||
if user_answer is None:
|
if user_answer is None:
|
||||||
|
correct = False
|
||||||
|
|
||||||
user_answer = UserAnswer()
|
user_answer = UserAnswer()
|
||||||
user_answer.question = question
|
user_answer.question = question
|
||||||
user_answer.user = user
|
user_answer.user = user
|
||||||
|
|
||||||
|
for option in request_answers:
|
||||||
|
if option in correct_option_checklist:
|
||||||
|
correct_option_checklist.remove(option)
|
||||||
|
|
||||||
|
if len(correct_option_checklist) is 0:
|
||||||
|
correct = True
|
||||||
|
|
||||||
|
user_answer.correct = correct
|
||||||
user_answer.save()
|
user_answer.save()
|
||||||
|
|
||||||
for option in request_answers:
|
for option in request_answers:
|
||||||
|
@ -74,13 +95,6 @@ def get_answer(request):
|
||||||
user_answer_options.option = Option.objects.get(id=option)
|
user_answer_options.option = Option.objects.get(id=option)
|
||||||
user_answer_options.save()
|
user_answer_options.save()
|
||||||
|
|
||||||
options = Option.objects.filter(question=question.id, correct=True)
|
|
||||||
|
|
||||||
options_id = []
|
|
||||||
for option in options:
|
|
||||||
options_id.append(option.id)
|
|
||||||
options_id = json.dumps(options_id)
|
|
||||||
|
|
||||||
return HttpResponse(options_id)
|
return HttpResponse(options_id)
|
||||||
|
|
||||||
|
|
||||||
|
@ -109,5 +123,7 @@ def statistic(request):
|
||||||
questions = Question.objects.all()
|
questions = Question.objects.all()
|
||||||
for question in questions:
|
for question in questions:
|
||||||
question.answers = UserAnswer.objects.filter(question=question.id).count()
|
question.answers = UserAnswer.objects.filter(question=question.id).count()
|
||||||
|
question.correct_answers = UserAnswer.objects.filter(question=question.id, correct=True).count()
|
||||||
|
question.correct_answers_percentage = question.correct_answers / question.answers
|
||||||
|
|
||||||
return render_to_response('statistic.html', {'settings': settings, 'users': users, 'questions': questions})
|
return render_to_response('statistic.html', {'settings': settings, 'users': users, 'questions': questions})
|
||||||
|
|
Loading…
Reference in a new issue