1 | ## $Id: studylevel.py 15963 2020-01-28 08:34:57Z 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 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.catalog.interfaces import ICatalog |
---|
27 | from zope.component import createObject, queryUtility, getUtility |
---|
28 | from zope.interface import implementedBy |
---|
29 | from waeup.kofa.interfaces import academic_sessions_vocab, VALIDATED, IKofaUtils |
---|
30 | from waeup.kofa.students.interfaces import ( |
---|
31 | IStudentStudyLevel, IStudentNavigation, ICourseTicket) |
---|
32 | from waeup.kofa.utils.helpers import attrs_to_fields |
---|
33 | from waeup.kofa.students.vocabularies import StudyLevelSource |
---|
34 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
35 | |
---|
36 | def find_carry_over(ticket): |
---|
37 | studylevel = ticket.__parent__ |
---|
38 | studycourse = ticket.__parent__.__parent__ |
---|
39 | levels = sorted(studycourse.keys()) |
---|
40 | index = levels.index(str(studylevel.level)) |
---|
41 | try: |
---|
42 | next_level = levels[index+1] |
---|
43 | except IndexError: |
---|
44 | return None |
---|
45 | next_studylevel = studycourse[next_level] |
---|
46 | co_ticket = next_studylevel.get(ticket.code, None) |
---|
47 | return co_ticket |
---|
48 | |
---|
49 | class StudentStudyLevel(grok.Container): |
---|
50 | """This is a container for course tickets. |
---|
51 | """ |
---|
52 | grok.implements(IStudentStudyLevel, IStudentNavigation) |
---|
53 | grok.provides(IStudentStudyLevel) |
---|
54 | |
---|
55 | def __init__(self): |
---|
56 | super(StudentStudyLevel, self).__init__() |
---|
57 | return |
---|
58 | |
---|
59 | @property |
---|
60 | def student(self): |
---|
61 | try: |
---|
62 | return self.__parent__.__parent__ |
---|
63 | except AttributeError: |
---|
64 | return None |
---|
65 | |
---|
66 | @property |
---|
67 | def certcode(self): |
---|
68 | try: |
---|
69 | return self.__parent__.certificate.code |
---|
70 | except AttributeError: |
---|
71 | return None |
---|
72 | |
---|
73 | @property |
---|
74 | def number_of_tickets(self): |
---|
75 | return len(self) |
---|
76 | |
---|
77 | @property |
---|
78 | def total_credits(self): |
---|
79 | total = 0 |
---|
80 | for ticket in self.values(): |
---|
81 | if not ticket.outstanding: |
---|
82 | total += ticket.credits |
---|
83 | return total |
---|
84 | |
---|
85 | @property |
---|
86 | def getSessionString(self): |
---|
87 | try: |
---|
88 | session_string = academic_sessions_vocab.getTerm( |
---|
89 | self.level_session).title |
---|
90 | except LookupError: |
---|
91 | return None |
---|
92 | return session_string |
---|
93 | |
---|
94 | @property |
---|
95 | def gpa_params_rectified(self): |
---|
96 | """Calculate corrected level (sessional) gpa parameters. |
---|
97 | The corrected gpa is displayed on transcripts only. |
---|
98 | """ |
---|
99 | credits_weighted = 0.0 |
---|
100 | credits_counted = 0 |
---|
101 | level_gpa = 0.0 |
---|
102 | for ticket in self.values(): |
---|
103 | if ticket.carry_over is False and ticket.total_score: |
---|
104 | if ticket.total_score < ticket.passmark: |
---|
105 | co_ticket = find_carry_over(ticket) |
---|
106 | if co_ticket is not None and co_ticket.weight is not None: |
---|
107 | credits_counted += co_ticket.credits |
---|
108 | credits_weighted += co_ticket.credits * co_ticket.weight |
---|
109 | continue |
---|
110 | credits_counted += ticket.credits |
---|
111 | credits_weighted += ticket.credits * ticket.weight |
---|
112 | if credits_counted: |
---|
113 | level_gpa = credits_weighted/credits_counted |
---|
114 | return level_gpa, credits_counted, credits_weighted |
---|
115 | |
---|
116 | @property |
---|
117 | def gpa_params(self): |
---|
118 | """Calculate gpa parameters for this level. |
---|
119 | """ |
---|
120 | credits_weighted = 0.0 |
---|
121 | credits_counted = 0 |
---|
122 | level_gpa = 0.0 |
---|
123 | for ticket in self.values(): |
---|
124 | if ticket.total_score is not None: |
---|
125 | credits_counted += ticket.credits |
---|
126 | credits_weighted += ticket.credits * ticket.weight |
---|
127 | if credits_counted: |
---|
128 | level_gpa = credits_weighted / credits_counted |
---|
129 | # Override level_gpa if value has been imported |
---|
130 | # (not implemented in base package) |
---|
131 | imported_gpa = getattr(self, 'imported_gpa', None) |
---|
132 | if imported_gpa: |
---|
133 | level_gpa = imported_gpa |
---|
134 | return level_gpa, credits_counted, credits_weighted |
---|
135 | |
---|
136 | @property |
---|
137 | def gpa(self): |
---|
138 | """Return string formatted gpa value. |
---|
139 | """ |
---|
140 | format_float = getUtility(IKofaUtils).format_float |
---|
141 | return format_float(self.gpa_params[0], 2) |
---|
142 | |
---|
143 | @property |
---|
144 | def passed_params(self): |
---|
145 | """Determine the number and credits of passed and failed courses. |
---|
146 | Count the number of courses registered but not taken. |
---|
147 | This method is used for level reports and the |
---|
148 | OutstandingCoursesExporter. |
---|
149 | """ |
---|
150 | passed = failed = 0 |
---|
151 | courses_failed = '' |
---|
152 | credits_failed = 0 |
---|
153 | credits_passed = 0 |
---|
154 | courses_not_taken = '' |
---|
155 | for ticket in self.values(): |
---|
156 | if ticket.total_score is not None: |
---|
157 | if ticket.total_score < ticket.passmark: |
---|
158 | failed += 1 |
---|
159 | credits_failed += ticket.credits |
---|
160 | if ticket.mandatory: |
---|
161 | courses_failed += 'm_%s_m ' % ticket.code |
---|
162 | else: |
---|
163 | courses_failed += '%s ' % ticket.code |
---|
164 | else: |
---|
165 | passed += 1 |
---|
166 | credits_passed += ticket.credits |
---|
167 | else: |
---|
168 | courses_not_taken += '%s ' % ticket.code |
---|
169 | return (passed, failed, credits_passed, |
---|
170 | credits_failed, courses_failed, |
---|
171 | courses_not_taken) |
---|
172 | |
---|
173 | @property |
---|
174 | def cumulative_params(self): |
---|
175 | """Calculate the cumulative gpa and other cumulative parameters |
---|
176 | for this level. |
---|
177 | All levels below this level are taken under consideration |
---|
178 | (including repeating levels). |
---|
179 | This method is used for level reports and meanwhile also |
---|
180 | for session results presentations. |
---|
181 | """ |
---|
182 | credits_passed = 0 |
---|
183 | total_credits = 0 |
---|
184 | total_credits_counted = 0 |
---|
185 | total_credits_weighted = 0 |
---|
186 | cgpa = 0.0 |
---|
187 | if self.__parent__: |
---|
188 | for level in self.__parent__.values(): |
---|
189 | if level.level > self.level: |
---|
190 | continue |
---|
191 | credits_passed += level.passed_params[2] |
---|
192 | total_credits += level.total_credits |
---|
193 | gpa_params = level.gpa_params |
---|
194 | total_credits_counted += gpa_params[1] |
---|
195 | total_credits_weighted += gpa_params[2] |
---|
196 | if total_credits_counted: |
---|
197 | cgpa = total_credits_weighted/total_credits_counted |
---|
198 | # Override cgpa if value has been imported |
---|
199 | # (not implemented in base package) |
---|
200 | imported_cgpa = getattr(self, 'imported_cgpa', None) |
---|
201 | if imported_cgpa: |
---|
202 | cgpa = imported_cgpa |
---|
203 | return (cgpa, total_credits_counted, total_credits_weighted, |
---|
204 | total_credits, credits_passed) |
---|
205 | |
---|
206 | @property |
---|
207 | def is_current_level(self): |
---|
208 | try: |
---|
209 | return self.__parent__.current_level == self.level |
---|
210 | except AttributeError: |
---|
211 | return False |
---|
212 | |
---|
213 | def writeLogMessage(self, view, message): |
---|
214 | return self.__parent__.__parent__.writeLogMessage(view, message) |
---|
215 | |
---|
216 | @property |
---|
217 | def level_title(self): |
---|
218 | studylevelsource = StudyLevelSource() |
---|
219 | return studylevelsource.factory.getTitle(self.__parent__, self.level) |
---|
220 | |
---|
221 | @property |
---|
222 | def course_registration_forbidden(self): |
---|
223 | try: |
---|
224 | deadline = grok.getSite()['configuration'][ |
---|
225 | str(self.level_session)].coursereg_deadline |
---|
226 | except (TypeError, KeyError): |
---|
227 | return |
---|
228 | if not deadline or deadline > datetime.now(pytz.utc): |
---|
229 | return |
---|
230 | if len(self.student['payments']): |
---|
231 | for ticket in self.student['payments'].values(): |
---|
232 | if ticket.p_category == 'late_registration' and \ |
---|
233 | ticket.p_session == self.level_session and \ |
---|
234 | ticket.p_state == 'paid': |
---|
235 | return |
---|
236 | return _("Course registration has ended. " |
---|
237 | "Please pay the late registration fee.") |
---|
238 | |
---|
239 | def addCourseTicket(self, ticket, course): |
---|
240 | """Add a course ticket object. |
---|
241 | """ |
---|
242 | if not ICourseTicket.providedBy(ticket): |
---|
243 | raise TypeError( |
---|
244 | 'StudentStudyLeves contain only ICourseTicket instances') |
---|
245 | ticket.code = course.code |
---|
246 | ticket.title = course.title |
---|
247 | ticket.fcode = course.__parent__.__parent__.__parent__.code |
---|
248 | ticket.dcode = course.__parent__.__parent__.code |
---|
249 | ticket.credits = course.credits |
---|
250 | ticket.passmark = course.passmark |
---|
251 | ticket.semester = course.semester |
---|
252 | self[ticket.code] = ticket |
---|
253 | return |
---|
254 | |
---|
255 | def addCertCourseTickets(self, cert): |
---|
256 | """Collect all certificate courses and create course |
---|
257 | tickets automatically. |
---|
258 | """ |
---|
259 | if cert is not None: |
---|
260 | for key, val in cert.items(): |
---|
261 | if val.level != self.level: |
---|
262 | continue |
---|
263 | ticket = createObject(u'waeup.CourseTicket') |
---|
264 | ticket.automatic = True |
---|
265 | ticket.mandatory = val.mandatory |
---|
266 | ticket.carry_over = False |
---|
267 | ticket.course_category = val.course_category |
---|
268 | self.addCourseTicket(ticket, val.course) |
---|
269 | return |
---|
270 | |
---|
271 | def updateCourseTicket(self, ticket, course): |
---|
272 | """Updates a course ticket object and return code |
---|
273 | if ticket has been invalidated. |
---|
274 | """ |
---|
275 | if not course: |
---|
276 | if ticket.title.endswith('cancelled)'): |
---|
277 | # Skip this tiket |
---|
278 | return |
---|
279 | # Invalidate course ticket |
---|
280 | ticket.title += u' (course cancelled)' |
---|
281 | ticket.credits = 0 |
---|
282 | ticket.passmark = 0 |
---|
283 | return ticket.code |
---|
284 | ticket.code = course.code |
---|
285 | ticket.title = course.title |
---|
286 | ticket.fcode = course.__parent__.__parent__.__parent__.code |
---|
287 | ticket.dcode = course.__parent__.__parent__.code |
---|
288 | ticket.credits = course.credits |
---|
289 | ticket.passmark = course.passmark |
---|
290 | ticket.semester = course.semester |
---|
291 | return |
---|
292 | |
---|
293 | StudentStudyLevel = attrs_to_fields( |
---|
294 | StudentStudyLevel, omit=['total_credits', 'gpa']) |
---|
295 | |
---|
296 | class StudentStudyLevelFactory(grok.GlobalUtility): |
---|
297 | """A factory for student study levels. |
---|
298 | """ |
---|
299 | grok.implements(IFactory) |
---|
300 | grok.name(u'waeup.StudentStudyLevel') |
---|
301 | title = u"Create a new student study level.", |
---|
302 | description = u"This factory instantiates new student study level instances." |
---|
303 | |
---|
304 | def __call__(self, *args, **kw): |
---|
305 | return StudentStudyLevel() |
---|
306 | |
---|
307 | def getInterfaces(self): |
---|
308 | return implementedBy(StudentStudyLevel) |
---|
309 | |
---|
310 | class CourseTicket(grok.Model): |
---|
311 | """This is a course ticket which allows the |
---|
312 | student to attend the course. Lecturers will enter scores and more at |
---|
313 | the end of the term. |
---|
314 | |
---|
315 | A course ticket contains a copy of the original course and |
---|
316 | certificate course data. If the courses and/or the referring certificate |
---|
317 | courses are removed, the corresponding tickets remain unchanged. |
---|
318 | So we do not need any event triggered actions on course tickets. |
---|
319 | """ |
---|
320 | grok.implements(ICourseTicket, IStudentNavigation) |
---|
321 | grok.provides(ICourseTicket) |
---|
322 | |
---|
323 | def __init__(self): |
---|
324 | super(CourseTicket, self).__init__() |
---|
325 | self.code = None |
---|
326 | return |
---|
327 | |
---|
328 | @property |
---|
329 | def student(self): |
---|
330 | """Get the associated student object. |
---|
331 | """ |
---|
332 | try: |
---|
333 | return self.__parent__.__parent__.__parent__ |
---|
334 | except AttributeError: # in unit tests |
---|
335 | return None |
---|
336 | |
---|
337 | @property |
---|
338 | def certcode(self): |
---|
339 | try: |
---|
340 | return self.__parent__.__parent__.certificate.code |
---|
341 | except AttributeError: # in unit tests |
---|
342 | return None |
---|
343 | |
---|
344 | @property |
---|
345 | def removable_by_student(self): |
---|
346 | """True if student is allowed to remove the ticket. |
---|
347 | """ |
---|
348 | return not self.mandatory |
---|
349 | |
---|
350 | @property |
---|
351 | def editable_by_lecturer(self): |
---|
352 | """True if lecturer is allowed to edit the ticket. |
---|
353 | """ |
---|
354 | try: |
---|
355 | cas = grok.getSite()[ |
---|
356 | 'configuration'].current_academic_session |
---|
357 | if self.student.state == VALIDATED and \ |
---|
358 | self.student.current_session == cas: |
---|
359 | return True |
---|
360 | except (AttributeError, TypeError): # in unit tests |
---|
361 | pass |
---|
362 | return False |
---|
363 | |
---|
364 | def writeLogMessage(self, view, message): |
---|
365 | return self.__parent__.__parent__.__parent__.writeLogMessage( |
---|
366 | view, message) |
---|
367 | |
---|
368 | @property |
---|
369 | def level(self): |
---|
370 | """Returns the id of the level the ticket has been added to. |
---|
371 | """ |
---|
372 | try: |
---|
373 | return self.__parent__.level |
---|
374 | except AttributeError: # in unit tests |
---|
375 | return None |
---|
376 | |
---|
377 | @property |
---|
378 | def level_session(self): |
---|
379 | """Returns the session of the level the ticket has been added to. |
---|
380 | """ |
---|
381 | try: |
---|
382 | return self.__parent__.level_session |
---|
383 | except AttributeError: # in unit tests |
---|
384 | return None |
---|
385 | |
---|
386 | @property |
---|
387 | def total_score(self): |
---|
388 | """Returns the total score of this ticket. In the base package |
---|
389 | this is simply the score. In customized packages this could be |
---|
390 | something else. |
---|
391 | """ |
---|
392 | return self.score |
---|
393 | |
---|
394 | @property |
---|
395 | def _getGradeWeightFromScore(self): |
---|
396 | """Nigerian Course Grading System |
---|
397 | """ |
---|
398 | if self.total_score is None: |
---|
399 | return (None, None) |
---|
400 | if self.total_score >= 70: |
---|
401 | return ('A',5) |
---|
402 | if self.total_score >= 60: |
---|
403 | return ('B',4) |
---|
404 | if self.total_score >= 50: |
---|
405 | return ('C',3) |
---|
406 | if self.total_score >= 45: |
---|
407 | return ('D',2) |
---|
408 | if self.total_score >= self.passmark: # passmark changed in 2013 from 40 to 45 |
---|
409 | return ('E',1) |
---|
410 | return ('F',0) |
---|
411 | |
---|
412 | @property |
---|
413 | def grade(self): |
---|
414 | """Returns the grade calculated from total score. |
---|
415 | """ |
---|
416 | return self._getGradeWeightFromScore[0] |
---|
417 | |
---|
418 | @property |
---|
419 | def weight(self): |
---|
420 | """Returns the weight calculated from total score. |
---|
421 | """ |
---|
422 | return self._getGradeWeightFromScore[1] |
---|
423 | |
---|
424 | CourseTicket = attrs_to_fields(CourseTicket) |
---|
425 | |
---|
426 | class CourseTicketFactory(grok.GlobalUtility): |
---|
427 | """A factory for student study levels. |
---|
428 | """ |
---|
429 | grok.implements(IFactory) |
---|
430 | grok.name(u'waeup.CourseTicket') |
---|
431 | title = u"Create a new course ticket.", |
---|
432 | description = u"This factory instantiates new course ticket instances." |
---|
433 | |
---|
434 | def __call__(self, *args, **kw): |
---|
435 | return CourseTicket() |
---|
436 | |
---|
437 | def getInterfaces(self): |
---|
438 | return implementedBy(CourseTicket) |
---|