1 | ## $Id: studylevel.py 14475 2017-01-27 16:52:40Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2012 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 a student study level |
---|
20 | and contains the course tickets. |
---|
21 | """ |
---|
22 | import grok |
---|
23 | import pytz |
---|
24 | from datetime import datetime |
---|
25 | from zope.component.interfaces import IFactory |
---|
26 | from zope.component import createObject |
---|
27 | from zope.interface import implementedBy |
---|
28 | from waeup.kofa.utils.helpers import attrs_to_fields |
---|
29 | from waeup.kofa.interfaces import CREATED |
---|
30 | from waeup.kofa.students.browser import TicketError |
---|
31 | from waeup.kofa.students.studylevel import ( |
---|
32 | StudentStudyLevel, CourseTicket, |
---|
33 | CourseTicketFactory, StudentStudyLevelFactory) |
---|
34 | from waeup.kofa.students.interfaces import IStudentNavigation, ICourseTicket |
---|
35 | from waeup.aaue.students.interfaces import ( |
---|
36 | ICustomStudentStudyLevel, ICustomCourseTicket) |
---|
37 | from waeup.aaue.interfaces import MessageFactory as _ |
---|
38 | |
---|
39 | |
---|
40 | class CustomStudentStudyLevel(StudentStudyLevel): |
---|
41 | """This is a container for course tickets. |
---|
42 | """ |
---|
43 | grok.implements(ICustomStudentStudyLevel, IStudentNavigation) |
---|
44 | grok.provides(ICustomStudentStudyLevel) |
---|
45 | |
---|
46 | @property |
---|
47 | def total_credits_s1(self): |
---|
48 | total = 0 |
---|
49 | for ticket in self.values(): |
---|
50 | if ticket.semester == 1: |
---|
51 | total += ticket.credits |
---|
52 | return total |
---|
53 | |
---|
54 | @property |
---|
55 | def total_credits_s2(self): |
---|
56 | total = 0 |
---|
57 | for ticket in self.values(): |
---|
58 | if ticket.semester == 2: |
---|
59 | total += ticket.credits |
---|
60 | return total |
---|
61 | |
---|
62 | @property |
---|
63 | def gpa_params(self): |
---|
64 | """Calculate gpa parameters for this level. |
---|
65 | """ |
---|
66 | credits_weighted = 0.0 |
---|
67 | credits_counted = 0 |
---|
68 | level_gpa = 0.0 |
---|
69 | for ticket in self.values(): |
---|
70 | if None not in (ticket.score, ticket.ca): |
---|
71 | credits_counted += ticket.credits |
---|
72 | credits_weighted += ticket.credits * ticket.weight |
---|
73 | if credits_counted: |
---|
74 | level_gpa = credits_weighted/credits_counted |
---|
75 | # Override level_gpa if value has been imported |
---|
76 | imported_gpa = getattr(self, 'imported_gpa', None) |
---|
77 | if imported_gpa: |
---|
78 | level_gpa = imported_gpa |
---|
79 | return level_gpa, credits_counted, credits_weighted |
---|
80 | |
---|
81 | @property |
---|
82 | def gpa_params_rectified(self): |
---|
83 | return self.gpa_params |
---|
84 | |
---|
85 | @property |
---|
86 | def passed_params(self): |
---|
87 | """Determine the number and credits of passed and failed courses. |
---|
88 | This method is used for level reports. |
---|
89 | """ |
---|
90 | passed = failed = 0 |
---|
91 | courses_failed = '' |
---|
92 | credits_failed = 0 |
---|
93 | credits_passed = 0 |
---|
94 | courses_not_taken = '' |
---|
95 | for ticket in self.values(): |
---|
96 | if None not in (ticket.score, ticket.ca): |
---|
97 | if ticket.total_score < ticket.passmark: |
---|
98 | failed += 1 |
---|
99 | credits_failed += ticket.credits |
---|
100 | if ticket.mandatory: |
---|
101 | courses_failed += 'm_%s_m ' % ticket.code |
---|
102 | else: |
---|
103 | courses_failed += '%s ' % ticket.code |
---|
104 | else: |
---|
105 | passed += 1 |
---|
106 | credits_passed += ticket.credits |
---|
107 | else: |
---|
108 | courses_not_taken += '%s ' % ticket.code |
---|
109 | if not len(courses_failed): |
---|
110 | courses_failed = 'NIL' |
---|
111 | if not len(courses_not_taken): |
---|
112 | courses_not_taken = 'NIL' |
---|
113 | return (passed, failed, credits_passed, |
---|
114 | credits_failed, courses_failed, |
---|
115 | courses_not_taken) |
---|
116 | |
---|
117 | @property |
---|
118 | def course_registration_forbidden(self): |
---|
119 | fac_dep_paid = True |
---|
120 | if self.student.entry_session >= 2016: |
---|
121 | fac_dep_paid = False |
---|
122 | for ticket in self.student['payments'].values(): |
---|
123 | if ticket.p_category == 'fac_dep' and \ |
---|
124 | ticket.p_session == self.level_session and \ |
---|
125 | ticket.p_state == 'paid': |
---|
126 | fac_dep_paid = True |
---|
127 | continue |
---|
128 | if not fac_dep_paid: |
---|
129 | return _("Please pay faculty and departmental dues first.") |
---|
130 | restitution_paid = True |
---|
131 | if self.student.entry_session < 2016 \ |
---|
132 | and self.student.current_mode == 'ug_ft': |
---|
133 | restitution_paid = False |
---|
134 | for ticket in self.student['payments'].values(): |
---|
135 | if ticket.p_category == 'restitution' and \ |
---|
136 | ticket.p_session == self.level_session and \ |
---|
137 | ticket.p_state == 'paid': |
---|
138 | restitution_paid = True |
---|
139 | continue |
---|
140 | if not restitution_paid: |
---|
141 | return _("Please pay restitution fee first.") |
---|
142 | if self.student.is_fresh: |
---|
143 | return |
---|
144 | try: |
---|
145 | if self.student.is_postgrad: |
---|
146 | deadline = grok.getSite()['configuration'][ |
---|
147 | str(self.level_session)].coursereg_deadline_pg |
---|
148 | elif self.student.current_mode.startswith('dp'): |
---|
149 | deadline = grok.getSite()['configuration'][ |
---|
150 | str(self.level_session)].coursereg_deadline_dp |
---|
151 | elif self.student.current_mode.endswith('_pt'): |
---|
152 | deadline = grok.getSite()['configuration'][ |
---|
153 | str(self.level_session)].coursereg_deadline_pt |
---|
154 | elif self.student.current_mode == 'found': |
---|
155 | deadline = grok.getSite()['configuration'][ |
---|
156 | str(self.level_session)].coursereg_deadline_found |
---|
157 | else: |
---|
158 | deadline = grok.getSite()['configuration'][ |
---|
159 | str(self.level_session)].coursereg_deadline |
---|
160 | except (TypeError, KeyError): |
---|
161 | return |
---|
162 | if not deadline or deadline > datetime.now(pytz.utc): |
---|
163 | return |
---|
164 | if len(self.student['payments']): |
---|
165 | for ticket in self.student['payments'].values(): |
---|
166 | if ticket.p_category == 'late_registration' and \ |
---|
167 | ticket.p_session == self.level_session and \ |
---|
168 | ticket.p_state == 'paid': |
---|
169 | return |
---|
170 | return _("Course registration has ended. " |
---|
171 | "Please pay the late registration fee.") |
---|
172 | |
---|
173 | # only AAUE |
---|
174 | @property |
---|
175 | def remark(self): |
---|
176 | certificate = getattr(self.__parent__,'certificate',None) |
---|
177 | end_level = getattr(certificate, 'end_level', None) |
---|
178 | failed_limit = 1.5 |
---|
179 | if self.student.entry_session < 2013: |
---|
180 | failed_limit = 1.0 |
---|
181 | # final level student remark |
---|
182 | if end_level and self.level >= end_level: |
---|
183 | if self.level > end_level: |
---|
184 | # spill-over level |
---|
185 | if self.gpa_params[1] == 0: |
---|
186 | # no credits taken |
---|
187 | return 'NEOR' |
---|
188 | else: |
---|
189 | if self.gpa_params[1] < 30: |
---|
190 | # credits taken below limit |
---|
191 | return 'NEOR' |
---|
192 | if self.level_verdict in ('FRNS', 'NEOR', 'NEOV'): |
---|
193 | return self.level_verdict |
---|
194 | if '_m' in self.passed_params[4]: |
---|
195 | return 'FRNS' |
---|
196 | if not self.cumulative_params[0]: |
---|
197 | return 'FRNS' |
---|
198 | if len(self.passed_params[5]) \ |
---|
199 | and not self.passed_params[5] == 'NIL': |
---|
200 | return 'FRNS' |
---|
201 | if self.cumulative_params[0] < failed_limit: |
---|
202 | return 'Fail' |
---|
203 | if self.cumulative_params[0] < 1.5: |
---|
204 | return 'Pass' |
---|
205 | if self.cumulative_params[0] < 2.4: |
---|
206 | return '3s_rd_s' |
---|
207 | if self.cumulative_params[0] < 3.5: |
---|
208 | return '2s_2_s' |
---|
209 | if self.cumulative_params[0] < 4.5: |
---|
210 | return '2s_1_s' |
---|
211 | if self.cumulative_params[0] < 5.1: |
---|
212 | return '1s_st_s' |
---|
213 | return 'N/A' |
---|
214 | # returning student remark |
---|
215 | if self.level_verdict in ('FRNS', 'NEOR', 'NEOV'): |
---|
216 | return 'Probation' |
---|
217 | if self.level_verdict == 'D': |
---|
218 | return 'Withdrawn' |
---|
219 | if self.gpa_params[1] < 30: |
---|
220 | # credits taken below limit |
---|
221 | return 'Probation' |
---|
222 | if self.cumulative_params[0] < failed_limit: |
---|
223 | return 'Probation' |
---|
224 | if self.cumulative_params[0] < 5.1: |
---|
225 | return 'Proceed' |
---|
226 | return 'N/A' |
---|
227 | |
---|
228 | def _schoolfeePaymentMade(self): |
---|
229 | if len(self.student['payments']): |
---|
230 | for ticket in self.student['payments'].values(): |
---|
231 | if ticket.p_state == 'paid' and \ |
---|
232 | ticket.p_category in ( |
---|
233 | 'schoolfee', 'schoolfee_incl', 'schoolfee_2',) and \ |
---|
234 | ticket.p_session == self.student[ |
---|
235 | 'studycourse'].current_session: |
---|
236 | return True |
---|
237 | return False |
---|
238 | |
---|
239 | def _coursePaymentsMade(self, course): |
---|
240 | if self.level_session < 2016: |
---|
241 | return True |
---|
242 | if not course.code[:3] in ('GST', 'ENT'): |
---|
243 | return True |
---|
244 | if len(self.student['payments']): |
---|
245 | paid_cats = list() |
---|
246 | for pticket in self.student['payments'].values(): |
---|
247 | if pticket.p_state == 'paid': |
---|
248 | paid_cats.append(pticket.p_category) |
---|
249 | if course.code in ('GST101', 'GST102', 'GST111', 'GST112') and \ |
---|
250 | not 'gst_registration_1' in paid_cats: |
---|
251 | return False |
---|
252 | if course.code in ('GST222',) and \ |
---|
253 | not 'gst_registration_2' in paid_cats: |
---|
254 | return False |
---|
255 | if course.code in ('ENT201',) and \ |
---|
256 | not 'ent_registration_1' in paid_cats: |
---|
257 | return False |
---|
258 | if course.code in ('GST101', 'GST102') and \ |
---|
259 | not 'gst_text_book_1' in paid_cats and \ |
---|
260 | not 'gst_text_book_0' in paid_cats: |
---|
261 | return False |
---|
262 | if course.code in ('GST111', 'GST112') and \ |
---|
263 | not 'gst_text_book_2' in paid_cats and \ |
---|
264 | not 'gst_text_book_0' in paid_cats: |
---|
265 | return False |
---|
266 | if course.code in ('GST222',) and \ |
---|
267 | not 'gst_text_book_3' in paid_cats: |
---|
268 | return False |
---|
269 | if course.code in ('ENT201',) and \ |
---|
270 | not 'ent_text_book_1' in paid_cats: |
---|
271 | return False |
---|
272 | return True |
---|
273 | return False |
---|
274 | |
---|
275 | def addCourseTicket(self, ticket, course): |
---|
276 | """Add a course ticket object. |
---|
277 | """ |
---|
278 | if not ICourseTicket.providedBy(ticket): |
---|
279 | raise TypeError( |
---|
280 | 'StudentStudyLeves contain only ICourseTicket instances') |
---|
281 | # Raise TicketError if course is in 2nd semester but |
---|
282 | # schoolfee has not yet been fully paid. |
---|
283 | if course.semester == 2 and not self._schoolfeePaymentMade(): |
---|
284 | raise TicketError( |
---|
285 | _('%s is a 2nd semester course which can only be added ' |
---|
286 | 'if school fees have been fully paid.' % course.code)) |
---|
287 | # Raise TicketError if registration fee or text |
---|
288 | # book fee haven't been paid. |
---|
289 | if not self._coursePaymentsMade(course): |
---|
290 | raise TicketError( |
---|
291 | _('%s can only be added if both registration fee and text ' |
---|
292 | 'book fee have been paid.' |
---|
293 | % course.code)) |
---|
294 | ticket.code = course.code |
---|
295 | ticket.title = course.title |
---|
296 | ticket.fcode = course.__parent__.__parent__.__parent__.code |
---|
297 | ticket.dcode = course.__parent__.__parent__.code |
---|
298 | ticket.credits = course.credits |
---|
299 | if self.student.entry_session < 2013: |
---|
300 | ticket.passmark = course.passmark - 5 |
---|
301 | else: |
---|
302 | ticket.passmark = course.passmark |
---|
303 | ticket.semester = course.semester |
---|
304 | self[ticket.code] = ticket |
---|
305 | return |
---|
306 | |
---|
307 | def addCertCourseTickets(self, cert): |
---|
308 | """Collect all certificate courses and create course |
---|
309 | tickets automatically. |
---|
310 | """ |
---|
311 | if cert is not None: |
---|
312 | for key, val in cert.items(): |
---|
313 | if val.level != self.level: |
---|
314 | continue |
---|
315 | ticket = createObject(u'waeup.CourseTicket') |
---|
316 | ticket.automatic = True |
---|
317 | ticket.mandatory = val.mandatory |
---|
318 | ticket.carry_over = False |
---|
319 | try: |
---|
320 | self.addCourseTicket(ticket, val.course) |
---|
321 | except TicketError: |
---|
322 | pass |
---|
323 | return |
---|
324 | |
---|
325 | CustomStudentStudyLevel = attrs_to_fields( |
---|
326 | CustomStudentStudyLevel, omit=[ |
---|
327 | 'total_credits', 'total_credits_s1', 'total_credits_s2', 'gpa']) |
---|
328 | |
---|
329 | class CustomStudentStudyLevelFactory(StudentStudyLevelFactory): |
---|
330 | """A factory for student study levels. |
---|
331 | """ |
---|
332 | |
---|
333 | def __call__(self, *args, **kw): |
---|
334 | return CustomStudentStudyLevel() |
---|
335 | |
---|
336 | def getInterfaces(self): |
---|
337 | return implementedBy(CustomStudentStudyLevel) |
---|
338 | |
---|
339 | class CustomCourseTicket(CourseTicket): |
---|
340 | """This is a course ticket which allows the |
---|
341 | student to attend the course. Lecturers will enter scores and more at |
---|
342 | the end of the term. |
---|
343 | |
---|
344 | A course ticket contains a copy of the original course and |
---|
345 | course referrer data. If the courses and/or their referrers are removed, the |
---|
346 | corresponding tickets remain unchanged. So we do not need any event |
---|
347 | triggered actions on course tickets. |
---|
348 | """ |
---|
349 | grok.implements(ICustomCourseTicket, IStudentNavigation) |
---|
350 | grok.provides(ICustomCourseTicket) |
---|
351 | |
---|
352 | @property |
---|
353 | def total_score(self): |
---|
354 | """Returns ca + score. |
---|
355 | """ |
---|
356 | if not None in (self.score, self.ca): |
---|
357 | return self.score + self.ca |
---|
358 | |
---|
359 | @property |
---|
360 | def editable_by_lecturer(self): |
---|
361 | """True if lecturer is allowed to edit the ticket. |
---|
362 | """ |
---|
363 | return True |
---|
364 | |
---|
365 | CustomCourseTicket = attrs_to_fields(CustomCourseTicket) |
---|
366 | |
---|
367 | class CustomCourseTicketFactory(CourseTicketFactory): |
---|
368 | """A factory for student study levels. |
---|
369 | """ |
---|
370 | |
---|
371 | def __call__(self, *args, **kw): |
---|
372 | return CustomCourseTicket() |
---|
373 | |
---|
374 | def getInterfaces(self): |
---|
375 | return implementedBy(CustomCourseTicket) |
---|