diff --git a/fs_quiz/urls.py b/fs_quiz/urls.py index ad80ba2..62dde4c 100644 --- a/fs_quiz/urls.py +++ b/fs_quiz/urls.py @@ -16,13 +16,16 @@ from django.contrib import admin from django.urls import path -from quiz.views import SearchView, QuizCreateView, QuizView, QuizResultView, QuestionCreateView +from quiz.views import SearchView, QuizCreateView, QuizListView, QuizView, QuizResultView, QuestionCreateView urlpatterns = [ path('', SearchView.as_view()), path('create_quiz', QuizCreateView.as_view(), name='create_quiz'), path('create_question', QuestionCreateView.as_view(), name='create_question'), - path('quiz/', QuizView.as_view(), name='quiz'), + path('quiz', QuizListView.as_view(), name='quiz_list'), + path('quiz/', QuizView.as_view(template_name='quiz.html'), name='quiz'), + path('quiz_old/', QuizView.as_view(template_name='quiz_old.html'), name='quiz_old'), + path('quiz_east/', QuizView.as_view(template_name='quiz_east.html'), name='quiz_east'), path('quiz_result/', QuizResultView.as_view(), name='quiz_result'), path('admin/', admin.site.urls), ] diff --git a/quiz/migrations/0006_questionattempt.py b/quiz/migrations/0006_questionattempt.py new file mode 100644 index 0000000..85fc63a --- /dev/null +++ b/quiz/migrations/0006_questionattempt.py @@ -0,0 +1,22 @@ +# Generated by Django 2.0 on 2018-01-04 12:44 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ('quiz', '0005_question_timestamp'), + ] + + operations = [ + migrations.CreateModel( + name='QuestionAttempt', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('duration', models.FloatField()), + ('answer', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='quiz.Answer')), + ('question', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='quiz.Question')), + ], + ), + ] diff --git a/quiz/migrations/0007_questionattempt_nullable.py b/quiz/migrations/0007_questionattempt_nullable.py new file mode 100644 index 0000000..de351fd --- /dev/null +++ b/quiz/migrations/0007_questionattempt_nullable.py @@ -0,0 +1,18 @@ +# Generated by Django 2.0 on 2018-01-04 13:27 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ('quiz', '0006_questionattempt'), + ] + + operations = [ + migrations.AlterField( + model_name='questionattempt', + name='answer', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='quiz.Answer'), + ), + ] diff --git a/quiz/migrations/0008_remove_answer_answered.py b/quiz/migrations/0008_remove_answer_answered.py new file mode 100644 index 0000000..a3ff6e7 --- /dev/null +++ b/quiz/migrations/0008_remove_answer_answered.py @@ -0,0 +1,16 @@ +# Generated by Django 2.0 on 2018-01-05 15:17 + +from django.db import migrations + + +class Migration(migrations.Migration): + dependencies = [ + ('quiz', '0007_questionattempt_nullable'), + ] + + operations = [ + migrations.RemoveField( + model_name='answer', + name='answered', + ), + ] diff --git a/quiz/models.py b/quiz/models.py index 3dd75f4..a4fbbde 100644 --- a/quiz/models.py +++ b/quiz/models.py @@ -13,13 +13,6 @@ class Question(models.Model): tags = models.ManyToManyField(Tag) timestamp = models.DateTimeField(auto_now=True) - def get_answer_count(self): - return self.answer_set.aggregate(models.Sum('answered'))['answered__sum'] - - def get_wrong_answer_count(self): - return self.answer_set.filter(correct=False).aggregate( - models.Sum('answered'))['answered__sum'] - def shuffled_answers(self): return self.answer_set.order_by('?') @@ -30,7 +23,6 @@ def __str__(self): class Answer(models.Model): text = models.TextField() correct = models.BooleanField() - answered = models.IntegerField(default=0) question = models.ForeignKey(to=Question, on_delete=models.CASCADE) def __str__(self): @@ -40,3 +32,15 @@ def __str__(self): class Quiz(models.Model): name = models.SlugField(max_length=16, primary_key=True) questions = models.ManyToManyField(Question) + + def shuffled_questions(self): + return self.questions.order_by('?') + + def __str__(self): + return self.name + + +class QuestionAttempt(models.Model): + question = models.ForeignKey(to=Question, on_delete=models.CASCADE) + answer = models.ForeignKey(to=Answer, null=True, on_delete=models.CASCADE) + duration = models.FloatField() diff --git a/quiz/templates/base.html b/quiz/templates/base.html index 1049a50..77bf9fc 100644 --- a/quiz/templates/base.html +++ b/quiz/templates/base.html @@ -15,6 +15,9 @@ Create Question + + Quiz list + {% block content %}

Hello, world!

diff --git a/quiz/templates/quiz.html b/quiz/templates/quiz.html index 3315c17..8523738 100644 --- a/quiz/templates/quiz.html +++ b/quiz/templates/quiz.html @@ -7,8 +7,12 @@

{{ quiz.name }}

{% csrf_token %} - {% for question in quiz.questions.all %} -
+ +
+ {% for question in quiz.shuffled_questions %} +

{{ question.text }}

@@ -16,37 +20,66 @@

{{ question.text }}

{% for answer in question.shuffled_answers %}
- - + +
{% endfor %}
{% if forloop.last %} + tabindex="1">Submit + {% else %} {% endif %} +
+ +
{% endfor %} +
+
+ +
+ +
-
{% endblock %} {% block head %} diff --git a/quiz/templates/quiz_east.html b/quiz/templates/quiz_east.html new file mode 100644 index 0000000..60b6401 --- /dev/null +++ b/quiz/templates/quiz_east.html @@ -0,0 +1,133 @@ +{% extends 'base.html' %} +{% block title %} + {{ quiz.name }} +{% endblock %} +{% block content %} +
+

{{ quiz.name }}

+
+ +
+ {% for question in quiz.questions.all %} +
+

{{ forloop.counter }}: {{ question.text }}

+
+
+
+ {% for answer in question.shuffled_answers %} +
+ + +
+ {% endfor %} +
+
+
+ {% endfor %} +
+ Submit + +
+ +
+
+
+ +{% endblock %} +{% block head %} + +{% endblock %} \ No newline at end of file diff --git a/quiz/templates/quiz_list.html b/quiz/templates/quiz_list.html new file mode 100644 index 0000000..12ccbb8 --- /dev/null +++ b/quiz/templates/quiz_list.html @@ -0,0 +1,16 @@ +{% extends 'base.html' %} +{% block content %} +
+
+ {% for quiz in quiz_list %} + + {% endfor %} +
+
+{% endblock content %} \ No newline at end of file diff --git a/quiz/templates/quiz_old.html b/quiz/templates/quiz_old.html new file mode 100644 index 0000000..66d87fd --- /dev/null +++ b/quiz/templates/quiz_old.html @@ -0,0 +1,119 @@ +{% extends 'base.html' %} +{% block title %} + {{ quiz.name }} +{% endblock %} +{% block content %} +
+

{{ quiz.name }}

+
+ +
+ {% for question in quiz.questions.all %} +
+

{{ question.text }}

+
+
+
+ {% for answer in question.shuffled_answers %} +
+ + +
+ {% endfor %} +
+
+
+ {% endfor %} +
+ Submit + +
+ +
+
+
+ +{% endblock %} +{% block head %} + +{% endblock %} \ No newline at end of file diff --git a/quiz/templates/quiz_result.html b/quiz/templates/quiz_result.html index 6e25977..6860584 100644 --- a/quiz/templates/quiz_result.html +++ b/quiz/templates/quiz_result.html @@ -5,15 +5,18 @@ {% block content %}

{{ quiz.name }}

-

- {% for question in quiz.questions.all %} +

+ Dere brukte {{ request.POST.timer }}s og var riktig. +

+ {% for attempt in view.question_attempts %}
-

{{ question.text }}

+

{{ attempt.duration }}s

+

{{ attempt.question.text }}

-
- {% for answer in question.answer_set.all %} +
+ {% for answer in attempt.question.answer_set.all %} {{ answer.text }} +{% if answer.pk == attempt.answer.pk %}answered{% endif %} answer item">{{ answer.text }} {% endfor %}
@@ -22,11 +25,15 @@

{{ question.text }}

{% endblock %} {% block head %}