refactored model for user answer options

This commit is contained in:
tkupek 2016-02-14 17:07:34 +01:00
parent fc15b01d66
commit eeea587ca3
5 changed files with 29 additions and 20 deletions

Binary file not shown.

View file

@ -1,6 +1,6 @@
from django.contrib import admin
from tkupek_elearning.elearning.models import Option, Setting, Question, UserAnswer, User
from tkupek_elearning.elearning.models import Option, Setting, Question, UserAnswer, User, UserAnswerOptions
class OptionInline(admin.TabularInline):
@ -24,4 +24,4 @@ class UserAdmin(admin.ModelAdmin):
admin.site.register(Question, QuestionAdmin)
admin.site.register(Setting),
admin.site.register(User, UserAdmin)
admin.site.register(User, UserAdmin)

View file

@ -56,10 +56,17 @@ class User(models.Model):
class UserAnswer(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=False)
question = models.ForeignKey(Question, on_delete=models.CASCADE, null=False)
answers = models.CharField(max_length=100, null=True)
def __str__(self):
return str(self.user) + " - " + str(self.question)
class Meta:
unique_together = (('user', 'question'),)
class UserAnswerOptions(models.Model):
user_answer = models.ForeignKey(UserAnswer, on_delete=models.CASCADE, null=False)
option = models.ForeignKey(Option, on_delete=models.CASCADE, null=False)
class Meta:
unique_together = (('user_answer', 'option'),)

View file

@ -85,7 +85,7 @@ function get_answer(id) {
var questionId=encodeURIComponent(id);
var token=encodeURIComponent(QueryString.token);
var answers=encodeURIComponent(getCheckboxAnswers(id));
var answers=encodeURIComponent(JSON.stringify(getCheckboxAnswers(id)));
answerRequest.open("GET", "/api?id="+questionId+"&token="+token+"&answers="+answers, true)
answerRequest.send(null)
@ -93,25 +93,21 @@ function get_answer(id) {
function getCheckboxAnswers(id) {
var answers = "";
var answers = [];
var elements = document.getElementsByName("checkbox_" + id);
for(var i = 0; i < elements.length; i++) {
if(elements[i].checked) {
answers += elements[i].value + "_";
answers.push(parseInt(elements[i].value));
}
}
if(answers != "") {
answers = answers.substring(0, answers.length - 1);
}
return answers;
}
function parseResponse(id, responseText) {
var correctOptions = responseText.split("_");
var correctOptions = JSON.parse(responseText);
var elements = document.getElementsByName("checkbox_" + id);

View file

@ -1,11 +1,13 @@
import datetime
import json
from django.core.exceptions import ObjectDoesNotExist
from django.http import HttpResponse
from django.shortcuts import render_to_response
from tkupek_elearning.elearning.models import Setting, Question, Option, UserAnswer, User
from tkupek_elearning.elearning.models import Setting, Question, Option, UserAnswer, User, UserAnswerOptions
# import pdb
import pdb
def start(request):
@ -52,6 +54,7 @@ def get_answer(request):
request_id = request.GET.get('id')
request_token = request.GET.get('token')
request_answers = request.GET.get('answers')
request_answers = json.loads(request_answers)
question = Question.objects.get(id=request_id)
user = User.objects.get(token=request_token)
@ -63,17 +66,20 @@ def get_answer(request):
user_answer = UserAnswer()
user_answer.question = question
user_answer.user = user
user_answer.answers = request_answers
user_answer.save()
for option in request_answers:
user_answer_options = UserAnswerOptions()
user_answer_options.user_answer = user_answer
user_answer_options.option = Option.objects.get(id=option)
user_answer_options.save()
options = Option.objects.filter(question=question.id, correct=True)
options_id = ""
options_id = []
for option in options:
options_id += str(option.id) + "_"
if options_id is not "":
options_id = options_id[:-1]
options_id.append(option.id)
options_id = json.dumps(options_id)
return HttpResponse(options_id)
@ -92,7 +98,7 @@ def log_last_seen(user):
user.save()
def statistic():
def statistic(request):
settings = Setting.objects.get(active=1)
users = User.objects.all()