145 lines
4.3 KiB
Python
145 lines
4.3 KiB
Python
from dataclasses import dataclass
|
|
from enum import Enum
|
|
from typing import List
|
|
|
|
from rest_framework.decorators import api_view
|
|
from rest_framework.response import Response
|
|
|
|
from vbv_lernwelt.assignment.models import (
|
|
Assignment,
|
|
AssignmentCompletion,
|
|
AssignmentCompletionStatus,
|
|
AssignmentType,
|
|
)
|
|
from vbv_lernwelt.core.models import User
|
|
from vbv_lernwelt.core.serializers import UserSerializer
|
|
from vbv_lernwelt.course.models import CourseSession
|
|
from vbv_lernwelt.course_session.models import CourseSessionAssignment
|
|
from vbv_lernwelt.learning_mentor.models import LearningMentor
|
|
|
|
|
|
class CompletionStatus(str, Enum):
|
|
PENDING = "PENDING"
|
|
SUBMITTED = "SUBMITTED"
|
|
EVALUATED = "EVALUATED"
|
|
|
|
|
|
@dataclass
|
|
class PraxisAssignmentCompletion:
|
|
status: CompletionStatus
|
|
user_id: str
|
|
last_name: str
|
|
|
|
|
|
@dataclass
|
|
class PraxisAssignmentStatus:
|
|
id: str
|
|
title: str
|
|
circle_id: str
|
|
completions: List[PraxisAssignmentCompletion]
|
|
|
|
|
|
def get_assignment_completions(
|
|
course_session: CourseSession, assignment: Assignment, participants: List[User]
|
|
) -> List[PraxisAssignmentCompletion]:
|
|
evaluation_results = AssignmentCompletion.objects.filter(
|
|
assignment_user__in=participants,
|
|
course_session=course_session,
|
|
assignment=assignment,
|
|
).values("completion_status", "assignment_user__last_name", "assignment_user")
|
|
|
|
user_status_map = {}
|
|
for result in evaluation_results:
|
|
completion_status = result["completion_status"]
|
|
|
|
if completion_status == AssignmentCompletionStatus.EVALUATION_SUBMITTED.value:
|
|
status = CompletionStatus.EVALUATED
|
|
elif completion_status in [
|
|
AssignmentCompletionStatus.SUBMITTED.value,
|
|
AssignmentCompletionStatus.EVALUATION_IN_PROGRESS.value,
|
|
]:
|
|
status = CompletionStatus.SUBMITTED
|
|
else:
|
|
status = CompletionStatus.PENDING
|
|
|
|
user_status_map[result["assignment_user"]] = (
|
|
status,
|
|
result["assignment_user__last_name"],
|
|
)
|
|
|
|
status_priority = {
|
|
CompletionStatus.SUBMITTED: 1,
|
|
CompletionStatus.EVALUATED: 2,
|
|
CompletionStatus.PENDING: 3,
|
|
}
|
|
|
|
sorted_participants = sorted(
|
|
participants,
|
|
key=lambda u: (
|
|
status_priority.get(
|
|
user_status_map.get(u.id, (CompletionStatus.PENDING, ""))[0]
|
|
),
|
|
user_status_map.get(u.id, ("", u.last_name))[1],
|
|
),
|
|
)
|
|
|
|
return [
|
|
PraxisAssignmentCompletion(
|
|
status=user_status_map.get(
|
|
user.id, (CompletionStatus.PENDING, user.last_name)
|
|
)[0],
|
|
user_id=user.id,
|
|
last_name=user.last_name,
|
|
)
|
|
for user in sorted_participants
|
|
]
|
|
|
|
|
|
def get_praxis_assignments(
|
|
course_session: CourseSession, participants: List[User]
|
|
) -> List[PraxisAssignmentStatus]:
|
|
records = []
|
|
|
|
for course_session_assignment in CourseSessionAssignment.objects.filter(
|
|
course_session=course_session,
|
|
learning_content__content_assignment__assignment_type__in=[
|
|
# TODO: Switch to PRAXIS_ASSIGNMENT
|
|
AssignmentType.CASEWORK.value,
|
|
],
|
|
):
|
|
learning_content = course_session_assignment.learning_content
|
|
records.append(
|
|
PraxisAssignmentStatus(
|
|
id=course_session_assignment.id,
|
|
title=learning_content.content_assignment.title,
|
|
circle_id=learning_content.get_circle().id,
|
|
completions=get_assignment_completions(
|
|
course_session=course_session,
|
|
assignment=learning_content.content_assignment,
|
|
participants=participants,
|
|
),
|
|
)
|
|
)
|
|
|
|
return records
|
|
|
|
|
|
@api_view(["GET"])
|
|
def mentor_summary(request, course_session_id: int):
|
|
if not request.user.is_authenticated:
|
|
return Response(status=403)
|
|
|
|
if request.method == "GET":
|
|
course_session = CourseSession.objects.get(id=course_session_id)
|
|
|
|
participants = LearningMentor.objects.filter(mentor=request.user)
|
|
|
|
return Response(
|
|
{
|
|
"participants": [UserSerializer(s).data for s in participants],
|
|
"praxis_assignments": get_praxis_assignments(
|
|
course_session, participants
|
|
),
|
|
}
|
|
)
|