diff --git a/rapids/views.py b/rapids/views.py index 1a192291..6df84da2 100755 --- a/rapids/views.py +++ b/rapids/views.py @@ -580,6 +580,119 @@ def mark_all(request, exam_pk, sk): def mark_review(request, exam_pk, sk): return mark(request, exam_pk, sk, unmarked_exam_answers_only=False, review=True) +@login_required +def exam_scores_cid2(request, pk): + exam = get_object_or_404(Exam, pk=pk) + + if not exam.exam_mode: + raise Http404("Packet not in exam mode") + + user_answers_and_marks = defaultdict(list) + user_answers_marks = defaultdict(list) + user_answers = defaultdict(list) + user_names = {} + + by_question = defaultdict(list) + unmarked = set() + + questions = exam.exam_questions.all() + + cids = ( + CidUserAnswer.objects.filter(question__in=questions, exam__id=pk) + ) + + + # Loop through all candidates + for cid_user_answer in cids: + # Convoluted (probably...) + cid = cid_user_answer.cid + s = cid_user_answer + user_names[cid] = cid + + q = cid_user_answer.question + + + if not s: + # skip if no answer + user_answers_marks[cid].append(0) + user_answers[cid].append("") + by_question[q].append(("", 0)) + continue + elif s.normal: + ans = "Normal" + else: + 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 = {} + user_scores_normalised = {} + for user in user_answers_marks: + user_scores[user] = sum( + [i for i in user_answers_marks[user] if i != "unmarked"] + ) + user_scores_normalised[user] = normaliseScore( + 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 + + return render( + request, + "rapids/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_normalised": user_scores_normalised, + "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, + }, + ) # @user_passes_test(user_is_admin, login_url="/accounts/login") @login_required