This commit is contained in:
Ross
2021-12-19 13:09:55 +00:00
parent 07dbdfe0e4
commit fccf25e2a1
16 changed files with 256 additions and 37 deletions
+70 -15
View File
@@ -59,6 +59,7 @@ import random
import plotly.express as px
# from rad.views import get_question_and_content_type
from django.db.models import Prefetch
def normaliseRapidsScore(score):
@@ -240,10 +241,18 @@ def generic_exam_json_edit(request):
class ExamViews(View, LoginRequiredMixin):
def __init__(
self, exam, question, cid_user_answer, app, question_type, loadJsonAnswer
self,
exam,
question,
answer,
cid_user_answer,
app,
question_type,
loadJsonAnswer,
):
self.Exam = exam
self.Question = question
self.Answer = answer
self.CidUserAnswer = cid_user_answer
self.app_name = app
self.question_type = question_type
@@ -414,7 +423,7 @@ class ExamViews(View, LoginRequiredMixin):
# print(Exam.objects.all())
# exams = Exam.objects.all()
# print("test", Exam.objects.all().get(id=pk))
exam = get_object_or_404(self.Exam, pk=pk)
exam = get_object_or_404(self.Exam.objects.prefetch_related("author"), pk=pk)
if request.user not in exam.author.all():
if not self.check_user_access(request.user, pk):
@@ -428,7 +437,38 @@ class ExamViews(View, LoginRequiredMixin):
if can_edit:
notes = self.exam_notes(request, pk)
questions = exam.exam_questions.all()
if self.app_name == "anatomy":
questions = (
exam.exam_questions.select_related(
"modality",
"structure",
"body_part",
"region",
"examination",
"question_type",
).all()
# .prefetch_related(
# Prefetch(
# "answers",
# queryset=self.Answer.objects.filter(
# proposed=False, status=self.Answer.MarkOptions.CORRECT
# ),
# )
# )
)
elif self.app_name == "rapids":
questions = (
exam.exam_questions.select_related()
.all()
.prefetch_related("images", "abnormality", "region", "examination")
)
elif self.app_name == "physics":
questions = (
exam.exam_questions.select_related("category").all()
# .prefetch_related("images", "abnormality", "region", "examination")
)
else:
questions = exam.exam_questions.select_related().all()
question_number = len(questions)
@@ -868,13 +908,15 @@ class ExamViews(View, LoginRequiredMixin):
user_answers = defaultdict(list)
user_names = {}
score_by_question = defaultdict(dict)
ans_by_question = defaultdict(dict)
by_question = defaultdict(dict)
unmarked = set()
questions = exam.exam_questions.all()
cid_user_answers = self.CidUserAnswer.objects.filter(question__in=questions, exam__id=pk)
# We could prefect the CidUserAnswers.answers here (if we didn't cache them)
cid_user_answers = self.CidUserAnswer.objects.select_related("question").filter(
question__in=questions, exam__id=pk
)
cids = set()
@@ -905,15 +947,25 @@ class ExamViews(View, LoginRequiredMixin):
user_answers_marks[cid].append(answer_score)
user_answers_and_marks[cid].append((ans, answer_score))
score_by_question[q][cid] = answer_score
ans_by_question[q][cid] = ans
if self.app_name in ("rapids", "anatomy", "sbas"):
by_question[q][cid] = (ans, answer_score)
else:
zipped_ans_scores = zip(ans, answer_score)
by_question[q][cid] = zipped_ans_scores
user_scores = {}
user_scores_normalised = {}
for user in user_answers_marks:
user_scores[user] = sum(
[i for i in user_answers_marks[user] if i != "unmarked"]
)
if self.app_name in ("rapids", "anatomy", "sbas"):
user_scores[user] = sum(
[i for i in user_answers_marks[user] if i != "unmarked"]
)
else:
user_scores[user] = sum(
[sum(i) for i in user_answers_marks[user] if i != "unmarked"]
)
if self.app_name == "rapids":
user_scores_normalised[user] = normaliseRapidsScore(
sum([i for i in user_answers_marks[user] if i != "unmarked"])
@@ -923,7 +975,12 @@ class ExamViews(View, LoginRequiredMixin):
user_scores_list = list(user_scores.values())
max_score = len(questions) * 2
if self.app_name in ("rapids", "anatomy"):
max_score = len(questions) * 2
elif self.app_name == "physics":
max_score = len(questions) * 5
else:
max_score = len(questions)
if len(user_scores_list) < 1:
mean = 0
@@ -962,7 +1019,6 @@ class ExamViews(View, LoginRequiredMixin):
exam.stats_graph = fig_html
exam.save()
return render(
request,
f"{self.app_name}/exam_scores_new.html",
@@ -971,8 +1027,7 @@ class ExamViews(View, LoginRequiredMixin):
"exam": exam,
"unmarked": unmarked,
"questions": questions,
"score_by_question": score_by_question,
"ans_by_question": ans_by_question,
"by_question": by_question,
"user_answers": dict(user_answers),
"user_answers_marks": dict(user_answers_marks),
"user_scores": user_scores,