Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions app/controllers/components/course/gradebook_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ def self.display_name
end

def sidebar_items
main_sidebar_items + settings_sidebar_items
end

private

def main_sidebar_items
return [] unless can?(:read_gradebook, current_course)

[
Expand All @@ -20,4 +26,17 @@ def sidebar_items
}
]
end

def settings_sidebar_items
return [] unless can?(:manage_gradebook_settings, current_course)

[
{
key: self.class.key,
type: :settings,
weight: 14,
path: course_admin_gradebook_path(current_course)
}
]
end
end
28 changes: 28 additions & 0 deletions app/controllers/course/admin/gradebook_settings_controller.rb
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
53 changes: 52 additions & 1 deletion app/controllers/course/gradebook_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ class Course::GradebookController < Course::ComponentController
def index
respond_to do |format|
format.json do
@weighted_view_enabled = @settings.weighted_view_enabled
@published_assessments = fetch_published_assessments
@categories, @tabs = fetch_categories_and_tabs
@students = fetch_students
assessment_ids = @published_assessments.pluck(:id)
load_weighted_view_contributions(assessment_ids) if @weighted_view_enabled
@assessment_max_grades = Course::Assessment.max_grades(assessment_ids)
@submissions = Course::Assessment::Submission.grade_summary(
student_ids: @students.map(&:user_id),
Expand All @@ -18,12 +20,60 @@ def index
end
end

def update_weights
authorize! :manage_gradebook_weights, current_course
updates = update_weights_params[:weights].map { |entry| parse_weight_entry(entry) }
Course::Gradebook::Contribution.bulk_update(course: current_course, updates: updates)
render json: { weights: serialize_weight_updates(updates) }
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotFound => e
render json: { errors: { base: e.message } }, status: :unprocessable_entity
end

private

def authorize_read_gradebook!
authorize! :read_gradebook, current_course
end

def load_weighted_view_contributions(assessment_ids)
@tab_contributions = Course::Gradebook::Contribution.
where(tab_id: @tabs.map(&:id)).index_by(&:tab_id)
@assessment_contributions = Course::Gradebook::AssessmentContribution.
where(assessment_id: assessment_ids).index_by(&:assessment_id)
end

def parse_weight_entry(entry)
{
tab_id: entry[:tabId].to_i,
weight: entry[:weight].to_f,
weight_mode: entry[:weightMode] || 'equal',
excluded_assessment_ids: (entry[:excludedAssessmentIds] || []).map(&:to_i),
assessment_weights: (entry[:assessmentWeights] || []).map do |aw|
{ assessment_id: aw[:assessmentId].to_i, weight: aw[:weight].to_f }
end
}
end

def update_weights_params
params.permit(
weights: [:tabId, :weight, :weightMode,
excludedAssessmentIds: [], assessmentWeights: [:assessmentId, :weight]]
)
end

def serialize_weight_updates(updates)
updates.map do |u|
entry = { tabId: u[:tab_id], weight: u[:weight], weightMode: u[:weight_mode].to_s,
excludedAssessmentIds: u[:excluded_assessment_ids] }
if u[:weight_mode].to_s == 'custom'
entry[:assessmentWeights] = u[:assessment_weights].map do |aw|
{ assessmentId: aw[:assessment_id], weight: aw[:weight] }
end
end
entry
end
end

def component
current_component_host[:course_gradebook_component]
end
Expand All @@ -36,7 +86,8 @@ def fetch_categories_and_tabs
def fetch_students
current_course.levels.to_a
current_course.course_users.students.without_phantom_users.
calculated(:experience_points).includes(:user).to_a
calculated(:experience_points).includes(:user).to_a.
sort_by { |cu| cu.user.name }
end

def fetch_published_assessments
Expand Down
2 changes: 2 additions & 0 deletions app/models/components/course/gradebook_ability_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module Course::GradebookAbilityComponent

def define_permissions
can :read_gradebook, Course, id: course.id if course_user&.staff?
can :manage_gradebook_weights, Course, id: course.id if course_user&.manager_or_owner?
can :manage_gradebook_settings, Course, id: course.id if course_user&.manager_or_owner?
super
end
end
15 changes: 14 additions & 1 deletion app/models/course.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class Course < ApplicationRecord
dependent: :destroy, inverse_of: :course
has_many :assessment_tabs, source: :tabs, through: :assessment_categories
has_many :assessments, through: :assessment_categories
has_many :gradebook_contributions, class_name: 'Course::Gradebook::Contribution',
dependent: :destroy, inverse_of: :course
has_many :assessment_skills, class_name: 'Course::Assessment::Skill',
dependent: :destroy
has_many :assessment_skill_branches, class_name: 'Course::Assessment::SkillBranch',
Expand Down Expand Up @@ -361,11 +363,22 @@ def nearest_forum_discussions(query_embedding, limit: 3)

# Set default values
def set_defaults
set_default_times
set_default_timeline
build_creator_course_user
end

def set_default_times
self.start_at ||= Time.zone.now.beginning_of_hour
self.end_at ||= self.start_at + 1.month
self.end_at ||= start_at + 1.month
end

def set_default_timeline
self.default_reference_timeline ||= reference_timelines.new(default: true)
self.default_timeline_algorithm ||= 0 # 'fixed' algorithm
end

def build_creator_course_user
return unless creator && course_users.empty?

course_users.build(user: creator,
Expand Down
5 changes: 4 additions & 1 deletion app/models/course/assessment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ class Course::Assessment < ApplicationRecord
inverse_of: :assessment, dependent: :destroy
has_one :plagiarism_check, class_name: 'Course::Assessment::PlagiarismCheck',
inverse_of: :assessment, dependent: :destroy, autosave: true
has_one :gradebook_assessment_contribution,
class_name: 'Course::Gradebook::AssessmentContribution',
dependent: :destroy, inverse_of: :assessment
has_many :live_feedbacks, class_name: 'Course::Assessment::LiveFeedback',
inverse_of: :assessment, dependent: :destroy
has_many :links, class_name: 'Course::Assessment::Link', inverse_of: :assessment, dependent: :destroy
Expand Down Expand Up @@ -211,7 +214,7 @@ def update_randomization(params)
# - The assessment don't have any submissions.
# - Switching from autograded mode to manually graded mode.
def allow_mode_switching?
submissions.count == 0 || autograded?
submissions.none? || autograded?
end

# @override ConditionalInstanceMethods#permitted_for!
Expand Down
31 changes: 21 additions & 10 deletions app/models/course/assessment/tab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class Course::Assessment::Tab < ApplicationRecord

belongs_to :category, class_name: 'Course::Assessment::Category', inverse_of: :tabs
has_many :assessments, class_name: 'Course::Assessment', dependent: :destroy, inverse_of: :tab
has_one :gradebook_contribution, class_name: 'Course::Gradebook::Contribution',
dependent: :destroy, inverse_of: :tab

has_many :folders, class_name: 'Course::Material::Folder', through: :assessments,
inverse_of: nil

Expand All @@ -33,17 +36,25 @@ def other_tabs_remaining?
end

def initialize_duplicate(duplicator, other)
self.category = if duplicator.duplicated?(other.category)
duplicator.duplicate(other.category)
else
duplicator.options[:destination_course].assessment_categories.first
end
assessments <<
other.assessments.select { |assessment| duplicator.duplicated?(assessment) }.map do |assessment|
duplicator.duplicate(assessment).tap do |duplicate_assessment|
duplicate_assessment.folder.parent = category.folder
end
self.category = duplicated_category_for(duplicator, other)

assessments << duplicated_assessments_for(duplicator, other)
end

def duplicated_category_for(duplicator, other)
return duplicator.duplicate(other.category) if duplicator.duplicated?(other.category)

duplicator.options[:destination_course].assessment_categories.first
end

def duplicated_assessments_for(duplicator, other)
other.assessments.filter_map do |assessment|
next unless duplicator.duplicated?(assessment)

duplicator.duplicate(assessment).tap do |duplicate_assessment|
duplicate_assessment.folder.parent = category.folder
end
end
end

private
Expand Down
6 changes: 6 additions & 0 deletions app/models/course/gradebook.rb
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
11 changes: 11 additions & 0 deletions app/models/course/gradebook/assessment_contribution.rb
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
Loading