## $Id: vocabularies.py 7688 2012-02-23 12:25:23Z 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 academics section.
"""
from zc.sourcefactory.basic import BasicSourceFactory
from zc.sourcefactory.contextual import BasicContextualSourceFactory
from zope.catalog.interfaces import ICatalog
from zope.component import getUtility
from waeup.sirp.interfaces import SimpleSIRPVocabulary, ISIRPUtils
from waeup.sirp.interfaces import MessageFactory as _

course_levels = SimpleSIRPVocabulary(
    (_('Pre-Studies'),10),
    (_('100 (Year 1)'),100),
    (_('200 (Year 2)'),200),
    (_('300 (Year 3)'),300),
    (_('400 (Year 4)'),400),
    (_('500 (Year 5)'),500),
    (_('600 (Year 6)'),600),
    (_('700 (Year 7)'),700),
    (_('800 (Year 8)'),800),
    )

class SemesterSource(BasicContextualSourceFactory):
    """An institution type source delivers semester or term descriptors.
    """
    def getValues(self, context):
        semesters_dict = getUtility(ISIRPUtils).getSemesterDict()
        return semesters_dict.keys()

    def getToken(self, context, value):
        return str(value)

    def getTitle(self, context, value):
        semesters_dict = getUtility(ISIRPUtils).getSemesterDict()
        return semesters_dict[value]

class InstTypeSource(BasicContextualSourceFactory):
    """An institution type source delivers types of institutions
    in the portal.
    """
    def getValues(self, context):
        insttypes_dict = getUtility(ISIRPUtils).getInstTypeDict()
        return sorted(insttypes_dict.keys())

    def getToken(self, context, value):
        return value

    def getTitle(self, context, value):
        insttypes_dict = getUtility(ISIRPUtils).getInstTypeDict()
        return insttypes_dict[value]

class AppCatSource(BasicContextualSourceFactory):
    """A application category source delivers all application categories
    provided in the portal.
    """
    def getValues(self, context):
        appcats_dict = getUtility(ISIRPUtils).getAppCatDict()
        return sorted(appcats_dict.keys())

    def getToken(self, context, value):
        return value

    def getTitle(self, context, value):
        appcats_dict = getUtility(ISIRPUtils).getAppCatDict()
        return appcats_dict[value]

class StudyModeSource(BasicContextualSourceFactory):
    """A study modes source delivers all study modes provided
    in the portal.
    """
    def getValues(self, context):
        studymodes_dict = getUtility(ISIRPUtils).getStudyModesDict()
        return sorted(studymodes_dict.keys())

    def getToken(self, context, value):
        return value

    def getTitle(self, context, value):
        studymodes_dict = getUtility(ISIRPUtils).getStudyModesDict()
        return studymodes_dict[value]

class CourseSource(BasicSourceFactory):
    """A course source delivers all courses inside the portal by looking
       up a catalog.
    """
    def getValues(self):
        catalog = getUtility(ICatalog, name='courses_catalog')
        return sorted(list(
                catalog.searchResults(
                    code=('', 'z*'))),key=lambda value: value.code)

    def getToken(self, value):
        return value.code

    def getTitle(self, value):
        return "%s - %s" % (value.code, value.title[:64])
