~aelkner/schooltool/schooltool.niepa

« back to all changes in this revision

Viewing changes to src/schooltool/niepa/app/browser/report_card.py

  • Committer: Alan Elkner
  • Date: 2010-10-24 18:35:08 UTC
  • Revision ID: aelkner@ubuntu-20101024183508-a9x6cah2nf5sfkzc
created report card

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# SchoolTool - common information systems platform for school administration
 
3
# Copyright (c) 2010 Shuttleworth Foundation
 
4
#
 
5
# This program is free software; you can redistribute it and/or modify
 
6
# it under the terms of the GNU General Public License as published by
 
7
# the Free Software Foundation; either version 2 of the License, or
 
8
# (at your option) any later version.
 
9
#
 
10
# This program is distributed in the hope that it will be useful,
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
# GNU General Public License for more details.
 
14
#
 
15
# You should have received a copy of the GNU General Public License
 
16
# along with this program; if not, write to the Free Software
 
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
#
 
19
"""
 
20
SchoolTool Niepa report card view.
 
21
"""
 
22
 
 
23
from decimal import Decimal
 
24
from reportlab.platypus.flowables import Flowable
 
25
 
 
26
from zope.browserpage.viewpagetemplatefile import ViewPageTemplateFile
 
27
from zope.publisher.browser import BrowserView
 
28
from zope.security.proxy import removeSecurityProxy
 
29
from zope.traversing.browser.absoluteurl import absoluteURL
 
30
 
 
31
from schooltool.app.browser.report import ReportPDFView
 
32
from schooltool.app.interfaces import ISchoolToolApplication
 
33
from schooltool.course.interfaces import IInstructor, ISectionContainer
 
34
from schooltool.group.interfaces import IGroupContainer
 
35
from schooltool.schoolyear.interfaces import ISchoolYearContainer
 
36
from schooltool.schoolyear.interfaces import ISchoolYear
 
37
from schooltool.term.interfaces import ITerm
 
38
 
 
39
from schooltool.gradebook.interfaces import IActivities
 
40
from schooltool.requirement.interfaces import IEvaluations
 
41
from schooltool.requirement.scoresystem import UNSCORED
 
42
 
 
43
from schooltool.niepa import NiepaMessage as _
 
44
 
 
45
 
 
46
class RotatedText(Flowable):
 
47
 
 
48
    def __init__(self, text):
 
49
        self.text = text
 
50
 
 
51
    def draw(self):
 
52
        self.canv.saveState()
 
53
        canvas = self.canv
 
54
        canvas.rotate(90)
 
55
        canvas.drawString(5, -10, self.text)
 
56
        self.canv.restoreState()
 
57
 
 
58
    def wrap(self,aW,aH):
 
59
        canv = self.canv
 
60
        return canv._leading, canv.stringWidth(self.text)
 
61
 
 
62
 
 
63
class ReportCardPDFView(ReportPDFView):
 
64
    """A view for the term's report card pdf"""
 
65
 
 
66
    template=ViewPageTemplateFile('templates/report_card_pdf.pt')
 
67
    per_page = 26
 
68
 
 
69
    def __call__(self):
 
70
        self.term = removeSecurityProxy(self.context)
 
71
        self.year = ISchoolYear(self.term)
 
72
        terms = tuple(self.year.values())
 
73
        this_index = terms.index(self.term)
 
74
 
 
75
        courses = set()
 
76
        for term in terms:
 
77
            for section in ISectionContainer(term).values():
 
78
                courses.add(tuple(section.courses))
 
79
        courses = list(courses)
 
80
 
 
81
        course_accums = {}
 
82
        for course in courses:
 
83
            scores = course_accums[course] = []
 
84
            for index in range(this_index + 1):
 
85
                term = terms[index]
 
86
                for section in ISectionContainer(term).values():
 
87
                    if tuple(section.courses) == course:
 
88
                        break
 
89
                else:
 
90
                    section = None
 
91
                scores.append(self.getSectionScores(section, term))
 
92
 
 
93
        self.data = course_accums
 
94
        return super(ReportCardPDFView, self).__call__()
 
95
 
 
96
    def getSectionScores(self, section, term):
 
97
        results = {}
 
98
        activities = IActivities(section)
 
99
        key = '%s_%s' % (self.year.__name__, term.__name__)
 
100
        worksheet = activities[key]
 
101
        for student in section.members:
 
102
            scores = results[student] = []
 
103
            evaluations = IEvaluations(student)
 
104
            for activity in worksheet.values():
 
105
                ev = evaluations.get(activity, None)
 
106
                if ev is not None and ev.value is not UNSCORED:
 
107
                    value = ev.value
 
108
                else:
 
109
                    value = Decimal('0')
 
110
                scores.append(value)
 
111
        return results
 
112
 
 
113
    def pages(self):
 
114
        results = []
 
115
        courses_tuple = [(', '.join([c.title for c in course]), course)
 
116
                         for course in self.data]
 
117
 
 
118
        for title, course in sorted(courses_tuple):
 
119
            scores = self.data[course]
 
120
            students = [(student.title, student, scores[-1][student])
 
121
                        for student in scores[-1]]
 
122
 
 
123
            rows, index = [], 0
 
124
            for student_title, student, student_scores in sorted(students):
 
125
                ca1 = student_scores[0]
 
126
                ca2 = student_scores[1]
 
127
                ca3 = student_scores[2]
 
128
                ca_total = ca1 + ca2 + ca3
 
129
                exam = student_scores[3]
 
130
                total = ca_total + exam
 
131
 
 
132
                if len(scores) == 1:
 
133
                    before = ''
 
134
                    cumulative = total
 
135
                else:
 
136
                    before = Decimal('0')
 
137
                    for prev_index in range(len(scores) - 1):
 
138
                        for stu, stu_scores in scores[prev_index].items():
 
139
                            if stu == student:
 
140
                                before += sum(stu_scores)
 
141
                    if len(scores) == 2:
 
142
                        cumulative = int(round((total + before) / 2))
 
143
                    else:
 
144
                        cumulative = int(round((total + before) / 3))
 
145
                        before = int(round(before / 2))
 
146
 
 
147
                index += 1
 
148
                grade, remarks = '', ''
 
149
                row = {
 
150
                    'index': index,
 
151
                    'name': student_title,
 
152
                    'ca_test1': ca1,
 
153
                    'ca_test2': ca2,
 
154
                    'ca_test3': ca3,
 
155
                    'ca_total': ca_total,
 
156
                    'exam': exam,
 
157
                    'total': total,
 
158
                    'before': before,
 
159
                    'cumulative': cumulative,
 
160
                    'grade': grade,
 
161
                    'remarks': remarks,
 
162
                    }
 
163
                rows.append(row)
 
164
 
 
165
            class_average = Decimal('0')
 
166
            for row in rows:
 
167
                class_average += Decimal(row['cumulative'])
 
168
            class_average = int(round(class_average / len(rows)))
 
169
            for row in rows:
 
170
                row['class_average'] = class_average
 
171
 
 
172
            row_tuples = [(row['cumulative'], row) for row in rows]
 
173
            row_tuples = reversed(sorted(row_tuples))
 
174
            for index, (cumulative, row) in enumerate(row_tuples):
 
175
                row['position'] = index + 1
 
176
 
 
177
            while rows:
 
178
                page_rows = rows[:self.per_page]
 
179
                result = {
 
180
                    'course': title,
 
181
                    'term': self.term.title,
 
182
                    'rows': page_rows,
 
183
                    'heights': '0.9cm,3.5cm' + ',0.7cm' * (len(page_rows)),
 
184
                    }
 
185
                results.append(result)
 
186
                rows = rows[self.per_page:]
 
187
 
 
188
        return results
 
189
 
 
190
    def widths(self):
 
191
        return '0.8cm,5cm' + ',0.8cm' * 11 + ',4cm'
 
192