source: main/waeup.kofa/branches/uli-rm-bootstrap/src/waeup/kofa/students/studycourse.py @ 17935

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

Exchange level and session on report slips.

Use sting formatting for displaying GPA values.

  • Property svn:keywords set to Id
File size: 6.3 KB
Line 
1## $Id: studycourse.py 14382 2017-01-10 18:33:30Z 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 getTranscriptData(self):
123        """Get a sorted list of dicts with level and course ticket data.
124
125        This method is used for transcripts.
126        """
127        levels = []
128        cgpa = 0.0
129        total_credits_counted = 0
130        for level_key, level_val in self.items():
131            tickets_1 = []
132            tickets_2 = []
133            tickets_3 = []
134            tickets = sorted(level_val.values(), key=lambda ticket: ticket.code)
135            for ticket in tickets:
136                if ticket.semester == 1:
137                    tickets_1.append(ticket)
138                elif ticket.semester == 2:
139                    tickets_2.append(ticket)
140                elif ticket.semester == 3:
141                    tickets_3.append(ticket)
142            gpa_params = level_val.gpa_params_rectified
143            sgpa = gpa_params[0]
144            total_credits_counted += gpa_params[1]
145            cgpa += gpa_params[2]
146            levels.append(
147                dict(level_key=level_key,
148                     level=level_val,
149                     tickets_1=tickets_1,
150                     tickets_2=tickets_2,
151                     tickets_3=tickets_3,
152                     sgpa=sgpa))
153        if total_credits_counted:
154            cgpa = cgpa/total_credits_counted
155        # Override cgpa if value has been imported
156        # (not implemented in base package)
157        imported_cgpa = getattr(self, 'imported_cgpa', None)
158        if imported_cgpa:
159            cgpa = imported_cgpa
160        return sorted(levels, key=lambda level: level['level_key']), cgpa
161
162StudentStudyCourse = attrs_to_fields(StudentStudyCourse)
163
164class StudentStudyCourseFactory(grok.GlobalUtility):
165    """A factory for student study courses.
166    """
167    grok.implements(IFactory)
168    grok.name(u'waeup.StudentStudyCourse')
169    title = u"Create a new student study course.",
170    description = u"This factory instantiates new student study course instances."
171
172    def __call__(self, *args, **kw):
173        return StudentStudyCourse()
174
175    def getInterfaces(self):
176        return implementedBy(StudentStudyCourse)
Note: See TracBrowser for help on using the repository browser.