## $Id: studylevel.py 16817 2022-02-17 09:09:52Z henrik $
##
## Copyright (C) 2012 Uli Fouquet & Henrik Bettermann
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##
"""
Container which holds the data of a student study level
and contains the course tickets.
"""
import grok
from zope.component.interfaces import IFactory
from zope.interface import implementedBy
from waeup.kofa.utils.helpers import attrs_to_fields
from waeup.kofa.students.studylevel import (
    StudentStudyLevel, CourseTicket,
    CourseTicketFactory, StudentStudyLevelFactory)
from waeup.kofa.students.interfaces import IStudentNavigation
from kofacustom.dspg.students.interfaces import (
    ICustomStudentStudyLevel, ICustomCourseTicket)


class CustomStudentStudyLevel(StudentStudyLevel):
    """This is a container for course tickets.
    """
    grok.implements(ICustomStudentStudyLevel, IStudentNavigation)
    grok.provides(ICustomStudentStudyLevel)

    @property
    def total_credits_s1(self):
        total = 0
        for ticket in self.values():
            if ticket.semester == 1 and not ticket.outstanding:
                total += ticket.credits
        return total

    @property
    def total_credits_s2(self):
        total = 0
        for ticket in self.values():
            if ticket.semester == 2 and not ticket.outstanding:
                total += ticket.credits
        return total

    @property
    def gpa_params(self):
        """Calculate gpa parameters for this level.
        """
        credits_weighted = 0.0
        credits_counted = 0
        level_gpa = 0.0
        for ticket in self.values():
            if ticket.total_score is not None:
                credits_counted += ticket.credits
                credits_weighted += ticket.credits * ticket.weight
        if credits_counted:
            level_gpa = round(credits_weighted / credits_counted, 2)
        # Override level_gpa if value has been imported
        # (not implemented in base package)
        imported_gpa = getattr(self, 'imported_gpa', None)
        if imported_gpa:
            level_gpa = imported_gpa
        return level_gpa, credits_counted, credits_weighted


CustomStudentStudyLevel = attrs_to_fields(
    CustomStudentStudyLevel, omit=['total_credits', 'gpa',
        'total_credits_s1', 'total_credits_s2'])

class CustomStudentStudyLevelFactory(StudentStudyLevelFactory):
    """A factory for student study levels.
    """

    def __call__(self, *args, **kw):
        return CustomStudentStudyLevel()

    def getInterfaces(self):
        return implementedBy(CustomStudentStudyLevel)

class CustomCourseTicket(CourseTicket):
    """
    """
    grok.implements(ICustomCourseTicket, IStudentNavigation)
    grok.provides(ICustomCourseTicket)


    @property
    def _getGradeWeightFromScore(self):
        """DSPG Course Grading System
        """
        if self.total_score is None:
            return (None, None)
        if self.total_score >= 75:
            return ('A',4)
        if self.total_score >= 70:
            return ('AB',3.5)
        if self.total_score >= 65:
            return ('B',3.25)
        if self.total_score >= 60:
            return ('BC',3.0)
        if self.total_score >= 55:
            return ('C',2.75)
        if self.total_score >= 50:
            return ('CD',2.5)
        if self.total_score >= 45:
            return ('D',2.25)
        if self.total_score >= 40:
            return ('E',2)
        return ('F',0)


CustomCourseTicket = attrs_to_fields(CustomCourseTicket)

class CustomCourseTicketFactory(CourseTicketFactory):
    """A factory for student study levels.
    """

    def __call__(self, *args, **kw):
        return CustomCourseTicket()

    def getInterfaces(self):
        return implementedBy(CustomCourseTicket)
