:mod:`waeup.kofa.university.certificate` -- Certificates for Kofa
******************************************************************

.. module:: waeup.kofa.university.certificate

Components that represent and manage certificates.

.. :doctest:
.. :layer: waeup.kofa.testing.KofaUnitTestLayer


Content Classes (models and containers)
=======================================


:class:`Certificate`
--------------------

.. class:: Certificate([code=u'NA',[ title=u'Unnamed Certificate',[ study_mode=None,[ start_level=None,[ end_level=None,[ application_category=None,[ m_prefix=u'']]]]]]])

   Create a certificate object with the given parameters.

   .. attribute:: grok.implements(ICertificate)

   All parameters are optional:

     >>> from waeup.kofa.university.certificate import Certificate
     >>> mycertificate = Certificate()

   Certificates have the attributes required by the `ICertificate` interface:

     >>> from waeup.kofa.university.interfaces import ICertificate
     >>> ICertificate.providedBy(mycertificate)
     True

     >>> from zope.interface.verify import verifyObject
     >>> verifyObject(ICertificate, mycertificate)
     True

   Beside the attributes, certificates are containers for
   certificate-courses (see :class:`CertificateCourse`). Each
   certificate course can be accessed by the code of the course it wraps.

   .. attribute:: title

      Each certificate has a title:

        >>> mycertificate.title
        u'Unnamed Certificate'

   .. attribute:: code

      Each certificate holds a code, which might be a shortcut or
      abbreviation of the real certificate name. By default the code
      is ``NA`` (=not assigned):

        >>> mycertificate.code
        u'NA'


   .. attribute:: study_mode

      Each :class:`Certificate` instance has a study mode:

        >>> print mycertificate.study_mode
        None

      .. XXX: This is not a proper description

   .. attribute:: start_level

      Each :class:`Certificate` instance has a start level:

        >>> print mycertificate.start_level
        None

      .. XXX: This is not a proper description

   .. attribute:: end_level

      Each :class:`Certificate` instance has a end level:

        >>> print mycertificate.end_level
        None

      .. XXX: This is not a proper description
   

   .. attribute:: application_category

      Each :class:`Certificate` instance has an application category:

        >>> print mycertificate.application_category
        None

      .. XXX: This is not a proper description

   .. method:: addCertCourse(course[, level=100,[ mandatory=True]])

      Add a certificate course. A course is an object implementing
      :class:`waeup.kofa.interfaces.ICourse`.

      Please don't be confused by the term 'certificate course'.
      As course objects will normaly be  stored
      in a department, the certificate course refers to
      the 'real' course in the department container.

   .. method:: delCertCourses(code)

      Remove a certificate course from a certificate.

      The certificate course must be given by its code number.

:class:`CertificateCourse`
--------------------------

.. class:: CertificateCourse(course[, level=100[, mandatory=True]])

   Create a certificate course.

   A certificate course are held in
   certificates and refer to an existing :class:`Course` instance held
   elsewhere.

   A certificate can require several certificate courses and one
   certificate course can be required by several certificates.

   .. attribute:: course

      An instance of :class:`ICourse`.

   .. attribute:: level

      An integer telling the level to which this course applies.

   .. attribute:: mandatory

      A bool stating whether this course is required or optional to
      get the certificate.



Utilities
=========

:class:`CertificateFactory`
---------------------------

.. class:: CertificateFactory()

   .. attribute:: grok.name(u'waeup.Certificate')

   .. attribute:: grok.implements(IFactory)

   A named utility to deliver new instances of :class:`Certificate`
   without the need to import the implementation before:

     >>> from zope.component import createObject
     >>> mycertificate = createObject(u'waeup.Certificate')
     >>> mycertificate
     <waeup.kofa.university.certificate.Certificate object at 0x...>

   The factory complies with the specifications from the
   :class:`IFactory` insterface:

     >>> from zope.interface.verify import verifyClass
     >>> from zope.component.interfaces import IFactory
     >>> from waeup.kofa.university.certificate import CertificateFactory
     >>> verifyClass(IFactory, CertificateFactory)
     True

   This means also, that we can get the interfaces of the created
   object from the factory:

     >>> certificate_factory = CertificateFactory()
     >>> certificate_factory.getInterfaces()
     <implementedBy waeup.kofa.university.certificate.Certificate>


:class:`CertificateCourseFactory`
---------------------------------

.. class:: CertificateCourseFactory()

   .. attribute:: grok.name(u'waeup.CertificateCourse')

   .. attribute:: grok.implements(IFactory)

   A named utility to deliver new instances of :class:`CertificateCourse`
   without the need to import the implementation before:

     >>> from zope.component import createObject
     >>> mycertificatecourse = createObject(u'waeup.CertificateCourse')
     >>> mycertificatecourse
     <waeup.kofa.university.certificate.CertificateCourse object at 0x...>

   The factory complies with the specifications from the
   :class:`IFactory` insterface:

     >>> from zope.interface.verify import verifyClass
     >>> from zope.component.interfaces import IFactory
     >>> from waeup.kofa.university.certificate import CertificateCourseFactory
     >>> verifyClass(IFactory, CertificateCourseFactory)
     True

   This means also, that we can get the interfaces of the created
   object from the factory:

     >>> certcourse_factory = CertificateCourseFactory()
     >>> certcourse_factory.getInterfaces()
     <implementedBy waeup.kofa.university.certificate.CertificateCourse>


Examples
========

Certificates
------------

We can create certificates:

    >>> from waeup.kofa.university.certificate import Certificate
    >>> mycertificate = Certificate()
    >>> mycertificate
    <waeup.kofa.university.certificate.Certificate object at 0x...>

Another way to create certificates is by asking for a factory called
``waeup.Certificate``. This way we can create a factory without
importing a class:

    >>> from zope.component import createObject
    >>> mycertificate = createObject(u'waeup.Certificate')
    >>> mycertificate
    <waeup.kofa.university.certificate.Certificate object at 0x...>

CertificateCourses
------------------

:class:`CertificateCourse` instances comply with the
:class:`ICertificateCourse` interface:

    >>> from waeup.kofa.university.interfaces import ICertificateCourse
    >>> from waeup.kofa.university.certificate import CertificateCourse
    >>> mycertcourse = CertificateCourse(None, 200, False)
    >>> ICertificateCourse.providedBy(mycertcourse)
    True

    >>> from zope.interface.verify import verifyObject
    >>> verifyObject(ICertificateCourse, mycertcourse)
    True

Also instances of :class:`CertificateCourse` can be created by asking
the component architechture:

    >>> from zope.component import createObject
    >>> mycertcourse = createObject(u'waeup.CertificateCourse')
    >>> mycertcourse
    <waeup.kofa.university.certificate.CertificateCourse object at 0x...>

.. _removecertificatecourses:

Persistence of certificate courses
----------------------------------

If a certificate course requires a certain course and this is course
is deleted, also the referring certificate course is deleted.

We setup a data structure that reflects typical usage. It looks like
this::

    Department-Instance
    |
    +---> courses
    |        |
    |        +--------------------> Course-Instance
    |                                        ^
    +---> certificates                       |
             |                               |
             +-----> Certificate-Instance    |
                        |                    |
                        +------> Certificate-Course

The certifcate-Course here refers to a Course-Instance.

In Python we build such a structure like this (from top to bottom):

    >>> from zope.component import createObject
    >>> mydept = createObject('waeup.Department')

In real world use this data will be stored in a ZODB. We setup our own
litte ZODB backend (which is easy!):

    >>> from ZODB import FileStorage, DB
    >>> dbpath = 'tinyData.fs'
    >>> class TinyZODB(object):
    ...   def __init__(self, path=dbpath):
    ...     self.storage = FileStorage.FileStorage(path)
    ...     self.db = DB(self.storage)
    ...     self.connection = self.db.open()
    ...     self.dbroot = self.connection.root()
    ...   def close(self):
    ...     self.connection.close()
    ...     self.db.close()
    ...     self.storage.close()

Now we can use this ZODB as backend database and store our data
structure:

    >>> import transaction
    >>> db = TinyZODB()
    >>> dbroot = db.dbroot
    >>> dbroot['mydept'] = mydept
    >>> mycourse = createObject('waeup.Course')
    >>> mycourse.code = 'MYCOURSE'
    >>> mydept.courses.addCourse(mycourse)
    >>> mycert = createObject('waeup.Certificate')
    >>> mycert.code = 'MYCERT'
    >>> mydept.certificates.addCertificate(mycert)
    >>> mycert.addCertCourse(mycourse)

    >>> transaction.commit()

The data is now stored in the ZODB. We can close the DB, reopen it
later and the data will still be there:

    >>> db.close()
    >>> newdb = TinyZODB()
    >>> newdbroot = newdb.dbroot
    >>> list(newdbroot)
    ['mydept']

The certificate course we stored in the certificate is indeed a
referrer of the course, not a copy of it:

    >>> course = newdbroot['mydept'].courses['MYCOURSE']
    >>> certcourse = newdbroot['mydept'].certificates['MYCERT']['MYCOURSE_100']
    >>> certcourse.course is course
    True

So, we can be sure that modifications to the course are immediately
reflected in the certcourse.

