## $Id: studylevel.py 9692 2012-11-20 06:37:11Z 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.component import createObject
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 waeup.aaue.students.interfaces import (
    ICustomStudentStudyLevel, ICustomCourseTicket)


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

    def addCertCourseTickets(self, cert):
        """Collect all certificate courses and create course
        tickets automatically.

        Add ticket only if student has paid for the course.semester.
        """
        payments = self.student.getPaymentTuples()
        if cert is not None:
            for key, val in cert.items():
                if val.level != self.level:
                    continue
                if val.course.semester == 2 and \
                    not (self.student.current_session,
                    'schoolfee_2', 'paid') in payments:
                    continue
                ticket = createObject(u'waeup.CourseTicket')
                ticket.automatic = True
                ticket.mandatory = val.mandatory
                ticket.carry_over = False
                self.addCourseTicket(ticket, val.course)
        return

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

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):
    """This is a course ticket which allows the
    student to attend the course. Lecturers will enter scores and more at
    the end of the term.

    A course ticket contains a copy of the original course and
    course referrer data. If the courses and/or their referrers are removed, the
    corresponding tickets remain unchanged. So we do not need any event
    triggered actions on course tickets.
    """
    grok.implements(ICustomCourseTicket, IStudentNavigation)
    grok.provides(ICustomCourseTicket)

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)
