From eeea587ca3260d592e26aa07850a8d1c69a48e75 Mon Sep 17 00:00:00 2001 From: tkupek Date: Sun, 14 Feb 2016 17:07:34 +0100 Subject: [PATCH] refactored model for user answer options --- db.sqlite3 | Bin 70656 -> 70656 bytes tkupek_elearning/elearning/admin.py | 4 +-- tkupek_elearning/elearning/models.py | 9 ++++++- .../elearning/static/js/elearning.js | 12 +++------ tkupek_elearning/elearning/views.py | 24 +++++++++++------- 5 files changed, 29 insertions(+), 20 deletions(-) diff --git a/db.sqlite3 b/db.sqlite3 index bbf6c742dbf15594fda6cfa5c9aa16d3a442d5cb..c426d5190ed11aaccb7194ad4cb830b82f967932 100644 GIT binary patch delta 2473 zcmbtVdu)?c6upLBuu)%tgosh%18dpJ7_x3`9ZW@K z8_`7n5OTrc9|j}Q5EEFSF(&Gw8^#!jW*Rl3n<1z;mO+>T0U6A5zivx$BhlEe=iKk! zdw%Dfdmi`Oc}UuMNZOsZfaVfH?m_Y)X^`IAQz|o>d-SOtpl!zaosF?GP$U(xK9a)r z>)3uMWoOyf@CF262y)mI`v5xG=2)h*l1V7!*i)BZj$;~Pr+?WD%4x&qP^7UX*xc0E z7K%2tMA{4BQIaGia&3G_Zz{KBX<-QsbwoD>Tf=S5P3`R{WjATSJ%g=piA2MZXfXOr zYuLc2^vkR5U`&BB8r~8PwMCjDjlq^yq1yn5NYRXSF+w-FBVk@t3<1hB2aL<&^6ipA z4@JqU?T+?v8-X#hC_!FTF-Efgu|x~;3WJA{qPp7fG42?DxiSffLAdr zBTT~Zm2*qFF=^VIffuCzj<`tz=iwBbfREt-yaG{J5BGxt`D_w#!^~W0kVsMa?CFGS zp-B7oaND#Vo}V=ecACm{#(=f)8XURefEc0ZI*amD^NqjV%p)?nK!Vu8aC~Rd}#+ zD2XkN{=#yFfL;~WtHOG#x?Z4>tt~e3az;`X?f|9}uHn`gd?Owd7=Zkl0x?ZzHC>Ea zi6&FY!Y6oEhL+q*@c$)!i5_Ql(9bg^IriP&Ng7)|Hb@Uj`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HL!!g^Pcrln}#DxP`0u0-xYLq_GE2A%>7?ED9C!&7g|6D$U<$8ewj= z20nTORj5;1Jq6~4Ro6HgK_%i!OK)-Y<2=ry7ay3$J)z>=BebI{?NHn_R>RWYIOi+^ z=Ik=-@i%_QoK-4wu;!0=ok4uZ3UHO-9_Far>r~GyVU#jVtgFx@=Ul?SY=y5tau=7S z#N@Q;mcufr=9I6Y3VO>iGGG@ ze2$Yigy#^2M@)%+(@?_dA9%)lo=YxEyrP$Z4KOyqUlC5xn=w9i8}-j?K|RKeZZ(}P zS4Xn})s#KPcdJ|3Q+$u=7&$H(;|bHy`H4bl$*=xF5VIu8B*XV4*SDC&Z}^2|xlW=C zqcO;s1DwNeKZQ@i#ce?fmcx=pi6kqt!vQyDh-iZ0FU%u{DNN8?FsyqJ)#QU~+7cumVD z`a0&fo3*m4sBu9D^HP~oD_eGi7NDbaKn&gZ6-Cjl(dgFL<_)#6NGuY)K;mk$+C>Zz z!Ztz!p^nfbET2ywz@f}OL`icXDVjh78Fe2KTPO)Q9u;&oBW7x^$h%pYfW*&<}V0YPhA|%hrKV<xjnJ>OjLK7?h1%ALB?*IS* 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()