added ajax request to client side

This commit is contained in:
tkupek 2016-02-09 19:51:05 +01:00
parent eb02b99f22
commit 6614bf8c26
9 changed files with 118 additions and 27 deletions

View file

@ -1 +1 @@
vcpby-elearning
tkupek-elearning

View file

@ -10,23 +10,7 @@
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="Python 2.7.6 (/usr/bin/python2.7)" project-jdk-type="Python SDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" default="false" assert-keyword="true" jdk-15="true" project-jdk-name="Python 2.7.6 (/usr/bin/python2.7)" project-jdk-type="Python SDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
<component name="masterDetails">
<states>
<state key="ProjectJDKs.UI">
<settings>
<last-edited>Python 2.7.6 (/usr/bin/python2.7)</last-edited>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
</states>
</component>
</project>

Binary file not shown.

View file

@ -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)
admin.site.register(Setting),
admin.site.register(UserAnswer),
admin.site.register(User)

View file

@ -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
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'),)

View file

@ -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<activexmodes.length; i++){
try{
return new ActiveXObject(activexmodes[i])
}
catch(e){
//suppress error
}
}
}
else if (window.XMLHttpRequest) // if Mozilla, Safari etc
return new XMLHttpRequest()
else
return false
}

View file

@ -27,11 +27,11 @@
{% for option in options %}
<label>
<input type="checkbox" name="option" value="{{ option.id }}">
<input name="checkbox_{{ question.id }}" type="checkbox" value="{{ option.id }}">
{{ option.text }}
</label>
{% endfor %}
<button class="margin10" type="button" id="showSolution" onmouseup="toggle_solution({{ question.id }})">{{ settings.button_solution }}</button>
<button class="margin10" type="button" id="showSolution" onmousedown="get_answer({{ question.id }})" onmouseup="toggle_solution({{ question.id }})">{{ settings.button_solution }}</button>
</form>
<p id="explanation_{{ question.id }}" class="hide">{{ question.explanation }}</p>

View file

@ -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)

View file

@ -20,5 +20,6 @@ import tkupek_elearning.elearning.views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'start', tkupek_elearning.elearning.views.home)
]
url(r'start', tkupek_elearning.elearning.views.home),
url(r'api', tkupek_elearning.elearning.views.getAnswer)
]