vcp-elearning/tkupek_elearning/elearning/views.py

55 lines
1.6 KiB
Python
Raw Normal View History

2016-02-09 20:46:04 +01:00
from django.core.exceptions import ObjectDoesNotExist
2016-02-09 19:51:05 +01:00
from django.http import HttpResponse
2016-01-31 16:17:37 +01:00
from django.shortcuts import render_to_response
2016-02-09 19:51:05 +01:00
from tkupek_elearning.elearning.models import Setting, Question, Option, UserAnswer, User
2016-01-31 18:19:45 +01:00
2016-02-09 21:35:46 +01:00
# import pdb
2016-01-31 16:17:37 +01:00
2016-02-09 20:46:04 +01:00
def home(request):
2016-01-31 18:19:45 +01:00
settings = Setting.objects.filter(active=1)
if settings:
2016-01-31 18:19:45 +01:00
settings = settings[0]
2016-01-31 16:17:37 +01:00
questions_options = {}
questions = Question.objects.all()
for question in questions:
options = Option.objects.filter(question=question.id)
questions_options[question] = options
return render_to_response('index.html', {'settings': settings, 'questions_options': questions_options})
2016-02-09 19:51:05 +01:00
2016-02-09 21:35:46 +01:00
def get_answer(request):
2016-02-09 20:46:04 +01:00
if request.method == 'GET':
request_id = request.GET.get('id')
request_token = request.GET.get('token')
request_answers = request.GET.get('answers')
question = Question.objects.get(id=request_id)
user = User.objects.get(token=request_token)
try:
2016-02-09 21:35:46 +01:00
user_answer = UserAnswer.objects.get(questionId=question.id, user=user.id)
2016-02-09 20:46:04 +01:00
except ObjectDoesNotExist:
2016-02-09 21:35:46 +01:00
user_answer = None
2016-02-09 20:46:04 +01:00
2016-02-09 21:35:46 +01:00
if user_answer is None:
user_answer = UserAnswer()
user_answer.questionId = question
user_answer.user = user
user_answer.answers = request_answers
user_answer.save()
2016-02-09 20:46:04 +01:00
options = Option.objects.filter(question=question.id, correct=True)
2016-02-09 19:51:05 +01:00
2016-02-09 21:35:46 +01:00
options_id = ""
for option in options:
options_id += str(option.id) + "_"
if options_id is not "":
options_id = options_id[:-1]
return HttpResponse(options_id)