Class: User
- Inherits:
-
ApplicationRecord
show all
- Includes:
- Awardee, Disabling, Mumuki::Domain::Helpers::User, Mumuki::Domain::Syncable, 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
-
#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
Tells if the given user can discuss in an organization.
-
#certificated_in?(certificate_program) ⇒ Boolean
-
#certificates_in_organization(organization = Organization.current) ⇒ Object
-
#clear_progress! ⇒ 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) ⇒ Object
-
#formal_first_name ⇒ Object
-
#formal_full_name ⇒ Object
-
#formal_last_name ⇒ Object
-
#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
-
#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
-
#run_submission!(submission, assignment, evaluation) ⇒ Object
-
#save_and_notify! ⇒ Object
-
#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! ⇒ 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
#any_granted_organizations, #discusser_of?, #full_name, #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
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
Instance Method Details
#accept_invitation!(invitation) ⇒ Object
104
105
106
|
# File 'app/models/user.rb', line 104
def accept_invitation!(invitation)
make_student_of! invitation.course_slug
end
|
#age ⇒ Object
245
246
247
248
249
|
# File 'app/models/user.rb', line 245
def age
if birthdate.present?
@age ||= Time.now.round_years_since(birthdate.to_time)
end
end
|
#attach!(role, course) ⇒ Object
139
140
141
142
|
# File 'app/models/user.rb', line 139
def attach!(role, course)
add_permission! role, course.slug
save_and_notify!
end
|
#build_assignment(exercise, organization) ⇒ Object
217
218
219
|
# File 'app/models/user.rb', line 217
def build_assignment(exercise, organization)
Assignment.build_for(self, exercise, organization)
end
|
#bury! ⇒ Object
169
170
171
172
|
# File 'app/models/user.rb', line 169
def bury!
update! self.class.buried_profile.merge(accepts_reminders: false, gender: nil, birthdate: nil)
end
|
#can_access_teacher_info_in?(organization) ⇒ Boolean
205
206
207
|
# File 'app/models/user.rb', line 205
def can_access_teacher_info_in?(organization)
teacher_of?(organization) || organization.teacher_training?
end
|
#can_discuss_here? ⇒ Boolean
201
202
203
|
# File 'app/models/user.rb', line 201
def can_discuss_here?
can_discuss_in? Organization.current
end
|
#can_discuss_in?(organization) ⇒ Boolean
Tells if the given user can discuss in an organization
This is true only when this organization has the forum enabled and the user has the discusser pseudo-permission and the discusser is trusted
193
194
195
|
# File 'app/models/user.rb', line 193
def can_discuss_in?(organization)
organization.forum_enabled? && discusser_of?(organization) && trusted_as_discusser_in?(organization) && !banned_from_forum?
end
|
#certificated_in?(certificate_program) ⇒ Boolean
300
301
302
|
# File 'app/models/user.rb', line 300
def certificated_in?(certificate_program)
certificates.where(certificate_program: certificate_program).exists?
end
|
#certificates_in_organization(organization = Organization.current) ⇒ Object
296
297
298
|
# File 'app/models/user.rb', line 296
def certificates_in_organization(organization = Organization.current)
certificates.where certificate_program: CertificateProgram.where(organization: organization)
end
|
#clear_progress! ⇒ Object
97
98
99
100
101
102
|
# File 'app/models/user.rb', line 97
def clear_progress!
transaction do
update! last_submission_date: nil, last_exercise: nil
assignments.destroy_all
end
end
|
#completed_containers(sequence, organization) ⇒ Object
Takes a didactic - ordered - sequence of content containers and returns those that have been completed
176
177
178
|
# File 'app/models/user.rb', line 176
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
182
183
184
185
186
187
|
# File 'app/models/user.rb', line 182
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
237
238
239
|
# File 'app/models/user.rb', line 237
def current_audience
current_organic_context&.target_audience
end
|
#current_immersive_context_and_content_at(path_item) ⇒ Object
278
279
280
281
282
|
# File 'app/models/user.rb', line 278
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
255
256
257
258
259
260
261
|
# File 'app/models/user.rb', line 255
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
251
252
253
|
# File 'app/models/user.rb', line 251
def current_organic_context
Organization.current? ? Organization.current : main_organization
end
|
#currently_in_exam? ⇒ Boolean
157
158
159
|
# File 'app/models/user.rb', line 157
def currently_in_exam?
exams.any? { |e| e.in_progress_for? self }
end
|
#custom_profile_picture ⇒ Object
161
162
163
|
# File 'app/models/user.rb', line 161
def custom_profile_picture
avatar&.image_url || image_url
end
|
#detach!(role, course) ⇒ Object
144
145
146
147
|
# File 'app/models/user.rb', line 144
def detach!(role, course)
remove_permission! role, course.slug
save_and_notify!
end
|
284
285
286
|
# File 'app/models/user.rb', line 284
def formal_first_name
verified_first_name || first_name
end
|
292
293
294
|
# File 'app/models/user.rb', line 292
def formal_full_name
"#{formal_first_name} #{formal_last_name}"
end
|
288
289
290
|
# File 'app/models/user.rb', line 288
def formal_last_name
verified_last_name || last_name
end
|
#import_from_resource_h!(json) ⇒ Object
120
121
122
|
# File 'app/models/user.rb', line 120
def import_from_resource_h!(json)
update! self.class.slice_resource_h json
end
|
#incognito? ⇒ Boolean
233
234
235
|
# File 'app/models/user.rb', line 233
def incognito?
false
end
|
#interpolations ⇒ Object
149
150
151
152
153
154
155
|
# File 'app/models/user.rb', line 149
def interpolations
{
'user_email' => email,
'user_first_name' => first_name,
'user_last_name' => last_name
}
end
|
#last_lesson ⇒ Object
53
54
55
|
# File 'app/models/user.rb', line 53
def last_lesson
last_guide.try(:lesson)
end
|
#messages_in_organization(organization = Organization.current) ⇒ Object
57
58
59
|
# File 'app/models/user.rb', line 57
def messages_in_organization(organization = Organization.current)
messages.where('assignments.organization': organization)
end
|
#name_initials ⇒ Object
209
210
211
|
# File 'app/models/user.rb', line 209
def name_initials
name.split.map(&:first).map(&:capitalize).join(' ')
end
|
#never_submitted? ⇒ Boolean
93
94
95
|
# File 'app/models/user.rb', line 93
def never_submitted?
last_submission_date.nil?
end
|
#next_exercise_at(guide) ⇒ Object
225
226
227
|
# File 'app/models/user.rb', line 225
def next_exercise_at(guide)
guide.pending_exercises(self).order('public.exercises.number asc').first
end
|
#notify_permissions_changed! ⇒ Object
263
264
265
266
267
268
269
270
|
# File 'app/models/user.rb', line 263
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
|
#passed_assignments ⇒ Object
81
82
83
|
# File 'app/models/user.rb', line 81
def passed_assignments
assignments.where(status: Mumuki::Domain::Status::Submission::Passed.to_i)
end
|
#passed_submissions_count ⇒ Object
69
70
71
|
# File 'app/models/user.rb', line 69
def passed_submissions_count
passed_assignments.count
end
|
#passed_submissions_count_in(organization) ⇒ Object
61
62
63
|
# File 'app/models/user.rb', line 61
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
221
222
223
|
# File 'app/models/user.rb', line 221
def pending_siblings_at(content)
content.pending_siblings_for(self)
end
|
#placeholder_image_url ⇒ Object
241
242
243
|
# File 'app/models/user.rb', line 241
def placeholder_image_url
PLACEHOLDER_IMAGE_URL
end
|
#profile_picture ⇒ Object
165
166
167
|
# File 'app/models/user.rb', line 165
def profile_picture
custom_profile_picture || placeholder_image_url
end
|
#progress_at(content, organization) ⇒ Object
213
214
215
|
# File 'app/models/user.rb', line 213
def progress_at(content, organization)
Indicator.find_or_initialize_by(user: self, organization: organization, content: content)
end
|
#run_submission!(submission, assignment, evaluation) ⇒ Object
229
230
231
|
# File 'app/models/user.rb', line 229
def run_submission!(submission, assignment, evaluation)
submission.run! assignment, evaluation
end
|
#save_and_notify! ⇒ Object
272
273
274
275
276
|
# File 'app/models/user.rb', line 272
def save_and_notify!
save!
notify_permissions_changed!
self
end
|
#solved_exercises_count ⇒ Object
77
78
79
|
# File 'app/models/user.rb', line 77
def solved_exercises_count
solved_exercises.count
end
|
#submissions_count ⇒ Object
65
66
67
|
# File 'app/models/user.rb', line 65
def submissions_count
assignments.pluck(:submissions_count).sum
end
|
#submitted_exercises_count ⇒ Object
73
74
75
|
# File 'app/models/user.rb', line 73
def submitted_exercises_count
submitted_exercises.count
end
|
#to_resource_h ⇒ Object
124
125
126
|
# File 'app/models/user.rb', line 124
def to_resource_h
super.merge(image_url: profile_picture)
end
|
#to_s ⇒ Object
89
90
91
|
# File 'app/models/user.rb', line 89
def to_s
"#{id}:#{name}:#{uid}"
end
|
#transfer_progress_to!(another) ⇒ Object
108
109
110
111
112
113
114
115
116
117
118
|
# File 'app/models/user.rb', line 108
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
197
198
199
|
# File 'app/models/user.rb', line 197
def trusted_as_discusser_in?(organization)
trusted_for_forum? || !organization.forum_only_for_trusted?
end
|
#unsubscribe_from_reminders! ⇒ Object
134
135
136
|
# File 'app/models/user.rb', line 134
def unsubscribe_from_reminders!
update! accepts_reminders: false
end
|
#verify_name! ⇒ Object
128
129
130
131
132
|
# File 'app/models/user.rb', line 128
def verify_name!
self.verified_first_name ||= first_name
self.verified_last_name ||= last_name
save!
end
|
#visit!(organization) ⇒ Object
85
86
87
|
# File 'app/models/user.rb', line 85
def visit!(organization)
update!(last_organization: organization) if organization != last_organization
end
|