def benchmark_calculate_course_category_aggregate(student, course, category, marking_period, items=None): if items is None: items = Item.objects.all() save = True else: # don't store aggregates for every one-off combination of items save = False items = items.filter(course=course, category=category) # if we're passed marking_period=None, we should consider items across the entire duration of the course # if we're passed a specific marking period instead, we should consider items matching only that marking period if marking_period is not None: items = items.filter(marking_period=marking_period) calculation_rule = benchmark_find_calculation_rule(course.marking_period.all()[0].school_year) # initialize attributes criteria = {"course": course, "category": category, "marking_period": marking_period} # silly name is silly, and should not be part of the criteria silly_name = "G! {} - {} ({}, {})".format(student, category, course, marking_period) # don't use get_or_create; otherwise we may end up saving an empty object try: agg = benchmark_get_or_flush(student.aggregate_set, **criteria) created = False except Aggregate.DoesNotExist: agg = Aggregate(student=student, **criteria) created = True agg.name = silly_name # begin the actual calculations! agg.cached_substitution = None category_numer = category_denom = Decimal(0) if category.allow_multiple_demonstrations: for category_item in items.exclude(points_possible=None): # Find the highest mark amongst demonstrations and count it as the grade for the item best = Mark.objects.filter(student=student, item=category_item).aggregate(Max("mark"))["mark__max"] if best is not None: calculate_as, display_as = calculation_rule.substitute(category_item, best) category_numer += calculate_as category_denom += category_item.points_possible # yes, agg will just end up with the last substitution, but tough if display_as is not None: agg.cached_substitution = display_as else: for category_mark in ( Mark.objects.filter(student=student, item__in=items).exclude(mark=None).exclude(item__points_possible=None) ): calculate_as, display_as = calculation_rule.substitute(category_mark.item, category_mark.mark) category_numer += calculate_as category_denom += category_mark.item.points_possible if display_as is not None: agg.cached_substitution = display_as if category_denom: agg.cached_value = category_numer / category_denom * agg._fallback_points_possible() else: agg.cached_value = None if save: agg.save() return agg, created
def benchmark_calculate_course_aggregate(student, course, marking_period, items=None, recalculate_all_categories=False): # doesn't recalculate component aggregates by default if items is None: # just leave items alone--we don't actually consider it here; we only pass it to benchmark_calculate_course_category_aggregate # setting items here will prevent benchmark_calculate_course_category_aggregate from saving anything save = True items_categories = () else: # don't store aggregates for every one-off combination of items save = False # we'll have to miss cache and recaculate any category to which an item belongs items_categories = Category.objects.filter(item__in=items).distinct() calculation_rule = benchmark_find_calculation_rule(course.marking_period.all()[0].school_year) # initialize attributes criteria = {'student': student, 'course': course, 'category': None, 'marking_period': marking_period} # silly name is silly, and should not be part of the criteria silly_name = 'G! {} - Course Average ({}, {})'.format(student, course, marking_period) # don't use get_or_create; otherwise we may end up saving an empty object try: agg = benchmark_get_or_flush(Aggregate, **criteria) created = False except Aggregate.DoesNotExist: agg = Aggregate(**criteria) created = True agg.name = silly_name # begin the actual calculations! agg.cached_substitution = None course_numer = course_denom = Decimal(0) for rule_category in calculation_rule.per_course_category_set.filter(apply_to_departments=course.department): criteria['category'] = rule_category.category cat_agg, cat_created = benchmark_get_create_or_flush(Aggregate, **criteria) if cat_created or recalculate_all_categories or rule_category.category in items_categories: cat_agg, cat_created = benchmark_calculate_course_category_aggregate(student, course, rule_category.category, marking_period, items) if cat_agg.cached_value is not None: course_numer += rule_category.weight * cat_agg.cached_value course_denom += rule_category.weight # yes, agg will just end up with the last substitution, but tough if cat_agg.cached_substitution is not None: agg.cached_substitution = cat_agg.cached_substitution if course_denom: agg.cached_value = course_numer / course_denom else: agg.cached_value = None if save: agg.save() if marking_period is not None: # temporary(?) integration with the rest of sword g, g_created = Grade.objects.get_or_create(student=student, course=course, marking_period=marking_period, override_final=False) if agg.cached_substitution is not None: # FIDDLESTICKS... INC does not fit in the column letter_grade_max_length = Grade._meta.get_field_by_name('letter_grade')[0].max_length g.letter_grade = agg.cached_substitution[:letter_grade_max_length] g.grade = None else: g.set_grade(agg.cached_value) g.save() return agg, created
def benchmark_calculate_course_aggregate(student, course, marking_period, items=None, recalculate_all_categories=False): # doesn't recalculate component aggregates by default if items is None: # QUICK HACK to use new Aggregate calculation method # TODO: Subclass Aggregate and override mark_set for one-off sets of Items agg, created = benchmark_get_create_or_flush( Aggregate, student=student, course=course, marking_period=marking_period, category=None ) agg.calculate(recalculate_all_categories) return agg, created # /HACK (haha, right.) # just leave items alone--we don't actually consider it here; we only pass it to benchmark_calculate_course_category_aggregate # setting items here will prevent benchmark_calculate_course_category_aggregate from saving anything save = True items_categories = () else: # don't store aggregates for every one-off combination of items save = False # we'll have to miss cache and recaculate any category to which an item belongs items_categories = Category.objects.filter(item__in=items).distinct() calculation_rule = benchmark_find_calculation_rule(course.marking_period.all()[0].school_year) # initialize attributes criteria = {"course": course, "category": None, "marking_period": marking_period} # silly name is silly, and should not be part of the criteria silly_name = "G! {} - Course Average ({}, {})".format(student, course, marking_period) # don't use get_or_create; otherwise we may end up saving an empty object try: agg = benchmark_get_or_flush(student.aggregate_set, **criteria) created = False except Aggregate.DoesNotExist: agg = Aggregate(student=student, **criteria) created = True agg.name = silly_name # begin the actual calculations! agg.cached_substitution = None course_numer = course_denom = Decimal(0) for rule_category in calculation_rule.per_course_category_set.filter(apply_to_departments=course.department): criteria["category"] = rule_category.category cat_agg, cat_created = benchmark_get_create_or_flush(student.aggregate_set, **criteria) if cat_created or recalculate_all_categories or rule_category.category in items_categories: cat_agg, cat_created = benchmark_calculate_course_category_aggregate( student, course, rule_category.category, marking_period, items ) if cat_agg.cached_value is not None: course_numer += rule_category.weight * cat_agg.cached_value course_denom += rule_category.weight # yes, agg will just end up with the last substitution, but tough if cat_agg.cached_substitution is not None: agg.cached_substitution = cat_agg.cached_substitution if course_denom: agg.cached_value = course_numer / course_denom else: agg.cached_value = None if save: agg.save() return agg, created
def benchmark_calculate_course_aggregate(student, course_section, marking_period, items=None, recalculate_all_categories=False): # doesn't recalculate component aggregates by default if items is None: # QUICK HACK to use new Aggregate calculation method # TODO: Subclass Aggregate and override mark_set for one-off sets of Items agg, created = benchmark_get_create_or_flush( Aggregate, student=student, course_section=course_section, marking_period=marking_period, category=None) agg.calculate(recalculate_all_categories) return agg, created # /HACK (haha, right.) # just leave items alone--we don't actually consider it here; we only pass it to benchmark_calculate_course_category_aggregate # setting items here will prevent benchmark_calculate_course_category_aggregate from saving anything save = True items_categories = () else: # don't store aggregates for every one-off combination of items save = False # we'll have to miss cache and recaculate any category to which an item belongs items_categories = Category.objects.filter(item__in=items).distinct() calculation_rule = benchmark_find_calculation_rule( course_section.marking_period.all()[0].school_year) # initialize attributes criteria = { 'course_section': course_section, 'category': None, 'marking_period': marking_period } # silly name is silly, and should not be part of the criteria silly_name = 'G! {} - Course Average ({}, {})'.format( student, course_section, marking_period) # don't use get_or_create; otherwise we may end up saving an empty object try: agg = benchmark_get_or_flush(student.aggregate_set, **criteria) created = False except Aggregate.DoesNotExist: agg = Aggregate(student=student, **criteria) created = True agg.name = silly_name # begin the actual calculations! agg.cached_substitution = None course_section_numer = course_section_denom = Decimal(0) for rule_category in calculation_rule.per_course_category_set.filter( apply_to_departments=course_section.department): criteria['category'] = rule_category.category cat_agg, cat_created = benchmark_get_create_or_flush( student.aggregate_set, **criteria) if cat_created or recalculate_all_categories or rule_category.category in items_categories: cat_agg, cat_created = benchmark_calculate_course_category_aggregate( student, course_section, rule_category.category, marking_period, items) if cat_agg.cached_value is not None: course_section_numer += rule_category.weight * cat_agg.cached_value course_section_denom += rule_category.weight # yes, agg will just end up with the last substitution, but tough if cat_agg.cached_substitution is not None: agg.cached_substitution = cat_agg.cached_substitution if course_section_denom: agg.cached_value = course_section_numer / course_section_denom else: agg.cached_value = None if save: agg.save() return agg, created
def benchmark_calculate_course_category_aggregate(student, course_section, category, marking_period, items=None): if items is None: items = Item.objects.all() save = True else: # don't store aggregates for every one-off combination of items save = False items = items.filter(course_section=course_section, category=category) # if we're passed marking_period=None, we should consider items across the entire duration of the course section # if we're passed a specific marking period instead, we should consider items matching only that marking period if marking_period is not None: items = items.filter(marking_period=marking_period) calculation_rule = benchmark_find_calculation_rule( course_section.marking_period.all()[0].school_year) # initialize attributes criteria = { 'course_section': course_section, 'category': category, 'marking_period': marking_period } # silly name is silly, and should not be part of the criteria silly_name = 'G! {} - {} ({}, {})'.format(student, category, course_section, marking_period) # don't use get_or_create; otherwise we may end up saving an empty object try: agg = benchmark_get_or_flush(student.aggregate_set, **criteria) created = False except Aggregate.DoesNotExist: agg = Aggregate(student=student, **criteria) created = True agg.name = silly_name # begin the actual calculations! agg.cached_substitution = None category_numer = category_denom = Decimal(0) if category.allow_multiple_demonstrations: for category_item in items.exclude(points_possible=None): # Find the highest mark amongst demonstrations and count it as the grade for the item best = Mark.objects.filter(student=student, item=category_item).aggregate( Max('mark'))['mark__max'] if best is not None: calculate_as, display_as = calculation_rule.substitute( category_item, best) category_numer += calculate_as category_denom += category_item.points_possible # yes, agg will just end up with the last substitution, but tough if display_as is not None: agg.cached_substitution = display_as else: for category_mark in Mark.objects.filter( student=student, item__in=items).exclude(mark=None).exclude( item__points_possible=None): calculate_as, display_as = calculation_rule.substitute( category_mark.item, category_mark.mark) category_numer += calculate_as category_denom += category_mark.item.points_possible if display_as is not None: agg.cached_substitution = display_as if category_denom: agg.cached_value = category_numer / category_denom * agg._fallback_points_possible( ) else: agg.cached_value = None if save: agg.save() return agg, created
def benchmark_calculate_course_aggregate(student, course, marking_period, items=None, recalculate_all_categories=False): # doesn't recalculate component aggregates by default if items is None: # just leave items alone--we don't actually consider it here; we only pass it to benchmark_calculate_course_category_aggregate # setting items here will prevent benchmark_calculate_course_category_aggregate from saving anything save = True items_categories = () else: # don't store aggregates for every one-off combination of items save = False # we'll have to miss cache and recaculate any category to which an item belongs items_categories = Category.objects.filter(item__in=items).distinct() calculation_rule = benchmark_find_calculation_rule( course.marking_period.all()[0].school_year) # initialize attributes criteria = { 'course': course, 'category': None, 'marking_period': marking_period } # silly name is silly, and should not be part of the criteria silly_name = 'G! {} - Course Average ({}, {})'.format( student, course, marking_period) # don't use get_or_create; otherwise we may end up saving an empty object try: agg = benchmark_get_or_flush(student.aggregate_set, **criteria) created = False except Aggregate.DoesNotExist: agg = Aggregate(student=student, **criteria) created = True agg.name = silly_name # begin the actual calculations! agg.cached_substitution = None course_numer = course_denom = Decimal(0) for rule_category in calculation_rule.per_course_category_set.filter( apply_to_departments=course.department): criteria['category'] = rule_category.category cat_agg, cat_created = benchmark_get_create_or_flush( student.aggregate_set, **criteria) if cat_created or recalculate_all_categories or rule_category.category in items_categories: cat_agg, cat_created = benchmark_calculate_course_category_aggregate( student, course, rule_category.category, marking_period, items) if cat_agg.cached_value is not None: course_numer += rule_category.weight * cat_agg.cached_value course_denom += rule_category.weight # yes, agg will just end up with the last substitution, but tough if cat_agg.cached_substitution is not None: agg.cached_substitution = cat_agg.cached_substitution if course_denom: agg.cached_value = course_numer / course_denom else: agg.cached_value = None if save: agg.save() if marking_period is not None: # temporary(?) integration with the rest of sword g, g_created = Grade.objects.get_or_create( student=student, course=course, marking_period=marking_period, override_final=False) if agg.cached_substitution is not None: # FIDDLESTICKS... INC does not fit in the column letter_grade_max_length = Grade._meta.get_field_by_name( 'letter_grade')[0].max_length g.letter_grade = agg.cached_substitution[: letter_grade_max_length] g.grade = None else: g.set_grade(agg.cached_value) g.save() return agg, created