Skip to content
grades.py 60.5 KiB
Newer Older
from __future__ import annotations
Andreas Klöckner's avatar
Andreas Klöckner committed

__copyright__ = "Copyright (C) 2014 Andreas Kloeckner"

__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

import re
Dong Zhuang's avatar
Dong Zhuang committed
from decimal import Decimal
from typing import (
    TYPE_CHECKING,
    Any,
    cast,
Andreas Klöckner's avatar
Andreas Klöckner committed
)
Dong Zhuang's avatar
Dong Zhuang committed

Andreas Klöckner's avatar
Andreas Klöckner committed
from crispy_forms.layout import Submit
from django import forms, http
from django.contrib import messages
from django.core.exceptions import (
    ObjectDoesNotExist,
    PermissionDenied,
    SuspiciousOperation,
Andreas Klöckner's avatar
Andreas Klöckner committed
)
from django.db import connection, transaction
from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse
Andreas Klöckner's avatar
Andreas Klöckner committed
from django.utils.timezone import now
from django.utils.translation import gettext, gettext_lazy as _, pgettext_lazy
Andreas Klöckner's avatar
Andreas Klöckner committed
from course.constants import participation_permission as pperm
from course.flow import adjust_flow_session_page_data
Andreas Klöckner's avatar
Andreas Klöckner committed
from course.models import (
    FlowPageVisit,
    FlowSession,
    GradeChange,
    GradeStateMachine,
    GradingOpportunity,
    Participation,
    grade_state_change_types,
    participation_status,
Andreas Klöckner's avatar
Andreas Klöckner committed
)
from course.utils import course_view, render_course_page
from course.views import get_now_or_fake_time
Andreas Klöckner's avatar
Andreas Klöckner committed
from relate.utils import (
    HTML5DateTimeInput,
    StyledForm,
    StyledModelForm,
    string_concat,
Andreas Klöckner's avatar
Andreas Klöckner committed
)

    from course.content import FlowDesc
    from course.models import Course, FlowPageVisitGrade
    from course.utils import CoursePageContext

# {{{ student grade book

@course_view
def view_participant_grades(pctx, participation_id=None):
    if pctx.participation is None:
        raise PermissionDenied(_("must be enrolled to view grades"))
    if participation_id is not None:
        grade_participation = Participation.objects.get(id=int(participation_id))
    else:
        grade_participation = pctx.participation

    is_privileged_view = pctx.has_permission(pperm.view_gradebook)
    if grade_participation != pctx.participation:
        if not is_privileged_view:
            raise PermissionDenied(_("may not view other people's grades"))
    # NOTE: It's important that these two queries are sorted consistently,
    # also consistently with the code below.
Dong Zhuang's avatar
Dong Zhuang committed

    gopp_extra_filter_kwargs = {}
    if not is_privileged_view:
        gopp_extra_filter_kwargs = {"shown_in_participant_grade_book": True}

    grading_opps = list(GradingOpportunity.objects
            .filter(
                course=pctx.course,
                shown_in_grade_book=True,
Dong Zhuang's avatar
Dong Zhuang committed
                **gopp_extra_filter_kwargs

    grade_changes = list(GradeChange.objects
            .filter(
                participation=grade_participation,
Dong Zhuang's avatar
Dong Zhuang committed
                opportunity__pk__in=[gopp.pk for gopp in grading_opps],
                opportunity__shown_in_grade_book=True)
            .order_by(
                "participation__id",
                "opportunity__identifier",
                "grade_time")
            .select_related("participation")
            .select_related("participation__user")
            .select_related("opportunity"))

    idx = 0

    grade_table = []
    for opp in grading_opps:
            if not (opp.shown_in_grade_book
                continue
        else:
            if not opp.shown_in_grade_book:
                continue

        while (
                idx < len(grade_changes)
                and grade_changes[idx].opportunity.identifier < opp.identifier
                ):
            idx += 1

        my_grade_changes = []
        while (
                idx < len(grade_changes)
                and grade_changes[idx].opportunity.pk == opp.pk):
            my_grade_changes.append(grade_changes[idx])
            idx += 1

        state_machine = GradeStateMachine()
        state_machine.consume(my_grade_changes)

        grade_table.append(
                GradeInfo(
                    opportunity=opp,
                    grade_state_machine=state_machine))

    return render_course_page(pctx, "course/gradebook-participant.html", {
        "grade_table": grade_table,
        "grade_participation": grade_participation,
        "grading_opportunities": grading_opps,
        "grade_state_change_types": grade_state_change_types,
        "is_privileged_view": is_privileged_view,
# {{{ participant list

@course_view
def view_participant_list(pctx):
    if not pctx.has_permission(pperm.view_gradebook):
        raise PermissionDenied(_("may not view grade book"))

    participations = list(Participation.objects
            .filter(
                course=pctx.course)
            .order_by("id")
            .select_related("user"))

    return render_course_page(pctx, "course/gradebook-participant-list.html", {
        "participations": participations,
        })

# }}}


# {{{ grading opportunity list

@course_view
def view_grading_opportunity_list(pctx):
    if not pctx.has_permission(pperm.view_gradebook):
        raise PermissionDenied(_("may not view grade book"))
    grading_opps = list(GradingOpportunity.objects
Loading
Loading full blame...