forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassessment_request.rb
More file actions
136 lines (113 loc) · 4.12 KB
/
Copy pathassessment_request.rb
File metadata and controls
136 lines (113 loc) · 4.12 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
136
#
# 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 AssessmentRequest < ActiveRecord::Base
include Workflow
include SendToStream
belongs_to :user
belongs_to :asset, polymorphic: [:submission]
belongs_to :assessor_asset, polymorphic: [:submission, :user], polymorphic_prefix: true
belongs_to :assessor, :class_name => 'User'
belongs_to :rubric_association
has_many :submission_comments, -> { published }
has_many :ignores, as: :asset
belongs_to :rubric_assessment
validates_presence_of :user_id, :asset_id, :asset_type, :workflow_state
before_save :infer_uuid
has_a_broadcast_policy
def infer_uuid
self.uuid ||= CanvasSlug.generate_securish_uuid
end
protected :infer_uuid
set_broadcast_policy do |p|
p.dispatch :rubric_assessment_submission_reminder
p.to { self.assessor }
p.whenever { |record|
record.assigned? && @send_reminder && rubric_association
}
p.dispatch :peer_review_invitation
p.to { self.assessor }
p.whenever { |record|
record.assigned? && @send_reminder && !rubric_association
}
end
scope :incomplete, -> { where(:workflow_state => 'assigned') }
scope :for_assessee, lambda { |user_id| where(:user_id => user_id) }
scope :for_assessor, lambda { |assessor_id| where(:assessor_id => assessor_id) }
scope :for_asset, lambda { |asset_id| where(:asset_id => asset_id)}
scope :for_assignment, lambda { |assignment_id| eager_load(:submission).where(:submissions => { :assignment_id => assignment_id})}
scope :for_course, lambda { |course_id| eager_load(:submission).where(:submissions => { :context_code => "course_#{course_id}"})}
scope :for_context_codes, lambda { |context_codes| eager_load(:submission).where(:submissions => { :context_code =>context_codes })}
scope :not_ignored_by, lambda { |user, purpose|
where("NOT EXISTS (?)",
Ignore.where("asset_id=assessment_requests.id").
where(asset_type: 'AssessmentRequest', user_id: user, purpose: purpose))
}
set_policy do
given {|user, session|
self.can_read_assessment_user_name?(user, session)
}
can :read_assessment_user
end
def can_read_assessment_user_name?(user, session)
!self.considered_anonymous? ||
self.user_id == user.id ||
self.submission.assignment.context.grants_right?(user, session, :view_all_grades)
end
def considered_anonymous?
self.submission.assignment.anonymous_peer_reviews?
end
def send_reminder!
@send_reminder = true
self.updated_at = Time.now
self.save!
ensure
@send_reminder = nil
end
def context
submission.try(:context)
end
def assessor_name
self.rubric_assessment.assessor_name rescue ((self.assessor.name rescue nil) || t("#unknown", "Unknown"))
end
on_create_send_to_streams do
self.assessor
end
on_update_send_to_streams do
self.assessor
end
workflow do
state :assigned do
event :complete, :transitions_to => :completed
end
# assessment request now has rubric_assessment
state :completed
end
def asset_title
(self.asset.assignment.title rescue self.asset.title) rescue t("#unknown", "Unknown")
end
def comment_added(comment)
self.workflow_state = "completed" unless self.rubric_association && self.rubric_association.rubric
end
def asset_user_name
self.asset.user.name rescue t("#unknown", "Unknown")
end
def asset_context_name
(self.asset.context.name rescue self.asset.assignment.context.name) rescue t("#unknown", "Unknown")
end
def self.serialization_excludes; [:uuid]; end
end