Create an alert when a course announcement is active

Refs MBL-10480

Test Plan:
 - Create a course with an observer linked to a student
 - Create a threshold for that observer-student link in the
   rails console for 'course_announcement' alert type
 - As a teacher create an announcement in the course
 - From the rails console see if an ObserverAlert model
   was created for that observer-student link
 - As a teacher create an announcement that is delayed
   for just a few minutes in the future
 - Once the announcement is active go to the rails console
   and check that there is an ObserverAlert for that
   observer-student link for that announcement (context)

Change-Id: Ia193df0135f314fd3e3f47a17d6fdd59dfb1eb00
Reviewed-on: https://gerrit.instructure.com/149718
Tested-by: Jenkins
Reviewed-by: Cameron Sutter <csutter@instructure.com>
QA-Review: Cameron Sutter <csutter@instructure.com>
Product-Review: Matthew Sessions <msessions@instructure.com>
This commit is contained in:
Matt Sessions 2018-05-09 13:36:08 -06:00 committed by Matthew Sessions
parent 6cb4ca5f75
commit ab3d6f4104
3 changed files with 57 additions and 1 deletions

View File

@ -28,6 +28,7 @@ class Announcement < DiscussionTopic
before_save :infer_content
before_save :respect_context_lock_rules
after_save :create_alert
validates_presence_of :context_id
validates_presence_of :context_type
validates_presence_of :message
@ -133,4 +134,22 @@ class Announcement < DiscussionTopic
def assignment
nil
end
def create_alert
return if !saved_changes.keys.include?('workflow_state') || saved_changes['workflow_state'][1] != 'active'
return if self.context_type != 'Course'
observers = self.course.enrollments.active.where(type: 'ObserverEnrollment')
observers.each do |observer|
link = UserObservationLink.active.
where(user_id: observer.associated_user_id, observer_id: observer.user_id).first
threshold = ObserverAlertThreshold.where(user_observation_link: link, alert_type: 'course_announcement').first
next if threshold.nil?
ObserverAlert.create!(user_observation_link: link, observer_alert_threshold: threshold,
context: self, alert_type: 'course_announcement', action_date: self.updated_at,
title: I18n.t("Announcement posted: %{title}", title: self.title))
end
end
end

View File

@ -18,5 +18,5 @@
class ObserverAlert < ActiveRecord::Base
belongs_to :user_observation_link, :inverse_of => :observer_alerts
belongs_to :observer_alert_threshold, :inverse_of => :observer_alerts
belongs_to :context, polymorphic: [:announcement, :assignment, :course, :account_notification]
belongs_to :context, polymorphic: [:discussion_topic, :assignment, :course, :account_notification]
end

View File

@ -39,4 +39,41 @@ describe ObserverAlert do
expect(saved_alert.user_observation_link).not_to be_nil
expect(saved_alert.observer_alert_threshold).not_to be_nil
end
describe 'course_announcement' do
before :once do
@course = course_factory()
@student = student_in_course(:active_all => true, :course => @course).user
@observer = course_with_observer(:course => @course, :associated_user_id => @student.id, :active_all => true).user
@link = UserObservationLink.create!(student: @student, observer: @observer, root_account: @account)
ObserverAlertThreshold.create!(user_observation_link: @link, alert_type: 'course_announcement')
# user without a threshold
@observer2 = course_with_observer(:course => @course, :associated_user_id => @student.id, :active_all => true).user
@link2 = UserObservationLink.create!(student: @student, observer: @observer2, root_account: @account)
end
it 'creates an alert when a user has a threshold for course announcements' do
a = announcement_model(:context => @course)
alert = ObserverAlert.where(user_observation_link: @link).first
expect(alert).not_to be_nil
expect(alert.context).to eq a
expect(alert.title).to include('Announcement posted: ')
alert2 = ObserverAlert.where(user_observation_link: @link2).first
expect(alert2).to be_nil
end
it 'creates an alert when the delayed announcement becomes active' do
a = announcement_model(:context => @course, :delayed_post_at => Time.zone.now, :workflow_state => :post_delayed)
alert = ObserverAlert.where(user_observation_link: @link, context: a).first
expect(alert).to be_nil
a.workflow_state = 'active'
a.save!
alert = ObserverAlert.where(user_observation_link: @link, context: a).first
expect(alert).not_to be_nil
end
end
end