source: main/waeup.aaue/trunk/src/waeup/aaue/students/studylevel.py @ 14600

Last change on this file since 14600 was 14576, checked in by Henrik Bettermann, 8 years ago

Take outstanding courses into consideration.

  • Property svn:keywords set to Id
File size: 15.3 KB
Line 
1## $Id: studylevel.py 14576 2017-02-23 14:22:19Z 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"""
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.component import createObject
27from zope.interface import implementedBy
28from waeup.kofa.utils.helpers import attrs_to_fields
29from waeup.kofa.interfaces import CREATED
30from waeup.kofa.students.browser import TicketError
31from waeup.kofa.students.studylevel import (
32    StudentStudyLevel, CourseTicket,
33    CourseTicketFactory, StudentStudyLevelFactory)
34from waeup.kofa.students.interfaces import IStudentNavigation, ICourseTicket
35from waeup.aaue.students.interfaces import (
36    ICustomStudentStudyLevel, ICustomCourseTicket)
37from waeup.aaue.interfaces import MessageFactory as _
38
39
40class 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 and not ticket.outstanding:
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 and not ticket.outstanding:
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 ticket.total_score is not None:
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 ticket.total_score is not None:
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
131
132        ######################################################
133        # Temporarily disable ug_ft course registration
134        if self.student.current_mode == 'ug_ft':
135            return _("Please check back later, course registration is not yet available.")
136        ######################################################
137
138
139
140        restitution_paid = True
141        if self.student.entry_session < 2016 \
142            and self.student.current_mode == 'ug_ft':
143            restitution_paid = False
144            for ticket in self.student['payments'].values():
145                if ticket.p_category == 'restitution' and \
146                    ticket.p_session == self.level_session and \
147                    ticket.p_state == 'paid':
148                        restitution_paid = True
149                        continue
150        if not restitution_paid:
151            return _("Please pay restitution fee first.")
152        if self.student.is_fresh:
153            return
154        try:
155            if self.student.is_postgrad:
156                deadline = grok.getSite()['configuration'][
157                        str(self.level_session)].coursereg_deadline_pg
158            elif self.student.current_mode.startswith('dp'):
159                deadline = grok.getSite()['configuration'][
160                        str(self.level_session)].coursereg_deadline_dp
161            elif self.student.current_mode.endswith('_pt'):
162                deadline = grok.getSite()['configuration'][
163                        str(self.level_session)].coursereg_deadline_pt
164            elif self.student.current_mode == 'found':
165                deadline = grok.getSite()['configuration'][
166                        str(self.level_session)].coursereg_deadline_found
167            else:
168                deadline = grok.getSite()['configuration'][
169                        str(self.level_session)].coursereg_deadline
170        except (TypeError, KeyError):
171            return
172        if not deadline or deadline > datetime.now(pytz.utc):
173            return
174        if len(self.student['payments']):
175            for ticket in self.student['payments'].values():
176                if ticket.p_category == 'late_registration' and \
177                    ticket.p_session == self.level_session and \
178                    ticket.p_state == 'paid':
179                        return
180        return _("Course registration has ended. "
181                 "Please pay the late registration fee.")
182
183    # only AAUE
184    @property
185    def remark(self):
186        certificate = getattr(self.__parent__,'certificate',None)
187        end_level = getattr(certificate, 'end_level', None)
188        failed_limit = 1.5
189        if self.student.entry_session < 2013:
190            failed_limit = 1.0
191        # final level student remark
192        if end_level and self.level >= end_level:
193            if self.level > end_level:
194                # spill-over level
195                if self.gpa_params[1] == 0:
196                    # no credits taken
197                    return 'NER'
198            elif self.gpa_params[1] < 30:
199                # credits taken below limit
200                return 'NER'
201            if self.level_verdict in ('FRNS', 'NER', 'NYV'):
202                return self.level_verdict
203            if '_m' in self.passed_params[4]:
204                return 'FRNS'
205            if not self.cumulative_params[0]:
206                return 'FRNS'
207            if len(self.passed_params[5]) \
208                and not self.passed_params[5] == 'Nil':
209                return 'FRNS'
210            if self.cumulative_params[0] < failed_limit:
211                return 'Fail'
212            if self.cumulative_params[0] < 1.5:
213                return 'Pass'
214            if self.cumulative_params[0] < 2.4:
215                return '3s_rd_s'
216            if self.cumulative_params[0] < 3.5:
217                return '2s_2_s'
218            if self.cumulative_params[0] < 4.5:
219                return '2s_1_s'
220            if self.cumulative_params[0] < 5.1:
221                return '1s_st_s'
222            return 'N/A'
223        # returning student remark
224        if self.level_verdict in ('FRNS', 'NER', 'NYV'):
225            return 'Probation'
226        if self.level_verdict == 'D':
227            return 'Withdrawn'
228        if self.gpa_params[1] < 30:
229            # credits taken below limit
230            return 'Probation'
231        if self.cumulative_params[0] < failed_limit:
232            return 'Probation'
233        if self.cumulative_params[0] < 5.1:
234            return 'Proceed'
235        return 'N/A'
236
237    def _schoolfeePaymentMade(self):
238        if len(self.student['payments']):
239            for ticket in self.student['payments'].values():
240                if ticket.p_state == 'paid' and \
241                    ticket.p_category in (
242                        'schoolfee', 'schoolfee_incl', 'schoolfee_2',)  and \
243                    ticket.p_session == self.student[
244                        'studycourse'].current_session:
245                    return True
246        return False
247
248    def _coursePaymentsMade(self, course):
249        if self.level_session < 2016:
250            return True
251        if not course.code[:3] in ('GST', 'ENT'):
252            return True
253        if len(self.student['payments']):
254            paid_cats = list()
255            for pticket in self.student['payments'].values():
256                if pticket.p_state == 'paid':
257                    paid_cats.append(pticket.p_category)
258            if course.code in ('GST101', 'GST102', 'GST111', 'GST112') and \
259                not 'gst_registration_1' in paid_cats:
260                return False
261            if course.code in ('GST222',) and \
262                not 'gst_registration_2' in paid_cats:
263                return False
264            if course.code in ('ENT201',) and \
265                not 'ent_registration_1' in paid_cats:
266                return False
267            if course.code in ('GST101', 'GST102') and \
268                not 'gst_text_book_1' in paid_cats and \
269                not 'gst_text_book_0' in paid_cats:
270                return False
271            if course.code in ('GST111', 'GST112') and \
272                not 'gst_text_book_2' in paid_cats and \
273                not 'gst_text_book_0' in paid_cats:
274                return False
275            if course.code in ('GST222',) and \
276                not 'gst_text_book_3' in paid_cats:
277                return False
278            if course.code in ('ENT201',) and \
279                not 'ent_text_book_1' in paid_cats:
280                return False
281            return True
282        return False
283
284    def addCourseTicket(self, ticket, course):
285        """Add a course ticket object.
286        """
287        if not ICourseTicket.providedBy(ticket):
288            raise TypeError(
289                'StudentStudyLeves contain only ICourseTicket instances')
290        # Raise TicketError if course is in 2nd semester but
291        # schoolfee has not yet been fully paid.
292        if course.semester == 2 and not self._schoolfeePaymentMade():
293            raise TicketError(
294                _('%s is a 2nd semester course which can only be added '
295                  'if school fees have been fully paid.' % course.code))
296        # Raise TicketError if registration fee or text
297        # book fee haven't been paid.
298        if not self._coursePaymentsMade(course):
299            raise TicketError(
300                _('%s can only be added if both registration fee and text '
301                  'book fee have been paid.'
302                  % course.code))
303        ticket.code = course.code
304        ticket.title = course.title
305        ticket.fcode = course.__parent__.__parent__.__parent__.code
306        ticket.dcode = course.__parent__.__parent__.code
307        ticket.credits = course.credits
308        if self.student.entry_session < 2013:
309            ticket.passmark = course.passmark - 5
310        else:
311            ticket.passmark = course.passmark
312        ticket.semester = course.semester
313        self[ticket.code] = ticket
314        return
315
316    def addCertCourseTickets(self, cert):
317        """Collect all certificate courses and create course
318        tickets automatically.
319        """
320        if cert is not None:
321            for key, val in cert.items():
322                if val.level != self.level:
323                    continue
324                ticket = createObject(u'waeup.CourseTicket')
325                ticket.automatic = True
326                ticket.mandatory = val.mandatory
327                ticket.carry_over = False
328                try:
329                    self.addCourseTicket(ticket, val.course)
330                except TicketError:
331                    pass
332        return
333
334CustomStudentStudyLevel = attrs_to_fields(
335    CustomStudentStudyLevel, omit=[
336    'total_credits', 'total_credits_s1', 'total_credits_s2', 'gpa'])
337
338class CustomStudentStudyLevelFactory(StudentStudyLevelFactory):
339    """A factory for student study levels.
340    """
341
342    def __call__(self, *args, **kw):
343        return CustomStudentStudyLevel()
344
345    def getInterfaces(self):
346        return implementedBy(CustomStudentStudyLevel)
347
348class CustomCourseTicket(CourseTicket):
349    """This is a course ticket which allows the
350    student to attend the course. Lecturers will enter scores and more at
351    the end of the term.
352
353    A course ticket contains a copy of the original course and
354    course referrer data. If the courses and/or their referrers are removed, the
355    corresponding tickets remain unchanged. So we do not need any event
356    triggered actions on course tickets.
357    """
358    grok.implements(ICustomCourseTicket, IStudentNavigation)
359    grok.provides(ICustomCourseTicket)
360
361    @property
362    def _getGradeWeightFromScore(self):
363        """Nigerian Course Grading System
364        """
365        if self.score == -1:
366            return ('-',0) # core course and result not yet available (used by AAUE)
367        if self.total_score is None:
368            return (None, None)
369        if self.total_score >= 70:
370            return ('A',5)
371        if self.total_score >= 60:
372            return ('B',4)
373        if self.total_score >= 50:
374            return ('C',3)
375        if self.total_score >= 45:
376            return ('D',2)
377        if self.total_score >= self.passmark: # passmark changed in 2013 from 40 to 45
378            return ('E',1)
379        return ('F',0)
380
381    @property
382    def total_score(self):
383        """Returns ca + score.
384        """
385        if self.score == -1:
386            return 0
387        if not None in (self.score, self.ca):
388            return self.score + self.ca
389        return None
390
391    @property
392    def editable_by_lecturer(self):
393        """True if lecturer is allowed to edit the ticket.
394        """
395        return True
396
397CustomCourseTicket = attrs_to_fields(CustomCourseTicket)
398
399class CustomCourseTicketFactory(CourseTicketFactory):
400    """A factory for student study levels.
401    """
402
403    def __call__(self, *args, **kw):
404        return CustomCourseTicket()
405
406    def getInterfaces(self):
407        return implementedBy(CustomCourseTicket)
Note: See TracBrowser for help on using the repository browser.