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 713f141..4978da7 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 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) +]