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