diff --git a/db.sqlite3 b/db.sqlite3 index bbf6c74..c426d51 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ diff --git a/tkupek_elearning/elearning/admin.py b/tkupek_elearning/elearning/admin.py index 5d349c4..8e41476 100644 --- a/tkupek_elearning/elearning/admin.py +++ b/tkupek_elearning/elearning/admin.py @@ -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) \ No newline at end of file diff --git a/tkupek_elearning/elearning/models.py b/tkupek_elearning/elearning/models.py index 44143f5..736f27e 100644 --- a/tkupek_elearning/elearning/models.py +++ b/tkupek_elearning/elearning/models.py @@ -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'),) diff --git a/tkupek_elearning/elearning/static/js/elearning.js b/tkupek_elearning/elearning/static/js/elearning.js index 12a0171..02c9ad1 100644 --- a/tkupek_elearning/elearning/static/js/elearning.js +++ b/tkupek_elearning/elearning/static/js/elearning.js @@ -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); diff --git a/tkupek_elearning/elearning/views.py b/tkupek_elearning/elearning/views.py index 25ec108..72a4c89 100644 --- a/tkupek_elearning/elearning/views.py +++ b/tkupek_elearning/elearning/views.py @@ -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()