Example #1
0
    def export(self,
               response,
               course_types_list,
               include_not_enough_answers=False,
               include_unpublished=False):
        self.workbook = xlwt.Workbook()
        self.init_styles(self.workbook)
        counter = 1

        for course_types in course_types_list:
            self.sheet = self.workbook.add_sheet("Sheet " + str(counter))
            counter += 1
            self.row = 0
            self.col = 0

            courses_with_results = list()
            course_states = ['published']
            if include_unpublished:
                course_states.extend(['evaluated', 'reviewed'])

            used_questionnaires = set()
            for course in self.semester.course_set.filter(
                    state__in=course_states, type__in=course_types).all():
                if course.is_single_result:
                    continue
                if not course.can_publish_grades and not include_not_enough_answers:
                    continue
                results = OrderedDict()
                for questionnaire, contributor, __, data, __ in calculate_results(
                        course):
                    if has_no_rating_answers(course, contributor,
                                             questionnaire):
                        continue
                    results.setdefault(questionnaire.id, []).extend(data)
                    used_questionnaires.add(questionnaire)
                courses_with_results.append((course, results))

            courses_with_results.sort(key=lambda cr: (cr[0].type, cr[0].name))
            used_questionnaires = sorted(used_questionnaires)

            course_type_names = [
                ct.name
                for ct in CourseType.objects.filter(pk__in=course_types)
            ]
            writec(
                self,
                _("Evaluation {0}\n\n{1}").format(
                    self.semester.name, ", ".join(course_type_names)),
                "headline")

            for course, results in courses_with_results:
                writec(self, course.name, "course", cols=2)

            writen(self)
            for course, results in courses_with_results:
                writec(self, _("Avg."), "avg")
                writec(self, _("Std. dev."), "border_top_bottom_right")

            for questionnaire in used_questionnaires:
                writen(self, questionnaire.name, "bold")
                for course, results in courses_with_results:
                    self.write_two_empty_cells_with_borders()

                filtered_questions = self.filter_text_and_heading_questions(
                    questionnaire.question_set.all())

                for question in filtered_questions:
                    if question.is_heading_question:
                        writen(self, question.text, "italic")
                    else:
                        writen(self, question.text)

                    for course, results in courses_with_results:
                        if questionnaire.id not in results or question.is_heading_question:
                            self.write_two_empty_cells_with_borders()
                            continue
                        qn_results = results[questionnaire.id]
                        values = []
                        deviations = []
                        total_count = 0
                        approval_count = 0

                        for grade_result in qn_results:
                            if grade_result.question.id == question.id:
                                if grade_result.average:
                                    values.append(grade_result.average *
                                                  grade_result.total_count)
                                    deviations.append(grade_result.deviation *
                                                      grade_result.total_count)
                                    total_count += grade_result.total_count
                                if grade_result.question.is_yes_no_question:
                                    approval_count += grade_result.approval_count
                        enough_answers = course.can_publish_grades
                        if values and enough_answers:
                            avg = sum(values) / total_count
                            dev = sum(deviations) / total_count

                            if question.is_yes_no_question:
                                percent_approval = float(
                                    approval_count) / float(
                                        total_count) if total_count > 0 else 0
                                writec(self, "{:.0%}".format(percent_approval),
                                       self.grade_to_style(avg))
                                writec(self, None, "border_right")
                            else:
                                writec(self, avg, self.grade_to_style(avg))
                                writec(self, dev, self.deviation_to_style(dev))
                        else:
                            self.write_two_empty_cells_with_borders()
                writen(self, None)
                for course, results in courses_with_results:
                    self.write_two_empty_cells_with_borders()

            writen(self, _("Overall Average Grade"), "bold")
            for course, results in courses_with_results:
                avg, dev = calculate_average_grades_and_deviation(course)
                if avg:
                    writec(self,
                           avg,
                           self.grade_to_style(avg, total=True),
                           cols=2)
                else:
                    self.write_two_empty_cells_with_borders()

            writen(self, _("Overall Average Standard Deviation"), "bold")
            for course, results in courses_with_results:
                avg, dev = calculate_average_grades_and_deviation(course)
                if dev is not None:
                    writec(self,
                           dev,
                           self.deviation_to_style(dev, total=True),
                           cols=2)
                else:
                    self.write_two_empty_cells_with_borders()

            writen(self, _("Total voters/Total participants"), "bold")
            for course, results in courses_with_results:
                percent_participants = float(course.num_voters) / float(
                    course.num_participants
                ) if course.num_participants > 0 else 0
                writec(self,
                       "{}/{} ({:.0%})".format(course.num_voters,
                                               course.num_participants,
                                               percent_participants),
                       "total_voters",
                       cols=2)

        self.workbook.save(response)
Example #2
0
    def export(self, response, course_types_list, include_not_enough_answers=False, include_unpublished=False):
        self.workbook = xlwt.Workbook()
        self.init_styles(self.workbook)
        counter = 1

        for course_types in course_types_list:
            self.sheet = self.workbook.add_sheet("Sheet " + str(counter))
            counter += 1
            self.row = 0
            self.col = 0

            courses_with_results = list()
            course_states = ['published']
            if include_unpublished:
                course_states.extend(['evaluated', 'reviewed'])

            used_questionnaires = set()
            for course in self.semester.course_set.filter(state__in=course_states, type__in=course_types).all():
                if course.is_single_result:
                    continue
                if not course.can_publish_grades and not include_not_enough_answers:
                    continue
                results = OrderedDict()
                for questionnaire, contributor, __, data, __ in calculate_results(course):
                    if has_no_rating_answers(course, contributor, questionnaire):
                        continue
                    results.setdefault(questionnaire.id, []).extend(data)
                    used_questionnaires.add(questionnaire)
                courses_with_results.append((course, results))

            courses_with_results.sort(key=lambda cr: (cr[0].type, cr[0].name))
            used_questionnaires = sorted(used_questionnaires)

            course_type_names = [ct.name for ct in CourseType.objects.filter(pk__in=course_types)]
            writec(self, _("Evaluation {0}\n\n{1}").format(self.semester.name, ", ".join(course_type_names)), "headline")

            for course, results in courses_with_results:
                writec(self, course.name, "course", cols=2)

            writen(self)
            for course, results in courses_with_results:
                writec(self, _("Avg."), "avg")
                writec(self, _("Std. dev."), "border_top_bottom_right")

            for questionnaire in used_questionnaires:
                writen(self, questionnaire.name, "bold")
                for course, results in courses_with_results:
                    self.write_two_empty_cells_with_borders()

                for question in questionnaire.question_set.all():
                    if question.is_text_question:
                        continue

                    writen(self, question.text)

                    for course, results in courses_with_results:
                        if questionnaire.id not in results:
                            self.write_two_empty_cells_with_borders()
                            continue
                        qn_results = results[questionnaire.id]
                        values = []
                        deviations = []
                        total_count = 0
                        approval_count = 0

                        for grade_result in qn_results:
                            if grade_result.question.id == question.id:
                                if grade_result.average:
                                    values.append(grade_result.average * grade_result.total_count)
                                    deviations.append(grade_result.deviation * grade_result.total_count)
                                    total_count += grade_result.total_count
                                if grade_result.question.is_yes_no_question:
                                    approval_count += grade_result.approval_count
                        enough_answers = course.can_publish_grades
                        if values and enough_answers:
                            avg = sum(values) / total_count
                            dev = sum(deviations) / total_count

                            if question.is_yes_no_question:
                                percent_approval = float(approval_count) / float(total_count) if total_count > 0 else 0
                                writec(self, "{:.0%}".format(percent_approval), self.grade_to_style(avg))
                                writec(self, None, "border_right")
                            else:
                                writec(self, avg, self.grade_to_style(avg))
                                writec(self, dev, self.deviation_to_style(dev))
                        else:
                            self.write_two_empty_cells_with_borders()
                writen(self, None)
                for course, results in courses_with_results:
                    self.write_two_empty_cells_with_borders()

            writen(self, _("Overall Average Grade"), "bold")
            for course, results in courses_with_results:
                avg, dev = calculate_average_grades_and_deviation(course)
                if avg:
                    writec(self, avg, self.grade_to_style(avg, total=True), cols=2)
                else:
                    self.write_two_empty_cells_with_borders()

            writen(self, _("Overall Average Standard Deviation"), "bold")
            for course, results in courses_with_results:
                avg, dev = calculate_average_grades_and_deviation(course)
                if dev is not None:
                    writec(self, dev, self.deviation_to_style(dev, total=True), cols=2)
                else:
                    self.write_two_empty_cells_with_borders()

            writen(self, _("Total voters/Total participants"), "bold")
            for course, results in courses_with_results:
                percent_participants = float(course.num_voters) / float(course.num_participants) if course.num_participants > 0 else 0
                writec(self, "{}/{} ({:.0%})".format(course.num_voters, course.num_participants, percent_participants), "total_voters", cols=2)

        self.workbook.save(response)