|
| 1 | +# frozen_string_literal: true |
| 2 | +class Course::Gradebook::Contribution < ApplicationRecord |
| 3 | + # `prefix: true` keeps Rails from generating a bare `equal?` predicate that would |
| 4 | + # override Ruby's Object#equal? (identity, arity 1). Helpers become |
| 5 | + # `weight_mode_equal?` etc.; the `weight_mode` reader still returns 'equal'/'custom'. |
| 6 | + enum :weight_mode, { equal: 0, custom: 1 }, prefix: true |
| 7 | + |
| 8 | + belongs_to :course, inverse_of: :gradebook_contributions |
| 9 | + belongs_to :tab, class_name: 'Course::Assessment::Tab', |
| 10 | + inverse_of: :gradebook_contribution, optional: true |
| 11 | + |
| 12 | + validates :creator, presence: true |
| 13 | + validates :updater, presence: true |
| 14 | + validates :weight, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 100 } |
| 15 | + validates :weight_mode, presence: true |
| 16 | + validates :tab_id, uniqueness: true |
| 17 | + validate :exactly_one_contributor |
| 18 | + validate :course_matches_contributor |
| 19 | + # Bulk-upserts tab contributions and their per-assessment contributions for a course. |
| 20 | + # Consumes the identical `updates` payload the controller parses today. |
| 21 | + # Raises ActiveRecord::RecordNotFound if any tab_id/assessment_id is unknown, and |
| 22 | + # ActiveRecord::RecordInvalid if validation fails or, for custom tabs, the included |
| 23 | + # assessment weights do not sum (at 2dp) to the tab total; the transaction rolls back. |
| 24 | + # |
| 25 | + # @param course [Course] |
| 26 | + # @param updates [Array<Hash>] each { tab_id:, weight:, weight_mode:, |
| 27 | + # excluded_assessment_ids: [Integer], assessment_weights: [{ assessment_id:, weight: }] } |
| 28 | + def self.bulk_update(course:, updates:) |
| 29 | + course_tab_ids = course.assessment_tabs.pluck(:id).to_set |
| 30 | + updates.each { |e| raise ActiveRecord::RecordNotFound unless course_tab_ids.include?(e[:tab_id]) } |
| 31 | + |
| 32 | + tabs_by_id = Course::Assessment::Tab.where(id: updates.map { |e| e[:tab_id] }). |
| 33 | + includes(:assessments).index_by(&:id) |
| 34 | + |
| 35 | + transaction { updates.each { |entry| apply_entry(course, tabs_by_id, entry) } } |
| 36 | + end |
| 37 | + |
| 38 | + # @api private |
| 39 | + def self.apply_entry(course, tabs_by_id, entry) |
| 40 | + tab = tabs_by_id[entry[:tab_id]] |
| 41 | + mode = (entry[:weight_mode] || 'equal').to_s |
| 42 | + |
| 43 | + contribution = find_or_initialize_by(tab_id: tab.id) |
| 44 | + contribution.course = course |
| 45 | + contribution.assign_attributes(weight: entry[:weight], weight_mode: mode) |
| 46 | + contribution.save! |
| 47 | + |
| 48 | + excluded_ids = entry[:excluded_assessment_ids] || [] |
| 49 | + apply_assessment_exclusions(tab, excluded_ids) |
| 50 | + |
| 51 | + if mode == 'custom' |
| 52 | + apply_custom_assessment_weights(tab, entry, excluded_ids.to_set) |
| 53 | + else |
| 54 | + clear_assessment_weights(tab) |
| 55 | + end |
| 56 | + end |
| 57 | + private_class_method :apply_entry |
| 58 | + |
| 59 | + # @api private |
| 60 | + def self.assessment_contribution_for(assessment) |
| 61 | + Course::Gradebook::AssessmentContribution.find_or_initialize_by(assessment_id: assessment.id) |
| 62 | + end |
| 63 | + private_class_method :assessment_contribution_for |
| 64 | + |
| 65 | + # @api private |
| 66 | + # Membership applies in both modes: excluded ids -> true, the rest of the tab -> false. |
| 67 | + def self.apply_assessment_exclusions(tab, excluded_ids) |
| 68 | + excluded_set = excluded_ids.to_set |
| 69 | + tab.assessments.each do |assessment| |
| 70 | + ac = assessment_contribution_for(assessment) |
| 71 | + ac.excluded = excluded_set.include?(assessment.id) |
| 72 | + ac.save! |
| 73 | + end |
| 74 | + end |
| 75 | + private_class_method :apply_assessment_exclusions |
| 76 | + |
| 77 | + # @api private |
| 78 | + def self.clear_assessment_weights(tab) |
| 79 | + tab.assessments.each do |assessment| |
| 80 | + ac = assessment_contribution_for(assessment) |
| 81 | + ac.weight = nil |
| 82 | + ac.save! |
| 83 | + end |
| 84 | + end |
| 85 | + private_class_method :clear_assessment_weights |
| 86 | + |
| 87 | + # @api private |
| 88 | + def self.apply_custom_assessment_weights(tab, entry, excluded_ids) |
| 89 | + assessments_by_id = tab.assessments.index_by(&:id) |
| 90 | + included_sum = 0 |
| 91 | + included_any = false |
| 92 | + (entry[:assessment_weights] || []).each do |aw| |
| 93 | + assessment = assessments_by_id[aw[:assessment_id]] |
| 94 | + raise ActiveRecord::RecordNotFound if assessment.nil? |
| 95 | + |
| 96 | + ac = assessment_contribution_for(assessment) |
| 97 | + ac.weight = aw[:weight] |
| 98 | + ac.save! |
| 99 | + next if excluded_ids.include?(aw[:assessment_id]) |
| 100 | + |
| 101 | + included_sum += aw[:weight] |
| 102 | + included_any = true |
| 103 | + end |
| 104 | + validate_custom_assessment_weights_sum!(tab, entry, included_sum, included_any) |
| 105 | + end |
| 106 | + private_class_method :apply_custom_assessment_weights |
| 107 | + |
| 108 | + def self.validate_custom_assessment_weights_sum!(tab, entry, included_sum, included_any) |
| 109 | + return unless included_any |
| 110 | + return unless (included_sum * 100).round != (entry[:weight] * 100).round |
| 111 | + |
| 112 | + tab.errors.add(:base, :custom_weights_mismatch) |
| 113 | + raise ActiveRecord::RecordInvalid, tab |
| 114 | + end |
| 115 | + |
| 116 | + private |
| 117 | + |
| 118 | + # Until the external-alignment design adds `external_assessment_id`, the only |
| 119 | + # contributor is the tab, so "exactly one" reduces to "tab present". |
| 120 | + def exactly_one_contributor |
| 121 | + errors.add(:tab, :blank) if tab_id.blank? |
| 122 | + end |
| 123 | + |
| 124 | + def course_matches_contributor |
| 125 | + return if tab.nil? || course.nil? |
| 126 | + |
| 127 | + errors.add(:course, :invalid) if tab.category.course_id != course_id |
| 128 | + end |
| 129 | +end |
0 commit comments