Class: User
- Inherits:
-
ApplicationRecord
show all
- Includes:
- Awardee, Disabling, Mumuki::Domain::Helpers::User, Mumuki::Domain::Syncable, Onomastic, WithDiscussionCreation, WithNotifications, WithPreferences, WithProfile, WithReminders, WithTermsAcceptance, WithUserNavigation
- Defined in:
- app/models/user.rb
Constant Summary
collapse
- PLACEHOLDER_IMAGE_URL =
'user_shape.png'.freeze
Instance Method Summary
collapse
-
#abbreviated_name ⇒ Object
-
#accept_invitation!(invitation) ⇒ Object
-
#age ⇒ Object
-
#attach!(role, course) ⇒ Object
-
#build_assignment(exercise, organization) ⇒ Object
-
#bury! ⇒ Object
-
#can_access_teacher_info_in?(organization) ⇒ Boolean
-
#can_discuss_here? ⇒ Boolean
-
#can_discuss_in?(organization) ⇒ Boolean
-
#certificate_in(certificate_program, certificate_h) ⇒ Object
-
#certificated_in?(certificate_program) ⇒ Boolean
-
#certificates_in_organization(organization = Organization.current) ⇒ Object
-
#clear_progress! ⇒ Object
-
#clear_progress_for!(organization) ⇒ Object
-
#completed_containers(sequence, organization) ⇒ Object
Takes a didactic - ordered - sequence of content containers and returns those that have been completed.
-
#completed_containers_with_lookahead(sequence, organization, lookahead: 1) ⇒ Object
Like ‘completed_containers`, returns a slice of the completed containers in the sequence, but adding a configurable number of trailing, non-completed contaienrs.
-
#current_audience ⇒ Object
-
#current_immersive_context_and_content_at(path_item) ⇒ Object
-
#current_immersive_context_at(path_item) ⇒ Object
-
#current_organic_context ⇒ Object
-
#currently_in_exam? ⇒ Boolean
-
#custom_profile_picture ⇒ Object
-
#detach!(role, course, deep: false) ⇒ Object
-
#forum_messages ⇒ Object
-
#has_assignments_in_organization?(organization) ⇒ Boolean
-
#ignores_notification?(notification) ⇒ Boolean
-
#import_from_resource_h!(json) ⇒ Object
-
#incognito? ⇒ Boolean
-
#interpolations ⇒ Object
-
#last_lesson ⇒ Object
-
#messages_in_organization(organization = Organization.current) ⇒ Object
-
#name_initials ⇒ Object
-
#never_submitted? ⇒ Boolean
-
#next_exercise_at(guide) ⇒ Object
-
#notify_permissions_changed! ⇒ Object
-
#notify_via_email!(notification) ⇒ Object
-
#passed_assignments ⇒ Object
-
#passed_submissions_count ⇒ Object
-
#passed_submissions_count_in(organization) ⇒ Object
-
#pending_siblings_at(content) ⇒ Object
-
#placeholder_image_url ⇒ Object
-
#profile_picture ⇒ Object
-
#progress_at(content, organization) ⇒ Object
-
#restore_organization_progress!(organization) ⇒ Object
-
#run_submission!(submission, assignment, evaluation) ⇒ Object
-
#save_and_notify! ⇒ Object
-
#solved_any_exercises?(organization = Organization.current) ⇒ Boolean
-
#solved_exercises_count ⇒ Object
-
#submissions_count ⇒ Object
-
#submitted_exercises_count ⇒ Object
-
#to_resource_h ⇒ Object
-
#to_s ⇒ Object
-
#transfer_progress_to!(another) ⇒ Object
-
#trusted_as_discusser_in?(organization) ⇒ Boolean
-
#unsubscribe_from_reminders! ⇒ Object
-
#verify_name!(force: false) ⇒ Object
-
#visit!(organization) ⇒ Object
#navigable_name, #navigation_end?
#build_reminder, #remind!, #should_remind?, #try_remind_with_lock!
#read_notification!, #unread_messages, #unread_notifications
Methods included from Awardee
#acquired_medals, #unacquired_medals
Methods included from Disabling
#disable!, #disabled?, #enabled?, #ensure_enabled!
#accept_forum_terms!, #accept_profile_terms!, #forum_terms, #has_accepted?, #has_forum_terms_to_accept?, #has_profile_terms_to_accept?, #has_role_terms_to_accept?, #profile_terms, #role_specific_terms
Methods included from Onomastic
#formal_first_name, #formal_full_name, #formal_last_name, #full_name, #has_verified_full_name?, #verified_full_name
#any_granted_organizations, #discusser_of?, #has_immersive_main_organization?, #has_role?, #has_student_granted_organizations?, #immersive_organization_at, #immersive_organization_with_content_at, #immersive_organizations_at, #immersive_organizations_with_content_at, #main_organization, #merge_permissions!, #platform_class_name, #profile_completed?, #to_param
#platform_class_name, #sync_key
active_between, aggregate_of, all_except, defaults, #delete, #destroy!, enum_prefixed_translations_for, numbered, organic_on, resource_fields, #save, #save_and_notify_changes!, serialize_symbolized_hash_array, teaser_on, #update_and_notify!, update_or_create!, whitelist_attributes, with_pg_retry, with_temporary_token
Methods included from WithPgLock
#with_pg_lock
Instance Method Details
#abbreviated_name ⇒ Object
231
232
233
|
# File 'app/models/user.rb', line 231
def abbreviated_name
"#{first_name} #{last_name.first.capitalize + '.' if last_name.present?}".strip
end
|
#accept_invitation!(invitation) ⇒ Object
120
121
122
|
# File 'app/models/user.rb', line 120
def accept_invitation!(invitation)
make_student_of! invitation.course_slug
end
|
#age ⇒ Object
267
268
269
270
271
|
# File 'app/models/user.rb', line 267
def age
if birthdate.present?
@age ||= Time.current.round_years_since(birthdate.in_time_zone)
end
end
|
#attach!(role, course) ⇒ Object
160
161
162
163
|
# File 'app/models/user.rb', line 160
def attach!(role, course)
add_permission! role, course.slug
save_and_notify!
end
|
#build_assignment(exercise, organization) ⇒ Object
239
240
241
|
# File 'app/models/user.rb', line 239
def build_assignment(exercise, organization)
Assignment.build_for(self, exercise, organization)
end
|
#bury! ⇒ Object
191
192
193
194
|
# File 'app/models/user.rb', line 191
def bury!
update! self.class.buried_profile.merge(accepts_reminders: false, gender: nil, birthdate: nil)
end
|
#can_access_teacher_info_in?(organization) ⇒ Boolean
223
224
225
|
# File 'app/models/user.rb', line 223
def can_access_teacher_info_in?(organization)
teacher_of?(organization) || organization.teacher_training?
end
|
#can_discuss_here? ⇒ Boolean
219
220
221
|
# File 'app/models/user.rb', line 219
def can_discuss_here?
can_discuss_in? Organization.current
end
|
#can_discuss_in?(organization) ⇒ Boolean
211
212
213
|
# File 'app/models/user.rb', line 211
def can_discuss_in?(organization)
organization.access_mode(self).discuss_here?
end
|
#certificate_in(certificate_program, certificate_h) ⇒ Object
318
319
320
321
322
|
# File 'app/models/user.rb', line 318
def certificate_in(certificate_program, certificate_h)
return if certificated_in?(certificate_program)
certificate = certificates.create certificate_h.merge(certificate_program: certificate_program)
UserMailer.certificate(certificate).post!
end
|
#certificated_in?(certificate_program) ⇒ Boolean
314
315
316
|
# File 'app/models/user.rb', line 314
def certificated_in?(certificate_program)
certificates.where(certificate_program: certificate_program).exists?
end
|
#certificates_in_organization(organization = Organization.current) ⇒ Object
310
311
312
|
# File 'app/models/user.rb', line 310
def certificates_in_organization(organization = Organization.current)
certificates.where certificate_program: CertificateProgram.where(organization: organization)
end
|
#clear_progress! ⇒ Object
103
104
105
106
107
108
|
# File 'app/models/user.rb', line 103
def clear_progress!
transaction do
update! last_submission_date: nil, last_exercise: nil
assignments.destroy_all
end
end
|
#clear_progress_for!(organization) ⇒ Object
324
325
326
327
328
329
330
331
332
333
334
|
# File 'app/models/user.rb', line 324
def clear_progress_for!(organization)
location = { organization: organization }.compact
target_assignments = assignments.where(location)
direct_messages.where(assignment: target_assignments).delete_all
target_assignments.delete_all
indicators.where(location).delete_all
user_stats.where(location).delete_all
end
|
#completed_containers(sequence, organization) ⇒ Object
Takes a didactic - ordered - sequence of content containers and returns those that have been completed
198
199
200
|
# File 'app/models/user.rb', line 198
def completed_containers(sequence, organization)
sequence.take_while { |it| it.content.completed_for?(self, organization) }
end
|
#completed_containers_with_lookahead(sequence, organization, lookahead: 1) ⇒ Object
Like ‘completed_containers`, returns a slice of the completed containers in the sequence, but adding a configurable number of trailing, non-completed contaienrs
204
205
206
207
208
209
|
# File 'app/models/user.rb', line 204
def completed_containers_with_lookahead(sequence, organization, lookahead: 1)
raise 'invalid lookahead' if lookahead < 1
count = completed_containers(sequence, organization).size
sequence[0..count + lookahead - 1]
end
|
#current_audience ⇒ Object
259
260
261
|
# File 'app/models/user.rb', line 259
def current_audience
current_organic_context&.target_audience
end
|
#current_immersive_context_and_content_at(path_item) ⇒ Object
304
305
306
307
308
|
# File 'app/models/user.rb', line 304
def current_immersive_context_and_content_at(path_item)
immersive_organization_with_content_at(path_item).tap do |orga, _|
return [Organization.current, path_item] unless orga.present?
end
end
|
#current_immersive_context_at(path_item) ⇒ Object
277
278
279
280
281
282
283
|
# File 'app/models/user.rb', line 277
def current_immersive_context_at(path_item)
if Organization.current?
immersive_organization_at(path_item) || Organization.current
else
main_organization
end
end
|
#current_organic_context ⇒ Object
273
274
275
|
# File 'app/models/user.rb', line 273
def current_organic_context
Organization.current? ? Organization.current : main_organization
end
|
#currently_in_exam? ⇒ Boolean
179
180
181
|
# File 'app/models/user.rb', line 179
def currently_in_exam?
exams.any? { |e| e.in_progress_for? self }
end
|
#custom_profile_picture ⇒ Object
183
184
185
|
# File 'app/models/user.rb', line 183
def custom_profile_picture
avatar&.image_url || image_url
end
|
#detach!(role, course, deep: false) ⇒ Object
165
166
167
168
169
|
# File 'app/models/user.rb', line 165
def detach!(role, course, deep: false)
make_ex_student_of! course.slug if !deep && student_of?(course.slug) && solved_any_exercises?(course.organization)
remove_permission! role, course.slug
save_and_notify!
end
|
#forum_messages ⇒ Object
344
345
346
|
# File 'app/models/user.rb', line 344
def forum_messages
Message.where(sender: self).or(Message.where('sender = ?', uid)).where.not(discussion_id: nil)
end
|
#has_assignments_in_organization?(organization) ⇒ Boolean
116
117
118
|
# File 'app/models/user.rb', line 116
def has_assignments_in_organization?(organization)
assignments_in_organization(organization).exists?
end
|
#ignores_notification?(notification) ⇒ Boolean
340
341
342
|
# File 'app/models/user.rb', line 340
def ignores_notification?(notification)
ignored_notifications.include? notification.subject
end
|
#import_from_resource_h!(json) ⇒ Object
136
137
138
|
# File 'app/models/user.rb', line 136
def import_from_resource_h!(json)
update! self.class.slice_resource_h json
end
|
#incognito? ⇒ Boolean
255
256
257
|
# File 'app/models/user.rb', line 255
def incognito?
false
end
|
#interpolations ⇒ Object
171
172
173
174
175
176
177
|
# File 'app/models/user.rb', line 171
def interpolations
{
'user_email' => email,
'user_first_name' => first_name,
'user_last_name' => last_name
}
end
|
#last_lesson ⇒ Object
59
60
61
|
# File 'app/models/user.rb', line 59
def last_lesson
last_guide.try(:lesson)
end
|
#messages_in_organization(organization = Organization.current) ⇒ Object
63
64
65
|
# File 'app/models/user.rb', line 63
def messages_in_organization(organization = Organization.current)
direct_messages.where('assignments.organization': organization)
end
|
#name_initials ⇒ Object
227
228
229
|
# File 'app/models/user.rb', line 227
def name_initials
name.split.map(&:first).map(&:capitalize).join(' ')
end
|
#never_submitted? ⇒ Boolean
99
100
101
|
# File 'app/models/user.rb', line 99
def never_submitted?
last_submission_date.nil?
end
|
#next_exercise_at(guide) ⇒ Object
247
248
249
|
# File 'app/models/user.rb', line 247
def next_exercise_at(guide)
guide.pending_exercises(self).order('exercises.number asc').first
end
|
#notify_permissions_changed! ⇒ Object
285
286
287
288
289
290
291
292
|
# File 'app/models/user.rb', line 285
def notify_permissions_changed!
return if permissions_before_last_save == permissions
Mumukit::Nuntius.notify! 'user-permissions-changed', user: {
uid: uid,
old_permissions: permissions_before_last_save.as_json,
new_permissions: permissions.as_json
}
end
|
#notify_via_email!(notification) ⇒ Object
336
337
338
|
# File 'app/models/user.rb', line 336
def notify_via_email!(notification)
UserMailer.notification(notification).post! unless ignores_notification? notification
end
|
#passed_assignments ⇒ Object
#passed_submissions_count ⇒ Object
75
76
77
|
# File 'app/models/user.rb', line 75
def passed_submissions_count
passed_assignments.count
end
|
#passed_submissions_count_in(organization) ⇒ Object
67
68
69
|
# File 'app/models/user.rb', line 67
def passed_submissions_count_in(organization)
assignments.where(top_submission_status: Mumuki::Domain::Status::Submission::Passed.to_i, organization: organization).count
end
|
#pending_siblings_at(content) ⇒ Object
243
244
245
|
# File 'app/models/user.rb', line 243
def pending_siblings_at(content)
content.pending_siblings_for(self)
end
|
#placeholder_image_url ⇒ Object
#profile_picture ⇒ Object
187
188
189
|
# File 'app/models/user.rb', line 187
def profile_picture
custom_profile_picture || placeholder_image_url
end
|
#progress_at(content, organization) ⇒ Object
235
236
237
|
# File 'app/models/user.rb', line 235
def progress_at(content, organization)
Indicator.find_or_initialize_by(user: self, organization: organization, content: content)
end
|
#restore_organization_progress!(organization) ⇒ Object
110
111
112
113
114
|
# File 'app/models/user.rb', line 110
def restore_organization_progress!(organization)
assignments_in_organization(organization).each do |assignment|
assignment.tap(&:parent).save!
end
end
|
#run_submission!(submission, assignment, evaluation) ⇒ Object
251
252
253
|
# File 'app/models/user.rb', line 251
def run_submission!(submission, assignment, evaluation)
submission.run! assignment, evaluation
end
|
#save_and_notify! ⇒ Object
298
299
300
301
302
|
# File 'app/models/user.rb', line 298
def save_and_notify!
save!
notify_permissions_changed!
self
end
|
#solved_any_exercises?(organization = Organization.current) ⇒ Boolean
294
295
296
|
# File 'app/models/user.rb', line 294
def solved_any_exercises?(organization = Organization.current)
Assignment.exists?(organization: organization, submitter: self)
end
|
#solved_exercises_count ⇒ Object
83
84
85
|
# File 'app/models/user.rb', line 83
def solved_exercises_count
solved_exercises.count
end
|
#submissions_count ⇒ Object
71
72
73
|
# File 'app/models/user.rb', line 71
def submissions_count
assignments.pluck(:submissions_count).sum
end
|
#submitted_exercises_count ⇒ Object
79
80
81
|
# File 'app/models/user.rb', line 79
def submitted_exercises_count
submitted_exercises.count
end
|
#to_resource_h ⇒ Object
140
141
142
|
# File 'app/models/user.rb', line 140
def to_resource_h
super.merge(image_url: profile_picture)
end
|
#to_s ⇒ Object
95
96
97
|
# File 'app/models/user.rb', line 95
def to_s
"#{id}:#{name}:#{uid}"
end
|
#transfer_progress_to!(another) ⇒ Object
124
125
126
127
128
129
130
131
132
133
134
|
# File 'app/models/user.rb', line 124
def transfer_progress_to!(another)
transaction do
assignments.update_all(submitter_id: another.id)
if another.never_submitted? || last_submission_date.try { |it| it > another.last_submission_date }
another.update! last_submission_date: last_submission_date,
last_exercise: last_exercise,
last_organization: last_organization
end
end
reload
end
|
#trusted_as_discusser_in?(organization) ⇒ Boolean
215
216
217
|
# File 'app/models/user.rb', line 215
def trusted_as_discusser_in?(organization)
trusted_for_forum? || !organization.forum_only_for_trusted?
end
|
#unsubscribe_from_reminders! ⇒ Object
155
156
157
|
# File 'app/models/user.rb', line 155
def unsubscribe_from_reminders!
update! accepts_reminders: false
end
|
#verify_name!(force: false) ⇒ Object
144
145
146
147
148
149
150
151
152
153
|
# File 'app/models/user.rb', line 144
def verify_name!(force: false)
if force
self.verified_first_name = first_name
self.verified_last_name = last_name
else
self.verified_first_name ||= first_name
self.verified_last_name ||= last_name
end
save!
end
|
#visit!(organization) ⇒ Object
91
92
93
|
# File 'app/models/user.rb', line 91
def visit!(organization)
update!(last_organization: organization) if organization != last_organization
end
|