Class: Participant

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ThinkFeelDoEngine::Concerns::ValidatePassword
Defined in:
app/models/participant.rb

Overview

A person enrolled in the intervention.

Constant Summary

Constants included from ThinkFeelDoEngine::Concerns::ValidatePassword

ThinkFeelDoEngine::Concerns::ValidatePassword::WEAK_PASSWORD_MESSAGE

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.activeObject



70
71
72
# File 'app/models/participant.rb', line 70

def self.active
  joins(:memberships).merge(Membership.active)
end

.inactiveObject



74
75
76
# File 'app/models/participant.rb', line 74

def self.inactive
  joins(:memberships).merge(Membership.inactive)
end

Instance Method Details

#active_group_is_social?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'app/models/participant.rb', line 104

def active_group_is_social?
  active_group && active_group.arm.social?
end

#build_data_record(association, attributes) ⇒ Object



156
157
158
# File 'app/models/participant.rb', line 156

def build_data_record(association, attributes)
  send(association).build(attributes)
end

#build_phq_assessment(attributes) ⇒ Object



160
161
162
# File 'app/models/participant.rb', line 160

def build_phq_assessment(attributes)
  phq_assessments.build(attributes)
end

#contact_status_enumObject



247
248
249
# File 'app/models/participant.rb', line 247

def contact_status_enum
  %w(sms email)
end

#count_all_incomplete(tool) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'app/models/participant.rb', line 176

def count_all_incomplete(tool)
  @count_all_incomplete ||= {}

  @count_all_incomplete[tool.id] ||= (
    if tool.is_a?(Tools::Messages)
      count_unread_messages
    else
      content_module_ids = tool.content_modules.where(is_viz: false)

      if content_module_ids
        active_membership.incomplete_tasks
          .for_content_module_ids(content_module_ids)
          .count
      else
        false
      end
    end
  )
end

#count_unread_messagesObject



172
173
174
# File 'app/models/participant.rb', line 172

def count_unread_messages
  received_messages.where(is_read: false).count
end

#current_groupObject



164
165
166
# File 'app/models/participant.rb', line 164

def current_group
  active_membership.try(:group)
end

#duration_of_last_sessionObject



108
109
110
111
112
# File 'app/models/participant.rb', line 108

def duration_of_last_session
  if latest_action_at && 
    latest_action_at - 
  end
end

#fetch_data_record(association, id) ⇒ Object



168
169
170
# File 'app/models/participant.rb', line 168

def fetch_data_record(association, id)
  send(association).find(id)
end

#flag_inactivityObject



227
228
229
230
231
232
# File 'app/models/participant.rb', line 227

def flag_inactivity
  
    .order(:created_at)
    .last
    .try(:update_attribute, :inactive_log_out, true)
end

#in_study?Boolean

Returns:

  • (Boolean)


277
278
279
280
281
282
283
284
# File 'app/models/participant.rb', line 277

def in_study?
  if active_membership.start_date <= Time.zone.today &&
     active_membership.end_date >= Time.zone.today
    true
  else
    false
  end
end

#latest_action_atObject



114
115
116
117
118
119
# File 'app/models/participant.rb', line 114

def latest_action_at
  events
    .order(recorded_at: :desc)
    .first
    .try(:recorded_at)
end

#learning_tasks(content_modules) ⇒ Object



196
197
198
199
# File 'app/models/participant.rb', line 196

def learning_tasks(content_modules)
  active_membership.task_statuses
                   .for_content_module_ids(content_modules.map(&:id))
end

#most_recent_membershipObject



273
274
275
# File 'app/models/participant.rb', line 273

def most_recent_membership
  memberships.order(end_date: :desc).first
end

#most_recent_unfinished_awake_periodObject



152
153
154
# File 'app/models/participant.rb', line 152

def most_recent_unfinished_awake_period
  unfinished_awake_periods.order("awake_periods.start_time DESC").first
end


211
212
213
# File 'app/models/participant.rb', line 211

def navigation_status
  participant_status || build_participant_status
end

#negative_emotions(emotion_array) ⇒ Object



265
266
267
268
269
270
271
# File 'app/models/participant.rb', line 265

def negative_emotions(emotion_array)
  emotion_array.collect do |emotion|
    unless emotion.is_positive
      [emotion.rating, emotion.created_at, emotion.name]
    end
  end.compact
end

#not_allowed_in_site?Boolean

Returns:

  • (Boolean)


98
99
100
101
102
# File 'app/models/participant.rb', line 98

def not_allowed_in_site?
  # participant not set to is_complete (hence withdrawal or termination)
  # and who have no active memberships
  active_membership.nil? && !memberships.where(is_complete: true).exists?
end

#notify_by_email?Boolean

Returns:

  • (Boolean)


251
252
253
# File 'app/models/participant.rb', line 251

def notify_by_email?
  "email" == contact_preference
end

#notify_by_sms?Boolean

Returns:

  • (Boolean)


255
256
257
# File 'app/models/participant.rb', line 255

def notify_by_sms?
  "sms" == contact_preference || "phone" == contact_preference
end

#populate_emotionsObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/models/participant.rb', line 121

def populate_emotions
  emotions_array = %w(
    Anxious
    Enthusiastic
    Grateful
    Happy
    Irritable
    Upset
    Sad
    Guilty
    Calm
    Concentrated
    Relaxed
  )
  emotions_array.each do |e|
    emotions.find_or_create_by(name: e)
  end

  emotions
end

#positive_emotions(emotion_array) ⇒ Object



259
260
261
262
263
# File 'app/models/participant.rb', line 259

def positive_emotions(emotion_array)
  emotion_array.collect do |emotion|
    [emotion.rating, emotion.created_at, emotion.name] if emotion.is_positive
  end.compact
end

#recent_accomplished_activitiesObject



215
216
217
# File 'app/models/participant.rb', line 215

def recent_accomplished_activities
  recent_activities.accomplished
end

#recent_activitiesObject



219
220
221
# File 'app/models/participant.rb', line 219

def recent_activities
  activities.during(recent_period[:start_time], recent_period[:end_time])
end

#recent_pleasurable_activitiesObject



223
224
225
# File 'app/models/participant.rb', line 223

def recent_pleasurable_activities
  recent_activities.pleasurable
end

#stepping_suggestionObject



201
202
203
204
205
206
207
208
209
# File 'app/models/participant.rb', line 201

def stepping_suggestion
  data = phq_assessments.map do |x|
    [x.release_date, x] if x.number_answered > 0
  end
  assessment_data = Hash[data]

  PhqStepping.new(assessment_data,
                  active_membership.start_date)
end

#timedout?(last_access) ⇒ Boolean

Checks whether the user session has expired based on configured time. Overridden devise method.

Returns:

  • (Boolean)


236
237
238
239
240
241
# File 'app/models/participant.rb', line 236

def timedout?(last_access)
  timedout = super
  flag_inactivity if timedout

  timedout
end

#unfinished_awake_periodsObject



142
143
144
145
146
147
148
149
150
# File 'app/models/participant.rb', line 142

def unfinished_awake_periods
  join_sql = <<-SQL
    LEFT JOIN activities
      ON activities.participant_id = awake_periods.participant_id
      AND activities.start_time = awake_periods.start_time
  SQL

  awake_periods.joins(join_sql).where("activities.start_time IS NULL")
end

#unplanned_activitiesObject



243
244
245
# File 'app/models/participant.rb', line 243

def unplanned_activities
  UnplannedActivities.new(self)
end