return grading period info when totals are hidden

`current_grading_period_id`, `current_grading_period_title`,
`has_grading_periods`, `multiple_grading_periods_enabled` are now
returned even when the course has turned on hiding the total score.

This information is returned from `api/v1/courses/:course_id` when
the include[] param includes `current_grading_period_scores` and
`total_scores`.

closes GRADE-1635

Test Plan
- Set up a grading period that the course falls in.
- In the course settings, turn on the setting
  "Hide totals in student grades summary"
- Send a GET request to
  `api/v1/courses/:course_id?
  include[]=current_grading_period_scores&include[]=total_scores`

- Verify that in the json response, the enrollments array contains
  objects with the four keys mentioned above, and that
  "hide_final_grades" is true.

Change-Id: Ic8dbbd7da3bef033f61b16289813e6b013f2e0e4
Reviewed-on: https://gerrit.instructure.com/170325
Reviewed-by: Jeremy Neander <jneander@instructure.com>
Reviewed-by: Adrian Packel <apackel@instructure.com>
Tested-by: Jenkins
QA-Review: Gary Mei <gmei@instructure.com>
Product-Review: Keith Garner <kgarner@instructure.com>
This commit is contained in:
Gary Mei 2018-10-17 13:13:15 -05:00
parent 6592f3cd72
commit 150b6c2913
4 changed files with 116 additions and 17 deletions

View File

@ -409,8 +409,18 @@ class CoursesController < ApplicationController
# 'current_period_unposted_final_grade' (see Enrollment documentation for # 'current_period_unposted_final_grade' (see Enrollment documentation for
# more information on these fields). In addition, when this argument is # more information on these fields). In addition, when this argument is
# passed, the course will have a 'has_grading_periods' attribute # passed, the course will have a 'has_grading_periods' attribute
# on it. This argument is ignored if the course is configured to hide final # on it. This argument is ignored if the total_scores argument is not
# grades or if the total_scores argument is not included. # included. If the course is configured to hide final grades, the
# following fields are not returned:
# 'totals_for_all_grading_periods_option',
# 'current_period_computed_current_score',
# 'current_period_computed_final_score',
# 'current_period_computed_current_grade',
# 'current_period_computed_final_grade',
# 'current_period_unposted_current_score',
# 'current_period_unposted_final_score',
# 'current_period_unposted_current_grade', and
# 'current_period_unposted_final_grade'
# - "term": Optional information to include with each Course. When # - "term": Optional information to include with each Course. When
# term is given, the information for the enrollment term for each course # term is given, the information for the enrollment term for each course
# is returned. # is returned.

View File

@ -142,6 +142,7 @@ module Api::V1
def enrollment_hash(enrollment) def enrollment_hash(enrollment)
enrollment_hash = default_enrollment_attributes(enrollment) enrollment_hash = default_enrollment_attributes(enrollment)
enrollment_hash[:associated_user_id] = enrollment.associated_user_id if enrollment.assigned_observer? enrollment_hash[:associated_user_id] = enrollment.associated_user_id if enrollment.assigned_observer?
enrollment_hash.merge!(grading_period_info) if include_grading_period_info? && enrollment.student?
enrollment_hash.merge!(total_scores(enrollment)) if include_total_scores? && enrollment.student? enrollment_hash.merge!(total_scores(enrollment)) if include_total_scores? && enrollment.student?
enrollment_hash enrollment_hash
end end
@ -177,13 +178,18 @@ module Api::V1
scores scores
end end
def grading_period_info
{
current_grading_period_id: current_grading_period&.id,
current_grading_period_title: current_grading_period&.title,
has_grading_periods: @course.grading_periods?,
multiple_grading_periods_enabled: @course.grading_periods? # for backwards compatibility
}
end
def current_grading_period_scores(student_enrollment) def current_grading_period_scores(student_enrollment)
scores = { scores = {
has_grading_periods: @course.grading_periods?,
multiple_grading_periods_enabled: @course.grading_periods?, # for backwards compatibility
totals_for_all_grading_periods_option: @course.display_totals_for_all_grading_periods?, totals_for_all_grading_periods_option: @course.display_totals_for_all_grading_periods?,
current_grading_period_title: current_grading_period&.title,
current_grading_period_id: current_grading_period&.id,
current_period_computed_current_score: grading_period_score(student_enrollment, :current), current_period_computed_current_score: grading_period_score(student_enrollment, :current),
current_period_computed_final_score: grading_period_score(student_enrollment, :final), current_period_computed_final_score: grading_period_score(student_enrollment, :final),
current_period_computed_current_grade: grading_period_grade(student_enrollment, :current), current_period_computed_current_grade: grading_period_grade(student_enrollment, :current),
@ -233,5 +239,11 @@ module Api::V1
@include_current_grading_period_scores = @include_current_grading_period_scores =
include_total_scores? && @includes.include?(:current_grading_period_scores) include_total_scores? && @includes.include?(:current_grading_period_scores)
end end
def include_grading_period_info?
return @include_grading_period_info unless @include_grading_period_info.nil?
@include_grading_period_info =
@includes.include?(:current_grading_period_scores) && @includes.include?(:total_scores)
end
end end
end end

View File

@ -23,10 +23,47 @@ module Api
module V1 module V1
describe CourseJson do describe CourseJson do
let(:course) { ::Course.create! }
let(:course_json) { CourseJson.new(course, nil, includes, []) }
let(:includes) { [] } let(:includes) { [] }
let(:course) { double(:course) }
let(:user) { double(:user) } let(:user) { double(:user) }
let(:course_json) { CourseJson.new( course, nil, includes, [] ) }
describe "#to_hash" do
let(:student) { course_with_user("StudentEnrollment", course: course, name: "Student", active_all: true).user }
before(:each) do
grading_period_group = course.grading_period_groups.create!
grading_period_group.grading_periods.create!(
title: "gp1",
start_date: 1.day.ago,
end_date: 2.days.from_now,
close_date: 3.days.from_now
)
end
it "contains information for the grading period even when final grades are hidden" do
course.update!(hide_final_grades: true)
includes = [:current_grading_period_scores, :total_scores]
enrollments = Api::V1::CourseJson.to_hash(course, student, includes, course.enrollments).fetch("enrollments")
expect(enrollments.first.keys).to include(
:current_grading_period_id,
:current_grading_period_title,
:has_grading_periods,
:multiple_grading_periods_enabled
)
end
it "does not contain information for the period when total scores and period scores are not included" do
includes = []
enrollments = Api::V1::CourseJson.to_hash(course, student, includes, course.enrollments).fetch("enrollments")
expect(enrollments.first.keys).not_to include(
:current_grading_period_id,
:current_grading_period_title,
:has_grading_periods,
:multiple_grading_periods_enabled
)
end
end
describe '#include_description' do describe '#include_description' do
let(:predicate){ course_json.include_description } let(:predicate){ course_json.include_description }

View File

@ -1910,17 +1910,57 @@ describe CoursesController, type: :request do
end end
end end
context "include current grading period scores" do context "grading period info" do
let(:grading_period_keys) do let(:grading_period_info_keys) do
[ 'multiple_grading_periods_enabled', [
'current_grading_period_id',
'current_grading_period_title',
'has_grading_periods', 'has_grading_periods',
'multiple_grading_periods_enabled'
]
end
before(:once) do
create_grading_periods_for(
@course2, grading_periods: [:old, :current, :future]
)
end
it "includes the grading period info if total_scores and current_grading_period_scores are included" do
json_response = courses_api_index_call(includes: ['total_scores', 'current_grading_period_scores'])
enrollment_json = enrollment(json_response)
expect(enrollment_json).to include(*grading_period_info_keys)
end
it "includes grading period info even if final grades are hidden" do
@course2.update!(hide_final_grades: true)
json_response = courses_api_index_call(includes: ['current_grading_period_scores', 'total_scores'])
enrollment_json = enrollment(json_response)
expect(enrollment_json).to include(*grading_period_info_keys)
end
it "does not include grading period info if total_scores but not current_grading_period_scores are included" do
json_response = courses_api_index_call(includes: ['total_scores'])
enrollment_json = enrollment(json_response)
expect(enrollment_json).not_to include(*grading_period_info_keys)
end
it "does not include grading period info if current_grading_period_scores but not total_scores are included" do
json_response = courses_api_index_call(includes: ['current_grading_period_scores'])
enrollment_json = enrollment(json_response)
expect(enrollment_json).not_to include(*grading_period_info_keys)
end
end
context "include current grading period scores" do
let(:grading_period_score_keys) do
[
'totals_for_all_grading_periods_option', 'totals_for_all_grading_periods_option',
'current_period_computed_current_score', 'current_period_computed_current_score',
'current_period_computed_final_score', 'current_period_computed_final_score',
'current_period_computed_current_grade', 'current_period_computed_current_grade',
'current_period_computed_final_grade', 'current_period_computed_final_grade'
'current_grading_period_title', ]
'current_grading_period_id' ]
end end
before(:once) do before(:once) do
@ -1933,7 +1973,7 @@ describe CoursesController, type: :request do
"and 'current_grading_period_scores' are requested" do "and 'current_grading_period_scores' are requested" do
json_response = courses_api_index_call(includes: ['total_scores', 'current_grading_period_scores']) json_response = courses_api_index_call(includes: ['total_scores', 'current_grading_period_scores'])
enrollment_json = enrollment(json_response) enrollment_json = enrollment(json_response)
expect(enrollment_json).to include(*grading_period_keys) expect(enrollment_json).to include(*grading_period_score_keys)
current_grading_period_title = 'Course Period 2: current period' current_grading_period_title = 'Course Period 2: current period'
expect(enrollment_json['current_grading_period_title']).to eq(current_grading_period_title) expect(enrollment_json['current_grading_period_title']).to eq(current_grading_period_title)
end end
@ -1949,7 +1989,7 @@ describe CoursesController, type: :request do
"not requested, even if 'current_grading_period_scores' are requested" do "not requested, even if 'current_grading_period_scores' are requested" do
json_response = courses_api_index_call(includes: ['current_grading_period_scores']) json_response = courses_api_index_call(includes: ['current_grading_period_scores'])
enrollment_json = enrollment(json_response) enrollment_json = enrollment(json_response)
expect(enrollment_json).to_not include(*grading_period_keys) expect(enrollment_json).to_not include(*grading_period_score_keys)
end end
it "does not include current grading period scores if final grades are hidden, " \ it "does not include current grading period scores if final grades are hidden, " \
@ -1958,7 +1998,7 @@ describe CoursesController, type: :request do
@course2.save @course2.save
json_response = courses_api_index_call(includes: ['total_scores', 'current_grading_period_scores']) json_response = courses_api_index_call(includes: ['total_scores', 'current_grading_period_scores'])
enrollment_json = enrollment(json_response) enrollment_json = enrollment(json_response)
expect(enrollment_json).not_to include(*grading_period_keys) expect(enrollment_json).not_to include(*grading_period_score_keys)
end end
it "returns true for 'has_grading_periods' on the enrollment " \ it "returns true for 'has_grading_periods' on the enrollment " \