source: main/waeup.kofa/trunk/src/waeup/kofa/students/studylevel.py @ 9680

Last change on this file since 9680 was 9532, checked in by Henrik Bettermann, 12 years ago

Limit the total number of credits per level.

  • Property svn:keywords set to Id
File size: 6.3 KB
Line 
1## $Id: studylevel.py 9532 2012-11-05 07:19:54Z henrik $
2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18"""
19Container which holds the data of a student study level
20and contains the course tickets.
21"""
22import grok
23from zope.component.interfaces import IFactory
24from zope.component import createObject
25from zope.interface import implementedBy
26from waeup.kofa.students.interfaces import (
27    IStudentStudyLevel, IStudentNavigation, ICourseTicket)
28from waeup.kofa.utils.helpers import attrs_to_fields
29from waeup.kofa.students.vocabularies import StudyLevelSource
30
31class StudentStudyLevel(grok.Container):
32    """This is a container for course tickets.
33    """
34    grok.implements(IStudentStudyLevel, IStudentNavigation)
35    grok.provides(IStudentStudyLevel)
36
37    def __init__(self):
38        super(StudentStudyLevel, self).__init__()
39        self.level = None
40        return
41
42    @property
43    def student(self):
44        try:
45            return self.__parent__.__parent__
46        except AttributeError:
47            return None
48
49    @property
50    def certcode(self):
51        try:
52            return self.__parent__.certificate.code
53        except AttributeError:
54            return None
55
56    @property
57    def number_of_tickets(self):
58        return len(self)
59
60    @property
61    def total_credits(self):
62        total = 0
63        for ticket in self.values():
64            total += ticket.credits
65        return total
66
67    @property
68    def is_current_level(self):
69        try:
70            return self.__parent__.current_level == self.level
71        except AttributeError:
72            return False
73
74    def writeLogMessage(self, view, message):
75        return self.__parent__.__parent__.writeLogMessage(view, message)
76
77    @property
78    def level_title(self):
79        studylevelsource = StudyLevelSource()
80        return studylevelsource.factory.getTitle(self.__parent__, self.level)
81
82    def addCourseTicket(self, ticket, course):
83        """Add a course ticket object.
84        """
85        if not ICourseTicket.providedBy(ticket):
86            raise TypeError(
87                'StudentStudyLeves contain only ICourseTicket instances')
88        ticket.code = course.code
89        ticket.title = course.title
90        ticket.fcode = course.__parent__.__parent__.__parent__.code
91        ticket.dcode = course.__parent__.__parent__.code
92        ticket.credits = course.credits
93        ticket.passmark = course.passmark
94        ticket.semester = course.semester
95        self[ticket.code] = ticket
96        return
97
98    def addCertCourseTickets(self, cert):
99        """Collect all certificate courses and create course
100        tickets automatically.
101        """
102        if cert is not None:
103            for key, val in cert.items():
104                if val.level != self.level:
105                    continue
106                ticket = createObject(u'waeup.CourseTicket')
107                ticket.automatic = True
108                ticket.mandatory = val.mandatory
109                ticket.carry_over = False
110                self.addCourseTicket(ticket, val.course)
111        return
112
113StudentStudyLevel = attrs_to_fields(StudentStudyLevel)
114
115class StudentStudyLevelFactory(grok.GlobalUtility):
116    """A factory for student study levels.
117    """
118    grok.implements(IFactory)
119    grok.name(u'waeup.StudentStudyLevel')
120    title = u"Create a new student study level.",
121    description = u"This factory instantiates new student study level instances."
122
123    def __call__(self, *args, **kw):
124        return StudentStudyLevel()
125
126    def getInterfaces(self):
127        return implementedBy(StudentStudyLevel)
128
129class CourseTicket(grok.Model):
130    """This is a course ticket which allows the
131    student to attend the course. Lecturers will enter scores and more at
132    the end of the term.
133
134    A course ticket contains a copy of the original course and
135    certificate course data. If the courses and/or the referrin certificate
136    courses are removed, the corresponding tickets remain unchanged.
137    So we do not need any event
138    triggered actions on course tickets.
139    """
140    grok.implements(ICourseTicket, IStudentNavigation)
141    grok.provides(ICourseTicket)
142
143    def __init__(self):
144        super(CourseTicket, self).__init__()
145        self.code = None
146        return
147
148    @property
149    def student(self):
150        """Get the associated student object.
151        """
152        try:
153            return self.__parent__.__parent__.__parent__
154        except AttributeError:
155            return None
156
157    @property
158    def certcode(self):
159        try:
160            return self.__parent__.__parent__.certificate.code
161        except AttributeError:
162            return None
163
164    def writeLogMessage(self, view, message):
165        return self.__parent__.__parent__.__parent__.writeLogMessage(view, message)
166
167    def getLevel(self):
168        """Returns the id of the level the ticket has been added to.
169        """
170        # XXX: shouldn't that be an attribute?
171        try:
172            return self.__parent__.level
173        except AttributeError:
174            return None
175
176    def getLevelSession(self):
177        """Returns the session of the level the ticket has been added to.
178        """
179        # XXX: shouldn't that be an attribute?
180        try:
181            return self.__parent__.level_session
182        except AttributeError:
183            return None
184
185
186CourseTicket = attrs_to_fields(CourseTicket)
187
188class CourseTicketFactory(grok.GlobalUtility):
189    """A factory for student study levels.
190    """
191    grok.implements(IFactory)
192    grok.name(u'waeup.CourseTicket')
193    title = u"Create a new course ticket.",
194    description = u"This factory instantiates new course ticket instances."
195
196    def __call__(self, *args, **kw):
197        return CourseTicket()
198
199    def getInterfaces(self):
200        return implementedBy(CourseTicket)
Note: See TracBrowser for help on using the repository browser.