| 1 | ## $Id: level_report.py 16692 2021-11-02 13:03:05Z henrik $ |
|---|
| 2 | ## |
|---|
| 3 | ## Copyright (C) 2013 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 | import grok |
|---|
| 20 | import textwrap |
|---|
| 21 | from zope.interface import implementer, Interface, Attribute |
|---|
| 22 | from zope.component import queryUtility, getUtility |
|---|
| 23 | from zope.i18n import translate |
|---|
| 24 | from zope.catalog.interfaces import ICatalog |
|---|
| 25 | |
|---|
| 26 | from reportlab.lib import colors |
|---|
| 27 | from reportlab.lib.styles import getSampleStyleSheet |
|---|
| 28 | from reportlab.lib.units import cm |
|---|
| 29 | from reportlab.platypus import Paragraph, Table, Spacer |
|---|
| 30 | from reportlab.platypus.flowables import Flowable |
|---|
| 31 | |
|---|
| 32 | from waeup.kofa.reports import IReport, IReportGenerator, Report |
|---|
| 33 | from waeup.kofa.browser.interfaces import IPDFCreator |
|---|
| 34 | from waeup.kofa.browser.pdf import SMALL_PARA_STYLE, get_signature_tables |
|---|
| 35 | from waeup.kofa.interfaces import ( |
|---|
| 36 | IKofaUtils, GRADUATED, |
|---|
| 37 | academic_sessions_vocab, registration_states_vocab) |
|---|
| 38 | |
|---|
| 39 | from waeup.kofa.students.reports.level_report import ( |
|---|
| 40 | LevelReport, LevelReportGenerator, |
|---|
| 41 | ILevelReport, LevelReportGeneratorPage, |
|---|
| 42 | LevelReportPDFView, |
|---|
| 43 | TTR) |
|---|
| 44 | |
|---|
| 45 | STYLE = getSampleStyleSheet() |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | TABLE_STYLE = [ |
|---|
| 49 | ('FONT', (0,0), (-1,-1), 'Helvetica', 8), |
|---|
| 50 | ('VALIGN', (0, 1), (-1,-1), 'TOP'), |
|---|
| 51 | #('FONT', (0,0), (0,-1), 'Helvetica-Bold', 8), # 1st column |
|---|
| 52 | ('FONT', (0,0), (-1,0), 'Helvetica-Bold', 8), # 1st row |
|---|
| 53 | #('FONT', (0,-1), (-1,-1), 'Helvetica-Bold', 8), # last row |
|---|
| 54 | #('FONT', (-1,0), (-1,-1), 'Helvetica-Bold', 8), # last column |
|---|
| 55 | ('ALIGN', (3,1), (-1,-1), 'RIGHT'), |
|---|
| 56 | ('ALIGN', (0,0), (0,-1), 'RIGHT'), |
|---|
| 57 | ('ALIGN', (7,0), (7,-1), 'LEFT'), |
|---|
| 58 | ('ALIGN', (8,0), (8,-1), 'LEFT'), |
|---|
| 59 | ('INNERGRID', (0,1), (-1,-1), 0.25, colors.black), |
|---|
| 60 | ('BOX', (0,1), (-1,-1), 1, colors.black), |
|---|
| 61 | ] |
|---|
| 62 | |
|---|
| 63 | def tbl_data_to_table(data): |
|---|
| 64 | result = [] |
|---|
| 65 | col_names = ( |
|---|
| 66 | 'S/N', |
|---|
| 67 | 'Matric No.', |
|---|
| 68 | 'Student Name', |
|---|
| 69 | 'Sex', |
|---|
| 70 | TTR('Total Credits Taken'), |
|---|
| 71 | TTR('Total Credits Passed'), |
|---|
| 72 | TTR('GPA'), |
|---|
| 73 | 'Courses Failed', |
|---|
| 74 | 'Outstanding Courses', |
|---|
| 75 | TTR('Cum. Credits Taken'), |
|---|
| 76 | TTR('Cum. Credits Passed'), |
|---|
| 77 | TTR('Cum. Credits Required'), |
|---|
| 78 | TTR('CGPA'), |
|---|
| 79 | TTR('Remark / Class of Degree (Grad.)')) |
|---|
| 80 | table = [col_names] |
|---|
| 81 | sn = 1 |
|---|
| 82 | for line in data: |
|---|
| 83 | # Underline mandatory (core) courses |
|---|
| 84 | failed_courses = Paragraph( |
|---|
| 85 | line[6].replace( |
|---|
| 86 | 'm_', '<u>').replace('_m', '</u>'), SMALL_PARA_STYLE) |
|---|
| 87 | line = line[:6] + (failed_courses,) + line[7:] |
|---|
| 88 | # Superscript in remarks (needed by AAUE) |
|---|
| 89 | remark = Paragraph( |
|---|
| 90 | line[12].replace( |
|---|
| 91 | 's_', '<super>').replace('_s', '</super>'), SMALL_PARA_STYLE) |
|---|
| 92 | line = line[:12] + (remark,) |
|---|
| 93 | line = (sn,) + line |
|---|
| 94 | table.append(line) |
|---|
| 95 | sn += 1 |
|---|
| 96 | return table |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | class CustomLevelReport(LevelReport): |
|---|
| 100 | """ |
|---|
| 101 | """ |
|---|
| 102 | |
|---|
| 103 | title = 'Summary of Results' |
|---|
| 104 | |
|---|
| 105 | @property |
|---|
| 106 | def signatures(self): |
|---|
| 107 | site = grok.getSite() |
|---|
| 108 | #dean = site['faculties'][self.faccode].officer_1 |
|---|
| 109 | hod = site['faculties'][self.faccode][self.depcode].officer_1 |
|---|
| 110 | ca = site['faculties'][self.faccode][self.depcode].officer_2 |
|---|
| 111 | #if not dean: |
|---|
| 112 | # dean = 'Dean of Faculty' |
|---|
| 113 | if not hod: |
|---|
| 114 | hod = 'HoD' |
|---|
| 115 | if not ca: |
|---|
| 116 | ca = 'Course Adviser' |
|---|
| 117 | #dean = dean.replace(' - ', '<br />') |
|---|
| 118 | hod = hod.replace(' - ', '<br />') |
|---|
| 119 | ca = ca.replace(' - ', '<br />') |
|---|
| 120 | return [hod, ca] |
|---|
| 121 | |
|---|
| 122 | def _get_students(self, faccode, depcode, certcode, session, level): |
|---|
| 123 | """Get students in a certain department, studying a certain programmen |
|---|
| 124 | who registered courses in a certain session at a certain level. |
|---|
| 125 | |
|---|
| 126 | Returns a list of student data tuples. |
|---|
| 127 | """ |
|---|
| 128 | site = grok.getSite() |
|---|
| 129 | cat = queryUtility(ICatalog, name="students_catalog") |
|---|
| 130 | if certcode == 'all': |
|---|
| 131 | certcode = None |
|---|
| 132 | result = cat.searchResults( |
|---|
| 133 | depcode = (depcode, depcode), faccode = (faccode, faccode), |
|---|
| 134 | certcode = (certcode, certcode) |
|---|
| 135 | ) |
|---|
| 136 | table = [] |
|---|
| 137 | format_float = getUtility(IKofaUtils).format_float |
|---|
| 138 | for stud in result: |
|---|
| 139 | if not stud['studycourse'].has_key(str(level)): |
|---|
| 140 | continue |
|---|
| 141 | if stud.state == GRADUATED: |
|---|
| 142 | continue |
|---|
| 143 | level_obj = stud['studycourse'][str(level)] |
|---|
| 144 | if level_obj.level_session != session: |
|---|
| 145 | continue |
|---|
| 146 | if self._excluded(level_obj): |
|---|
| 147 | continue |
|---|
| 148 | passed_params = level_obj.passed_params |
|---|
| 149 | failed_courses = textwrap.fill(passed_params[4], 35) |
|---|
| 150 | not_taken_courses = textwrap.fill(passed_params[5], 17) |
|---|
| 151 | fullname = textwrap.fill(stud.display_fullname, 29) |
|---|
| 152 | sex = stud.sex |
|---|
| 153 | remark = getattr(level_obj, 'remark', '') |
|---|
| 154 | cert = stud['studycourse'].certificate |
|---|
| 155 | end_level = getattr(cert, 'end_level', None) |
|---|
| 156 | try: |
|---|
| 157 | tcrg = int(getattr(cert, 'custom_float_1', None)) |
|---|
| 158 | except TypeError: |
|---|
| 159 | tcrg = None |
|---|
| 160 | pass |
|---|
| 161 | credits_counted = level_obj.gpa_params[1] |
|---|
| 162 | credits_passed = passed_params[2] |
|---|
| 163 | level_gpa = format_float(level_obj.gpa_params[0], 2) |
|---|
| 164 | cum_credits_taken = level_obj.cumulative_params[1] |
|---|
| 165 | cum_credits_passed = level_obj.cumulative_params[4] |
|---|
| 166 | if end_level and level >= end_level: |
|---|
| 167 | cgpa = format_float(level_obj.cumulative_params[0], 2) |
|---|
| 168 | else: |
|---|
| 169 | cgpa = format_float(level_obj.cumulative_params[0], 3) |
|---|
| 170 | if remark in ('FRNS', 'NER', 'NYV'): |
|---|
| 171 | cgpa = '' |
|---|
| 172 | if remark in ('NER', 'NYV'): |
|---|
| 173 | # hide all data (requestd by AAUE, Nigeria) |
|---|
| 174 | credits_counted = '' |
|---|
| 175 | credits_passed = '' |
|---|
| 176 | level_gpa = '' |
|---|
| 177 | failed_courses = '' |
|---|
| 178 | not_taken_courses = '' |
|---|
| 179 | cum_credits_taken = '' |
|---|
| 180 | cum_credits_passed = '' |
|---|
| 181 | line = ( |
|---|
| 182 | stud.matric_number, |
|---|
| 183 | fullname, |
|---|
| 184 | sex, |
|---|
| 185 | credits_counted, |
|---|
| 186 | credits_passed, |
|---|
| 187 | level_gpa, |
|---|
| 188 | failed_courses, |
|---|
| 189 | not_taken_courses, |
|---|
| 190 | cum_credits_taken, |
|---|
| 191 | cum_credits_passed, |
|---|
| 192 | tcrg, |
|---|
| 193 | cgpa, |
|---|
| 194 | remark, |
|---|
| 195 | ) |
|---|
| 196 | table.append(line) |
|---|
| 197 | return sorted(table, key=lambda value:value[0]) |
|---|
| 198 | |
|---|
| 199 | def create_pdf(self, job_id): |
|---|
| 200 | creator = getUtility(IPDFCreator, name=self.pdfcreator) |
|---|
| 201 | table_data = tbl_data_to_table(self.data) |
|---|
| 202 | #col_widths = [3.5*cm] * len(self.data[0]) |
|---|
| 203 | col_widths = [0.8*cm, 3*cm, 5*cm, 0.8*cm, 0.8*cm, 0.8*cm, 1*cm, |
|---|
| 204 | 4*cm, 4*cm, 0.8*cm, 0.8*cm, 0.8*cm, 1*cm, 2*cm] |
|---|
| 205 | pdf_data = [Paragraph('<b>%s - Report %s</b>' |
|---|
| 206 | % (self.creation_dt_string, job_id), |
|---|
| 207 | STYLE["Normal"]), |
|---|
| 208 | Spacer(1, 12),] |
|---|
| 209 | pdf_data += [Paragraph( |
|---|
| 210 | translate( |
|---|
| 211 | '${a}<br />${b}<br />${c}<br />' |
|---|
| 212 | 'Session: ${d}<br />Level: ${e}', |
|---|
| 213 | mapping = {'a':self.certtitle, |
|---|
| 214 | 'b':self.deptitle, |
|---|
| 215 | 'c':self.factitle, |
|---|
| 216 | 'd':self.session, |
|---|
| 217 | 'e':self.level, |
|---|
| 218 | }), |
|---|
| 219 | STYLE["Normal"]), |
|---|
| 220 | Spacer(1, 12),] |
|---|
| 221 | pdf_data += [ |
|---|
| 222 | Table(table_data, style=TABLE_STYLE, colWidths=col_widths)] |
|---|
| 223 | pdf_data.append(Spacer(1, 40)) |
|---|
| 224 | if self.signatures: |
|---|
| 225 | signaturetables = get_signature_tables( |
|---|
| 226 | self.signatures, landscape=True) |
|---|
| 227 | pdf_data.append(signaturetables[0]) |
|---|
| 228 | |
|---|
| 229 | pdf = creator.create_pdf( |
|---|
| 230 | pdf_data, None, self.title, self.author, |
|---|
| 231 | self.right_footer, note = self.note |
|---|
| 232 | ) |
|---|
| 233 | return pdf |
|---|
| 234 | |
|---|
| 235 | class CustomLevelReportGenerator(LevelReportGenerator): |
|---|
| 236 | |
|---|
| 237 | def generate(self, site, faccode=None, depcode=None, certcode=None, |
|---|
| 238 | session=None, level=None, author=None): |
|---|
| 239 | result = CustomLevelReport(faccode=faccode, depcode=depcode, |
|---|
| 240 | certcode=certcode, session=session, level=level, author=author) |
|---|
| 241 | return result |
|---|
| 242 | |
|---|
| 243 | # Level Report for graduating students only |
|---|
| 244 | |
|---|
| 245 | class IGradLevelReport(ILevelReport): |
|---|
| 246 | """Marker interface to distuingish graduating students level reports |
|---|
| 247 | from ordinary level reports. |
|---|
| 248 | """ |
|---|
| 249 | |
|---|
| 250 | @implementer(IGradLevelReport) |
|---|
| 251 | class GradLevelReport(CustomLevelReport): |
|---|
| 252 | """ |
|---|
| 253 | """ |
|---|
| 254 | |
|---|
| 255 | title = 'Summary of Results (Graduating Students only)' |
|---|
| 256 | |
|---|
| 257 | @property |
|---|
| 258 | def right_footer(self): |
|---|
| 259 | return self.title + ' - %s -' % self.session |
|---|
| 260 | |
|---|
| 261 | def _excluded(self, level_obj): |
|---|
| 262 | # Do not list student if selecetd level is not final or an extension |
|---|
| 263 | if level_obj.level < level_obj.student['studycourse'].certificate.end_level: |
|---|
| 264 | return True |
|---|
| 265 | # Filter out students with extension levels higher than selected level |
|---|
| 266 | final_level = int(max(level_obj.student['studycourse'].keys())) |
|---|
| 267 | if final_level > level_obj.level: |
|---|
| 268 | return True |
|---|
| 269 | if level_obj.remark not in ( |
|---|
| 270 | 'Pass', '3s_rd_s', '2s_2_s', '2s_1_s', '1s_st_s', |
|---|
| 271 | 'Merit', 'Credit', 'Distinction'): |
|---|
| 272 | return True |
|---|
| 273 | return False |
|---|
| 274 | |
|---|
| 275 | class GradLevelReportGenerator(LevelReportGenerator): |
|---|
| 276 | |
|---|
| 277 | # title = 'Summary of Results (Graduating Students)' |
|---|
| 278 | title = 'Level Report (Graduating Students only)' |
|---|
| 279 | grok.name('grad_level_report') |
|---|
| 280 | |
|---|
| 281 | def generate(self, site, faccode=None, depcode=None, certcode=None, |
|---|
| 282 | session=None, level=None, author=None): |
|---|
| 283 | result = GradLevelReport(faccode=faccode, depcode=depcode, |
|---|
| 284 | certcode=certcode, session=session, level=level, author=author) |
|---|
| 285 | return result |
|---|
| 286 | |
|---|
| 287 | class GradLevelReportGeneratorPage(LevelReportGeneratorPage): |
|---|
| 288 | |
|---|
| 289 | grok.context(GradLevelReportGenerator) |
|---|
| 290 | |
|---|
| 291 | class GradLevelReportPDFView(LevelReportPDFView): |
|---|
| 292 | |
|---|
| 293 | grok.context(IGradLevelReport) |
|---|
| 294 | grok.name('pdf') |
|---|
| 295 | grok.require('waeup.handleReports') |
|---|
| 296 | |
|---|
| 297 | def _filename(self): |
|---|
| 298 | return 'GradLevelReport_rno%s_%s.pdf' % ( |
|---|
| 299 | self.context.__name__, |
|---|
| 300 | self.context.creation_dt_string) |
|---|