2016-02-21 22:41:24 +01:00
|
|
|
from __future__ import division
|
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-21 22:41:24 +01:00
|
|
|
import datetime
|
|
|
|
import json
|
2016-01-31 16:17:37 +01:00
|
|
|
|
2016-02-14 17:07:34 +01:00
|
|
|
from tkupek_elearning.elearning.models import Setting, Question, Option, UserAnswer, User, UserAnswerOptions
|
2016-01-31 18:19:45 +01:00
|
|
|
|
2016-02-14 17:07:34 +01:00
|
|
|
|
|
|
|
import pdb
|
2016-01-31 16:17:37 +01:00
|
|
|
|
2016-02-14 16:15:13 +01:00
|
|
|
|
2016-02-21 22:41:24 +01:00
|
|
|
def get_progress(user):
|
|
|
|
progress_max = Question.objects.all().count()
|
|
|
|
progress_current = UserAnswer.objects.filter(user=user.id).count()
|
|
|
|
progress = str(int(progress_current / progress_max * 100))
|
|
|
|
return progress
|
|
|
|
|
|
|
|
|
2016-02-14 16:15:13 +01:00
|
|
|
def start(request):
|
2016-02-14 15:02:26 +01:00
|
|
|
|
|
|
|
token = request.GET.get('token')
|
|
|
|
|
|
|
|
try:
|
|
|
|
user = User.objects.get(token=token)
|
|
|
|
except ObjectDoesNotExist:
|
|
|
|
user = None
|
|
|
|
|
2016-02-14 16:15:13 +01:00
|
|
|
settings = Setting.objects.get(active=1)
|
2016-01-31 16:17:37 +01:00
|
|
|
|
2016-02-14 15:02:26 +01:00
|
|
|
if user is not None:
|
2016-02-14 16:15:13 +01:00
|
|
|
|
|
|
|
log_last_seen(user)
|
|
|
|
|
2016-02-14 15:02:26 +01:00
|
|
|
settings.message_welcome_user = settings.message_welcome_user.replace('{username}', user.name)
|
|
|
|
|
|
|
|
questions_options = {}
|
|
|
|
questions = Question.objects.all()
|
|
|
|
for question in questions:
|
|
|
|
options = Option.objects.filter(question=question.id)
|
|
|
|
|
|
|
|
user_answer = get_user_answer(question, user)
|
|
|
|
|
|
|
|
if user_answer is None:
|
|
|
|
question.enable = True
|
|
|
|
else:
|
|
|
|
question.enable = False
|
2016-01-31 21:18:13 +01:00
|
|
|
|
2016-02-14 15:02:26 +01:00
|
|
|
questions_options[question] = options
|
|
|
|
questions_options[question] = options
|
|
|
|
|
2016-02-21 22:41:24 +01:00
|
|
|
progress = get_progress(user)
|
|
|
|
|
|
|
|
return render_to_response('elearning.html', {'settings': settings, 'questions_options': questions_options, 'progress': progress})
|
2016-02-14 15:02:26 +01:00
|
|
|
|
|
|
|
else:
|
|
|
|
return render_to_response('access_denied.html', {'settings': settings})
|
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')
|
2016-02-14 17:07:34 +01:00
|
|
|
request_answers = json.loads(request_answers)
|
2016-02-09 20:46:04 +01:00
|
|
|
|
|
|
|
question = Question.objects.get(id=request_id)
|
|
|
|
user = User.objects.get(token=request_token)
|
2016-02-14 16:15:13 +01:00
|
|
|
log_last_seen(user)
|
2016-02-09 20:46:04 +01:00
|
|
|
|
2016-02-17 23:58:07 +01:00
|
|
|
correct_options = Option.objects.filter(question=question.id, correct=True)
|
|
|
|
correct_option_checklist = []
|
|
|
|
for option in correct_options:
|
|
|
|
correct_option_checklist.append(option.id)
|
|
|
|
|
|
|
|
options_id = []
|
|
|
|
for option in correct_options:
|
|
|
|
options_id.append(option.id)
|
|
|
|
options_id = json.dumps(options_id)
|
|
|
|
|
2016-02-14 15:02:26 +01:00
|
|
|
user_answer = get_user_answer(question, user)
|
2016-02-09 20:46:04 +01:00
|
|
|
|
2016-02-09 21:35:46 +01:00
|
|
|
if user_answer is None:
|
2016-02-17 23:58:07 +01:00
|
|
|
correct = False
|
|
|
|
|
2016-02-09 21:35:46 +01:00
|
|
|
user_answer = UserAnswer()
|
2016-02-09 21:46:52 +01:00
|
|
|
user_answer.question = question
|
2016-02-09 21:35:46 +01:00
|
|
|
user_answer.user = user
|
2016-02-17 23:58:07 +01:00
|
|
|
|
|
|
|
for option in request_answers:
|
|
|
|
if option in correct_option_checklist:
|
|
|
|
correct_option_checklist.remove(option)
|
|
|
|
|
|
|
|
if len(correct_option_checklist) is 0:
|
|
|
|
correct = True
|
|
|
|
|
|
|
|
user_answer.correct = correct
|
2016-02-09 21:35:46 +01:00
|
|
|
user_answer.save()
|
2016-02-09 20:46:04 +01:00
|
|
|
|
2016-02-14 17:07:34 +01:00
|
|
|
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()
|
|
|
|
|
2016-02-21 22:41:24 +01:00
|
|
|
holder = {'options_id': options_id, 'progress': get_progress(user)}
|
|
|
|
holder = json.dumps(holder)
|
|
|
|
|
|
|
|
return HttpResponse(holder)
|
2016-02-14 15:02:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_user_answer(question, user):
|
|
|
|
try:
|
|
|
|
user_answer = UserAnswer.objects.get(question=question.id, user=user.id)
|
|
|
|
except ObjectDoesNotExist:
|
|
|
|
user_answer = None
|
|
|
|
|
2016-02-14 16:15:13 +01:00
|
|
|
return user_answer
|
|
|
|
|
|
|
|
|
|
|
|
def log_last_seen(user):
|
|
|
|
user.last_seen = datetime.datetime.now()
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
|
2016-02-14 17:07:34 +01:00
|
|
|
def statistic(request):
|
2016-02-14 16:15:13 +01:00
|
|
|
|
2016-03-03 22:00:14 +01:00
|
|
|
token = request.GET.get('token')
|
|
|
|
|
|
|
|
try:
|
|
|
|
auth = Setting.objects.get(active=1, token=token)
|
|
|
|
except ObjectDoesNotExist:
|
|
|
|
auth = None
|
|
|
|
|
2016-02-14 16:15:13 +01:00
|
|
|
settings = Setting.objects.get(active=1)
|
|
|
|
|
2016-03-03 22:00:14 +01:00
|
|
|
if auth is not None:
|
|
|
|
settings = Setting.objects.get(active=1)
|
|
|
|
users = User.objects.all()
|
2016-02-14 16:15:13 +01:00
|
|
|
|
2016-03-03 22:00:14 +01:00
|
|
|
for user in users:
|
|
|
|
user.questions_answered = UserAnswer.objects.filter(user=user.id).count()
|
2016-02-14 16:15:13 +01:00
|
|
|
|
2016-03-03 22:00:14 +01:00
|
|
|
questions = Question.objects.all()
|
|
|
|
for question in questions:
|
|
|
|
question.answers = UserAnswer.objects.filter(question=question.id).count()
|
|
|
|
question.correct_answers = UserAnswer.objects.filter(question=question.id, correct=True).count()
|
|
|
|
if question.answers:
|
|
|
|
question.correct_answers_percentage = str(int(question.correct_answers / question.answers * 100))
|
|
|
|
question.correct_answers_percentage += str(' %')
|
|
|
|
|
|
|
|
return render_to_response('statistic.html', {'settings': settings, 'users': users, 'questions': questions})
|
|
|
|
|
|
|
|
else:
|
|
|
|
return render_to_response('access_denied.html', {'settings': settings})
|