## $Id: vocabularies.py 7434 2011-12-22 07:39:00Z henrik $
##
## 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
##
"""Vocabularies and sources for the application section.
"""
from zope.component import getUtility
from zope.catalog.interfaces import ICatalog
from waeup.sirp.interfaces import SimpleSIRPVocabulary
from waeup.sirp.students.vocabularies import CertificateSource

#: Types of applications we support.
APPLICATION_TYPES = (
    ('General Studies', 'app','APP'),
    ('Pre-NCE Programme', 'prence','PRE'),
    ('Post UME Screening Test', 'pume','PUME'),
    ('Post UDE Screening', 'pude','PUDE'),
    ('Part Time Degree in Education', 'sandwich','SAND'),
    ('Part-Time Degree Programmes', 'pt','PTP'),
    ('Diploma Programmes', 'dp','DPP'),
    ('PCE Screening', 'pce','PCE'),
    ('Certificate Programmes', 'ct','CTP'),
    ('Common Entry Screening Test', 'cest','CEST'),
    )

#: A :class:`waeup.sirp.interfaces.SimpleSIRPVocabulary` of supported
#: application or screening types.
application_types_vocab = SimpleSIRPVocabulary(
    *[(x[0],x[1]) for x in APPLICATION_TYPES])
application_pins_vocab = SimpleSIRPVocabulary(
    *[(u"%s (%s)" % (x[2],x[0]),x[2]) for x in APPLICATION_TYPES])


class AppCatCertificateSource(CertificateSource):
    """An application certificate source delivers all courses which belong to
    a certain application_category.
    """
    def getValues(self, context):
        # appliction category not available when certificate was deleted.
        # shouldn't that info be part of applicant info instead?
        # when we cannot determine the appcat, we will display all courses.
        appcat = getattr(getattr(context, '__parent__', None),
                         'application_category', None)
        catalog = getUtility(ICatalog, name='certificates_catalog')
        result = catalog.searchResults(
            application_category=(appcat,appcat))
        result = sorted(result, key=lambda value: value.code)
        curr_course = context.course1
        if curr_course is not None and curr_course not in result:
            # display also current course even if it is not catalogued
            # (any more)
            result = [curr_course,] + result
        return result
