[9633] | 1 | ## $Id: student_statistics.py 15698 2019-10-22 10:54:34Z 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 | import grok |
---|
[12897] | 19 | from zope.i18n import translate |
---|
[9633] | 20 | from zope.catalog.interfaces import ICatalog |
---|
| 21 | from zope.component import queryUtility, getUtility |
---|
[9680] | 22 | from zope.interface import implementer, Interface, Attribute |
---|
[9633] | 23 | from waeup.kofa.interfaces import ( |
---|
[9649] | 24 | IKofaUtils, |
---|
| 25 | academic_sessions_vocab, registration_states_vocab) |
---|
[12897] | 26 | from waeup.kofa.students.vocabularies import StudyLevelSource |
---|
[9633] | 27 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
| 28 | from waeup.kofa.reports import IReport |
---|
| 29 | |
---|
[10550] | 30 | class IStudentStatisticsReport(IReport): |
---|
[9633] | 31 | |
---|
[9680] | 32 | session = Attribute('Session to report') |
---|
[12897] | 33 | level = Attribute('Level to report') |
---|
[9680] | 34 | mode = Attribute('Study modes group to report') |
---|
| 35 | creation_dt_string = Attribute('Human readable report creation datetime') |
---|
| 36 | |
---|
[12897] | 37 | def get_student_stats(session, mode, level, breakdown): |
---|
| 38 | """Get students in a certain session, study mode and current level. |
---|
[9633] | 39 | |
---|
[12515] | 40 | Returns a table ordered by code (faculty or department, one per row) and |
---|
[9633] | 41 | registration state (cols). The result is a 3-tuple representing |
---|
[13236] | 42 | ((<PATHS>), (<STATES>), (<NUM_OF_STUDENTS>)). The |
---|
[9633] | 43 | (<NUM_OF_STUDENTS>) is an n-tuple with each entry containing the |
---|
[12515] | 44 | number of students found in that faculty/department and with the respective |
---|
[9633] | 45 | state. |
---|
| 46 | |
---|
| 47 | Sample result: |
---|
| 48 | |
---|
| 49 | >>> ((u'FAC1', u'FAC2'), |
---|
| 50 | ... ('created', 'accepted', 'registered'), |
---|
| 51 | ... ((12, 10, 1), (0, 5, 25))) |
---|
| 52 | |
---|
| 53 | This result means: there are 5 students in FAC2 in state |
---|
| 54 | 'accepted' while 12 students in 'FAC1' are in state 'created'. |
---|
| 55 | """ |
---|
| 56 | site = grok.getSite() |
---|
| 57 | states = tuple([x.value for x in registration_states_vocab]) |
---|
| 58 | states = states + (u'Total',) |
---|
[12515] | 59 | if breakdown == 'faccode': |
---|
[13236] | 60 | paths = tuple(sorted([x for x in site['faculties'].keys()], |
---|
[12515] | 61 | key=lambda x: x.lower())) |
---|
| 62 | elif breakdown == 'depcode': |
---|
| 63 | faculties = site['faculties'] |
---|
[12619] | 64 | deppaths = [] |
---|
[12515] | 65 | for fac in faculties.values(): |
---|
| 66 | for dep in fac.values(): |
---|
[12619] | 67 | deppaths.append('%s/%s' % (fac.code, dep.code)) |
---|
| 68 | paths = tuple(sorted([x for x in deppaths], |
---|
| 69 | key=lambda x: x.lower())) |
---|
[9633] | 70 | # XXX: Here we do _one_ query and then examine the single |
---|
| 71 | # students. One could also do multiple queries and just look for |
---|
| 72 | # the result length (not introspecting the delivered students |
---|
| 73 | # further). |
---|
| 74 | cat = queryUtility(ICatalog, name="students_catalog") |
---|
| 75 | result = cat.searchResults(current_session=(session, session)) |
---|
[13236] | 76 | table = [[0 for x in xrange(len(states))] for y in xrange(len(paths)+1)] |
---|
[9649] | 77 | mode_groups = getUtility(IKofaUtils).MODE_GROUPS |
---|
[9633] | 78 | for stud in result: |
---|
[14512] | 79 | if mode != 'All' and stud.current_mode \ |
---|
| 80 | and stud.current_mode not in mode_groups[mode]: |
---|
[9647] | 81 | continue |
---|
[12897] | 82 | if level != 0 and stud.current_level != level: |
---|
| 83 | continue |
---|
[13236] | 84 | if getattr(stud, breakdown) is None: |
---|
[9927] | 85 | continue |
---|
[13236] | 86 | if breakdown == 'faccode': |
---|
| 87 | row_name = getattr(stud, 'faccode') |
---|
| 88 | elif breakdown == 'depcode': |
---|
| 89 | row_name = '%s/%s' % ( |
---|
| 90 | getattr(stud, 'faccode'), getattr(stud, 'depcode')) |
---|
| 91 | row = paths.index(row_name) |
---|
[9633] | 92 | col = states.index(stud.state) |
---|
| 93 | table[row][col] += 1 |
---|
| 94 | table[-1][col] += 1 |
---|
| 95 | table[row][-1] += 1 |
---|
| 96 | table[-1][-1] += 1 |
---|
| 97 | # turn lists into tuples |
---|
| 98 | table = tuple([tuple(row) for row in table]) |
---|
| 99 | |
---|
[12619] | 100 | paths = paths + (u'Total',) |
---|
| 101 | return (paths, states, table) |
---|
| 102 | |
---|
[9633] | 103 | from reportlab.lib import colors |
---|
| 104 | from reportlab.lib.styles import getSampleStyleSheet |
---|
| 105 | from reportlab.lib.units import cm |
---|
| 106 | from reportlab.platypus import Paragraph, Table, Spacer |
---|
| 107 | from waeup.kofa.reports import IReport, IReportGenerator |
---|
| 108 | from waeup.kofa.reports import Report |
---|
| 109 | from waeup.kofa.browser.interfaces import IPDFCreator |
---|
| 110 | |
---|
| 111 | STYLE = getSampleStyleSheet() |
---|
| 112 | |
---|
| 113 | def tbl_data_to_table(row_names, col_names, data): |
---|
| 114 | result = [] |
---|
| 115 | new_col_names = [] |
---|
| 116 | for name in col_names: |
---|
| 117 | new_col_names.append(name.replace(' ', '\n')) |
---|
| 118 | head = [''] + list(new_col_names) |
---|
| 119 | result = [head] |
---|
| 120 | for idx, row_name in enumerate(row_names): |
---|
| 121 | row = [row_name] + list(data[idx]) |
---|
| 122 | result.append(row) |
---|
| 123 | return result |
---|
| 124 | |
---|
| 125 | TABLE_STYLE = [ |
---|
| 126 | ('FONT', (0,0), (-1,-1), 'Helvetica', 8), |
---|
| 127 | ('FONT', (0,0), (0,-1), 'Helvetica-Bold', 8), |
---|
| 128 | ('FONT', (0,0), (-1,0), 'Helvetica-Bold', 8), |
---|
| 129 | ('FONT', (0,-1), (-1,-1), 'Helvetica-Bold', 8), |
---|
| 130 | ('FONT', (-1,0), (-1,-1), 'Helvetica-Bold', 8), |
---|
| 131 | ('ALIGN', (1,1), (-1,-1), 'RIGHT'), |
---|
| 132 | ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), |
---|
| 133 | ('LINEBELOW', (0,-1), (-1,-1), 0.25, colors.black), |
---|
| 134 | ('LINEAFTER', (-1,0), (-1,-1), 0.25, colors.black), |
---|
| 135 | ('LINEBEFORE', (-1,0), (-1,-1), 1.0, colors.black), |
---|
[10558] | 136 | #('LINEABOVE', (0,-1), (-1,-1), 1.0, colors.black), |
---|
| 137 | #('LINEABOVE', (0,0), (-1,0), 0.25, colors.black), |
---|
[9633] | 138 | ] |
---|
| 139 | |
---|
[10550] | 140 | @implementer(IStudentStatisticsReport) |
---|
| 141 | class StudentStatisticsReport(Report): |
---|
[9633] | 142 | data = None |
---|
| 143 | session = None |
---|
[9647] | 144 | mode = None |
---|
[14438] | 145 | pdfcreator = 'landscape' |
---|
[14607] | 146 | title = translate(_('Student Statistics')) |
---|
[9633] | 147 | |
---|
[14610] | 148 | @property |
---|
| 149 | def title(self): |
---|
| 150 | return translate(_('Student Statistics')) |
---|
| 151 | |
---|
[12897] | 152 | def __init__(self, session, mode, level, breakdown, author='System'): |
---|
[10550] | 153 | super(StudentStatisticsReport, self).__init__( |
---|
[12515] | 154 | args=[session, mode, breakdown], kwargs={'author':author}) |
---|
[14514] | 155 | self.sessioncode = session |
---|
| 156 | self.levelcode = level |
---|
[12897] | 157 | self.studylevelsource = StudyLevelSource().factory |
---|
| 158 | self.portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
[9647] | 159 | self.session = academic_sessions_vocab.getTerm(session).title |
---|
| 160 | self.mode = mode |
---|
[12897] | 161 | if level == 0: |
---|
[14514] | 162 | self.level = 'All levels' |
---|
[12897] | 163 | else: |
---|
[14514] | 164 | self.level = translate( |
---|
| 165 | self.studylevelsource.getTitle(None, int(level)), |
---|
[12897] | 166 | 'waeup.kofa', target_language=self.portal_language) |
---|
[12515] | 167 | self.breakdown = breakdown |
---|
[9633] | 168 | self.author = author |
---|
[9678] | 169 | self.creation_dt_string = self.creation_dt.astimezone( |
---|
[9668] | 170 | getUtility(IKofaUtils).tzinfo).strftime("%Y-%m-%d %H:%M:%S %Z") |
---|
[12897] | 171 | self.data = get_student_stats(session, mode, level, breakdown) |
---|
[9633] | 172 | |
---|
[14373] | 173 | def create_pdf(self, job_id): |
---|
[14438] | 174 | creator = getUtility(IPDFCreator, name=self.pdfcreator) |
---|
[9633] | 175 | table_data = tbl_data_to_table(*self.data) |
---|
| 176 | col_widths = [None,] + [1.6*cm] * len(self.data[1]) + [None,] |
---|
[14373] | 177 | pdf_data = [Paragraph('<b>%s - Report %s</b>' |
---|
| 178 | % (self.creation_dt_string, job_id), |
---|
[9657] | 179 | STYLE["Normal"]), |
---|
| 180 | Spacer(1, 12),] |
---|
[14514] | 181 | pdf_data += [Paragraph( |
---|
| 182 | translate( |
---|
| 183 | 'Study Mode: ${a}<br />' |
---|
| 184 | 'Academic Session: ${b}<br />Level: ${c}<br />', |
---|
| 185 | mapping = {'a':self.mode, |
---|
| 186 | 'b':self.session, |
---|
| 187 | 'c':self.level, |
---|
| 188 | }), |
---|
| 189 | STYLE["Normal"]), |
---|
| 190 | Spacer(1, 12),] |
---|
[9657] | 191 | pdf_data += [ |
---|
[9633] | 192 | Table(table_data, style=TABLE_STYLE, colWidths=col_widths)] |
---|
[14514] | 193 | right_footer = translate( |
---|
| 194 | _('${a} Students - ${b} -', |
---|
| 195 | mapping = {'a':self.mode, 'b':self.session})) |
---|
| 196 | pdf = creator.create_pdf( |
---|
[14610] | 197 | pdf_data, None, self.title, self.author, right_footer |
---|
[14514] | 198 | ) |
---|
[9633] | 199 | return pdf |
---|
| 200 | |
---|
| 201 | @implementer(IReportGenerator) |
---|
[10550] | 202 | class StudentStatisticsReportGenerator(grok.GlobalUtility): |
---|
[9633] | 203 | |
---|
[10559] | 204 | title = _('Student Statistics') |
---|
| 205 | grok.name('student_stats') |
---|
[9633] | 206 | |
---|
[12897] | 207 | def generate( |
---|
| 208 | self, site, session=None, mode=None, |
---|
| 209 | level=None, breakdown=None, author=None): |
---|
[12515] | 210 | result = StudentStatisticsReport( |
---|
[12897] | 211 | session=session, mode=mode, level=level, |
---|
| 212 | breakdown=breakdown, author=author) |
---|
[9633] | 213 | return result |
---|
| 214 | |
---|
| 215 | ############################################################### |
---|
| 216 | ## Browser related stuff |
---|
| 217 | ## |
---|
| 218 | ## XXX: move to local browser module |
---|
| 219 | ############################################################### |
---|
| 220 | from waeup.kofa.browser.layout import KofaPage |
---|
| 221 | from waeup.kofa.interfaces import academic_sessions_vocab |
---|
| 222 | from waeup.kofa.reports import get_generators |
---|
[11254] | 223 | from waeup.kofa.browser.breadcrumbs import Breadcrumb |
---|
[9633] | 224 | grok.templatedir('browser_templates') |
---|
[10550] | 225 | class StudentStatisticsReportGeneratorPage(KofaPage): |
---|
[9633] | 226 | |
---|
[10550] | 227 | grok.context(StudentStatisticsReportGenerator) |
---|
[9633] | 228 | grok.name('index.html') |
---|
[12900] | 229 | grok.require('waeup.handleReports') |
---|
[9633] | 230 | |
---|
[10555] | 231 | label = _('Create student statistics report') |
---|
[9637] | 232 | |
---|
[9633] | 233 | @property |
---|
| 234 | def generator_name(self): |
---|
| 235 | for name, gen in get_generators(): |
---|
| 236 | if gen == self.context: |
---|
| 237 | return name |
---|
| 238 | return None |
---|
| 239 | |
---|
[12897] | 240 | def update( |
---|
| 241 | self, CREATE=None, session=None, mode=None, level=None, breakdown=None): |
---|
[9633] | 242 | self.parent_url = self.url(self.context.__parent__) |
---|
| 243 | self._set_session_values() |
---|
[9647] | 244 | self._set_mode_values() |
---|
[12897] | 245 | self._set_level_values() |
---|
[12515] | 246 | self._set_breakdown_values() |
---|
[9633] | 247 | if CREATE and session: |
---|
[12897] | 248 | # create a new report job for students by session and level |
---|
[9633] | 249 | container = self.context.__parent__ |
---|
| 250 | user_id = self.request.principal.id |
---|
[9647] | 251 | kw = dict( |
---|
| 252 | session=int(session), |
---|
[12515] | 253 | mode=mode, |
---|
[12897] | 254 | level=int(level), |
---|
[12515] | 255 | breakdown=breakdown) |
---|
[9633] | 256 | self.flash(_('New report is being created in background')) |
---|
[9679] | 257 | job_id = container.start_report_job( |
---|
[9633] | 258 | self.generator_name, user_id, kw=kw) |
---|
[9654] | 259 | ob_class = self.__implemented__.__name__.replace('waeup.kofa.','') |
---|
| 260 | grok.getSite().logger.info( |
---|
[14514] | 261 | '%s - report %s created: %s ' |
---|
| 262 | '(session=%s, mode=%s, level=%s, breakdown=%s)' % ( |
---|
[12897] | 263 | ob_class, job_id, self.context.title, |
---|
| 264 | session, mode, level, breakdown)) |
---|
[9633] | 265 | self.redirect(self.parent_url) |
---|
| 266 | return |
---|
| 267 | return |
---|
| 268 | |
---|
| 269 | def _set_session_values(self): |
---|
| 270 | vocab_terms = academic_sessions_vocab.by_value.values() |
---|
| 271 | self.sessions = [(x.title, x.token) for x in vocab_terms] |
---|
| 272 | return |
---|
| 273 | |
---|
[9647] | 274 | def _set_mode_values(self): |
---|
[9649] | 275 | mode_groups = getUtility(IKofaUtils).MODE_GROUPS |
---|
| 276 | self.modes = sorted([(key, key) for key in mode_groups.keys()]) |
---|
[9647] | 277 | return |
---|
[9633] | 278 | |
---|
[12897] | 279 | def _set_level_values(self): |
---|
| 280 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
| 281 | studylevelsource = StudyLevelSource().factory |
---|
[14514] | 282 | self.levels = [(u'All', 0)] |
---|
[12897] | 283 | for code in studylevelsource.getValues(None): |
---|
| 284 | title = translate(studylevelsource.getTitle(None, code), |
---|
| 285 | 'waeup.kofa', target_language=portal_language) |
---|
| 286 | self.levels.append((title, code)) |
---|
| 287 | return |
---|
| 288 | |
---|
[12515] | 289 | def _set_breakdown_values(self): |
---|
[15698] | 290 | self.breakdowns = [(_('Faculties'), 'faccode'), (_('Departments'), 'depcode')] |
---|
[12515] | 291 | return |
---|
| 292 | |
---|
[10550] | 293 | class StudentStatisticsReportPDFView(grok.View): |
---|
[9633] | 294 | |
---|
[10550] | 295 | grok.context(IStudentStatisticsReport) |
---|
[9633] | 296 | grok.name('pdf') |
---|
[12913] | 297 | grok.require('waeup.handleReports') |
---|
[9633] | 298 | |
---|
[10575] | 299 | def _filename(self): |
---|
[14514] | 300 | return 'StudentStatisticsReport_rno%s_%s.pdf' % ( |
---|
| 301 | self.context.__name__, |
---|
[9678] | 302 | self.context.creation_dt_string) |
---|
[10575] | 303 | |
---|
| 304 | def render(self): |
---|
| 305 | filename = self._filename().replace( |
---|
[9673] | 306 | '/', '_').replace(' ','_').replace(':', '-') |
---|
[9633] | 307 | self.response.setHeader( |
---|
| 308 | 'Content-Type', 'application/pdf') |
---|
| 309 | self.response.setHeader( |
---|
| 310 | 'Content-Disposition:', 'attachment; filename="%s' % filename) |
---|
[14373] | 311 | pdf_stream = self.context.create_pdf(self.context.__name__) |
---|
[9654] | 312 | ob_class = self.__implemented__.__name__.replace('waeup.kofa.','') |
---|
[9679] | 313 | grok.getSite().logger.info('%s - report %s downloaded: %s' % ( |
---|
| 314 | ob_class, self.context.__name__, filename)) |
---|
[9633] | 315 | return pdf_stream |
---|