source: main/waeup.kofa/trunk/src/waeup/kofa/students/studylevel.py @ 17946

Last change on this file since 17946 was 17378, checked in by Henrik Bettermann, 19 months ago

Bugfix: Rectified GPA calculation was wrong.

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