This commit is contained in:
Ross
2021-12-18 09:14:41 +00:00
parent 61c806594a
commit 2cc73e62ff
6 changed files with 296 additions and 158 deletions
+118 -118
View File
@@ -475,124 +475,124 @@ def exam_scores_refresh(request, pk):
},
)
@login_required
def exam_scores_cid(request, pk):
exam = get_object_or_404(Exam, pk=pk)
if not exam.exam_mode:
raise Http404("Packet not in exam mode")
questions = exam.exam_questions.all()
cids = (
CidUserAnswer.objects.filter(question__in=questions, exam__id=pk)
.order_by("cid")
.values_list("cid", flat=True)
.distinct()
)
user_answers_and_marks = defaultdict(list)
user_answers_marks = defaultdict(list)
user_answers = defaultdict(list)
user_names = {}
by_question = defaultdict(list)
unmarked = set()
# Loop through all candidates
for cid in cids:
# Convoluted (probably...)
user_names[cid] = cid
for q in questions:
# Get user answer
s = q.cid_user_answers.filter(cid=cid, exam__id=pk).first()
if not s:
# skip if no answer
user_answers_marks[cid].append(0)
user_answers[cid].append("")
by_question[q].append(("", 0))
continue
ans = s.answer
answer_score = s.get_answer_score()
if answer_score == "unmarked":
index = exam.get_question_index(q)
unmarked.add(index)
user_answers[cid].append(ans)
user_answers_marks[cid].append(answer_score)
user_answers_and_marks[cid].append((ans, answer_score))
by_question[q].append((ans, answer_score))
user_scores = {}
for user in user_answers_marks:
user_scores[user] = sum(
[i for i in user_answers_marks[user] if i != "unmarked"]
)
user_scores_list = list(user_scores.values())
if len(user_scores_list) < 1:
mean = 0
median = 0
mode = 0
fig_html = ""
else:
mean = statistics.mean(user_scores_list)
median = statistics.median(user_scores_list)
try:
mode = statistics.mode(user_scores_list)
except statistics.StatisticsError:
mode = "No unique mode"
df = user_scores_list
fig = px.histogram(
df,
x=0,
title="{}: distribution of scores".format(exam),
labels={"0": "Score"},
height=400,
width=600,
)
fig_html = fig.to_html()
max_score = len(questions) * 2
exam.stats_mean = mean
exam.stats_median = median
exam.stats_mode = mode
exam.stats_candidates = len(user_scores_list)
exam.stats_max_possible = max_score
exam.stats_min = min(user_scores_list)
exam.stats_max = max(user_scores_list)
exam.stats_graph = fig_html
exam.save()
return render(
request,
"anatomy/exam_scores.html",
{
"cids": cids,
"exam": exam,
"unmarked": unmarked,
"questions": questions,
"by_question": by_question,
"user_answers": dict(user_answers),
"user_answers_marks": dict(user_answers_marks),
"user_scores": user_scores,
"user_scores_list": user_scores_list,
"user_names": user_names,
"user_answers_and_marks": user_answers_and_marks,
"max_score": max_score,
"mean": mean,
"median": median,
"mode": mode,
"plot": fig_html,
},
)
#@login_required
#def exam_scores_cid(request, pk):
# exam = get_object_or_404(Exam, pk=pk)
#
# if not exam.exam_mode:
# raise Http404("Packet not in exam mode")
#
# questions = exam.exam_questions.all()
#
# cids = (
# CidUserAnswer.objects.filter(question__in=questions, exam__id=pk)
# .order_by("cid")
# .values_list("cid", flat=True)
# .distinct()
# )
#
# user_answers_and_marks = defaultdict(list)
# user_answers_marks = defaultdict(list)
# user_answers = defaultdict(list)
# user_names = {}
#
# by_question = defaultdict(list)
# unmarked = set()
#
# # Loop through all candidates
# for cid in cids:
# # Convoluted (probably...)
# user_names[cid] = cid
# for q in questions:
# # Get user answer
# s = q.cid_user_answers.filter(cid=cid, exam__id=pk).first()
# if not s:
# # skip if no answer
# user_answers_marks[cid].append(0)
# user_answers[cid].append("")
# by_question[q].append(("", 0))
# continue
#
# ans = s.answer
# answer_score = s.get_answer_score()
# if answer_score == "unmarked":
# index = exam.get_question_index(q)
# unmarked.add(index)
# user_answers[cid].append(ans)
# user_answers_marks[cid].append(answer_score)
# user_answers_and_marks[cid].append((ans, answer_score))
#
# by_question[q].append((ans, answer_score))
#
# user_scores = {}
# for user in user_answers_marks:
# user_scores[user] = sum(
# [i for i in user_answers_marks[user] if i != "unmarked"]
# )
#
# user_scores_list = list(user_scores.values())
#
# if len(user_scores_list) < 1:
# mean = 0
# median = 0
# mode = 0
# fig_html = ""
# else:
# mean = statistics.mean(user_scores_list)
# median = statistics.median(user_scores_list)
# try:
# mode = statistics.mode(user_scores_list)
# except statistics.StatisticsError:
# mode = "No unique mode"
#
# df = user_scores_list
# fig = px.histogram(
# df,
# x=0,
# title="{}: distribution of scores".format(exam),
# labels={"0": "Score"},
# height=400,
# width=600,
# )
# fig_html = fig.to_html()
#
# max_score = len(questions) * 2
#
# exam.stats_mean = mean
# exam.stats_median = median
# exam.stats_mode = mode
#
# exam.stats_candidates = len(user_scores_list)
# exam.stats_max_possible = max_score
#
# exam.stats_min = min(user_scores_list)
# exam.stats_max = max(user_scores_list)
#
# exam.stats_graph = fig_html
# exam.save()
#
# return render(
# request,
# "anatomy/exam_scores.html",
# {
# "cids": cids,
# "exam": exam,
# "unmarked": unmarked,
# "questions": questions,
# "by_question": by_question,
# "user_answers": dict(user_answers),
# "user_answers_marks": dict(user_answers_marks),
# "user_scores": user_scores,
# "user_scores_list": user_scores_list,
# "user_names": user_names,
# "user_answers_and_marks": user_answers_and_marks,
# "max_score": max_score,
# "mean": mean,
# "median": median,
# "mode": mode,
# "plot": fig_html,
# },
# )
def exam_scores_cid_user(request, pk, sk, passcode):