From eda6ef5183175ec8aec5de4c8c8201e756a57a38 Mon Sep 17 00:00:00 2001 From: lws49 Date: Thu, 11 Jun 2026 17:35:03 +0800 Subject: [PATCH 1/3] feat(gradebook): add gradebook with column picker, CSV export, grade links & sorting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Introduce the course gradebook: a frozen-column table of students × assessments with a column picker, CSV export, and per-grade links into submissions. Gradebook table - Page with TanStack-backed table: pinned checkbox + Name columns, sticky header and Max Marks rows, frozen-column border seams that survive sticky scroll compositing. - Column picker (assessments grouped by tab/category) and CSV export of the selected columns; empty-state hint when no data columns are chosen. - External ID column, shown when any student has one. - Grade cells link to the student's submission; a dismissible GradeLinkHint banner explains the affordance (persisted per-user via useDismissibleOnce). - "Search students" global search. - Default sort with name ascending, null/undefined at bottom of sort regardless of order Shared table builder - getColumnCanGlobalFilter is gated on column visibility ("search what you see"): hiding a column via the picker removes it from search, and the nullable-first-row type sniff is bypassed. Affects all TanStack tables. Backend - Gradebook controller, ability and course component; index JSON serializes students, assessments, submissions (with submissionId) and gamification. - Submission grade query also selects the submission id for grade links. - Remove the redundant ScoreAssessmentSummary download from Statistics. --- .circleci/config.yml | 1 + .../components/course/gradebook_component.rb | 22 + .../course/gradebook_controller.rb | 56 ++ .../course/statistics/aggregate_controller.rb | 7 - .../assessments_score_summary_download_job.rb | 17 - .../course/gradebook_ability_component.rb | 9 + app/models/course/assessment.rb | 16 + app/models/course/assessment/submission.rb | 21 + ...essments_score_summary_download_service.rb | 82 -- .../course/courses/sidebar.json.jbuilder | 1 + .../course/gradebook/index.json.jbuilder | 36 + .../aggregate/all_assessments.json.jbuilder | 3 + client/app/api/course/Gradebook.ts | 15 + .../api/course/Statistics/CourseStatistics.ts | 8 - client/app/api/course/index.js | 2 + .../bundles/course/container/CourseLoader.ts | 8 + .../container/__tests__/CourseLoader.test.ts | 46 + .../__tests__/GradeLinkHint.test.tsx | 53 ++ .../__tests__/GradebookColumnTree.test.tsx | 305 +++++++ .../__tests__/GradebookIndex.test.tsx | 149 ++++ .../__tests__/GradebookTable.test.tsx | 793 +++++++++++++++++ .../gradebook/components/GradeLinkHint.tsx | 47 + .../components/GradebookColumnTree.tsx | 219 +++++ .../gradebook/components/GradebookTable.tsx | 808 ++++++++++++++++++ .../components/buildAssessmentColumnIds.ts | 7 + .../app/bundles/course/gradebook/constants.ts | 5 + .../app/bundles/course/gradebook/handles.ts | 21 + .../bundles/course/gradebook/operations.ts | 12 + .../gradebook/pages/GradebookIndex/index.tsx | 106 +++ .../app/bundles/course/gradebook/selectors.ts | 24 + client/app/bundles/course/gradebook/store.ts | 63 ++ client/app/bundles/course/gradebook/types.ts | 8 + .../bundles/course/statistics/operations.ts | 24 - .../AssessmentsScoreSummaryDownload.tsx | 115 --- .../assessments/AssessmentsStatistics.tsx | 1 + .../AssessmentsStatisticsTable.tsx | 53 +- .../AssessmentsStatisticsTable.test.tsx | 58 ++ client/app/bundles/course/statistics/types.ts | 1 + client/app/bundles/course/translations.ts | 4 + .../tables/InvitationResultExistingTable.tsx | 1 - .../tables/InvitationResultPrimaryTable.tsx | 1 - client/app/bundles/users/store.ts | 13 + client/app/bundles/users/types.ts | 7 + .../core/buttons/DownloadButton.tsx | 2 +- .../lib/components/core/dialogs/Prompt.tsx | 3 + .../MuiTableAdapter/MuiColumnPickerPrompt.tsx | 12 +- .../useTanStackTableBuilder.tsx | 104 ++- .../table/__tests__/csvGenerator.test.ts | 22 +- .../useTanStackTableBuilder.test.tsx | 241 +++++- .../components/table/__tests__/utils.test.ts | 21 + .../lib/components/table/adapters/Toolbar.ts | 2 +- .../table/builder/featureTemplates.ts | 3 + client/app/lib/constants/icons.ts | 3 + client/app/lib/helpers/url-builders.js | 3 + .../__tests__/useDismissibleOnce.test.tsx | 62 ++ client/app/lib/hooks/useDismissibleOnce.ts | 48 ++ client/app/lib/translations/table.ts | 4 + client/app/routers/course/gradebook.tsx | 23 + client/app/routers/course/index.tsx | 2 + client/app/store.ts | 2 + client/app/types/course/courses.ts | 1 + client/app/types/course/gradebook.ts | 42 + client/locales/en.json | 77 +- client/locales/ko.json | 72 +- client/locales/zh.json | 76 +- config/locales/en/csv.yml | 8 - config/locales/ko/csv.yml | 8 - config/locales/zh/csv.yml | 8 - config/routes.rb | 5 +- .../coursemology/seed_600_gradebook.rake | 242 ++++++ lib/tasks/coursemology/seed_gradebook.rake | 249 ++++++ .../course/gradebook_controller_spec.rb | 204 +++++ .../statistics/aggregate_controller_spec.rb | 23 + .../course/assessment/submission_spec.rb | 66 ++ spec/models/course/assessment_spec.rb | 29 + ...ent_score_summary_download_service_spec.rb | 162 ---- 76 files changed, 4564 insertions(+), 513 deletions(-) create mode 100644 app/controllers/components/course/gradebook_component.rb create mode 100644 app/controllers/course/gradebook_controller.rb delete mode 100644 app/jobs/course/statistics/assessments_score_summary_download_job.rb create mode 100644 app/models/components/course/gradebook_ability_component.rb delete mode 100644 app/services/course/statistics/assessments_score_summary_download_service.rb create mode 100644 app/views/course/gradebook/index.json.jbuilder create mode 100644 client/app/api/course/Gradebook.ts create mode 100644 client/app/bundles/course/container/__tests__/CourseLoader.test.ts create mode 100644 client/app/bundles/course/gradebook/__tests__/GradeLinkHint.test.tsx create mode 100644 client/app/bundles/course/gradebook/__tests__/GradebookColumnTree.test.tsx create mode 100644 client/app/bundles/course/gradebook/__tests__/GradebookIndex.test.tsx create mode 100644 client/app/bundles/course/gradebook/__tests__/GradebookTable.test.tsx create mode 100644 client/app/bundles/course/gradebook/components/GradeLinkHint.tsx create mode 100644 client/app/bundles/course/gradebook/components/GradebookColumnTree.tsx create mode 100644 client/app/bundles/course/gradebook/components/GradebookTable.tsx create mode 100644 client/app/bundles/course/gradebook/components/buildAssessmentColumnIds.ts create mode 100644 client/app/bundles/course/gradebook/constants.ts create mode 100644 client/app/bundles/course/gradebook/handles.ts create mode 100644 client/app/bundles/course/gradebook/operations.ts create mode 100644 client/app/bundles/course/gradebook/pages/GradebookIndex/index.tsx create mode 100644 client/app/bundles/course/gradebook/selectors.ts create mode 100644 client/app/bundles/course/gradebook/store.ts create mode 100644 client/app/bundles/course/gradebook/types.ts delete mode 100644 client/app/bundles/course/statistics/pages/StatisticsIndex/assessments/AssessmentsScoreSummaryDownload.tsx create mode 100644 client/app/bundles/course/statistics/pages/StatisticsIndex/assessments/__tests__/AssessmentsStatisticsTable.test.tsx create mode 100644 client/app/lib/components/table/__tests__/utils.test.ts create mode 100644 client/app/lib/hooks/__tests__/useDismissibleOnce.test.tsx create mode 100644 client/app/lib/hooks/useDismissibleOnce.ts create mode 100644 client/app/routers/course/gradebook.tsx create mode 100644 client/app/types/course/gradebook.ts create mode 100644 lib/tasks/coursemology/seed_600_gradebook.rake create mode 100644 lib/tasks/coursemology/seed_gradebook.rake create mode 100644 spec/controllers/course/gradebook_controller_spec.rb delete mode 100644 spec/services/course/statistics/assessment_score_summary_download_service_spec.rb diff --git a/.circleci/config.yml b/.circleci/config.yml index 86bc10bc760..f73f1357250 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -119,6 +119,7 @@ commands: command: yarn build:test environment: AVAILABLE_CPUS: 4 + NODE_OPTIONS: --max-old-space-size=4096 - save_cache: paths: diff --git a/app/controllers/components/course/gradebook_component.rb b/app/controllers/components/course/gradebook_component.rb new file mode 100644 index 00000000000..774a8fa36e1 --- /dev/null +++ b/app/controllers/components/course/gradebook_component.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true +class Course::GradebookComponent < SimpleDelegator + include Course::ControllerComponentHost::Component + + def self.display_name + 'Gradebook' + end + + def sidebar_items + return [] unless can?(:read_gradebook, current_course) + + [ + { + key: self.class.key, + icon: :gradebook, + type: :normal, + weight: 9, + path: course_gradebook_path(current_course) + } + ] + end +end diff --git a/app/controllers/course/gradebook_controller.rb b/app/controllers/course/gradebook_controller.rb new file mode 100644 index 00000000000..4201a2f5f2c --- /dev/null +++ b/app/controllers/course/gradebook_controller.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true +class Course::GradebookController < Course::ComponentController + before_action :authorize_read_gradebook! + before_action :preload_levels, only: [:index] + + def index + respond_to do |format| + format.json do + @published_assessments = fetch_published_assessments + @categories, @tabs = fetch_categories_and_tabs + @students = fetch_students + assessment_ids = @published_assessments.pluck(:id) + @assessment_max_grades = Course::Assessment.max_grades(assessment_ids) + @submissions = Course::Assessment::Submission.grade_summary( + student_ids: @students.map(&:user_id), + assessment_ids: assessment_ids + ) + end + end + end + + private + + def authorize_read_gradebook! + authorize! :read_gradebook, current_course + end + + def component + current_component_host[:course_gradebook_component] + end + + def fetch_categories_and_tabs + tabs = @published_assessments.map(&:tab).uniq(&:id) + [tabs.map(&:category).uniq(&:id), tabs] + end + + def fetch_students + current_course.course_users.students.without_phantom_users. + calculated(:experience_points).includes(user: :emails).to_a + end + + def fetch_published_assessments + current_course.assessments. + published. + includes(tab: :category). + joins(tab: :category). + reorder('course_assessment_categories.weight, course_assessment_tabs.weight, course_assessments.id') + end + + # Pre-loads course levels to avoid N+1 queries when each course_user.level_number is rendered. + # level_number is derived, not an association (it buckets EXP against course.levels), so it + # can't be added to fetch_students' includes. See Course::LevelsConcern#level_for. + def preload_levels + current_course.levels.to_a + end +end diff --git a/app/controllers/course/statistics/aggregate_controller.rb b/app/controllers/course/statistics/aggregate_controller.rb index 2e44dc06f81..306984f30e6 100644 --- a/app/controllers/course/statistics/aggregate_controller.rb +++ b/app/controllers/course/statistics/aggregate_controller.rb @@ -47,13 +47,6 @@ def activity_get_help @course_user_hash = current_course.course_users.index_by(&:user_id) end - def download_score_summary - job = Course::Statistics::AssessmentsScoreSummaryDownloadJob. - perform_later(current_course, params[:assessment_ids]).job - - render partial: 'jobs/submitted', locals: { job: job } - end - private def sanitize_date_range(start_at_param, end_at_param) diff --git a/app/jobs/course/statistics/assessments_score_summary_download_job.rb b/app/jobs/course/statistics/assessments_score_summary_download_job.rb deleted file mode 100644 index 86ce040b994..00000000000 --- a/app/jobs/course/statistics/assessments_score_summary_download_job.rb +++ /dev/null @@ -1,17 +0,0 @@ -# frozen_string_literal: true -class Course::Statistics::AssessmentsScoreSummaryDownloadJob < ApplicationJob - include TrackableJob - queue_as :lowest - retry_on StandardError, attempts: 0 - - protected - - def perform_tracked(course, assessment_ids) - file_name = "#{Pathname.normalize_filename(course.title)}_score_summary_#{Time.now.strftime '%Y%m%d_%H%M'}.csv" - service = Course::Statistics::AssessmentsScoreSummaryDownloadService.new(course, assessment_ids, file_name) - csv_file = service.generate - redirect_to SendFile.send_file(csv_file, file_name) - ensure - service&.cleanup - end -end diff --git a/app/models/components/course/gradebook_ability_component.rb b/app/models/components/course/gradebook_ability_component.rb new file mode 100644 index 00000000000..d5b9862f299 --- /dev/null +++ b/app/models/components/course/gradebook_ability_component.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true +module Course::GradebookAbilityComponent + include AbilityHost::Component + + def define_permissions + can :read_gradebook, Course, id: course.id if course_user&.staff? + super + end +end diff --git a/app/models/course/assessment.rb b/app/models/course/assessment.rb index 3128ed42528..aec93f9de08 100644 --- a/app/models/course/assessment.rb +++ b/app/models/course/assessment.rb @@ -160,6 +160,22 @@ def self.use_relative_model_naming? true end + # Returns a hash of assessment_id => max_grade (sum of question maximum_grades). + def self.max_grades(assessment_ids) + return {} if assessment_ids.empty? + + rows = find_by_sql( + sanitize_sql_array([<<-SQL.squish, assessment_ids]) + SELECT cqa.assessment_id, COALESCE(SUM(caq.maximum_grade), 0) AS max_grade + FROM course_question_assessments cqa + JOIN course_assessment_questions caq ON caq.id = cqa.question_id + WHERE cqa.assessment_id IN (?) + GROUP BY cqa.assessment_id + SQL + ) + rows.to_h { |row| [row.assessment_id, row.max_grade.to_f] } + end + def to_partial_path 'course/assessment/assessments/assessment' end diff --git a/app/models/course/assessment/submission.rb b/app/models/course/assessment/submission.rb index c4919d6ec14..cb5df4c80f6 100644 --- a/app/models/course/assessment/submission.rb +++ b/app/models/course/assessment/submission.rb @@ -323,6 +323,27 @@ def self.on_dependent_status_change(answer) answer.submission.last_graded_time = Time.now end + # Returns an array of submission rows for the given students and assessments. + # Each row has: student_id (creator_id), assessment_id, grade (float). + # Only graded/published submissions are included. + def self.grade_summary(student_ids:, assessment_ids:) + return [] if student_ids.empty? || assessment_ids.empty? + + find_by_sql( + sanitize_sql_array([<<-SQL.squish, student_ids, assessment_ids]) + SELECT cas.creator_id AS student_id, cas.assessment_id, + cas.id AS submission_id, SUM(caa.grade) AS grade + FROM course_assessment_submissions cas + JOIN course_assessment_answers caa ON caa.submission_id = cas.id + WHERE cas.creator_id IN (?) + AND cas.assessment_id IN (?) + AND cas.workflow_state IN ('graded', 'published') + AND caa.current_answer = TRUE + GROUP BY cas.creator_id, cas.assessment_id, cas.id + SQL + ) + end + private # Queues the submission for auto grading, after the submission has changed to the submitted state. diff --git a/app/services/course/statistics/assessments_score_summary_download_service.rb b/app/services/course/statistics/assessments_score_summary_download_service.rb deleted file mode 100644 index 88e98756c6a..00000000000 --- a/app/services/course/statistics/assessments_score_summary_download_service.rb +++ /dev/null @@ -1,82 +0,0 @@ -# frozen_string_literal: true -require 'csv' -class Course::Statistics::AssessmentsScoreSummaryDownloadService - include TmpCleanupHelper - include ApplicationFormattersHelper - - def initialize(course, assessment_ids, file_name) - @course = course - @assessment_ids = assessment_ids - @file_name = file_name - @base_dir = Dir.mktmpdir('assessment-score-summary-') - end - - def generate - ActsAsTenant.without_tenant do - generate_csv_report - end - end - - def generate_csv_report - assessment_score_summary_file_path = File.join(@base_dir, @file_name) - - load_total_grades - CSV.open(assessment_score_summary_file_path, 'w') do |csv| - download_score_summary(csv) - end - - assessment_score_summary_file_path - end - - private - - def cleanup_entries - [@base_dir] - end - - def load_total_grades - @course_assessment_hash = Course::Assessment.where(id: @assessment_ids, course_id: @course.id).to_h do |assessment| - [assessment.id, assessment] - end - - @assessments = assessments - @submissions = Course::Assessment::Submission.where(assessment_id: @assessments.map(&:id)). - calculated(:grade). - preload(creator: :course_users) - - @submission_grade_hash = submission_grade_hash - @all_students = @course.course_users.students.order_alphabetically.preload(user: :emails) - @include_external_id = @course.course_users.students.where.not(external_id: [nil, '']).exists? - end - - def submission_grade_hash - @submissions.to_h do |submission| - course_user = submission.creator.course_users.find { |u| u.course_id == @course.id } - [[course_user.id, submission.assessment_id], submission.grade] - end - end - - def assessments - @assessment_ids.filter { |assessment_id| !@course_assessment_hash[assessment_id.to_i].nil? }.map do |assessment_id| - @course_assessment_hash[assessment_id.to_i] - end - end - - def download_score_summary(csv) - # header - csv << [ - I18n.t('csv.score_summary.headers.name'), - I18n.t('csv.score_summary.headers.email'), - I18n.t('csv.score_summary.headers.type'), - *(@include_external_id ? [I18n.t('csv.score_summary.headers.external_id')] : []), - *@assessments.map(&:title) - ] - - # content - @all_students.each do |student| - csv << [student.name, student.user.email, student.phantom? ? 'phantom' : 'normal', - *(@include_external_id ? [student.external_id.presence || ''] : []), - *@assessments.flat_map { |a| @submission_grade_hash[[student.id, a.id]] || '' }] - end - end -end diff --git a/app/views/course/courses/sidebar.json.jbuilder b/app/views/course/courses/sidebar.json.jbuilder index d7de1bebca1..1a31ba3f24f 100644 --- a/app/views/course/courses/sidebar.json.jbuilder +++ b/app/views/course/courses/sidebar.json.jbuilder @@ -4,6 +4,7 @@ json.courseUrl course_path(current_course) json.courseLogoUrl url_to_course_logo(current_course) json.courseUserUrl url_to_user_or_course_user(current_course, current_course_user) json.userName current_user&.name +json.userId current_user&.id if current_course_user.present? && can?(:read, current_course) json.courseUserName current_course_user.name diff --git a/app/views/course/gradebook/index.json.jbuilder b/app/views/course/gradebook/index.json.jbuilder new file mode 100644 index 00000000000..d2180773a40 --- /dev/null +++ b/app/views/course/gradebook/index.json.jbuilder @@ -0,0 +1,36 @@ +# frozen_string_literal: true +json.categories @categories do |cat| + json.id cat.id + json.title cat.title +end + +json.tabs @tabs do |tab| + json.id tab.id + json.title tab.title + json.categoryId tab.category_id +end + +json.assessments @published_assessments do |assessment| + json.id assessment.id + json.title assessment.title + json.tabId assessment.tab_id + json.maxGrade @assessment_max_grades[assessment.id] || 0 +end + +json.students @students do |course_user| + json.id course_user.user_id + json.name course_user.name + json.email course_user.user.email + json.externalId course_user.external_id + json.level course_user.level_number + json.totalXp course_user.experience_points +end + +json.submissions @submissions do |sub| + json.studentId sub.student_id + json.assessmentId sub.assessment_id + json.submissionId sub.submission_id + json.grade sub.grade&.to_f +end + +json.gamificationEnabled current_course.gamified? diff --git a/app/views/course/statistics/aggregate/all_assessments.json.jbuilder b/app/views/course/statistics/aggregate/all_assessments.json.jbuilder index 458efa1067c..7bc08582f3d 100644 --- a/app/views/course/statistics/aggregate/all_assessments.json.jbuilder +++ b/app/views/course/statistics/aggregate/all_assessments.json.jbuilder @@ -35,3 +35,6 @@ json.assessments @assessments do |assessment| json.numAttempted @num_attempted_students_hash[assessment.id] || 0 json.numLate @num_late_students_hash[assessment.id] || 0 end + +json.gradebookEnabled current_course.component_enabled?(Course::GradebookComponent) && + can?(:read_gradebook, current_course) diff --git a/client/app/api/course/Gradebook.ts b/client/app/api/course/Gradebook.ts new file mode 100644 index 00000000000..e00c94a64c3 --- /dev/null +++ b/client/app/api/course/Gradebook.ts @@ -0,0 +1,15 @@ +import { GradebookData } from 'types/course/gradebook'; + +import { APIResponse } from 'api/types'; + +import BaseCourseAPI from './Base'; + +export default class GradebookAPI extends BaseCourseAPI { + get #urlPrefix(): string { + return `/courses/${this.courseId}/gradebook`; + } + + index(): APIResponse { + return this.client.get(this.#urlPrefix); + } +} diff --git a/client/app/api/course/Statistics/CourseStatistics.ts b/client/app/api/course/Statistics/CourseStatistics.ts index 6e2d3e370f4..b478bec85df 100644 --- a/client/app/api/course/Statistics/CourseStatistics.ts +++ b/client/app/api/course/Statistics/CourseStatistics.ts @@ -1,5 +1,3 @@ -import { JobSubmitted } from 'types/jobs'; - import { APIResponse } from 'api/types'; import { AssessmentsStatistics, @@ -53,10 +51,4 @@ export default class CourseStatisticsAPI extends BaseCourseAPI { params, }); } - - downloadScoreSummary(assessmentIds: number[]): APIResponse { - return this.client.get(`${this.#urlPrefix}/assessments/download`, { - params: { assessment_ids: assessmentIds }, - }); - } } diff --git a/client/app/api/course/index.js b/client/app/api/course/index.js index 8f5df6176fe..355a5878c53 100644 --- a/client/app/api/course/index.js +++ b/client/app/api/course/index.js @@ -12,6 +12,7 @@ import DuplicationAPI from './Duplication'; import EnrolRequestsAPI from './EnrolRequests'; import ExperiencePointsRecordAPI from './ExperiencePointsRecord'; import ForumAPI from './Forum'; +import GradebookAPI from './Gradebook'; import GroupsAPI from './Groups'; import LeaderboardAPI from './Leaderboard'; import LearningMapAPI from './LearningMap'; @@ -48,6 +49,7 @@ const CourseAPI = { experiencePointsRecord: new ExperiencePointsRecordAPI(), folders: new FoldersAPI(), forum: ForumAPI, + gradebook: new GradebookAPI(), groups: new GroupsAPI(), leaderboard: new LeaderboardAPI(), learningMap: new LearningMapAPI(), diff --git a/client/app/bundles/course/container/CourseLoader.ts b/client/app/bundles/course/container/CourseLoader.ts index b55a5866b8f..8abe2df1ec1 100644 --- a/client/app/bundles/course/container/CourseLoader.ts +++ b/client/app/bundles/course/container/CourseLoader.ts @@ -3,9 +3,11 @@ import { useLoaderData, useOutletContext, } from 'react-router-dom'; +import { dispatch as imperativeDispatch } from 'store'; import { CourseLayoutData, SidebarItemData } from 'types/course/courses'; import CourseAPI from 'api/course'; +import { actions as userActions } from 'bundles/users/store'; import { syncSignals } from 'lib/hooks/unread'; const extractUnreadCountsInto = ( @@ -34,6 +36,12 @@ export const loader: LoaderFunction = async ({ params }) => { const response = await CourseAPI.courses.fetchLayout(id); + // Hydrate the authenticated user's id into the global store. It is otherwise + // only populated on the user profile page, so any course page relying on it + // (e.g. per-user localStorage namespacing for table column prefs) would read 0. + const { userId } = response.data; + if (userId != null) imperativeDispatch(userActions.setCurrentUserId(userId)); + syncSignals(extractUnreadCountsFromLayoutData(response.data)); return response.data; diff --git a/client/app/bundles/course/container/__tests__/CourseLoader.test.ts b/client/app/bundles/course/container/__tests__/CourseLoader.test.ts new file mode 100644 index 00000000000..3db67b15dce --- /dev/null +++ b/client/app/bundles/course/container/__tests__/CourseLoader.test.ts @@ -0,0 +1,46 @@ +import { store } from 'store'; + +import CourseAPI from 'api/course'; + +import { loader } from '../CourseLoader'; + +jest.mock('api/course', () => ({ + __esModule: true, + default: { courses: { fetchLayout: jest.fn() } }, +})); + +const mockFetchLayout = CourseAPI.courses.fetchLayout as jest.Mock; + +const runLoader = (courseId: string): Promise => + // The router passes { request, params }; only params.courseId is read here, so + // we route through `unknown` to call it with just the params it reads. + ( + loader as unknown as (args: { + params: { courseId: string }; + }) => Promise + )({ + params: { courseId }, + }); + +describe('CourseLoader loader', () => { + it('hydrates the authenticated user id into the global store', async () => { + mockFetchLayout.mockResolvedValueOnce({ + data: { courseTitle: 'C', userName: 'Alice', userId: 77 }, + }); + + await runLoader('1'); + + expect(store.getState().global.user.user.id).toBe(77); + }); + + it('leaves the global user id untouched when the payload has no user id', async () => { + store.dispatch({ type: 'system/SET_CURRENT_USER_ID', userId: 5 }); + mockFetchLayout.mockResolvedValueOnce({ + data: { courseTitle: 'C', userName: null, userId: null }, + }); + + await runLoader('1'); + + expect(store.getState().global.user.user.id).toBe(5); + }); +}); diff --git a/client/app/bundles/course/gradebook/__tests__/GradeLinkHint.test.tsx b/client/app/bundles/course/gradebook/__tests__/GradeLinkHint.test.tsx new file mode 100644 index 00000000000..2ad267268aa --- /dev/null +++ b/client/app/bundles/course/gradebook/__tests__/GradeLinkHint.test.tsx @@ -0,0 +1,53 @@ +import { store as appStore } from 'store'; +import { fireEvent, render, screen, waitFor } from 'test-utils'; + +import GradeLinkHint, { + GRADE_LINK_HINT_KEY, +} from '../components/GradeLinkHint'; + +const USER_ID = 42; +const STORAGE_KEY = `${USER_ID}:${GRADE_LINK_HINT_KEY}`; + +// The dismissal flag is namespaced by the authenticated user id from the global +// user store, which the course layout loader hydrates on every course page. +const userState = { + global: { + ...appStore.getState().global, + user: { + ...appStore.getState().global.user, + user: { id: USER_ID, name: '', imageUrl: '' }, + }, + }, +}; + +const renderHint = (): void => { + render(, { state: userState }); +}; + +beforeEach(() => localStorage.clear()); + +describe('GradeLinkHint', () => { + it('explains that grades are clickable and lead to the submission', async () => { + renderHint(); + expect(await screen.findByText(/click any grade/i)).toBeInTheDocument(); + }); + + it('hides and persists dismissal when the close button is clicked', async () => { + renderHint(); + fireEvent.click(await screen.findByRole('button', { name: /close/i })); + + expect(screen.queryByText(/click any grade/i)).not.toBeInTheDocument(); + expect(localStorage.getItem(STORAGE_KEY)).toBe('true'); + }); + + it('does not render when already dismissed', async () => { + localStorage.setItem(STORAGE_KEY, 'true'); + renderHint(); + // Wait for the async locale load to settle (spinner gone) before asserting + // absence, so we are not just observing the pre-load loading state. + await waitFor(() => + expect(screen.queryByRole('progressbar')).not.toBeInTheDocument(), + ); + expect(screen.queryByText(/click any grade/i)).not.toBeInTheDocument(); + }); +}); diff --git a/client/app/bundles/course/gradebook/__tests__/GradebookColumnTree.test.tsx b/client/app/bundles/course/gradebook/__tests__/GradebookColumnTree.test.tsx new file mode 100644 index 00000000000..280f3c71b7d --- /dev/null +++ b/client/app/bundles/course/gradebook/__tests__/GradebookColumnTree.test.tsx @@ -0,0 +1,305 @@ +import { IntlProvider } from 'react-intl'; +import { fireEvent, render, screen } from '@testing-library/react'; + +import { buildAssessmentColumnId } from '../components/buildAssessmentColumnIds'; +import GradebookColumnTree from '../components/GradebookColumnTree'; +import type { AssessmentData, CategoryData, TabData } from '../types'; + +const categories: CategoryData[] = [{ id: 1, title: 'Cat A' }]; +const tabs: TabData[] = [{ id: 10, title: 'Tab 1', categoryId: 1 }]; +const assessments: AssessmentData[] = [ + { id: 100, title: 'Quiz 1', tabId: 10, maxGrade: 10 }, + { id: 101, title: 'Quiz 2', tabId: 10, maxGrade: 10 }, +]; + +const asnId100 = buildAssessmentColumnId(100); +const asnId101 = buildAssessmentColumnId(101); +const allIds = ['name', 'email', 'externalId', 'level', asnId100, asnId101]; + +const wrap = (node: JSX.Element): JSX.Element => ( + + {node} + +); + +describe('GradebookColumnTree', () => { + it('renders Student info and Grades branch labels', () => { + const visibility = Object.fromEntries(allIds.map((id) => [id, true])); + render( + wrap( + visibility[id] ?? true} + setManyVisible={jest.fn()} + setVisible={jest.fn()} + tabs={tabs} + />, + ), + ); + expect(screen.getByText('Student info')).toBeInTheDocument(); + expect(screen.getByText('Grades')).toBeInTheDocument(); + }); + + it('renders Gamification branch when gamificationEnabled', () => { + const visibility = Object.fromEntries(allIds.map((id) => [id, true])); + render( + wrap( + visibility[id] ?? true} + setManyVisible={jest.fn()} + setVisible={jest.fn()} + tabs={tabs} + />, + ), + ); + expect(screen.getByText('Gamification')).toBeInTheDocument(); + expect( + screen.getByRole('checkbox', { name: /^level$/i }), + ).toBeInTheDocument(); + expect( + screen.getByRole('checkbox', { name: /^total xp$/i }), + ).toBeInTheDocument(); + }); + + it('hides Gamification branch when gamificationEnabled is false', () => { + const visibility = Object.fromEntries(allIds.map((id) => [id, true])); + render( + wrap( + visibility[id] ?? true} + setManyVisible={jest.fn()} + setVisible={jest.fn()} + tabs={tabs} + />, + ), + ); + expect(screen.queryByText('Gamification')).not.toBeInTheDocument(); + expect( + screen.queryByRole('checkbox', { name: /^level$/i }), + ).not.toBeInTheDocument(); + }); + + it('renders an External ID checkbox in the Student info group', () => { + const visibility = Object.fromEntries(allIds.map((id) => [id, true])); + render( + wrap( + visibility[id] ?? true} + setManyVisible={jest.fn()} + setVisible={jest.fn()} + tabs={tabs} + />, + ), + ); + expect( + screen.getByRole('checkbox', { name: /external id/i }), + ).toBeInTheDocument(); + }); + + it('clicking the External ID checkbox calls setVisible with its column id', () => { + const setVisible = jest.fn(); + const visibility = Object.fromEntries(allIds.map((id) => [id, true])); + render( + wrap( + visibility[id] ?? true} + setManyVisible={jest.fn()} + setVisible={setVisible} + tabs={tabs} + />, + ), + ); + fireEvent.click(screen.getByRole('checkbox', { name: /external id/i })); + expect(setVisible).toHaveBeenCalledWith('externalId', expect.any(Boolean)); + }); + + it('name checkbox is disabled and always checked', () => { + const visibility: Record = { + name: false, + email: true, + [asnId100]: true, + [asnId101]: true, + }; + render( + wrap( + visibility[id] ?? true} + setManyVisible={jest.fn()} + setVisible={jest.fn()} + tabs={tabs} + />, + ), + ); + const nameCheckbox = screen.getByRole('checkbox', { name: /^name/i }); + expect(nameCheckbox).toBeDisabled(); + expect(nameCheckbox).toBeChecked(); + }); + + it('non-name student info checkboxes are enabled and reflect visibility state', () => { + const visibility: Record = { + name: true, + email: false, + [asnId100]: true, + [asnId101]: true, + }; + render( + wrap( + visibility[id] ?? true} + setManyVisible={jest.fn()} + setVisible={jest.fn()} + tabs={tabs} + />, + ), + ); + const emailCheckbox = screen.getByRole('checkbox', { name: /^email$/i }); + expect(emailCheckbox).not.toBeDisabled(); + expect(emailCheckbox).not.toBeChecked(); + }); + + it('clicking a student info checkbox calls setVisible with its column id', () => { + const setVisible = jest.fn(); + const visibility = Object.fromEntries(allIds.map((id) => [id, true])); + render( + wrap( + visibility[id] ?? true} + setManyVisible={jest.fn()} + setVisible={setVisible} + tabs={tabs} + />, + ), + ); + fireEvent.click(screen.getByRole('checkbox', { name: /^email$/i })); + expect(setVisible).toHaveBeenCalledWith('email', expect.any(Boolean)); + }); + + it('renders Category, Tab, and assessment checkboxes', () => { + const visibility = Object.fromEntries(allIds.map((id) => [id, true])); + render( + wrap( + visibility[id] ?? true} + setManyVisible={jest.fn()} + setVisible={jest.fn()} + tabs={tabs} + />, + ), + ); + expect(screen.getByText('Cat A')).toBeInTheDocument(); + expect(screen.getByText('Tab 1')).toBeInTheDocument(); + expect( + screen.getByRole('checkbox', { name: /quiz 1/i }), + ).toBeInTheDocument(); + expect( + screen.getByRole('checkbox', { name: /quiz 2/i }), + ).toBeInTheDocument(); + }); + + it('clicking an assessment checkbox calls setVisible with the single column id', () => { + const setVisible = jest.fn(); + const visibility = Object.fromEntries(allIds.map((id) => [id, true])); + render( + wrap( + visibility[id] ?? true} + setManyVisible={jest.fn()} + setVisible={setVisible} + tabs={tabs} + />, + ), + ); + fireEvent.click(screen.getByRole('checkbox', { name: /quiz 1/i })); + expect(setVisible).toHaveBeenCalledWith(asnId100, expect.any(Boolean)); + }); + + it('renders "Always included" chip next to the Name row', () => { + const visibility = Object.fromEntries(allIds.map((id) => [id, true])); + render( + wrap( + visibility[id] ?? true} + setManyVisible={jest.fn()} + setVisible={jest.fn()} + tabs={tabs} + />, + ), + ); + expect(screen.getByText('Always included')).toBeInTheDocument(); + }); + + it('does not render "Always included" chip next to email row', () => { + const visibility = Object.fromEntries(allIds.map((id) => [id, true])); + render( + wrap( + visibility[id] ?? true} + setManyVisible={jest.fn()} + setVisible={jest.fn()} + tabs={tabs} + />, + ), + ); + expect(screen.getAllByText('Always included')).toHaveLength(1); + }); + + it('Student info branch is indeterminate when some but not all student cols are visible', () => { + const visibility: Record = { + name: true, + email: false, + [asnId100]: true, + [asnId101]: true, + }; + render( + wrap( + visibility[id] ?? true} + setManyVisible={jest.fn()} + setVisible={jest.fn()} + tabs={tabs} + />, + ), + ); + expect( + screen.getByRole('checkbox', { name: /student info/i }), + ).toHaveAttribute('data-indeterminate', 'true'); + }); +}); diff --git a/client/app/bundles/course/gradebook/__tests__/GradebookIndex.test.tsx b/client/app/bundles/course/gradebook/__tests__/GradebookIndex.test.tsx new file mode 100644 index 00000000000..25b66fd3ed3 --- /dev/null +++ b/client/app/bundles/course/gradebook/__tests__/GradebookIndex.test.tsx @@ -0,0 +1,149 @@ +import { fireEvent, render, screen, waitFor } from 'test-utils'; + +import toast from 'lib/hooks/toast'; + +import fetchGradebook from '../operations'; +import GradebookIndex from '../pages/GradebookIndex'; + +jest.mock('../../container/CourseLoader', () => ({ + useCourseContext: (): { courseTitle: string; id: number } => ({ + courseTitle: 'Test Course', + id: 1, + }), +})); + +jest.mock('lib/hooks/toast', () => ({ + __esModule: true, + default: { error: jest.fn(), success: jest.fn() }, +})); + +jest.mock('../operations', () => ({ + __esModule: true, + default: jest.fn(() => (): Promise => Promise.resolve()), +})); + +const mockFetchGradebook = fetchGradebook as jest.Mock; + +const emptyState = { + gradebook: { + categories: [], + tabs: [], + assessments: [], + students: [], + submissions: [], + gamificationEnabled: false, + }, +}; + +const noStudentsState = { + gradebook: { + categories: [{ id: 1, title: 'Cat A' }], + tabs: [{ id: 10, title: 'Tab 1', categoryId: 1 }], + assessments: [{ id: 100, title: 'Quiz 1', tabId: 10, maxGrade: 10 }], + students: [], + submissions: [], + gamificationEnabled: false, + }, +}; + +const populatedState = { + gradebook: { + categories: [{ id: 1, title: 'Cat A' }], + tabs: [{ id: 10, title: 'Tab 1', categoryId: 1 }], + assessments: [{ id: 100, title: 'Quiz 1', tabId: 10, maxGrade: 10 }], + students: [ + { + id: 1, + name: 'Alice', + email: 'alice@example.com', + externalId: null, + level: 3, + totalXp: 150, + }, + ], + submissions: [ + { studentId: 1, assessmentId: 100, submissionId: 1000, grade: 8 }, + ], + gamificationEnabled: false, + }, +}; + +const populatedStateWithGamification = { + gradebook: { + ...populatedState.gradebook, + gamificationEnabled: true, + }, +}; + +beforeEach(() => { + jest.clearAllMocks(); + mockFetchGradebook.mockReturnValue((): Promise => Promise.resolve()); +}); + +describe('GradebookIndex', () => { + it('shows loading indicator initially', () => { + render(, { state: emptyState }); + expect(screen.getByRole('progressbar')).toBeInTheDocument(); + }); + + it('shows the gradebook table after data loads', async () => { + render(, { state: populatedState }); + expect( + await screen.findByRole('button', { name: /export/i }), + ).toBeInTheDocument(); + }); + + it('shows the page title', async () => { + render(, { state: populatedState }); + expect(await screen.findByText('Gradebook')).toBeInTheDocument(); + }); + + it('shows empty students message when there are no students', async () => { + render(, { state: noStudentsState }); + expect( + await screen.findByText('No students enrolled yet'), + ).toBeInTheDocument(); + }); + + it('shows empty students message when both assessments and students are absent', async () => { + render(, { state: emptyState }); + expect( + await screen.findByText('No students enrolled yet'), + ).toBeInTheDocument(); + }); + + it('shows error toast when fetch fails', async () => { + mockFetchGradebook.mockReturnValueOnce( + (): Promise => Promise.reject(new Error('Network error')), + ); + render(, { state: emptyState }); + await waitFor(() => expect(toast.error).toHaveBeenCalled()); + }); + + it('shows grade-only hint in column picker when gamification is disabled and no data cols selected', async () => { + render(, { state: populatedState }); + fireEvent.click( + await screen.findByRole('button', { name: /select columns/i }), + ); + expect( + await screen.findByText( + 'No grade columns selected - export will include student info only.', + ), + ).toBeInTheDocument(); + }); + + it('shows grade-and-gamification hint in column picker when gamification is enabled and no data cols selected', async () => { + render(, { state: populatedStateWithGamification }); + fireEvent.click( + await screen.findByRole('button', { name: /select columns/i }), + ); + fireEvent.click( + await screen.findByRole('checkbox', { name: /gamification/i }), + ); + expect( + await screen.findByText( + 'No grade or gamification columns selected - export will include student info only.', + ), + ).toBeInTheDocument(); + }); +}); diff --git a/client/app/bundles/course/gradebook/__tests__/GradebookTable.test.tsx b/client/app/bundles/course/gradebook/__tests__/GradebookTable.test.tsx new file mode 100644 index 00000000000..449fcc5a618 --- /dev/null +++ b/client/app/bundles/course/gradebook/__tests__/GradebookTable.test.tsx @@ -0,0 +1,793 @@ +import userEvent from '@testing-library/user-event'; +import { store as appStore } from 'store'; +import { render, screen, waitFor, within } from 'test-utils'; + +import GradebookTable from '../components/GradebookTable'; +import type { + AssessmentData, + CategoryData, + StudentData, + SubmissionData, + TabData, +} from '../types'; + +const categories: CategoryData[] = [{ id: 1, title: 'Cat A' }]; +const tabs: TabData[] = [{ id: 10, title: 'Tab 1', categoryId: 1 }]; +const assessments: AssessmentData[] = [ + { id: 100, title: 'Quiz 1', tabId: 10, maxGrade: 10 }, +]; +const students: StudentData[] = [ + { + id: 1, + name: 'Alice', + email: 'alice@example.com', + externalId: null, + level: 3, + totalXp: 150, + }, + { + id: 2, + name: 'Bob', + email: 'bob@example.com', + externalId: null, + level: 5, + totalXp: 300, + }, +]; +const submissions: SubmissionData[] = [ + { submissionId: 0, studentId: 1, assessmentId: 100, grade: 8 }, +]; + +const makeStudents = (n: number): StudentData[] => + Array.from({ length: n }, (_, i) => ({ + id: i + 1, + name: `Student ${i + 1}`, + email: `student${i + 1}@example.com`, + externalId: null, + level: 1, + totalXp: 0, + })); + +// Asserts the given texts appear in this top-to-bottom DOM order. +const expectInOrder = (names: string[]): void => { + for (let i = 0; i < names.length - 1; i += 1) { + const earlier = screen.getByText(names[i]); + const later = screen.getByText(names[i + 1]); + expect( + // eslint-disable-next-line no-bitwise + earlier.compareDocumentPosition(later) & Node.DOCUMENT_POSITION_FOLLOWING, + ).toBeTruthy(); + } +}; + +// User id used in all renders so localStorage is keyed as `${USER_ID}:gradebook_columns_1`. +// It is seeded into the global user store, which the course layout loader hydrates +// on every course page; the builder namespaces column persistence by that id. +const USER_ID = 42; +const STORAGE_KEY = `${USER_ID}:gradebook_columns_1`; +const SORT_STORAGE_KEY = `${STORAGE_KEY}_sort`; + +const userState = { + global: { + ...appStore.getState().global, + user: { + ...appStore.getState().global.user, + user: { id: USER_ID, name: '', imageUrl: '' }, + }, + }, +}; + +interface RenderOptions { + gamificationEnabled?: boolean; +} + +const renderTable = ({ + gamificationEnabled = true, +}: RenderOptions = {}): void => { + render( + , + { state: userState }, + ); +}; + +const renderTableWithAssessmentVisible = ( + options: RenderOptions = {}, +): void => { + localStorage.setItem( + STORAGE_KEY, + JSON.stringify({ + name: true, + email: true, + 'asn-100': true, + }), + ); + renderTable(options); +}; + +describe('GradebookTable', () => { + beforeEach(() => localStorage.clear()); + + it('renders both student names', async () => { + renderTableWithAssessmentVisible(); + expect(await screen.findByText('Alice')).toBeInTheDocument(); + expect(await screen.findByText('Bob')).toBeInTheDocument(); + }); + + it('renders two header rows (column titles and max marks)', async () => { + localStorage.setItem( + STORAGE_KEY, + JSON.stringify({ + name: true, + email: true, + + 'asn-100': true, + }), + ); + const { container } = render( + , + { state: userState }, + ); + await screen.findByText('Alice'); + expect(container.querySelectorAll('thead tr')).toHaveLength(2); + }); + + it('shows Select Columns button and Export button', async () => { + renderTableWithAssessmentVisible(); + await screen.findByText('Alice'); + expect( + screen.getByRole('button', { name: /select columns/i }), + ).toBeInTheDocument(); + expect(screen.getByRole('button', { name: /export/i })).toBeInTheDocument(); + }); + + describe('export button label reflects selection', () => { + it('shows "Export all rows" when no rows are selected', async () => { + renderTableWithAssessmentVisible(); + await screen.findByText('Alice'); + expect( + screen.getByRole('button', { name: /export all rows/i }), + ).toBeInTheDocument(); + }); + + it('shows tooltip "all rows will be exported" when no rows are selected', async () => { + const user = userEvent.setup(); + renderTableWithAssessmentVisible(); + const exportBtn = await screen.findByRole('button', { + name: /export all rows/i, + }); + await user.hover(exportBtn); + expect( + await screen.findByText(/all rows will be exported/i), + ).toBeInTheDocument(); + }); + + it('hides the tooltip when a row is selected', async () => { + const user = userEvent.setup(); + renderTableWithAssessmentVisible(); + const checkboxes = await screen.findAllByRole('checkbox'); + await user.click(checkboxes[1]); + const exportBtn = await screen.findByRole('button', { + name: /export 1 row/i, + }); + await user.hover(exportBtn); + expect( + screen.queryByText(/all rows will be exported/i), + ).not.toBeInTheDocument(); + }); + + it('shows "Export 1 row" when one row is selected', async () => { + const user = userEvent.setup(); + renderTableWithAssessmentVisible(); + const checkboxes = await screen.findAllByRole('checkbox'); + await user.click(checkboxes[1]); + await waitFor(() => + expect( + screen.getByRole('button', { name: /export 1 row/i }), + ).toBeInTheDocument(), + ); + }); + + it('shows "Export all rows" when all rows are selected via the corner checkbox', async () => { + const user = userEvent.setup(); + renderTableWithAssessmentVisible(); + const checkboxes = await screen.findAllByRole('checkbox'); + await user.click(checkboxes[0]); + await waitFor(() => + expect( + screen.getByRole('button', { name: /export all rows/i }), + ).toBeInTheDocument(), + ); + expect( + screen.queryByRole('button', { name: /export \d+ row/i }), + ).not.toBeInTheDocument(); + }); + }); + + it('shows the Max Marks header row', async () => { + renderTableWithAssessmentVisible(); + expect(await screen.findByText('Max Marks')).toBeInTheDocument(); + }); + + it('renders row selection checkboxes', async () => { + renderTableWithAssessmentVisible(); + await screen.findByText('Alice'); + expect(screen.getAllByRole('checkbox').length).toBeGreaterThanOrEqual(2); + }); + + describe('row selection', () => { + it('keeps search input visible after selecting a row', async () => { + const user = userEvent.setup(); + renderTableWithAssessmentVisible(); + const checkboxes = await screen.findAllByRole('checkbox'); + await user.click(checkboxes[1]); + expect(screen.getByRole('textbox')).toBeInTheDocument(); + }); + + it('keeps Export button visible after selecting a row', async () => { + const user = userEvent.setup(); + renderTableWithAssessmentVisible(); + const checkboxes = await screen.findAllByRole('checkbox'); + await user.click(checkboxes[1]); + expect( + screen.getByRole('button', { name: /export/i }), + ).toBeInTheDocument(); + }); + }); + + it('does not show assessment columns in the table by default', async () => { + renderTable(); + await screen.findByText('Alice'); + expect(screen.queryByText('Quiz 1')).not.toBeInTheDocument(); + }); + + it('shows gamification columns by default when gamification is enabled', async () => { + renderTable({ gamificationEnabled: true }); + expect(await screen.findByText('Level')).toBeInTheDocument(); + expect(screen.getByText('Total XP')).toBeInTheDocument(); + }); + + describe('gamification columns', () => { + it('shows level and totalXp in the column picker when gamification is enabled', async () => { + const user = userEvent.setup(); + renderTable({ gamificationEnabled: true }); + const selectColumnsBtn = await screen.findByRole('button', { + name: /select columns/i, + }); + await user.click(selectColumnsBtn); + const dialog = await screen.findByRole('dialog'); + expect(within(dialog).getByText('Level')).toBeInTheDocument(); + expect(within(dialog).getByText('Total XP')).toBeInTheDocument(); + }); + }); + + describe('locked name column', () => { + it('name is always visible even when localStorage sets it to false', async () => { + localStorage.setItem( + STORAGE_KEY, + JSON.stringify({ + name: false, + email: true, + + 'asn-100': true, + }), + ); + renderTable(); + await waitFor(() => + expect(screen.getByText('Alice')).toBeInTheDocument(), + ); + }); + }); + + describe('gamification disabled', () => { + it('level and totalXp absent from table headers when gamification is disabled', async () => { + localStorage.setItem( + STORAGE_KEY, + JSON.stringify({ + name: true, + email: true, + + level: true, + totalXp: true, + 'asn-100': true, + }), + ); + renderTable({ gamificationEnabled: false }); + await screen.findByText('Alice'); + expect(screen.queryByText('Level')).not.toBeInTheDocument(); + expect(screen.queryByText('Total XP')).not.toBeInTheDocument(); + }); + }); + + it('shows the table when gamification columns are visible and assessments are deselected', async () => { + localStorage.setItem(STORAGE_KEY, JSON.stringify({ 'asn-100': false })); + renderTable({ gamificationEnabled: true }); + expect(await screen.findByText('Alice')).toBeInTheDocument(); + expect(screen.getByRole('table')).toBeInTheDocument(); + }); + + it('export button is always enabled regardless of which columns are selected', async () => { + localStorage.setItem(STORAGE_KEY, JSON.stringify({ 'asn-100': false })); + renderTable({ gamificationEnabled: false }); + await screen.findByText('Alice'); + expect(screen.getByRole('button', { name: /export/i })).not.toBeDisabled(); + }); + + it('shows the table (not an empty state) when all assessments are deselected', async () => { + localStorage.setItem(STORAGE_KEY, JSON.stringify({ 'asn-100': false })); + renderTable({ gamificationEnabled: false }); + expect(await screen.findByRole('table')).toBeInTheDocument(); + expect(await screen.findByText('Alice')).toBeInTheDocument(); + }); + + it('shows the table when all optional columns are deselected with gamification', async () => { + localStorage.setItem( + STORAGE_KEY, + JSON.stringify({ 'asn-100': false, level: false, totalXp: false }), + ); + renderTable({ gamificationEnabled: true }); + expect(await screen.findByRole('table')).toBeInTheDocument(); + expect(await screen.findByText('Alice')).toBeInTheDocument(); + }); + + it('shows pagination when all assessments are deselected', async () => { + localStorage.setItem(STORAGE_KEY, JSON.stringify({ 'asn-100': false })); + renderTable({ gamificationEnabled: false }); + await screen.findByText('Alice'); + expect(screen.getByText(/rows per page/i)).toBeInTheDocument(); + }); + + it('shows the table with assessment columns when restored from localStorage', async () => { + localStorage.setItem( + STORAGE_KEY, + JSON.stringify({ + name: true, + email: true, + + 'asn-100': true, + }), + ); + renderTable(); + expect(await screen.findByText('Quiz 1')).toBeInTheDocument(); + }); + + // An assessment selected (and persisted) earlier may be deleted before the next + // visit, leaving a stale column id in localStorage. The table must ignore it + // rather than crash, and still render the surviving assessment columns. + it('does not crash when localStorage references a deleted assessment', async () => { + localStorage.setItem( + STORAGE_KEY, + JSON.stringify({ + name: true, + email: true, + 'asn-100': true, + 'asn-999': true, + }), + ); + renderTable(); + expect(await screen.findByText('Alice')).toBeInTheDocument(); + // The surviving assessment still renders; the deleted one is silently dropped. + expect(screen.getByText('Quiz 1')).toBeInTheDocument(); + }); + + describe('search', () => { + it('filters by name', async () => { + const user = userEvent.setup(); + renderTableWithAssessmentVisible(); + const input = await screen.findByRole('textbox'); + await user.type(input, 'Alice'); + await waitFor(() => + expect(screen.queryByText('Bob')).not.toBeInTheDocument(), + ); + expect(screen.getByText('Alice')).toBeInTheDocument(); + }); + + it('filters by email', async () => { + const user = userEvent.setup(); + renderTableWithAssessmentVisible(); + const input = await screen.findByRole('textbox'); + await user.type(input, 'bob@example.com'); + await waitFor(() => + expect(screen.queryByText('Alice')).not.toBeInTheDocument(), + ); + expect(screen.getByText('Bob')).toBeInTheDocument(); + }); + }); + + describe('external ID column', () => { + const studentsWithExtId: StudentData[] = [ + { + id: 1, + name: 'Alice', + email: 'alice@example.com', + externalId: 'EXT-001', + level: 3, + totalXp: 150, + }, + { + id: 2, + name: 'Bob', + email: 'bob@example.com', + externalId: null, + level: 5, + totalXp: 300, + }, + ]; + + const renderWith = (studs: StudentData[]): void => { + render( + , + { state: userState }, + ); + }; + + it('shows the External ID column by default when a student has an external ID', async () => { + renderWith(studentsWithExtId); + expect(await screen.findByText('External ID')).toBeInTheDocument(); + expect(screen.getByText('EXT-001')).toBeInTheDocument(); + }); + + it('hides the External ID column by default when no student has an external ID', async () => { + renderWith(students); + await screen.findByText('Alice'); + expect(screen.queryByText('External ID')).not.toBeInTheDocument(); + }); + + it('treats a blank external ID as none and hides the column by default', async () => { + const studentsWithBlankExtId: StudentData[] = [ + { + id: 1, + name: 'Alice', + email: 'alice@example.com', + externalId: '', + level: 3, + totalXp: 150, + }, + ]; + renderWith(studentsWithBlankExtId); + await screen.findByText('Alice'); + expect(screen.queryByText('External ID')).not.toBeInTheDocument(); + }); + + it('offers the External ID checkbox in the picker even when no student has one', async () => { + const user = userEvent.setup(); + renderWith(students); + const btn = await screen.findByRole('button', { + name: /select columns/i, + }); + await user.click(btn); + const dialog = await screen.findByRole('dialog'); + expect( + within(dialog).getByRole('checkbox', { name: /external id/i }), + ).toBeInTheDocument(); + }); + + it('filters by external ID', async () => { + const user = userEvent.setup(); + renderWith(studentsWithExtId); + const input = await screen.findByRole('textbox'); + await user.type(input, 'EXT-001'); + await waitFor(() => + expect(screen.queryByText('Bob')).not.toBeInTheDocument(), + ); + expect(screen.getByText('Alice')).toBeInTheDocument(); + }); + }); + + describe('sorting', () => { + it('defaults to sorting by name ascending on first load', async () => { + // Raw prop order is Bob, then Alice. A working default name-asc sort must + // reorder the rendered rows to Alice, then Bob. + render( + , + { state: userState }, + ); + await screen.findByText('Alice'); + expectInOrder(['Alice', 'Bob']); + }); + + it('cycles back to ascending on a third click (sort is never cleared)', async () => { + const user = userEvent.setup(); + renderTableWithAssessmentVisible(); + await screen.findByText('Alice'); + await user.click(screen.getByRole('button', { name: /name/i })); // → desc + await waitFor(() => expectInOrder(['Bob', 'Alice'])); + await user.click(screen.getByRole('button', { name: /name/i })); // → asc again + await waitFor(() => expectInOrder(['Alice', 'Bob'])); + }); + + it('toggles to descending when the name header is clicked', async () => { + const user = userEvent.setup(); + renderTableWithAssessmentVisible(); + await screen.findByText('Alice'); + expectInOrder(['Alice', 'Bob']); // default ascending + await user.click(screen.getByRole('button', { name: /name/i })); + await waitFor(() => expectInOrder(['Bob', 'Alice'])); + }); + + it('preserves the active sort after searching', async () => { + const user = userEvent.setup(); + renderTableWithAssessmentVisible(); + await screen.findByText('Alice'); + // Switch to descending so the order is distinct from both raw and default. + await user.click(screen.getByRole('button', { name: /name/i })); + await waitFor(() => expectInOrder(['Bob', 'Alice'])); + // A search matching both students must not reset the descending sort. + await user.type(screen.getByRole('textbox'), 'example.com'); + await waitFor(() => + expect(screen.getByText('Alice')).toBeInTheDocument(), + ); + expectInOrder(['Bob', 'Alice']); + }); + + it('resets to name ascending when the sorted column is hidden', async () => { + const user = userEvent.setup(); + // Start with Quiz 1 visible and sort by it descending. + localStorage.setItem( + STORAGE_KEY, + JSON.stringify({ name: true, 'asn-100': true }), + ); + render( + , + { state: userState }, + ); + await screen.findByText('Alice'); + await user.click(screen.getByRole('button', { name: /quiz 1/i })); // sort by grade asc + await user.click(screen.getByRole('button', { name: /quiz 1/i })); // → desc + await waitFor(() => expectInOrder(['Alice', 'Bob'])); // Alice has grade 8, Bob has none + + // Hide Quiz 1 via the column picker. + await user.click(screen.getByRole('button', { name: /select columns/i })); + const dialog = await screen.findByRole('dialog'); + await user.click( + within(dialog).getByRole('checkbox', { name: /quiz 1/i }), + ); + await user.click(screen.getByRole('button', { name: /apply/i })); + + // Sort should reset to name ascending: Alice before Bob. + await waitFor(() => expectInOrder(['Alice', 'Bob'])); + }); + + it('saves the sort to localStorage when the user clicks a column header', async () => { + const user = userEvent.setup(); + renderTableWithAssessmentVisible(); + await screen.findByText('Alice'); + await user.click(screen.getByRole('button', { name: /name/i })); // → desc + await waitFor(() => expectInOrder(['Bob', 'Alice'])); + expect( + JSON.parse(localStorage.getItem(SORT_STORAGE_KEY) ?? 'null'), + ).toEqual([{ id: 'name', desc: true }]); + }); + + it('restores sort from localStorage on re-mount', async () => { + // Pre-seed descending name sort so the table should render Bob before Alice. + localStorage.setItem( + SORT_STORAGE_KEY, + JSON.stringify([{ id: 'name', desc: true }]), + ); + renderTableWithAssessmentVisible(); + await screen.findByText('Alice'); + expectInOrder(['Bob', 'Alice']); + }); + + it('persists the name-ascending reset to localStorage when a sorted column is hidden', async () => { + const user = userEvent.setup(); + localStorage.setItem( + STORAGE_KEY, + JSON.stringify({ name: true, 'asn-100': true }), + ); + render( + , + { state: userState }, + ); + await screen.findByText('Alice'); + await user.click(screen.getByRole('button', { name: /quiz 1/i })); // asc + await user.click(screen.getByRole('button', { name: /quiz 1/i })); // desc + + await user.click(screen.getByRole('button', { name: /select columns/i })); + const dialog = await screen.findByRole('dialog'); + await user.click( + within(dialog).getByRole('checkbox', { name: /quiz 1/i }), + ); + await user.click(screen.getByRole('button', { name: /apply/i })); + + await waitFor(() => expectInOrder(['Alice', 'Bob'])); + expect( + JSON.parse(localStorage.getItem(SORT_STORAGE_KEY) ?? 'null'), + ).toEqual([{ id: 'name', desc: false }]); + }); + + describe('assessment grade sorting', () => { + const studentFor = (id: number, name: string): StudentData => ({ + id, + name, + email: `${name.toLowerCase()}@example.com`, + externalId: null, + level: 1, + totalXp: 0, + }); + + const renderGrades = ( + studs: StudentData[], + subs: SubmissionData[], + ): void => { + localStorage.setItem( + STORAGE_KEY, + JSON.stringify({ name: true, 'asn-100': true }), + ); + render( + , + { state: userState }, + ); + }; + + // Alice graded 8, Bob graded 3, Carol no submission (undefined), + // Dave submitted but ungraded (null). + const mixedStudents: StudentData[] = [ + studentFor(1, 'Alice'), + studentFor(2, 'Bob'), + studentFor(3, 'Carol'), + studentFor(4, 'Dave'), + ]; + const mixedSubmissions: SubmissionData[] = [ + { submissionId: 1, studentId: 1, assessmentId: 100, grade: 8 }, + { submissionId: 2, studentId: 2, assessmentId: 100, grade: 3 }, + { submissionId: 4, studentId: 4, assessmentId: 100, grade: null }, + ]; + + it('sorts missing grades to the bottom in ascending order', async () => { + const user = userEvent.setup(); + renderGrades(mixedStudents, mixedSubmissions); + await screen.findByText('Alice'); + // Grade columns sort desc-first, so click twice to reach ascending. + await user.click(screen.getByRole('button', { name: /quiz 1/i })); // desc + await user.click(screen.getByRole('button', { name: /quiz 1/i })); // asc + // Ascending: Bob(3), Alice(8), then the two missing rows last. + await waitFor(() => expectInOrder(['Bob', 'Alice'])); + expectInOrder(['Alice', 'Carol']); + expectInOrder(['Alice', 'Dave']); + }); + + it('keeps missing grades at the bottom in descending order', async () => { + const user = userEvent.setup(); + renderGrades(mixedStudents, mixedSubmissions); + await screen.findByText('Alice'); + // Grade columns sort desc-first, so a single click yields descending. + await user.click(screen.getByRole('button', { name: /quiz 1/i })); // desc + // Descending: Alice(8), Bob(3), then the two missing rows still last. + await waitFor(() => expectInOrder(['Alice', 'Bob'])); + expectInOrder(['Bob', 'Carol']); + expectInOrder(['Bob', 'Dave']); + }); + + it('sorts grades numerically (9 before 10), not lexically', async () => { + const user = userEvent.setup(); + renderGrades( + [studentFor(1, 'Alice'), studentFor(2, 'Bob')], + [ + { submissionId: 1, studentId: 1, assessmentId: 100, grade: 9 }, + { submissionId: 2, studentId: 2, assessmentId: 100, grade: 10 }, + ], + ); + await screen.findByText('Alice'); + // Grade columns sort desc-first, so click twice to reach ascending. + await user.click(screen.getByRole('button', { name: /quiz 1/i })); // desc + await user.click(screen.getByRole('button', { name: /quiz 1/i })); // asc + // Numeric ascending: 9 (Alice) before 10 (Bob). Lexical would reverse this. + await waitFor(() => expectInOrder(['Alice', 'Bob'])); + }); + }); + }); + + describe('cross-page selection', () => { + it('export label reflects selection count across pages', async () => { + const user = userEvent.setup(); + localStorage.setItem( + STORAGE_KEY, + JSON.stringify({ + name: true, + email: true, + + 'asn-100': true, + }), + ); + render( + , + { state: userState }, + ); + + const checkboxes = await screen.findAllByRole('checkbox'); + await user.click(checkboxes[1]); + await waitFor(() => + expect( + screen.getByRole('button', { name: /export 1 row/i }), + ).toBeInTheDocument(), + ); + + // Default page size is DEFAULT_TABLE_ROWS_PER_PAGE (100), so 101 students + // span two pages: Student 1 is on page 1, Student 101 alone on page 2. + await user.click( + screen.getByRole('button', { name: /go to next page/i }), + ); + await waitFor(() => + expect(screen.getByText('Student 101')).toBeInTheDocument(), + ); + expect(screen.queryByText('Student 1')).not.toBeInTheDocument(); + + expect( + screen.getByRole('button', { name: /export 1 row/i }), + ).toBeInTheDocument(); + }); + }); +}); diff --git a/client/app/bundles/course/gradebook/components/GradeLinkHint.tsx b/client/app/bundles/course/gradebook/components/GradeLinkHint.tsx new file mode 100644 index 00000000000..7b2e8f44a1f --- /dev/null +++ b/client/app/bundles/course/gradebook/components/GradeLinkHint.tsx @@ -0,0 +1,47 @@ +import { FC } from 'react'; +import { defineMessages } from 'react-intl'; +import { Alert, Typography } from '@mui/material'; + +import { getUserEntity } from 'bundles/users/selectors'; +import { useAppSelector } from 'lib/hooks/store'; +import useDismissibleOnce from 'lib/hooks/useDismissibleOnce'; +import useTranslation from 'lib/hooks/useTranslation'; + +export const GRADE_LINK_HINT_KEY = 'gradebook_grade_link_hint'; + +const translations = defineMessages({ + hint: { + id: 'course.gradebook.GradeLinkHint.hint', + defaultMessage: + "Each grade is the total of the marks in a student's submission. Click any grade to open that submission and adjust the marks.", + }, +}); + +/** + * One-time, dismissable nudge explaining that each grade in the "All assessments" + * table is a link into the student's submission. Grades have no gradebook-level edit + * (an assessment's total is always the sum of its question marks), so clicking into the + * submission is the only path to changing a grade — an affordance that surprises users + * because a number is not expected to be clickable. + * Dismissal is remembered per user via localStorage (see useDismissibleOnce). + */ +const GradeLinkHint: FC = () => { + const { t } = useTranslation(); + const userId = useAppSelector(getUserEntity).id; + const { dismissed, dismiss } = useDismissibleOnce( + GRADE_LINK_HINT_KEY, + userId, + ); + + if (dismissed) return null; + + return ( +
+ + {t(translations.hint)} + +
+ ); +}; + +export default GradeLinkHint; diff --git a/client/app/bundles/course/gradebook/components/GradebookColumnTree.tsx b/client/app/bundles/course/gradebook/components/GradebookColumnTree.tsx new file mode 100644 index 00000000000..e2b5d526485 --- /dev/null +++ b/client/app/bundles/course/gradebook/components/GradebookColumnTree.tsx @@ -0,0 +1,219 @@ +import { useMemo } from 'react'; +import { defineMessages } from 'react-intl'; +import { Chip } from '@mui/material'; + +import IndentedCheckbox from 'lib/components/core/IndentedCheckbox'; +import { + ColumnPickerRenderContext, + ColumnPickerTreeGroup, +} from 'lib/components/table'; +import useTranslation from 'lib/hooks/useTranslation'; +import tableTranslations from 'lib/translations/table'; + +import { + GAMIFICATION_COL_IDS, + type GamificationColId, + STUDENT_INFO_COL_IDS, + type StudentInfoColId, +} from '../constants'; +import type { AssessmentData, CategoryData, TabData } from '../types'; + +import { + buildAssessmentColumnId, + parseAssessmentColumnId, +} from './buildAssessmentColumnIds'; + +const translations = defineMessages({ + studentInfo: { + id: 'course.gradebook.GradebookColumnTree.studentInfo', + defaultMessage: 'Student info', + }, + gamification: { + id: 'course.gradebook.GradebookColumnTree.gamification', + defaultMessage: 'Gamification', + }, + grades: { + id: 'course.gradebook.GradebookColumnTree.grades', + defaultMessage: 'Grades', + }, + alwaysIncluded: { + id: 'course.gradebook.GradebookColumnTree.alwaysIncluded', + defaultMessage: 'Always included', + }, +}); + +interface GradebookColumnTreeProps extends ColumnPickerRenderContext { + categories: CategoryData[]; + tabs: TabData[]; + assessments: AssessmentData[]; + gamificationEnabled: boolean; +} + +const GAMIFICATION_ALL_IDS = [...GAMIFICATION_COL_IDS]; + +const GradebookColumnTree = ({ + isVisible, + setVisible, + setManyVisible, + categories, + tabs, + assessments, + gamificationEnabled, +}: GradebookColumnTreeProps): JSX.Element => { + const { t } = useTranslation(); + const context: ColumnPickerRenderContext = { + isVisible, + setVisible, + setManyVisible, + }; + + const asnIds = useMemo( + () => assessments.map((a) => buildAssessmentColumnId(a.id)), + [assessments], + ); + + const tabAsnIds = useMemo(() => { + const map = new Map(); + assessments.forEach((a) => { + const existing = map.get(a.tabId) ?? []; + map.set(a.tabId, [...existing, buildAssessmentColumnId(a.id)]); + }); + return map; + }, [assessments]); + + const catTabs = useMemo(() => { + const map = new Map(); + tabs.forEach((tab) => { + const existing = map.get(tab.categoryId) ?? []; + map.set(tab.categoryId, [...existing, tab]); + }); + return map; + }, [tabs]); + + const asnById = useMemo( + () => new Map(assessments.map((a) => [a.id, a])), + [assessments], + ); + + const catAsnIds = useMemo(() => { + const map = new Map(); + tabs.forEach((tab) => { + const tabIds = tabAsnIds.get(tab.id) ?? []; + const existing = map.get(tab.categoryId) ?? []; + map.set(tab.categoryId, [...existing, ...tabIds]); + }); + return map; + }, [tabs, tabAsnIds]); + + return ( +
+ + {STUDENT_INFO_COL_IDS.map((id: StudentInfoColId) => + id === 'name' ? ( + + {t(tableTranslations[id])} + + + } + /> + ) : ( + setVisible(id, e.target.checked)} + /> + ), + )} + + + {gamificationEnabled && ( + + {GAMIFICATION_COL_IDS.map((id: GamificationColId) => ( + setVisible(id, e.target.checked)} + /> + ))} + + )} + + + {categories.map((cat) => { + const catIds = catAsnIds.get(cat.id) ?? []; + const thisCatTabs = catTabs.get(cat.id) ?? []; + return ( + + {thisCatTabs.map((tab) => { + const tabIds = tabAsnIds.get(tab.id) ?? []; + return ( + + {tabIds.map((id) => { + const asnId = parseAssessmentColumnId(id); + const asn = + asnId !== null ? asnById.get(asnId) : undefined; + if (!asn) return null; + return ( + setVisible(id, e.target.checked)} + /> + ); + })} + + ); + })} + + ); + })} + +
+ ); +}; + +export default GradebookColumnTree; diff --git a/client/app/bundles/course/gradebook/components/GradebookTable.tsx b/client/app/bundles/course/gradebook/components/GradebookTable.tsx new file mode 100644 index 00000000000..eac4f665074 --- /dev/null +++ b/client/app/bundles/course/gradebook/components/GradebookTable.tsx @@ -0,0 +1,808 @@ +import { + forwardRef, + useCallback, + useLayoutEffect, + useMemo, + useRef, + useState, +} from 'react'; +import { defineMessages } from 'react-intl'; +import { + Checkbox, + Paper, + type SxProps, + Table, + TableBody, + TableCell, + TableContainer, + TableHead, + TableRow, + TableSortLabel, + type Theme, + Tooltip, +} from '@mui/material'; +import { flexRender } from '@tanstack/react-table'; + +import Link from 'lib/components/core/Link'; +import type { + ColumnPickerRenderContext, + ColumnTemplate, +} from 'lib/components/table/builder'; +import MuiTablePagination from 'lib/components/table/MuiTableAdapter/MuiTablePagination'; +import MuiTableToolbar from 'lib/components/table/MuiTableAdapter/MuiTableToolbar'; +import useTanStackTableBuilder from 'lib/components/table/TanStackTableBuilder'; +import { + DEFAULT_MINI_TABLE_ROWS_PER_PAGE, + DEFAULT_TABLE_ROWS_PER_PAGE, +} from 'lib/constants/sharedConstants'; +import { getEditSubmissionURL } from 'lib/helpers/url-builders'; +import useTranslation from 'lib/hooks/useTranslation'; +import tableTranslations from 'lib/translations/table'; + +import { GAMIFICATION_COL_IDS } from '../constants'; +import type { + AssessmentData, + CategoryData, + StudentData, + SubmissionData, + TabData, +} from '../types'; + +import { + buildAssessmentColumnId, + parseAssessmentColumnId, +} from './buildAssessmentColumnIds'; +import GradebookColumnTree from './GradebookColumnTree'; + +const COL_WIDTHS = { + name: 160, + email: 250, + externalId: 160, + level: 80, + totalXp: 120, + assessment: 150, +} as const; + +const CHECKBOX_WIDTH = 56; + +const getColWidth = (id: string): number => + COL_WIDTHS[id as keyof typeof COL_WIDTHS] ?? COL_WIDTHS.assessment; + +const isLeftAligned = (id: string): boolean => + id === 'name' || id === 'email' || id === 'externalId'; + +const translations = defineMessages({ + searchStudents: { + id: 'course.gradebook.GradebookIndex.searchStudents', + defaultMessage: 'Search students', + }, + exportButton: { + id: 'course.gradebook.GradebookIndex.exportButton', + defaultMessage: 'Export all rows', + }, + exportRows: { + id: 'course.gradebook.GradebookIndex.exportRows', + defaultMessage: 'Export {count, plural, one {# row} other {# rows}}', + }, + exportAllTooltip: { + id: 'course.gradebook.GradebookIndex.exportAllTooltip', + defaultMessage: 'No rows selected - all rows will be exported.', + }, + selectColumns: { + id: 'course.gradebook.GradebookIndex.selectColumns', + defaultMessage: 'Select Columns', + }, + dialogTitle: { + id: 'course.gradebook.GradebookIndex.dialogTitle', + defaultMessage: 'Select columns', + }, + maxMarks: { + id: 'course.gradebook.GradebookTable.maxMarks', + defaultMessage: 'Max Marks', + }, + noDataColumnsHint: { + id: 'course.gradebook.GradebookTable.noDataColumnsHint', + defaultMessage: + 'No grade columns selected - export will include student info only.', + }, + noDataColumnsHintWithGamification: { + id: 'course.gradebook.GradebookTable.noDataColumnsHintWithGamification', + defaultMessage: + 'No grade or gamification columns selected - export will include student info only.', + }, +}); + +const HeaderLabel = forwardRef< + HTMLSpanElement, + { text: string; onSingleLine: (fits: boolean) => void } +>(({ text, onSingleLine }, forwardedRef): JSX.Element => { + const innerRef = useRef(null); + const [display, setDisplay] = useState(text); + + useLayoutEffect(() => { + const el = innerRef.current; + if (!el) return; + + const lh = parseFloat(getComputedStyle(el).lineHeight) || 20; + const oneLineH = lh + 1; + const twoLineH = lh * 2 + 1; + + el.textContent = text; + + if (el.scrollHeight <= oneLineH) { + onSingleLine(true); + setDisplay(text); + return; + } + + onSingleLine(false); + + if (el.scrollHeight <= twoLineH) { + setDisplay(text); + return; + } + + let lo = 1; + let hi = text.length; + let best = `${text[0]}…`; + while (lo <= hi) { + const mid = Math.floor((lo + hi) / 2); + const candidate = `${text.slice(0, mid)}…`; + el.textContent = candidate; + if (el.scrollHeight <= twoLineH) { + best = candidate; + lo = mid + 1; + } else { + hi = mid - 1; + } + } + // Ensure DOM reflects `best` before React reconciles — the loop's last + // el.textContent assignment may be a too-long candidate, not `best`. + el.textContent = best; + setDisplay(best); + }, [text, onSingleLine]); + + return ( + { + innerRef.current = node; + if (typeof forwardedRef === 'function') forwardedRef(node); + else if (forwardedRef) forwardedRef.current = node; + }} + style={{ display: 'block' }} + > + {display} + + ); +}); +HeaderLabel.displayName = 'HeaderLabel'; + +interface GradebookRow { + studentId: number; + name: string; + email: string; + externalId: string | null; + level: number; + totalXp: number; + grades: Partial>; + submissionIds: Partial>; +} + +interface GradebookTableProps { + categories: CategoryData[]; + tabs: TabData[]; + assessments: AssessmentData[]; + students: StudentData[]; + submissions: SubmissionData[]; + courseTitle: string; + courseId: number; + gamificationEnabled: boolean; +} + +const GradebookTable = ({ + categories, + tabs, + assessments, + students, + submissions, + courseTitle, + courseId, + gamificationEnabled, +}: GradebookTableProps): JSX.Element => { + const { t } = useTranslation(); + + const submissionsByStudent = useMemo(() => { + const map = new Map>(); + submissions.forEach((s) => { + let byAssessment = map.get(s.studentId); + if (!byAssessment) { + byAssessment = new Map(); + map.set(s.studentId, byAssessment); + } + byAssessment.set(s.assessmentId, s); + }); + return map; + }, [submissions]); + + const rows = useMemo( + () => + students.map((student) => { + const subs = submissionsByStudent.get(student.id); + const grades: Partial> = {}; + const submissionIds: Partial> = {}; + assessments.forEach((a) => { + const sub = subs?.get(a.id); + if (sub != null) { + grades[a.id] = sub.grade; + submissionIds[a.id] = sub.submissionId; + } + }); + return { + studentId: student.id, + name: student.name, + email: student.email, + externalId: student.externalId, + level: student.level, + totalXp: student.totalXp, + grades, + submissionIds, + }; + }), + [students, assessments, submissionsByStudent], + ); + + const hasExternalIds = useMemo( + () => students.some((s) => s.externalId != null && s.externalId !== ''), + [students], + ); + + const columns = useMemo[]>(() => { + const cols: ColumnTemplate[] = [ + { + id: 'name', + title: t(tableTranslations.name), + of: 'name', + cell: (row) => row.name, + csvDownloadable: true, + searchable: true, + sortable: true, + searchProps: { getValue: (row) => row.name }, + }, + { + id: 'email', + title: t(tableTranslations.email), + of: 'email', + cell: (row) => row.email, + csvDownloadable: true, + searchable: true, + sortable: true, + }, + ]; + + // The External ID column is always offered in the picker, but only shown by + // default when the course actually uses external IDs (see column picker). + cols.push({ + id: 'externalId', + title: t(tableTranslations.externalId), + of: 'externalId', + cell: (row) => row.externalId ?? '', + csvDownloadable: true, + searchable: true, + sortable: true, + defaultVisible: hasExternalIds, + }); + + if (gamificationEnabled) { + cols.push({ + id: 'level', + title: t(tableTranslations.level), + of: 'level', + cell: (row) => row.level, + csvDownloadable: true, + sortable: true, + }); + cols.push({ + id: 'totalXp', + title: t(tableTranslations.totalXp), + of: 'totalXp', + cell: (row) => row.totalXp, + csvDownloadable: true, + sortable: true, + }); + } + + assessments.forEach((asn) => { + const colId = buildAssessmentColumnId(asn.id); + cols.push({ + id: colId, + title: asn.title, + // null (ungraded) → undefined so sortUndefined: 'last' fires for both missing and ungraded rows + accessorFn: (row) => row.grades[asn.id] ?? undefined, + sortable: true, + sortProps: { + undefinedPriority: 'last', + sort: (a, b) => { + const aGrade = a.grades[asn.id]; + const bGrade = b.grades[asn.id]; + if (aGrade == null || bGrade == null) return 0; + return aGrade - bGrade; + }, + }, + cell: (row) => { + const grade = row.grades[asn.id]; + if (grade === undefined) return '—'; + if (grade === null) return ''; + const submissionId = row.submissionIds[asn.id]; + if (submissionId != null) + return ( + + {grade} + + ); + return grade; + }, + csvDownloadable: true, + defaultVisible: false, + }); + }); + return cols; + }, [assessments, gamificationEnabled, hasExternalIds, t]); + + const assessmentMaxGrades = useMemo( + () => new Map(assessments.map((a) => [a.id, a.maxGrade])), + [assessments], + ); + + const dataColumnIds = useMemo( + () => [ + ...assessments.map((a) => buildAssessmentColumnId(a.id)), + ...GAMIFICATION_COL_IDS, + ], + [assessments], + ); + + const columnPicker = useMemo( + () => ({ + render: (context: ColumnPickerRenderContext) => ( + + ), + locked: ['name'], + triggerLabel: t(translations.selectColumns), + dialogTitle: t(translations.dialogTitle), + getExtraHeaderRows: (colIds): string[][] => { + const hasAssessments = colIds.some( + (id) => parseAssessmentColumnId(id) !== null, + ); + if (!hasAssessments) return []; + return [ + colIds.map((id) => { + if (id === 'name') return t(translations.maxMarks); + const asnId = parseAssessmentColumnId(id); + if (asnId !== null) + return String(assessmentMaxGrades.get(asnId) ?? ''); + return ''; + }), + ]; + }, + storageKey: `gradebook_columns_${courseId}`, + dataColumnIds, + noDataColumnsHint: gamificationEnabled + ? t(translations.noDataColumnsHintWithGamification) + : t(translations.noDataColumnsHint), + }), + [ + assessments, + categories, + gamificationEnabled, + tabs, + t, + assessmentMaxGrades, + courseId, + dataColumnIds, + ], + ); + + const { toolbar, body, pagination, header } = + useTanStackTableBuilder({ + data: rows, + columns, + getRowId: (row) => row.studentId.toString(), + getRowEqualityData: (row) => row, + indexing: { rowSelectable: true }, + pagination: { + initialPageSize: DEFAULT_TABLE_ROWS_PER_PAGE, + rowsPerPage: [ + DEFAULT_MINI_TABLE_ROWS_PER_PAGE, + 25, + 50, + DEFAULT_TABLE_ROWS_PER_PAGE, + ], + showAllRows: true, + }, + search: { searchPlaceholder: t(translations.searchStudents) }, + sort: { + initially: { by: 'name', order: 'asc' }, + enableRemoval: false, + resetOnHide: true, + }, + toolbar: { show: true, keepNative: true }, + csvDownload: { + filename: `${courseTitle}_gradebook`, + showDownloadButton: false, + }, + columnPicker, + }); + + const visibility = toolbar?.getColumnVisibility?.() ?? {}; + const isColVisible = (id: string): boolean => visibility[id] ?? true; + const visibleCols = columns.filter((c) => + isColVisible(c.id ?? (c.of as string)), + ); + + const sortByColId = new Map( + (header?.headers ?? []).map( + (h, i) => [h.id, header?.forEach(h, i).sorting] as const, + ), + ); + + const selectedCount = body.selectedCount ?? 0; + + const directExportLabel = useMemo((): string => { + const isPartialSelection = selectedCount > 0 && selectedCount < rows.length; + if (isPartialSelection) + return t(translations.exportRows, { count: selectedCount }); + return t(translations.exportButton); + }, [selectedCount, rows.length, t]); + + const toolbarWithLabel = toolbar?.columnPicker + ? { + ...toolbar, + columnPicker: { + ...toolbar.columnPicker, + directExportLabel, + directExportTooltip: + selectedCount === 0 ? t(translations.exportAllTooltip) : undefined, + }, + } + : toolbar; + + const totalWidth = useMemo( + () => + CHECKBOX_WIDTH + + visibleCols.reduce((sum, c) => { + const id = c.id ?? (c.of as string); + return sum + getColWidth(id); + }, 0), + [visibleCols], + ); + + const allRowsSelected = body.allFilteredSelected ?? false; + const someRowsSelected = body.someFilteredSelected ?? false; + const toggleAllRows = (): void => body.toggleAllFiltered?.(); + + const hasVisibleAssessments = useMemo( + () => + visibleCols.some( + (c) => parseAssessmentColumnId(c.id ?? (c.of as string)) !== null, + ), + [visibleCols], + ); + + const row1Ref = useRef(null); + const [row2Top, setRow2Top] = useState(0); + useLayoutEffect(() => { + setRow2Top(row1Ref.current?.offsetHeight ?? 0); + }, [visibleCols]); + + const headerFitsRef = useRef>({}); + const [headerFits, setHeaderFits] = useState>({}); + const onSingleLine = useCallback((id: string, fits: boolean): void => { + if (headerFitsRef.current[id] !== fits) { + headerFitsRef.current[id] = fits; + setHeaderFits((prev) => ({ ...prev, [id]: fits })); + } + }, []); + const singleLineCallbacks = useMemo( + () => + new Map( + visibleCols.map((c) => { + const id = c.id ?? (c.of as string); + return [id, (f: boolean): void => onSingleLine(id, f)]; + }), + ), + [visibleCols, onSingleLine], + ); + + return ( +
+ +
+ + {/* A bounded maxHeight is what makes `stickyHeader` actually stick: + `overflowX: 'auto'` already promotes this container to a scroll + container on both axes, so the sticky and the frozen + checkbox/Name columns pin relative to THIS element. Without a height + cap the container grows to fit every row and never scrolls + internally, leaving the header no scroll range. */} + + ({ + tableLayout: 'fixed', + borderCollapse: 'separate', + borderSpacing: 0, + + '& th, & td': { + boxSizing: 'border-box', + border: 0, + + // Draws the cell grid without relying on collapsed borders. + borderBottom: `0.5px solid ${theme.palette.grey[200]}`, + }, + })} + > + + + {visibleCols.map((c) => { + const id = c.id ?? (c.of as string); + return ; + })} + + + + ({ + top: 0, + zIndex: 3, + position: 'sticky', + left: 0, + bgcolor: 'background.default', + width: CHECKBOX_WIDTH, + minWidth: CHECKBOX_WIDTH, + maxWidth: CHECKBOX_WIDTH, + px: 0, + textAlign: 'center', + verticalAlign: 'middle', + // Solid 1px bottom seam under the frozen header columns. The + // table's 0.5px grid border (specificity 0,1,1) outranks a + // plain per-cell sx (0,1,0), so double the selector with `&&` + // (0,2,0) to win — and a full 1px survives sticky scroll + // compositing where 0.5px can drop and let the body show + // through the row1/row2 seam. + '&&': { + borderBottom: `1px solid ${theme.palette.grey[200]}`, + }, + })} + > + + + {visibleCols.map((c) => { + const id = c.id ?? (c.of as string); + const label = typeof c.title === 'string' ? c.title : id; + const isLeft = isLeftAligned(id); + const fits = headerFits[id] ?? false; + const sort = sortByColId.get(id); + const labelNode = ( + + + + + + ); + return ( + ({ + verticalAlign: isLeft || fits ? 'middle' : 'bottom', + ...(id === 'name' && { + position: 'sticky', + left: CHECKBOX_WIDTH, + zIndex: 3, + bgcolor: 'background.default', + // Right edge of the frozen region + matching 1px + // bottom seam. `&&` (0,2,0) is needed to beat the + // table's `& th` border rule (0,1,1). + '&&': { + borderRight: `1px solid ${theme.palette.grey[200]}`, + borderBottom: `1px solid ${theme.palette.grey[200]}`, + }, + }), + })} + > + {sort ? ( + + {labelNode} + + ) : ( + labelNode + )} + + ); + })} + + {hasVisibleAssessments && ( + ({ + '& .MuiTableCell-stickyHeader': { + top: row2Top, + }, + // Solid 1px bottom edge under the whole Max Marks row so the + // frozen columns read as a complete header block and the + // body never shows through on scroll. `& .MuiTableCell-root` + // (0,2,0) outranks the table's `& th` rule (0,1,1). + '& .MuiTableCell-root': { + borderTop: `1px solid ${theme.palette.grey[200]}`, + borderBottom: `1px solid ${theme.palette.grey[200]}`, + }, + })} + > + + {visibleCols.map((c) => { + const id = c.id ?? (c.of as string); + const asnId = parseAssessmentColumnId(id); + let cellContent: string = ''; + if (id === 'name') cellContent = t(translations.maxMarks); + else if (asnId !== null) { + const maxGrade = assessmentMaxGrades.get(asnId); + cellContent = maxGrade != null ? `/${maxGrade}` : ''; + } + return ( + ({ + bgcolor: 'grey.100', + ...(id === 'name' && { + position: 'sticky', + left: CHECKBOX_WIDTH, + zIndex: 3, + // Continue the frozen region's right edge. + '&&': { + borderTop: `1px solid ${theme.palette.grey[200]}`, + borderRight: `1px solid ${theme.palette.grey[200]}`, + }, + }), + })} + > + {cellContent} + + ); + })} + + )} + + + {body.rows.map((row, idx) => { + const rowProps = body.forEachRow(row, idx); + return ( + + ({ + position: 'sticky', + left: 0, + zIndex: 1, + bgcolor: 'background.paper', + width: CHECKBOX_WIDTH, + minWidth: CHECKBOX_WIDTH, + maxWidth: CHECKBOX_WIDTH, + px: 0, + textAlign: 'center', + // Sticky cells composite on their own layer, so this + // cell's `borderBottom` gets covered by the next row's + // opaque sticky background (Blink) — dropping the + // separator. Draw it as the lower row's `borderTop` + // instead; that border is owned by the row's own + // layer and always paints. Row 0's top edge is already + // the header cell's (higher z-index) bottom border. + borderBottom: 'none', + borderTop: + idx === 0 + ? undefined + : `0.5px solid ${theme.palette.grey[200]}`, + })} + > + + + {row + .getVisibleCells() + .filter((cell) => cell.column.id !== 'rowSelector') + .map((cell) => { + // Sticky cover for the frozen `name` column, mirroring + // the checkbox cell above. Declared as a directly-typed + // const so the callback is contextually typed (a ternary + // in the `sx` prop would strip that context). + const nameCellSx: SxProps = (theme) => ({ + position: 'sticky', + left: CHECKBOX_WIDTH, + zIndex: 1, + bgcolor: 'background.paper', + // Same sticky-layer cover as the checkbox column: draw + // the separator as the lower row's `borderTop`, not a + // covered `borderBottom`. + borderBottom: 'none', + borderTop: + idx === 0 + ? undefined + : `0.5px solid ${theme.palette.grey[200]}`, + // Continue the frozen region's right edge down the data + // rows. `&&` (0,2,0) beats the table's `& td` border + // rule (0,1,1). + '&&': { + borderRight: `1px solid ${theme.palette.grey[200]}`, + }, + }); + return ( + + {flexRender( + cell.column.columnDef.cell, + cell.getContext(), + )} + + ); + })} + + ); + })} + +
+
+ {pagination && } +
+
+
+ ); +}; + +export default GradebookTable; diff --git a/client/app/bundles/course/gradebook/components/buildAssessmentColumnIds.ts b/client/app/bundles/course/gradebook/components/buildAssessmentColumnIds.ts new file mode 100644 index 00000000000..d12a4bd26a7 --- /dev/null +++ b/client/app/bundles/course/gradebook/components/buildAssessmentColumnIds.ts @@ -0,0 +1,7 @@ +export const buildAssessmentColumnId = (asnId: number): string => + `asn-${asnId}`; + +export const parseAssessmentColumnId = (colId: string): number | null => { + const match = colId.match(/^asn-(\d+)$/); + return match ? Number(match[1]) : null; +}; diff --git a/client/app/bundles/course/gradebook/constants.ts b/client/app/bundles/course/gradebook/constants.ts new file mode 100644 index 00000000000..4c61d2723fb --- /dev/null +++ b/client/app/bundles/course/gradebook/constants.ts @@ -0,0 +1,5 @@ +export const STUDENT_INFO_COL_IDS = ['name', 'email', 'externalId'] as const; +export type StudentInfoColId = (typeof STUDENT_INFO_COL_IDS)[number]; + +export const GAMIFICATION_COL_IDS = ['level', 'totalXp'] as const; +export type GamificationColId = (typeof GAMIFICATION_COL_IDS)[number]; diff --git a/client/app/bundles/course/gradebook/handles.ts b/client/app/bundles/course/gradebook/handles.ts new file mode 100644 index 00000000000..0022bfbd02c --- /dev/null +++ b/client/app/bundles/course/gradebook/handles.ts @@ -0,0 +1,21 @@ +import { defineMessages } from 'react-intl'; + +import type { CrumbPath, DataHandle } from 'lib/hooks/router/dynamicNest'; + +const translations = defineMessages({ + header: { + id: 'course.gradebook.GradebookIndex.gradebook', + defaultMessage: 'Gradebook', + }, +}); + +export const gradebookHandle: DataHandle = (match) => { + const courseId = match.params.courseId; + + return { + getData: async (): Promise => ({ + activePath: `/courses/${courseId}/gradebook`, + content: { title: translations.header }, + }), + }; +}; diff --git a/client/app/bundles/course/gradebook/operations.ts b/client/app/bundles/course/gradebook/operations.ts new file mode 100644 index 00000000000..35790580ed0 --- /dev/null +++ b/client/app/bundles/course/gradebook/operations.ts @@ -0,0 +1,12 @@ +import type { Operation } from 'store'; + +import CourseAPI from 'api/course'; + +import { actions } from './store'; + +const fetchGradebook = (): Operation => async (dispatch) => { + const response = await CourseAPI.gradebook.index(); + dispatch(actions.saveGradebook(response.data)); +}; + +export default fetchGradebook; diff --git a/client/app/bundles/course/gradebook/pages/GradebookIndex/index.tsx b/client/app/bundles/course/gradebook/pages/GradebookIndex/index.tsx new file mode 100644 index 00000000000..6d90823b5e4 --- /dev/null +++ b/client/app/bundles/course/gradebook/pages/GradebookIndex/index.tsx @@ -0,0 +1,106 @@ +import { FC, useEffect, useState } from 'react'; +import { defineMessages } from 'react-intl'; +import { useParams } from 'react-router-dom'; +import { PeopleAlt } from '@mui/icons-material'; +import { Typography } from '@mui/material'; + +import Page from 'lib/components/core/layouts/Page'; +import LoadingIndicator from 'lib/components/core/LoadingIndicator'; +import { useAppDispatch, useAppSelector } from 'lib/hooks/store'; +import toast from 'lib/hooks/toast'; +import useTranslation from 'lib/hooks/useTranslation'; + +import { useCourseContext } from '../../../container/CourseLoader'; +import GradebookTable from '../../components/GradebookTable'; +import GradeLinkHint from '../../components/GradeLinkHint'; +import fetchGradebook from '../../operations'; +import { + getAssessments, + getCategories, + getGamificationEnabled, + getStudents, + getSubmissions, + getTabs, +} from '../../selectors'; + +const translations = defineMessages({ + gradebook: { + id: 'course.gradebook.GradebookIndex.gradebook', + defaultMessage: 'Gradebook', + }, + fetchFailure: { + id: 'course.gradebook.GradebookIndex.fetchFailure', + defaultMessage: 'Failed to retrieve Gradebook.', + }, + noStudents: { + id: 'course.gradebook.GradebookIndex.noStudents', + defaultMessage: 'No students enrolled yet', + }, + noStudentsHint: { + id: 'course.gradebook.GradebookIndex.noStudentsHint', + defaultMessage: 'Grades will appear here once students join the course.', + }, +}); + +const GradebookIndex: FC = () => { + const { t } = useTranslation(); + const dispatch = useAppDispatch(); + const { courseTitle } = useCourseContext(); + const { courseId: courseIdParam } = useParams(); + const courseId = parseInt(courseIdParam!, 10); + const [isLoading, setIsLoading] = useState(true); + + const assessments = useAppSelector(getAssessments); + const categories = useAppSelector(getCategories); + const tabs = useAppSelector(getTabs); + const students = useAppSelector(getStudents); + const submissions = useAppSelector(getSubmissions); + const gamificationEnabled = useAppSelector(getGamificationEnabled); + + useEffect(() => { + dispatch(fetchGradebook()) + .finally(() => setIsLoading(false)) + .catch(() => toast.error(t(translations.fetchFailure))); + }, [dispatch]); + + let content: JSX.Element; + if (isLoading) { + content = ; + } else if (students.length === 0) { + content = ( +
+ + + {t(translations.noStudents)} + + + {t(translations.noStudentsHint)} + +
+ ); + } else { + content = ( + <> + + + + ); + } + + return ( + + {content} + + ); +}; + +export default GradebookIndex; diff --git a/client/app/bundles/course/gradebook/selectors.ts b/client/app/bundles/course/gradebook/selectors.ts new file mode 100644 index 00000000000..fbe62e2611a --- /dev/null +++ b/client/app/bundles/course/gradebook/selectors.ts @@ -0,0 +1,24 @@ +import type { AppState } from 'store'; + +type GradebookState = AppState['gradebook']; + +function getLocalState(state: AppState): GradebookState { + return state.gradebook; +} + +export const getCategories = (state: AppState): GradebookState['categories'] => + getLocalState(state).categories; +export const getTabs = (state: AppState): GradebookState['tabs'] => + getLocalState(state).tabs; +export const getAssessments = ( + state: AppState, +): GradebookState['assessments'] => getLocalState(state).assessments; +export const getStudents = (state: AppState): GradebookState['students'] => + getLocalState(state).students; +export const getSubmissions = ( + state: AppState, +): GradebookState['submissions'] => getLocalState(state).submissions; +export const getGamificationEnabled = ( + state: AppState, +): GradebookState['gamificationEnabled'] => + getLocalState(state).gamificationEnabled; diff --git a/client/app/bundles/course/gradebook/store.ts b/client/app/bundles/course/gradebook/store.ts new file mode 100644 index 00000000000..00e3291032b --- /dev/null +++ b/client/app/bundles/course/gradebook/store.ts @@ -0,0 +1,63 @@ +import { produce } from 'immer'; +import type { GradebookData } from 'types/course/gradebook'; + +import type { + AssessmentData, + CategoryData, + StudentData, + SubmissionData, + TabData, +} from './types'; + +const SAVE_GRADEBOOK = 'course/gradebook/SAVE_GRADEBOOK'; + +interface GradebookState { + categories: CategoryData[]; + tabs: TabData[]; + assessments: AssessmentData[]; + students: StudentData[]; + submissions: SubmissionData[]; + gamificationEnabled: boolean; +} + +interface SaveGradebookAction { + type: typeof SAVE_GRADEBOOK; + payload: GradebookData; +} + +const initialState: GradebookState = { + categories: [], + tabs: [], + assessments: [], + students: [], + submissions: [], + gamificationEnabled: false, +}; + +const reducer = produce( + (draft: GradebookState, action: SaveGradebookAction) => { + switch (action.type) { + case SAVE_GRADEBOOK: { + draft.categories = action.payload.categories; + draft.tabs = action.payload.tabs; + draft.assessments = action.payload.assessments; + draft.students = action.payload.students; + draft.submissions = action.payload.submissions; + draft.gamificationEnabled = action.payload.gamificationEnabled; + break; + } + default: + break; + } + }, + initialState, +); + +export const actions = { + saveGradebook: (data: GradebookData): SaveGradebookAction => ({ + type: SAVE_GRADEBOOK, + payload: data, + }), +}; + +export default reducer; diff --git a/client/app/bundles/course/gradebook/types.ts b/client/app/bundles/course/gradebook/types.ts new file mode 100644 index 00000000000..f94aa7bf9c5 --- /dev/null +++ b/client/app/bundles/course/gradebook/types.ts @@ -0,0 +1,8 @@ +export type { + AssessmentData, + CategoryData, + GradebookData, + StudentData, + SubmissionData, + TabData, +} from 'types/course/gradebook'; diff --git a/client/app/bundles/course/statistics/operations.ts b/client/app/bundles/course/statistics/operations.ts index c2060ed41fe..1c071b810ef 100644 --- a/client/app/bundles/course/statistics/operations.ts +++ b/client/app/bundles/course/statistics/operations.ts @@ -1,8 +1,4 @@ -import { AxiosError } from 'axios'; -import { JobCompleted, JobErrored } from 'types/jobs'; - import CourseAPI from 'api/course'; -import pollJob from 'lib/helpers/jobHelpers'; import { AssessmentsStatistics, @@ -14,8 +10,6 @@ import { StudentsStatistics, } from './types'; -const DOWNLOAD_JOB_POLL_INTERVAL_MS = 2000; - export const fetchStatisticsIndex = async (): Promise => { const response = await CourseAPI.statistics.course.fetchStatisticsIndex(); return response.data; @@ -62,21 +56,3 @@ export const fetchCourseGetHelpActivity = async (params?: { await CourseAPI.statistics.course.fetchCourseGetHelpActivity(params); return response.data; }; - -export const downloadScoreSummary = ( - handleSuccess: (successData: JobCompleted) => void, - handleFailure: (error: JobErrored | AxiosError) => void, - assessmentIds: number[], -): void => { - CourseAPI.statistics.course - .downloadScoreSummary(assessmentIds) - .then((response) => { - pollJob( - response.data.jobUrl, - handleSuccess, - handleFailure, - DOWNLOAD_JOB_POLL_INTERVAL_MS, - ); - }) - .catch(handleFailure); -}; diff --git a/client/app/bundles/course/statistics/pages/StatisticsIndex/assessments/AssessmentsScoreSummaryDownload.tsx b/client/app/bundles/course/statistics/pages/StatisticsIndex/assessments/AssessmentsScoreSummaryDownload.tsx deleted file mode 100644 index 355bb97ee45..00000000000 --- a/client/app/bundles/course/statistics/pages/StatisticsIndex/assessments/AssessmentsScoreSummaryDownload.tsx +++ /dev/null @@ -1,115 +0,0 @@ -import { useState } from 'react'; -import { defineMessages } from 'react-intl'; -import { Button, Typography } from '@mui/material'; -import { AxiosError } from 'axios'; -import { JobCompleted, JobErrored } from 'types/jobs'; - -import { downloadScoreSummary } from 'course/statistics/operations'; -import { CourseAssessment } from 'course/statistics/types'; -import Prompt, { PromptText } from 'lib/components/core/dialogs/Prompt'; -import loadingToast, { LoadingToast } from 'lib/hooks/toast/loadingToast'; -import useTranslation from 'lib/hooks/useTranslation'; - -interface AssessmentsScoreSummaryDownloadProps { - assessments: CourseAssessment[]; -} - -const translations = defineMessages({ - selectedNUsers: { - id: 'course.statistics.StatisticsIndex.assessments.selectedNUsers', - defaultMessage: - 'Download Score Summary ({n, plural, =1 {# assessment} other {# assessments}})', - }, - download: { - id: 'course.statistics.StatisticsIndex.assessments.downloadCsv', - defaultMessage: 'Download', - }, - downloadCsvDialogTitle: { - id: 'course.statistics.StatisticsIndex.assessments.downloadCsv', - defaultMessage: 'Download Score Summary for the following Assessments?', - }, - downloadScoreSummarySuccess: { - id: 'course.statistics.StatisticsIndex.assessments.downloadScoreSummarySuccess', - defaultMessage: 'Successfully downloaded score summary', - }, - downloadScoreSummaryFailure: { - id: 'course.statistics.StatisticsIndex.assessments.downloadScoreSummaryFailure', - defaultMessage: 'An error occurred while downloading score summary', - }, - downloadScoreSummaryPending: { - id: 'course.statistics.StatisticsIndex.assessments.downloadScoreSummaryPending', - defaultMessage: - 'Please wait as your request to download is being processed', - }, -}); - -const AssessmentsScoreSummaryDownload = ( - props: AssessmentsScoreSummaryDownloadProps, -): JSX.Element => { - const { assessments } = props; - const { t } = useTranslation(); - - const [openDialog, setOpenDialog] = useState(false); - const [isDownloading, setIsDownloading] = useState(false); - - const handleSuccess = - (loadToast: LoadingToast) => - (successData: JobCompleted): void => { - window.location.href = successData.redirectUrl!; - loadToast.success(t(translations.downloadScoreSummarySuccess)); - setIsDownloading(false); - setOpenDialog(false); - }; - - const handleFailure = - (loadToast: LoadingToast) => - (error: JobErrored | AxiosError): void => { - const message = - error?.message || t(translations.downloadScoreSummaryFailure); - loadToast.error(message); - setIsDownloading(false); - }; - - const handleOnClick = (): void => { - setIsDownloading(true); - const loadToast = loadingToast(t(translations.downloadScoreSummaryPending)); - downloadScoreSummary( - handleSuccess(loadToast), - handleFailure(loadToast), - assessments.map((assessment) => assessment.id), - ); - }; - - return ( - <> - - - setOpenDialog(false)} - open={openDialog} - primaryColor="info" - primaryLabel={t(translations.download)} - title={t(translations.downloadCsvDialogTitle)} - > - - {assessments.map((assessment) => ( -
  • {assessment.title}
  • - ))} -
    -
    - - ); -}; - -export default AssessmentsScoreSummaryDownload; diff --git a/client/app/bundles/course/statistics/pages/StatisticsIndex/assessments/AssessmentsStatistics.tsx b/client/app/bundles/course/statistics/pages/StatisticsIndex/assessments/AssessmentsStatistics.tsx index f8a8a4ed78a..930eef2ed5d 100644 --- a/client/app/bundles/course/statistics/pages/StatisticsIndex/assessments/AssessmentsStatistics.tsx +++ b/client/app/bundles/course/statistics/pages/StatisticsIndex/assessments/AssessmentsStatistics.tsx @@ -12,6 +12,7 @@ const AssessmentsStatistics: FC = () => { {(data) => ( )} diff --git a/client/app/bundles/course/statistics/pages/StatisticsIndex/assessments/AssessmentsStatisticsTable.tsx b/client/app/bundles/course/statistics/pages/StatisticsIndex/assessments/AssessmentsStatisticsTable.tsx index 0b686edd539..450e1208850 100644 --- a/client/app/bundles/course/statistics/pages/StatisticsIndex/assessments/AssessmentsStatisticsTable.tsx +++ b/client/app/bundles/course/statistics/pages/StatisticsIndex/assessments/AssessmentsStatisticsTable.tsx @@ -1,6 +1,6 @@ import { FC } from 'react'; import { defineMessages } from 'react-intl'; -import { Typography } from '@mui/material'; +import { Box, Typography } from '@mui/material'; import { CourseAssessment } from 'course/statistics/types'; import Link from 'lib/components/core/Link'; @@ -14,13 +14,12 @@ import { getAssessmentStatisticsURL, getAssessmentWithCategoryURL, getAssessmentWithTabURL, + getCourseGradebookURL, } from 'lib/helpers/url-builders'; import { getCourseId } from 'lib/helpers/url-helpers'; import useTranslation from 'lib/hooks/useTranslation'; import { formatMiniDateTime, formatSecondsDuration } from 'lib/moment'; -import AssessmentsScoreSummaryDownload from './AssessmentsScoreSummaryDownload'; - const translations = defineMessages({ title: { id: 'course.statistics.StatisticsIndex.assessments.title', @@ -78,15 +77,26 @@ const translations = defineMessages({ id: 'course.statistics.StatisticsIndex.assessments.searchBar', defaultMessage: 'Search by Assessment Title, Tab, or Category', }, + subtitle: { + id: 'course.statistics.StatisticsIndex.assessments.subtitle', + defaultMessage: + 'To view and export individual student grades, open Gradebook.', + }, + subtitleDisabled: { + id: 'course.statistics.StatisticsIndex.assessments.subtitleDisabled', + defaultMessage: + 'To view and export individual student grades, enable Gradebook.', + }, }); interface Props { numStudents: number; assessments: CourseAssessment[]; + gradebookEnabled: boolean; } const AssessmentsStatisticsTable: FC = (props) => { - const { numStudents, assessments } = props; + const { numStudents, assessments, gradebookEnabled } = props; const courseId = getCourseId(); const { t } = useTranslation(); @@ -243,9 +253,26 @@ const AssessmentsStatisticsTable: FC = (props) => { return ( <> - - {t(translations.tableTitle, { numStudents })} - + + + {t(translations.tableTitle, { numStudents })} + + + {gradebookEnabled + ? t(translations.subtitle, { + url: (chunks) => ( + {chunks} + ), + }) + : t(translations.subtitleDisabled, { + url: (chunks) => ( + + {chunks} + + ), + })} + + = (props) => { } getRowEqualityData={(assessment): CourseAssessment => assessment} getRowId={(assessment): string => assessment.id.toString()} - indexing={{ indices: true, rowSelectable: true }} + indexing={{ indices: true }} pagination={{ rowsPerPage: [DEFAULT_TABLE_ROWS_PER_PAGE], showAllRows: true, @@ -285,15 +312,7 @@ const AssessmentsStatisticsTable: FC = (props) => { }, }, }} - toolbar={{ - show: true, - activeToolbar: (selectedAssessments): JSX.Element => ( - - ), - keepNative: true, - }} + toolbar={{ show: true }} /> ); diff --git a/client/app/bundles/course/statistics/pages/StatisticsIndex/assessments/__tests__/AssessmentsStatisticsTable.test.tsx b/client/app/bundles/course/statistics/pages/StatisticsIndex/assessments/__tests__/AssessmentsStatisticsTable.test.tsx new file mode 100644 index 00000000000..5eb96b0bd7b --- /dev/null +++ b/client/app/bundles/course/statistics/pages/StatisticsIndex/assessments/__tests__/AssessmentsStatisticsTable.test.tsx @@ -0,0 +1,58 @@ +import { render, screen } from 'test-utils'; + +import { CourseAssessment } from 'course/statistics/types'; + +import AssessmentsStatisticsTable from '../AssessmentsStatisticsTable'; + +const assessments: CourseAssessment[] = [ + { + id: 1, + title: 'Quiz 1', + startAt: new Date('2026-01-01T00:00:00Z'), + tab: { id: 1, title: 'Tab 1' }, + category: { id: 1, title: 'Category 1' }, + maximumGrade: 10, + numSubmitted: 2, + numAttempted: 3, + numLate: 1, + }, +]; + +const renderTable = (gradebookEnabled = true): void => { + render( + , + { at: ['/courses/1/statistics/assessments'] }, + ); +}; + +describe('', () => { + // Regression guard: an orphaned rowSelectable (selection with no activeToolbar) + // makes the toolbar render an empty 6.5rem bar on select, shifting every row down. + // Re-enabling row selection here brings that layout bug back. + it('keeps row selection off so selecting cannot shift the table down', async () => { + renderTable(); + await screen.findByText('Quiz 1'); + expect(screen.queryByRole('checkbox')).not.toBeInTheDocument(); + }); + + it('renders the native CSV download button', async () => { + renderTable(); + expect(await screen.findByTestId('DownloadIcon')).toBeInTheDocument(); + }); + + it('points to the Gradebook for individual student grades when enabled', async () => { + renderTable(true); + const link = await screen.findByRole('link', { name: /gradebook/i }); + expect(link).toHaveAttribute('href', '/courses/1/gradebook'); + }); + + it('points to course settings to enable Gradebook when it is disabled', async () => { + renderTable(false); + const link = await screen.findByRole('link', { name: /gradebook/i }); + expect(link).toHaveAttribute('href', '/courses/1/admin/components'); + }); +}); diff --git a/client/app/bundles/course/statistics/types.ts b/client/app/bundles/course/statistics/types.ts index 8dea0dfc4bd..076743f504c 100644 --- a/client/app/bundles/course/statistics/types.ts +++ b/client/app/bundles/course/statistics/types.ts @@ -137,6 +137,7 @@ export interface CourseAssessment { export interface AssessmentsStatistics { numStudents: number; assessments: CourseAssessment[]; + gradebookEnabled: boolean; } export interface CourseGetHelpActivity { diff --git a/client/app/bundles/course/translations.ts b/client/app/bundles/course/translations.ts index b92b165a744..c52ce359071 100644 --- a/client/app/bundles/course/translations.ts +++ b/client/app/bundles/course/translations.ts @@ -75,6 +75,10 @@ const translations = defineMessages({ id: 'course.componentTitles.course_forums_component', defaultMessage: 'Forums', }, + course_gradebook_component: { + id: 'course.componentTitles.course_gradebook_component', + defaultMessage: 'Gradebook', + }, course_groups_component: { id: 'course.componentTitles.course_groups_component', defaultMessage: 'Groups', diff --git a/client/app/bundles/course/user-invitations/components/tables/InvitationResultExistingTable.tsx b/client/app/bundles/course/user-invitations/components/tables/InvitationResultExistingTable.tsx index 51915776959..9f517937cf0 100644 --- a/client/app/bundles/course/user-invitations/components/tables/InvitationResultExistingTable.tsx +++ b/client/app/bundles/course/user-invitations/components/tables/InvitationResultExistingTable.tsx @@ -7,7 +7,6 @@ import { ColumnTemplate } from 'lib/components/table'; import Table from 'lib/components/table/Table'; import { DEFAULT_MINI_TABLE_ROWS_PER_PAGE, - DEFAULT_TABLE_ROWS_PER_PAGE, TIMELINE_ALGORITHMS, } from 'lib/constants/sharedConstants'; import useTranslation from 'lib/hooks/useTranslation'; diff --git a/client/app/bundles/course/user-invitations/components/tables/InvitationResultPrimaryTable.tsx b/client/app/bundles/course/user-invitations/components/tables/InvitationResultPrimaryTable.tsx index 3da6e4d2da0..5b2ad57a8ae 100644 --- a/client/app/bundles/course/user-invitations/components/tables/InvitationResultPrimaryTable.tsx +++ b/client/app/bundles/course/user-invitations/components/tables/InvitationResultPrimaryTable.tsx @@ -5,7 +5,6 @@ import { ColumnTemplate } from 'lib/components/table'; import Table from 'lib/components/table/Table'; import { DEFAULT_MINI_TABLE_ROWS_PER_PAGE, - DEFAULT_TABLE_ROWS_PER_PAGE, TIMELINE_ALGORITHMS, } from 'lib/constants/sharedConstants'; import useTranslation from 'lib/hooks/useTranslation'; diff --git a/client/app/bundles/users/store.ts b/client/app/bundles/users/store.ts index 230d7149d51..44ff5de5596 100644 --- a/client/app/bundles/users/store.ts +++ b/client/app/bundles/users/store.ts @@ -12,6 +12,8 @@ import { SaveCourseListAction, SaveInstanceListAction, SaveUserAction, + SET_CURRENT_USER_ID, + SetCurrentUserIdAction, } from './types'; const initialState: GlobalUserState = { @@ -33,6 +35,13 @@ const reducer = produce((draft: GlobalUserState, action: GlobalActionType) => { draft.user = userEntity; break; } + // Sets only the authenticated user's id, leaving name/imageUrl untouched. + // The course layout fetch knows the current user id but not the full profile; + // this is enough to namespace per-user client state (e.g. table column prefs). + case SET_CURRENT_USER_ID: { + draft.user.id = action.userId; + break; + } case SAVE_COURSE_LIST: { if (action.courses) { const coursesList = action.courses; @@ -67,6 +76,10 @@ export const actions = { return { type: SAVE_USER, user }; }, + setCurrentUserId: (userId: number): SetCurrentUserIdAction => { + return { type: SET_CURRENT_USER_ID, userId }; + }, + saveCourses: ( courses: UserCourseListData[], courseType: 'current' | 'completed', diff --git a/client/app/bundles/users/types.ts b/client/app/bundles/users/types.ts index f4cf9580e59..6a073977c78 100644 --- a/client/app/bundles/users/types.ts +++ b/client/app/bundles/users/types.ts @@ -12,6 +12,7 @@ import { // Action Names export const SAVE_USER = 'system/SAVE_USER'; +export const SET_CURRENT_USER_ID = 'system/SET_CURRENT_USER_ID'; export const SAVE_COURSE_LIST = 'system/SAVE_COURSE_LIST'; export const SAVE_INSTANCE_LIST = 'system/SAVE_INSTANCE_LIST'; @@ -21,6 +22,11 @@ export interface SaveUserAction { user: UserBasicListData; } +export interface SetCurrentUserIdAction { + type: typeof SET_CURRENT_USER_ID; + userId: number; +} + export interface SaveCourseListAction { type: typeof SAVE_COURSE_LIST; courses: UserCourseListData[]; @@ -34,6 +40,7 @@ export interface SaveInstanceListAction { export type GlobalActionType = | SaveUserAction + | SetCurrentUserIdAction | SaveCourseListAction | SaveInstanceListAction; diff --git a/client/app/lib/components/core/buttons/DownloadButton.tsx b/client/app/lib/components/core/buttons/DownloadButton.tsx index f241597144d..a9788884b08 100644 --- a/client/app/lib/components/core/buttons/DownloadButton.tsx +++ b/client/app/lib/components/core/buttons/DownloadButton.tsx @@ -24,7 +24,7 @@ const DownloadButton = (props: DownloadButtonProps): JSX.Element => ( > - {props.children} + {props.children} diff --git a/client/app/lib/components/core/dialogs/Prompt.tsx b/client/app/lib/components/core/dialogs/Prompt.tsx index 5330d10c7c4..32f4c7f251a 100644 --- a/client/app/lib/components/core/dialogs/Prompt.tsx +++ b/client/app/lib/components/core/dialogs/Prompt.tsx @@ -15,6 +15,7 @@ interface BasePromptProps { open?: boolean; title?: string | ReactNode; children?: string | ReactNode; + footer?: ReactNode; onClose?: () => void; onClosed?: () => void; disabled?: boolean; @@ -84,6 +85,8 @@ const Prompt = (props: PromptProps): JSX.Element => { )} + {props.footer} + {!props.cancel ? ( + )} + + {toolbar.onDirectExport && ( + + + + + + )} + + + + {showingDefaults && ( + + {canManageWeights + ? t(translations.defaultWeights) + : t(translations.defaultWeightsNoAccess)} + + )} + {allWeightsZero && !showingDefaults && ( + + {canManageWeights + ? t(translations.noWeightsConfigured) + : t(translations.noWeightsNoAccess)} + + )} + ({ + maxHeight: 'calc(100vh - 22rem)', + overflowX: 'auto', + borderTop: `1px solid ${theme.palette.grey[400]}`, + borderLeft: `1px solid ${theme.palette.grey[400]}`, + borderRight: `1px solid ${theme.palette.grey[400]}`, + })} + > +
    { + const line = theme.palette.grey[400]; + const right = `inset -1px 0 0 ${line}`; + const bottom = `inset 0 -1px 0 ${line}`; + const groupRight = `inset -2px 0 0 ${line}`; + return { + tableLayout: 'fixed', + borderCollapse: 'separate', + borderSpacing: 0, + '& th, & td': { + boxSizing: 'border-box', + border: 0, + boxShadow: `${right}, ${bottom}`, + py: 0.25, + px: 1, + lineHeight: 1.2, + height: 32, + overflow: 'hidden', + textOverflow: 'ellipsis', + }, + '& [data-group-end]': { + boxShadow: `${groupRight}, ${bottom}`, + }, + }; + }} + > + + + + {showEmail && } + {showExternalId && } + {resolvedTabs.map((tab) => ( + + ))} + + + + + + + + + {t(tableTranslations.name)} + + {showEmail && ( + + {t(tableTranslations.email)} + + )} + {showExternalId && ( + + {t(tableTranslations.externalId)} + + )} + {visibleCategories.map((cat) => ( + + {cat.title} + + ))} + + + {t(translations.total)} + + + + + + + + + + + {resolvedTabs.map((tab, i) => ( + + + {tab.title} + + + ))} + + + + {resolvedTabs.map((tab, i) => ( + + {tabSubheaderLabel(tab)} + + ))} + + {totalWeight === 100 ? ( + totalWeightHeaderLabel + ) : ( + + + {displayMode === 'percent' + ? t(translations.percentTotalWarning, { + weight: totalWeight, + }) + : t(translations.outOfWeight, { + weight: totalWeight, + })} + + + )} + + + + + + {body.rows.map((row, idx) => { + const rowProps = body.forEachRow(row, idx); + const studentId = row.original.studentId; + const isExpanded = expandedIds.has(studentId); + return ( + + + + + + + toggleExpanded(studentId)} + size="small" + sx={{ + mr: 0.5, + p: 0.25, + }} + > + {isExpanded ? ( + + ) : ( + + )} + + + {row.original.name} + + + {showEmail && ( + + + {row.original.email} + + + )} + {showExternalId && ( + + + {row.original.externalId ?? ''} + + + )} + {row.original.subtotals.map((subtotal, i) => { + const weight = resolvedTabs[i].gradebookWeight ?? 0; + return ( + + {fmtDisplay( + tabDisplayValue(subtotal, weight), + columnPrecisions.tabs[i], + )} + + ); + })} + + {fmtDisplay( + totalDisplayValue(row.original.total), + columnPrecisions.total, + )} + + + {isExpanded && + (breakdownsByStudent.get(studentId) ?? []).flatMap( + (tb, tabIdx) => + tb.assessments.map((a) => { + const isExcluded = a.excluded; + const weightText = t( + translations.percentOfGrade, + { + weight: + Math.round(a.effectiveWeight * 100) / 100, + }, + ); + const gradeText = + a.grade === null + ? `-/${a.maxGrade}` + : `${a.grade}/${a.maxGrade}`; + return ( + + + + + + {a.title} + + + + {`${gradeText} · ${isExcluded ? t(translations.excluded) : weightText}`} + + + {showEmail && ( + + )} + {showExternalId && ( + + )} + {resolvedTabs.map((tab, i) => { + const tabCellValue = isExcluded + ? '—' + : fmtDisplay( + breakdownDisplayValue(a), + columnPrecisions.tabs[i], + ); + return ( + + {i === tabIdx ? tabCellValue : ''} + + ); + })} + + + ); + }), + )} + + ); + })} + +
    + + {pagination && } + + + + {canManageWeights && ( + setConfigureOpen(false)} + open={configureOpen} + tabs={tabs} + /> + )} + + {toolbar.columnPicker && toolbar.commitColumnVisibility && ( + setPickerOpen(false)} + open={pickerOpen} + /> + )} + + ); +}; + +export default WeightedGradebookTable; diff --git a/client/app/bundles/course/gradebook/components/WeightedViewHint.tsx b/client/app/bundles/course/gradebook/components/WeightedViewHint.tsx new file mode 100644 index 00000000000..ad14463ad19 --- /dev/null +++ b/client/app/bundles/course/gradebook/components/WeightedViewHint.tsx @@ -0,0 +1,65 @@ +import { FC } from 'react'; +import { defineMessages } from 'react-intl'; +import { Alert, Typography } from '@mui/material'; + +import { getUserEntity } from 'bundles/users/selectors'; +import Link from 'lib/components/core/Link'; +import { useAppSelector } from 'lib/hooks/store'; +import useDismissibleOnce from 'lib/hooks/useDismissibleOnce'; +import useTranslation from 'lib/hooks/useTranslation'; + +export const WEIGHTED_VIEW_HINT_KEY = 'gradebook_weighted_view_hint'; + +const translations = defineMessages({ + hint: { + id: 'course.gradebook.WeightedViewHint.hint', + defaultMessage: + 'Want to set how much each item contributes to students’ total grades? Enable Weighted Total view in {link}.', + }, + settingsLink: { + id: 'course.gradebook.WeightedViewHint.settingsLink', + defaultMessage: 'Gradebook settings', + }, +}); + +interface Props { + courseId: number; +} + +/** + * One-time, dismissable nudge shown to managers in the (always-visible) gradebook view + * when the weighted view is turned off. It advertises the capability and links to the + * setting that enables it, since that setting is otherwise buried in course admin. + * Dismissal is remembered per user via localStorage (see useDismissibleOnce). + */ +const WeightedViewHint: FC = ({ courseId }) => { + const { t } = useTranslation(); + const userId = useAppSelector(getUserEntity).id; + const { dismissed, dismiss } = useDismissibleOnce( + WEIGHTED_VIEW_HINT_KEY, + userId, + ); + + if (dismissed) return null; + + return ( +
    + + + {t(translations.hint, { + link: ( + + {t(translations.settingsLink)} + + ), + })} + + +
    + ); +}; + +export default WeightedViewHint; diff --git a/client/app/bundles/course/gradebook/computeWeighted.ts b/client/app/bundles/course/gradebook/computeWeighted.ts new file mode 100644 index 00000000000..4eea81960b7 --- /dev/null +++ b/client/app/bundles/course/gradebook/computeWeighted.ts @@ -0,0 +1,319 @@ +// client/app/bundles/course/gradebook/computeWeighted.ts +import { + AssessmentData, + StudentData, + SubmissionData, + TabData, +} from 'types/course/gradebook'; + +type GradeEntry = Pick; + +export interface WeightedRow { + studentId: number; + name: string; + email: string; + externalId: string | null; + subtotals: (number | null)[]; + total: number | null; +} + +export interface AssessmentContribution { + assessmentId: number; + title: string; + grade: number | null; + maxGrade: number; + points: number; // contribution to this tab's weighted-points cell + // Share of the overall grade this assessment carries, in percentage points. + // Equal mode: the tab's weight split evenly across its assessments. + // Custom mode: the assessment's own configured weight. + effectiveWeight: number; + excluded: boolean; +} + +export interface TabBreakdown { + tabId: number; + assessments: AssessmentContribution[]; +} + +type GradeLookup = Map; + +const gradeKey = (studentId: number, assessmentId: number): string => + `${studentId}:${assessmentId}`; + +// Fraction earned on an assessment. A maxGrade of 0 (e.g. an ungraded 0-point +// assignment) would make grade/maxGrade an NaN; coerce that to 0 so it never +// poisons subtotals, totals or the breakdown. +export const gradeRatio = (grade: number, maxGrade: number): number => + maxGrade === 0 ? 0 : grade / maxGrade; + +// Index submissions by (student, assessment) once: O(submissions). +const buildGradeLookup = (submissions: GradeEntry[]): GradeLookup => { + const lookup: GradeLookup = new Map(); + submissions.forEach((s) => { + if (s.grade != null) + lookup.set(gradeKey(s.studentId, s.assessmentId), s.grade); + }); + return lookup; +}; + +// Group assessments by tab once: O(assessments). +const buildAssessmentsByTab = ( + assessments: AssessmentData[], +): Map => { + const byTab = new Map(); + assessments.forEach((a) => { + const list = byTab.get(a.tabId); + if (list) list.push(a); + else byTab.set(a.tabId, [a]); + }); + return byTab; +}; + +// Equal-weight formula: average of (grade/maxGrade) ratios over INCLUDED assessments. +// Excluded assessments are dropped from both numerator and count; ungraded included +// contribute 0. Returns null when no assessment is included. +const equalSubtotal = ( + studentId: number, + tabAssessments: AssessmentData[], + gradeLookup: GradeLookup, +): number | null => { + const included = tabAssessments.filter((a) => !a.gradebookExcluded); + if (included.length === 0) return null; + const ratios = included.map((a) => { + const grade = gradeLookup.get(gradeKey(studentId, a.id)); + return grade != null ? gradeRatio(grade, a.maxGrade) : 0; + }); + return ratios.reduce((acc, r) => acc + r, 0) / ratios.length; +}; + +// Custom-weight formula: Σ(grade_i/maxGrade_i × assessmentWeight_i) / tabWeight over +// INCLUDED assessments. Returns null if tabWeight=0 or no assessment is included; +// ungraded included assessments contribute 0. +const customSubtotal = ( + studentId: number, + tab: TabData, + tabAssessments: AssessmentData[], + gradeLookup: GradeLookup, +): number | null => { + const tabWeight = tab.gradebookWeight ?? 0; + if (tabWeight === 0) return null; + let numerator = 0; + let hasContributing = false; + tabAssessments.forEach((a) => { + if (a.gradebookExcluded) return; + const grade = gradeLookup.get(gradeKey(studentId, a.id)); + const assessmentWeight = a.gradebookWeight ?? 0; + if (grade != null) + numerator += gradeRatio(grade, a.maxGrade) * assessmentWeight; + hasContributing = true; + }); + return hasContributing ? numerator / tabWeight : null; +}; + +// Single source of truth for the subtotal math, operating on prebuilt indexes. +const subtotalFromLookup = ( + studentId: number, + tab: TabData, + tabAssessments: AssessmentData[] | undefined, + gradeLookup: GradeLookup, +): number | null => { + if (!tabAssessments || tabAssessments.length === 0) return null; + if (tab.weightMode === 'custom') { + return customSubtotal(studentId, tab, tabAssessments, gradeLookup); + } + return equalSubtotal(studentId, tabAssessments, gradeLookup); +}; + +// Weighted, additive total from already-computed subtotals. +const totalFromSubtotals = ( + subtotals: (number | null)[], + tabs: TabData[], +): number | null => { + let contributingCount = 0; + let total = 0; + subtotals.forEach((sub, i) => { + if (sub == null) return; + contributingCount += 1; + total += (tabs[i].gradebookWeight ?? 0) * sub; + }); + return contributingCount > 0 ? total : null; +}; + +interface SubtotalArgs { + studentId: number; + tab: TabData; + assessments: AssessmentData[]; + submissions: GradeEntry[]; +} + +export const computeTabSubtotal = ({ + studentId, + tab, + assessments, + submissions, +}: SubtotalArgs): number | null => + subtotalFromLookup( + studentId, + tab, + assessments.filter((a) => a.tabId === tab.id), + buildGradeLookup(submissions), + ); + +interface TotalArgs { + studentId: number; + tabs: TabData[]; + assessments: AssessmentData[]; + submissions: GradeEntry[]; +} + +export const computeStudentTotal = ({ + studentId, + tabs, + assessments, + submissions, +}: TotalArgs): number | null => { + const gradeLookup = buildGradeLookup(submissions); + const assessmentsByTab = buildAssessmentsByTab(assessments); + const subtotals = tabs.map((tab) => + subtotalFromLookup( + studentId, + tab, + assessmentsByTab.get(tab.id), + gradeLookup, + ), + ); + return totalFromSubtotals(subtotals, tabs); +}; + +export const computeStudentBreakdown = ({ + studentId, + tabs, + assessments, + submissions, +}: TotalArgs): TabBreakdown[] => { + const gradeLookup = buildGradeLookup(submissions); + const assessmentsByTab = buildAssessmentsByTab(assessments); + return tabs.map((tab) => { + const list = assessmentsByTab.get(tab.id) ?? []; + const weight = tab.gradebookWeight ?? 0; + const includedCount = list.filter((a) => !a.gradebookExcluded).length; + + const contributions = list.map((a) => { + const excluded = !!a.gradebookExcluded; + const grade = gradeLookup.get(gradeKey(studentId, a.id)) ?? null; + const ratio = grade != null ? gradeRatio(grade, a.maxGrade) : 0; + let points: number; + let effectiveWeight: number; + if (excluded) { + points = 0; + effectiveWeight = 0; + } else if (tab.weightMode === 'custom') { + points = ratio * (a.gradebookWeight ?? 0); + effectiveWeight = a.gradebookWeight ?? 0; + } else { + points = includedCount > 0 ? (ratio / includedCount) * weight : 0; + effectiveWeight = includedCount > 0 ? weight / includedCount : 0; + } + return { + assessmentId: a.id, + title: a.title, + grade, + maxGrade: a.maxGrade, + points, + effectiveWeight, + excluded, + }; + }); + return { tabId: tab.id, assessments: contributions }; + }); +}; + +interface WeightedRowsArgs { + students: StudentData[]; + tabs: TabData[]; + assessments: AssessmentData[]; + submissions: GradeEntry[]; +} + +// Batch entry point used by the table: builds the indexes ONCE and reuses them +// across every student, computing each subtotal a single time. +export const computeWeightedRows = ({ + students, + tabs, + assessments, + submissions, +}: WeightedRowsArgs): WeightedRow[] => { + const gradeLookup = buildGradeLookup(submissions); + const assessmentsByTab = buildAssessmentsByTab(assessments); + return students.map((student) => { + const subtotals = tabs.map((tab) => + subtotalFromLookup( + student.id, + tab, + assessmentsByTab.get(tab.id), + gradeLookup, + ), + ); + return { + studentId: student.id, + name: student.name, + email: student.email, + externalId: student.externalId, + subtotals, + total: totalFromSubtotals(subtotals, tabs), + }; + }); +}; + +export const sumWeights = (tabs: TabData[]): number => + tabs.reduce((acc, t) => acc + (t.gradebookWeight ?? 0), 0); + +const r2 = (n: number): number => Math.round(n * 100) / 100; + +// Ids of tabs that have at least one assessment — only these are eligible for a +// default weight (an empty tab carries no grades, so weighting it is wasted). +const nonEmptyTabIds = ( + tabs: TabData[], + assessments: Pick[], +): number[] => { + const populated = new Set(assessments.map((a) => a.tabId)); + return tabs.filter((t) => populated.has(t.id)).map((t) => t.id); +}; + +// True when no tab weight has been configured (every weight is 0/null) yet there +// is at least one tab to weight — i.e. the table is showing the equal-split +// default rather than an instructor's configuration. Drives the "default weights" +// banner and dialog pre-fill so both read from one source of truth. +export const usingDefaultWeights = ( + tabs: TabData[], + assessments: Pick[], +): boolean => + sumWeights(tabs) === 0 && nonEmptyTabIds(tabs, assessments).length > 0; + +// When no weights are configured, distribute 100 equally across non-empty tabs so +// the weighted view is meaningful out of the box; the last such tab absorbs the +// rounding remainder so the result sums to exactly 100. Returns the input array +// unchanged (same reference) once any tab carries a weight, so a real configuration +// is never overwritten. +export const resolveTabWeights = ( + tabs: TabData[], + assessments: Pick[], +): TabData[] => { + if (sumWeights(tabs) !== 0) return tabs; + const ids = nonEmptyTabIds(tabs, assessments); + const n = ids.length; + if (n === 0) return tabs; + const base = r2(100 / n); + const weightById = new Map( + ids.map((id, i) => [id, i === n - 1 ? r2(100 - base * (n - 1)) : base]), + ); + return tabs.map((tab) => + weightById.has(tab.id) + ? { + ...tab, + gradebookWeight: weightById.get(tab.id), + weightMode: tab.weightMode ?? 'equal', + } + : tab, + ); +}; diff --git a/client/app/bundles/course/gradebook/operations.ts b/client/app/bundles/course/gradebook/operations.ts index 35790580ed0..ae2f962fbbf 100644 --- a/client/app/bundles/course/gradebook/operations.ts +++ b/client/app/bundles/course/gradebook/operations.ts @@ -1,4 +1,5 @@ import type { Operation } from 'store'; +import type { UpdateWeightsPayload } from 'types/course/gradebook'; import CourseAPI from 'api/course'; @@ -9,4 +10,11 @@ const fetchGradebook = (): Operation => async (dispatch) => { dispatch(actions.saveGradebook(response.data)); }; +export const updateGradebookWeights = + (weights: UpdateWeightsPayload['weights']): Operation => + async (dispatch) => { + const response = await CourseAPI.gradebook.updateWeights({ weights }); + dispatch(actions.updateTabWeights(response.data)); + }; + export default fetchGradebook; diff --git a/client/app/bundles/course/gradebook/pages/GradebookIndex/index.tsx b/client/app/bundles/course/gradebook/pages/GradebookIndex/index.tsx index 6d90823b5e4..095d3758fc3 100644 --- a/client/app/bundles/course/gradebook/pages/GradebookIndex/index.tsx +++ b/client/app/bundles/course/gradebook/pages/GradebookIndex/index.tsx @@ -1,8 +1,8 @@ -import { FC, useEffect, useState } from 'react'; +import { FC, useEffect, useState, useTransition } from 'react'; import { defineMessages } from 'react-intl'; -import { useParams } from 'react-router-dom'; +import { useParams, useSearchParams } from 'react-router-dom'; import { PeopleAlt } from '@mui/icons-material'; -import { Typography } from '@mui/material'; +import { Tab, Tabs, Typography } from '@mui/material'; import Page from 'lib/components/core/layouts/Page'; import LoadingIndicator from 'lib/components/core/LoadingIndicator'; @@ -13,14 +13,18 @@ import useTranslation from 'lib/hooks/useTranslation'; import { useCourseContext } from '../../../container/CourseLoader'; import GradebookTable from '../../components/GradebookTable'; import GradeLinkHint from '../../components/GradeLinkHint'; +import WeightedGradebookTable from '../../components/WeightedGradebookTable'; +import WeightedViewHint from '../../components/WeightedViewHint'; import fetchGradebook from '../../operations'; import { getAssessments, + getCanManageWeights, getCategories, getGamificationEnabled, getStudents, getSubmissions, getTabs, + getWeightedViewEnabled, } from '../../selectors'; const translations = defineMessages({ @@ -40,6 +44,14 @@ const translations = defineMessages({ id: 'course.gradebook.GradebookIndex.noStudentsHint', defaultMessage: 'Grades will appear here once students join the course.', }, + allAssessments: { + id: 'course.gradebook.GradebookIndex.allAssessments', + defaultMessage: 'All Assessments', + }, + byWeight: { + id: 'course.gradebook.GradebookIndex.byWeight', + defaultMessage: 'Weighted Total', + }, }); const GradebookIndex: FC = () => { @@ -48,7 +60,12 @@ const GradebookIndex: FC = () => { const { courseTitle } = useCourseContext(); const { courseId: courseIdParam } = useParams(); const courseId = parseInt(courseIdParam!, 10); + const [searchParams, setSearchParams] = useSearchParams(); const [isLoading, setIsLoading] = useState(true); + const [viewMode, setViewMode] = useState<'all' | 'weighted'>( + searchParams.get('view') === 'weighted' ? 'weighted' : 'all', + ); + const [isPending, startTransition] = useTransition(); const assessments = useAppSelector(getAssessments); const categories = useAppSelector(getCategories); @@ -56,6 +73,8 @@ const GradebookIndex: FC = () => { const students = useAppSelector(getStudents); const submissions = useAppSelector(getSubmissions); const gamificationEnabled = useAppSelector(getGamificationEnabled); + const weightedViewEnabled = useAppSelector(getWeightedViewEnabled); + const canManageWeights = useAppSelector(getCanManageWeights); useEffect(() => { dispatch(fetchGradebook()) @@ -78,27 +97,74 @@ const GradebookIndex: FC = () => { ); + } else if (weightedViewEnabled && viewMode === 'weighted') { + content = ( + + ); } else { content = ( - <> - - - + ); } return ( - {content} + {!isLoading && canManageWeights && !weightedViewEnabled && ( + + )} + {weightedViewEnabled && !isLoading && students.length > 0 && ( + + startTransition(() => { + setViewMode(v); + setSearchParams(v === 'weighted' ? { view: 'weighted' } : {}); + }) + } + TabIndicatorProps={{ style: { height: 2 } }} + value={viewMode} + > + + + + )} + {!isLoading && + students.length > 0 && + !(weightedViewEnabled && viewMode === 'weighted') && } +
    + {isPending && ( +
    + +
    + )} +
    + {content} +
    +
    ); }; diff --git a/client/app/bundles/course/gradebook/selectors.ts b/client/app/bundles/course/gradebook/selectors.ts index fbe62e2611a..0fb7d1398d5 100644 --- a/client/app/bundles/course/gradebook/selectors.ts +++ b/client/app/bundles/course/gradebook/selectors.ts @@ -22,3 +22,7 @@ export const getGamificationEnabled = ( state: AppState, ): GradebookState['gamificationEnabled'] => getLocalState(state).gamificationEnabled; +export const getWeightedViewEnabled = (state: AppState): boolean => + getLocalState(state).weightedViewEnabled; +export const getCanManageWeights = (state: AppState): boolean => + getLocalState(state).canManageWeights; diff --git a/client/app/bundles/course/gradebook/store.ts b/client/app/bundles/course/gradebook/store.ts index 00e3291032b..4777e16b3b6 100644 --- a/client/app/bundles/course/gradebook/store.ts +++ b/client/app/bundles/course/gradebook/store.ts @@ -1,5 +1,8 @@ import { produce } from 'immer'; -import type { GradebookData } from 'types/course/gradebook'; +import type { + GradebookData, + UpdateWeightsPayload, +} from 'types/course/gradebook'; import type { AssessmentData, @@ -10,6 +13,7 @@ import type { } from './types'; const SAVE_GRADEBOOK = 'course/gradebook/SAVE_GRADEBOOK'; +const UPDATE_TAB_WEIGHTS = 'course/gradebook/UPDATE_TAB_WEIGHTS'; interface GradebookState { categories: CategoryData[]; @@ -18,6 +22,8 @@ interface GradebookState { students: StudentData[]; submissions: SubmissionData[]; gamificationEnabled: boolean; + weightedViewEnabled: boolean; + canManageWeights: boolean; } interface SaveGradebookAction { @@ -25,6 +31,11 @@ interface SaveGradebookAction { payload: GradebookData; } +interface UpdateTabWeightsAction { + type: typeof UPDATE_TAB_WEIGHTS; + payload: UpdateWeightsPayload; +} + const initialState: GradebookState = { categories: [], tabs: [], @@ -32,10 +43,15 @@ const initialState: GradebookState = { students: [], submissions: [], gamificationEnabled: false, + weightedViewEnabled: false, + canManageWeights: false, }; const reducer = produce( - (draft: GradebookState, action: SaveGradebookAction) => { + ( + draft: GradebookState, + action: SaveGradebookAction | UpdateTabWeightsAction, + ) => { switch (action.type) { case SAVE_GRADEBOOK: { draft.categories = action.payload.categories; @@ -44,6 +60,43 @@ const reducer = produce( draft.students = action.payload.students; draft.submissions = action.payload.submissions; draft.gamificationEnabled = action.payload.gamificationEnabled; + draft.weightedViewEnabled = action.payload.weightedViewEnabled; + draft.canManageWeights = action.payload.canManageWeights; + break; + } + case UPDATE_TAB_WEIGHTS: { + action.payload.weights.forEach( + ({ + tabId, + weight, + weightMode, + assessmentWeights, + excludedAssessmentIds, + }) => { + const tab = draft.tabs.find((t) => t.id === tabId); + if (tab) { + tab.gradebookWeight = weight; + tab.weightMode = weightMode; + } + const excludedSet = new Set(excludedAssessmentIds ?? []); + const tabAssessments = draft.assessments.filter( + (a) => a.tabId === tabId, + ); + tabAssessments.forEach((a) => { + a.gradebookExcluded = excludedSet.has(a.id); + }); + if (weightMode === 'equal') { + tabAssessments.forEach((a) => { + a.gradebookWeight = null; + }); + } else if (assessmentWeights) { + assessmentWeights.forEach(({ assessmentId, weight: aw }) => { + const a = draft.assessments.find((x) => x.id === assessmentId); + if (a) a.gradebookWeight = aw; + }); + } + }, + ); break; } default: @@ -58,6 +111,12 @@ export const actions = { type: SAVE_GRADEBOOK, payload: data, }), + updateTabWeights: ( + payload: UpdateWeightsPayload, + ): UpdateTabWeightsAction => ({ + type: UPDATE_TAB_WEIGHTS, + payload, + }), }; export default reducer; diff --git a/client/app/bundles/course/gradebook/types.ts b/client/app/bundles/course/gradebook/types.ts index f94aa7bf9c5..b91689df872 100644 --- a/client/app/bundles/course/gradebook/types.ts +++ b/client/app/bundles/course/gradebook/types.ts @@ -5,4 +5,5 @@ export type { StudentData, SubmissionData, TabData, + UpdateWeightsPayload, } from 'types/course/gradebook'; diff --git a/client/app/lib/components/core/buttons/SegmentedSwitch.tsx b/client/app/lib/components/core/buttons/SegmentedSwitch.tsx new file mode 100644 index 00000000000..88956fae16b --- /dev/null +++ b/client/app/lib/components/core/buttons/SegmentedSwitch.tsx @@ -0,0 +1,222 @@ +import { + KeyboardEvent, + ReactNode, + useLayoutEffect, + useRef, + useState, +} from 'react'; +import { Box, ButtonBase, Tooltip } from '@mui/material'; + +export interface SegmentedSwitchOption { + value: T; + /** Visible content. Keep it short — one word reads best in this control. */ + label: ReactNode; + /** Optional hint shown on hover/focus of the segment. */ + tooltip?: ReactNode; + /** + * Accessible name for the segment. Falls back to `label` when that is a + * plain string; supply this when `label` is an icon or other non-text node. + */ + ariaLabel?: string; +} + +interface SegmentedSwitchProps { + /** The currently selected option's value. */ + value: T; + /** The options, left to right. Designed for 2 but renders any count. */ + options: SegmentedSwitchOption[]; + /** Fired with the next value when a different segment is chosen. */ + onChange: (value: T) => void; + /** Accessible name for the whole control (the radiogroup). */ + ariaLabel: string; + disabled?: boolean; + /** + * Pass `self-stretch` to grow the switch to a taller row neighbour (e.g. a + * small `TextField`), so the two align without hardcoding a height. + */ + className?: string; +} + +// Sized to MUI `size="small"` controls: 13px text, ~30.75px tall. Pixels are +// resolved through the theme's `pxToRem` so the switch matches its siblings +// regardless of the app's `htmlFontSize` (Coursemology uses 10, so a hardcoded +// rem would render ~38% too small). `MIN_HEIGHT` is a floor — `self-stretch` +// lets the switch grow to match a taller neighbour in the same flex row. +const FONT_PX = 13; +const PADDING_X = 1.5; +const MIN_HEIGHT = 30.75; + +/** + * A compact, peer-state mode switcher: a pill track with the options side by + * side and a single elevated thumb that slides to the active one. + * + * Unlike a `Switch`, neither option reads as "off" — both are equally valid — + * and unlike `ToggleButtonGroup` it stays content-width, so it fits a packed + * toolbar or a dense prompt row. Use it when a binary choice has no default + * "on" state (e.g. Points vs. Percentage, Equal vs. Custom). + */ +const SegmentedSwitch = ( + props: SegmentedSwitchProps, +): JSX.Element => { + const { value, options, onChange, ariaLabel, disabled, className } = props; + + const containerRef = useRef(null); + const optionRefs = useRef<(HTMLButtonElement | null)[]>([]); + const [thumb, setThumb] = useState<{ left: number; width: number }>({ + left: 0, + width: 0, + }); + + const activeIndex = Math.max( + 0, + options.findIndex((o) => o.value === value), + ); + + useLayoutEffect(() => { + const measure = (): void => { + const el = optionRefs.current[activeIndex]; + const container = containerRef.current; + if (!el || !container) return; + setThumb({ + left: el.offsetLeft - container.clientLeft, + width: el.offsetWidth, + }); + }; + measure(); + const observer = new ResizeObserver(measure); + if (containerRef.current) observer.observe(containerRef.current); + return () => observer.disconnect(); + }, [activeIndex, options.length]); + + const select = (index: number): void => { + const next = options[index]; + if (next && next.value !== value) onChange(next.value); + }; + + const handleKeyDown = (event: KeyboardEvent): void => { + if (disabled) return; + const last = options.length - 1; + let next = activeIndex; + if (event.key === 'ArrowRight' || event.key === 'ArrowDown') { + next = activeIndex === last ? 0 : activeIndex + 1; + } else if (event.key === 'ArrowLeft' || event.key === 'ArrowUp') { + next = activeIndex === 0 ? last : activeIndex - 1; + } else { + return; + } + event.preventDefault(); + select(next); + optionRefs.current[next]?.focus(); + }; + + return ( + ({ + position: 'relative', + display: 'inline-flex', + alignItems: 'stretch', + minHeight: MIN_HEIGHT, + boxSizing: 'border-box', + p: '3px', + borderRadius: 999, + bgcolor: theme.palette.action.hover, + border: `1px solid ${theme.palette.divider}`, + opacity: disabled ? 0.5 : 1, + pointerEvents: disabled ? 'none' : 'auto', + })} + > + ({ + position: 'absolute', + top: '3px', + bottom: '3px', + left: 0, + width: thumb.width, + borderRadius: 999, + bgcolor: theme.palette.background.paper, + boxShadow: theme.shadows[1], + opacity: thumb.width === 0 ? 0 : 1, + transform: `translateX(${thumb.left}px)`, + transition: theme.transitions.create(['transform', 'width'], { + duration: 260, + easing: 'cubic-bezier(0.34, 1.36, 0.64, 1)', + }), + zIndex: 0, + })} + /> + {options.map((option, index) => { + const selected = index === activeIndex; + const label = + option.ariaLabel ?? + (typeof option.label === 'string' ? option.label : undefined); + const segment = ( + { + optionRefs.current[index] = el; + }} + aria-checked={selected} + aria-label={label} + disabled={disabled} + disableRipple + onClick={() => select(index)} + role="radio" + sx={(theme) => ({ + position: 'relative', + zIndex: 1, + display: 'inline-flex', + alignItems: 'center', + justifyContent: 'center', + fontFamily: 'inherit', + fontSize: theme.typography.pxToRem(FONT_PX), + height: '100%', + fontWeight: selected ? 650 : 550, + letterSpacing: '0.01em', + color: selected + ? theme.palette.text.primary + : theme.palette.text.secondary, + px: PADDING_X, + py: 0, + borderRadius: 999, + whiteSpace: 'nowrap', + transition: theme.transitions.create('color', { duration: 180 }), + '&:hover': { color: theme.palette.text.primary }, + '&:focus-visible': { + outline: `2px solid ${theme.palette.primary.main}`, + outlineOffset: 2, + }, + })} + tabIndex={selected ? 0 : -1} + > + {option.label} + + ); + + return option.tooltip ? ( + + {segment} + + ) : ( + + {segment} + + ); + })} + + ); +}; + +export default SegmentedSwitch; diff --git a/client/app/lib/components/core/buttons/__test__/SegmentedSwitch.test.tsx b/client/app/lib/components/core/buttons/__test__/SegmentedSwitch.test.tsx new file mode 100644 index 00000000000..df09d449854 --- /dev/null +++ b/client/app/lib/components/core/buttons/__test__/SegmentedSwitch.test.tsx @@ -0,0 +1,131 @@ +import userEvent from '@testing-library/user-event'; +import { fireEvent, render, screen, within } from 'test-utils'; + +import SegmentedSwitch from '../SegmentedSwitch'; + +// Render synchronously without the real provider's locale-loading spinner +// (uses the manual mock at lib/components/wrappers/__mocks__/I18nProvider). +jest.mock('lib/components/wrappers/I18nProvider'); + +const OPTIONS = [ + { value: 'points', label: 'Points' }, + { value: 'percent', label: 'Percentage' }, +] as const; + +const setup = ( + value: 'points' | 'percent', + overrides: Partial< + Parameters>[0] + > = {}, +): { onChange: jest.Mock } => { + const onChange = jest.fn(); + render( + , + ); + return { onChange }; +}; + +describe('', () => { + it('renders a radiogroup with one radio per option, named by ariaLabel', () => { + setup('points'); + const group = screen.getByRole('radiogroup', { name: 'Display mode' }); + expect(within(group).getAllByRole('radio')).toHaveLength(2); + expect(screen.getByRole('radio', { name: 'Points' })).toBeInTheDocument(); + expect( + screen.getByRole('radio', { name: 'Percentage' }), + ).toBeInTheDocument(); + }); + + it('marks only the selected option aria-checked', () => { + setup('percent'); + expect(screen.getByRole('radio', { name: 'Points' })).toHaveAttribute( + 'aria-checked', + 'false', + ); + expect(screen.getByRole('radio', { name: 'Percentage' })).toHaveAttribute( + 'aria-checked', + 'true', + ); + }); + + it('keeps a single tab stop via roving tabindex', () => { + setup('points'); + expect(screen.getByRole('radio', { name: 'Points' })).toHaveAttribute( + 'tabindex', + '0', + ); + expect(screen.getByRole('radio', { name: 'Percentage' })).toHaveAttribute( + 'tabindex', + '-1', + ); + }); + + it('falls back to selecting the first option when value matches none', () => { + setup('points', { value: 'nonexistent' as 'points' | 'percent' }); + expect(screen.getByRole('radio', { name: 'Points' })).toHaveAttribute( + 'aria-checked', + 'true', + ); + expect(screen.getByRole('radio', { name: 'Percentage' })).toHaveAttribute( + 'aria-checked', + 'false', + ); + }); + + it('fires onChange with the chosen value when an inactive option is clicked', async () => { + const user = userEvent.setup(); + const { onChange } = setup('points'); + await user.click(screen.getByRole('radio', { name: 'Percentage' })); + expect(onChange).toHaveBeenCalledWith('percent'); + }); + + it('does not fire onChange when the already-active option is clicked', async () => { + const user = userEvent.setup(); + const { onChange } = setup('points'); + await user.click(screen.getByRole('radio', { name: 'Points' })); + expect(onChange).not.toHaveBeenCalled(); + }); + + it('shows a tooltip on hover for an option that supplies one', async () => { + const user = userEvent.setup(); + setup('points', { + options: [ + { value: 'points', label: 'Points', tooltip: 'Raw points earned' }, + { value: 'percent', label: 'Percentage' }, + ], + }); + await user.hover(screen.getByRole('radio', { name: 'Points' })); + expect(await screen.findByRole('tooltip')).toHaveTextContent( + 'Raw points earned', + ); + }); + + it('uses an explicit option ariaLabel as the radio accessible name', () => { + setup('points', { + options: [ + { value: 'points', label: 'Points', ariaLabel: 'Show as points' }, + { value: 'percent', label: 'Percentage' }, + ], + }); + expect( + screen.getByRole('radio', { name: 'Show as points' }), + ).toBeInTheDocument(); + expect( + screen.queryByRole('radio', { name: 'Points' }), + ).not.toBeInTheDocument(); + }); + + it('disables every option and suppresses onChange when disabled', () => { + const { onChange } = setup('points', { disabled: true }); + const percent = screen.getByRole('radio', { name: 'Percentage' }); + expect(percent).toBeDisabled(); + fireEvent.click(percent); + expect(onChange).not.toHaveBeenCalled(); + }); +}); diff --git a/client/app/lib/components/wrappers/__mocks__/I18nProvider.tsx b/client/app/lib/components/wrappers/__mocks__/I18nProvider.tsx new file mode 100644 index 00000000000..71d61318e33 --- /dev/null +++ b/client/app/lib/components/wrappers/__mocks__/I18nProvider.tsx @@ -0,0 +1,16 @@ +import { ReactNode } from 'react'; +import { IntlProvider } from 'react-intl'; + +// Manual mock for lib/components/wrappers/I18nProvider. The real provider renders +// a LoadingIndicator while it async-loads locale messages, so suites that assert +// synchronously would only ever see the spinner. Activate this synchronous +// stand-in per-suite with `jest.mock('lib/components/wrappers/I18nProvider')`. +// It is NOT applied automatically — only when a test opts in — so suites relying +// on the real provider's loading transition are unaffected. +const I18nProvider = ({ children }: { children: ReactNode }): JSX.Element => ( + + {children} + +); + +export default I18nProvider; diff --git a/client/app/routers/course/admin.tsx b/client/app/routers/course/admin.tsx index f9e1e1028b1..a157415aaf6 100644 --- a/client/app/routers/course/admin.tsx +++ b/client/app/routers/course/admin.tsx @@ -119,6 +119,17 @@ const adminRouter: Translated = (_) => ({ ).default, }), }, + { + path: 'gradebook', + lazy: async (): Promise> => ({ + Component: ( + await import( + /* webpackChunkName: 'GradebookSettings' */ + 'course/admin/pages/GradebookSettings' + ) + ).default, + }), + }, { path: 'comments', lazy: async (): Promise> => ({ diff --git a/client/app/types/course/admin/gradebook.ts b/client/app/types/course/admin/gradebook.ts new file mode 100644 index 00000000000..13301111fa0 --- /dev/null +++ b/client/app/types/course/admin/gradebook.ts @@ -0,0 +1,9 @@ +export interface GradebookSettingsData { + weightedViewEnabled: boolean; +} + +export interface GradebookSettingsPostData { + settings_gradebook_component: { + weighted_view_enabled: GradebookSettingsData['weightedViewEnabled']; + }; +} diff --git a/client/app/types/course/gradebook.ts b/client/app/types/course/gradebook.ts index e57b2cdd6d2..c9009afbfe0 100644 --- a/client/app/types/course/gradebook.ts +++ b/client/app/types/course/gradebook.ts @@ -7,6 +7,8 @@ export interface TabData { id: number; title: string; categoryId: number; + gradebookWeight?: number; + weightMode?: 'equal' | 'custom'; } export interface AssessmentData { @@ -14,6 +16,8 @@ export interface AssessmentData { title: string; tabId: number; maxGrade: number; + gradebookWeight?: number | null; + gradebookExcluded?: boolean; } export interface StudentData { @@ -39,4 +43,16 @@ export interface GradebookData { students: StudentData[]; submissions: SubmissionData[]; gamificationEnabled: boolean; + weightedViewEnabled: boolean; + canManageWeights: boolean; +} + +export interface UpdateWeightsPayload { + weights: { + tabId: number; + weight: number; + weightMode?: 'equal' | 'custom'; + excludedAssessmentIds?: number[]; + assessmentWeights?: { assessmentId: number; weight: number }[]; + }[]; } diff --git a/client/locales/en.json b/client/locales/en.json index 5a536c15e2f..fd663f40200 100644 --- a/client/locales/en.json +++ b/client/locales/en.json @@ -9268,5 +9268,164 @@ }, "lib.translations.table.column.newExternalId": { "defaultMessage": "New External ID" + }, + "course.admin.GradebookSettings.gradebookSettings": { + "defaultMessage": "Gradebook settings" + }, + "course.admin.GradebookSettings.weightedViewEnabled": { + "defaultMessage": "Enable Weighted Total view" + }, + "course.admin.GradebookSettings.weightedViewEnabledHint": { + "defaultMessage": "Enables a \"Weighted Total\" view in the gradebook where staff can configure per-tab weights and see a weighted total column." + }, + "course.gradebook.ConfigureWeightsPrompt.allExcluded": { + "defaultMessage": "All assessments in \"{tab}\" are excluded - it contributes nothing to the total." + }, + "course.gradebook.ConfigureWeightsPrompt.allExcludedCount": { + "defaultMessage": "All {n} excluded" + }, + "course.gradebook.ConfigureWeightsPrompt.customMode": { + "defaultMessage": "Custom" + }, + "course.gradebook.ConfigureWeightsPrompt.customSum": { + "defaultMessage": "Assessment weights: {sum} / {total}" + }, + "course.gradebook.ConfigureWeightsPrompt.defaultsHint": { + "defaultMessage": "No weights set yet - these are suggested defaults with every tab counting equally. Save to confirm, or adjust below." + }, + "course.gradebook.ConfigureWeightsPrompt.descriptionDrop": { + "defaultMessage": "In Equal mode, optionally drop each student's N lowest-scoring assessments before averaging." + }, + "course.gradebook.ConfigureWeightsPrompt.descriptionExclusion": { + "defaultMessage": "Expand a tab to include or exclude individual assessments from grading." + }, + "course.gradebook.ConfigureWeightsPrompt.descriptionIntro": { + "defaultMessage": "Control how tabs and assessments count toward each student's total grade." + }, + "course.gradebook.ConfigureWeightsPrompt.descriptionModes": { + "defaultMessage": "Choose Equal (all assessments share the tab's weight) or Custom (set each assessment's share)." + }, + "course.gradebook.ConfigureWeightsPrompt.descriptionWeights": { + "defaultMessage": "Set each tab's weight - how much it contributes to the total (weights should sum to 100)." + }, + "course.gradebook.ConfigureWeightsPrompt.equalMode": { + "defaultMessage": "Equal" + }, + "course.gradebook.ConfigureWeightsPrompt.excluded": { + "defaultMessage": "Excluded" + }, + "course.gradebook.ConfigureWeightsPrompt.excludedCount": { + "defaultMessage": "{n} excluded" + }, + "course.gradebook.ConfigureWeightsPrompt.includeAssessment": { + "defaultMessage": "Include {assessment} in grade" + }, + "course.gradebook.ConfigureWeightsPrompt.modeAria": { + "defaultMessage": "{tab} weight mode" + }, + "course.gradebook.ConfigureWeightsPrompt.ofGrade": { + "defaultMessage": "{pct}% of grade" + }, + "course.gradebook.ConfigureWeightsPrompt.promptTitle": { + "defaultMessage": "Configure contributions" + }, + "course.gradebook.ConfigureWeightsPrompt.saveError": { + "defaultMessage": "Failed to save weights. Please try again." + }, + "course.gradebook.ConfigureWeightsPrompt.total": { + "defaultMessage": "Total: {sum}%" + }, + "course.gradebook.ConfigureWeightsPrompt.unbalanced": { + "defaultMessage": "Assessment weights for \"{tab}\" must sum to its tab total before saving." + }, + "course.gradebook.ConfigureWeightsPrompt.valueTooHigh": { + "defaultMessage": "Value must be at most 100" + }, + "course.gradebook.ConfigureWeightsPrompt.valueTooLow": { + "defaultMessage": "Value must be at least 0" + }, + "course.gradebook.ConfigureWeightsPrompt.weightsDoNotSum": { + "defaultMessage": "Weights do not sum to 100. Saving is allowed; Total may be inaccurate." + }, + "course.gradebook.GradebookIndex.allAssessments": { + "defaultMessage": "All Assessments" + }, + "course.gradebook.GradebookIndex.byWeight": { + "defaultMessage": "Weighted Total" + }, + "course.gradebook.WeightedGradebookTable.collapseRow": { + "defaultMessage": "Collapse {name}" + }, + "course.gradebook.WeightedGradebookTable.configureWeights": { + "defaultMessage": "Configure Weights" + }, + "course.gradebook.WeightedGradebookTable.defaultWeights": { + "defaultMessage": "Showing default weights - every tab counts equally. Click \"Configure Weights\" to set your own." + }, + "course.gradebook.WeightedGradebookTable.defaultWeightsNoAccess": { + "defaultMessage": "Showing default weights - every tab counts equally until weights are configured." + }, + "course.gradebook.WeightedGradebookTable.displayPercent": { + "defaultMessage": "Percentage" + }, + "course.gradebook.WeightedGradebookTable.displayPercentTooltip": { + "defaultMessage": "What fraction of each tab the student earned. 100% on a tab worth 20% = the student earned all 20 grade points from that tab." + }, + "course.gradebook.WeightedGradebookTable.displayPoints": { + "defaultMessage": "Points" + }, + "course.gradebook.WeightedGradebookTable.displayPointsTooltip": { + "defaultMessage": "How many grade points each tab contributes. Columns add up to the projected total." + }, + "course.gradebook.WeightedGradebookTable.downloadCsv": { + "defaultMessage": "Download as CSV" + }, + "course.gradebook.WeightedGradebookTable.email": { + "defaultMessage": "Email" + }, + "course.gradebook.WeightedGradebookTable.excluded": { + "defaultMessage": "Excluded" + }, + "course.gradebook.WeightedGradebookTable.expandRow": { + "defaultMessage": "Expand {name}" + }, + "course.gradebook.WeightedGradebookTable.name": { + "defaultMessage": "Name" + }, + "course.gradebook.WeightedGradebookTable.noWeightsConfigured": { + "defaultMessage": "No weights configured - all tab weights are 0. Click \"Configure Weights\" to assign weights." + }, + "course.gradebook.WeightedGradebookTable.noWeightsNoAccess": { + "defaultMessage": "No tab weights have been configured yet." + }, + "course.gradebook.WeightedGradebookTable.outOfWeight": { + "defaultMessage": "/{weight}" + }, + "course.gradebook.WeightedGradebookTable.percentOfGrade": { + "defaultMessage": "{weight}% of grade" + }, + "course.gradebook.WeightedGradebookTable.percentTotalExact": { + "defaultMessage": "100% total" + }, + "course.gradebook.WeightedGradebookTable.percentTotalWarning": { + "defaultMessage": "{weight}% total" + }, + "course.gradebook.WeightedGradebookTable.weightedTotal": { + "defaultMessage": "Weighted Total" + }, + "course.gradebook.WeightedGradebookTable.searchStudents": { + "defaultMessage": "Search students" + }, + "course.gradebook.WeightedGradebookTable.weightsDoNotSum": { + "defaultMessage": "Weights do not sum to 100. Total may be inaccurate." + }, + "course.gradebook.TotalHint.policy": { + "defaultMessage": "Totals count ungraded assessments as 0." + }, + "course.gradebook.WeightedViewHint.hint": { + "defaultMessage": "Want to set how much each item contributes to students’ total grades? Enable Weighted Total view in {link}." + }, + "course.gradebook.WeightedViewHint.settingsLink": { + "defaultMessage": "Gradebook settings" } } diff --git a/client/locales/ko.json b/client/locales/ko.json index 2855b1a3a4e..d26fc2c8cab 100644 --- a/client/locales/ko.json +++ b/client/locales/ko.json @@ -9253,5 +9253,164 @@ }, "lib.translations.table.column.newExternalId": { "defaultMessage": "새 외부 ID" + }, + "course.admin.GradebookSettings.gradebookSettings": { + "defaultMessage": "성적부 설정" + }, + "course.admin.GradebookSettings.weightedViewEnabled": { + "defaultMessage": "가중 성적 보기 사용" + }, + "course.admin.GradebookSettings.weightedViewEnabledHint": { + "defaultMessage": "성적부에 \"가중 총점\" 보기를 추가하여 교직원이 탭별 가중치를 설정하고 가중 총점 열을 확인할 수 있습니다." + }, + "course.gradebook.ConfigureWeightsPrompt.allExcluded": { + "defaultMessage": "“{tab}”의 모든 평가가 제외되어 총점에 반영되지 않습니다." + }, + "course.gradebook.ConfigureWeightsPrompt.allExcludedCount": { + "defaultMessage": "전체 {n}개 제외됨" + }, + "course.gradebook.ConfigureWeightsPrompt.customMode": { + "defaultMessage": "사용자 지정" + }, + "course.gradebook.ConfigureWeightsPrompt.customSum": { + "defaultMessage": "평가 가중치: {sum} / {total}" + }, + "course.gradebook.ConfigureWeightsPrompt.defaultsHint": { + "defaultMessage": "아직 설정된 가중치가 없습니다. 모든 탭이 동일한 비중으로 반영되도록 제안된 기본값입니다. 저장하여 확정하거나 아래에서 조정하세요." + }, + "course.gradebook.ConfigureWeightsPrompt.descriptionDrop": { + "defaultMessage": "동일 모드에서는 평균을 내기 전에 각 학생의 점수가 가장 낮은 평가 N개를 선택적으로 제외할 수 있습니다." + }, + "course.gradebook.ConfigureWeightsPrompt.descriptionExclusion": { + "defaultMessage": "탭을 펼쳐 개별 평가를 성적에 포함하거나 제외하세요." + }, + "course.gradebook.ConfigureWeightsPrompt.descriptionIntro": { + "defaultMessage": "각 탭과 평가가 학생의 총 성적에 어떻게 반영되는지 관리합니다." + }, + "course.gradebook.ConfigureWeightsPrompt.descriptionModes": { + "defaultMessage": "“동일”(모든 평가가 탭의 가중치를 동일하게 나눔)또는 “사용자 지정”(각 평가의 비중을 설정)을 선택하세요." + }, + "course.gradebook.ConfigureWeightsPrompt.descriptionWeights": { + "defaultMessage": "각 탭의 가중치, 즉 총 성적에 기여하는 비중을 설정하세요. 가중치 합계는 100이어야 합니다." + }, + "course.gradebook.ConfigureWeightsPrompt.equalMode": { + "defaultMessage": "동일" + }, + "course.gradebook.ConfigureWeightsPrompt.excluded": { + "defaultMessage": "제외됨" + }, + "course.gradebook.ConfigureWeightsPrompt.excludedCount": { + "defaultMessage": "{n}개 제외됨" + }, + "course.gradebook.ConfigureWeightsPrompt.includeAssessment": { + "defaultMessage": "{assessment} 성적에 포함" + }, + "course.gradebook.ConfigureWeightsPrompt.modeAria": { + "defaultMessage": "{tab} 가중치 모드" + }, + "course.gradebook.ConfigureWeightsPrompt.ofGrade": { + "defaultMessage": "성적의 {pct}%" + }, + "course.gradebook.ConfigureWeightsPrompt.promptTitle": { + "defaultMessage": "기여도 설정" + }, + "course.gradebook.ConfigureWeightsPrompt.saveError": { + "defaultMessage": "가중치를 저장하지 못했습니다. 다시 시도해 주세요." + }, + "course.gradebook.ConfigureWeightsPrompt.total": { + "defaultMessage": "합계: {sum}%" + }, + "course.gradebook.ConfigureWeightsPrompt.unbalanced": { + "defaultMessage": "저장하기 전에 “{tab}”의 평가 가중치 합계가 해당 탭의 총 가중치와 같아야 합니다." + }, + "course.gradebook.ConfigureWeightsPrompt.valueTooHigh": { + "defaultMessage": "값은 최대 100이어야 합니다" + }, + "course.gradebook.ConfigureWeightsPrompt.valueTooLow": { + "defaultMessage": "값은 최소 0이어야 합니다" + }, + "course.gradebook.ConfigureWeightsPrompt.weightsDoNotSum": { + "defaultMessage": "가중치 합계가 100이 아닙니다. 저장은 가능하지만 총점이 정확하지 않을 수 있습니다." + }, + "course.gradebook.GradebookIndex.allAssessments": { + "defaultMessage": "전체 평가" + }, + "course.gradebook.GradebookIndex.byWeight": { + "defaultMessage": "가중 총점" + }, + "course.gradebook.WeightedGradebookTable.collapseRow": { + "defaultMessage": "{name} 접기" + }, + "course.gradebook.WeightedGradebookTable.configureWeights": { + "defaultMessage": "가중치 설정" + }, + "course.gradebook.WeightedGradebookTable.defaultWeights": { + "defaultMessage": "기본 가중치를 표시하고 있습니다. 모든 탭이 동일하게 반영됩니다. \"가중치 설정\"을 클릭하여 직접 설정하세요." + }, + "course.gradebook.WeightedGradebookTable.defaultWeightsNoAccess": { + "defaultMessage": "기본 가중치를 표시하고 있습니다. 가중치가 설정되기 전까지 모든 탭이 동일하게 반영됩니다." + }, + "course.gradebook.WeightedGradebookTable.displayPercent": { + "defaultMessage": "백분율" + }, + "course.gradebook.WeightedGradebookTable.displayPercentTooltip": { + "defaultMessage": "학생이 각 탭에서 획득한 비율입니다. 비중이 20%인 탭에서 100%는 해당 탭의 20점을 모두 획득했음을 의미합니다." + }, + "course.gradebook.WeightedGradebookTable.displayPoints": { + "defaultMessage": "점수" + }, + "course.gradebook.WeightedGradebookTable.displayPointsTooltip": { + "defaultMessage": "각 탭이 기여하는 성적 점수입니다. 각 열의 합이 예상 총점이 됩니다." + }, + "course.gradebook.WeightedGradebookTable.downloadCsv": { + "defaultMessage": "CSV로 다운로드" + }, + "course.gradebook.WeightedGradebookTable.email": { + "defaultMessage": "이메일" + }, + "course.gradebook.WeightedGradebookTable.excluded": { + "defaultMessage": "제외됨" + }, + "course.gradebook.WeightedGradebookTable.expandRow": { + "defaultMessage": "{name} 펼치기" + }, + "course.gradebook.WeightedGradebookTable.name": { + "defaultMessage": "이름" + }, + "course.gradebook.WeightedGradebookTable.noWeightsConfigured": { + "defaultMessage": "설정된 가중치가 없습니다. 모든 탭의 가중치가 0입니다. \"가중치 설정\"을 클릭하여 가중치를 지정하세요." + }, + "course.gradebook.WeightedGradebookTable.noWeightsNoAccess": { + "defaultMessage": "아직 탭 가중치가 설정되지 않았습니다." + }, + "course.gradebook.WeightedGradebookTable.outOfWeight": { + "defaultMessage": "/{weight}" + }, + "course.gradebook.WeightedGradebookTable.percentOfGrade": { + "defaultMessage": "성적의 {weight}%" + }, + "course.gradebook.WeightedGradebookTable.percentTotalExact": { + "defaultMessage": "합계 100%" + }, + "course.gradebook.WeightedGradebookTable.percentTotalWarning": { + "defaultMessage": "합계 {weight}%" + }, + "course.gradebook.WeightedGradebookTable.weightedTotal": { + "defaultMessage": "가중 총점" + }, + "course.gradebook.WeightedGradebookTable.searchStudents": { + "defaultMessage": "학생 검색" + }, + "course.gradebook.WeightedGradebookTable.weightsDoNotSum": { + "defaultMessage": "가중치 합계가 100이 아닙니다. 총점이 정확하지 않을 수 있습니다." + }, + "course.gradebook.TotalHint.policy": { + "defaultMessage": "총점은 채점되지 않은 평가를 0점으로 계산합니다." + }, + "course.gradebook.WeightedViewHint.hint": { + "defaultMessage": "가중 총점이 필요하신가요? 각 탭이 학생의 전체 성적에 반영되는 비중을 설정하고 여기에서 가중 총점을 확인할 수 있습니다. {link}에서 활성화하세요." + }, + "course.gradebook.WeightedViewHint.settingsLink": { + "defaultMessage": "성적부 설정" } } diff --git a/client/locales/zh.json b/client/locales/zh.json index 41c456b03e7..970c2dca7a2 100644 --- a/client/locales/zh.json +++ b/client/locales/zh.json @@ -9247,5 +9247,164 @@ }, "lib.translations.table.column.newExternalId": { "defaultMessage": "新外部编号" + }, + "course.admin.GradebookSettings.gradebookSettings": { + "defaultMessage": "成绩册设置" + }, + "course.admin.GradebookSettings.weightedViewEnabled": { + "defaultMessage": "启用加权成绩视图" + }, + "course.admin.GradebookSettings.weightedViewEnabledHint": { + "defaultMessage": "在成绩册中启用“加权总成绩”视图,教职员可以配置各标签页的权重并查看加权总成绩列。" + }, + "course.gradebook.ConfigureWeightsPrompt.allExcluded": { + "defaultMessage": "“{tab}”中的所有评估均已排除,且不会计入总成绩。" + }, + "course.gradebook.ConfigureWeightsPrompt.allExcludedCount": { + "defaultMessage": "已排除全部 {n} 个" + }, + "course.gradebook.ConfigureWeightsPrompt.customMode": { + "defaultMessage": "自定义" + }, + "course.gradebook.ConfigureWeightsPrompt.customSum": { + "defaultMessage": "评估权重:{sum} / {total}" + }, + "course.gradebook.ConfigureWeightsPrompt.defaultsHint": { + "defaultMessage": "尚未设置权重。以下为建议默认值,所有标签页均按相同比重计算。保存以确认,或在下方进行调整。" + }, + "course.gradebook.ConfigureWeightsPrompt.descriptionDrop": { + "defaultMessage": "在等权模式下,可选择在求平均分前剔除每位学生得分最低的 N 个评估。" + }, + "course.gradebook.ConfigureWeightsPrompt.descriptionExclusion": { + "defaultMessage": "展开标签页以将个别评估纳入或排除在评分之外。" + }, + "course.gradebook.ConfigureWeightsPrompt.descriptionIntro": { + "defaultMessage": "控制各标签页和评估如何计入每位学生的总成绩。" + }, + "course.gradebook.ConfigureWeightsPrompt.descriptionModes": { + "defaultMessage": "选择“等权”(所有评估平分该标签页的权重)或“自定义”(设置每个评估所占的份额)。" + }, + "course.gradebook.ConfigureWeightsPrompt.descriptionWeights": { + "defaultMessage": "设置每个标签页的权重,即其对总成绩的贡献比例(权重合计应为 100)。" + }, + "course.gradebook.ConfigureWeightsPrompt.equalMode": { + "defaultMessage": "等权" + }, + "course.gradebook.ConfigureWeightsPrompt.excluded": { + "defaultMessage": "已排除" + }, + "course.gradebook.ConfigureWeightsPrompt.excludedCount": { + "defaultMessage": "已排除 {n} 个" + }, + "course.gradebook.ConfigureWeightsPrompt.includeAssessment": { + "defaultMessage": "将 {assessment} 计入成绩" + }, + "course.gradebook.ConfigureWeightsPrompt.modeAria": { + "defaultMessage": "{tab} 权重模式" + }, + "course.gradebook.ConfigureWeightsPrompt.ofGrade": { + "defaultMessage": "占成绩的 {pct}%" + }, + "course.gradebook.ConfigureWeightsPrompt.promptTitle": { + "defaultMessage": "配置贡献比例" + }, + "course.gradebook.ConfigureWeightsPrompt.saveError": { + "defaultMessage": "保存权重失败。请重试。" + }, + "course.gradebook.ConfigureWeightsPrompt.total": { + "defaultMessage": "总计:{sum}%" + }, + "course.gradebook.ConfigureWeightsPrompt.unbalanced": { + "defaultMessage": "保存前,“{tab}”的评估权重合计必须等于该标签页的总权重。" + }, + "course.gradebook.ConfigureWeightsPrompt.valueTooHigh": { + "defaultMessage": "数值最多为 100" + }, + "course.gradebook.ConfigureWeightsPrompt.valueTooLow": { + "defaultMessage": "数值至少为 0" + }, + "course.gradebook.ConfigureWeightsPrompt.weightsDoNotSum": { + "defaultMessage": "权重合计不为 100。仍可保存;总成绩可能不准确。" + }, + "course.gradebook.GradebookIndex.allAssessments": { + "defaultMessage": "全部评估" + }, + "course.gradebook.GradebookIndex.byWeight": { + "defaultMessage": "加权总成绩" + }, + "course.gradebook.WeightedGradebookTable.collapseRow": { + "defaultMessage": "收起 {name}" + }, + "course.gradebook.WeightedGradebookTable.configureWeights": { + "defaultMessage": "配置权重" + }, + "course.gradebook.WeightedGradebookTable.defaultWeights": { + "defaultMessage": "正在显示默认权重-所有标签页比重相同。点击“配置权重”进行自定义设置。" + }, + "course.gradebook.WeightedGradebookTable.defaultWeightsNoAccess": { + "defaultMessage": "正在显示默认权重-在配置权重之前,所有标签页比重相同。" + }, + "course.gradebook.WeightedGradebookTable.displayPercent": { + "defaultMessage": "百分比" + }, + "course.gradebook.WeightedGradebookTable.displayPercentTooltip": { + "defaultMessage": "学生在各标签页所获得的比例。在比重为 20% 的标签页获得 100%,即表示学生获得了该标签页的全部 20 个成绩分。" + }, + "course.gradebook.WeightedGradebookTable.displayPoints": { + "defaultMessage": "分数" + }, + "course.gradebook.WeightedGradebookTable.displayPointsTooltip": { + "defaultMessage": "各标签页贡献的成绩分数。各列相加即为预计总成绩。" + }, + "course.gradebook.WeightedGradebookTable.downloadCsv": { + "defaultMessage": "下载为 CSV" + }, + "course.gradebook.WeightedGradebookTable.email": { + "defaultMessage": "电子邮件" + }, + "course.gradebook.WeightedGradebookTable.excluded": { + "defaultMessage": "已排除" + }, + "course.gradebook.WeightedGradebookTable.expandRow": { + "defaultMessage": "展开 {name}" + }, + "course.gradebook.WeightedGradebookTable.name": { + "defaultMessage": "姓名" + }, + "course.gradebook.WeightedGradebookTable.noWeightsConfigured": { + "defaultMessage": "尚未配置权重-所有标签页的权重均为 0。点击“配置权重”以分配权重。" + }, + "course.gradebook.WeightedGradebookTable.noWeightsNoAccess": { + "defaultMessage": "尚未配置任何标签页权重。" + }, + "course.gradebook.WeightedGradebookTable.outOfWeight": { + "defaultMessage": "/{weight}" + }, + "course.gradebook.WeightedGradebookTable.percentOfGrade": { + "defaultMessage": "占成绩的 {weight}%" + }, + "course.gradebook.WeightedGradebookTable.percentTotalExact": { + "defaultMessage": "合计 100%" + }, + "course.gradebook.WeightedGradebookTable.percentTotalWarning": { + "defaultMessage": "合计 {weight}%" + }, + "course.gradebook.WeightedGradebookTable.weightedTotal": { + "defaultMessage": "加权总成绩" + }, + "course.gradebook.WeightedGradebookTable.searchStudents": { + "defaultMessage": "搜索学生" + }, + "course.gradebook.WeightedGradebookTable.weightsDoNotSum": { + "defaultMessage": "权重合计不为 100。总成绩可能不准确。" + }, + "course.gradebook.TotalHint.policy": { + "defaultMessage": "总成绩将未评分的评估按 0 分计算。" + }, + "course.gradebook.WeightedViewHint.hint": { + "defaultMessage": "想要设置每个项目对学生总成绩的占比吗?请在{link}中启用“加权总成绩”视图。" + }, + "course.gradebook.WeightedViewHint.settingsLink": { + "defaultMessage": "成绩册设置" } } diff --git a/config/locales/en/activerecord/errors.yml b/config/locales/en/activerecord/errors.yml index 95f0da1660a..713858a6dcb 100644 --- a/config/locales/en/activerecord/errors.yml +++ b/config/locales/en/activerecord/errors.yml @@ -97,6 +97,7 @@ en: autograded_no_partial_answer: 'There are updated answers which have not been re-submitted yet. Please re-submit all answers before finalising your submission.' course/assessment/tab: deletion: 'the last tab cannot be deleted' + custom_weights_mismatch: 'Custom assessment weights must sum to the tab total' course/condition: attributes: conditional: diff --git a/config/locales/ko/activerecord/errors.yml b/config/locales/ko/activerecord/errors.yml index 62c4016a70f..6b6c01e11c2 100644 --- a/config/locales/ko/activerecord/errors.yml +++ b/config/locales/ko/activerecord/errors.yml @@ -95,6 +95,7 @@ ko: autograded_no_partial_answer: '업데이트된 답변이 아직 다시 제출되지 않았습니다. 제출을 완료하기 전에 모든 답변을 다시 제출하세요.' course/assessment/tab: deletion: '마지막 탭은 삭제할 수 없습니다' + custom_weights_mismatch: '사용자 지정 평가 가중치의 합은 해당 탭의 총점과 같아야 합니다.' course/condition: attributes: conditional: diff --git a/config/locales/zh/activerecord/errors.yml b/config/locales/zh/activerecord/errors.yml index 5cbb439debc..6a654814f36 100644 --- a/config/locales/zh/activerecord/errors.yml +++ b/config/locales/zh/activerecord/errors.yml @@ -95,6 +95,7 @@ zh: autograded_no_partial_answer: '有一些更新的答案还没有重新提交。请在最后提交前重新提交所有答案。' course/assessment/tab: deletion: '最后一项无法被删除' + custom_weights_mismatch: '自定义评估权重的总和必须等于该标签的总分。' course/condition: attributes: conditional: diff --git a/config/routes.rb b/config/routes.rb index 9746e03277f..0ff051754ca 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -196,6 +196,9 @@ get 'leaderboard' => 'leaderboard_settings#edit' patch 'leaderboard' => 'leaderboard_settings#update' + get 'gradebook' => 'gradebook_settings#edit' + patch 'gradebook' => 'gradebook_settings#update' + get 'comments' => 'discussion/topic_settings#edit', as: 'topics' patch 'comments' => 'discussion/topic_settings#update' @@ -498,6 +501,7 @@ resource :gradebook, only: [] do get '/' => 'gradebook#index' + patch '/weights' => 'gradebook#update_weights' end scope module: :discussion do diff --git a/db/migrate/20260611000000_create_gradebook_contribution_tables.rb b/db/migrate/20260611000000_create_gradebook_contribution_tables.rb new file mode 100644 index 00000000000..e7c7aac7213 --- /dev/null +++ b/db/migrate/20260611000000_create_gradebook_contribution_tables.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true +class CreateGradebookContributionTables < ActiveRecord::Migration[7.2] + def change + create_table :course_gradebook_tab_contributions do |t| + t.references :course, null: false, foreign_key: { to_table: :courses }, + index: { name: 'fk__course_gradebook_tab_contributions_course_id' } + t.references :tab, null: false, + foreign_key: { to_table: :course_assessment_tabs, on_delete: :cascade }, + index: { unique: true, + name: 'index_course_gradebook_tab_contributions_on_tab_id' } + t.decimal :weight, precision: 5, scale: 2, null: false, default: 0 + t.integer :weight_mode, null: false, default: 0 + + t.references :creator, null: false, foreign_key: { to_table: :users }, + index: { name: 'fk__course_gradebook_tab_contributions_creator_id' } + t.references :updater, null: false, foreign_key: { to_table: :users }, + index: { name: 'fk__course_gradebook_tab_contributions_updater_id' } + t.timestamps null: false + end + + create_table :course_gradebook_assessment_contributions do |t| + t.references :assessment, null: false, + foreign_key: { to_table: :course_assessments, on_delete: :cascade }, + index: { unique: true, + name: 'index_cgac_on_assessment_id' } + t.decimal :weight, precision: 5, scale: 2, null: true + t.boolean :excluded, null: false, default: false + + t.references :creator, null: false, foreign_key: { to_table: :users }, + index: { name: 'fk__cgac_creator_id' } + t.references :updater, null: false, foreign_key: { to_table: :users }, + index: { name: 'fk__cgac_updater_id' } + t.timestamps null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 1d320432f9a..2a1fc7a65f9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2026_05_14_052933) do +ActiveRecord::Schema[7.2].define(version: 2026_06_11_000000) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" enable_extension "uuid-ossp" @@ -856,6 +856,34 @@ t.index ["updater_id"], name: "fk__course_forums_updater_id" end + create_table "course_gradebook_assessment_contributions", force: :cascade do |t| + t.bigint "assessment_id", null: false + t.decimal "weight", precision: 5, scale: 2 + t.boolean "excluded", default: false, null: false + t.bigint "creator_id", null: false + t.bigint "updater_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["assessment_id"], name: "index_cgac_on_assessment_id", unique: true + t.index ["creator_id"], name: "fk__cgac_creator_id" + t.index ["updater_id"], name: "fk__cgac_updater_id" + end + + create_table "course_gradebook_tab_contributions", force: :cascade do |t| + t.bigint "course_id", null: false + t.bigint "tab_id", null: false + t.decimal "weight", precision: 5, scale: 2, default: "0.0", null: false + t.integer "weight_mode", default: 0, null: false + t.bigint "creator_id", null: false + t.bigint "updater_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["course_id"], name: "fk__course_gradebook_tabcontributions_course_id" + t.index ["creator_id"], name: "fk__course_gradebook_tabcontributions_creator_id" + t.index ["tab_id"], name: "index_course_gradebook_tabcontributions_on_tab_id", unique: true + t.index ["updater_id"], name: "fk__course_gradebook_tabcontributions_updater_id" + end + create_table "course_group_categories", force: :cascade do |t| t.bigint "course_id", null: false t.string "name", default: "", null: false @@ -1915,6 +1943,13 @@ add_foreign_key "course_forums", "courses", name: "fk_course_forums_course_id" add_foreign_key "course_forums", "users", column: "creator_id", name: "fk_course_forums_creator_id" add_foreign_key "course_forums", "users", column: "updater_id", name: "fk_course_forums_updater_id" + add_foreign_key "course_gradebook_assessment_contributions", "course_assessments", column: "assessment_id", on_delete: :cascade + add_foreign_key "course_gradebook_assessment_contributions", "users", column: "creator_id" + add_foreign_key "course_gradebook_assessment_contributions", "users", column: "updater_id" + add_foreign_key "course_gradebook_tab_contributions", "course_assessment_tabs", column: "tab_id", on_delete: :cascade + add_foreign_key "course_gradebook_tab_contributions", "courses" + add_foreign_key "course_gradebook_tab_contributions", "users", column: "creator_id" + add_foreign_key "course_gradebook_tab_contributions", "users", column: "updater_id" add_foreign_key "course_group_categories", "courses" add_foreign_key "course_group_categories", "users", column: "creator_id" add_foreign_key "course_group_categories", "users", column: "updater_id" diff --git a/spec/controllers/course/admin/gradebook_settings_controller_spec.rb b/spec/controllers/course/admin/gradebook_settings_controller_spec.rb new file mode 100644 index 00000000000..b19c7678b18 --- /dev/null +++ b/spec/controllers/course/admin/gradebook_settings_controller_spec.rb @@ -0,0 +1,156 @@ +# frozen_string_literal: true +require 'rails_helper' + +RSpec.describe Course::Admin::GradebookSettingsController, type: :controller do + let(:instance) { Instance.default } + with_tenant(:instance) do + let(:course) { create(:course) } + let(:manager) { create(:course_manager, course: course) } + let(:teaching_assistant) { create(:course_teaching_assistant, course: course) } + let(:student) { create(:course_student, course: course) } + + describe '#edit' do + context 'as manager' do + render_views + before { controller_sign_in(controller, manager.user) } + + it 'returns settings JSON' do + get :edit, params: { course_id: course.id }, format: :json + expect(response).to have_http_status(:ok) + body = JSON.parse(response.body) + expect(body).to include('weightedViewEnabled' => false) + end + + it 'reflects an already-enabled setting' do + ctx = Struct.new(:current_course, :key).new(course, Course::GradebookComponent.key) + Course::Settings::GradebookComponent.new(ctx).weighted_view_enabled = true + course.save! + get :edit, params: { course_id: course.id }, format: :json + expect(JSON.parse(response.body)).to include('weightedViewEnabled' => true) + end + end + + context 'as teaching assistant' do + before { controller_sign_in(controller, teaching_assistant.user) } + + it 'is denied' do + expect do + get :edit, params: { course_id: course.id }, format: :json + end.to raise_error(CanCan::AccessDenied) + end + end + + context 'as student' do + before { controller_sign_in(controller, student.user) } + + it 'is denied' do + expect do + get :edit, params: { course_id: course.id }, format: :json + end.to raise_error(CanCan::AccessDenied) + end + end + + context 'when the gradebook component is disabled' do + before do + controller_sign_in(controller, manager.user) + allow(controller).to receive_message_chain('current_component_host.[]').and_return(nil) + end + + it 'raises a component not found error' do + expect do + get :edit, params: { course_id: course.id }, format: :json + end.to raise_error(ComponentNotFoundError) + end + end + end + + describe '#update' do + context 'as manager' do + render_views + before { controller_sign_in(controller, manager.user) } + + it 'updates weighted_view_enabled and returns 200' do + patch :update, + params: { course_id: course.id, + settings_gradebook_component: { weighted_view_enabled: true } }, + format: :json + expect(response).to have_http_status(:ok) + body = JSON.parse(response.body) + expect(body).to include('weightedViewEnabled' => true) + end + + it 'preserves existing tab gradebook_weights when toggling setting' do + category = create(:course_assessment_category, course: course) + tab = create(:course_assessment_tab, category: category) + contribution = create(:course_gradebook_tab_contribution, tab: tab, course: course, weight: 50) + + patch :update, + params: { course_id: course.id, + settings_gradebook_component: { weighted_view_enabled: true } }, + format: :json + expect(contribution.reload.weight).to eq(50) + + patch :update, + params: { course_id: course.id, + settings_gradebook_component: { weighted_view_enabled: false } }, + format: :json + expect(contribution.reload.weight).to eq(50) + end + + it 'renders errors with 400 when persistence fails' do + allow_any_instance_of(Course).to receive(:save).and_return(false) + patch :update, + params: { course_id: course.id, + settings_gradebook_component: { weighted_view_enabled: true } }, + format: :json + expect(response).to have_http_status(:bad_request) + expect(JSON.parse(response.body)).to have_key('errors') + end + + it 'raises ParameterMissing when settings_gradebook_component is absent' do + expect do + patch :update, params: { course_id: course.id }, format: :json + end.to raise_error(ActionController::ParameterMissing) + end + + it 'ignores attributes outside the permitted set' do + patch :update, + params: { course_id: course.id, + settings_gradebook_component: { + weighted_view_enabled: true, some_forbidden_attr: 'x' + } }, + format: :json + expect(response).to have_http_status(:ok) + expect(JSON.parse(response.body)).to include('weightedViewEnabled' => true) + end + end + + context 'as teaching assistant' do + before { controller_sign_in(controller, teaching_assistant.user) } + + it 'is denied' do + expect do + patch :update, + params: { course_id: course.id, + settings_gradebook_component: { weighted_view_enabled: true } }, + format: :json + end.to raise_error(CanCan::AccessDenied) + end + end + + context 'as student' do + let(:student) { create(:course_student, course: course) } + before { controller_sign_in(controller, student.user) } + + it 'is denied' do + expect do + patch :update, + params: { course_id: course.id, + settings_gradebook_component: { weighted_view_enabled: true } }, + format: :json + end.to raise_error(CanCan::AccessDenied) + end + end + end + end +end diff --git a/spec/controllers/course/gradebook_controller_spec.rb b/spec/controllers/course/gradebook_controller_spec.rb index a213e80f476..2c1a5736648 100644 --- a/spec/controllers/course/gradebook_controller_spec.rb +++ b/spec/controllers/course/gradebook_controller_spec.rb @@ -8,6 +8,7 @@ let(:course) { create(:course) } let(:student) { create(:course_user, :student, course: course) } let(:staff) { create(:course_user, :teaching_assistant, course: course) } + let(:manager) { create(:course_user, :manager, course: course) } describe '#index' do render_views @@ -37,6 +38,13 @@ let(:ta) { create(:course_teaching_assistant, course: course) } before { controller_sign_in(controller, ta.user) } + it { expect { subject }.to raise_error(CanCan::AccessDenied) } + end + + context 'when a manager visits the page' do + let(:manager) { create(:course_manager, course: course) } + before { controller_sign_in(controller, manager.user) } + it { expect(subject).to be_successful } it 'returns all required top-level keys' do @@ -48,18 +56,11 @@ end end - context 'when a manager visits the page' do - let(:manager) { create(:course_manager, course: course) } - before { controller_sign_in(controller, manager.user) } - - it { expect(subject).to be_successful } - end - context 'when an observer visits the page' do let(:observer) { create(:course_observer, course: course) } before { controller_sign_in(controller, observer.user) } - it { expect(subject).to be_successful } + it { expect { subject }.to raise_error(CanCan::AccessDenied) } end context 'with a published assessment and a graded submission' do @@ -77,7 +78,7 @@ before do submission.answers.update_all(grade: 5.0, current_answer: true) - controller_sign_in(controller, ta.user) + controller_sign_in(controller, manager.user) end it 'includes the assessment in the assessments array' do @@ -129,9 +130,8 @@ end context 'when a student has an external ID' do - let(:ta) { create(:course_teaching_assistant, course: course) } let!(:student) { create(:course_student, course: course, external_id: 'EXT-123') } - before { controller_sign_in(controller, ta.user) } + before { controller_sign_in(controller, manager.user) } it 'returns the external ID in the students array' do subject @@ -143,7 +143,7 @@ end context 'with a graded submission where the answer grade is exactly 0' do - let(:ta) { create(:course_teaching_assistant, course: course) } + let(:manager) { create(:course_manager, course: course) } let(:tab) { course.assessment_categories.first.tabs.first } let!(:assessment) do create(:course_assessment_assessment, :published_with_mcq_question, @@ -157,7 +157,7 @@ before do submission.answers.update_all(grade: 0.0, current_answer: true) - controller_sign_in(controller, ta.user) + controller_sign_in(controller, manager.user) end it 'returns grade 0 (not null) in the submissions array' do @@ -172,7 +172,7 @@ end context 'with a graded submission where answer grades are null (blank)' do - let(:ta) { create(:course_teaching_assistant, course: course) } + let(:manager) { create(:course_manager, course: course) } let(:tab) { course.assessment_categories.first.tabs.first } let!(:assessment) do create(:course_assessment_assessment, :published_with_mcq_question, @@ -186,7 +186,7 @@ before do submission.answers.update_all(grade: nil, current_answer: true) - controller_sign_in(controller, ta.user) + controller_sign_in(controller, manager.user) end it 'returns null grade (not 0) in the submissions array' do @@ -200,5 +200,236 @@ end end end + + describe 'PATCH update_weights' do + let(:manager) { create(:course_manager, course: course) } + let(:ta) { create(:course_teaching_assistant, course: course) } + let(:student) { create(:course_student, course: course) } + let(:category) { create(:course_assessment_category, course: course) } + let!(:tab1) { create(:course_assessment_tab, category: category) } + let!(:tab2) { create(:course_assessment_tab, category: category) } + def weight_for(tab) + Course::Gradebook::TabContribution.find_by(tab_id: tab.id)&.weight + end + + let(:valid_payload) do + { weights: [{ tabId: tab1.id, weight: 60 }, { tabId: tab2.id, weight: 40 }] } + end + + context 'as manager' do + before { controller_sign_in(controller, manager.user) } + + it 'updates and returns 200' do + patch :update_weights, params: { course_id: course.id, **valid_payload }, format: :json + expect(response).to have_http_status(:ok) + expect(weight_for(tab1)).to eq(60) + expect(weight_for(tab2)).to eq(40) + end + + it 'accepts sum < 100' do + patch :update_weights, + params: { course_id: course.id, weights: [tabId: tab1.id, weight: 30] }, + format: :json + expect(response).to have_http_status(:ok) + end + + it 'accepts sum > 100' do + patch :update_weights, + params: { course_id: course.id, + weights: [{ tabId: tab1.id, weight: 70 }, { tabId: tab2.id, weight: 70 }] }, + format: :json + expect(response).to have_http_status(:ok) + end + + it 'rejects negative with 422 and no partial write' do + create(:course_gradebook_tab_contribution, tab: tab1, course: course, weight: 10) + patch :update_weights, + params: { course_id: course.id, + weights: [{ tabId: tab1.id, weight: 50 }, { tabId: tab2.id, weight: -1 }] }, + format: :json + expect(response).to have_http_status(:unprocessable_content) + expect(weight_for(tab1)).to eq(10) + end + + it 'rejects >100 with 422' do + patch :update_weights, + params: { course_id: course.id, weights: [tabId: tab1.id, weight: 101] }, + format: :json + expect(response).to have_http_status(:unprocessable_content) + end + + it 'rejects foreign tab id with 422' do + other_course = create(:course) + other_tab = create(:course_assessment_tab, + category: create(:course_assessment_category, course: other_course)) + patch :update_weights, + params: { course_id: course.id, weights: [tabId: other_tab.id, weight: 50] }, + format: :json + expect(response).to have_http_status(:unprocessable_content) + end + + it 'treats an omitted weights param as a no-op rather than a 500' do + patch :update_weights, params: { course_id: course.id }, format: :json + expect(response).to have_http_status(:ok) + expect(JSON.parse(response.body)['weights']).to eq([]) + end + + it 'rounds the echoed weight to 2dp so it matches the stored DECIMAL(5,2)' do + patch :update_weights, + params: { course_id: course.id, weights: [tabId: tab1.id, weight: 33.333] }, + format: :json + expect(response).to have_http_status(:ok) + expect(JSON.parse(response.body)['weights'].first['weight']).to eq(33.33) + expect(weight_for(tab1)).to eq(33.33) + end + end + + context 'as TA' do + before { controller_sign_in(controller, ta.user) } + it 'is denied' do + expect do + patch :update_weights, params: { course_id: course.id, **valid_payload }, format: :json + end.to raise_error(CanCan::AccessDenied) + end + end + + context 'as student' do + before { controller_sign_in(controller, student.user) } + it 'is denied' do + expect do + patch :update_weights, params: { course_id: course.id, **valid_payload }, format: :json + end.to raise_error(CanCan::AccessDenied) + end + end + + context 'when setting is disabled' do + before { controller_sign_in(controller, manager.user) } + + it 'still allows update (storage independent of display)' do + patch :update_weights, params: { course_id: course.id, **valid_payload }, format: :json + expect(response).to have_http_status(:ok) + expect(weight_for(tab1)).to eq(60) + end + end + + describe '#update_weights with modes' do + render_views + + let(:category) { create(:course_assessment_category, course: course) } + let(:tab) { create(:course_assessment_tab, category: category) } + let!(:a1) { create(:assessment, course: course, tab: tab) } + let!(:a2) { create(:assessment, course: course, tab: tab) } + + before { controller_sign_in(controller, manager.user) } + + it 'persists custom mode + assessment weights and echoes them back' do + patch :update_weights, as: :json, params: { + course_id: course.id, + weights: [{ + tabId: tab.id, weight: '50', weightMode: 'custom', + assessmentWeights: [ + { assessmentId: a1.id, weight: '30' }, + { assessmentId: a2.id, weight: '20' } + ] + }] + } + expect(response).to have_http_status(:ok) + body = JSON.parse(response.body) + entry = body['weights'].first + expect(entry['weightMode']).to eq('custom') + expect(entry['assessmentWeights']).to contain_exactly( + { 'assessmentId' => a1.id, 'weight' => 30.0 }, + { 'assessmentId' => a2.id, 'weight' => 20.0 } + ) + expect(a1.reload.gradebook_assessment_contribution.weight).to eq(30.0) + end + + it 'returns 422 when custom weights do not sum to the tab total' do + patch :update_weights, as: :json, params: { + course_id: course.id, + weights: [{ + tabId: tab.id, weight: '50', weightMode: 'custom', + assessmentWeights: [{ assessmentId: a1.id, weight: '10' }] + }] + } + expect(response).to have_http_status(:unprocessable_content) + end + + it 'persists and echoes per-assessment exclusion in equal mode' do + patch :update_weights, as: :json, params: { + course_id: course.id, + weights: [{ + tabId: tab.id, weight: '50', weightMode: 'equal', + excludedAssessmentIds: [a1.id] + }] + } + expect(response).to have_http_status(:ok) + expect(a1.reload.gradebook_assessment_contribution.excluded).to eq(true) + expect(a2.reload.gradebook_assessment_contribution.excluded).to eq(false) + entry = JSON.parse(response.body)['weights'].first + expect(entry['excludedAssessmentIds']).to eq([a1.id]) + end + end + end + + describe 'GET index — weighted view fields' do + render_views + let(:manager) { create(:course_manager, course: course) } + let(:ta) { create(:course_teaching_assistant, course: course) } + let(:category) { create(:course_assessment_category, course: course) } + let!(:tab) { create(:course_assessment_tab, category: category) } + let!(:contribution) { create(:course_gradebook_tab_contribution, tab: tab, course: course, weight: 30) } + let!(:assessment) do + create(:course_assessment_assessment, :published_with_mcq_question, + course: course, tab: tab) + end + + context 'when setting is disabled (default)' do + before { controller_sign_in(controller, manager.user) } + + it 'returns weightedViewEnabled false and omits gradebookWeight per tab' do + get :index, params: { course_id: course.id }, format: :json + body = JSON.parse(response.body) + expect(body['weightedViewEnabled']).to eq(false) + tab_json = body['tabs'].find { |t| t['id'] == tab.id } + expect(tab_json).not_to have_key('gradebookWeight') + end + end + + context 'when setting is enabled' do + before do + ctx = Struct.new(:current_course, :key).new(course, Course::GradebookComponent.key) + Course::Settings::GradebookComponent.new(ctx).weighted_view_enabled = true + course.save! + end + + it 'includes weightedViewEnabled true and gradebookWeight per tab for manager' do + controller_sign_in(controller, manager.user) + get :index, params: { course_id: course.id }, format: :json + body = JSON.parse(response.body) + expect(body['weightedViewEnabled']).to eq(true) + expect(body['canManageWeights']).to eq(true) + tab_json = body['tabs'].find { |t| t['id'] == tab.id } + expect(tab_json['gradebookWeight']).to eq(30) + end + + it 'serializes weightMode on tabs and gradebookWeight on assessments when weighted view is enabled' do + controller_sign_in(controller, manager.user) + get :index, params: { course_id: course.id }, format: :json + body = JSON.parse(response.body) + tab_json = body['tabs'].find { |t| t['id'] == tab.id } + expect(tab_json).to have_key('weightMode') + expect(body['assessments'].first).to have_key('gradebookWeight') + end + + it 'serializes gradebookExcluded on assessments when weighted view is enabled' do + controller_sign_in(controller, manager.user) + get :index, params: { course_id: course.id }, format: :json + body = JSON.parse(response.body) + expect(body['assessments'].first).to have_key('gradebookExcluded') + expect(body['assessments'].first['gradebookExcluded']).to eq(false) + end + end + end end end diff --git a/spec/factories/course_gradebook_assessment_contributions.rb b/spec/factories/course_gradebook_assessment_contributions.rb new file mode 100644 index 00000000000..6a0126e520f --- /dev/null +++ b/spec/factories/course_gradebook_assessment_contributions.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true +FactoryBot.define do + factory :course_gradebook_assessment_contribution, class: Course::Gradebook::AssessmentContribution.name do + assessment + excluded { false } + end +end diff --git a/spec/factories/course_gradebook_tab_contributions.rb b/spec/factories/course_gradebook_tab_contributions.rb new file mode 100644 index 00000000000..decf87e61bd --- /dev/null +++ b/spec/factories/course_gradebook_tab_contributions.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true +FactoryBot.define do + factory :course_gradebook_tab_contribution, class: Course::Gradebook::TabContribution.name do + association :tab, factory: :course_assessment_tab + course { tab.category.course } + weight { 0 } + weight_mode { :equal } + end +end diff --git a/spec/models/course/gradebook/assessment_contribution_spec.rb b/spec/models/course/gradebook/assessment_contribution_spec.rb new file mode 100644 index 00000000000..107e55a1e96 --- /dev/null +++ b/spec/models/course/gradebook/assessment_contribution_spec.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true +require 'rails_helper' + +RSpec.describe Course::Gradebook::AssessmentContribution do + let!(:instance) { Instance.default } + with_tenant(:instance) do + let(:course) { create(:course) } + let(:tab) { create(:course_assessment_tab, course: course) } + let(:assessment) { create(:assessment, course: course, tab: tab) } + + it 'allows a nil weight (equal mode)' do + contribution = build(:course_gradebook_assessment_contribution, assessment: assessment, weight: nil) + expect(contribution).to be_valid + end + + it 'rejects a negative weight' do + contribution = build(:course_gradebook_assessment_contribution, assessment: assessment, weight: -1) + expect(contribution).not_to be_valid + end + + it 'accepts a non-negative weight' do + contribution = build(:course_gradebook_assessment_contribution, assessment: assessment, weight: 0) + expect(contribution).to be_valid + contribution.weight = 25.5 + expect(contribution).to be_valid + end + + it 'defaults excluded to false' do + contribution = create(:course_gradebook_assessment_contribution, assessment: assessment) + expect(contribution.excluded).to eq(false) + end + + it 'rejects a nil excluded flag' do + contribution = build(:course_gradebook_assessment_contribution, assessment: assessment) + contribution.excluded = nil + expect(contribution).not_to be_valid + end + + it 'enforces one row per assessment' do + create(:course_gradebook_assessment_contribution, assessment: assessment) + duplicate = build(:course_gradebook_assessment_contribution, assessment: assessment) + expect(duplicate).not_to be_valid + end + + it 'is destroyed when its assessment is destroyed' do + create(:course_gradebook_assessment_contribution, assessment: assessment) + expect { assessment.destroy! }.to change { described_class.count }.by(-1) + end + end +end diff --git a/spec/models/course/gradebook/tab_contribution_spec.rb b/spec/models/course/gradebook/tab_contribution_spec.rb new file mode 100644 index 00000000000..0f9d1bf11f9 --- /dev/null +++ b/spec/models/course/gradebook/tab_contribution_spec.rb @@ -0,0 +1,239 @@ +# frozen_string_literal: true +require 'rails_helper' + +RSpec.describe Course::Gradebook::TabContribution do + let!(:instance) { Instance.default } + with_tenant(:instance) do + let(:course) { create(:course) } + let(:category) { create(:course_assessment_category, course: course) } + let(:tab) { create(:course_assessment_tab, category: category) } + + describe 'validations' do + it 'is valid with a tab and matching course' do + expect(build(:course_gradebook_tab_contribution, tab: tab, course: course)).to be_valid + end + + it 'requires a tab' do + contribution = build(:course_gradebook_tab_contribution, tab: tab, course: course) + contribution.tab = nil + expect(contribution).not_to be_valid + end + + it 'enforces one row per tab' do + create(:course_gradebook_tab_contribution, tab: tab, course: course) + duplicate = build(:course_gradebook_tab_contribution, tab: tab, course: course) + expect(duplicate).not_to be_valid + end + + it 'rejects a course that does not match the tab course' do + contribution = build(:course_gradebook_tab_contribution, tab: tab) + contribution.course = create(:course) + expect(contribution).not_to be_valid + end + + it 'rejects a negative weight' do + contribution = build(:course_gradebook_tab_contribution, tab: tab, course: course, weight: -1) + expect(contribution).not_to be_valid + end + + it 'rejects a weight above 100' do + contribution = build(:course_gradebook_tab_contribution, tab: tab, course: course, weight: 101) + expect(contribution).not_to be_valid + end + + it 'accepts a weight of exactly 100' do + contribution = build(:course_gradebook_tab_contribution, tab: tab, course: course, weight: 100) + expect(contribution).to be_valid + end + + it 'requires a weight_mode' do + contribution = build(:course_gradebook_tab_contribution, tab: tab, course: course) + contribution.weight_mode = nil + expect(contribution).not_to be_valid + end + + it 'defaults weight 0, mode equal' do + contribution = create(:course_gradebook_tab_contribution, tab: tab, course: course) + expect(contribution.weight).to eq(0) + expect(contribution.weight_mode).to eq('equal') + end + end + + describe 'dependent destroy' do + it 'is destroyed when its tab is destroyed' do + create(:course_assessment_tab, category: category) # sibling so the tab is deletable + create(:course_gradebook_tab_contribution, tab: tab, course: course) + expect { tab.destroy! }.to change { described_class.count }.by(-1) + end + end + + describe '.bulk_update' do + let(:tab1) { create(:course_assessment_tab, category: category) } + let(:tab2) { create(:course_assessment_tab, category: category) } + + def weight_for(tab) + described_class.find_by(tab_id: tab.id)&.weight + end + + it 'upserts a contribution per given tab' do + described_class.bulk_update( + course: course, + updates: [{ tab_id: tab1.id, weight: 60 }, { tab_id: tab2.id, weight: 40 }] + ) + expect(weight_for(tab1)).to eq(60) + expect(weight_for(tab2)).to eq(40) + expect(described_class.find_by(tab_id: tab1.id).course_id).to eq(course.id) + expect(described_class.where(tab_id: [tab1.id, tab2.id]).count).to eq(2) + end + + it 'is a no-op for an empty updates array' do + expect do + described_class.bulk_update(course: course, updates: []) + end.not_to change(described_class, :count) + end + + it 'is transactional — an invalid value rolls everything back' do + create(:course_gradebook_tab_contribution, tab: tab1, course: course, weight: 10) + create(:course_gradebook_tab_contribution, tab: tab2, course: course, weight: 20) + expect do + described_class.bulk_update( + course: course, + updates: [{ tab_id: tab1.id, weight: 50 }, { tab_id: tab2.id, weight: 999 }] + ) + end.to raise_error(ActiveRecord::RecordInvalid) + expect(weight_for(tab1)).to eq(10) + expect(weight_for(tab2)).to eq(20) + end + + it 'rejects a foreign tab_id' do + other_course = create(:course) + other_tab = create(:course_assessment_tab, + category: create(:course_assessment_category, course: other_course)) + expect do + described_class.bulk_update(course: course, updates: [tab_id: other_tab.id, weight: 50]) + end.to raise_error(ActiveRecord::RecordNotFound) + end + + context 'with assessments in the tab' do + let(:tab) { create(:course_assessment_tab, category: category) } + let!(:a1) { create(:assessment, course: course, tab: tab) } + let!(:a2) { create(:assessment, course: course, tab: tab) } + + def assessment_weight(assessment) + assessment.reload.gradebook_assessment_contribution&.weight + end + + def excluded?(assessment) + assessment.reload.gradebook_assessment_contribution&.excluded + end + + it 'persists custom mode + assessment weights that sum to the tab total' do + described_class.bulk_update( + course: course, + updates: [ + tab_id: tab.id, weight: 50.0, weight_mode: 'custom', + assessment_weights: [ + { assessment_id: a1.id, weight: 30.0 }, + { assessment_id: a2.id, weight: 20.0 } + ] + ] + ) + expect(tab.reload.gradebook_contribution.weight_mode).to eq('custom') + expect(assessment_weight(a1)).to eq(30.0) + expect(assessment_weight(a2)).to eq(20.0) + end + + it 'raises RecordInvalid when custom weights do not sum to the tab total' do + expect do + described_class.bulk_update( + course: course, + updates: [ + tab_id: tab.id, weight: 50.0, weight_mode: 'custom', + assessment_weights: [ + { assessment_id: a1.id, weight: 30.0 }, + { assessment_id: a2.id, weight: 5.0 } + ] + ] + ) + end.to raise_error(ActiveRecord::RecordInvalid) + expect(assessment_weight(a1)).to be_nil # rolled back + end + + it 'nulls assessment weights when switching the tab to equal mode' do + create(:course_gradebook_assessment_contribution, assessment: a1, weight: 30.0) + described_class.bulk_update( + course: course, + updates: [tab_id: tab.id, weight: 50.0, weight_mode: 'equal'] + ) + expect(tab.reload.gradebook_contribution.weight_mode).to eq('equal') + expect(assessment_weight(a1)).to be_nil + end + + it 'persists per-assessment exclusion in equal mode' do + described_class.bulk_update( + course: course, + updates: [tab_id: tab.id, weight: 50.0, weight_mode: 'equal', + excluded_assessment_ids: [a1.id]] + ) + expect(excluded?(a1)).to eq(true) + expect(excluded?(a2)).to eq(false) + end + + it 'drops excluded assessments from the custom balance check and keeps their weight' do + described_class.bulk_update( + course: course, + updates: [ + tab_id: tab.id, weight: 30.0, weight_mode: 'custom', + excluded_assessment_ids: [a2.id], + assessment_weights: [ + { assessment_id: a1.id, weight: 30.0 }, + { assessment_id: a2.id, weight: 20.0 } + ] + ] + ) + expect(excluded?(a1)).to eq(false) + expect(excluded?(a2)).to eq(true) + expect(assessment_weight(a2)).to eq(20.0) # retained for restore, not zeroed + end + + it 'skips the custom balance check when every assessment is excluded' do + expect do + described_class.bulk_update( + course: course, + updates: [ + tab_id: tab.id, weight: 30.0, weight_mode: 'custom', + excluded_assessment_ids: [a1.id, a2.id], + assessment_weights: [ + { assessment_id: a1.id, weight: 0.0 }, + { assessment_id: a2.id, weight: 0.0 } + ] + ] + ) + end.not_to raise_error + end + + it 'raises RecordNotFound when a custom assessment weight targets an assessment outside the tab' do + foreign = create(:assessment, course: course) + expect do + described_class.bulk_update( + course: course, + updates: [ + tab_id: tab.id, weight: 50.0, weight_mode: 'custom', + assessment_weights: [{ assessment_id: foreign.id, weight: 50.0 }] + ] + ) + end.to raise_error(ActiveRecord::RecordNotFound) + end + + it 're-including a previously excluded assessment clears the flag' do + create(:course_gradebook_assessment_contribution, assessment: a1, excluded: true) + described_class.bulk_update( + course: course, + updates: [tab_id: tab.id, weight: 50.0, weight_mode: 'equal', excluded_assessment_ids: []] + ) + expect(excluded?(a1)).to eq(false) + end + end + end + end +end diff --git a/spec/models/course/gradebook_ability_spec.rb b/spec/models/course/gradebook_ability_spec.rb new file mode 100644 index 00000000000..91257859ddb --- /dev/null +++ b/spec/models/course/gradebook_ability_spec.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true +require 'rails_helper' + +RSpec.describe Course::GradebookAbilityComponent do + let!(:instance) { Instance.default } + with_tenant(:instance) do + subject { Ability.new(user, course, course_user) } + let(:course) { create(:course) } + + context 'when the user is a Course Manager' do + let(:course_user) { create(:course_manager, course: course) } + let(:user) { course_user.user } + it { is_expected.to be_able_to(:read_gradebook, course) } + it { is_expected.to be_able_to(:manage_gradebook_weights, course) } + it { is_expected.to be_able_to(:manage_gradebook_settings, course) } + end + + context 'when the user is a Course Owner' do + let(:course_user) { create(:course_owner, course: course) } + let(:user) { course_user.user } + it { is_expected.to be_able_to(:read_gradebook, course) } + it { is_expected.to be_able_to(:manage_gradebook_weights, course) } + it { is_expected.to be_able_to(:manage_gradebook_settings, course) } + end + + context 'when the user is a Teaching Assistant' do + let(:course_user) { create(:course_teaching_assistant, course: course) } + let(:user) { course_user.user } + it { is_expected.not_to be_able_to(:read_gradebook, course) } + it { is_expected.not_to be_able_to(:manage_gradebook_weights, course) } + it { is_expected.not_to be_able_to(:manage_gradebook_settings, course) } + end + + context 'when the user is a Course Student' do + let(:course_user) { create(:course_student, course: course) } + let(:user) { course_user.user } + it { is_expected.not_to be_able_to(:read_gradebook, course) } + it { is_expected.not_to be_able_to(:manage_gradebook_weights, course) } + it { is_expected.not_to be_able_to(:manage_gradebook_settings, course) } + end + end +end diff --git a/spec/models/course/settings/gradebook_component_spec.rb b/spec/models/course/settings/gradebook_component_spec.rb new file mode 100644 index 00000000000..5b4fb09665f --- /dev/null +++ b/spec/models/course/settings/gradebook_component_spec.rb @@ -0,0 +1,75 @@ +# frozen_string_literal: true +require 'rails_helper' + +RSpec.describe Course::Settings::GradebookComponent do + let!(:instance) { Instance.default } + with_tenant(:instance) do + let(:course) { create(:course) } + let(:settings) do + context = OpenStruct.new(current_course: course, key: Course::GradebookComponent.key) + Course::Settings::GradebookComponent.new(context) + end + + describe '#weighted_view_enabled' do + it 'returns false by default' do + expect(settings.weighted_view_enabled).to eq(false) + end + end + + describe '#weighted_view_enabled=' do + it 'persists true when set to true' do + settings.weighted_view_enabled = true + course.save! + expect(settings.weighted_view_enabled).to eq(true) + end + + it 'persists false when set to false after being true' do + settings.weighted_view_enabled = true + course.save! + settings.weighted_view_enabled = false + course.save! + expect(settings.weighted_view_enabled).to eq(false) + end + + it 'survives a reload into a fresh component instance' do + settings.weighted_view_enabled = true + course.save! + course.reload + fresh = Course::Settings::GradebookComponent.new( + OpenStruct.new(current_course: course, key: Course::GradebookComponent.key) + ) + expect(fresh.weighted_view_enabled).to eq(true) + end + + it 'handles string "1" as truthy' do + settings.weighted_view_enabled = '1' + expect(settings.weighted_view_enabled).to eq(true) + end + + it 'handles string "true" as truthy' do + settings.weighted_view_enabled = 'true' + expect(settings.weighted_view_enabled).to eq(true) + end + + it 'handles string "on" as truthy' do + settings.weighted_view_enabled = 'on' + expect(settings.weighted_view_enabled).to eq(true) + end + + it 'handles string "0" as falsy' do + settings.weighted_view_enabled = '0' + expect(settings.weighted_view_enabled).to eq(false) + end + + it 'handles string "false" as falsy' do + settings.weighted_view_enabled = 'false' + expect(settings.weighted_view_enabled).to eq(false) + end + + it 'handles string "off" as falsy' do + settings.weighted_view_enabled = 'off' + expect(settings.weighted_view_enabled).to eq(false) + end + end + end +end From 5fc023932fe2196b593250683f2a206a5839266c Mon Sep 17 00:00:00 2001 From: lws49 Date: Tue, 16 Jun 2026 15:33:07 +0800 Subject: [PATCH 3/3] feat(gradebook): add Level contribution to weighted gradebook - map student Level to grade-points via a safe parsed arithmetic formula - fold the Level term into weighted Total and per-student breakdown - add Configure Contributions controls: formula, level, show, clamp - persist a singleton LevelConfig per course (new table + migration) - surface a Level column in the weighted table, toggleable via column picker - warn when a contribution falls outside the range of level contributions - add toggle to clamp contributions within the range of level contributions --- .../course/gradebook_controller.rb | 77 ++- app/models/course.rb | 2 + app/models/course/gradebook/level_config.rb | 127 +++++ .../course/gradebook/index.json.jbuilder | 19 + .../__tests__/ConfigureWeightsPrompt.test.tsx | 346 ++++++++++++- .../__tests__/GradebookIndex.test.tsx | 25 + .../__tests__/GradebookTable.test.tsx | 7 + .../__tests__/WeightedGradebookTable.test.tsx | 478 ++++++++++++++++++ .../__tests__/computeWeighted.test.ts | 240 ++++++++- .../gradebook/__tests__/levelFormula.test.ts | 360 +++++++++++++ .../gradebook/__tests__/operations.test.ts | 55 ++ .../course/gradebook/__tests__/store.test.ts | 300 +++++++++++ .../components/ConfigureWeightsPrompt.tsx | 298 ++++++++++- .../components/WeightedGradebookTable.tsx | 415 ++++++++++++++- .../course/gradebook/computeWeighted.ts | 119 ++++- .../bundles/course/gradebook/levelFormula.ts | 380 ++++++++++++++ .../bundles/course/gradebook/operations.ts | 25 +- .../gradebook/pages/GradebookIndex/index.tsx | 7 + .../app/bundles/course/gradebook/selectors.ts | 6 + client/app/bundles/course/gradebook/store.ts | 35 ++ client/app/types/course/gradebook.ts | 29 ++ client/locales/en.json | 84 ++- client/locales/ko.json | 87 ++++ client/locales/zh.json | 87 ++++ ...16000000_create_gradebook_level_configs.rb | 30 ++ db/schema.rb | 22 +- .../course/gradebook_controller_spec.rb | 189 +++++++ .../course_gradebook_level_configs.rb | 11 + .../course/gradebook/level_config_spec.rb | 336 ++++++++++++ 29 files changed, 4123 insertions(+), 73 deletions(-) create mode 100644 app/models/course/gradebook/level_config.rb create mode 100644 client/app/bundles/course/gradebook/__tests__/levelFormula.test.ts create mode 100644 client/app/bundles/course/gradebook/__tests__/operations.test.ts create mode 100644 client/app/bundles/course/gradebook/levelFormula.ts create mode 100644 db/migrate/20260616000000_create_gradebook_level_configs.rb create mode 100644 spec/factories/course_gradebook_level_configs.rb create mode 100644 spec/models/course/gradebook/level_config_spec.rb diff --git a/app/controllers/course/gradebook_controller.rb b/app/controllers/course/gradebook_controller.rb index 375972e7eb2..3a12f399c87 100644 --- a/app/controllers/course/gradebook_controller.rb +++ b/app/controllers/course/gradebook_controller.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true -class Course::GradebookController < Course::ComponentController +class Course::GradebookController < Course::ComponentController # rubocop:disable Metrics/ClassLength before_action :authorize_read_gradebook! before_action :preload_levels, only: [:index] @@ -10,13 +10,11 @@ def index @published_assessments = fetch_published_assessments @categories, @tabs = fetch_categories_and_tabs @students = fetch_students + @course_max_level = [current_course.levels.count - 1, 0].max 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), - assessment_ids: assessment_ids - ) + load_weighted_view(assessment_ids) if @weighted_view_enabled + load_grades(assessment_ids) + @student_level_contributions = compute_student_level_contributions end end end @@ -25,7 +23,10 @@ def update_weights authorize! :manage_gradebook_weights, current_course updates = (update_weights_params[:weights] || []).map { |entry| parse_weight_entry(entry) } Course::Gradebook::TabContribution.bulk_update(course: current_course, updates: updates) - render json: { weights: serialize_weight_updates(updates) } + level_config = persist_level_contribution + response_body = { weights: serialize_weight_updates(updates) } + response_body[:levelContribution] = serialize_level_contribution(level_config) if level_config + render json: response_body rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotFound => e render json: { errors: { base: e.message } }, status: :unprocessable_entity end @@ -36,6 +37,11 @@ def authorize_read_gradebook! authorize! :read_gradebook, current_course end + def load_weighted_view(assessment_ids) + @level_config = current_course.gradebook_level_config + load_weighted_view_contributions(assessment_ids) + end + def load_weighted_view_contributions(assessment_ids) @tab_contributions = Course::Gradebook::TabContribution. where(tab_id: @tabs.map(&:id)).index_by(&:tab_id) @@ -43,6 +49,14 @@ def load_weighted_view_contributions(assessment_ids) where(assessment_id: assessment_ids).index_by(&:assessment_id) end + def load_grades(assessment_ids) + @assessment_max_grades = Course::Assessment.max_grades(assessment_ids) + @submissions = Course::Assessment::Submission.grade_summary( + student_ids: @students.map(&:user_id), + assessment_ids: assessment_ids + ) + end + # Weights are stored as DECIMAL(5,2); round at the boundary so the echoed response # matches the persisted value and the custom-weight sum check stays exact at 2dp. def parse_weight_entry(entry) @@ -64,6 +78,42 @@ def update_weights_params ) end + def persist_level_contribution + attrs = level_contribution_attrs + return nil if attrs.nil? + + Course::Gradebook::LevelConfig.upsert_for(course: current_course, attrs: attrs) + end + + def level_contribution_attrs + lc = params[:levelContribution] + return nil if lc.blank? + + permitted = lc.permit(:enabled, :formula, :weight, :show, :clamp, formulaAst: {}) + formula_ast = permitted[:formulaAst] + attrs = { + enabled: permitted[:enabled], + formula: permitted[:formula], + formula_ast: formula_ast.present? ? formula_ast.to_h : nil, + weight: permitted[:weight], + show: permitted[:show] + } + # Only forward :clamp when the client actually sent it, so LevelConfig.upsert_for's + # default-true fallback applies on omission rather than persisting NULL. + attrs[:clamp] = permitted[:clamp] if permitted.key?(:clamp) + attrs + end + + def serialize_level_contribution(config) + { + enabled: config.enabled, + formula: config.formula, + weight: config.weight.to_f, + show: config.show, + clamp: config.clamp + } + end + def serialize_weight_updates(updates) updates.map do |u| entry = { tabId: u[:tab_id], weight: u[:weight], weightMode: u[:weight_mode].to_s, @@ -88,7 +138,8 @@ def fetch_categories_and_tabs def fetch_students current_course.course_users.students.without_phantom_users. - calculated(:experience_points).includes(user: :emails).to_a + calculated(:experience_points).includes(user: :emails).to_a. + sort_by { |cu| cu.user.name } end def fetch_published_assessments @@ -105,4 +156,12 @@ def fetch_published_assessments def preload_levels current_course.levels.to_a end + + def compute_student_level_contributions + return {} unless @level_config&.enabled + + @students.to_h do |cu| + [cu.user_id, @level_config.evaluate_for(level: cu.level_number)] + end + end end diff --git a/app/models/course.rb b/app/models/course.rb index ed3220b27b3..e5b6c0cde6c 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -55,6 +55,8 @@ class Course < ApplicationRecord # rubocop:disable Metrics/ClassLength has_many :assessments, through: :assessment_categories has_many :gradebook_contributions, class_name: 'Course::Gradebook::TabContribution', dependent: :destroy, inverse_of: :course + has_one :gradebook_level_config, class_name: 'Course::Gradebook::LevelConfig', + 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', diff --git a/app/models/course/gradebook/level_config.rb b/app/models/course/gradebook/level_config.rb new file mode 100644 index 00000000000..97c35c58fac --- /dev/null +++ b/app/models/course/gradebook/level_config.rb @@ -0,0 +1,127 @@ +# frozen_string_literal: true +class Course::Gradebook::LevelConfig < ApplicationRecord # rubocop:disable Metrics/ClassLength + belongs_to :course, inverse_of: :gradebook_level_config + + validates :weight, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 100 } + validates :formula, presence: true, if: :enabled + validates :formula_ast, presence: true, if: :enabled + validates :course_id, uniqueness: true + validate :formula_ast_structure + + # Upserts the singleton Level config for a course from a normalized attrs hash + # (symbol keys, snake_case). Raises ActiveRecord::RecordInvalid on validation failure. + def self.upsert_for(course:, attrs:) + config = find_or_initialize_by(course_id: course.id) + config.assign_attributes( + enabled: ActiveRecord::Type::Boolean.new.cast(attrs[:enabled]), + formula: attrs[:formula].to_s, + formula_ast: attrs[:formula_ast], + weight: attrs[:weight].to_f, + show: ActiveRecord::Type::Boolean.new.cast(attrs[:show]) == true, + clamp: attrs.key?(:clamp) ? ActiveRecord::Type::Boolean.new.cast(attrs[:clamp]) : true + ) + config.save! + config + end + + def evaluate_for(level:) + return nil unless enabled && formula_ast.present? + + result = evaluate_node(formula_ast, level.to_f) + return nil unless result.is_a?(Numeric) && result.finite? + + value = result.to_f + clamp ? value.clamp(0.0, weight.to_f) : value + rescue StandardError + nil + end + + BINOPS = %w[+ - * /].freeze + CALL1_FNS = %w[floor ceil round].freeze + CALL2_FNS = %w[min max].freeze + + def self.valid_ast?(node, depth = 0) + return false if depth > 20 + return false unless node.is_a?(Hash) && node['type'].is_a?(String) + + valid_node_type?(node, depth) + end + + def self.valid_node_type?(node, depth) + case node['type'] + when 'num' then node['value'].is_a?(Numeric) + when 'var' then node['name'] == 'level' + when 'neg' then valid_operand?(node, 'operand', depth) + when 'binop' then valid_binop?(node, depth) + when 'call1' then valid_call1?(node, depth) + when 'call2' then valid_call2?(node, depth) + else false + end + end + + def self.valid_operand?(node, key, depth) + node.key?(key) && valid_ast?(node[key], depth + 1) + end + + def self.valid_binop?(node, depth) + BINOPS.include?(node['op']) && + valid_operand?(node, 'left', depth) && + valid_operand?(node, 'right', depth) + end + + def self.valid_call1?(node, depth) + CALL1_FNS.include?(node['fn']) && valid_operand?(node, 'arg', depth) + end + + def self.valid_call2?(node, depth) + CALL2_FNS.include?(node['fn']) && + valid_operand?(node, 'a', depth) && + valid_operand?(node, 'b', depth) + end + private_class_method :valid_node_type?, :valid_operand?, :valid_binop?, :valid_call1?, :valid_call2? + + private + + def formula_ast_structure + return if formula_ast.blank? + + errors.add(:formula_ast, 'has an invalid structure') unless self.class.valid_ast?(formula_ast) + end + + def evaluate_node(node, level) + case node['type'] + when 'num' then node['value'].to_f + when 'var' then level + when 'neg' then -evaluate_node(node['operand'], level) + when 'binop' then evaluate_binop(node, level) + when 'call1' then evaluate_call1(node, level) + when 'call2' then evaluate_call2(node, level) + end + end + + def evaluate_binop(node, level) + l = evaluate_node(node['left'], level) + r = evaluate_node(node['right'], level) + case node['op'] + when '+' then l + r + when '-' then l - r + when '*' then l * r + when '/' then (r == 0) ? Float::NAN : l / r + end + end + + def evaluate_call1(node, level) + a = evaluate_node(node['arg'], level) + case node['fn'] + when 'floor' then a.floor.to_f + when 'ceil' then a.ceil.to_f + when 'round' then a.round.to_f + end + end + + def evaluate_call2(node, level) + a = evaluate_node(node['a'], level) + b = evaluate_node(node['b'], level) + (node['fn'] == 'min') ? [a, b].min : [a, b].max + end +end diff --git a/app/views/course/gradebook/index.json.jbuilder b/app/views/course/gradebook/index.json.jbuilder index 3241fa2b687..a493cfe953e 100644 --- a/app/views/course/gradebook/index.json.jbuilder +++ b/app/views/course/gradebook/index.json.jbuilder @@ -37,6 +37,7 @@ json.students @students do |course_user| json.externalId course_user.external_id json.level course_user.level_number json.totalXp course_user.experience_points + json.levelContribution @student_level_contributions[course_user.user_id] end json.submissions @submissions do |sub| @@ -47,3 +48,21 @@ json.submissions @submissions do |sub| end json.gamificationEnabled current_course.gamified? + +json.courseMaxLevel @course_max_level + +json.levelContribution do + if @weighted_view_enabled && @level_config + json.enabled @level_config.enabled + json.formula @level_config.formula + json.weight @level_config.weight.to_f + json.show @level_config.show + json.clamp @level_config.clamp + else + json.enabled false + json.formula '' + json.weight 0 + json.show false + json.clamp true + end +end diff --git a/client/app/bundles/course/gradebook/__tests__/ConfigureWeightsPrompt.test.tsx b/client/app/bundles/course/gradebook/__tests__/ConfigureWeightsPrompt.test.tsx index 2f1f87a0f6c..78949dd4d26 100644 --- a/client/app/bundles/course/gradebook/__tests__/ConfigureWeightsPrompt.test.tsx +++ b/client/app/bundles/course/gradebook/__tests__/ConfigureWeightsPrompt.test.tsx @@ -21,6 +21,7 @@ const A1 = 'Assignment 1'; const A2 = 'Assignment 2'; const ASSIGN_A1 = 'Assignments: Assignment 1'; const INCLUDE_A1 = 'Include Assignment 1 in grade'; +const LEVEL_FORMULA = 'min(level, 10) * 0.8'; const categories = [{ id: 1, title: 'Missions' }]; const tabs = [ @@ -33,13 +34,43 @@ const assessments = [ { id: 102, title: A2, tabId: 10, maxGrade: 50 }, ]; +const defaultLevelContribution = { + enabled: false, + formula: '', + weight: 0, + show: false, + clamp: true, +}; + +const enabledLevel = (over = {}): Record => ({ + enabled: true, + formula: LEVEL_FORMULA, + weight: 8, + show: true, + clamp: true, + ...over, +}); + +const students = [ + { id: 1, name: 'A', email: 'a@x', externalId: null, level: 5, totalXp: 0 }, + { id: 2, name: 'B', email: 'b@x', externalId: null, level: 12, totalXp: 0 }, +]; + +const levelZeroStudent = [ + { id: 1, name: 'A', email: 'a@x', externalId: null, level: 0, totalXp: 0 }, +]; + const setup = (overrides = {}): ReturnType => render( , @@ -191,24 +222,27 @@ describe('', () => { ); fireEvent.click(screen.getByRole('button', { name: /save/i })); await waitFor(() => { - expect(operations.updateGradebookWeights).toHaveBeenCalledWith([ - { - tabId: 10, - weight: 50, - weightMode: 'custom', - excludedAssessmentIds: [], - assessmentWeights: [ - { assessmentId: 101, weight: 25 }, - { assessmentId: 102, weight: 25 }, - ], - }, - { - tabId: 11, - weight: 50, - weightMode: 'equal', - excludedAssessmentIds: [], - }, - ]); + expect(operations.updateGradebookWeights).toHaveBeenCalledWith( + [ + { + tabId: 10, + weight: 50, + weightMode: 'custom', + excludedAssessmentIds: [], + assessmentWeights: [ + { assessmentId: 101, weight: 25 }, + { assessmentId: 102, weight: 25 }, + ], + }, + { + tabId: 11, + weight: 50, + weightMode: 'equal', + excludedAssessmentIds: [], + }, + ], + expect.objectContaining({ enabled: false }), + ); }); }); @@ -272,6 +306,41 @@ describe('', () => { within(modeGroup('Optional')).getByRole('radio', { name: /custom/i }), ).toBeDisabled(); }); + + it('defaults the clamp toggle to checked', () => { + setup({ gamificationEnabled: true, levelContribution: enabledLevel() }); + expect( + screen.getByRole('checkbox', { name: /Keep results between/i }), + ).toBeChecked(); + }); + + it('sends clamp in the save payload', async () => { + setup({ gamificationEnabled: true, levelContribution: enabledLevel() }); + fireEvent.click( + screen.getByRole('checkbox', { name: /Keep results between/i }), + ); + fireEvent.click(screen.getByRole('button', { name: /save/i })); + await waitFor(() => + expect(operations.updateGradebookWeights).toHaveBeenCalled(), + ); + const arg = (operations.updateGradebookWeights as jest.Mock).mock + .calls[0][1]; + expect(arg.clamp).toBe(false); + }); + + it('suppresses the range alert when clamp is off', () => { + setup({ + gamificationEnabled: true, + students, // levels 5, 12 + levelContribution: enabledLevel({ + formula: 'level * 5', + weight: 10, + clamp: false, + }), + // raw 25 and 60 are above 10, but clamp is off -> no alert + }); + expect(screen.queryByText(/above 10/i)).not.toBeInTheDocument(); + }); }); describe('per-assessment exclusion', () => { @@ -544,3 +613,244 @@ describe('per-assessment exclusion', () => { }); }); }); + +describe('level contribution section', () => { + beforeEach(() => jest.clearAllMocks()); + + it('is not rendered when gamificationEnabled is false', () => { + setup({ gamificationEnabled: false }); + expect(screen.queryByText(/level contribution/i)).not.toBeInTheDocument(); + }); + + it('renders the level section when gamificationEnabled is true', () => { + setup({ gamificationEnabled: true }); + expect(screen.getByText(/level contribution/i)).toBeInTheDocument(); + }); + + it('seeds enabled, formula, weight, and show from the levelContribution prop', () => { + setup({ gamificationEnabled: true, levelContribution: enabledLevel() }); + expect(screen.getByLabelText(/formula/i)).toHaveValue(LEVEL_FORMULA); + // Exact name: the clamp label ("...max level contributions") also matches + // /level contribution/i, so an exact string isolates the enable toggle. + expect( + screen.getByRole('checkbox', { name: 'Level contribution' }), + ).toBeChecked(); + }); + + it('no longer offers a "custom max level" control', () => { + setup({ gamificationEnabled: true, levelContribution: enabledLevel() }); + expect( + screen.queryByRole('checkbox', { name: /custom max level/i }), + ).not.toBeInTheDocument(); + // The removed control was a numeric field; scope to spinbutton so the clamp + // checkbox ("...max level contributions") isn't mistaken for it. + expect( + screen.queryByRole('spinbutton', { name: /max level/i }), + ).not.toBeInTheDocument(); + }); + + it('shows the highest student level and the course maximum level', () => { + setup({ + gamificationEnabled: true, + courseMaxLevel: 14, + students, // levels 5 and 12 + levelContribution: enabledLevel(), + }); + expect(screen.getByText(/Highest student level: 12/)).toBeInTheDocument(); + expect(screen.getByText(/Course maximum level: 14/)).toBeInTheDocument(); + }); + + it('renders the level weight as a bare number with no caption or tooltip', () => { + setup({ gamificationEnabled: true, levelContribution: enabledLevel() }); + expect( + screen.getByRole('spinbutton', { name: 'Level contribution' }), + ).toBeInTheDocument(); + expect(screen.queryByText(/Suggested maximum/i)).not.toBeInTheDocument(); + expect(screen.queryByText(/never caps or blocks/i)).not.toBeInTheDocument(); + }); + + it('describes the level term without the word "bonus"', () => { + setup({ gamificationEnabled: true, levelContribution: enabledLevel() }); + expect( + screen.getByText(/Adds grade-points from each student/i), + ).toBeInTheDocument(); + expect(screen.queryByText(/bonus/i)).not.toBeInTheDocument(); + }); + + it('always shows the formula syntax reference, even when the formula is valid', () => { + setup({ + gamificationEnabled: true, + students, // valid default formula min(level, 10) for these students + levelContribution: enabledLevel(), + }); + expect( + screen.getByText(/floor, ceil, round, min and max/i), + ).toBeInTheDocument(); + expect(screen.getByText(/arithmetic operators/i)).toBeInTheDocument(); + }); + + it('shows no warning when every contribution is in range', () => { + setup({ + gamificationEnabled: true, + students, // levels 5, 12 → min(level,10)*0.8 = 4, 8 — both within 0..8 + levelContribution: enabledLevel(), + }); + expect(screen.queryByText(/below 0/i)).not.toBeInTheDocument(); + expect(screen.queryByText(/above/i)).not.toBeInTheDocument(); + }); + + it('names the offending student(s) only on the lower bound when none exceed the max', () => { + setup({ + gamificationEnabled: true, + students, // levels 5, 12 + levelContribution: enabledLevel({ formula: 'level - 8', weight: 10 }), + // contributions -3 (A) and 4 (B): only A is below 0 + }); + expect(screen.getByText('A (-3.00) is below 0.')).toBeInTheDocument(); + expect( + screen.getByText(/These contributions will be set to 0\./), + ).toBeInTheDocument(); + expect(screen.queryByText(/above/i)).not.toBeInTheDocument(); + }); + + it('names the worst offenders (value, highest first) only on the upper bound', () => { + setup({ + gamificationEnabled: true, + students, // levels 5, 12 + levelContribution: enabledLevel({ formula: 'level * 5', weight: 10 }), + // contributions 25 (A) and 60 (B), both above 10 + }); + expect( + screen.getByText('B (60.00) and A (25.00) are above 10.'), + ).toBeInTheDocument(); + expect( + screen.getByText(/These contributions will be set to 10\./), + ).toBeInTheDocument(); + expect(screen.queryByText(/below 0/i)).not.toBeInTheDocument(); + }); + + it('caps the list at two names and appends "and N more"', () => { + const many = [ + { + id: 1, + name: 'S1', + email: 'a@x', + externalId: null, + level: 3, + totalXp: 0, + }, + { + id: 2, + name: 'S2', + email: 'b@x', + externalId: null, + level: 4, + totalXp: 0, + }, + { + id: 3, + name: 'S3', + email: 'c@x', + externalId: null, + level: 5, + totalXp: 0, + }, + { + id: 4, + name: 'S4', + email: 'd@x', + externalId: null, + level: 6, + totalXp: 0, + }, + ]; + setup({ + gamificationEnabled: true, + students: many, + levelContribution: enabledLevel({ formula: 'level * 5', weight: 10 }), + // contributions 15,20,25,30 — all above 10; top two then "and 2 more" + }); + expect( + screen.getByText('S4 (30.00), S3 (25.00) and 2 more are above 10.'), + ).toBeInTheDocument(); + }); + + it('names offenders on both bounds with a combined fix instruction', () => { + setup({ + gamificationEnabled: true, + students, // levels 5, 12 + levelContribution: enabledLevel({ + formula: 'level * 5 - 30', + weight: 10, + }), + // contributions -5 (A) and 30 (B): one below 0, one above 10 + }); + expect( + screen.getByText('B (30.00) is above 10. A (-5.00) is below 0.'), + ).toBeInTheDocument(); + expect( + screen.getByText( + /Contributions below 0 will be set to 0, and contributions above 10 will be set to 10\./, + ), + ).toBeInTheDocument(); + }); + + it('seeds the level weight to the course max level when unconfigured (works on first open)', () => { + setup({ + gamificationEnabled: true, + courseMaxLevel: 30, + students, // levels 5, 12 + // fresh course: enabled, but no formula / weight yet + levelContribution: { enabled: true, formula: '', weight: 0, show: false }, + }); + // weight defaults to 30 and the formula seeds to min(level, 30) → in range, + // so there is no out-of-range warning. + expect( + screen.getByRole('spinbutton', { name: 'Level contribution' }), + ).toHaveValue(30); + expect(screen.queryByText(/below 0/i)).not.toBeInTheDocument(); + }); + + it('shows a parse error and disables Save when the formula is invalid', () => { + setup({ + gamificationEnabled: true, + levelContribution: enabledLevel({ formula: 'level +' }), + }); + expect(screen.getByRole('button', { name: /save/i })).toBeDisabled(); + }); + + it('includes levelContribution in the save payload without a maxLevel field', async () => { + setup({ gamificationEnabled: true, levelContribution: enabledLevel() }); + fireEvent.click(screen.getByRole('button', { name: /save/i })); + await waitFor(() => + expect(operations.updateGradebookWeights).toHaveBeenCalled(), + ); + const [, lvl] = (operations.updateGradebookWeights as jest.Mock).mock + .calls[0]; + expect(lvl).toMatchObject({ + enabled: true, + formula: LEVEL_FORMULA, + weight: 8, + show: true, + }); + expect(lvl).not.toHaveProperty('maxLevel'); + }); + + it('adds the level weight to the Total when enabled', () => { + // tabs sum to 100; level adds 8 → Total should be 108 + setup({ gamificationEnabled: true, levelContribution: enabledLevel() }); + expect(screen.getByText(/Total:\s*108%/)).toBeInTheDocument(); + }); + + it('warns and names students when the formula divides by zero for them', async () => { + setup({ + gamificationEnabled: true, + students: levelZeroStudent, + levelContribution: enabledLevel({ formula: '100 / level' }), + }); + // ...render with the level contribution enabled and a student at level 0... + // ...set the formula field to '100 / level'... + expect(await screen.findByText(/divides by zero/i)).toBeInTheDocument(); + expect(screen.getByText(/set to 0/i)).toBeInTheDocument(); + }); +}); diff --git a/client/app/bundles/course/gradebook/__tests__/GradebookIndex.test.tsx b/client/app/bundles/course/gradebook/__tests__/GradebookIndex.test.tsx index 63853662cbd..1c07b29cbc9 100644 --- a/client/app/bundles/course/gradebook/__tests__/GradebookIndex.test.tsx +++ b/client/app/bundles/course/gradebook/__tests__/GradebookIndex.test.tsx @@ -34,6 +34,14 @@ const emptyState = { gamificationEnabled: false, weightedViewEnabled: false, canManageWeights: false, + courseMaxLevel: 0, + levelContribution: { + enabled: false, + formula: '', + weight: 0, + show: false, + clamp: true, + }, }, }; @@ -47,6 +55,14 @@ const noStudentsState = { gamificationEnabled: false, weightedViewEnabled: false, canManageWeights: false, + courseMaxLevel: 0, + levelContribution: { + enabled: false, + formula: '', + weight: 0, + show: false, + clamp: true, + }, }, }; @@ -63,6 +79,7 @@ const populatedState = { externalId: null, level: 3, totalXp: 150, + levelContribution: null, }, ], submissions: [ @@ -71,6 +88,14 @@ const populatedState = { gamificationEnabled: false, weightedViewEnabled: false, canManageWeights: false, + courseMaxLevel: 0, + levelContribution: { + enabled: false, + formula: '', + weight: 0, + show: false, + clamp: true, + }, }, }; diff --git a/client/app/bundles/course/gradebook/__tests__/GradebookTable.test.tsx b/client/app/bundles/course/gradebook/__tests__/GradebookTable.test.tsx index 449fcc5a618..b70451c63bb 100644 --- a/client/app/bundles/course/gradebook/__tests__/GradebookTable.test.tsx +++ b/client/app/bundles/course/gradebook/__tests__/GradebookTable.test.tsx @@ -24,6 +24,7 @@ const students: StudentData[] = [ externalId: null, level: 3, totalXp: 150, + levelContribution: null, }, { id: 2, @@ -32,6 +33,7 @@ const students: StudentData[] = [ externalId: null, level: 5, totalXp: 300, + levelContribution: null, }, ]; const submissions: SubmissionData[] = [ @@ -46,6 +48,7 @@ const makeStudents = (n: number): StudentData[] => externalId: null, level: 1, totalXp: 0, + levelContribution: null, })); // Asserts the given texts appear in this top-to-bottom DOM order. @@ -420,6 +423,7 @@ describe('GradebookTable', () => { externalId: 'EXT-001', level: 3, totalXp: 150, + levelContribution: null, }, { id: 2, @@ -428,6 +432,7 @@ describe('GradebookTable', () => { externalId: null, level: 5, totalXp: 300, + levelContribution: null, }, ]; @@ -468,6 +473,7 @@ describe('GradebookTable', () => { externalId: '', level: 3, totalXp: 150, + levelContribution: null, }, ]; renderWith(studentsWithBlankExtId); @@ -658,6 +664,7 @@ describe('GradebookTable', () => { externalId: null, level: 1, totalXp: 0, + levelContribution: null, }); const renderGrades = ( diff --git a/client/app/bundles/course/gradebook/__tests__/WeightedGradebookTable.test.tsx b/client/app/bundles/course/gradebook/__tests__/WeightedGradebookTable.test.tsx index f0464daae6b..fcfdb317364 100644 --- a/client/app/bundles/course/gradebook/__tests__/WeightedGradebookTable.test.tsx +++ b/client/app/bundles/course/gradebook/__tests__/WeightedGradebookTable.test.tsx @@ -1,6 +1,7 @@ import userEvent from '@testing-library/user-event'; import { store as appStore } from 'store'; import { render, screen, waitFor, within } from 'test-utils'; +import TestApp from 'utilities/TestApp'; import WeightedGradebookTable from '../components/WeightedGradebookTable'; import type { @@ -18,6 +19,7 @@ jest.mock('lib/components/wrappers/I18nProvider'); const USER_ID = 42; const WEIGHTED_STORAGE_KEY = `${USER_ID}:gradebook_weighted_columns_1`; const EXTERNAL_ID = 'External ID'; +const LEVEL_CONTRIBUTION = 'Level Contribution'; // Mirrors the component's data-testid for a breakdown row: // `breakdown-row-${studentId}-${tabId}-${assessmentId}`. @@ -65,6 +67,7 @@ const makeStudent = (id: number, name: string): StudentData => ({ externalId: null, level: 1, totalXp: 0, + levelContribution: null, }); const makeSub = ( @@ -73,6 +76,14 @@ const makeSub = ( grade: number | null, ): SubmissionData => ({ submissionId: 0, studentId, assessmentId, grade }); +const defaultLevelContribution = { + enabled: false, + formula: '', + weight: 0, + show: false, + clamp: true, +}; + interface RenderWeightedOptions { categories?: CategoryData[]; tabs?: TabData[]; @@ -82,6 +93,9 @@ interface RenderWeightedOptions { canManageWeights?: boolean; courseTitle?: string; courseId?: number; + gamificationEnabled?: boolean; + courseMaxLevel?: number; + levelContribution?: typeof defaultLevelContribution; } const renderWeighted = ( @@ -100,7 +114,10 @@ const renderWeighted = ( canManageWeights={opts.canManageWeights ?? true} categories={cats} courseId={opts.courseId ?? 1} + courseMaxLevel={opts.courseMaxLevel ?? 0} courseTitle={opts.courseTitle ?? 'Test Course'} + gamificationEnabled={opts.gamificationEnabled ?? false} + levelContribution={opts.levelContribution ?? defaultLevelContribution} students={students} submissions={submissions} tabs={tabs} @@ -834,6 +851,90 @@ describe('WeightedGradebookTable', () => { expect(cells[3]).toBeEmptyDOMElement(); }); + it('aligns breakdown tab values with the parent columns when both level columns are shown', async () => { + const user = userEvent.setup(); + renderWeighted({ + ...expandable, + students: [ + { ...makeStudent(1, 'Alice'), level: 8, levelContribution: 15 }, + ], + gamificationEnabled: true, + courseMaxLevel: 30, + levelContribution: { + enabled: true, + show: true, + weight: 30, + formula: 'level', + clamp: true, + }, + }); + await user.click(screen.getByRole('button', { name: /expand Alice/i })); + + // With Raw Level + Level Contribution present and no identity columns: + // checkbox | Name | Level | Level Contribution | Missions | Quizzes | Total + const detail = await screen.findByTestId(breakdownRowId(1, 10, 1)); + const cells = within(detail).getAllByRole('cell'); + expect(cells).toHaveLength(7); + // Mission 1's contribution (24) lands under Missions (index 4) — not + // shifted left into the Level (2) or Level Contribution (3) columns. + expect(cells[2]).toBeEmptyDOMElement(); // Raw Level + expect(cells[3]).toBeEmptyDOMElement(); // Level Contribution + expect(cells[4]).toHaveTextContent('24'); // Missions + }); + + it('gives every column an explicit-width colgroup , including Total', () => { + renderWeighted({ + ...expandable, + students: [ + { ...makeStudent(1, 'Alice'), level: 8, levelContribution: 15 }, + ], + gamificationEnabled: true, + courseMaxLevel: 30, + levelContribution: { + enabled: true, + show: true, + weight: 30, + formula: 'level', + clamp: true, + }, + }); + + const cols = [...document.querySelectorAll('colgroup col')]; + const studentRow = screen.getByText('Alice').closest('tr')!; + const cells = within(studentRow).getAllByRole('cell'); + expect(cells).toHaveLength(7); + expect(cols).toHaveLength(cells.length); + cols.forEach((col) => expect(col.style.width).not.toBe('')); + }); + + it('shows the level contribution in its own column on the level breakdown row', async () => { + const user = userEvent.setup(); + renderWeighted({ + ...expandable, + students: [ + { ...makeStudent(1, 'Alice'), level: 8, levelContribution: 15 }, + ], + gamificationEnabled: true, + courseMaxLevel: 30, + levelContribution: { + enabled: true, + show: true, + weight: 30, + formula: 'level', + clamp: true, + }, + }); + await user.click(screen.getByRole('button', { name: /expand Alice/i })); + + // Synthetic Level breakdown row (tabId -1, assessmentId -1): its + // contribution (15) sits under Level Contribution, tab columns stay empty. + const levelRow = await screen.findByTestId(breakdownRowId(1, -1, -1)); + const cells = within(levelRow).getAllByRole('cell'); + expect(cells).toHaveLength(7); + expect(cells[3]).toHaveTextContent('15'); // Level Contribution + expect(cells[4]).toBeEmptyDOMElement(); // Missions empty on the level row + }); + it('renders each assessment as a grade/maxGrade percentage in percent mode', async () => { const user = userEvent.setup(); renderWeighted(expandable); @@ -1331,3 +1432,380 @@ describe('WeightedGradebookTable', () => { }); }); }); + +describe('level contribution columns', () => { + beforeEach(() => localStorage.clear()); + + // `enabled` drives the always-on "Level Contribution" column; `show` + // additionally drives the raw "Level" column. Formula uses only the `level` + // grammar variable so these tests assert layout without coupling to the + // contribution-value computation (owned elsewhere). + const levelOn = { + enabled: true, + formula: 'level', + weight: 10, + show: true, + clamp: true, + }; + + // The first row carries every column's top header cell left-to-right + // (Name, raw Level, Level Contribution, category groups, Total) — the cleanest + // place to assert presence and ordering. + const headerRow1 = (): HTMLElement => + document.querySelector('thead tr') as HTMLElement; + + it('shows neither level column when gamification is off', () => { + renderWeighted({ gamificationEnabled: false, levelContribution: levelOn }); + expect(screen.queryByText(LEVEL_CONTRIBUTION)).not.toBeInTheDocument(); + expect(screen.queryByText('Level')).not.toBeInTheDocument(); + }); + + it('shows neither level column when the contribution is disabled', () => { + renderWeighted({ + gamificationEnabled: true, + levelContribution: { ...levelOn, enabled: false }, + }); + expect(screen.queryByText(LEVEL_CONTRIBUTION)).not.toBeInTheDocument(); + expect(screen.queryByText('Level')).not.toBeInTheDocument(); + }); + + it('shows Level Contribution but hides raw Level when show is off', () => { + renderWeighted({ + gamificationEnabled: true, + levelContribution: { ...levelOn, show: false }, + }); + expect( + within(headerRow1()).getByText(LEVEL_CONTRIBUTION), + ).toBeInTheDocument(); + expect(within(headerRow1()).queryByText('Level')).not.toBeInTheDocument(); + }); + + it('shows both Level Contribution and raw Level when enabled and show are on', () => { + renderWeighted({ gamificationEnabled: true, levelContribution: levelOn }); + expect( + within(headerRow1()).getByText(LEVEL_CONTRIBUTION), + ).toBeInTheDocument(); + expect(within(headerRow1()).getByText('Level')).toBeInTheDocument(); + }); + + it('orders raw Level last among student info and Level Contribution first among contributions', () => { + renderWeighted({ + gamificationEnabled: true, + levelContribution: levelOn, + students: [{ ...makeStudent(1, 'Alice'), externalId: 'EXT-1' }], + }); + const headers = within(headerRow1()) + .getAllByRole('columnheader') + .map((c) => c.textContent); + const ext = headers.indexOf('External ID'); + const raw = headers.indexOf('Level'); + const contrib = headers.indexOf(LEVEL_CONTRIBUTION); + const cat = headers.indexOf('Cat A'); + // External ID (student info) < raw Level (last student info) < Level + // Contribution (first contribution) < Cat A (tab group). + expect(ext).toBeLessThan(raw); + expect(raw).toBeLessThan(contrib); + expect(contrib).toBeLessThan(cat); + }); + + it("renders each student's actual level in the raw Level column", () => { + renderWeighted({ + gamificationEnabled: true, + // level * 2 keeps the contribution value (14) distinct from the raw level + // (7) so the assertion targets the raw Level cell unambiguously. + levelContribution: { ...levelOn, formula: 'level * 2' }, + students: [{ ...makeStudent(1, 'Alice'), level: 7 }], + }); + const aliceRow = screen.getByText('Alice').closest('tr')!; + expect(within(aliceRow).getByText('7')).toBeInTheDocument(); + }); + + it('lists the Level row first in the expanded breakdown', async () => { + const user = userEvent.setup(); + renderWeighted({ + gamificationEnabled: true, + levelContribution: { ...levelOn, formula: 'level' }, + tabs: [makeTab(10, 'Tab 1', 1, 100)], + assessments: [makeAssessment(100, 'Quiz 1', 10, 10)], + // BE pre-computes the per-student contribution; Alice (level 1) under + // formula 'level' → 1. The component renders this value, not the formula. + students: [{ ...makeStudent(1, 'Alice'), levelContribution: 1 }], + submissions: [makeSub(1, 100, 8)], + }); + await user.click(screen.getByRole('button', { name: /expand Alice/i })); + const bdRows = await screen.findAllByTestId(/^breakdown-row-/); + // Synthetic level tab uses LEVEL_TAB_ID (-1); its row must come first, before + // the real tab (id 10) rows — mirroring the column order. + expect(bdRows[0].getAttribute('data-testid')).toMatch( + /^breakdown-row-1--1-/, + ); + expect( + bdRows.some((r) => r.getAttribute('data-testid')?.includes('-10-')), + ).toBe(true); + }); + + it('shows the raw level (no max-level denominator) in the Level breakdown row', async () => { + const user = userEvent.setup(); + renderWeighted({ + gamificationEnabled: true, + // courseMaxLevel plays no part in the contribution, so showing "14/20" + // would falsely imply a level/maxLevel derivation. Expect a plain "Level 14". + levelContribution: { ...levelOn, formula: 'level', weight: 20 }, + courseMaxLevel: 20, + tabs: [makeTab(10, 'Tab 1', 1, 100)], + assessments: [makeAssessment(100, 'Quiz 1', 10, 10)], + // BE-supplied contribution; Alice (level 14) under formula 'level' → 14. + students: [ + { ...makeStudent(1, 'Alice'), level: 14, levelContribution: 14 }, + ], + submissions: [makeSub(1, 100, 8)], + }); + await user.click(screen.getByRole('button', { name: /expand Alice/i })); + const levelRow = (await screen.findAllByTestId(/^breakdown-row-1--1-/))[0]; + expect(within(levelRow).getByText(/Level 14/)).toBeInTheDocument(); + // The misleading "14/20" fraction must NOT appear. + expect( + within(levelRow).queryByText(/14\s*\/\s*\d+/), + ).not.toBeInTheDocument(); + }); + + // Over-budget warning: the level weight is a suggested maximum, so a formula can + // push a student's contribution past it or below 0. The Level Contribution subheader + // shows a bound-aware message (above-only, below-only, or both). + it('shows above-only tooltip when a student exceeds the level weight', () => { + renderWeighted({ + gamificationEnabled: true, + // level * 5 → Alice (level 3) contributes 15, over the 10-pt budget. + levelContribution: { ...levelOn, formula: 'level * 5', weight: 10 }, + students: [{ ...makeStudent(1, 'Alice'), level: 3 }], + }); + expect( + screen.getByLabelText(/above the maximum level contributions/i), + ).toBeInTheDocument(); + }); + + it('shows below-only tooltip when a student contribution falls below 0', () => { + renderWeighted({ + gamificationEnabled: true, + // level - 10 → Alice (level 1) contributes -9, below 0. + levelContribution: { ...levelOn, formula: 'level - 10', weight: 10 }, + students: [{ ...makeStudent(1, 'Alice'), level: 1 }], + }); + expect(screen.getByLabelText(/below 0/i)).toBeInTheDocument(); + }); + + it('shows both-bounds tooltip when students breach both bounds', () => { + renderWeighted({ + gamificationEnabled: true, + // level - 3 with weight 5: Alice (level 1) → -2 (below 0), Bob (level 10) → 7 (above 5). + levelContribution: { ...levelOn, formula: 'level - 3', weight: 5 }, + students: [ + { ...makeStudent(1, 'Alice'), level: 1 }, + { ...makeStudent(2, 'Bob'), level: 10 }, + ], + }); + expect( + screen.getByLabelText(/outside the valid range/i), + ).toBeInTheDocument(); + }); + + it('does not flag the subheader when every student is within budget', () => { + renderWeighted({ + gamificationEnabled: true, + // level → Alice (level 3) contributes 3, within the 10-pt budget. + levelContribution: { ...levelOn, formula: 'level', weight: 10 }, + students: [{ ...makeStudent(1, 'Alice'), level: 3 }], + }); + expect( + screen.queryByLabelText( + /above the level weight|below 0|outside the valid range/i, + ), + ).not.toBeInTheDocument(); + }); + + // Regression: neither level column is user-pickable, so a stale persisted-hidden + // entry from a prior session must NOT keep them hidden (no picker to recover). + it('keeps Level Contribution visible despite a stale persisted-hidden entry', () => { + localStorage.setItem( + WEIGHTED_STORAGE_KEY, + JSON.stringify({ levelContribution: false }), + ); + renderWeighted({ gamificationEnabled: true, levelContribution: levelOn }); + expect( + within(headerRow1()).getByText(LEVEL_CONTRIBUTION), + ).toBeInTheDocument(); + }); + + it('keeps raw Level visible despite a stale persisted-hidden entry', () => { + localStorage.setItem( + WEIGHTED_STORAGE_KEY, + JSON.stringify({ level: false }), + ); + renderWeighted({ gamificationEnabled: true, levelContribution: levelOn }); + expect(within(headerRow1()).getByText('Level')).toBeInTheDocument(); + }); + + // Regression: toggling settings while the table is already mounted (e.g. the + // teacher saves the dialog) must reveal the affected column live. + it('reveals raw Level when show is toggled on after mount', () => { + const props = { + assessments: [makeAssessment(100, 'Quiz 1', 10, 150)], + canManageWeights: true, + categories: [makeCategory(1, 'Cat A')], + courseId: 1, + courseMaxLevel: 10, + courseTitle: 'Test Course', + gamificationEnabled: true, + students: [makeStudent(1, 'Alice')], + submissions: [], + tabs: [makeTab(10, 'Tab 1', 1, 100)], + }; + const { rerender } = render( + , + { state: userState }, + ); + expect(within(headerRow1()).queryByText('Level')).not.toBeInTheDocument(); + + // rerender bypasses test-utils' TestApp wrapper, so re-wrap to keep providers. + rerender( + + + , + ); + expect(within(headerRow1()).getByText('Level')).toBeInTheDocument(); + }); + + it('reveals Level Contribution when the contribution is enabled after mount', () => { + const props = { + assessments: [makeAssessment(100, 'Quiz 1', 10, 150)], + canManageWeights: true, + categories: [makeCategory(1, 'Cat A')], + courseId: 1, + courseMaxLevel: 10, + courseTitle: 'Test Course', + gamificationEnabled: true, + students: [makeStudent(1, 'Alice')], + submissions: [], + tabs: [makeTab(10, 'Tab 1', 1, 100)], + }; + const { rerender } = render( + , + { state: userState }, + ); + expect( + within(headerRow1()).queryByText(LEVEL_CONTRIBUTION), + ).not.toBeInTheDocument(); + + rerender( + + + , + ); + expect( + within(headerRow1()).getByText(LEVEL_CONTRIBUTION), + ).toBeInTheDocument(); + }); + + it('shows a receipt icon and clamped value when a contribution is capped above', async () => { + renderWeighted({ + gamificationEnabled: true, + courseMaxLevel: 30, + students: [ + { ...makeStudent(1, 'Alice'), level: 8, levelContribution: 10 }, + ], + levelContribution: { + enabled: true, + show: false, + weight: 10, + formula: 'level * 5', // raw 40 -> clamped 10 + clamp: true, + }, + }); + const row = screen.getByText('Alice').closest('tr')!; + expect(within(row).getByTestId('WarningAmberIcon')).toBeInTheDocument(); + }); + + it('shows no receipt icon when clamp is off', () => { + renderWeighted({ + gamificationEnabled: true, + courseMaxLevel: 30, + students: [ + { ...makeStudent(1, 'Alice'), level: 8, levelContribution: 40 }, + ], + levelContribution: { + enabled: true, + show: false, + weight: 10, + formula: 'level * 5', // raw 40, shown raw because clamp off + clamp: false, + }, + }); + const row = screen.getByText('Alice').closest('tr')!; + expect( + within(row).queryByTestId('WarningAmberIcon'), + ).not.toBeInTheDocument(); + }); + + // Divide-by-zero: the formula is mathematically undefined for this student. + // Store/backend coerce it to null; the cell must surface 0 + a warning to + // match the clamp cells and the dialog's "set to 0" copy. + const divByZeroProps = { + gamificationEnabled: true, + courseMaxLevel: 30, + students: [ + { ...makeStudent(1, 'Alice'), level: 8, levelContribution: null }, + ], + levelContribution: { + enabled: true, + show: false, + weight: 30, + formula: '30 / (level - 8)', // at level 8 -> divide by zero -> NaN + clamp: true, + }, + }; + + it('renders 0 with a warning icon when the formula divides by zero', () => { + renderWeighted(divByZeroProps); + // checkbox | Name | Level Contribution | Tab 1 | Total + const cells = within(screen.getByText('Alice').closest('tr')!).getAllByRole( + 'cell', + ); + expect( + within(cells[2]).getByTestId('WarningAmberIcon'), + ).toBeInTheDocument(); + expect(cells[2]).toHaveTextContent('0'); + }); + + it('explains the divide-by-zero fallback in the cell tooltip on hover', async () => { + const user = userEvent.setup(); + renderWeighted(divByZeroProps); + const row = screen.getByText('Alice').closest('tr')!; + await user.hover(within(row).getByTestId('WarningAmberIcon')); + expect(await screen.findByRole('tooltip')).toHaveTextContent( + /divides by zero/i, + ); + }); + + it('still shows 0 with a warning on divide-by-zero when clamp is off', () => { + renderWeighted({ + ...divByZeroProps, + levelContribution: { ...divByZeroProps.levelContribution, clamp: false }, + }); + const cells = within(screen.getByText('Alice').closest('tr')!).getAllByRole( + 'cell', + ); + expect( + within(cells[2]).getByTestId('WarningAmberIcon'), + ).toBeInTheDocument(); + expect(cells[2]).toHaveTextContent('0'); + }); +}); diff --git a/client/app/bundles/course/gradebook/__tests__/computeWeighted.test.ts b/client/app/bundles/course/gradebook/__tests__/computeWeighted.test.ts index e6fe8abe6a9..091424f2af1 100644 --- a/client/app/bundles/course/gradebook/__tests__/computeWeighted.test.ts +++ b/client/app/bundles/course/gradebook/__tests__/computeWeighted.test.ts @@ -1,13 +1,19 @@ // client/app/bundles/course/gradebook/__tests__/computeWeighted.test.ts +import type { LevelContributionData } from 'types/course/gradebook'; + import { computeStudentBreakdown, computeStudentTotal, computeTabSubtotal, computeWeightedRows, + LEVEL_TAB_ID, + levelOffenders, + levelOutOfRange, resolveTabWeights, sumWeights, usingDefaultWeights, } from '../computeWeighted'; +import { parseFormula } from '../levelFormula'; const assessments = [ { id: 1, tabId: 10, maxGrade: 100, title: 'A' }, @@ -20,7 +26,7 @@ const subs = ( ): { studentId: number; assessmentId: number; grade: number | null }[] => entries; -describe('computeTabSubtotal — equal mode (default)', () => { +describe('computeTabSubtotal - equal mode (default)', () => { it('returns null when tab has no assessments', () => { expect( computeTabSubtotal({ @@ -76,7 +82,7 @@ describe('computeTabSubtotal — equal mode (default)', () => { }); }); -describe('computeTabSubtotal — custom mode', () => { +describe('computeTabSubtotal - custom mode', () => { const customTab = { id: 10, title: 'M', @@ -359,7 +365,7 @@ describe('sumWeights', () => { }); }); -describe('resolveTabWeights — equal-split default when unconfigured', () => { +describe('resolveTabWeights - equal-split default when unconfigured', () => { const twoTabs = [ { id: 10, title: 'M', categoryId: 0, gradebookWeight: 0 }, { id: 20, title: 'T', categoryId: 0, gradebookWeight: 0 }, @@ -475,6 +481,7 @@ describe('computeWeightedRows', () => { externalId: null, level: 1, totalXp: 0, + levelContribution: null, }, { id: 2, @@ -483,6 +490,7 @@ describe('computeWeightedRows', () => { externalId: null, level: 1, totalXp: 0, + levelContribution: null, }, ]; const rowSubmissions = subs([ @@ -584,7 +592,7 @@ describe('computeWeightedRows', () => { }); }); -describe('computeWeightedRows — identity passthrough', () => { +describe('computeWeightedRows - identity passthrough', () => { it('carries name, email and externalId from each student onto the row', () => { const students = [ { @@ -594,6 +602,7 @@ describe('computeWeightedRows — identity passthrough', () => { externalId: 'EXT-1', level: 5, totalXp: 1234, + levelContribution: null, }, ]; const tabs = [ @@ -703,7 +712,7 @@ describe('computeStudentBreakdown', () => { }); }); -describe('exclusion — equal mode', () => { +describe('exclusion - equal mode', () => { it('averages over included assessments only (excluded dropped from numerator and count)', () => { // a1 80/100=0.8 included, a2 excluded -> subtotal = 0.8 / 1 = 0.8 const withExcluded = [ @@ -736,7 +745,7 @@ describe('exclusion — equal mode', () => { }); }); -describe('exclusion — custom mode', () => { +describe('exclusion - custom mode', () => { it('drops excluded assessments from the numerator', () => { // tab weight 30; a1 weight 30 graded 90/100=0.9 -> 0.9*30=27; a2 excluded. // subtotal = 27 / 30 = 0.9 @@ -771,7 +780,7 @@ describe('exclusion — custom mode', () => { }); }); -describe('breakdown — exclusion', () => { +describe('breakdown - exclusion', () => { const bdAssessments = [ { id: 1, tabId: 10, maxGrade: 100, title: 'A' }, { id: 2, tabId: 10, maxGrade: 50, title: 'B', gradebookExcluded: true }, @@ -868,3 +877,220 @@ describe('zero-maxGrade assessments (0/0 must not produce NaN)', () => { expect(a.points).toBe(0); }); }); + +const lc = ( + over: Partial = {}, +): LevelContributionData => ({ + enabled: true, + formula: 'level / 30 * 8', // cap baked in literally - no maxLevel variable + weight: 8, + show: false, + clamp: true, + ...over, +}); + +describe('computeWeightedRows - level contribution', () => { + const baseStudent = { + id: 1, + name: 'A', + email: 'a@x', + externalId: null, + level: 15, + totalXp: 0, + }; + + it('reads levelContribution from the student object', () => { + const rows = computeWeightedRows({ + students: [{ ...baseStudent, levelContribution: 4 }], + tabs: [], + assessments: [], + submissions: [], + }); + expect(rows[0].levelContribution).toBeCloseTo(4); + expect(rows[0].total).toBeCloseTo(4); + }); + + it('contributes null when the student levelContribution is null', () => { + const rows = computeWeightedRows({ + students: [{ ...baseStudent, levelContribution: null }], + tabs: [], + assessments: [], + submissions: [], + }); + expect(rows[0].levelContribution).toBeNull(); + expect(rows[0].total).toBeNull(); + }); + + it('treats levelContribution as null when showLevelContribution is false', () => { + const rows = computeWeightedRows({ + students: [{ ...baseStudent, levelContribution: 4 }], + tabs: [], + assessments: [], + submissions: [], + showLevelContribution: false, + }); + expect(rows[0].levelContribution).toBeNull(); + expect(rows[0].total).toBeNull(); + }); + + it('carries level from student regardless of showLevelContribution', () => { + const rows = computeWeightedRows({ + students: [{ ...baseStudent, levelContribution: null }], + tabs: [], + assessments: [], + submissions: [], + }); + expect(rows[0].level).toBe(15); + }); +}); + +describe('levelOutOfRange', () => { + it('flags a student whose contribution exceeds the weight', () => { + const parsed = parseFormula('level'); // raw level as points + expect(parsed.ok).toBe(true); + if (parsed.ok) { + expect( + levelOutOfRange( + [{ level: 50 }], + lc({ formula: 'level', weight: 8 }), + parsed, + ), + ).toBe(true); + expect( + levelOutOfRange( + [{ level: 5 }], + lc({ formula: 'level', weight: 8 }), + parsed, + ), + ).toBe(false); + } + }); +}); + +describe('levelOffenders', () => { + const A = { id: 1, name: 'A', level: 5 }; + const B = { id: 2, name: 'B', level: 12 }; + const C = { id: 3, name: 'C', level: 3 }; + + it('returns no offenders when the formula does not parse', () => { + const parsed = parseFormula('level /'); + expect(levelOffenders([A], parsed, 10)).toEqual({ + below: [], + above: [], + unscoreable: [], + }); + }); + + it('returns no offenders when every contribution is within [0, max]', () => { + const parsed = parseFormula('level * 0.1'); // 0.5, 1.2 - within [0, 10] + if (!parsed.ok) throw new Error('expected ok'); + expect(levelOffenders([A, B], parsed, 10)).toEqual({ + below: [], + above: [], + unscoreable: [], + }); + }); + + it('lists students above the max, most extreme first', () => { + const parsed = parseFormula('level * 5'); // A 25, B 60, C 15 + if (!parsed.ok) throw new Error('expected ok'); + const { above, below } = levelOffenders([A, B, C], parsed, 10); + expect(below).toEqual([]); + expect(above.map((o) => o.name)).toEqual(['B', 'A', 'C']); + expect(above[0]).toMatchObject({ id: 2, name: 'B', value: 60 }); + }); + + it('lists students below 0, most negative first', () => { + const parsed = parseFormula('level - 8'); // A -3, B 4, C -5 + if (!parsed.ok) throw new Error('expected ok'); + const { above, below } = levelOffenders([A, B, C], parsed, 10); + expect(above).toEqual([]); + expect(below.map((o) => o.name)).toEqual(['C', 'A']); + expect(below[0]).toMatchObject({ id: 3, name: 'C', value: -5 }); + }); + + it('splits offenders across both bounds', () => { + const parsed = parseFormula('level * 5 - 30'); // A -5, B 30 + if (!parsed.ok) throw new Error('expected ok'); + const { above, below } = levelOffenders([A, B], parsed, 10); + expect(below.map((o) => o.value)).toEqual([-5]); + expect(above.map((o) => o.value)).toEqual([30]); + }); +}); + +describe('computeStudentBreakdown - level', () => { + it('appends a synthetic Level entry when enabled with pre-computed points', () => { + const breakdown = computeStudentBreakdown({ + studentId: 1, + tabs: [], + assessments: [], + submissions: [], + level: 15, + levelContribution: lc(), + levelContributionPoints: 4, // pre-computed: 15/30*8=4 + }); + const levelTab = breakdown.find((tb) => tb.tabId === LEVEL_TAB_ID); + expect(levelTab).toBeDefined(); + expect(levelTab!.assessments[0].title).toBe('Level'); + expect(levelTab!.assessments[0].points).toBeCloseTo(4); + expect(levelTab!.assessments[0].effectiveWeight).toBe(8); + }); + + it('omits the Level entry when disabled', () => { + const breakdown = computeStudentBreakdown({ + studentId: 1, + tabs: [], + assessments: [], + submissions: [], + level: 15, + levelContribution: lc({ enabled: false }), + levelContributionPoints: null, + }); + expect(breakdown.find((tb) => tb.tabId === LEVEL_TAB_ID)).toBeUndefined(); + }); + + it('omits the Level entry when levelContributionPoints is null (even if enabled)', () => { + const breakdown = computeStudentBreakdown({ + studentId: 1, + tabs: [], + assessments: [], + submissions: [], + level: 15, + levelContribution: lc(), + levelContributionPoints: null, + }); + expect(breakdown.find((tb) => tb.tabId === LEVEL_TAB_ID)).toBeUndefined(); + }); +}); + +describe('levelOffenders unscoreable bucket', () => { + const students = [ + { id: 1, name: 'Ann', level: 0 }, + { id: 2, name: 'Bob', level: 5 }, + { id: 3, name: 'Cy', level: 10 }, + ]; + + it('collects students whose contribution is NaN (divide-by-zero)', () => { + const parsed = parseFormula('100 / level'); // level 0 -> NaN + const r = levelOffenders(students, parsed, 50); + expect(r.unscoreable.map((o) => o.id)).toEqual([1]); + expect(r.unscoreable[0].level).toBe(0); + expect(r.below).toHaveLength(0); + expect(r.above).toHaveLength(0); + }); + + it('is empty when no student divides by zero', () => { + const parsed = parseFormula('level'); + expect(levelOffenders(students, parsed, 50).unscoreable).toHaveLength(0); + }); + + it('sorts unscoreable by level ascending', () => { + const parsed = parseFormula('100 / (level - 5)'); // NaN at level 5 only + const many = [ + { id: 1, name: 'Zoe', level: 5 }, + { id: 2, name: 'Amy', level: 5 }, + ]; + const r = levelOffenders(many, parsed, 50); + expect(r.unscoreable.map((o) => o.name)).toEqual(['Amy', 'Zoe']); + }); +}); diff --git a/client/app/bundles/course/gradebook/__tests__/levelFormula.test.ts b/client/app/bundles/course/gradebook/__tests__/levelFormula.test.ts new file mode 100644 index 00000000000..4ea820b2a6a --- /dev/null +++ b/client/app/bundles/course/gradebook/__tests__/levelFormula.test.ts @@ -0,0 +1,360 @@ +import type { FormulaNode } from 'types/course/gradebook'; + +import { + evaluateNode, + parseFormula, + seedLevelFormula, + serializeFormula, +} from '../levelFormula'; + +const evalOk = (src: string, level: number): number => { + const parsed = parseFormula(src); + if (!parsed.ok) throw new Error(`expected ok, got: ${parsed.error}`); + return parsed.evaluate(level); +}; + +describe('parseFormula', () => { + it('evaluates arithmetic with correct precedence', () => { + expect(evalOk('1 + 2 * 3', 0)).toBe(7); + expect(evalOk('(1 + 2) * 3', 0)).toBe(9); + expect(evalOk('10 / 2 - 1', 0)).toBe(4); + }); + + it('binds level as a raw variable (no clamp)', () => { + expect(evalOk('level * 0.05', 15)).toBeCloseTo(0.75); + expect(evalOk('level', 40)).toBe(40); + }); + + it('caps with min (the common cap idiom)', () => { + expect(evalOk('min(level, 25) * 0.05', 25)).toBeCloseTo(1.25); + expect(evalOk('min(level, 25) * 0.05', 40)).toBeCloseTo(1.25); // capped + expect(evalOk('min(level, 25) * 0.05', 10)).toBeCloseTo(0.5); + }); + + it('supports min and max (two-arg)', () => { + expect(evalOk('max(0, level - 5) * 0.1', 5)).toBe(0); + expect(evalOk('max(0, level - 5) * 0.1', 30)).toBeCloseTo(2.5); + }); + + it('supports floor, ceil, round (one-arg)', () => { + expect(evalOk('floor(level / 5)', 12)).toBe(2); + expect(evalOk('ceil(level / 10)', 11)).toBe(2); + expect(evalOk('round(level / 3)', 5)).toBe(2); // 1.66 -> 2 + }); + + it('supports unary minus', () => { + expect(evalOk('-level + 10', 3)).toBe(7); + }); + + it('rejects maxLevel — it is no longer a variable', () => { + expect(parseFormula('maxLevel').ok).toBe(false); + expect(parseFormula('level / maxLevel').ok).toBe(false); + }); + + it('rejects unknown identifiers (no eval surface)', () => { + expect(parseFormula('system').ok).toBe(false); + expect(parseFormula('window').ok).toBe(false); + expect(parseFormula('level.constructor').ok).toBe(false); + }); + + it('rejects unknown functions and wrong arity', () => { + expect(parseFormula('sqrt(level)').ok).toBe(false); + expect(parseFormula('eval(level)').ok).toBe(false); + expect(parseFormula('min(level)').ok).toBe(false); // min needs 2 args + expect(parseFormula('floor(level, 2)').ok).toBe(false); // floor needs 1 arg + }); + + it('rejects malformed input with a message', () => { + const r = parseFormula('level / '); + expect(r.ok).toBe(false); + if (!r.ok) expect(r.error).toMatch(/unfinished/i); + }); +}); + +// Plain-language errors for non-technical staff. The thrown message is shown +// verbatim under the formula field, so it must read like prose, never compiler +// jargon (no "identifier", "token", "rparen"). +const errorOf = (src: string): string => { + const r = parseFormula(src); + if (r.ok) throw new Error(`expected a parse error for "${src}"`); + return r.error; +}; + +describe('parseFormula — plain-language errors', () => { + it('never leaks compiler jargon', () => { + const jargon = /identifier|token|rparen|lparen|trailing input/i; + [ + 'lveel', + 'min(level 20)', + 'level %2', + 'level / ', + 'min(level,)', + 'level 2', + 'flor(level)', + ].forEach((src) => { + expect(errorOf(src)).not.toMatch(jargon); + }); + }); + + it('suggests the nearest variable for a misspelt word', () => { + expect(errorOf('lveel')).toContain('"lveel" is not recognised'); + expect(errorOf('lveel')).toContain('did you mean "level"'); + expect(errorOf('levl')).toContain('did you mean "level"'); + }); + + it('suggests the nearest function for a misspelt call', () => { + expect(errorOf('flor(level)')).toContain('did you mean "floor"'); + expect(errorOf('mn(level, 2)')).toContain('did you mean "min"'); + }); + + it('lists the valid words when nothing is close', () => { + const msg = errorOf('xyzzy'); + expect(msg).toContain('"xyzzy" is not recognised'); + expect(msg).not.toMatch(/did you mean/i); + expect(msg).toMatch(/level, floor, ceil, round, min, max/); + }); + + it('lists only functions when an unknown call has no close match', () => { + const msg = errorOf('frobnicate(level)'); + expect(msg).not.toMatch(/did you mean/i); + expect(msg).toMatch(/floor, ceil, round, min, max/); + expect(msg).not.toMatch(/level,/); + }); + + it('explains a missing comma with an example', () => { + expect(errorOf('min(level 20)')).toMatch(/comma/i); + expect(errorOf('min(level 20)')).toMatch(/min\(level, 25\)/); + }); + + it('explains a missing closing bracket', () => { + expect(errorOf('min(level, 20')).toMatch(/closing bracket/i); + }); + + it('explains a stray symbol', () => { + expect(errorOf('level % 2')).toContain('"%" cannot be used'); + }); + + it('explains an unfinished formula', () => { + expect(errorOf('level *')).toMatch(/unfinished/i); + }); + + it('flags two values with a missing operator', () => { + expect(errorOf('level 2')).toMatch(/operator is missing/i); + expect(errorOf('level 2')).toMatch(/level × 2/); + }); + + it('explains an invalid number', () => { + expect(errorOf('1.2.3')).toContain('"1.2.3" is not a valid number'); + }); +}); + +describe('seedLevelFormula', () => { + it('seeds default formula to `level`', () => { + expect(seedLevelFormula).toBe('level'); + }); +}); + +describe('evaluateNode — direct traversal', () => { + it('num: returns the literal value', () => { + const node: FormulaNode = { type: 'num', value: 3.5 }; + expect(evaluateNode(node, { level: 0 })).toBe(3.5); + }); + + it('var: returns the level from scope', () => { + const node: FormulaNode = { type: 'var', name: 'level' }; + expect(evaluateNode(node, { level: 15 })).toBe(15); + }); + + it('neg: negates operand', () => { + const node: FormulaNode = { + type: 'neg', + operand: { type: 'num', value: 4 }, + }; + expect(evaluateNode(node, { level: 0 })).toBe(-4); + }); + + it('binop +: adds left and right', () => { + const node: FormulaNode = { + type: 'binop', + op: '+', + left: { type: 'num', value: 3 }, + right: { type: 'var', name: 'level' }, + }; + expect(evaluateNode(node, { level: 7 })).toBe(10); + }); + + it('binop /: returns NaN on div-by-zero', () => { + const node: FormulaNode = { + type: 'binop', + op: '/', + left: { type: 'var', name: 'level' }, + right: { type: 'num', value: 0 }, + }; + expect(evaluateNode(node, { level: 5 })).toBeNaN(); + }); + + it('call1 floor: applies Math.floor', () => { + const node: FormulaNode = { + type: 'call1', + fn: 'floor', + arg: { + type: 'binop', + op: '/', + left: { type: 'var', name: 'level' }, + right: { type: 'num', value: 5 }, + }, + }; + expect(evaluateNode(node, { level: 12 })).toBe(2); // floor(12/5) = floor(2.4) = 2 + }); + + it('call2 min: returns smaller of two args', () => { + const node: FormulaNode = { + type: 'call2', + fn: 'min', + a: { type: 'var', name: 'level' }, + b: { type: 'num', value: 20 }, + }; + expect(evaluateNode(node, { level: 15 })).toBe(15); + expect(evaluateNode(node, { level: 25 })).toBe(20); + }); + + it('call2 max: returns larger of two args', () => { + const node: FormulaNode = { + type: 'call2', + fn: 'max', + a: { type: 'num', value: 0 }, + b: { + type: 'binop', + op: '-', + left: { type: 'var', name: 'level' }, + right: { type: 'num', value: 5 }, + }, + }; + expect(evaluateNode(node, { level: 3 })).toBe(0); // max(0, -2) + expect(evaluateNode(node, { level: 8 })).toBe(3); // max(0, 3) + }); +}); + +describe('serializeFormula — round-trips', () => { + it('serializes a number literal', () => { + const result = serializeFormula('3.5'); + expect(result.ok).toBe(true); + if (result.ok) expect(result.ast).toEqual({ type: 'num', value: 3.5 }); + }); + + it('serializes the level variable', () => { + const result = serializeFormula('level'); + expect(result.ok).toBe(true); + if (result.ok) expect(result.ast).toEqual({ type: 'var', name: 'level' }); + }); + + it('serializes unary minus', () => { + const result = serializeFormula('-level'); + expect(result.ok).toBe(true); + if (result.ok) + expect(result.ast).toEqual({ + type: 'neg', + operand: { type: 'var', name: 'level' }, + }); + }); + + it('serializes a binop tree for level * 0.5', () => { + const result = serializeFormula('level * 0.5'); + expect(result.ok).toBe(true); + if (result.ok) { + expect(result.ast).toEqual({ + type: 'binop', + op: '*', + left: { type: 'var', name: 'level' }, + right: { type: 'num', value: 0.5 }, + }); + } + }); + + it('serializes min(level, 20)', () => { + const result = serializeFormula('min(level, 20)'); + expect(result.ok).toBe(true); + if (result.ok) { + expect(result.ast).toEqual({ + type: 'call2', + fn: 'min', + a: { type: 'var', name: 'level' }, + b: { type: 'num', value: 20 }, + }); + } + }); + + it('serializes floor(level / 5)', () => { + const result = serializeFormula('floor(level / 5)'); + expect(result.ok).toBe(true); + if (result.ok) { + expect(result.ast).toEqual({ + type: 'call1', + fn: 'floor', + arg: { + type: 'binop', + op: '/', + left: { type: 'var', name: 'level' }, + right: { type: 'num', value: 5 }, + }, + }); + } + }); + + it('returns ok:false on invalid input', () => { + const result = serializeFormula('level /'); + expect(result.ok).toBe(false); + }); + + it('round-trip: serialized AST evaluates identically to parseFormula', () => { + const formula = 'min(level, 25) * 0.05'; + const parsed = parseFormula(formula); + const serialized = serializeFormula(formula); + expect(parsed.ok).toBe(true); + expect(serialized.ok).toBe(true); + if (parsed.ok && serialized.ok) { + [10, 25, 40].forEach((level) => { + expect(evaluateNode(serialized.ast, { level })).toBeCloseTo( + parsed.evaluate(level), + ); + }); + } + }); +}); + +describe('constant divide-by-zero (always divides by zero)', () => { + it('rejects a literal divide by zero', () => { + const r = parseFormula('level / 0'); + expect(r.ok).toBe(false); + if (!r.ok) expect(r.error).toMatch(/always divides by zero/i); + }); + + it('rejects a constant expression that evaluates to zero', () => { + const r = parseFormula('100 / (5 - 5)'); + expect(r.ok).toBe(false); + if (!r.ok) expect(r.error).toMatch(/always divides by zero/i); + }); + + it('rejects a nested constant zero divisor', () => { + const r = parseFormula('level / (1 / 0)'); + expect(r.ok).toBe(false); + if (!r.ok) expect(r.error).toMatch(/always divides by zero/i); + }); + + it('accepts a constant non-zero divisor', () => { + expect(parseFormula('floor(level / 5)').ok).toBe(true); + }); + + it('accepts a level-dependent divisor', () => { + expect(parseFormula('100 / level').ok).toBe(true); + expect(parseFormula('100 / (level - 2)').ok).toBe(true); + }); + + it('returns NaN when the divisor evaluates to zero for a student', () => { + expect(evalOk('level / (level - 5)', 5)).toBeNaN(); + }); + + it('still divides normally when the divisor is non-zero', () => { + expect(evalOk('level / (level - 5)', 7)).toBeCloseTo(3.5); + }); +}); diff --git a/client/app/bundles/course/gradebook/__tests__/operations.test.ts b/client/app/bundles/course/gradebook/__tests__/operations.test.ts new file mode 100644 index 00000000000..e1a3fca281f --- /dev/null +++ b/client/app/bundles/course/gradebook/__tests__/operations.test.ts @@ -0,0 +1,55 @@ +import CourseAPI from 'api/course'; + +import { updateGradebookWeights } from '../operations'; + +jest.mock('api/course', () => ({ + gradebook: { + updateWeights: jest.fn(), + }, +})); + +const mockedUpdateWeights = CourseAPI.gradebook.updateWeights as jest.Mock; + +describe('updateGradebookWeights', () => { + beforeEach(() => { + mockedUpdateWeights.mockReset(); + }); + + // The backend response does NOT echo formulaAst. The operation must merge the + // client-side formulaAst back into the dispatched payload so the reducer can + // optimistically recompute each student's levelContribution. Dispatching the + // raw response strands formulaAst undefined → reducer nulls every student. + it('merges formulaAst back into the dispatched action', async () => { + const formulaAst = { + type: 'call2' as const, + fn: 'min' as const, + a: { type: 'var' as const, name: 'level' as const }, + b: { type: 'num' as const, value: 30 }, + }; + mockedUpdateWeights.mockResolvedValue({ + data: { + weights: [], + levelContribution: { + enabled: true, + formula: 'min(level, 30)', + weight: 10, + show: false, + }, + }, + }); + + const dispatch = jest.fn(); + await updateGradebookWeights([], { + enabled: true, + formula: 'min(level, 30)', + formulaAst, + weight: 10, + show: false, + clamp: true, + })(dispatch, jest.fn(), {}); + + expect(dispatch).toHaveBeenCalledTimes(1); + const dispatched = dispatch.mock.calls[0][0]; + expect(dispatched.payload.levelContribution.formulaAst).toEqual(formulaAst); + }); +}); diff --git a/client/app/bundles/course/gradebook/__tests__/store.test.ts b/client/app/bundles/course/gradebook/__tests__/store.test.ts index 97143ade931..394d406a0b2 100644 --- a/client/app/bundles/course/gradebook/__tests__/store.test.ts +++ b/client/app/bundles/course/gradebook/__tests__/store.test.ts @@ -15,6 +15,14 @@ const baseState = { gamificationEnabled: false, weightedViewEnabled: false, canManageWeights: false, + courseMaxLevel: 0, + levelContribution: { + enabled: false, + formula: '', + weight: 0, + show: false, + clamp: true, + }, }; describe('UPDATE_TAB_WEIGHTS reducer', () => { @@ -126,6 +134,14 @@ describe('UPDATE_TAB_WEIGHTS reducer', () => { gamificationEnabled: false, weightedViewEnabled: true, canManageWeights: true, + courseMaxLevel: 0, + levelContribution: { + enabled: false, + formula: '', + weight: 0, + show: false, + clamp: true, + }, }), ); @@ -151,3 +167,287 @@ describe('UPDATE_TAB_WEIGHTS reducer', () => { ); }); }); + +describe('level contribution', () => { + it('stores levelContribution and courseMaxLevel on SAVE_GRADEBOOK', () => { + const data = { + categories: [], + tabs: [], + assessments: [], + students: [], + submissions: [], + gamificationEnabled: true, + weightedViewEnabled: true, + canManageWeights: true, + courseMaxLevel: 20, + levelContribution: { + enabled: true, + formula: 'level / 20 * 8', + weight: 8, + show: true, + clamp: true, + }, + }; + const next = reducer(undefined, actions.saveGradebook(data)); + expect(next.courseMaxLevel).toBe(20); + expect(next.levelContribution.enabled).toBe(true); + expect(next.levelContribution.weight).toBe(8); + }); + + it('applies an echoed levelContribution on UPDATE_TAB_WEIGHTS', () => { + const next = reducer( + undefined, + actions.updateTabWeights({ + weights: [], + levelContribution: { + enabled: true, + formula: 'level', + formulaAst: null, + weight: 5, + show: false, + clamp: true, + }, + }), + ); + expect(next.levelContribution.enabled).toBe(true); + expect(next.levelContribution.weight).toBe(5); + }); + + it('recomputes student levelContribution from formulaAst when enabled', () => { + const stateWithStudents = reducer( + undefined, + actions.saveGradebook({ + categories: [], + tabs: [], + assessments: [], + students: [ + { + id: 1, + name: 'A', + email: 'a@x', + externalId: null, + level: 10, + totalXp: 0, + levelContribution: null, + }, + { + id: 2, + name: 'B', + email: 'b@x', + externalId: null, + level: 20, + totalXp: 0, + levelContribution: null, + }, + ], + submissions: [], + gamificationEnabled: true, + weightedViewEnabled: true, + canManageWeights: true, + courseMaxLevel: 30, + levelContribution: { + enabled: false, + formula: '', + weight: 0, + show: false, + clamp: true, + }, + }), + ); + // formula: 'level * 0.5' → AST: binop(*) [var(level), num(0.5)] + const formulaAst = { + type: 'binop' as const, + op: '*' as const, + left: { type: 'var' as const, name: 'level' as const }, + right: { type: 'num' as const, value: 0.5 }, + }; + const next = reducer( + stateWithStudents, + actions.updateTabWeights({ + weights: [], + levelContribution: { + enabled: true, + formula: 'level * 0.5', + formulaAst, + weight: 15, + show: false, + clamp: true, + }, + }), + ); + expect(next.students[0].levelContribution).toBeCloseTo(5); // 10 * 0.5 + expect(next.students[1].levelContribution).toBeCloseTo(10); // 20 * 0.5 + }); + + it('clamps recomputed levelContribution to [0, weight] when clamp is on', () => { + const stateWithStudents = reducer( + undefined, + actions.saveGradebook({ + categories: [], + tabs: [], + assessments: [], + students: [ + { + id: 1, + name: 'A', + email: 'a@x', + externalId: null, + level: 10, + totalXp: 0, + levelContribution: null, + }, + { + id: 2, + name: 'B', + email: 'b@x', + externalId: null, + level: 20, + totalXp: 0, + levelContribution: null, + }, + ], + submissions: [], + gamificationEnabled: true, + weightedViewEnabled: true, + canManageWeights: true, + courseMaxLevel: 30, + levelContribution: { + enabled: false, + formula: '', + weight: 0, + show: false, + clamp: true, + }, + }), + ); + // formula: 'level + 5' → AST: binop(+) [var(level), num(5)] + const formulaAst = { + type: 'binop' as const, + op: '+' as const, + left: { type: 'var' as const, name: 'level' as const }, + right: { type: 'num' as const, value: 5 }, + }; + const next = reducer( + stateWithStudents, + actions.updateTabWeights({ + weights: [], + levelContribution: { + enabled: true, + formula: 'level + 5', + formulaAst, + weight: 12, + show: false, + clamp: true, + }, + }), + ); + // raw 10 + 5 = 15 → clamped to weight 12; raw 20 + 5 = 25 → clamped to 12 + expect(next.students[0].levelContribution).toBeCloseTo(12); + expect(next.students[1].levelContribution).toBeCloseTo(12); + }); + + it('leaves recomputed levelContribution unclamped when clamp is off', () => { + const stateWithStudents = reducer( + undefined, + actions.saveGradebook({ + categories: [], + tabs: [], + assessments: [], + students: [ + { + id: 1, + name: 'A', + email: 'a@x', + externalId: null, + level: 10, + totalXp: 0, + levelContribution: null, + }, + ], + submissions: [], + gamificationEnabled: true, + weightedViewEnabled: true, + canManageWeights: true, + courseMaxLevel: 30, + levelContribution: { + enabled: false, + formula: '', + weight: 0, + show: false, + clamp: true, + }, + }), + ); + const formulaAst = { + type: 'binop' as const, + op: '+' as const, + left: { type: 'var' as const, name: 'level' as const }, + right: { type: 'num' as const, value: 5 }, + }; + const next = reducer( + stateWithStudents, + actions.updateTabWeights({ + weights: [], + levelContribution: { + enabled: true, + formula: 'level + 5', + formulaAst, + weight: 12, + show: false, + clamp: false, + }, + }), + ); + // clamp off → raw 10 + 5 = 15 kept as-is + expect(next.students[0].levelContribution).toBeCloseTo(15); + }); + + it('sets all students levelContribution to null when level contribution disabled', () => { + const stateWithStudents = reducer( + undefined, + actions.saveGradebook({ + categories: [], + tabs: [], + assessments: [], + students: [ + { + id: 1, + name: 'A', + email: 'a@x', + externalId: null, + level: 10, + totalXp: 0, + levelContribution: 5, + }, + ], + submissions: [], + gamificationEnabled: true, + weightedViewEnabled: true, + canManageWeights: true, + courseMaxLevel: 30, + levelContribution: { + enabled: true, + formula: 'level * 0.5', + weight: 15, + show: false, + clamp: true, + }, + }), + ); + const next = reducer( + stateWithStudents, + actions.updateTabWeights({ + weights: [], + levelContribution: { + enabled: false, + formula: '', + formulaAst: null, + weight: 0, + show: false, + clamp: true, + }, + }), + ); + expect(next.students[0].levelContribution).toBeNull(); + }); +}); diff --git a/client/app/bundles/course/gradebook/components/ConfigureWeightsPrompt.tsx b/client/app/bundles/course/gradebook/components/ConfigureWeightsPrompt.tsx index 8937cd5388f..46cfdad6d9e 100644 --- a/client/app/bundles/course/gradebook/components/ConfigureWeightsPrompt.tsx +++ b/client/app/bundles/course/gradebook/components/ConfigureWeightsPrompt.tsx @@ -5,6 +5,7 @@ import { Alert, Checkbox, Collapse, + FormControlLabel, IconButton, Stack, TextField, @@ -13,6 +14,9 @@ import { import type { AssessmentData, CategoryData, + LevelContributionData, + LevelContributionSaveData, + StudentData, TabData, } from 'types/course/gradebook'; @@ -23,7 +27,18 @@ import toast from 'lib/hooks/toast'; import useTranslation from 'lib/hooks/useTranslation'; import formTranslations from 'lib/translations/form'; -import { resolveTabWeights, usingDefaultWeights } from '../computeWeighted'; +import type { LevelOffender } from '../computeWeighted'; +import { + levelOffenders, + resolveTabWeights, + usingDefaultWeights, +} from '../computeWeighted'; +import { + ParsedFormula, + parseFormula, + seedLevelFormula, + serializeFormula, +} from '../levelFormula'; import { updateGradebookWeights } from '../operations'; const translations = defineMessages({ @@ -128,12 +143,81 @@ const translations = defineMessages({ defaultMessage: 'No weights set yet - these are suggested defaults with every tab counting equally. Save to confirm, or adjust below.', }, + levelTitle: { + id: 'course.gradebook.ConfigureWeightsPrompt.levelTitle', + defaultMessage: 'Level contribution', + }, + levelSubtitle: { + id: 'course.gradebook.ConfigureWeightsPrompt.levelSubtitle', + defaultMessage: + "Adds grade-points from each student's level, on top of tab contributions.", + }, + levelHighestStudent: { + id: 'course.gradebook.ConfigureWeightsPrompt.levelHighestStudent', + defaultMessage: 'Highest student level: {level}', + }, + levelCourseMax: { + id: 'course.gradebook.ConfigureWeightsPrompt.levelCourseMax', + defaultMessage: 'Course maximum level: {courseMaxLevel}', + }, + levelClampLabel: { + id: 'course.gradebook.ConfigureWeightsPrompt.levelClampLabel', + defaultMessage: 'Keep results between 0 and max level contributions', + }, + levelFormulaLabel: { + id: 'course.gradebook.ConfigureWeightsPrompt.levelFormulaLabel', + defaultMessage: 'Formula', + }, + levelFormulaHelper: { + id: 'course.gradebook.ConfigureWeightsPrompt.levelFormulaHelper', + defaultMessage: + 'Formulas may contain numbers, arithmetic operators (+ − * /), parentheses, "level" as a variable, and the functions floor, ceil, round, min and max.', + }, + levelShowLabel: { + id: 'course.gradebook.ConfigureWeightsPrompt.levelShowLabel', + defaultMessage: 'Show level column in table', + }, + levelOffendersAbove: { + id: 'course.gradebook.ConfigureWeightsPrompt.levelOffendersAbove', + defaultMessage: + '{count, plural, =1 {{name1} is above {max}.} =2 {{name1} and {name2} are above {max}.} other {{name1}, {name2} and {extra} more are above {max}.}}', + }, + levelOffendersBelow: { + id: 'course.gradebook.ConfigureWeightsPrompt.levelOffendersBelow', + defaultMessage: + '{count, plural, =1 {{name1} is below 0.} =2 {{name1} and {name2} are below 0.} other {{name1}, {name2} and {extra} more are below 0.}}', + }, + levelFixAtMost: { + id: 'course.gradebook.ConfigureWeightsPrompt.levelFixAtMost', + defaultMessage: 'These contributions will be set to {max}.', + }, + levelFixAtLeast: { + id: 'course.gradebook.ConfigureWeightsPrompt.levelFixAtLeast', + defaultMessage: 'These contributions will be set to 0.', + }, + levelFixBetween: { + id: 'course.gradebook.ConfigureWeightsPrompt.levelFixBetween', + defaultMessage: + 'Contributions below 0 will be set to 0, and contributions above {max} will be set to {max}.', + }, + levelUnscoreable: { + id: 'course.gradebook.ConfigureWeightsPrompt.levelUnscoreable', + defaultMessage: + '{count, plural, =1 {The formula divides by zero for {name1}, so their level contribution is set to 0.} =2 {The formula divides by zero for {name1} and {name2}, so their level contributions are set to 0.} other {The formula divides by zero for {name1}, {name2} and {extra} more, so their level contributions are set to 0.}}', + }, }); type WeightMode = 'equal' | 'custom'; const r2 = (n: number): number => Math.round(n * 100) / 100; const cents = (n: number): number => Math.round(n * 100); +// "Bob (60.00)" — student name with their contribution at 2dp, for the warning. +const fmtOffender = (o: LevelOffender): string => + `${o.name} (${r2(o.value).toFixed(2)})`; +// "Bob (level 0)" — student name with their level, for the divide-by-zero warning +// (the contribution value is undefined, so we show level instead). +const fmtUnscoreable = (o: LevelOffender): string => + `${o.name} (level ${o.level})`; // Distribute a tab total across assessment ids at 2dp; the last id absorbs the rounding // remainder so the seeded values sum back exactly to total. const distributeEqual = ( @@ -156,6 +240,10 @@ interface Props { categories: CategoryData[]; tabs: TabData[]; assessments: AssessmentData[]; + gamificationEnabled: boolean; + courseMaxLevel: number; + levelContribution: LevelContributionData; + students: StudentData[]; } const ConfigureWeightsPrompt: FC = ({ @@ -164,6 +252,10 @@ const ConfigureWeightsPrompt: FC = ({ categories, tabs, assessments, + gamificationEnabled, + courseMaxLevel, + levelContribution, + students, }) => { const { t } = useTranslation(); const dispatch = useAppDispatch(); @@ -204,6 +296,17 @@ const ConfigureWeightsPrompt: FC = ({ const [expanded, setExpanded] = useState>({}); const [submitting, setSubmitting] = useState(false); + const [levelEnabled, setLevelEnabled] = useState(levelContribution.enabled); + const [levelFormula, setLevelFormula] = useState( + levelContribution.formula || seedLevelFormula, + ); + // Seed the suggested maximum to the course's top level. + const [levelWeight, setLevelWeight] = useState( + levelContribution.weight || courseMaxLevel, + ); + const [levelShow, setLevelShow] = useState(levelContribution.show); + const [levelClamp, setLevelClamp] = useState(levelContribution.clamp); + useEffect(() => { if (open) { setWeights(seedWeights()); @@ -211,6 +314,11 @@ const ConfigureWeightsPrompt: FC = ({ setAssessmentWeights(seedAssessmentWeights()); setExcluded(seedExclusions()); setExpanded({}); + setLevelEnabled(levelContribution.enabled); + setLevelFormula(levelContribution.formula || seedLevelFormula); + setLevelWeight(levelContribution.weight || courseMaxLevel); + setLevelShow(levelContribution.show); + setLevelClamp(levelContribution.clamp); } }, [open]); @@ -239,7 +347,31 @@ const ConfigureWeightsPrompt: FC = ({ const effectiveWeight = (tabId: number): number => isAllExcluded(tabId) ? 0 : weights[tabId] ?? 0; - const sum = tabs.reduce((acc, tb) => acc + effectiveWeight(tb.id), 0); + const parsedLevel: ParsedFormula | null = + levelEnabled && levelFormula ? parseFormula(levelFormula) : null; + const levelParseError = + parsedLevel && !parsedLevel.ok ? parsedLevel.error : null; + // Students whose contribution falls outside [0, levelWeight], split by bound — + // the out-of-range warning names the worst offenders on each side. + const offenders = levelOffenders(students, parsedLevel, levelWeight); + // The cohort's real top level — distinct from courseMaxLevel (the course ceiling). + // Helps staff pick a sensible cap; null when there are no students to read. + const highestStudentLevel = students.length + ? Math.max(...students.map((s) => s.level)) + : null; + // Which bound(s) the cohort breaches — drives a message scoped to the actual + // problem (only-below, only-above, or both). + const hasBelow = offenders.below.length > 0; + const hasAbove = offenders.above.length > 0; + const hasUnscoreable = offenders.unscoreable.length > 0; + // Fix instruction matching the breached bound(s). + let levelFixMessage = translations.levelFixAtLeast; + if (hasBelow && hasAbove) levelFixMessage = translations.levelFixBetween; + else if (hasAbove) levelFixMessage = translations.levelFixAtMost; + + const sum = + tabs.reduce((acc, tb) => acc + effectiveWeight(tb.id), 0) + + (levelEnabled ? levelWeight : 0); const hasInvalid = Object.values(weights).some((w) => validate(w) !== null) || Object.values(assessmentWeights).some((w) => validate(w) !== null); @@ -278,9 +410,19 @@ const ConfigureWeightsPrompt: FC = ({ setExpanded((prev) => ({ ...prev, [tabId]: !prev[tabId] })); const handleSave = async (): Promise => { - if (hasInvalid || hasUnbalanced) return; + if (hasInvalid || hasUnbalanced || !!levelParseError) return; setSubmitting(true); try { + const serialized = levelEnabled ? serializeFormula(levelFormula) : null; + const formulaAst = serialized?.ok ? serialized.ast : null; + const lcPayload: LevelContributionSaveData = { + enabled: levelEnabled, + formula: levelFormula, + formulaAst, + weight: levelWeight, + show: levelShow, + clamp: levelClamp, + }; await dispatch( updateGradebookWeights( tabs.map((tb) => { @@ -304,6 +446,7 @@ const ConfigureWeightsPrompt: FC = ({ } return entry; }), + lcPayload, ), ); onClose(); @@ -319,7 +462,9 @@ const ConfigureWeightsPrompt: FC = ({ onClickPrimary={handleSave} onClose={onClose} open={open} - primaryDisabled={submitting || hasInvalid || hasUnbalanced} + primaryDisabled={ + submitting || hasInvalid || hasUnbalanced || !!levelParseError + } primaryLabel={t(formTranslations.save)} title={t(translations.promptTitle)} > @@ -585,6 +730,151 @@ const ConfigureWeightsPrompt: FC = ({ ))} + {gamificationEnabled && ( +
    +
    + setLevelEnabled(e.target.checked)} + size="small" + /> + } + label={ + + {t(translations.levelTitle)} + + } + sx={{ flex: 1, mr: 0 }} + /> + setLevelWeight((prev) => r2(prev))} + onChange={(e) => + setLevelWeight( + e.target.value === '' ? 0 : Number(e.target.value), + ) + } + size="small" + sx={{ width: 96 }} + type="number" + value={levelEnabled ? levelWeight : 0} + /> +
    + {levelEnabled && ( + + + {t(translations.levelSubtitle)} + + + {t(translations.levelFormulaHelper)} + + setLevelFormula(e.target.value)} + size="small" + value={levelFormula} + /> + {highestStudentLevel != null && ( + + {t(translations.levelHighestStudent, { + level: highestStudentLevel, + })} + + )} + + {t(translations.levelCourseMax, { courseMaxLevel })} + + setLevelShow(e.target.checked)} + size="small" + sx={{ py: 0.25 }} + /> + } + label={ + + {t(translations.levelShowLabel)} + + } + /> + setLevelClamp(e.target.checked)} + size="small" + sx={{ py: 0.25 }} + /> + } + label={ + + {t(translations.levelClampLabel)} + + } + /> + {((levelClamp && (hasBelow || hasAbove)) || hasUnscoreable) && ( + + + {[ + levelClamp && + hasAbove && + t(translations.levelOffendersAbove, { + count: offenders.above.length, + name1: fmtOffender(offenders.above[0]), + name2: offenders.above[1] + ? fmtOffender(offenders.above[1]) + : '', + extra: Math.max(0, offenders.above.length - 2), + max: levelWeight, + }), + levelClamp && + hasBelow && + t(translations.levelOffendersBelow, { + count: offenders.below.length, + name1: fmtOffender(offenders.below[0]), + name2: offenders.below[1] + ? fmtOffender(offenders.below[1]) + : '', + extra: Math.max(0, offenders.below.length - 2), + }), + hasUnscoreable && + t(translations.levelUnscoreable, { + count: offenders.unscoreable.length, + name1: fmtUnscoreable(offenders.unscoreable[0]), + name2: offenders.unscoreable[1] + ? fmtUnscoreable(offenders.unscoreable[1]) + : '', + extra: Math.max(0, offenders.unscoreable.length - 2), + }), + ] + .filter(Boolean) + .join(' ')} + + {levelClamp && (hasBelow || hasAbove) && ( + + {t(levelFixMessage, { max: levelWeight })} + + )} + + )} + + )} +
    + )} {t(translations.total, { sum })} diff --git a/client/app/bundles/course/gradebook/components/WeightedGradebookTable.tsx b/client/app/bundles/course/gradebook/components/WeightedGradebookTable.tsx index b02d21c0179..9edccf4c954 100644 --- a/client/app/bundles/course/gradebook/components/WeightedGradebookTable.tsx +++ b/client/app/bundles/course/gradebook/components/WeightedGradebookTable.tsx @@ -1,10 +1,11 @@ import { Fragment, useLayoutEffect, useMemo, useRef, useState } from 'react'; -import { defineMessages } from 'react-intl'; +import { defineMessages, MessageDescriptor } from 'react-intl'; import { Download, InfoOutlined, KeyboardArrowDown, KeyboardArrowRight, + WarningAmber, } from '@mui/icons-material'; import { Alert, @@ -24,6 +25,7 @@ import { import type { AssessmentData, CategoryData, + LevelContributionData, StudentData, SubmissionData, TabData, @@ -45,9 +47,12 @@ import { computeStudentBreakdown, computeWeightedRows, gradeRatio, + LEVEL_TAB_ID, + levelOffenders, resolveTabWeights, usingDefaultWeights, } from '../computeWeighted'; +import { parseFormula } from '../levelFormula'; import ConfigureWeightsPrompt from './ConfigureWeightsPrompt'; import ProjectedTotalHint, { @@ -165,6 +170,46 @@ const translations = defineMessages({ id: 'course.gradebook.WeightedGradebookTable.weightedTotal', defaultMessage: 'Weighted Total', }, + levelHeader: { + id: 'course.gradebook.WeightedGradebookTable.levelHeader', + defaultMessage: 'Level', + }, + levelContributionHeader: { + id: 'course.gradebook.WeightedGradebookTable.levelContributionHeader', + defaultMessage: 'Level Contribution', + }, + levelBreakdownDetail: { + id: 'course.gradebook.WeightedGradebookTable.levelBreakdownDetail', + defaultMessage: 'Level {level}', + }, + levelOverBudgetAboveOnly: { + id: 'course.gradebook.WeightedGradebookTable.levelOverBudgetAboveOnly', + defaultMessage: + 'Some level contributions are above the maximum level contributions and have been capped.', + }, + levelOverBudgetBelowOnly: { + id: 'course.gradebook.WeightedGradebookTable.levelOverBudgetBelowOnly', + defaultMessage: + 'Some level contributions are below 0 and have been raised to 0.', + }, + levelOverBudgetBoth: { + id: 'course.gradebook.WeightedGradebookTable.levelOverBudgetBoth', + defaultMessage: + 'Some level contributions are outside the valid range (below 0 or above the maximum level contributions) and are floored and capped accordingly.', + }, + levelCellCappedAbove: { + id: 'course.gradebook.WeightedGradebookTable.levelCellCappedAbove', + defaultMessage: 'Set to {weight} because the formula produced {raw}.', + }, + levelCellCappedBelow: { + id: 'course.gradebook.WeightedGradebookTable.levelCellCappedBelow', + defaultMessage: 'Set to 0 because the formula produced {raw}.', + }, + levelCellDivByZero: { + id: 'course.gradebook.WeightedGradebookTable.levelCellDivByZero', + defaultMessage: + 'Set to 0 because the formula divides by zero for this level.', + }, }); type DisplayMode = 'points' | 'percent'; @@ -178,6 +223,9 @@ interface Props { canManageWeights: boolean; courseTitle: string; courseId: number; + gamificationEnabled: boolean; + courseMaxLevel: number; + levelContribution: LevelContributionData; } const precisionNeeded = (v: number): 0 | 1 | 2 => { @@ -209,6 +257,9 @@ const NAME_WIDTH_EXPANDED = 260; const EMAIL_WIDTH = 250; const EXTERNAL_ID_WIDTH = 150; const TAB_WIDTH = 120; +const LEVEL_WIDTH = 90; +const LEVEL_CONTRIBUTIONS_WIDTH = 110; +const TOTAL_WIDTH = 120; const WeightedGradebookTable = ({ categories, @@ -219,6 +270,9 @@ const WeightedGradebookTable = ({ canManageWeights, courseTitle, courseId, + gamificationEnabled, + courseMaxLevel, + levelContribution, }: Props): JSX.Element => { const { t } = useTranslation(); const [configureOpen, setConfigureOpen] = useState(false); @@ -253,11 +307,86 @@ const WeightedGradebookTable = ({ ); }, [assessments]); - const totalWeight = resolvedTabs.reduce( + // Level Contribution column: shown whenever the contribution is active (drives + // the total + per-row points too). + const showLevelContribution = + gamificationEnabled && levelContribution.enabled; + // Raw Level column: the actual student level, shown alongside the contribution + // so staff can sanity-check it. Gated additionally on `show`. + // + // Both columns are server-controlled (not in the column picker), so their + // *presence* — not just their default visibility — is gated here, and they are + // locked visible (below). Plumbing these through `defaultVisible` failed: + // defaultVisible is a one-time seed that loses to stale persisted localStorage, + // stranding a column hidden with no picker entry to recover it. + const showRawLevel = showLevelContribution && levelContribution.show; + + // The level weight is a suggested maximum (never caps the total), so a formula + // can push a student's contribution past it. Flag that on the column subheader. + // Reuses the dialog's check so both views warn on identical conditions. + const levelBudgetOffenders = useMemo( + () => + showLevelContribution + ? levelOffenders( + students, + parseFormula(levelContribution.formula), + levelContribution.weight, + ) + : null, + [showLevelContribution, students, levelContribution], + ); + + // Students for whom the formula is undefined (divide-by-zero -> NaN). The + // store/backend store these as null; we surface them as 0 + a warning. + const unscoreableIds = useMemo( + () => new Set(levelBudgetOffenders?.unscoreable.map((o) => o.id) ?? []), + [levelBudgetOffenders], + ); + const levelOverBudget = + levelBudgetOffenders !== null && + (levelBudgetOffenders.below.length > 0 || + levelBudgetOffenders.above.length > 0); + const levelClampByStudent = useMemo(() => { + const map = new Map< + number, + { bound: 'below' | 'above' | 'unscoreable'; raw: number } + >(); + if (showLevelContribution && levelBudgetOffenders) { + if (levelContribution.clamp) { + levelBudgetOffenders.below.forEach((o) => + map.set(o.id, { bound: 'below', raw: o.value }), + ); + levelBudgetOffenders.above.forEach((o) => + map.set(o.id, { bound: 'above', raw: o.value }), + ); + } + levelBudgetOffenders.unscoreable.forEach((o) => + map.set(o.id, { bound: 'unscoreable', raw: NaN }), + ); + } + return map; + }, [showLevelContribution, levelContribution.clamp, levelBudgetOffenders]); + const getLevelOverBudgetTranslation = (): MessageDescriptor => { + const below = levelBudgetOffenders?.below.length ?? 0; + const above = levelBudgetOffenders?.above.length ?? 0; + if (below > 0 && above > 0) return translations.levelOverBudgetBoth; + if (above > 0) return translations.levelOverBudgetAboveOnly; + return translations.levelOverBudgetBelowOnly; + }; + const getLevelCellTranslation = ( + bound: 'below' | 'above' | 'unscoreable', + ): MessageDescriptor => { + if (bound === 'unscoreable') return translations.levelCellDivByZero; + if (bound === 'below') return translations.levelCellCappedBelow; + return translations.levelCellCappedAbove; + }; + const tabTotalWeight = resolvedTabs.reduce( (acc, tab) => acc + (allExcludedTabIds.has(tab.id) ? 0 : tab.gradebookWeight ?? 0), 0, ); + const totalWeight = + tabTotalWeight + (showLevelContribution ? levelContribution.weight : 0); const allWeightsZero = totalWeight === 0; const totalDisplayValue = (total: number | null): number | null => { @@ -296,20 +425,54 @@ const WeightedGradebookTable = ({ const nameWidth = expandedIds.size > 0 ? NAME_WIDTH_EXPANDED : NAME_WIDTH_COLLAPSED; + const studentLevelById = useMemo( + () => new Map(students.map((s) => [s.id, s.level])), + [students], + ); + + const studentLevelContributionById = useMemo( + () => new Map(students.map((s) => [s.id, s.levelContribution ?? null])), + [students], + ); + const breakdownsByStudent = useMemo( () => new Map( - [...expandedIds].map((studentId) => [ - studentId, - computeStudentBreakdown({ + [...expandedIds].map((studentId) => { + const breakdown = computeStudentBreakdown({ studentId, tabs: resolvedTabs, assessments, submissions, - }), - ]), + level: studentLevelById.get(studentId) ?? 0, + levelContribution: showLevelContribution + ? levelContribution + : undefined, + levelContributionPoints: showLevelContribution + ? studentLevelContributionById.get(studentId) ?? null + : null, + courseMaxLevel, + }); + // Level row first, mirroring the Level Contribution column being the + // first contribution column (left of the tabs). + const ordered = [ + ...breakdown.filter((tb) => tb.tabId === LEVEL_TAB_ID), + ...breakdown.filter((tb) => tb.tabId !== LEVEL_TAB_ID), + ]; + return [studentId, ordered] as [studentId: number, typeof ordered]; + }), ), - [expandedIds, resolvedTabs, assessments, submissions], + [ + expandedIds, + resolvedTabs, + assessments, + submissions, + studentLevelById, + studentLevelContributionById, + showLevelContribution, + levelContribution, + courseMaxLevel, + ], ); const row1Ref = useRef(null); @@ -369,16 +532,26 @@ const WeightedGradebookTable = ({ return () => observer.disconnect(); }, [visibleCategories, resolvedTabs]); - const rows = useMemo( - () => - computeWeightedRows({ - students, - tabs: resolvedTabs, - assessments, - submissions, - }), - [students, resolvedTabs, assessments, submissions], - ); + const rows = useMemo(() => { + const base = computeWeightedRows({ + students, + tabs: resolvedTabs, + assessments, + submissions, + showLevelContribution, + }); + if (unscoreableIds.size === 0) return base; + return base.map((r) => + unscoreableIds.has(r.studentId) ? { ...r, levelContribution: 0 } : r, + ); + }, [ + students, + resolvedTabs, + assessments, + submissions, + showLevelContribution, + unscoreableIds, + ]); const columnPrecisions = useMemo(() => { const tabPrecs = resolvedTabs.map((tab, idx) => @@ -391,6 +564,7 @@ const WeightedGradebookTable = ({ return { tabs: tabPrecs, total: columnPrecision(rows.map((r) => totalDisplayValue(r.total))), + level: columnPrecision(rows.map((r) => r.levelContribution ?? null)), }; }, [rows, resolvedTabs, displayMode, totalWeight]); @@ -429,6 +603,31 @@ const WeightedGradebookTable = ({ }, ]; + // Raw Level — the right-most student-info column, just left of the + // contribution columns. The actual student level, shown for verification. + if (showRawLevel) { + cols.push({ + id: 'level', + title: t(translations.levelHeader), + accessorFn: (row) => `${row.level}`, + cell: (row) => `${row.level}`, + csvDownloadable: true, + }); + } + + // Level Contribution — the left-most contribution column, immediately right + // of the student-info columns and before the per-tab columns. + if (showLevelContribution) { + cols.push({ + id: 'levelContribution', + title: t(translations.levelContributionHeader), + accessorFn: (row) => fmtCsv(row.levelContribution ?? null), + cell: (row) => + fmtDisplay(row.levelContribution ?? null, columnPrecisions.level), + csvDownloadable: true, + }); + } + resolvedTabs.forEach((tab, idx) => { const weight = tab.gradebookWeight ?? 0; const prec = columnPrecisions.tabs[idx]; @@ -460,19 +659,31 @@ const WeightedGradebookTable = ({ columnPrecisions, displayMode, totalWeight, + showLevelContribution, + showRawLevel, + levelContribution.weight, ]); + // Lock the level columns visible whenever present so a stale persisted-hidden + // entry can't keep them hidden (the picker doesn't expose them to recover). + const lockedColumns = useMemo(() => { + const locked = ['name']; + if (showLevelContribution) locked.push('levelContribution'); + if (showRawLevel) locked.push('level'); + return locked; + }, [showLevelContribution, showRawLevel]); + const columnPicker = useMemo( () => ({ render: (context: ColumnPickerRenderContext) => ( ), - locked: ['name'], + locked: lockedColumns, triggerLabel: t(translations.selectColumns), dialogTitle: t(translations.dialogTitle), storageKey: `gradebook_weighted_columns_${courseId}`, }), - [courseId, t], + [courseId, t, lockedColumns], ); const { @@ -513,6 +724,12 @@ const WeightedGradebookTable = ({ let lastIdentityField: 'name' | 'email' | 'externalId' = 'name'; if (showExternalId) lastIdentityField = 'externalId'; else if (showEmail) lastIdentityField = 'email'; + // These columns are locked visible when present, so visibility resolves to true; + // gating the manual header on it keeps the header in lockstep with the body + // (no colSpan drift) even on the first frame before the lock effect runs. + const showLevelContributionCol = + showLevelContribution && (visibility.levelContribution ?? true) === true; + const showRawLevelCol = showRawLevel && (visibility.level ?? true) === true; const allRowsSelected = body.allFilteredSelected ?? false; const someRowsSelected = body.someFilteredSelected ?? false; @@ -523,6 +740,11 @@ const WeightedGradebookTable = ({ ? t(translations.percentTotalExact) : t(translations.outOfWeight, { weight: totalWeight }); + const levelBudgetLabel = + displayMode === 'percent' + ? t(translations.percentOfGrade, { weight: levelContribution.weight }) + : t(translations.outOfWeight, { weight: levelContribution.weight }); + return (
    {!allWeightsZero && !showingDefaults && } @@ -649,10 +871,14 @@ const WeightedGradebookTable = ({ {showEmail && } {showExternalId && } + {showRawLevelCol && } + {showLevelContributionCol && ( + + )} {resolvedTabs.map((tab) => ( ))} - + @@ -710,6 +936,36 @@ const WeightedGradebookTable = ({ {t(tableTranslations.externalId)} )} + {/* Raw Level — last student-info column (spans all 3 rows, no + weight subheader). */} + {showRawLevelCol && ( + + {t(translations.levelHeader)} + + )} + {/* Level Contribution — first contribution column, before the + category-grouped tabs. */} + {showLevelContributionCol && ( + + {t(translations.levelContributionHeader)} + + )} {visibleCategories.map((cat) => ( + {showLevelContributionCol && ( + + {levelContribution.clamp && + levelOverBudget && + levelBudgetOffenders ? ( + + + {levelBudgetLabel} + + + + ) : ( + levelBudgetLabel + )} + + )} {resolvedTabs.map((tab, i) => ( )} + {showRawLevelCol && ( + + {row.original.level} + + )} + {showLevelContributionCol && + ((): React.JSX.Element => { + const valueText = fmtDisplay( + row.original.levelContribution ?? null, + columnPrecisions.level, + ); + const info = levelClampByStudent.get( + row.original.studentId, + ); + return ( + + {info ? ( + + + + + {valueText} + + ) : ( + valueText + )} + + ); + })()} {row.original.subtotals.map((subtotal, i) => { const weight = resolvedTabs[i].gradebookWeight ?? 0; return ( @@ -918,7 +1249,7 @@ const WeightedGradebookTable = ({ {isExpanded && (breakdownsByStudent.get(studentId) ?? []).flatMap( - (tb, tabIdx) => + (tb) => tb.assessments.map((a) => { const isExcluded = a.excluded; const weightText = t( @@ -928,10 +1259,20 @@ const WeightedGradebookTable = ({ Math.round(a.effectiveWeight * 100) / 100, }, ); - const gradeText = + const assessmentGradeText = a.grade === null ? `-/${a.maxGrade}` : `${a.grade}/${a.maxGrade}`; + // The level row has no max — courseMaxLevel plays + // no part in the contribution, so showing + // "level/courseMaxLevel" would falsely imply a + // proportional derivation. Show the raw level only. + const gradeText = + tb.tabId === LEVEL_TAB_ID + ? t(translations.levelBreakdownDetail, { + level: a.grade ?? 0, + }) + : assessmentGradeText; return ( )} + {/* Mirror the parent row's two level columns so + the tab cells below stay column-aligned. Raw + Level has no per-assessment breakdown (empty); + Level Contribution carries its value on the + synthetic level row (tabId === LEVEL_TAB_ID), + matching how each tab cell carries its own. */} + {showRawLevelCol && } + {showLevelContributionCol && ( + + {tb.tabId === LEVEL_TAB_ID + ? fmtDisplay( + a.points, + columnPrecisions.level, + ) + : ''} + + )} {resolvedTabs.map((tab, i) => { const tabCellValue = isExcluded ? '—' @@ -1021,7 +1379,12 @@ const WeightedGradebookTable = ({ align="right" {...groupEndIf(tabIsCategoryEnd[i])} > - {i === tabIdx ? tabCellValue : ''} + {/* Place the value by tab id, not array + position, so the breakdown row order is + free to differ from the column order. */} + {tab.id === tb.tabId + ? tabCellValue + : ''} ); })} @@ -1044,8 +1407,12 @@ const WeightedGradebookTable = ({ setConfigureOpen(false)} open={configureOpen} + students={students} tabs={tabs} /> )} diff --git a/client/app/bundles/course/gradebook/computeWeighted.ts b/client/app/bundles/course/gradebook/computeWeighted.ts index 4eea81960b7..40905fe8485 100644 --- a/client/app/bundles/course/gradebook/computeWeighted.ts +++ b/client/app/bundles/course/gradebook/computeWeighted.ts @@ -1,19 +1,28 @@ // client/app/bundles/course/gradebook/computeWeighted.ts import { AssessmentData, + LevelContributionData, StudentData, SubmissionData, TabData, } from 'types/course/gradebook'; +import { ParsedFormula } from './levelFormula'; + type GradeEntry = Pick; +// Synthetic ids for the Level term — disjoint from real (positive) tab/assessment ids. +export const LEVEL_TAB_ID = -1; +export const LEVEL_ASSESSMENT_ID = -1; + export interface WeightedRow { studentId: number; name: string; email: string; externalId: string | null; subtotals: (number | null)[]; + level: number; + levelContribution: number | null; total: number | null; } @@ -139,6 +148,71 @@ const totalFromSubtotals = ( return contributingCount > 0 ? total : null; }; +// Combine the tab total with the Level term: null only when neither contributes. +const combineTotal = ( + tabTotal: number | null, + lvl: number | null, +): number | null => + tabTotal == null && lvl == null ? null : (tabTotal ?? 0) + (lvl ?? 0); + +// True when any student's contribution falls outside [0, weight] (drives the dialog warning). +export const levelOutOfRange = ( + students: { level: number }[], + cfg: LevelContributionData, + parsed: ParsedFormula, +): boolean => { + if (!parsed.ok) return false; + return students.some((s) => { + const p = parsed.evaluate(s.level); + return p < 0 || p > cfg.weight; + }); +}; + +export interface LevelOffender { + id: number; + name: string; + level: number; + value: number; +} + +export interface LevelOffenders { + // Below 0, most negative first; above max, highest first — so the dialog can + // name the worst offenders on each side. + below: LevelOffender[]; + above: LevelOffender[]; + // Students whose contribution is undefined (divide-by-zero) — value is NaN, + // so the dialog names them by level, not value. + unscoreable: LevelOffender[]; +} + +// Students whose Level contribution falls outside [0, max], split by which bound +// they breach. Empty on both sides when the formula is invalid. Feeds the dialog's +// out-of-range warning, which names the most extreme offenders. +export const levelOffenders = ( + students: { id: number; name: string; level: number }[], + parsed: ParsedFormula | null, + max: number, +): LevelOffenders => { + if (!parsed?.ok) return { below: [], above: [], unscoreable: [] }; + const evaluated = students.map((s) => ({ + id: s.id, + name: s.name, + level: s.level, + value: parsed.evaluate(s.level), + })); + return { + below: evaluated + .filter((e) => e.value < 0) + .sort((a, b) => a.value - b.value), + above: evaluated + .filter((e) => e.value > max) + .sort((a, b) => b.value - a.value), + unscoreable: evaluated + .filter((e) => Number.isNaN(e.value)) + .sort((a, b) => a.level - b.level || a.name.localeCompare(b.name)), + }; +}; + interface SubtotalArgs { studentId: number; tab: TabData; @@ -164,6 +238,11 @@ interface TotalArgs { tabs: TabData[]; assessments: AssessmentData[]; submissions: GradeEntry[]; + level?: number; + levelContribution?: LevelContributionData; + levelContributionPoints?: number | null; + // The course's current top level, used only as the Level breakdown row's denominator. + courseMaxLevel?: number; } export const computeStudentTotal = ({ @@ -171,6 +250,8 @@ export const computeStudentTotal = ({ tabs, assessments, submissions, + level, + levelContributionPoints, }: TotalArgs): number | null => { const gradeLookup = buildGradeLookup(submissions); const assessmentsByTab = buildAssessmentsByTab(assessments); @@ -182,7 +263,8 @@ export const computeStudentTotal = ({ gradeLookup, ), ); - return totalFromSubtotals(subtotals, tabs); + const tabTotal = totalFromSubtotals(subtotals, tabs); + return combineTotal(tabTotal, levelContributionPoints ?? null); }; export const computeStudentBreakdown = ({ @@ -190,10 +272,14 @@ export const computeStudentBreakdown = ({ tabs, assessments, submissions, + level, + levelContribution, + levelContributionPoints, + courseMaxLevel, }: TotalArgs): TabBreakdown[] => { const gradeLookup = buildGradeLookup(submissions); const assessmentsByTab = buildAssessmentsByTab(assessments); - return tabs.map((tab) => { + const result: TabBreakdown[] = tabs.map((tab) => { const list = assessmentsByTab.get(tab.id) ?? []; const weight = tab.gradebookWeight ?? 0; const includedCount = list.filter((a) => !a.gradebookExcluded).length; @@ -226,6 +312,25 @@ export const computeStudentBreakdown = ({ }); return { tabId: tab.id, assessments: contributions }; }); + + const lvl = levelContributionPoints ?? null; + if (levelContribution?.enabled && lvl != null) { + result.push({ + tabId: LEVEL_TAB_ID, + assessments: [ + { + assessmentId: LEVEL_ASSESSMENT_ID, + title: 'Level', + grade: level ?? 0, + maxGrade: courseMaxLevel ?? 0, + points: lvl, + effectiveWeight: levelContribution.weight, + excluded: false, + }, + ], + }); + } + return result; }; interface WeightedRowsArgs { @@ -233,6 +338,7 @@ interface WeightedRowsArgs { tabs: TabData[]; assessments: AssessmentData[]; submissions: GradeEntry[]; + showLevelContribution?: boolean; } // Batch entry point used by the table: builds the indexes ONCE and reuses them @@ -242,6 +348,7 @@ export const computeWeightedRows = ({ tabs, assessments, submissions, + showLevelContribution = true, }: WeightedRowsArgs): WeightedRow[] => { const gradeLookup = buildGradeLookup(submissions); const assessmentsByTab = buildAssessmentsByTab(assessments); @@ -254,13 +361,19 @@ export const computeWeightedRows = ({ gradeLookup, ), ); + const tabTotal = totalFromSubtotals(subtotals, tabs); + const lvl = showLevelContribution + ? student.levelContribution ?? null + : null; return { studentId: student.id, name: student.name, email: student.email, externalId: student.externalId, subtotals, - total: totalFromSubtotals(subtotals, tabs), + level: student.level, + levelContribution: lvl, + total: combineTotal(tabTotal, lvl), }; }); }; diff --git a/client/app/bundles/course/gradebook/levelFormula.ts b/client/app/bundles/course/gradebook/levelFormula.ts new file mode 100644 index 00000000000..bbeb8793b88 --- /dev/null +++ b/client/app/bundles/course/gradebook/levelFormula.ts @@ -0,0 +1,380 @@ +// A deliberately tiny arithmetic grammar mapping a student's Level to grade-points. +// PARSE, DON'T EVAL: the source is read into an AST of known node types and evaluated +// with plain arithmetic. No code path runs the string as code, so a hostile string can +// only ever produce a parse error. +// +// Grammar (standard precedence): +// expr := term (('+' | '-') term)* +// term := factor (('*' | '/') factor)* +// factor := '-' factor | primary +// primary := number | var | func '(' args ')' | '(' expr ')' +// var := 'level' +// func1 := 'floor' | 'ceil' | 'round' (1 arg) +// func2 := 'min' | 'max' (2 args) +// +// Examples (staff type these into the formula field): +// min(level, 20) * 0.5 → 0.5 grade-points per level, capped at level 20 (max 10) +// floor(level / 5) → 1 grade-point per 5 levels +// level → 1 grade-point per level, uncapped +// Anything outside this grammar (assignments, unknown identifiers, unknown +// functions, stray characters, …) is rejected at parse time — parseFormula +// returns { ok: false, error } rather than throwing or running the input. + +import type { FormulaNode } from 'types/course/gradebook'; + +interface Scope { + level: number; +} + +const FUNCTIONS_1: Record number> = { + floor: Math.floor, + ceil: Math.ceil, + round: Math.round, +}; +const FUNCTIONS_2: Record number> = { + min: Math.min, + max: Math.max, +}; +const VARIABLES = new Set(['level']); + +const FUNCTION_NAMES = [ + ...Object.keys(FUNCTIONS_1), + ...Object.keys(FUNCTIONS_2), +]; +const VALID_NAMES = [...VARIABLES, ...FUNCTION_NAMES]; + +const editDistance = (a: string, b: string): number => { + const rows = a.length; + const cols = b.length; + const dp: number[][] = Array.from({ length: rows + 1 }, () => + new Array(cols + 1).fill(0), + ); + for (let i = 0; i <= rows; i += 1) dp[i][0] = i; + for (let j = 0; j <= cols; j += 1) dp[0][j] = j; + for (let i = 1; i <= rows; i += 1) { + for (let j = 1; j <= cols; j += 1) { + const cost = a[i - 1] === b[j - 1] ? 0 : 1; + dp[i][j] = Math.min( + dp[i - 1][j] + 1, + dp[i][j - 1] + 1, + dp[i - 1][j - 1] + cost, + ); + } + } + return dp[rows][cols]; +}; + +const nearestName = ( + name: string, + candidates: readonly string[], +): string | null => { + const lower = name.toLowerCase(); + let best: string | null = null; + let bestDistance = Infinity; + candidates.forEach((candidate) => { + const distance = editDistance(lower, candidate); + if (distance < bestDistance) { + bestDistance = distance; + best = candidate; + } + }); + return bestDistance <= 2 ? best : null; +}; + +const unrecognisedError = ( + name: string, + candidates: readonly string[], +): string => { + const suggestion = nearestName(name, candidates); + return suggestion + ? `"${name}" is not recognised - did you mean "${suggestion}"?` + : `"${name}" is not recognised. You can use: ${candidates.join(', ')}.`; +}; + +export type ParsedFormula = + | { ok: true; evaluate: (level: number) => number } + | { ok: false; error: string }; + +type Token = + | { type: 'num'; value: number } + | { type: 'ident'; value: string } + | { type: 'op'; value: '+' | '-' | '*' | '/' } + | { type: 'lparen' } + | { type: 'rparen' } + | { type: 'comma' }; + +const isDigit = (c: string): boolean => c >= '0' && c <= '9'; +const isAlpha = (c: string): boolean => + (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); + +const tokenize = (src: string): Token[] => { + const tokens: Token[] = []; + let i = 0; + while (i < src.length) { + const c = src[i]; + if (c === ' ' || c === '\t' || c === '\n' || c === '\r') { + i += 1; + } else if (isDigit(c) || c === '.') { + let j = i + 1; + while (j < src.length && (isDigit(src[j]) || src[j] === '.')) j += 1; + const text = src.slice(i, j); + const value = Number(text); + if (!Number.isFinite(value)) + throw new Error(`"${text}" is not a valid number.`); + tokens.push({ type: 'num', value }); + i = j; + } else if (isAlpha(c)) { + let j = i + 1; + while (j < src.length && isAlpha(src[j])) j += 1; + tokens.push({ type: 'ident', value: src.slice(i, j) }); + i = j; + } else if (c === '+' || c === '-' || c === '−' || c === '*' || c === '/') { + const op = c === '−' ? '-' : c; + tokens.push({ type: 'op', value: op as '+' | '-' | '*' | '/' }); + i += 1; + } else if (c === '(') { + tokens.push({ type: 'lparen' }); + i += 1; + } else if (c === ')') { + tokens.push({ type: 'rparen' }); + i += 1; + } else if (c === ',') { + tokens.push({ type: 'comma' }); + i += 1; + } else { + throw new Error(`"${c}" cannot be used in a formula.`); + } + } + return tokens; +}; + +// Recursive-descent parser building a serializable FormulaNode AST. +const buildAst = (tokens: Token[]): FormulaNode => { + let pos = 0; + const peek = (): Token | undefined => tokens[pos]; + const eat = (): Token => { + const t = tokens[pos]; + if (!t) throw new Error('The formula looks unfinished.'); + pos += 1; + return t; + }; + const expect = (type: 'comma' | 'rparen'): void => { + const t = peek(); + if (!t || t.type !== type) { + throw new Error( + type === 'comma' + ? 'This needs a comma - for example min(level, 25).' + : 'This needs a closing bracket ")".', + ); + } + pos += 1; + }; + + let parseExpr: () => FormulaNode; + + const parsePrimary = (): FormulaNode => { + const t = eat(); + if (t.type === 'num') return { type: 'num', value: t.value }; + if (t.type === 'lparen') { + const inner = parseExpr(); + expect('rparen'); + return inner; + } + if (t.type === 'ident') { + const name = t.value; + if (peek()?.type === 'lparen') { + eat(); // consume '(' + if (FUNCTIONS_1[name]) { + const arg = parseExpr(); + expect('rparen'); + return { type: 'call1', fn: name as 'floor' | 'ceil' | 'round', arg }; + } + if (FUNCTIONS_2[name]) { + const a = parseExpr(); + expect('comma'); + const b = parseExpr(); + expect('rparen'); + return { type: 'call2', fn: name as 'min' | 'max', a, b }; + } + throw new Error(unrecognisedError(name, FUNCTION_NAMES)); + } + if (!VARIABLES.has(name)) + throw new Error(unrecognisedError(name, VALID_NAMES)); + return { type: 'var', name: 'level' }; + } + throw new Error('The formula is not complete here.'); + }; + + const parseFactor = (): FormulaNode => { + const t = peek(); + if (t && t.type === 'op' && t.value === '-') { + eat(); + const operand = parseFactor(); + return { type: 'neg', operand }; + } + return parsePrimary(); + }; + + const parseTerm = (): FormulaNode => { + let left = parseFactor(); + let t = peek(); + while (t && t.type === 'op' && (t.value === '*' || t.value === '/')) { + eat(); + const right = parseFactor(); + const op = t.value as '*' | '/'; + left = { type: 'binop', op, left, right }; + t = peek(); + } + return left; + }; + + parseExpr = (): FormulaNode => { + let left = parseTerm(); + let t = peek(); + while (t && t.type === 'op' && (t.value === '+' || t.value === '-')) { + eat(); + const right = parseTerm(); + const op = t.value as '+' | '-'; + left = { type: 'binop', op, left, right }; + t = peek(); + } + return left; + }; + + const ast = parseExpr(); + if (pos !== tokens.length) + throw new Error( + 'Looks like an operator is missing - for example level × 2.', + ); + return ast; +}; + +const evaluateBinop = ( + op: '+' | '-' | '*' | '/', + left: number, + right: number, +): number => { + switch (op) { + case '+': + return left + right; + case '-': + return left - right; + case '*': + return left * right; + case '/': + return right === 0 ? NaN : left / right; + default: + return 0; + } +}; + +// Traverse a FormulaNode AST, substituting scope.level for 'var' nodes. +// Used by both parseFormula (FE evaluation) and serializeFormula callers. +export const evaluateNode = (node: FormulaNode, scope: Scope): number => { + switch (node.type) { + case 'num': + return node.value; + case 'var': + return scope.level; + case 'neg': + return -evaluateNode(node.operand, scope); + case 'binop': { + const left = evaluateNode(node.left, scope); + const right = evaluateNode(node.right, scope); + return evaluateBinop(node.op, left, right); + } + case 'call1': { + const arg = evaluateNode(node.arg, scope); + return FUNCTIONS_1[node.fn](arg); + } + case 'call2': { + const left = evaluateNode(node.a, scope); + const right = evaluateNode(node.b, scope); + return FUNCTIONS_2[node.fn](left, right); + } + default: + return 0; + } +}; + +const dependsOnLevel = (node: FormulaNode): boolean => { + switch (node.type) { + case 'var': + return true; + case 'num': + return false; + case 'neg': + return dependsOnLevel(node.operand); + case 'binop': + return dependsOnLevel(node.left) || dependsOnLevel(node.right); + case 'call1': + return dependsOnLevel(node.arg); + case 'call2': + return dependsOnLevel(node.a) || dependsOnLevel(node.b); + default: + return false; + } +}; + +const hasConstantZeroDivisor = (node: FormulaNode): boolean => { + switch (node.type) { + case 'num': + case 'var': + return false; + case 'neg': + return hasConstantZeroDivisor(node.operand); + case 'binop': + if ( + node.op === '/' && + !dependsOnLevel(node.right) && + evaluateNode(node.right, { level: 0 }) === 0 + ) + return true; + return ( + hasConstantZeroDivisor(node.left) || hasConstantZeroDivisor(node.right) + ); + case 'call1': + return hasConstantZeroDivisor(node.arg); + case 'call2': + return hasConstantZeroDivisor(node.a) || hasConstantZeroDivisor(node.b); + default: + return false; + } +}; + +// Parse src into a serializable FormulaNode AST. Returns ok:false on any parse error. +// Used by the save flow to produce the AST that is sent to and stored by the backend. +export const serializeFormula = ( + src: string, +): { ok: true; ast: FormulaNode } | { ok: false; error: string } => { + try { + const tokens = tokenize(src); + if (tokens.length === 0) throw new Error('Enter a formula.'); + const ast = buildAst(tokens); + if (hasConstantZeroDivisor(ast)) + throw new Error('This formula always divides by zero.'); + return { ok: true, ast }; + } catch (e) { + return { + ok: false, + error: e instanceof Error ? e.message : 'This formula is not valid.', + }; + } +}; + +// Parse src and return an evaluate function. Public API is unchanged — callers +// get { ok: true, evaluate } or { ok: false, error }. Backed by evaluateNode. +export const parseFormula = (src: string): ParsedFormula => { + const result = serializeFormula(src); + if (!result.ok) return result; + const { ast } = result; + return { + ok: true, + evaluate: (level): number => { + const val = evaluateNode(ast, { level }); + return Number.isFinite(val) ? val : NaN; + }, + }; +}; + +export const seedLevelFormula = 'level'; diff --git a/client/app/bundles/course/gradebook/operations.ts b/client/app/bundles/course/gradebook/operations.ts index ae2f962fbbf..344f24fb7ca 100644 --- a/client/app/bundles/course/gradebook/operations.ts +++ b/client/app/bundles/course/gradebook/operations.ts @@ -1,5 +1,8 @@ import type { Operation } from 'store'; -import type { UpdateWeightsPayload } from 'types/course/gradebook'; +import type { + LevelContributionSaveData, + UpdateWeightsPayload, +} from 'types/course/gradebook'; import CourseAPI from 'api/course'; @@ -11,10 +14,24 @@ const fetchGradebook = (): Operation => async (dispatch) => { }; export const updateGradebookWeights = - (weights: UpdateWeightsPayload['weights']): Operation => + ( + weights: UpdateWeightsPayload['weights'], + levelContribution?: LevelContributionSaveData, + ): Operation => async (dispatch) => { - const response = await CourseAPI.gradebook.updateWeights({ weights }); - dispatch(actions.updateTabWeights(response.data)); + const response = await CourseAPI.gradebook.updateWeights( + levelContribution ? { weights, levelContribution } : { weights }, + ); + // BE response does not echo formulaAst; merge it back so the store reducer + // can optimistically recompute per-student levelContribution without a refetch. + const responseData = { ...response.data }; + if (levelContribution && responseData.levelContribution) { + responseData.levelContribution = { + ...responseData.levelContribution, + formulaAst: levelContribution.formulaAst, + }; + } + dispatch(actions.updateTabWeights(responseData)); }; export default fetchGradebook; diff --git a/client/app/bundles/course/gradebook/pages/GradebookIndex/index.tsx b/client/app/bundles/course/gradebook/pages/GradebookIndex/index.tsx index 095d3758fc3..03f36e41746 100644 --- a/client/app/bundles/course/gradebook/pages/GradebookIndex/index.tsx +++ b/client/app/bundles/course/gradebook/pages/GradebookIndex/index.tsx @@ -20,7 +20,9 @@ import { getAssessments, getCanManageWeights, getCategories, + getCourseMaxLevel, getGamificationEnabled, + getLevelContribution, getStudents, getSubmissions, getTabs, @@ -75,6 +77,8 @@ const GradebookIndex: FC = () => { const gamificationEnabled = useAppSelector(getGamificationEnabled); const weightedViewEnabled = useAppSelector(getWeightedViewEnabled); const canManageWeights = useAppSelector(getCanManageWeights); + const courseMaxLevel = useAppSelector(getCourseMaxLevel); + const levelContribution = useAppSelector(getLevelContribution); useEffect(() => { dispatch(fetchGradebook()) @@ -104,7 +108,10 @@ const GradebookIndex: FC = () => { canManageWeights={canManageWeights} categories={categories} courseId={courseId} + courseMaxLevel={courseMaxLevel} courseTitle={courseTitle} + gamificationEnabled={gamificationEnabled} + levelContribution={levelContribution} students={students} submissions={submissions} tabs={tabs} diff --git a/client/app/bundles/course/gradebook/selectors.ts b/client/app/bundles/course/gradebook/selectors.ts index 0fb7d1398d5..6256f7af9f6 100644 --- a/client/app/bundles/course/gradebook/selectors.ts +++ b/client/app/bundles/course/gradebook/selectors.ts @@ -26,3 +26,9 @@ export const getWeightedViewEnabled = (state: AppState): boolean => getLocalState(state).weightedViewEnabled; export const getCanManageWeights = (state: AppState): boolean => getLocalState(state).canManageWeights; +export const getLevelContribution = ( + state: AppState, +): GradebookState['levelContribution'] => + getLocalState(state).levelContribution; +export const getCourseMaxLevel = (state: AppState): number => + getLocalState(state).courseMaxLevel; diff --git a/client/app/bundles/course/gradebook/store.ts b/client/app/bundles/course/gradebook/store.ts index 4777e16b3b6..e5a30f044c5 100644 --- a/client/app/bundles/course/gradebook/store.ts +++ b/client/app/bundles/course/gradebook/store.ts @@ -1,9 +1,11 @@ import { produce } from 'immer'; import type { GradebookData, + LevelContributionData, UpdateWeightsPayload, } from 'types/course/gradebook'; +import { evaluateNode } from './levelFormula'; import type { AssessmentData, CategoryData, @@ -24,6 +26,8 @@ interface GradebookState { gamificationEnabled: boolean; weightedViewEnabled: boolean; canManageWeights: boolean; + levelContribution: LevelContributionData; + courseMaxLevel: number; } interface SaveGradebookAction { @@ -45,6 +49,14 @@ const initialState: GradebookState = { gamificationEnabled: false, weightedViewEnabled: false, canManageWeights: false, + levelContribution: { + enabled: false, + formula: '', + weight: 0, + show: false, + clamp: true, + }, + courseMaxLevel: 0, }; const reducer = produce( @@ -62,6 +74,9 @@ const reducer = produce( draft.gamificationEnabled = action.payload.gamificationEnabled; draft.weightedViewEnabled = action.payload.weightedViewEnabled; draft.canManageWeights = action.payload.canManageWeights; + draft.levelContribution = + action.payload.levelContribution ?? initialState.levelContribution; + draft.courseMaxLevel = action.payload.courseMaxLevel ?? 0; break; } case UPDATE_TAB_WEIGHTS: { @@ -97,6 +112,26 @@ const reducer = produce( } }, ); + if (action.payload.levelContribution) { + const { formulaAst, ...lcData } = action.payload.levelContribution; + draft.levelContribution = lcData; + if (lcData.enabled && formulaAst) { + draft.students.forEach((student) => { + const val = evaluateNode(formulaAst, { level: student.level }); + if (!Number.isFinite(val)) { + student.levelContribution = null; + } else { + student.levelContribution = lcData.clamp + ? Math.min(Math.max(val, 0), lcData.weight) + : val; + } + }); + } else { + draft.students.forEach((student) => { + student.levelContribution = null; + }); + } + } break; } default: diff --git a/client/app/types/course/gradebook.ts b/client/app/types/course/gradebook.ts index c9009afbfe0..49a9c62818d 100644 --- a/client/app/types/course/gradebook.ts +++ b/client/app/types/course/gradebook.ts @@ -27,6 +27,7 @@ export interface StudentData { externalId: string | null; level: number; totalXp: number; + levelContribution: number | null; } export interface SubmissionData { @@ -36,6 +37,14 @@ export interface SubmissionData { grade: number | null; } +export interface LevelContributionData { + enabled: boolean; + formula: string; + weight: number; + show: boolean; + clamp: boolean; +} + export interface GradebookData { categories: CategoryData[]; tabs: TabData[]; @@ -45,6 +54,8 @@ export interface GradebookData { gamificationEnabled: boolean; weightedViewEnabled: boolean; canManageWeights: boolean; + courseMaxLevel: number; + levelContribution: LevelContributionData; } export interface UpdateWeightsPayload { @@ -55,4 +66,22 @@ export interface UpdateWeightsPayload { excludedAssessmentIds?: number[]; assessmentWeights?: { assessmentId: number; weight: number }[]; }[]; + levelContribution?: LevelContributionSaveData; +} + +export type FormulaNode = + | { type: 'num'; value: number } + | { type: 'var'; name: 'level' } + | { type: 'neg'; operand: FormulaNode } + | { + type: 'binop'; + op: '+' | '-' | '*' | '/'; + left: FormulaNode; + right: FormulaNode; + } + | { type: 'call1'; fn: 'floor' | 'ceil' | 'round'; arg: FormulaNode } + | { type: 'call2'; fn: 'min' | 'max'; a: FormulaNode; b: FormulaNode }; + +export interface LevelContributionSaveData extends LevelContributionData { + formulaAst: FormulaNode | null; } diff --git a/client/locales/en.json b/client/locales/en.json index fd663f40200..642b295640e 100644 --- a/client/locales/en.json +++ b/client/locales/en.json @@ -8147,9 +8147,6 @@ "lib.components.table.MuiColumnPickerPrompt.defaultTitle": { "defaultMessage": "Select columns" }, - "lib.components.table.MuiColumnPickerPrompt.export": { - "defaultMessage": "Apply and Export" - }, "lib.components.table.MuiTableToolbar.directExport": { "defaultMessage": "Export" }, @@ -9293,6 +9290,78 @@ "course.gradebook.ConfigureWeightsPrompt.defaultsHint": { "defaultMessage": "No weights set yet - these are suggested defaults with every tab counting equally. Save to confirm, or adjust below." }, + "course.gradebook.ConfigureWeightsPrompt.levelTitle": { + "defaultMessage": "Level contribution" + }, + "course.gradebook.ConfigureWeightsPrompt.levelSubtitle": { + "defaultMessage": "Adds grade-points from each student's level, on top of tab contributions." + }, + "course.gradebook.ConfigureWeightsPrompt.levelHighestStudent": { + "defaultMessage": "Highest student level: {level}" + }, + "course.gradebook.ConfigureWeightsPrompt.levelCourseMax": { + "defaultMessage": "Course maximum level: {courseMaxLevel}" + }, + "course.gradebook.ConfigureWeightsPrompt.levelFormulaLabel": { + "defaultMessage": "Formula" + }, + "course.gradebook.ConfigureWeightsPrompt.levelFormulaHelper": { + "defaultMessage": "Formulas may contain numbers, arithmetic operators (+ − * /), parentheses, \"level\" as a variable, and the functions floor, ceil, round, min and max." + }, + "course.gradebook.ConfigureWeightsPrompt.levelPreview": { + "defaultMessage": "At max level ({maxLevel}): {pts} pts" + }, + "course.gradebook.ConfigureWeightsPrompt.levelCustomMaxLabel": { + "defaultMessage": "Use custom max level" + }, + "course.gradebook.ConfigureWeightsPrompt.levelMaxLevelLabel": { + "defaultMessage": "Max level" + }, + "course.gradebook.ConfigureWeightsPrompt.levelMaxLevelInvalid": { + "defaultMessage": "Max level must be at least 1" + }, + "course.gradebook.ConfigureWeightsPrompt.levelShowLabel": { + "defaultMessage": "Show level column in table" + }, + "course.gradebook.ConfigureWeightsPrompt.levelOffendersAbove": { + "defaultMessage": "{count, plural, =1 {{name1} is above {max}.} =2 {{name1} and {name2} are above {max}.} other {{name1}, {name2} and {extra} more are above {max}.}}" + }, + "course.gradebook.ConfigureWeightsPrompt.levelOffendersBelow": { + "defaultMessage": "{count, plural, =1 {{name1} is below 0.} =2 {{name1} and {name2} are below 0.} other {{name1}, {name2} and {extra} more are below 0.}}" + }, + "course.gradebook.ConfigureWeightsPrompt.levelFixAtMost": { + "defaultMessage": "These contributions will be set to {max}." + }, + "course.gradebook.ConfigureWeightsPrompt.levelFixAtLeast": { + "defaultMessage": "These contributions will be set to 0." + }, + "course.gradebook.ConfigureWeightsPrompt.levelFixBetween": { + "defaultMessage": "Contributions below 0 will be set to 0, and contributions above {max} will be set to {max}." + }, + "course.gradebook.ConfigureWeightsPrompt.levelClampLabel": { + "defaultMessage": "Keep results between 0 and max level contributions" + }, + "course.gradebook.ConfigureWeightsPrompt.levelUnscoreable": { + "defaultMessage": "{count, plural, =1 {The formula divides by zero for {name1}, so their level contribution is set to 0.} =2 {The formula divides by zero for {name1} and {name2}, so their level contributions are set to 0.} other {The formula divides by zero for {name1}, {name2} and {extra} more, so their level contributions are set to 0.}}" + }, + "course.gradebook.WeightedGradebookTable.levelOverBudgetAboveOnly": { + "defaultMessage": "Some level contributions are above the maximum level contributions and have been capped." + }, + "course.gradebook.WeightedGradebookTable.levelOverBudgetBelowOnly": { + "defaultMessage": "Some level contributions are below 0 and have been raised to 0." + }, + "course.gradebook.WeightedGradebookTable.levelOverBudgetBoth": { + "defaultMessage": "Some level contributions are outside the valid range (below 0 or above the maximum level contributions) and are floored and capped accordingly." + }, + "course.gradebook.WeightedGradebookTable.levelCellCappedAbove": { + "defaultMessage": "Set to {weight} because the formula produced {raw}." + }, + "course.gradebook.WeightedGradebookTable.levelCellCappedBelow": { + "defaultMessage": "Set to 0 because the formula produced {raw}." + }, + "course.gradebook.WeightedGradebookTable.levelCellDivByZero": { + "defaultMessage": "Set to 0 because the formula divides by zero for this level." + }, "course.gradebook.ConfigureWeightsPrompt.descriptionDrop": { "defaultMessage": "In Equal mode, optionally drop each student's N lowest-scoring assessments before averaging." }, @@ -9419,6 +9488,15 @@ "course.gradebook.WeightedGradebookTable.weightsDoNotSum": { "defaultMessage": "Weights do not sum to 100. Total may be inaccurate." }, + "course.gradebook.WeightedGradebookTable.levelHeader": { + "defaultMessage": "Level" + }, + "course.gradebook.WeightedGradebookTable.levelContributionHeader": { + "defaultMessage": "Level Contribution" + }, + "course.gradebook.WeightedGradebookTable.levelBreakdownDetail": { + "defaultMessage": "Level {level}" + }, "course.gradebook.TotalHint.policy": { "defaultMessage": "Totals count ungraded assessments as 0." }, diff --git a/client/locales/ko.json b/client/locales/ko.json index d26fc2c8cab..8943b8d0587 100644 --- a/client/locales/ko.json +++ b/client/locales/ko.json @@ -5396,6 +5396,9 @@ "course.gradebook.GradebookIndex.noStudents": { "defaultMessage": "학생 없음" }, + "course.gradebook.GradebookIndex.noStudentsHint": { + "defaultMessage": "학생이 강좌에 참여하면 여기에 성적이 표시됩니다." + }, "course.gradebook.GradebookTable.maxMarks": { "defaultMessage": "최고 점수" }, @@ -5405,6 +5408,9 @@ "course.gradebook.GradebookTable.noDataColumnsHintWithGamification": { "defaultMessage": "성적 또는 게임화 열을 선택하지 않았습니다. 내보내기에는 학생 정보만 포함됩니다." }, + "course.gradebook.GradeLinkHint.hint": { + "defaultMessage": "각 성적은 학생 제출물의 점수 합계입니다. 성적을 클릭하면 해당 제출물을 열고 점수를 조정할 수 있습니다." + }, "course.group.GroupCreationForm.description": { "defaultMessage": "설명 (선택사항)" }, @@ -9278,6 +9284,60 @@ "course.gradebook.ConfigureWeightsPrompt.defaultsHint": { "defaultMessage": "아직 설정된 가중치가 없습니다. 모든 탭이 동일한 비중으로 반영되도록 제안된 기본값입니다. 저장하여 확정하거나 아래에서 조정하세요." }, + "course.gradebook.ConfigureWeightsPrompt.levelTitle": { + "defaultMessage": "레벨 기여도" + }, + "course.gradebook.ConfigureWeightsPrompt.levelSubtitle": { + "defaultMessage": "탭 기여도에 더해 각 학생의 레벨에서 성적 점수를 추가합니다." + }, + "course.gradebook.ConfigureWeightsPrompt.levelHighestStudent": { + "defaultMessage": "최고 학생 레벨: {level}" + }, + "course.gradebook.ConfigureWeightsPrompt.levelCourseMax": { + "defaultMessage": "강좌 최대 레벨: {courseMaxLevel}" + }, + "course.gradebook.ConfigureWeightsPrompt.levelFormulaLabel": { + "defaultMessage": "수식" + }, + "course.gradebook.ConfigureWeightsPrompt.levelFormulaHelper": { + "defaultMessage": "수식에는 숫자, 산술 연산자(+ − * /), 괄호, 변수 \"level\", 그리고 floor, ceil, round, min, max 함수를 사용할 수 있습니다." + }, + "course.gradebook.ConfigureWeightsPrompt.levelPreview": { + "defaultMessage": "최대 레벨({maxLevel})에서: {pts}점" + }, + "course.gradebook.ConfigureWeightsPrompt.levelCustomMaxLabel": { + "defaultMessage": "사용자 지정 최대 레벨 사용" + }, + "course.gradebook.ConfigureWeightsPrompt.levelMaxLevelLabel": { + "defaultMessage": "최대 레벨" + }, + "course.gradebook.ConfigureWeightsPrompt.levelMaxLevelInvalid": { + "defaultMessage": "최대 레벨은 1 이상이어야 합니다" + }, + "course.gradebook.ConfigureWeightsPrompt.levelShowLabel": { + "defaultMessage": "표에 레벨 열 표시" + }, + "course.gradebook.ConfigureWeightsPrompt.levelOffendersAbove": { + "defaultMessage": "{count, plural, =1 {{name1}이(가) {max}보다 높습니다.} =2 {{name1} 및 {name2}이(가) {max}보다 높습니다.} other {{name1}, {name2} 및 그 외 {extra}명이 {max}보다 높습니다.}}" + }, + "course.gradebook.ConfigureWeightsPrompt.levelOffendersBelow": { + "defaultMessage": "{count, plural, =1 {{name1}이(가) 0보다 낮습니다.} =2 {{name1} 및 {name2}이(가) 0보다 낮습니다.} other {{name1}, {name2} 및 그 외 {extra}명이 0보다 낮습니다.}}" + }, + "course.gradebook.ConfigureWeightsPrompt.levelFixAtMost": { + "defaultMessage": "이 기여도는 {max}(으)로 설정됩니다." + }, + "course.gradebook.ConfigureWeightsPrompt.levelFixAtLeast": { + "defaultMessage": "이 기여도는 0으로 설정됩니다." + }, + "course.gradebook.ConfigureWeightsPrompt.levelFixBetween": { + "defaultMessage": "0보다 낮은 기여도는 0으로 설정되고, {max}보다 높은 값은 {max}(으)로 설정됩니다." + }, + "course.gradebook.ConfigureWeightsPrompt.levelClampLabel": { + "defaultMessage": "기여도를 0에서 최대 레벨 기여도 사이로 유지" + }, + "course.gradebook.ConfigureWeightsPrompt.levelUnscoreable": { + "defaultMessage": "{count, plural, =1 {{name1}의 경우 수식이 0으로 나누기를 하므로, 해당 레벨 기여도가 0으로 설정됩니다.} =2 {{name1} 및 {name2}의 경우 수식이 0으로 나누기를 하므로, 해당 레벨 기여도가 0으로 설정됩니다.} other {{name1}, {name2} 및 그 외 {extra}명의 경우 수식이 0으로 나누기를 하므로, 해당 레벨 기여도가 0으로 설정됩니다.}}" + }, "course.gradebook.ConfigureWeightsPrompt.descriptionDrop": { "defaultMessage": "동일 모드에서는 평균을 내기 전에 각 학생의 점수가 가장 낮은 평가 N개를 선택적으로 제외할 수 있습니다." }, @@ -9404,6 +9464,33 @@ "course.gradebook.WeightedGradebookTable.weightsDoNotSum": { "defaultMessage": "가중치 합계가 100이 아닙니다. 총점이 정확하지 않을 수 있습니다." }, + "course.gradebook.WeightedGradebookTable.levelOverBudgetAboveOnly": { + "defaultMessage": "일부 레벨 기여도가 레벨 가중치를 초과하여 제한되었습니다." + }, + "course.gradebook.WeightedGradebookTable.levelOverBudgetBelowOnly": { + "defaultMessage": "일부 레벨 기여도가 0 미만이어서 0으로 올려졌습니다." + }, + "course.gradebook.WeightedGradebookTable.levelOverBudgetBoth": { + "defaultMessage": "일부 레벨 기여도가 유효 범위를 벗어났습니다 — 0 미만이거나 레벨 가중치를 초과합니다." + }, + "course.gradebook.WeightedGradebookTable.levelCellCappedAbove": { + "defaultMessage": "수식 결과가 {raw}이므로 {weight}(으)로 설정되었습니다." + }, + "course.gradebook.WeightedGradebookTable.levelCellCappedBelow": { + "defaultMessage": "수식 결과가 {raw}이므로 0으로 설정되었습니다." + }, + "course.gradebook.WeightedGradebookTable.levelCellDivByZero": { + "defaultMessage": "이 레벨에서 수식이 0으로 나누기를 하므로 0으로 설정되었습니다." + }, + "course.gradebook.WeightedGradebookTable.levelHeader": { + "defaultMessage": "레벨" + }, + "course.gradebook.WeightedGradebookTable.levelContributionHeader": { + "defaultMessage": "레벨 기여도" + }, + "course.gradebook.WeightedGradebookTable.levelBreakdownDetail": { + "defaultMessage": "레벨 {level}" + }, "course.gradebook.TotalHint.policy": { "defaultMessage": "총점은 채점되지 않은 평가를 0점으로 계산합니다." }, diff --git a/client/locales/zh.json b/client/locales/zh.json index 970c2dca7a2..44c94729029 100644 --- a/client/locales/zh.json +++ b/client/locales/zh.json @@ -5390,6 +5390,9 @@ "course.gradebook.GradebookIndex.noStudents": { "defaultMessage": "无学生" }, + "course.gradebook.GradebookIndex.noStudentsHint": { + "defaultMessage": "学生加入课程后,成绩将显示在这里。" + }, "course.gradebook.GradebookTable.maxMarks": { "defaultMessage": "最高分" }, @@ -5399,6 +5402,9 @@ "course.gradebook.GradebookTable.noDataColumnsHintWithGamification": { "defaultMessage": "未选择任何成绩或游戏化列——导出内容将仅包含学生信息。" }, + "course.gradebook.GradeLinkHint.hint": { + "defaultMessage": "每个成绩都是学生提交内容中各项分数的总和。点击任一成绩即可打开该提交内容并调整分数。" + }, "course.group.GroupCreationForm.description": { "defaultMessage": "说明(可选)" }, @@ -9272,6 +9278,63 @@ "course.gradebook.ConfigureWeightsPrompt.defaultsHint": { "defaultMessage": "尚未设置权重。以下为建议默认值,所有标签页均按相同比重计算。保存以确认,或在下方进行调整。" }, + "course.gradebook.ConfigureWeightsPrompt.levelTitle": { + "defaultMessage": "等级贡献值" + }, + "course.gradebook.ConfigureWeightsPrompt.levelSubtitle": { + "defaultMessage": "在标签页贡献值之外,根据每位学生的等级添加成绩点数。" + }, + "course.gradebook.ConfigureWeightsPrompt.levelHighestStudent": { + "defaultMessage": "最高学生等级:{level}" + }, + "course.gradebook.ConfigureWeightsPrompt.levelCourseMax": { + "defaultMessage": "课程最高等级:{courseMaxLevel}" + }, + "course.gradebook.ConfigureWeightsPrompt.levelFormulaLabel": { + "defaultMessage": "公式" + }, + "course.gradebook.ConfigureWeightsPrompt.levelFormulaHelper": { + "defaultMessage": "公式可以包含数字、算术运算符(+ − * /)、括号、作为变量的 \"level\",以及函数 floor、ceil、round、min 和 max。" + }, + "course.gradebook.ConfigureWeightsPrompt.levelPreview": { + "defaultMessage": "最高等级({maxLevel})时:{pts} 分" + }, + "course.gradebook.ConfigureWeightsPrompt.levelCustomMaxLabel": { + "defaultMessage": "使用自定义最高等级" + }, + "course.gradebook.ConfigureWeightsPrompt.levelMaxLevelLabel": { + "defaultMessage": "最高等级" + }, + "course.gradebook.ConfigureWeightsPrompt.levelMaxLevelInvalid": { + "defaultMessage": "最高等级必须至少为 1" + }, + "course.gradebook.ConfigureWeightsPrompt.levelShowLabel": { + "defaultMessage": "在表格中显示等级列" + }, + "course.gradebook.ConfigureWeightsPrompt.levelOffendersAbove": { + "defaultMessage": "{count, plural, =1 {{name1} 高于 {max}。} =2 {{name1} 和 {name2} 高于 {max}。} other {{name1}、{name2} 以及另外 {extra} 人高于 {max}。}}" + }, + "course.gradebook.ConfigureWeightsPrompt.levelOffendersBelow": { + "defaultMessage": "{count, plural, =1 {{name1} 低于 0。} =2 {{name1} 和 {name2} 低于 0。} other {{name1}、{name2} 以及另外 {extra} 人低于 0。}}" + }, + "course.gradebook.ConfigureWeightsPrompt.levelFixAtMost": { + "defaultMessage": "这些贡献值将被设置为 {max}。" + }, + "course.gradebook.ConfigureWeightsPrompt.levelFixAtLeast": { + "defaultMessage": "这些贡献值将被设置为 0。" + }, + "course.gradebook.ConfigureWeightsPrompt.levelFixBetween": { + "defaultMessage": "低于 0 的贡献值将被设置为 0,高于 {max} 的值将被设置为 {max}。" + }, + "course.gradebook.ConfigureWeightsPrompt.levelClampLabel": { + "defaultMessage": "将贡献值保持在 0 到最高等级贡献值之间" + }, + "course.gradebook.ConfigureWeightsPrompt.levelUnscoreable": { + "defaultMessage": "{count, plural, =1 {公式对 {name1} 进行了除以零运算,因此其等级贡献值被设为 0。} =2 {公式对 {name1} 和 {name2} 进行了除以零运算,因此他们的等级贡献值被设为 0。} other {公式对 {name1}、{name2} 以及另外 {extra} 人进行了除以零运算,因此他们的等级贡献值被设为 0。}}" + }, + "course.gradebook.WeightedGradebookTable.levelCellDivByZero": { + "defaultMessage": "由于该等级的公式进行了除以零运算,因此设置为 0。" + }, "course.gradebook.ConfigureWeightsPrompt.descriptionDrop": { "defaultMessage": "在等权模式下,可选择在求平均分前剔除每位学生得分最低的 N 个评估。" }, @@ -9398,6 +9461,30 @@ "course.gradebook.WeightedGradebookTable.weightsDoNotSum": { "defaultMessage": "权重合计不为 100。总成绩可能不准确。" }, + "course.gradebook.WeightedGradebookTable.levelOverBudgetAboveOnly": { + "defaultMessage": "部分等级贡献值超出等级权重上限,已被限制。" + }, + "course.gradebook.WeightedGradebookTable.levelOverBudgetBelowOnly": { + "defaultMessage": "部分等级贡献值低于 0,已被提升至 0。" + }, + "course.gradebook.WeightedGradebookTable.levelOverBudgetBoth": { + "defaultMessage": "部分等级贡献值超出有效范围 — 低于 0 或超出等级权重上限。" + }, + "course.gradebook.WeightedGradebookTable.levelCellCappedAbove": { + "defaultMessage": "由于公式产生了 {raw},因此设置为 {weight}。" + }, + "course.gradebook.WeightedGradebookTable.levelCellCappedBelow": { + "defaultMessage": "由于公式产生了 {raw},因此设置为 0。" + }, + "course.gradebook.WeightedGradebookTable.levelHeader": { + "defaultMessage": "等级" + }, + "course.gradebook.WeightedGradebookTable.levelContributionHeader": { + "defaultMessage": "等级贡献值" + }, + "course.gradebook.WeightedGradebookTable.levelBreakdownDetail": { + "defaultMessage": "等级 {level}" + }, "course.gradebook.TotalHint.policy": { "defaultMessage": "总成绩将未评分的评估按 0 分计算。" }, diff --git a/db/migrate/20260616000000_create_gradebook_level_configs.rb b/db/migrate/20260616000000_create_gradebook_level_configs.rb new file mode 100644 index 00000000000..671c2fb904f --- /dev/null +++ b/db/migrate/20260616000000_create_gradebook_level_configs.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true +class CreateGradebookLevelConfigs < ActiveRecord::Migration[7.2] + def change + create_table :course_gradebook_level_configs do |t| + t.references :course, null: false, + foreign_key: { to_table: :courses, on_delete: :cascade }, + index: { + unique: true, + name: 'index_course_gradebook_level_configs_on_course_id' + } + + t.boolean :enabled, null: false, default: false + t.string :formula, null: false, default: '' + t.jsonb :formula_ast + t.decimal :weight, precision: 5, scale: 2, null: false, default: 0 + t.boolean :show, null: false, default: false + t.boolean :clamp, null: false, default: true + + t.references :creator, null: false, + foreign_key: { to_table: :users }, + index: { name: 'fk__course_gradebook_level_configs_creator_id' } + + t.references :updater, null: false, + foreign_key: { to_table: :users }, + index: { name: 'fk__course_gradebook_level_configs_updater_id' } + + t.timestamps null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 2a1fc7a65f9..9bb292b31b7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2026_06_11_000000) do +ActiveRecord::Schema[7.2].define(version: 2026_06_16_000000) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" enable_extension "uuid-ossp" @@ -869,6 +869,23 @@ t.index ["updater_id"], name: "fk__cgac_updater_id" end + create_table "course_gradebook_level_configs", force: :cascade do |t| + t.bigint "course_id", null: false + t.boolean "enabled", default: false, null: false + t.string "formula", default: "", null: false + t.jsonb "formula_ast" + t.decimal "weight", precision: 5, scale: 2, default: "0.0", null: false + t.boolean "show", default: false, null: false + t.boolean "clamp", default: true, null: false + t.bigint "creator_id", null: false + t.bigint "updater_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["course_id"], name: "index_course_gradebook_level_configs_on_course_id", unique: true + t.index ["creator_id"], name: "fk__course_gradebook_level_configs_creator_id" + t.index ["updater_id"], name: "fk__course_gradebook_level_configs_updater_id" + end + create_table "course_gradebook_tab_contributions", force: :cascade do |t| t.bigint "course_id", null: false t.bigint "tab_id", null: false @@ -1946,6 +1963,9 @@ add_foreign_key "course_gradebook_assessment_contributions", "course_assessments", column: "assessment_id", on_delete: :cascade add_foreign_key "course_gradebook_assessment_contributions", "users", column: "creator_id" add_foreign_key "course_gradebook_assessment_contributions", "users", column: "updater_id" + add_foreign_key "course_gradebook_level_configs", "courses", on_delete: :cascade + add_foreign_key "course_gradebook_level_configs", "users", column: "creator_id" + add_foreign_key "course_gradebook_level_configs", "users", column: "updater_id" add_foreign_key "course_gradebook_tab_contributions", "course_assessment_tabs", column: "tab_id", on_delete: :cascade add_foreign_key "course_gradebook_tab_contributions", "courses" add_foreign_key "course_gradebook_tab_contributions", "users", column: "creator_id" diff --git a/spec/controllers/course/gradebook_controller_spec.rb b/spec/controllers/course/gradebook_controller_spec.rb index 2c1a5736648..7c7542b6597 100644 --- a/spec/controllers/course/gradebook_controller_spec.rb +++ b/spec/controllers/course/gradebook_controller_spec.rb @@ -54,6 +54,26 @@ expect(data).to have_key(key), "expected response to have key '#{key}'" end end + + it 'serializes courseMaxLevel and a default levelContribution' do + subject + data = JSON.parse(response.body) + expect(data).to have_key('courseMaxLevel') + expect(data['courseMaxLevel']).to be_a(Integer) + expect(data['levelContribution']).to include( + 'enabled' => false, 'formula' => '', 'weight' => 0, 'show' => false + ) + expect(data['levelContribution']).to include('clamp' => true) + end + + it 'includes levelContribution (null) on each student when level contribution disabled' do + subject + data = JSON.parse(response.body) + data['students'].each do |s| + expect(s).to have_key('levelContribution') + expect(s['levelContribution']).to be_nil + end + end end context 'when an observer visits the page' do @@ -129,6 +149,113 @@ end end + context 'with weighted view enabled and a level config with formula_ast' do + let(:manager) { create(:course_manager, course: course) } + let!(:student_cu) { create(:course_student, course: course) } + + before do + # Enable weighted view via the persisted course component setting, so the + # controller's @settings.weighted_view_enabled reads true on the real request. + gradebook_settings = Course::Settings::GradebookComponent.new( + OpenStruct.new(current_course: course, key: Course::GradebookComponent.key) + ) + gradebook_settings.weighted_view_enabled = true + course.save! + controller_sign_in(controller, manager.user) + # Create a level config with a known AST: formula = 'level * 0.5' + ast = { + 'type' => 'binop', 'op' => '*', + 'left' => { 'type' => 'var', 'name' => 'level' }, + 'right' => { 'type' => 'num', 'value' => 0.5 } + } + Course::Gradebook::LevelConfig.upsert_for( + course: course, + attrs: { enabled: true, formula: 'level * 0.5', formula_ast: ast, weight: 10, show: false } + ) + end + + it 'includes levelContribution on each student' do + get :index, params: { course_id: course.id }, format: :json + data = JSON.parse(response.body) + student_data = data['students'].find { |s| s['id'] == student_cu.user_id } + expect(student_data).not_to be_nil + expect(student_data).to have_key('levelContribution') + # level is 0 initially; 0 * 0.5 = 0.0 + expect(student_data['levelContribution'].to_f).to be_within(0.001).of(0.0) + end + end + + context 'with weighted view enabled, a level config, and a student above level 0' do + let(:manager) { create(:course_manager, course: course) } + let!(:level1) { create(:course_level, course: course, experience_points_threshold: 100) } + let!(:student_cu) { create(:course_student, course: course) } + let!(:exp_record) do + create(:course_experience_points_record, course_user: student_cu, points_awarded: 500) + end + + before do + gradebook_settings = Course::Settings::GradebookComponent.new( + OpenStruct.new(current_course: course, key: Course::GradebookComponent.key) + ) + gradebook_settings.weighted_view_enabled = true + course.save! + controller_sign_in(controller, manager.user) + # formula = 'level' → contribution equals the student's level_number. + Course::Gradebook::LevelConfig.upsert_for( + course: course, + attrs: { enabled: true, formula: 'level', + formula_ast: { 'type' => 'var', 'name' => 'level' }, + weight: 10, show: false } + ) + end + + it 'computes a non-null levelContribution from the student level_number' do + get :index, params: { course_id: course.id }, format: :json + data = JSON.parse(response.body) + student_data = data['students'].find { |s| s['id'] == student_cu.user_id } + expect(student_data).not_to be_nil + # 500 EXP places the student in level 1 (threshold 100); formula 'level' → 1.0. + expect(student_data['level']).to eq(1) + expect(student_data['levelContribution']).not_to be_nil + expect(student_data['levelContribution'].to_f).to be_within(0.001).of(1.0) + end + end + + context 'with a clamping level config and an out-of-range student' do + let(:manager) { create(:course_manager, course: course) } + let!(:level1) { create(:course_level, course: course, experience_points_threshold: 100) } + let!(:student_cu) { create(:course_student, course: course) } + let!(:exp_record) do + create(:course_experience_points_record, course_user: student_cu, points_awarded: 500) + end + + before do + gradebook_settings = Course::Settings::GradebookComponent.new( + OpenStruct.new(current_course: course, key: Course::GradebookComponent.key) + ) + gradebook_settings.weighted_view_enabled = true + course.save! + controller_sign_in(controller, manager.user) + # formula 'level * 5', weight 3, clamp on: level 1 -> raw 5 -> clamped 3. + Course::Gradebook::LevelConfig.upsert_for( + course: course, + attrs: { enabled: true, formula: 'level * 5', + formula_ast: { 'type' => 'binop', 'op' => '*', + 'left' => { 'type' => 'var', 'name' => 'level' }, + 'right' => { 'type' => 'num', 'value' => 5 } }, + weight: 3, show: false, clamp: true } + ) + end + + it 'serializes the clamped per-student contribution' do + get :index, params: { course_id: course.id }, format: :json + data = JSON.parse(response.body) + student_data = data['students'].find { |s| s['id'] == student_cu.user_id } + expect(student_data['levelContribution'].to_f).to be_within(0.001).of(3.0) + expect(data['levelContribution']).to include('clamp' => true) + end + end + context 'when a student has an external ID' do let!(:student) { create(:course_student, course: course, external_id: 'EXT-123') } before { controller_sign_in(controller, manager.user) } @@ -282,6 +409,68 @@ def weight_for(tab) expect(JSON.parse(response.body)['weights'].first['weight']).to eq(33.33) expect(weight_for(tab1)).to eq(33.33) end + + it 'persists an enabled levelContribution and echoes it back' do + patch :update_weights, params: { + course_id: course.id, + weights: [], + levelContribution: { + enabled: true, formula: 'min(level, 30) * 0.05', weight: 8, show: true + } + }, format: :json + expect(response).to have_http_status(:ok) + config = course.reload.gradebook_level_config + expect(config.enabled).to eq(true) + expect(config.formula).to eq('min(level, 30) * 0.05') + expect(config.weight).to eq(8) + echoed = JSON.parse(response.body)['levelContribution'] + expect(echoed).to include('enabled' => true, 'weight' => 8.0) + expect(echoed).not_to have_key('maxLevel') + end + + it 'persists the clamp flag sent in the levelContribution payload' do + patch :update_weights, params: { + course_id: course.id, + weights: [], + levelContribution: { + enabled: true, formula: 'level', weight: 8, show: false, clamp: false + } + }, format: :json + expect(response).to have_http_status(:ok) + expect(course.reload.gradebook_level_config.clamp).to eq(false) + expect(JSON.parse(response.body)['levelContribution']).to include('clamp' => false) + end + + it 'rejects an enabled levelContribution with a blank formula (422)' do + patch :update_weights, params: { + course_id: course.id, weights: [], + levelContribution: { enabled: true, formula: '', weight: 8, show: false } + }, format: :json + expect(response).to have_http_status(:unprocessable_content) + expect(course.reload.gradebook_level_config).to be_nil + end + + it 'leaves the config untouched when no levelContribution is sent' do + patch :update_weights, params: { course_id: course.id, **valid_payload }, format: :json + expect(response).to have_http_status(:ok) + expect(JSON.parse(response.body)).not_to have_key('levelContribution') + expect(course.reload.gradebook_level_config).to be_nil + end + + it 'rejects a tampered formulaAst with 422' do + patch :update_weights, params: { + course_id: course.id, + weights: [], + levelContribution: { + enabled: true, + formula: 'level', + formulaAst: { type: 'evil', payload: 'x' }, + weight: 5, + show: false + } + }, format: :json + expect(response).to have_http_status(:unprocessable_content) + end end context 'as TA' do diff --git a/spec/factories/course_gradebook_level_configs.rb b/spec/factories/course_gradebook_level_configs.rb new file mode 100644 index 00000000000..e6637cad7e7 --- /dev/null +++ b/spec/factories/course_gradebook_level_configs.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true +FactoryBot.define do + factory :course_gradebook_level_config, class: Course::Gradebook::LevelConfig.name do + course + enabled { false } + formula { '' } + weight { 0 } + show { false } + clamp { true } + end +end diff --git a/spec/models/course/gradebook/level_config_spec.rb b/spec/models/course/gradebook/level_config_spec.rb new file mode 100644 index 00000000000..98bd7986114 --- /dev/null +++ b/spec/models/course/gradebook/level_config_spec.rb @@ -0,0 +1,336 @@ +# frozen_string_literal: true +require 'rails_helper' + +RSpec.describe Course::Gradebook::LevelConfig do + let!(:instance) { Instance.default } + with_tenant(:instance) do + let(:course) { create(:course) } + + it { is_expected.to belong_to(:course) } + + describe 'validations' do + it 'is valid with defaults (disabled, blank formula)' do + expect(build(:course_gradebook_level_config, course: course)).to be_valid + end + + it 'requires a formula when enabled' do + config = build(:course_gradebook_level_config, course: course, enabled: true, formula: '') + expect(config).not_to be_valid + end + + it 'allows a blank formula when disabled' do + config = build(:course_gradebook_level_config, course: course, enabled: false, formula: '') + expect(config).to be_valid + end + + it 'rejects a negative weight' do + config = build(:course_gradebook_level_config, course: course, weight: -1) + expect(config).not_to be_valid + end + + it 'enforces one config per course' do + create(:course_gradebook_level_config, course: course) + expect(build(:course_gradebook_level_config, course: course)).not_to be_valid + end + end + + it 'is destroyed with its course' do + create(:course_gradebook_level_config, course: course) + expect { course.destroy! }.to change { described_class.count }.by(-1) + end + + describe '.upsert_for' do + it 'creates a config from a camelCase-free attrs hash' do + config = described_class.upsert_for( + course: course, + attrs: { enabled: true, formula: 'min(level, 30) * 0.05', weight: 8, show: true } + ) + expect(config.enabled).to eq(true) + expect(config.formula).to eq('min(level, 30) * 0.05') + expect(config.weight).to eq(8) + expect(config.show).to eq(true) + end + + it 'updates the existing singleton on a second call' do + described_class.upsert_for(course: course, attrs: { enabled: true, formula: 'level', weight: 2 }) + described_class.upsert_for(course: course, attrs: { enabled: false, formula: '', weight: 0 }) + expect(described_class.where(course_id: course.id).count).to eq(1) + expect(course.reload.gradebook_level_config.enabled).to eq(false) + end + + it 'raises RecordInvalid when enabled with a blank formula' do + expect do + described_class.upsert_for(course: course, attrs: { enabled: true, formula: '', weight: 1 }) + end.to raise_error(ActiveRecord::RecordInvalid) + end + end + + describe '.valid_ast?' do + it 'accepts a num node' do + expect(described_class.valid_ast?({ 'type' => 'num', 'value' => 3.5 })).to be true + end + + it 'accepts a var node for level' do + expect(described_class.valid_ast?({ 'type' => 'var', 'name' => 'level' })).to be true + end + + it 'rejects var with wrong name' do + expect(described_class.valid_ast?({ 'type' => 'var', 'name' => 'xp' })).to be false + end + + it 'accepts a neg node with valid operand' do + expect(described_class.valid_ast?({ 'type' => 'neg', + 'operand' => { 'type' => 'var', 'name' => 'level' } })).to be true + end + + it 'accepts a binop node with valid op and children' do + node = { + 'type' => 'binop', 'op' => '*', + 'left' => { 'type' => 'var', 'name' => 'level' }, + 'right' => { 'type' => 'num', 'value' => 0.5 } + } + expect(described_class.valid_ast?(node)).to be true + end + + it 'rejects binop with invalid op' do + node = { + 'type' => 'binop', 'op' => '%', + 'left' => { 'type' => 'num', 'value' => 1 }, + 'right' => { 'type' => 'num', 'value' => 1 } + } + expect(described_class.valid_ast?(node)).to be false + end + + it 'accepts a call1 node' do + node = { 'type' => 'call1', 'fn' => 'floor', 'arg' => { 'type' => 'var', 'name' => 'level' } } + expect(described_class.valid_ast?(node)).to be true + end + + it 'rejects call1 with unknown fn' do + node = { 'type' => 'call1', 'fn' => 'sqrt', 'arg' => { 'type' => 'var', 'name' => 'level' } } + expect(described_class.valid_ast?(node)).to be false + end + + it 'accepts a call2 node' do + node = { + 'type' => 'call2', 'fn' => 'min', + 'a' => { 'type' => 'var', 'name' => 'level' }, + 'b' => { 'type' => 'num', 'value' => 20 } + } + expect(described_class.valid_ast?(node)).to be true + end + + it 'rejects call2 with unknown fn' do + node = { + 'type' => 'call2', 'fn' => 'pow', + 'a' => { 'type' => 'num', 'value' => 2 }, + 'b' => { 'type' => 'num', 'value' => 3 } + } + expect(described_class.valid_ast?(node)).to be false + end + + it 'rejects an unknown type' do + expect(described_class.valid_ast?({ 'type' => 'evil', 'payload' => 'x' })).to be false + end + + it 'rejects a non-hash' do + expect(described_class.valid_ast?('not a hash')).to be false + expect(described_class.valid_ast?(nil)).to be false + end + + it 'rejects a tree exceeding depth 20' do + # Build a deeply-nested neg chain: neg(neg(neg(...(var)...))) with 21 levels + deep = { 'type' => 'var', 'name' => 'level' } + 21.times { deep = { 'type' => 'neg', 'operand' => deep } } + expect(described_class.valid_ast?(deep)).to be false + end + + it 'rejects a node with a missing required key' do + expect(described_class.valid_ast?({ 'type' => 'binop', 'op' => '+', + 'left' => { 'type' => 'num', 'value' => 1 } })).to be false + end + end + + describe '#evaluate_for' do + # These examples verify raw AST evaluation, independent of clamping, so they + # disable clamp (the factory weight is 0, which would otherwise clamp every + # result to 0). Clamping itself is covered in '#evaluate_for clamping' below. + let(:config) do + build(:course_gradebook_level_config, course: course, enabled: true, clamp: false) + end + + it 'evaluates a num node (constant formula)' do + config.formula_ast = { 'type' => 'num', 'value' => 5.0 } + expect(config.evaluate_for(level: 10)).to eq(5.0) + end + + it 'evaluates a var node (returns the level)' do + config.formula_ast = { 'type' => 'var', 'name' => 'level' } + expect(config.evaluate_for(level: 15)).to eq(15.0) + end + + it 'evaluates a binop multiply: level * 0.5' do + config.formula_ast = { + 'type' => 'binop', 'op' => '*', + 'left' => { 'type' => 'var', 'name' => 'level' }, + 'right' => { 'type' => 'num', 'value' => 0.5 } + } + expect(config.evaluate_for(level: 20)).to be_within(0.001).of(10.0) + end + + it 'evaluates a call2 min: min(level, 20) * 0.5' do + config.formula_ast = { + 'type' => 'binop', 'op' => '*', + 'left' => { + 'type' => 'call2', 'fn' => 'min', + 'a' => { 'type' => 'var', 'name' => 'level' }, + 'b' => { 'type' => 'num', 'value' => 20 } + }, + 'right' => { 'type' => 'num', 'value' => 0.5 } + } + expect(config.evaluate_for(level: 15)).to be_within(0.001).of(7.5) + expect(config.evaluate_for(level: 25)).to be_within(0.001).of(10.0) + end + + it 'evaluates floor: floor(level / 5)' do + config.formula_ast = { + 'type' => 'call1', 'fn' => 'floor', + 'arg' => { + 'type' => 'binop', 'op' => '/', + 'left' => { 'type' => 'var', 'name' => 'level' }, + 'right' => { 'type' => 'num', 'value' => 5 } + } + } + expect(config.evaluate_for(level: 12)).to eq(2.0) # floor(12/5) = 2 + end + + it 'returns nil when the divisor is zero' do + config.formula_ast = { + 'type' => 'binop', 'op' => '/', + 'left' => { 'type' => 'var', 'name' => 'level' }, + 'right' => { 'type' => 'num', 'value' => 0 } + } + expect(config.evaluate_for(level: 5)).to be_nil + end + + it 'returns nil only for the student whose level zeroes the divisor' do + # 100 / level: undefined at level 0, defined elsewhere + config.formula_ast = { + 'type' => 'binop', 'op' => '/', + 'left' => { 'type' => 'num', 'value' => 100 }, + 'right' => { 'type' => 'var', 'name' => 'level' } + } + expect(config.evaluate_for(level: 0)).to be_nil + expect(config.evaluate_for(level: 5)).to eq(20.0) + end + + it 'returns nil when disabled' do + config.enabled = false + config.formula_ast = { 'type' => 'var', 'name' => 'level' } + expect(config.evaluate_for(level: 10)).to be_nil + end + + it 'returns nil when formula_ast is nil' do + config.formula_ast = nil + expect(config.evaluate_for(level: 10)).to be_nil + end + end + + describe '.upsert_for — formula_ast' do + let(:valid_ast) do + { + 'type' => 'binop', 'op' => '*', + 'left' => { 'type' => 'var', 'name' => 'level' }, + 'right' => { 'type' => 'num', 'value' => 0.5 } + } + end + + it 'stores a valid formula_ast' do + config = described_class.upsert_for( + course: course, + attrs: { enabled: true, formula: 'level * 0.5', formula_ast: valid_ast, weight: 10 } + ) + expect(config.formula_ast).to eq(valid_ast) + end + + it 'stores nil formula_ast when not provided' do + config = described_class.upsert_for( + course: course, + attrs: { enabled: true, formula: 'level', weight: 5 } + ) + expect(config.formula_ast).to be_nil + end + + it 'raises RecordInvalid for a malformed formula_ast' do + expect do + described_class.upsert_for( + course: course, + attrs: { enabled: true, formula: 'level', formula_ast: { 'type' => 'evil' }, weight: 5 } + ) + end.to raise_error(ActiveRecord::RecordInvalid) + end + end + describe '#evaluate_for clamping' do + # formula 'level * 5' + let(:times5) do + { 'type' => 'binop', 'op' => '*', + 'left' => { 'type' => 'var', 'name' => 'level' }, + 'right' => { 'type' => 'num', 'value' => 5 } } + end + # formula 'level - 5' + let(:minus5) do + { 'type' => 'binop', 'op' => '-', + 'left' => { 'type' => 'var', 'name' => 'level' }, + 'right' => { 'type' => 'num', 'value' => 5 } } + end + + it 'caps a value above the weight to the weight when clamp is on' do + config = build(:course_gradebook_level_config, course: course, enabled: true, + formula: 'level * 5', formula_ast: times5, + weight: 3, clamp: true) + expect(config.evaluate_for(level: 2)).to eq(3.0) # raw 10 -> 3 + end + + it 'floors a negative value to 0 when clamp is on' do + config = build(:course_gradebook_level_config, course: course, enabled: true, + formula: 'level - 5', formula_ast: minus5, + weight: 10, clamp: true) + expect(config.evaluate_for(level: 1)).to eq(0.0) # raw -4 -> 0 + end + + it 'leaves an in-range value unchanged when clamp is on' do + config = build(:course_gradebook_level_config, course: course, enabled: true, + formula: 'level * 5', formula_ast: times5, + weight: 100, clamp: true) + expect(config.evaluate_for(level: 1)).to eq(5.0) + end + + it 'returns the raw value when clamp is off' do + config = build(:course_gradebook_level_config, course: course, enabled: true, + formula: 'level * 5', formula_ast: times5, + weight: 3, clamp: false) + expect(config.evaluate_for(level: 2)).to eq(10.0) + end + end + + describe '.upsert_for clamp' do + it 'persists clamp false' do + described_class.upsert_for( + course: course, + attrs: { enabled: true, formula: 'level', + formula_ast: { 'type' => 'var', 'name' => 'level' }, + weight: 10, show: false, clamp: false } + ) + expect(course.reload.gradebook_level_config.clamp).to be(false) + end + + it 'defaults clamp to true when the key is omitted' do + described_class.upsert_for( + course: course, + attrs: { enabled: false, formula: '', formula_ast: nil, weight: 0, show: false } + ) + expect(course.reload.gradebook_level_config.clamp).to be(true) + end + end + end +end