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