From 6614bf8c2666d98cc74a4ea6f8069919e6a7b470 Mon Sep 17 00:00:00 2001 From: tkupek Date: Tue, 9 Feb 2016 19:51:05 +0100 Subject: [PATCH] added ajax request to client side --- .idea/.name | 2 +- .idea/misc.xml | 18 +----- db.sqlite3 | Bin 50176 -> 70656 bytes tkupek_elearning/elearning/admin.py | 6 +- tkupek_elearning/elearning/models.py | 22 ++++++- .../elearning/static/js/elearning.js | 57 ++++++++++++++++++ .../elearning/templates/index.html | 4 +- tkupek_elearning/elearning/views.py | 31 +++++++++- tkupek_elearning/urls.py | 5 +- 9 files changed, 118 insertions(+), 27 deletions(-) diff --git a/.idea/.name b/.idea/.name index f2ec957..8ade273 100644 --- a/.idea/.name +++ b/.idea/.name @@ -1 +1 @@ -vcpby-elearning \ No newline at end of file +tkupek-elearning \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index da6f99e..b83ca74 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -10,23 +10,7 @@ - + - - - - - Python 2.7.6 (/usr/bin/python2.7) - - - - - - - \ No newline at end of file diff --git a/db.sqlite3 b/db.sqlite3 index 713f141758864270830666a7315508568ad372d1..4978da742e8e875d4b7afad528d40e5c397f1aea 100644 GIT binary patch delta 3866 zcma)84QyLi6~6bK=h%LBnx;YJy96ifEzOMC2gwO<`7$Fa#y~Tbx@FW_z&N#?77hEA6 zMd2p=9d5vf@F(~qyaiWE#vYKxVY2lxs<=TSEYL^TIzM4tD`9*OVNMT4!Elps6o;GT z*2%vC-`3I$yAj9SX5kq0LmFOy@4z^`4u1h3a~sZ-@}XWeb^2U z!2+nz4c{yox0(L6Y;Ry0j7G$r2 zfv%GAU#8#hVNq{j$%kdYR*r16*KC-RzVi^kv!=5x>cy|)m+(*U6S#n9OGY;?_*p!N zJt?A**R25Ajs@)X>3t+g)$4p4i$mC>rBYSGy{#->kGU$49($!qy?YglyD?W$?DDaA zBX+0C*V}u7U0yhk;1>J~{y|FjI$R@BybQmFSKuO?FSmAm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_IZo^M(NAB=c z0eP#;w~x0M=)4dN^OjS*{r2%eI#ARt(upG&b}b!Q<|Q)KNeNjE$D?sUjTuc%#PGOn zSpJA7%`#@&G;bW|?zIMzI(u|vsaqr?O|=gqFGdrJnvh~)RaIhAtlWA0I_7BdF}#7` z4KnM!0-O687VYR3bxlp_YJ}ujv+|tMBGq}`kqh&?(wY5K8{k1ihUzEa!IJSwC=4~b zQA;-+!^fnG*itQIv^jlcLL@7lvJ{WT)Il#%ZnsK7qP1Wje?{#`QbJV1k{lNzvg#oU ji}|-QztOI~hKgjmmgC{5AkovePQLFf^OM(V*&+6S`FTpZ delta 709 zcmXw0TSydP6#oDDW@gt}FKdo&?1pA&5osnGJxHJ_=z>xYy+oIF&1Kut*=2Vn!Ur86 zLNdr{)*ymzJ`}#hIFwKZ64*lp)k{z$E(x27cEgOaht3NAa6ZoE`_6wjXYjr_IE+zW zVLfB4maLLYAZszI>dNX_bw-dVa$t^-)nXk#u#7c)#wX0#g<=1Z!ChECXe`vAOoh%+ZtUhI_b_Yk?$)OP_s$wjfCUDtUQsQtDr0=*q1yL=u3p+MKVhE@QpAQiuXnm2_pHZN0rf} za*xqeY%aOHB{}dje8o$Az*F3YAE(fV5lgRu=`VOfRVCi?bJzre8!!0At@DIQ7lUAHTn%4UYcDkWLgb!V&qmj53u)CUXI>+7L z#k)t<$!^h|FJXbvUA|xezwnJjZ(%N+FQ@$CX1f(EQvMyxnSRk;?pK{#vPY2wFQdT^ z3D1R?;I(w8V0$weJ~_NYVK ze3tQ9VFs;ewe)kGcNn_gl&-AXk5Y|X&3-0hieVLt_=wkdhAEn79QM?W4z~iN7}9u* z6eTahPCbZ;GUsEAk6HQ%x5pkv6;b4C%*;(qSfedmZrCq-4(~PxyjyMc+A{wSH#^9a diff --git a/tkupek_elearning/elearning/admin.py b/tkupek_elearning/elearning/admin.py index 2aae186..0c57a08 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 +from tkupek_elearning.elearning.models import Option, Setting, Question, UserAnswer, User class OptionInline(admin.TabularInline): @@ -13,4 +13,6 @@ class QuestionAdmin(admin.ModelAdmin): ] admin.site.register(Question, QuestionAdmin) -admin.site.register(Setting) \ No newline at end of file +admin.site.register(Setting), +admin.site.register(UserAnswer), +admin.site.register(User) diff --git a/tkupek_elearning/elearning/models.py b/tkupek_elearning/elearning/models.py index facd379..f90d6b6 100644 --- a/tkupek_elearning/elearning/models.py +++ b/tkupek_elearning/elearning/models.py @@ -1,6 +1,9 @@ from __future__ import unicode_literals +import uuid + from django.db import models +from django.db.models.signals import post_init, pre_init class Setting(models.Model): @@ -30,4 +33,21 @@ class Option(models.Model): question = models.ForeignKey(Question, on_delete=models.CASCADE, null=False) def __str__(self): - return self.text \ No newline at end of file + return self.text + + +class User(models.Model): + token = models.CharField(max_length=32, null=True) + name = models.CharField(max_length=100, null=False) + + def __str__(self): + return self.name + + +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) + + class Meta: + unique_together = (('user', 'questionId'),) \ No newline at end of file diff --git a/tkupek_elearning/elearning/static/js/elearning.js b/tkupek_elearning/elearning/static/js/elearning.js index 17e6d0b..3530718 100644 --- a/tkupek_elearning/elearning/static/js/elearning.js +++ b/tkupek_elearning/elearning/static/js/elearning.js @@ -6,4 +6,61 @@ function toggle_solution(id) } else { element.className = "show"; } +} + +function get_answer(id) { + + var answerRequest=new ajaxRequest() + answerRequest.onreadystatechange=function(){ + if (answerRequest.readyState==4){ + if (answerRequest.status==200 || window.location.href.indexOf("http")==-1){ + alert(answerRequest.responseText); + } + else{ + alert("An error has occured making the request"); + } + } + } + var questionId=encodeURIComponent(id) + var token=encodeURIComponent("1ea6de64cf5a11e5ada41c6f6525891e") + var answers=encodeURIComponent(getCheckboxAnswers(id)) + answerRequest.open("GET", "api?id="+questionId+"&token="+token+"&answers="+answers, true) + answerRequest.send(null) +} + +function getCheckboxAnswers(id) { + + var answers = ""; + + var elements = document.getElementsByName("checkbox_" + id); + for(var i = 0; i < elements.length; i++) { + if(elements[i].checked) { + answers += elements[i].value + "_"; + } + } + + if(answers != "") { + answers = answers.substring(0, answers.length - 1); + } + + return answers; +} + + +function ajaxRequest(){ + var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE + if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken) + for (var i=0; i - + {{ option.text }} {% endfor %} - +

{{ question.explanation }}

diff --git a/tkupek_elearning/elearning/views.py b/tkupek_elearning/elearning/views.py index 708f045..bdffe0e 100644 --- a/tkupek_elearning/elearning/views.py +++ b/tkupek_elearning/elearning/views.py @@ -1,8 +1,9 @@ +from django.http import HttpResponse from django.shortcuts import render_to_response -from tkupek_elearning.elearning.models import Setting, Question, Option +from tkupek_elearning.elearning.models import Setting, Question, Option, UserAnswer, User -#import pdb +import pdb def home(request): @@ -17,3 +18,29 @@ def home(request): questions_options[question] = options return render_to_response('index.html', {'settings': settings, 'questions_options': questions_options}) + + +def getAnswer(request): + + # 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) diff --git a/tkupek_elearning/urls.py b/tkupek_elearning/urls.py index ac08c56..5fd377c 100644 --- a/tkupek_elearning/urls.py +++ b/tkupek_elearning/urls.py @@ -20,5 +20,6 @@ import tkupek_elearning.elearning.views urlpatterns = [ url(r'^admin/', admin.site.urls), - url(r'start', tkupek_elearning.elearning.views.home) -] \ No newline at end of file + url(r'start', tkupek_elearning.elearning.views.home), + url(r'api', tkupek_elearning.elearning.views.getAnswer) +]