## Copyright (C) 2011 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 the student study courses
and contains the (student) study level objects.
"""
import grok
from grok import index
from zope.component.interfaces import IFactory
from zope.securitypolicy.interfaces import IPrincipalRoleManager
from waeup.sirp.students.interfaces import (
    IStudentStudyCourse, IStudentNavigation, IStudentStudyLevel)
from waeup.sirp.students.studylevel import CourseTicket
from waeup.sirp.utils.helpers import attrs_to_fields

class StudentStudyCourse(grok.Container):
    """This is a container for study levels.
    """
    grok.implements(IStudentStudyCourse, IStudentNavigation)
    grok.provides(IStudentStudyCourse)

    def __init__(self):
        super(StudentStudyCourse, self).__init__()
        return

    def getStudent(self):
        return self.__parent__

    def addStudentStudyLevel(self, cert, studylevel):
        """Add a study level object.
        """
        if not IStudentStudyLevel.providedBy(studylevel):
            raise TypeError(
                'StudentStudyCourses contain only IStudentStudyLevel instances')
        self[str(studylevel.level)] = studylevel

        #Create course tickets automatically
        for key, val in cert.items():
            if val.level != studylevel.level:
                continue
            ticket = CourseTicket()
            ticket.code = val.getCourseCode()
            ticket.automatic = True
            ticket.core_or_elective = val.core_or_elective
            ticket.title = val.course.title
            ticket.faculty = val.course.__parent__.__parent__.__parent__.title
            ticket.department = val.course.__parent__.__parent__.title
            ticket.credits = val.course.credits
            ticket.passmark = val.course.passmark
            ticket.semester = val.course.semester
            self[str(studylevel.level)][val.getCourseCode()] = ticket
        return

StudentStudyCourse = attrs_to_fields(StudentStudyCourse)

#@grok.subscribe(IStudentStudyCourse, grok.IObjectAddedEvent)
#def handle_studycourse_added(obj, event):
#    """When a studycourse is added, update the local roles.
#    """
#    cert = getattr(obj, 'certificate', None)
#    if cert is None:
#        # cert never set yet, no need to update local roles.
#        return
#    update_local_roles(obj)
#    return

@grok.subscribe(IStudentStudyCourse, grok.IObjectModifiedEvent)
def handle_studycourse_modified(obj, event):
    """When a studycourse is changed, also update the local roles.
    """
    update_local_roles(obj)
    return

def update_local_roles(studycourse):
    """Update local roles of given studycourse.
    """
    student = getattr(studycourse, '__parent__', None)
    if student is None:
        # not part of student, no need to update local roles
        return
    replace_local_clearance_officers(student)
    return

def replace_local_clearance_officers(student):
    """Set local roles of given student.

    The local roles are determined by looking up desired
    principals. Then we remove those principals from local roles that
    are not wanted/outdated and add those wanted.
    """
    prm = IPrincipalRoleManager(student)
    curr_officers = prm.getPrincipalsForRole(
        'waeup.local.ClearanceOfficer')
    new_officers = get_valid_clearance_officers(student)
    #print "STUD %s, CURR: %s, NEW: %s" % (student, curr_officers,
    #                                      new_officers)
    for officer in curr_officers:
        if officer in new_officers:
            continue
        prm.unsetRoleForPrincipal('waeup.local.ClearanceOfficer', officer)
    for officer in new_officers:
        if officer in curr_officers:
            continue
        prm.assignRoleToPrincipal('waeup.local.ClearanceOfficer', officer)

    return

def get_valid_clearance_officers(student):
    """Lookup desired clearance officers for `student`.

    We look up the connected certificate, then its dept. and faculty
    to see which local roles are required: students get the clearance
    officers assigned to their certificate.

    Not sure, though, whether we really have to set the real clearance
    officer role for the wanted principals on the dept and faculty.
    """
    cert = getattr(student, 'certificate', None)
    if cert is None:
        return []
    dept = getattr(getattr(cert, '__parent__', None), '__parent__', None)
    fac = getattr(dept, '__parent__', None)
    if dept is None or fac is None:
        # not a serious cert
        return []
    # get officers (principals) responsible for the dept and faculty
    dept_officers = IPrincipalRoleManager(dept).getPrincipalsForRole(
        'waeup.local.ClearanceOfficer')
    fac_officers =  IPrincipalRoleManager(fac).getPrincipalsForRole(
        'waeup.local.ClearanceOfficer')
    return dept_officers + fac_officers

class StudentStudyCourseFactory(grok.GlobalUtility):
    """A factory for students.
    """
    grok.implements(IFactory)
    grok.name(u'waeup.StudentStudyCourse')
    title = u"Create a new student study course.",
    description = u"This factory instantiates new student study course instances."

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

    def getInterfaces(self):
        return implementedBy(StudentStudyCourse)