canvas-lms/lib/dates_overridable.rb

220 lines
6.2 KiB
Ruby
Raw Normal View History

module DatesOverridable
attr_accessor :applied_overrides, :overridden_for_user, :overridden
make observers viewing discussions vdd lock date aware fixes CNVS-518 also included: - observers now get visible students' section overrides in their AssignmentOverride.visible_to scope - fixed a bug where calling some DatesOverridable methods would use an overridden date where you wouldn't expect (see the specs) - added a method to get the original object from an overridden one - made DiscussionTopicPresenter handle due dates overridden to nil test plan notes - keep an eye out for regressions in displayed due dates - discussion locking behavior should be as follows -- viewing the discussion page before the earliest applicable unlock date should show a locked discussion page that lists the earliest unlock date -- viewing the discussion after the earliest unlock date should show the discussion -- the discussion page should show the due date when only one due date applies -- the discussion page should show a "multiple due dates" ui when more than one due date applies -- the "multiple due dates" ui should only display entries for sections that the observer's linked students are in -- the "multiple due dates" ui should display lock dates from the associated section override or from the original assignment if the section override does not override lock dates test plan - check an observer not observing student -- ensure that the discussion locking behavior behaves according to the observer's section's override - check an observer observing one student -- ensure that the discussion locking behaves according to the student's section's override - check an observer observing multiple student's in more than one section -- make overrides for each student's section that differ in due dates and lock dates -- ensure that the discussion locking behaves according to the students' sections' combined lock dates -- ensure that the discussion page shows a "multiple due dates" ui when the discussion is unlocked Change-Id: I8f2970f0962cdc60cf9a423f01a876bf0ae909d4 Reviewed-on: https://gerrit.instructure.com/17452 Reviewed-by: Simon Williams <simon@instructure.com> Tested-by: Jenkins <jenkins@instructure.com> QA-Review: Marc LeGendre <marc@instructure.com> Reviewed-by: Mark Ericksen <marke@instructure.com>
2013-02-05 08:51:39 +08:00
attr_writer :without_overrides
def self.included(base)
base.has_many :assignment_overrides, :dependent => :destroy
base.has_many :active_assignment_overrides, :class_name => 'AssignmentOverride', :conditions => {:workflow_state => 'active'}
base.has_many :assignment_override_students, :dependent => :destroy
base.validates_associated :assignment_overrides
base.extend(ClassMethods)
end
def overridden_for(user)
AssignmentOverrideApplicator.assignment_overridden_for(self, user)
end
def overrides_visible_to(user, overrides=active_assignment_overrides)
# the visible_to scope is potentially expensive. skip its conditions if the
# initial scope is already empty
if overrides.first.present?
overrides.visible_to(user, context)
else
overrides
end
end
def has_overrides?
assignment_overrides.count > 0
end
make observers viewing discussions vdd lock date aware fixes CNVS-518 also included: - observers now get visible students' section overrides in their AssignmentOverride.visible_to scope - fixed a bug where calling some DatesOverridable methods would use an overridden date where you wouldn't expect (see the specs) - added a method to get the original object from an overridden one - made DiscussionTopicPresenter handle due dates overridden to nil test plan notes - keep an eye out for regressions in displayed due dates - discussion locking behavior should be as follows -- viewing the discussion page before the earliest applicable unlock date should show a locked discussion page that lists the earliest unlock date -- viewing the discussion after the earliest unlock date should show the discussion -- the discussion page should show the due date when only one due date applies -- the discussion page should show a "multiple due dates" ui when more than one due date applies -- the "multiple due dates" ui should only display entries for sections that the observer's linked students are in -- the "multiple due dates" ui should display lock dates from the associated section override or from the original assignment if the section override does not override lock dates test plan - check an observer not observing student -- ensure that the discussion locking behavior behaves according to the observer's section's override - check an observer observing one student -- ensure that the discussion locking behaves according to the student's section's override - check an observer observing multiple student's in more than one section -- make overrides for each student's section that differ in due dates and lock dates -- ensure that the discussion locking behaves according to the students' sections' combined lock dates -- ensure that the discussion page shows a "multiple due dates" ui when the discussion is unlocked Change-Id: I8f2970f0962cdc60cf9a423f01a876bf0ae909d4 Reviewed-on: https://gerrit.instructure.com/17452 Reviewed-by: Simon Williams <simon@instructure.com> Tested-by: Jenkins <jenkins@instructure.com> QA-Review: Marc LeGendre <marc@instructure.com> Reviewed-by: Mark Ericksen <marke@instructure.com>
2013-02-05 08:51:39 +08:00
def without_overrides
@without_overrides || self
end
# returns two values indicating which due dates for this assignment apply
# and/or are visible to the user.
#
# the first is the due date as it applies to the user as a student, if any
# (nil if the user has no student enrollment(s) in the assignment's course)
#
# the second is a list of due dates a they apply to users, sections, or
# groups visible to the user as an admin (nil if the user has no
# admin/observer enrollment(s) in the assignment's course)
#
# in both cases, "due dates" is a hash with due_at (full timestamp), all_day
# flag, and all_day_date. for the "as an admin" list, each due date from
# an override will also have a 'title' key to identify which subset of the
# course is affected by that due date, and an 'override' key referencing the
# override itself. for the original due date, it will instead have a 'base'
# flag (value true).
def due_dates_for(user)
as_student, as_admin = nil, nil
return nil, nil if context.nil?
if user.nil?
make observers viewing discussions vdd lock date aware fixes CNVS-518 also included: - observers now get visible students' section overrides in their AssignmentOverride.visible_to scope - fixed a bug where calling some DatesOverridable methods would use an overridden date where you wouldn't expect (see the specs) - added a method to get the original object from an overridden one - made DiscussionTopicPresenter handle due dates overridden to nil test plan notes - keep an eye out for regressions in displayed due dates - discussion locking behavior should be as follows -- viewing the discussion page before the earliest applicable unlock date should show a locked discussion page that lists the earliest unlock date -- viewing the discussion after the earliest unlock date should show the discussion -- the discussion page should show the due date when only one due date applies -- the discussion page should show a "multiple due dates" ui when more than one due date applies -- the "multiple due dates" ui should only display entries for sections that the observer's linked students are in -- the "multiple due dates" ui should display lock dates from the associated section override or from the original assignment if the section override does not override lock dates test plan - check an observer not observing student -- ensure that the discussion locking behavior behaves according to the observer's section's override - check an observer observing one student -- ensure that the discussion locking behaves according to the student's section's override - check an observer observing multiple student's in more than one section -- make overrides for each student's section that differ in due dates and lock dates -- ensure that the discussion locking behaves according to the students' sections' combined lock dates -- ensure that the discussion page shows a "multiple due dates" ui when the discussion is unlocked Change-Id: I8f2970f0962cdc60cf9a423f01a876bf0ae909d4 Reviewed-on: https://gerrit.instructure.com/17452 Reviewed-by: Simon Williams <simon@instructure.com> Tested-by: Jenkins <jenkins@instructure.com> QA-Review: Marc LeGendre <marc@instructure.com> Reviewed-by: Mark Ericksen <marke@instructure.com>
2013-02-05 08:51:39 +08:00
return self.without_overrides.due_date_hash, nil
end
if context.user_has_been_student?(user)
as_student = self.overridden_for(user).due_date_hash
end
if context.user_has_been_admin?(user)
as_admin = due_dates_visible_to(user)
elsif context.user_has_been_observer?(user)
as_admin = observed_student_due_dates(user).uniq
if as_admin.empty?
as_admin = [self.overridden_for(user).due_date_hash]
end
elsif context.user_has_no_enrollments?(user)
as_admin = all_due_dates
end
return as_student, as_admin
end
def all_due_dates
all_dates = assignment_overrides.overriding_due_at.map(&:as_hash)
make observers viewing discussions vdd lock date aware fixes CNVS-518 also included: - observers now get visible students' section overrides in their AssignmentOverride.visible_to scope - fixed a bug where calling some DatesOverridable methods would use an overridden date where you wouldn't expect (see the specs) - added a method to get the original object from an overridden one - made DiscussionTopicPresenter handle due dates overridden to nil test plan notes - keep an eye out for regressions in displayed due dates - discussion locking behavior should be as follows -- viewing the discussion page before the earliest applicable unlock date should show a locked discussion page that lists the earliest unlock date -- viewing the discussion after the earliest unlock date should show the discussion -- the discussion page should show the due date when only one due date applies -- the discussion page should show a "multiple due dates" ui when more than one due date applies -- the "multiple due dates" ui should only display entries for sections that the observer's linked students are in -- the "multiple due dates" ui should display lock dates from the associated section override or from the original assignment if the section override does not override lock dates test plan - check an observer not observing student -- ensure that the discussion locking behavior behaves according to the observer's section's override - check an observer observing one student -- ensure that the discussion locking behaves according to the student's section's override - check an observer observing multiple student's in more than one section -- make overrides for each student's section that differ in due dates and lock dates -- ensure that the discussion locking behaves according to the students' sections' combined lock dates -- ensure that the discussion page shows a "multiple due dates" ui when the discussion is unlocked Change-Id: I8f2970f0962cdc60cf9a423f01a876bf0ae909d4 Reviewed-on: https://gerrit.instructure.com/17452 Reviewed-by: Simon Williams <simon@instructure.com> Tested-by: Jenkins <jenkins@instructure.com> QA-Review: Marc LeGendre <marc@instructure.com> Reviewed-by: Mark Ericksen <marke@instructure.com>
2013-02-05 08:51:39 +08:00
all_dates << without_overrides.due_date_hash.merge(:base => true)
end
def due_dates_visible_to(user)
# Overrides
overrides = overrides_visible_to(user).overriding_due_at
list = overrides.map(&:as_hash)
# Base
make observers viewing discussions vdd lock date aware fixes CNVS-518 also included: - observers now get visible students' section overrides in their AssignmentOverride.visible_to scope - fixed a bug where calling some DatesOverridable methods would use an overridden date where you wouldn't expect (see the specs) - added a method to get the original object from an overridden one - made DiscussionTopicPresenter handle due dates overridden to nil test plan notes - keep an eye out for regressions in displayed due dates - discussion locking behavior should be as follows -- viewing the discussion page before the earliest applicable unlock date should show a locked discussion page that lists the earliest unlock date -- viewing the discussion after the earliest unlock date should show the discussion -- the discussion page should show the due date when only one due date applies -- the discussion page should show a "multiple due dates" ui when more than one due date applies -- the "multiple due dates" ui should only display entries for sections that the observer's linked students are in -- the "multiple due dates" ui should display lock dates from the associated section override or from the original assignment if the section override does not override lock dates test plan - check an observer not observing student -- ensure that the discussion locking behavior behaves according to the observer's section's override - check an observer observing one student -- ensure that the discussion locking behaves according to the student's section's override - check an observer observing multiple student's in more than one section -- make overrides for each student's section that differ in due dates and lock dates -- ensure that the discussion locking behaves according to the students' sections' combined lock dates -- ensure that the discussion page shows a "multiple due dates" ui when the discussion is unlocked Change-Id: I8f2970f0962cdc60cf9a423f01a876bf0ae909d4 Reviewed-on: https://gerrit.instructure.com/17452 Reviewed-by: Simon Williams <simon@instructure.com> Tested-by: Jenkins <jenkins@instructure.com> QA-Review: Marc LeGendre <marc@instructure.com> Reviewed-by: Mark Ericksen <marke@instructure.com>
2013-02-05 08:51:39 +08:00
list << without_overrides.due_date_hash.merge(:base => true)
end
def observed_student_due_dates(user)
ObserverEnrollment.observed_students(context, user).map do |student, enrollments|
self.overridden_for(student).due_date_hash
end
end
def due_date_hash
hash = { :due_at => due_at }
if self.is_a?(Assignment)
hash.merge!({ :all_day => all_day, :all_day_date => all_day_date })
end
if @applied_overrides && override = @applied_overrides.find { |o| o.due_at == due_at }
hash[:override] = override
hash[:title] = override.title
end
hash
end
def multiple_due_dates_apply_to?(user)
as_instructor = self.due_dates_for(user).second
as_instructor && as_instructor.map{ |hash|
self.class.due_date_compare_value(hash[:due_at]) }.uniq.size > 1
end
# deprecated alias method - can be removed once all plugins are updated
def multiple_due_dates_apply_to(user)
multiple_due_dates_apply_to?(user)
end
def multiple_due_dates?
if overridden
!!multiple_due_dates_apply_to?(overridden_for_user)
else
raise "#{self.class.name} has not been overridden"
end
end
def due_dates
if overridden
as_student, as_teacher = due_dates_for(overridden_for_user)
as_teacher || [as_student]
else
raise "#{self.class.name} has not been overridden"
end
end
def overridden_for?(user)
overridden && (overridden_for_user == user)
end
# like due_dates_for, but for unlock_at values instead. for consistency, each
# unlock_at is still represented by a hash, even though the "as a student"
# value will only have one key.
def unlock_ats_for(user)
as_student, as_instructor = nil, nil
if context.user_has_been_student?(user)
overridden = self.overridden_for(user)
as_student = { :unlock_at => overridden.unlock_at }
end
if context.user_has_been_instructor?(user)
overrides = self.overrides_visible_to(user).overriding_unlock_at
as_instructor = overrides.map do |override|
{
:title => override.title,
:unlock_at => override.unlock_at,
:override => override
}
end
as_instructor << {
:base => true,
make observers viewing discussions vdd lock date aware fixes CNVS-518 also included: - observers now get visible students' section overrides in their AssignmentOverride.visible_to scope - fixed a bug where calling some DatesOverridable methods would use an overridden date where you wouldn't expect (see the specs) - added a method to get the original object from an overridden one - made DiscussionTopicPresenter handle due dates overridden to nil test plan notes - keep an eye out for regressions in displayed due dates - discussion locking behavior should be as follows -- viewing the discussion page before the earliest applicable unlock date should show a locked discussion page that lists the earliest unlock date -- viewing the discussion after the earliest unlock date should show the discussion -- the discussion page should show the due date when only one due date applies -- the discussion page should show a "multiple due dates" ui when more than one due date applies -- the "multiple due dates" ui should only display entries for sections that the observer's linked students are in -- the "multiple due dates" ui should display lock dates from the associated section override or from the original assignment if the section override does not override lock dates test plan - check an observer not observing student -- ensure that the discussion locking behavior behaves according to the observer's section's override - check an observer observing one student -- ensure that the discussion locking behaves according to the student's section's override - check an observer observing multiple student's in more than one section -- make overrides for each student's section that differ in due dates and lock dates -- ensure that the discussion locking behaves according to the students' sections' combined lock dates -- ensure that the discussion page shows a "multiple due dates" ui when the discussion is unlocked Change-Id: I8f2970f0962cdc60cf9a423f01a876bf0ae909d4 Reviewed-on: https://gerrit.instructure.com/17452 Reviewed-by: Simon Williams <simon@instructure.com> Tested-by: Jenkins <jenkins@instructure.com> QA-Review: Marc LeGendre <marc@instructure.com> Reviewed-by: Mark Ericksen <marke@instructure.com>
2013-02-05 08:51:39 +08:00
:unlock_at => self.without_overrides.unlock_at
}
end
return as_student, as_instructor
end
# like unlock_ats_for, but for lock_at values instead.
def lock_ats_for(user)
as_student, as_instructor = nil, nil
if context.user_has_been_student?(user)
overridden = self.overridden_for(user)
as_student = { :lock_at => overridden.lock_at }
end
if context.user_has_been_instructor?(user)
overrides = self.overrides_visible_to(user).overriding_lock_at
as_instructor = overrides.map do |override|
{
:title => override.title,
:lock_at => override.lock_at,
:override => override
}
end
as_instructor << {
:base => true,
make observers viewing discussions vdd lock date aware fixes CNVS-518 also included: - observers now get visible students' section overrides in their AssignmentOverride.visible_to scope - fixed a bug where calling some DatesOverridable methods would use an overridden date where you wouldn't expect (see the specs) - added a method to get the original object from an overridden one - made DiscussionTopicPresenter handle due dates overridden to nil test plan notes - keep an eye out for regressions in displayed due dates - discussion locking behavior should be as follows -- viewing the discussion page before the earliest applicable unlock date should show a locked discussion page that lists the earliest unlock date -- viewing the discussion after the earliest unlock date should show the discussion -- the discussion page should show the due date when only one due date applies -- the discussion page should show a "multiple due dates" ui when more than one due date applies -- the "multiple due dates" ui should only display entries for sections that the observer's linked students are in -- the "multiple due dates" ui should display lock dates from the associated section override or from the original assignment if the section override does not override lock dates test plan - check an observer not observing student -- ensure that the discussion locking behavior behaves according to the observer's section's override - check an observer observing one student -- ensure that the discussion locking behaves according to the student's section's override - check an observer observing multiple student's in more than one section -- make overrides for each student's section that differ in due dates and lock dates -- ensure that the discussion locking behaves according to the students' sections' combined lock dates -- ensure that the discussion page shows a "multiple due dates" ui when the discussion is unlocked Change-Id: I8f2970f0962cdc60cf9a423f01a876bf0ae909d4 Reviewed-on: https://gerrit.instructure.com/17452 Reviewed-by: Simon Williams <simon@instructure.com> Tested-by: Jenkins <jenkins@instructure.com> QA-Review: Marc LeGendre <marc@instructure.com> Reviewed-by: Mark Ericksen <marke@instructure.com>
2013-02-05 08:51:39 +08:00
:lock_at => self.without_overrides.lock_at
}
end
return as_student, as_instructor
end
module ClassMethods
def due_date_compare_value(date)
# due dates are considered equal if they're the same up to the minute
date.to_i / 60
end
def due_dates_equal?(date1, date2)
due_date_compare_value(date1) == due_date_compare_value(date2)
end
end
end