start updating physics

This commit is contained in:
Ross
2021-12-10 23:02:19 +00:00
parent d9c6f464ee
commit ea52d573fe
7 changed files with 546 additions and 273 deletions
+119 -3
View File
@@ -46,7 +46,7 @@ from rest_framework.pagination import PageNumberPagination
from django.core.exceptions import PermissionDenied
from .forms import ExamForm
from .forms import CidUserAnswerForm, ExamForm
from .decorators import user_is_author_or_physics_checker, user_is_exam_author_or_physics_checker
class AuthorOrCheckerRequiredMixin(object):
@@ -240,7 +240,7 @@ def exam_scores_cid_user(request, pk, sk):
)
def exam_take(request, pk):
def exam_take_old(request, pk):
exam = get_object_or_404(Exam, pk=pk)
@@ -251,13 +251,129 @@ def exam_take(request, pk):
return render(
request,
"physics/exam_take.html",
"physics/exam_take_old.html",
{
"exam": exam,
"questions": questions,
},
)
def exam_start(request, pk):
exam = get_object_or_404(Exam, pk=pk)
if not exam.active:
raise Http404("Exam not found")
return render(
request,
"physics/exam_start.html",
{
"exam": exam,
},
)
def exam_finish(request, pk, cid):
exam = get_object_or_404(Exam, pk=pk)
questions = exam.exam_questions.all()
answers = CidUserAnswer.objects.filter(
cid=cid, exam=exam)
answer_question_map = {}
for ans in answers:
answer_question_map[ans.question] = ans
question_answer_tuples = []
answer_count = 0
for q in questions:
if q in answer_question_map and answer_question_map[q].answer:
question_answer_tuples.append((q, answer_question_map[q]))
answer_count += 1
else:
question_answer_tuples.append((q, None))
if not exam.active:
raise Http404("Exam not found")
return render(
request,
"physics/exam_finish.html",
{
"exam": exam,
"cid": cid,
"question_answer_tuples": question_answer_tuples,
"answer_count": answer_count,
"exam_length": len(questions),
},
)
def exam_take(request, pk, sk, cid):
exam = get_object_or_404(Exam, pk=pk)
if not exam.active:
raise Http404("Exam not found")
question = exam.exam_questions.all()[sk]
exam_length = len(exam.exam_questions.all())
pos = exam.get_question_index(question)
answer = question.cid_user_answers.filter(
cid=cid, exam=exam).first()
if request.method == "POST":
if answer:
form = CidUserAnswerForm(request.POST, instance=answer)
else:
form = CidUserAnswerForm(request.POST)
if form.is_valid():
answer = form.save(commit=False)
answer.cid = cid
answer.question = question
answer.exam = exam
# answer.published_date = timezone.now()
answer.save()
if "next" in request.POST:
return redirect("sbas:exam_take", pk=pk, sk=pos + 1, cid=cid)
elif "previous" in request.POST:
return redirect("sbas:exam_take", pk=pk, sk=pos - 1, cid=cid)
elif "finish" in request.POST:
return redirect("sbas:exam_finish", pk=pk, cid=cid)
elif "goto" in request.POST:
return redirect("sbas:exam_take", pk=pk, sk=int(request.POST.get("goto")), cid=cid)
else:
form = CidUserAnswerForm(instance=answer)
previous = -1
if sk > 0:
previous = sk - 1
next = sk + 1
if sk == exam_length - 1:
next = False
saved_answer = False
if answer is not None:
saved_answer = answer.answer
return render(
request,
"physics/exam_take.html",
{
"form": form,
"cid": cid,
"exam": exam,
"question": question,
"next": next,
"previous": previous,
"exam_length": exam_length,
"pos": pos,
"saved_answer": saved_answer,
},
)
def loadJsonAnswer(answer):
# As access is not restricted make sure the data appears valid