source: main/waeup.kofa/trunk/src/waeup/kofa/students/studycourse.py @ 14368

Last change on this file since 14368 was 14361, checked in by Henrik Bettermann, 8 years ago

AAUE has requested to display only two digits of the GPA without rounding. _display_gpa can be used to customize this.

  • Property svn:keywords set to Id
File size: 6.4 KB
Line 
1## $Id: studycourse.py 14361 2016-12-22 19:58:41Z 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 the student study courses
20and contains the (student) study level objects.
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    IStudentStudyCourse, IStudentNavigation, IStudentStudyLevel,
28    IStudentStudyCourseTranscript)
29from waeup.kofa.students.studylevel import CourseTicket
30from waeup.kofa.students.workflow import CLEARED, RETURNING, PAID
31from waeup.kofa.utils.helpers import attrs_to_fields
32
33class StudentStudyCourse(grok.Container):
34    """This is a container for study levels.
35    """
36    grok.implements(IStudentStudyCourse, IStudentNavigation,
37        IStudentStudyCourseTranscript)
38    grok.provides(IStudentStudyCourse)
39
40    is_special_postgrad = False
41
42    def __init__(self):
43        super(StudentStudyCourse, self).__init__()
44        return
45
46    @property
47    def student(self):
48        return self.__parent__
49
50    def writeLogMessage(self, view, message):
51        return self.__parent__.writeLogMessage(view, message)
52
53    @property
54    def next_session_allowed(self):
55        certificate = getattr(self, 'certificate', None)
56        if certificate == None:
57            return False
58        if self.student.state in (CLEARED, RETURNING):
59            return True
60        if self.student.state == PAID \
61            and self.student.is_postgrad:
62            return True
63        return False
64
65    @property
66    def is_postgrad(self):
67        if self.certificate is None:
68            return False
69        return self.certificate.study_mode.startswith('pg')
70
71    @property
72    def is_current(self):
73        if self.__name__ and '_' in self.__name__:
74            return False
75        return True
76
77    @property
78    def is_previous(self):
79        if self.__name__ == 'studycourse_2':
80            return True
81        if self.__name__ == 'studycourse_1' and  \
82            not self.__parent__.has_key('studycourse_2'):
83            return True
84        return False
85
86    def addStudentStudyLevel(self, cert, studylevel):
87        """Add a study level object.
88        """
89        if not IStudentStudyLevel.providedBy(studylevel):
90            raise TypeError(
91                'StudentStudyCourses contain only IStudentStudyLevel instances')
92        self[str(studylevel.level)] = studylevel
93        studylevel.addCertCourseTickets(cert)
94        # Collect carry-over courses in base levels (not in repeating levels)
95        try:
96            co_enabled = grok.getSite()['configuration'].carry_over
97        except TypeError:
98            # In tests we might not have a site object
99            co_enabled = True
100        if not co_enabled or studylevel.level % 100 != 0:
101            return
102        levels = sorted(self.keys())
103        index = levels.index(str(studylevel.level))
104        if index <= 0:
105            return
106        previous_level = self[levels[index-1]]
107        for key, val in previous_level.items():
108            if val.total_score >= val.passmark:
109                continue
110            if key in self[str(studylevel.level)]:
111                # Carry-over ticket exists
112                continue
113            co_ticket = createObject(u'waeup.CourseTicket')
114            for name in ['code', 'title', 'credits', 'passmark',
115                         'semester', 'mandatory', 'fcode', 'dcode']:
116                setattr(co_ticket, name, getattr(val, name))
117            co_ticket.automatic = True
118            co_ticket.carry_over = True
119            self[str(studylevel.level)][co_ticket.code] = co_ticket
120        return
121
122    def _display_gpa(self, gpa):
123        return round(gpa, 2)
124
125    def getTranscriptData(self):
126        """Get a sorted list of dicts with level and course ticket data.
127
128        This method is used for transcripts.
129        """
130        levels = []
131        cgpa = 0.0
132        total_credits_counted = 0
133        for level_key, level_val in self.items():
134            tickets_1 = []
135            tickets_2 = []
136            tickets_3 = []
137            tickets = sorted(level_val.values(), key=lambda ticket: ticket.code)
138            for ticket in tickets:
139                if ticket.semester == 1:
140                    tickets_1.append(ticket)
141                elif ticket.semester == 2:
142                    tickets_2.append(ticket)
143                elif ticket.semester == 3:
144                    tickets_3.append(ticket)
145            gpa_params = level_val.gpa_params_rectified
146            sgpa = gpa_params[0]
147            total_credits_counted += gpa_params[1]
148            cgpa += gpa_params[2]
149            levels.append(
150                dict(level_key=level_key,
151                     level=level_val,
152                     tickets_1=tickets_1,
153                     tickets_2=tickets_2,
154                     tickets_3=tickets_3,
155                     sgpa=sgpa))
156        if total_credits_counted:
157            cgpa = self._display_gpa(cgpa/total_credits_counted)
158        # Override cgpa if value has been imported
159        # (not implemented in base package)
160        imported_cgpa = getattr(self, 'imported_cgpa', None)
161        if imported_cgpa:
162            cgpa = imported_cgpa
163        return sorted(levels, key=lambda level: level['level_key']), cgpa
164
165StudentStudyCourse = attrs_to_fields(StudentStudyCourse)
166
167class StudentStudyCourseFactory(grok.GlobalUtility):
168    """A factory for student study courses.
169    """
170    grok.implements(IFactory)
171    grok.name(u'waeup.StudentStudyCourse')
172    title = u"Create a new student study course.",
173    description = u"This factory instantiates new student study course instances."
174
175    def __call__(self, *args, **kw):
176        return StudentStudyCourse()
177
178    def getInterfaces(self):
179        return implementedBy(StudentStudyCourse)
Note: See TracBrowser for help on using the repository browser.