forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannouncement.rb
More file actions
135 lines (109 loc) · 4.21 KB
/
Copy pathannouncement.rb
File metadata and controls
135 lines (109 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
class Announcement < DiscussionTopic
belongs_to :context, polymorphic: [:course, :group]
has_a_broadcast_policy
include HasContentTags
sanitize_field :message, CanvasSanitize::SANITIZE
before_save :infer_content
before_save :respect_context_lock_rules
validates_presence_of :context_id
validates_presence_of :context_type
validates_presence_of :message
acts_as_list scope: { context: self, type: 'Announcement' }
def validate_draft_state_change
old_draft_state, new_draft_state = self.changes['workflow_state']
self.errors.add :workflow_state, I18n.t('#announcements.error_draft_state', "This topic cannot be set to draft state because it is an announcement.") if new_draft_state == 'unpublished'
end
def infer_content
self.title ||= t(:no_title, "No Title")
end
protected :infer_content
def respect_context_lock_rules
lock if !locked? &&
context.is_a?(Course) &&
context.lock_all_announcements?
end
protected :respect_context_lock_rules
def self.lock_from_course(course)
Announcement.where(
:context_type => 'Course',
:context_id => course,
:workflow_state => 'active'
).update_all(:locked => true)
end
set_broadcast_policy! do
dispatch :new_announcement
to { users_with_permissions(active_participants_include_tas_and_teachers(true) - [user]) }
whenever { |record|
record.send_notification_for_context? and
((record.just_created and !(record.post_delayed? || record.unpublished?)) || record.changed_state(:active, :unpublished) || record.changed_state(:active, :post_delayed))
}
dispatch :announcement_created_by_you
to { user }
whenever { |record|
record.send_notification_for_context? and
((record.just_created and !(record.post_delayed? || record.unpublished?)) || record.changed_state(:active, :unpublished) || record.changed_state(:active, :post_delayed))
}
end
set_policy do
given { |user| self.user.present? && self.user == user }
can :update and can :reply and can :read
given { |user| self.user.present? && self.user == user && self.discussion_entries.active.empty? }
can :delete
given do |user|
self.grants_right?(user, :read) &&
(self.context.is_a?(Group) ||
(user &&
(self.context.grants_right?(user, :read_as_admin) ||
(self.context.is_a?(Course) &&
self.context.includes_user?(user)))))
end
can :read_replies
given { |user, session| self.context.grants_right?(user, session, :read_announcements) }
can :read
given { |user, session| self.context.grants_right?(user, session, :post_to_forum) && !self.locked?}
can :reply
given { |user, session| self.context.is_a?(Group) && self.context.grants_right?(user, session, :post_to_forum) }
can :create
given { |user, session| self.context.grants_all_rights?(user, session, :read_announcements, :moderate_forum) } #admins.include?(user) }
can :update and can :delete and can :reply and can :create and can :read and can :attach
given do |user, session|
self.allow_rating && (!self.only_graders_can_rate ||
self.context.grants_right?(user, session, :manage_grades))
end
can :rate
end
def is_announcement; true end
# no one should receive discussion entry notifications for announcements
def subscribers
[]
end
def subscription_hold(user, context_enrollment, session)
:topic_is_announcement
end
def can_unpublish?(opts=nil)
false
end
def published?
true
end
def assignment
nil
end
end