diff --git a/db.sqlite3 b/db.sqlite3 index 4978da7..875621c 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ diff --git a/tkupek_elearning/elearning/models.py b/tkupek_elearning/elearning/models.py index f90d6b6..d1e55c4 100644 --- a/tkupek_elearning/elearning/models.py +++ b/tkupek_elearning/elearning/models.py @@ -47,7 +47,10 @@ class User(models.Model): class UserAnswer(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE, null=False) questionId = models.ForeignKey(Question, on_delete=models.CASCADE, null=False) - answers = models.ForeignKey(Option, on_delete=models.CASCADE, null=False) + answers = models.CharField(max_length=100, null=True) + + def __str__(self): + return str(self.user) + " - " + str(self.questionId) class Meta: unique_together = (('user', 'questionId'),) \ No newline at end of file diff --git a/tkupek_elearning/elearning/views.py b/tkupek_elearning/elearning/views.py index bdffe0e..bec9e7b 100644 --- a/tkupek_elearning/elearning/views.py +++ b/tkupek_elearning/elearning/views.py @@ -1,3 +1,4 @@ +from django.core.exceptions import ObjectDoesNotExist from django.http import HttpResponse from django.shortcuts import render_to_response @@ -5,8 +6,8 @@ from tkupek_elearning.elearning.models import Setting, Question, Option, UserAns import pdb -def home(request): +def home(request): settings = Setting.objects.filter(active=1) if settings: settings = settings[0] @@ -21,26 +22,27 @@ def home(request): def getAnswer(request): + if request.method == 'GET': - # if request.method == 'GET': - # pdb.set_trace() - # request_questionId = request.GET.get('id') - # request_answers = request.GET.get('answers') - # request_userToken = request.GET.get('user') - # - # question = Question.objects.filter(questionId=request_questionId) - # user = User.objects.filter(token=request_userToken) - # - # userAnswer = UserAnswer.objects.filter(questionId=question.id, user=user.token) - # if not userAnswer: - # userAnswer = UserAnswer() - # userAnswer.questionId = question - # userAnswer.user = user - # userAnswer.answers = "" - # userAnswer.save() - # else: - # return HttpResponse('error:alreadyAnswered'); - # - # options = Option.objects.filter(question=question.id) - # - # return HttpResponse(options) + request_id = request.GET.get('id') + request_token = request.GET.get('token') + request_answers = request.GET.get('answers') + + question = Question.objects.get(id=request_id) + user = User.objects.get(token=request_token) + + try: + userAnswer = UserAnswer.objects.get(questionId=question.id, user=user.id) + except ObjectDoesNotExist: + userAnswer = None + + if userAnswer is None: + userAnswer = UserAnswer() + userAnswer.questionId = question + userAnswer.user = user + userAnswer.answers = request_answers + userAnswer.save() + + options = Option.objects.filter(question=question.id, correct=True) + + return HttpResponse(options)