-
Notifications
You must be signed in to change notification settings - Fork 78
feat(gradebook): weighted view with weights and exclusions #8436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
LWS49
merged 1 commit into
lws49/feat-gradebook-export
from
lws49/feat-gradebook-weighted-view
Jun 29, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
app/controllers/course/admin/gradebook_settings_controller.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # frozen_string_literal: true | ||
| class Course::Admin::GradebookSettingsController < Course::Admin::Controller | ||
| def edit | ||
| respond_to(&:json) | ||
| end | ||
|
|
||
| def update | ||
| if @settings.update(gradebook_settings_params) && current_course.save | ||
| render 'edit' | ||
| else | ||
| render json: { errors: @settings.errors }, status: :bad_request | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def gradebook_settings_params | ||
| params.require(:settings_gradebook_component).permit(:weighted_view_enabled) | ||
| end | ||
|
|
||
| def component | ||
| current_component_host[:course_gradebook_component] | ||
| end | ||
|
|
||
| def authorize_admin | ||
| authorize! :manage_gradebook_settings, current_course | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # frozen_string_literal: true | ||
| module Course::Gradebook | ||
| def self.table_name_prefix | ||
| "#{Course.table_name.singularize}_gradebook_" | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # frozen_string_literal: true | ||
| class Course::Gradebook::AssessmentContribution < ApplicationRecord | ||
| belongs_to :assessment, class_name: 'Course::Assessment', | ||
| inverse_of: :gradebook_assessment_contribution | ||
|
|
||
| validates :creator, presence: true | ||
| validates :updater, presence: true | ||
| validates :weight, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true | ||
| validates :excluded, inclusion: { in: [true, false] } | ||
| validates :assessment_id, uniqueness: true | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| # frozen_string_literal: true | ||
| class Course::Gradebook::TabContribution < ApplicationRecord | ||
| # `prefix: true` keeps Rails from generating a bare `equal?` predicate that would | ||
| # override Ruby's Object#equal? (identity, arity 1). Helpers become | ||
| # `weight_mode_equal?` etc.; the `weight_mode` reader still returns 'equal'/'custom'. | ||
| enum :weight_mode, { equal: 0, custom: 1 }, prefix: true | ||
|
|
||
| belongs_to :course, inverse_of: :gradebook_contributions | ||
| belongs_to :tab, class_name: 'Course::Assessment::Tab', | ||
|
adi-herwana-nus marked this conversation as resolved.
|
||
| inverse_of: :gradebook_contribution | ||
|
|
||
| validates :creator, presence: true | ||
| validates :updater, presence: true | ||
| validates :weight, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 100 } | ||
| validates :weight_mode, presence: true | ||
| validates :tab_id, uniqueness: true | ||
| validate :course_matches_contributor | ||
| # Bulk-upserts tab contributions and their per-assessment contributions for a course. | ||
| # Consumes the identical `updates` payload the controller parses today. | ||
| # Raises ActiveRecord::RecordNotFound if any tab_id/assessment_id is unknown, and | ||
| # ActiveRecord::RecordInvalid if validation fails or, for custom tabs, the included | ||
| # assessment weights do not sum (at 2dp) to the tab total; the transaction rolls back. | ||
| # | ||
| # @param course [Course] | ||
| # @param updates [Array<Hash>] each { tab_id:, weight:, weight_mode:, | ||
| # excluded_assessment_ids: [Integer], assessment_weights: [{ assessment_id:, weight: }] } | ||
| def self.bulk_update(course:, updates:) | ||
| course_tab_ids = course.assessment_tabs.pluck(:id).to_set | ||
| updates.each { |e| raise ActiveRecord::RecordNotFound unless course_tab_ids.include?(e[:tab_id]) } | ||
|
|
||
| tabs_by_id = Course::Assessment::Tab.where(id: updates.map { |e| e[:tab_id] }). | ||
| includes(:assessments).index_by(&:id) | ||
|
|
||
| transaction { updates.each { |entry| apply_entry(course, tabs_by_id, entry) } } | ||
| end | ||
|
|
||
| # @api private | ||
| def self.apply_entry(course, tabs_by_id, entry) | ||
| tab = tabs_by_id[entry[:tab_id]] | ||
| mode = (entry[:weight_mode] || 'equal').to_s | ||
|
|
||
| contribution = find_or_initialize_by(tab_id: tab.id) | ||
| contribution.course = course | ||
| contribution.assign_attributes(weight: entry[:weight], weight_mode: mode) | ||
| contribution.save! | ||
|
|
||
| excluded_ids = entry[:excluded_assessment_ids] || [] | ||
| apply_assessment_exclusions(tab, excluded_ids) | ||
|
|
||
| if mode == 'custom' | ||
| apply_custom_assessment_weights(tab, entry, excluded_ids.to_set) | ||
| else | ||
| clear_assessment_weights(tab) | ||
| end | ||
| end | ||
| private_class_method :apply_entry | ||
|
|
||
| # @api private | ||
| def self.assessment_contribution_for(assessment) | ||
| Course::Gradebook::AssessmentContribution.find_or_initialize_by(assessment_id: assessment.id) | ||
| end | ||
| private_class_method :assessment_contribution_for | ||
|
|
||
| # @api private | ||
| # Membership applies in both modes: excluded ids -> true, the rest of the tab -> false. | ||
| def self.apply_assessment_exclusions(tab, excluded_ids) | ||
| excluded_set = excluded_ids.to_set | ||
| tab.assessments.each do |assessment| | ||
| ac = assessment_contribution_for(assessment) | ||
| ac.excluded = excluded_set.include?(assessment.id) | ||
| ac.save! | ||
| end | ||
| end | ||
| private_class_method :apply_assessment_exclusions | ||
|
|
||
| # @api private | ||
| def self.clear_assessment_weights(tab) | ||
| tab.assessments.each do |assessment| | ||
| ac = assessment_contribution_for(assessment) | ||
| ac.weight = nil | ||
| ac.save! | ||
| end | ||
| end | ||
| private_class_method :clear_assessment_weights | ||
|
|
||
| # @api private | ||
| def self.apply_custom_assessment_weights(tab, entry, excluded_ids) | ||
| assessments_by_id = tab.assessments.index_by(&:id) | ||
| included_sum = 0 | ||
| included_any = false | ||
| (entry[:assessment_weights] || []).each do |aw| | ||
| assessment = assessments_by_id[aw[:assessment_id]] | ||
| raise ActiveRecord::RecordNotFound if assessment.nil? | ||
|
|
||
| ac = assessment_contribution_for(assessment) | ||
| ac.weight = aw[:weight] | ||
| ac.save! | ||
| next if excluded_ids.include?(aw[:assessment_id]) | ||
|
|
||
| included_sum += aw[:weight] | ||
| included_any = true | ||
| end | ||
| validate_custom_assessment_weights_sum!(tab, entry, included_sum, included_any) | ||
| end | ||
| private_class_method :apply_custom_assessment_weights | ||
|
|
||
| def self.validate_custom_assessment_weights_sum!(tab, entry, included_sum, included_any) | ||
| return unless included_any | ||
| return unless (included_sum * 100).round != (entry[:weight] * 100).round | ||
|
|
||
| tab.errors.add(:base, :custom_weights_mismatch) | ||
| raise ActiveRecord::RecordInvalid, tab | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def course_matches_contributor | ||
| return if tab.nil? || course.nil? | ||
|
|
||
| errors.add(:course, :invalid) if tab.category.course_id != course_id | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # frozen_string_literal: true | ||
| class Course::Settings::GradebookComponent < Course::Settings::Component | ||
| # Returns whether weighted view is enabled (disabled by default). | ||
| # | ||
| # @return [Boolean] Setting on whether weighted view is enabled. | ||
| def weighted_view_enabled | ||
| ActiveRecord::Type::Boolean.new.cast(settings.weighted_view_enabled) || false | ||
| end | ||
|
|
||
| # Enable or disable the weighted view. | ||
| # | ||
| # @param [Boolean|Integer|String] value Setting on whether weighted view is enabled. | ||
| def weighted_view_enabled=(value) | ||
| settings.weighted_view_enabled = ActiveRecord::Type::Boolean.new.cast(value) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # frozen_string_literal: true | ||
| json.weightedViewEnabled @settings.weighted_view_enabled |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see you have a separate PR open to restrict gradebook access to manager/owner.
However this is already doing that, does that mean the other PR is redundant now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep, will retire that PR accordingly.