source: main/waeup.kofa/branches/henrik-transcript-workflow/src/waeup/kofa/students/studycourse.py @ 17952

Last change on this file since 17952 was 15155, checked in by Henrik Bettermann, 6 years ago

Reorganise interfaces.

Transcript processing views and viewlets are now in the context of studycourses. Officers can now validate, sign and release transcripts directly on the transcript page.

  • Property svn:keywords set to Id
File size: 6.3 KB
Line 
1## $Id: studycourse.py 15155 2018-09-20 07:04:17Z 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 the student study courses
20and contains the (student) study level objects.
21"""
22import grok
23from zope.component.interfaces import IFactory
24from zope.component import createObject
25from zope.interface import implementedBy
26from waeup.kofa.students.interfaces import (
27    IStudentStudyCourse, IStudentNavigation, IStudentStudyLevel,
28    IStudentTranscript)
29from waeup.kofa.students.studylevel import CourseTicket
30from waeup.kofa.students.workflow import CLEARED, RETURNING, PAID
31from waeup.kofa.utils.helpers import attrs_to_fields
32
33class StudentStudyCourse(grok.Container):
34    """This is a container for study levels.
35    """
36    grok.implements(IStudentStudyCourse, IStudentNavigation)
37    grok.provides(IStudentStudyCourse)
38
39    is_special_postgrad = False
40
41    def __init__(self):
42        super(StudentStudyCourse, self).__init__()
43        return
44
45    @property
46    def student(self):
47        return self.__parent__
48
49    def writeLogMessage(self, view, message):
50        return self.__parent__.writeLogMessage(view, message)
51
52    @property
53    def next_session_allowed(self):
54        certificate = getattr(self, 'certificate', None)
55        if certificate == None:
56            return False
57        if self.student.state in (CLEARED, RETURNING):
58            return True
59        if self.student.state == PAID \
60            and self.student.is_postgrad:
61            return True
62        return False
63
64    @property
65    def is_postgrad(self):
66        if self.certificate is None:
67            return False
68        return self.certificate.study_mode.startswith('pg')
69
70    @property
71    def is_current(self):
72        if self.__name__ and '_' in self.__name__:
73            return False
74        return True
75
76    @property
77    def is_previous(self):
78        if self.__name__ == 'studycourse_2':
79            return True
80        if self.__name__ == 'studycourse_1' and  \
81            not self.__parent__.has_key('studycourse_2'):
82            return True
83        return False
84
85    def addStudentStudyLevel(self, cert, studylevel):
86        """Add a study level object.
87        """
88        if not IStudentStudyLevel.providedBy(studylevel):
89            raise TypeError(
90                'StudentStudyCourses contain only IStudentStudyLevel instances')
91        self[str(studylevel.level)] = studylevel
92        studylevel.addCertCourseTickets(cert)
93        # Collect carry-over courses in base levels (not in repeating levels)
94        try:
95            co_enabled = grok.getSite()['configuration'].carry_over
96        except TypeError:
97            # In tests we might not have a site object
98            co_enabled = True
99        if not co_enabled or studylevel.level % 100 != 0:
100            return
101        levels = sorted(self.keys())
102        index = levels.index(str(studylevel.level))
103        if index <= 0:
104            return
105        previous_level = self[levels[index-1]]
106        for key, val in previous_level.items():
107            if val.total_score >= val.passmark:
108                continue
109            if key in self[str(studylevel.level)]:
110                # Carry-over ticket exists
111                continue
112            co_ticket = createObject(u'waeup.CourseTicket')
113            for name in ['code', 'title', 'credits', 'passmark',
114                         'semester', 'mandatory', 'fcode', 'dcode']:
115                setattr(co_ticket, name, getattr(val, name))
116            co_ticket.automatic = True
117            co_ticket.carry_over = True
118            self[str(studylevel.level)][co_ticket.code] = co_ticket
119        return
120
121    def getTranscriptData(self):
122        """Get a sorted list of dicts with level and course ticket data.
123
124        This method is used for transcripts.
125        """
126        levels = []
127        cgpa = 0.0
128        total_credits_counted = 0
129        for level_key, level_val in self.items():
130            tickets_1 = []
131            tickets_2 = []
132            tickets_3 = []
133            tickets = sorted(level_val.values(), key=lambda ticket: ticket.code)
134            for ticket in tickets:
135                if ticket.semester == 1:
136                    tickets_1.append(ticket)
137                elif ticket.semester == 2:
138                    tickets_2.append(ticket)
139                elif ticket.semester == 3:
140                    tickets_3.append(ticket)
141            gpa_params = level_val.gpa_params_rectified
142            sgpa = gpa_params[0]
143            total_credits_counted += gpa_params[1]
144            cgpa += gpa_params[2]
145            levels.append(
146                dict(level_key=level_key,
147                     level=level_val,
148                     tickets_1=tickets_1,
149                     tickets_2=tickets_2,
150                     tickets_3=tickets_3,
151                     sgpa=sgpa))
152        if total_credits_counted:
153            cgpa = cgpa/total_credits_counted
154        # Override cgpa if value has been imported
155        # (not implemented in base package)
156        imported_cgpa = getattr(self, 'imported_cgpa', None)
157        if imported_cgpa:
158            cgpa = imported_cgpa
159        return sorted(levels, key=lambda level: level['level_key']), cgpa
160
161StudentStudyCourse = attrs_to_fields(StudentStudyCourse)
162
163class StudentStudyCourseFactory(grok.GlobalUtility):
164    """A factory for student study courses.
165    """
166    grok.implements(IFactory)
167    grok.name(u'waeup.StudentStudyCourse')
168    title = u"Create a new student study course.",
169    description = u"This factory instantiates new student study course instances."
170
171    def __call__(self, *args, **kw):
172        return StudentStudyCourse()
173
174    def getInterfaces(self):
175        return implementedBy(StudentStudyCourse)
Note: See TracBrowser for help on using the repository browser.