diff --git a/tkupek_elearning/elearning/models.py b/tkupek_elearning/elearning/models.py index d8625f7..fd509c0 100644 --- a/tkupek_elearning/elearning/models.py +++ b/tkupek_elearning/elearning/models.py @@ -16,7 +16,7 @@ class Setting(models.Model): logo = models.CharField(max_length=256, null=True) active = models.BooleanField(unique=True, default=False) - def __unicode__(self): + def __str__(self): return self.title @@ -26,17 +26,16 @@ class Question(models.Model): text = models.TextField(null=True) explanation = models.TextField(null=True) - def __unicode__(self): + def __str__(self): return self.title - class Option(models.Model): text = models.CharField(max_length=100, null=True) correct = models.BooleanField(null=False, default=False) question = models.ForeignKey(Question, on_delete=models.CASCADE, null=False) - def __unicode__(self): + def __str__(self): return self.text @@ -50,15 +49,16 @@ class User(models.Model): name = models.CharField(max_length=100, null=False) last_seen = models.DateTimeField(null=True) - def __unicode__(self): + def __str__(self): return self.name class UserAnswer(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE, null=False) question = models.ForeignKey(Question, on_delete=models.CASCADE, null=False) + correct = models.NullBooleanField(null=True) - def __unicode__(self): + def __str__(self): return str(self.user) + " - " + str(self.question) class Meta: diff --git a/tkupek_elearning/elearning/templates/statistic.html b/tkupek_elearning/elearning/templates/statistic.html index ff00014..4efa1f9 100644 --- a/tkupek_elearning/elearning/templates/statistic.html +++ b/tkupek_elearning/elearning/templates/statistic.html @@ -42,11 +42,15 @@ title answers + correct + correct percentage {% for question in questions %} {{ question.title }} {{ question.answers }} + {{ question.correct_answers }} + {{ question.correct_answers_percentage }} {% endfor %} diff --git a/tkupek_elearning/elearning/views.py b/tkupek_elearning/elearning/views.py index 72a4c89..ce5580b 100644 --- a/tkupek_elearning/elearning/views.py +++ b/tkupek_elearning/elearning/views.py @@ -60,12 +60,33 @@ def get_answer(request): user = User.objects.get(token=request_token) log_last_seen(user) + 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) + user_answer = get_user_answer(question, user) if user_answer is None: + correct = False + user_answer = UserAnswer() user_answer.question = question user_answer.user = user + + 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 user_answer.save() for option in request_answers: @@ -74,13 +95,6 @@ def get_answer(request): user_answer_options.option = Option.objects.get(id=option) user_answer_options.save() - options = Option.objects.filter(question=question.id, correct=True) - - options_id = [] - for option in options: - options_id.append(option.id) - options_id = json.dumps(options_id) - return HttpResponse(options_id) @@ -109,5 +123,7 @@ def statistic(request): 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() + question.correct_answers_percentage = question.correct_answers / question.answers return render_to_response('statistic.html', {'settings': settings, 'users': users, 'questions': questions})