1 | ## $Id: studylevel.py 14393 2017-01-13 12:33:16Z 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 | return (passed, failed, credits_passed, |
---|
112 | credits_failed, courses_failed, |
---|
113 | courses_not_taken) |
---|
114 | |
---|
115 | @property |
---|
116 | def course_registration_forbidden(self): |
---|
117 | fac_dep_paid = True |
---|
118 | if self.student.entry_session >= 2016: |
---|
119 | fac_dep_paid = False |
---|
120 | for ticket in self.student['payments'].values(): |
---|
121 | if ticket.p_category == 'fac_dep' and \ |
---|
122 | ticket.p_session == self.level_session and \ |
---|
123 | ticket.p_state == 'paid': |
---|
124 | fac_dep_paid = True |
---|
125 | continue |
---|
126 | if not fac_dep_paid: |
---|
127 | return _("Please pay faculty and departmental dues first.") |
---|
128 | restitution_paid = True |
---|
129 | if self.student.entry_session < 2016 \ |
---|
130 | and self.student.current_mode == 'ug_ft': |
---|
131 | restitution_paid = False |
---|
132 | for ticket in self.student['payments'].values(): |
---|
133 | if ticket.p_category == 'restitution' and \ |
---|
134 | ticket.p_session == self.level_session and \ |
---|
135 | ticket.p_state == 'paid': |
---|
136 | restitution_paid = True |
---|
137 | continue |
---|
138 | if not restitution_paid: |
---|
139 | return _("Please pay restitution fee first.") |
---|
140 | if self.student.is_fresh: |
---|
141 | return |
---|
142 | try: |
---|
143 | if self.student.is_postgrad: |
---|
144 | deadline = grok.getSite()['configuration'][ |
---|
145 | str(self.level_session)].coursereg_deadline_pg |
---|
146 | elif self.student.current_mode.startswith('dp'): |
---|
147 | deadline = grok.getSite()['configuration'][ |
---|
148 | str(self.level_session)].coursereg_deadline_dp |
---|
149 | elif self.student.current_mode.endswith('_pt'): |
---|
150 | deadline = grok.getSite()['configuration'][ |
---|
151 | str(self.level_session)].coursereg_deadline_pt |
---|
152 | elif self.student.current_mode == 'found': |
---|
153 | deadline = grok.getSite()['configuration'][ |
---|
154 | str(self.level_session)].coursereg_deadline_found |
---|
155 | else: |
---|
156 | deadline = grok.getSite()['configuration'][ |
---|
157 | str(self.level_session)].coursereg_deadline |
---|
158 | except (TypeError, KeyError): |
---|
159 | return |
---|
160 | if not deadline or deadline > datetime.now(pytz.utc): |
---|
161 | return |
---|
162 | if len(self.student['payments']): |
---|
163 | for ticket in self.student['payments'].values(): |
---|
164 | if ticket.p_category == 'late_registration' and \ |
---|
165 | ticket.p_session == self.level_session and \ |
---|
166 | ticket.p_state == 'paid': |
---|
167 | return |
---|
168 | return _("Course registration has ended. " |
---|
169 | "Please pay the late registration fee.") |
---|
170 | |
---|
171 | # only AAUE |
---|
172 | @property |
---|
173 | def remark(self): |
---|
174 | certificate = getattr(self.__parent__,'certificate',None) |
---|
175 | end_level = getattr(certificate, 'end_level', None) |
---|
176 | failed_limit = 1.5 |
---|
177 | if self.student.entry_session < 2013: |
---|
178 | failed_limit = 1.0 |
---|
179 | # final level student remark |
---|
180 | if end_level and self.level >= end_level: |
---|
181 | failed_courses = self.passed_params[4] |
---|
182 | if '_m' in failed_courses or not self.cumulative_params[0]: |
---|
183 | return 'FRNS' |
---|
184 | if self.cumulative_params[0] < failed_limit: |
---|
185 | return 'Fail' |
---|
186 | if self.cumulative_params[0] < 2.4: |
---|
187 | return '3s_rd_s' |
---|
188 | if self.cumulative_params[0] < 3.5: |
---|
189 | return '2s_2_s' |
---|
190 | if self.cumulative_params[0] < 4.5: |
---|
191 | return '2s_1_s' |
---|
192 | if self.cumulative_params[0] < 5.1: |
---|
193 | return '1s_st_s' |
---|
194 | return 'N/A' |
---|
195 | # returning student remark |
---|
196 | if self.cumulative_params[0] < failed_limit: |
---|
197 | return 'Probation' |
---|
198 | if self.cumulative_params[0] < 5.1: |
---|
199 | return 'Proceed' |
---|
200 | return 'N/A' |
---|
201 | |
---|
202 | def _schoolfeePaymentMade(self): |
---|
203 | if len(self.student['payments']): |
---|
204 | for ticket in self.student['payments'].values(): |
---|
205 | if ticket.p_state == 'paid' and \ |
---|
206 | ticket.p_category in ( |
---|
207 | 'schoolfee', 'schoolfee_incl', 'schoolfee_2',) and \ |
---|
208 | ticket.p_session == self.student[ |
---|
209 | 'studycourse'].current_session: |
---|
210 | return True |
---|
211 | return False |
---|
212 | |
---|
213 | def _coursePaymentsMade(self, course): |
---|
214 | if self.level_session < 2016: |
---|
215 | return True |
---|
216 | if not course.code[:3] in ('GST', 'ENT'): |
---|
217 | return True |
---|
218 | if len(self.student['payments']): |
---|
219 | paid_cats = list() |
---|
220 | for pticket in self.student['payments'].values(): |
---|
221 | if pticket.p_state == 'paid': |
---|
222 | paid_cats.append(pticket.p_category) |
---|
223 | if course.code in ('GST101', 'GST102', 'GST111', 'GST112') and \ |
---|
224 | not 'gst_registration_1' in paid_cats: |
---|
225 | return False |
---|
226 | if course.code in ('GST222',) and \ |
---|
227 | not 'gst_registration_2' in paid_cats: |
---|
228 | return False |
---|
229 | if course.code in ('ENT201',) and \ |
---|
230 | not 'ent_registration_1' in paid_cats: |
---|
231 | return False |
---|
232 | if course.code in ('GST101', 'GST102') and \ |
---|
233 | not 'gst_text_book_1' in paid_cats and \ |
---|
234 | not 'gst_text_book_0' in paid_cats: |
---|
235 | return False |
---|
236 | if course.code in ('GST111', 'GST112') and \ |
---|
237 | not 'gst_text_book_2' in paid_cats and \ |
---|
238 | not 'gst_text_book_0' in paid_cats: |
---|
239 | return False |
---|
240 | if course.code in ('GST222',) and \ |
---|
241 | not 'gst_text_book_3' in paid_cats: |
---|
242 | return False |
---|
243 | if course.code in ('ENT201',) and \ |
---|
244 | not 'ent_text_book_1' in paid_cats: |
---|
245 | return False |
---|
246 | return True |
---|
247 | return False |
---|
248 | |
---|
249 | def addCourseTicket(self, ticket, course): |
---|
250 | """Add a course ticket object. |
---|
251 | """ |
---|
252 | if not ICourseTicket.providedBy(ticket): |
---|
253 | raise TypeError( |
---|
254 | 'StudentStudyLeves contain only ICourseTicket instances') |
---|
255 | # Raise TicketError if course is in 2nd semester but |
---|
256 | # schoolfee has not yet been fully paid. |
---|
257 | if course.semester == 2 and not self._schoolfeePaymentMade(): |
---|
258 | raise TicketError( |
---|
259 | _('%s is a 2nd semester course which can only be added ' |
---|
260 | 'if school fees have been fully paid.' % course.code)) |
---|
261 | # Raise TicketError if registration fee or text |
---|
262 | # book fee haven't been paid. |
---|
263 | if not self._coursePaymentsMade(course): |
---|
264 | raise TicketError( |
---|
265 | _('%s can only be added if both registration fee and text ' |
---|
266 | 'book fee have been paid.' |
---|
267 | % course.code)) |
---|
268 | ticket.code = course.code |
---|
269 | ticket.title = course.title |
---|
270 | ticket.fcode = course.__parent__.__parent__.__parent__.code |
---|
271 | ticket.dcode = course.__parent__.__parent__.code |
---|
272 | ticket.credits = course.credits |
---|
273 | if self.student.entry_session < 2013: |
---|
274 | ticket.passmark = course.passmark - 5 |
---|
275 | else: |
---|
276 | ticket.passmark = course.passmark |
---|
277 | ticket.semester = course.semester |
---|
278 | self[ticket.code] = ticket |
---|
279 | return |
---|
280 | |
---|
281 | def addCertCourseTickets(self, cert): |
---|
282 | """Collect all certificate courses and create course |
---|
283 | tickets automatically. |
---|
284 | """ |
---|
285 | if cert is not None: |
---|
286 | for key, val in cert.items(): |
---|
287 | if val.level != self.level: |
---|
288 | continue |
---|
289 | ticket = createObject(u'waeup.CourseTicket') |
---|
290 | ticket.automatic = True |
---|
291 | ticket.mandatory = val.mandatory |
---|
292 | ticket.carry_over = False |
---|
293 | try: |
---|
294 | self.addCourseTicket(ticket, val.course) |
---|
295 | except TicketError: |
---|
296 | pass |
---|
297 | return |
---|
298 | |
---|
299 | CustomStudentStudyLevel = attrs_to_fields( |
---|
300 | CustomStudentStudyLevel, omit=[ |
---|
301 | 'total_credits', 'total_credits_s1', 'total_credits_s2', 'gpa']) |
---|
302 | |
---|
303 | class CustomStudentStudyLevelFactory(StudentStudyLevelFactory): |
---|
304 | """A factory for student study levels. |
---|
305 | """ |
---|
306 | |
---|
307 | def __call__(self, *args, **kw): |
---|
308 | return CustomStudentStudyLevel() |
---|
309 | |
---|
310 | def getInterfaces(self): |
---|
311 | return implementedBy(CustomStudentStudyLevel) |
---|
312 | |
---|
313 | class CustomCourseTicket(CourseTicket): |
---|
314 | """This is a course ticket which allows the |
---|
315 | student to attend the course. Lecturers will enter scores and more at |
---|
316 | the end of the term. |
---|
317 | |
---|
318 | A course ticket contains a copy of the original course and |
---|
319 | course referrer data. If the courses and/or their referrers are removed, the |
---|
320 | corresponding tickets remain unchanged. So we do not need any event |
---|
321 | triggered actions on course tickets. |
---|
322 | """ |
---|
323 | grok.implements(ICustomCourseTicket, IStudentNavigation) |
---|
324 | grok.provides(ICustomCourseTicket) |
---|
325 | |
---|
326 | @property |
---|
327 | def total_score(self): |
---|
328 | """Returns ca + score. |
---|
329 | """ |
---|
330 | if not None in (self.score, self.ca): |
---|
331 | return self.score + self.ca |
---|
332 | |
---|
333 | @property |
---|
334 | def editable_by_lecturer(self): |
---|
335 | """True if lecturer is allowed to edit the ticket. |
---|
336 | """ |
---|
337 | return True |
---|
338 | |
---|
339 | CustomCourseTicket = attrs_to_fields(CustomCourseTicket) |
---|
340 | |
---|
341 | class CustomCourseTicketFactory(CourseTicketFactory): |
---|
342 | """A factory for student study levels. |
---|
343 | """ |
---|
344 | |
---|
345 | def __call__(self, *args, **kw): |
---|
346 | return CustomCourseTicket() |
---|
347 | |
---|
348 | def getInterfaces(self): |
---|
349 | return implementedBy(CustomCourseTicket) |
---|