Skip to content
models.py 47.1 KiB
Newer Older
ifaint's avatar
ifaint committed
    due_time = models.DateTimeField(default=None, blank=True, null=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Due time'))
ifaint's avatar
ifaint committed
    creation_time = models.DateTimeField(default=now,
ifaint's avatar
ifaint committed
            verbose_name=_('Creation time'))
ifaint's avatar
ifaint committed
    shown_in_grade_book = models.BooleanField(default=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Shown in grade book'))
ifaint's avatar
ifaint committed
    shown_in_student_grade_book = models.BooleanField(default=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Shown in student grade book'))
ifaint's avatar
ifaint committed
        verbose_name = _("Grading opportunity")
        verbose_name_plural = _("Grading opportunities")
        ordering = ("course", "due_time", "identifier")
        unique_together = (("course", "identifier"),)
Dong Zhuang's avatar
Dong Zhuang committed
                # Translators: For GradingOpportunity
                _("%(opportunity_name)s (%(opportunity_id)s) in %(course)s")
                % {
                    "opportunity_name": self.name,
                    "opportunity_id": self.identifier,
                    "course": self.course})


class GradeChange(models.Model):
    """Per 'grading opportunity', each participant may accumulate multiple grades
    that are aggregated according to :attr:`GradingOpportunity.aggregation_strategy`.

    In addition, for each opportunity, grade changes are grouped by their 'attempt'
    identifier, where later grades with the same :attr:`attempt_id` supersede earlier
    ones.
    """
ifaint's avatar
ifaint committed
    opportunity = models.ForeignKey(GradingOpportunity,
ifaint's avatar
ifaint committed
            verbose_name=_('Grading opportunity'))
ifaint's avatar
ifaint committed
    participation = models.ForeignKey(Participation,
ifaint's avatar
ifaint committed
            verbose_name=_('Participation'))

    state = models.CharField(max_length=50,
ifaint's avatar
ifaint committed
            choices=GRADE_STATE_CHANGE_CHOICES,
            # Translators: something like 'status'.
ifaint's avatar
ifaint committed
            verbose_name=_('State'))
    attempt_id = models.CharField(max_length=50, null=True, blank=True,
            default="main",
ifaint's avatar
ifaint committed
            # Translators: help text of "attempt_id" in GradeChange class
ifaint's avatar
ifaint committed
            help_text=_("Grade changes are grouped by their 'attempt ID' "
            "where later grades with the same attempt ID supersede earlier "
ifaint's avatar
ifaint committed
            "ones."),
ifaint's avatar
ifaint committed
            verbose_name=_('Attempt ID'))
    points = models.DecimalField(max_digits=10, decimal_places=2,
ifaint's avatar
ifaint committed
            blank=True, null=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Points'))
ifaint's avatar
ifaint committed
    max_points = models.DecimalField(max_digits=10, decimal_places=2,
ifaint's avatar
ifaint committed
            verbose_name=_('Max points'))
ifaint's avatar
ifaint committed
    comment = models.TextField(blank=True, null=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Comment'))
ifaint's avatar
ifaint committed
    due_time = models.DateTimeField(default=None, blank=True, null=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Due time'))
ifaint's avatar
ifaint committed
    creator = models.ForeignKey(User, null=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Creator'))
ifaint's avatar
ifaint committed
    grade_time = models.DateTimeField(default=now, db_index=True,
ifaint's avatar
ifaint committed
            verbose_name=_('Grade time'))
    flow_session = models.ForeignKey(FlowSession, null=True, blank=True,
ifaint's avatar
ifaint committed
            related_name="grade_changes",
ifaint's avatar
ifaint committed
            verbose_name=_('Flow session'))
ifaint's avatar
ifaint committed
        verbose_name = _("Grade change")
        verbose_name_plural = _("Grade changes")
        ordering = ("opportunity", "participation", "grade_time")

    def __unicode__(self):
        # Translators: information for GradeChange
        return _("%(participation)s %(state)s on %(opportunityname)s") % {
            'participation': self.participation,
            'state': self.state,
            'opportunityname': self.opportunity.name}

    def clean(self):
        if self.opportunity.course != self.participation.course:
ifaint's avatar
ifaint committed
            raise ValidationError(_("Participation and opportunity must live "
                    "in the same course"))
    def percentage(self):
        if self.max_points is not None and self.points is not None:
            return 100*self.points/self.max_points
        else:
            return None

# }}}


# {{{ grade state machine

class GradeStateMachine(object):
    def __init__(self):
        self.opportunity = None

        self.state = None
        self._clear_grades()
        self.due_time = None
        self.last_graded_time = None
        self.last_report_time = None

        # applies to *all* grade changes
        self._last_grade_change_time = None

    def _clear_grades(self):
        self.state = None
        self.last_grade_time = None
        self.valid_percentages = []
        self.attempt_id_to_gchange = {}
    def _consume_grade_change(self, gchange, set_is_superseded):
        if self.opportunity is None:
            self.opportunity = gchange.opportunity
            self.due_time = self.opportunity.due_time
        else:
            assert self.opportunity.pk == gchange.opportunity.pk

        # check that times are increasing
        if self._last_grade_change_time is not None:
            assert gchange.grade_time > self._last_grade_change_time
            self._last_grade_change_time = gchange.grade_time

        if gchange.state == grade_state_change_types.graded:
            if self.state == grade_state_change_types.unavailable:
                raise ValueError(
                        _("cannot accept grade once opportunity has been "
                            "marked 'unavailable'"))
            if self.state == grade_state_change_types.exempt:
                raise ValueError(
                        _("cannot accept grade once opportunity has been "
ifaint's avatar
ifaint committed
                        "marked 'exempt'"))
            #if self.due_time is not None and gchange.grade_time > self.due_time:
                #raise ValueError("cannot accept grade after due date")

            self.state = gchange.state
            if gchange.attempt_id is not None:
                if (set_is_superseded and
                        gchange.attempt_id in self.attempt_id_to_gchange):
                    self.attempt_id_to_gchange[gchange.attempt_id] \
                            .is_superseded = True
                self.attempt_id_to_gchange[gchange.attempt_id] \
                        = gchange
            else:
                self.valid_percentages.append(gchange.percentage())
            self.last_graded_time = gchange.grade_time

        elif gchange.state == grade_state_change_types.unavailable:
            self._clear_grades()
            self.state = gchange.state

        elif gchange.state == grade_state_change_types.do_over:
            self._clear_grades()

        elif gchange.state == grade_state_change_types.exempt:
            self._clear_grades()
            self.state = gchange.state

        elif gchange.state == grade_state_change_types.report_sent:
            self.last_report_time = gchange.grade_time

        elif gchange.state == grade_state_change_types.extension:
            self.due_time = gchange.due_time

        elif gchange.state in [
                grade_state_change_types.grading_started,
                grade_state_change_types.retrieved,
                ]:
            pass
        else:
            raise RuntimeError(
                    _("invalid grade change state '%s'") % gchange.state)
    def consume(self, iterable, set_is_superseded=False):
        for gchange in iterable:
            gchange.is_superseded = False
            self._consume_grade_change(gchange, set_is_superseded)

        valid_grade_changes = sorted(
                (gchange
                for gchange in self.attempt_id_to_gchange.values()
                if gchange.percentage() is not None),
                key=lambda gchange: gchange.grade_time)

        self.valid_percentages.extend(
                gchange.percentage()
        del self.attempt_id_to_gchange

        return self

    def percentage(self):
        """
        :return: a percentage of achieved points, or *None*
        """
        if self.opportunity is None or not self.valid_percentages:
            return None

        strategy = self.opportunity.aggregation_strategy

        if strategy == grade_aggregation_strategy.max_grade:
            return max(self.valid_percentages)
        elif strategy == grade_aggregation_strategy.min_grade:
            return min(self.valid_percentages)
        elif strategy == grade_aggregation_strategy.avg_grade:
            return sum(self.valid_percentages)/len(self.valid_percentages)
        elif strategy == grade_aggregation_strategy.use_earliest:
            return self.valid_percentages[0]
        elif strategy == grade_aggregation_strategy.use_latest:
            return self.valid_percentages[-1]
        else:
            raise ValueError(
                    _("invalid grade aggregation strategy '%s'") % strategy)
    def stringify_state(self):
        if self.state is None:
            return u"- ∅ -"
        elif self.state == grade_state_change_types.exempt:
ifaint's avatar
ifaint committed
            return "_((exempt))"
        elif self.state == grade_state_change_types.graded:
            if self.valid_percentages:
                result = "%.1f%%" % self.percentage()
                if len(self.valid_percentages) > 1:
                    result += " (/%d)" % len(self.valid_percentages)
                return result
            else:
                return u"- ∅ -"
ifaint's avatar
ifaint committed
            return "_((other state))"
    def stringify_machine_readable_state(self):
        if self.state is None:
            return u"NONE"
        elif self.state == grade_state_change_types.exempt:
            return "EXEMPT"
        elif self.state == grade_state_change_types.graded:
            if self.valid_percentages:
                return "%.3f" % self.percentage()
            else:
                return u"NONE"
        else:
            return u"OTHER_STATE"

    def stringify_percentage(self):
        if self.state == grade_state_change_types.graded:
            if self.valid_percentages:
                return "%.1f" % self.percentage()
            else:
                return u""
        else:
            return ""
# }}}


# {{{ flow <-> grading integration

def get_flow_grading_opportunity(course, flow_id, flow_desc, grading_rule):
    from course.utils import FlowSessionGradingRule
    assert isinstance(grading_rule, FlowSessionGradingRule)

    gopp, created = GradingOpportunity.objects.get_or_create(
            course=course,
            identifier=grading_rule.grade_identifier,
                    # Translators: display the name of a flow
                    _("Flow: %(flow_desc_title)s")
                    % {"flow_desc_title": flow_desc.title}),
                flow_id=flow_id,
                aggregation_strategy=grading_rule.grade_aggregation_strategy,

# {{{ XMPP log

class InstantMessage(models.Model):
ifaint's avatar
ifaint committed
    participation = models.ForeignKey(Participation,
ifaint's avatar
ifaint committed
            verbose_name=_('Participation'))
ifaint's avatar
ifaint committed
    text = models.CharField(max_length=200,
ifaint's avatar
ifaint committed
            verbose_name=_('Text'))
ifaint's avatar
ifaint committed
    time = models.DateTimeField(default=now,
ifaint's avatar
ifaint committed
            verbose_name=_('Time'))
ifaint's avatar
ifaint committed
        verbose_name = _("Instant message")
        verbose_name_plural = _("Instant messages")
        ordering = ("participation__course", "time")

    def __unicode__(self):
        return "%s: %s" % (self.participation, self.text)

# }}}

# vim: foldmethod=marker