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



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

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

.inactiveObject



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

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

Instance Method Details

#active_group_is_social?Boolean

Returns:

  • (Boolean)


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

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

#build_data_record(association, attributes) ⇒ Object



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

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

#build_phq_assessment(attributes) ⇒ Object



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

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

#contact_status_enumObject



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

def contact_status_enum
  %w(sms email)
end

#count_all_incomplete(tool) ⇒ Object



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

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



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

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

#current_groupObject



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

def current_group
  active_membership.group
end

#duration_of_last_sessionObject



107
108
109
# File 'app/models/participant.rb', line 107

def duration_of_last_session
  latest_action_at - 
end

#fetch_data_record(association, id) ⇒ Object



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

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

#flag_inactivityObject



224
225
226
227
228
229
# File 'app/models/participant.rb', line 224

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

#in_study?Boolean

Returns:

  • (Boolean)


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

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

#is_not_allowed_in_siteObject



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

def is_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

#latest_action_atObject



111
112
113
114
115
116
# File 'app/models/participant.rb', line 111

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

#learning_tasks(content_modules) ⇒ Object



193
194
195
196
# File 'app/models/participant.rb', line 193

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

#most_recent_membershipObject



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

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

#most_recent_unfinished_awake_periodObject



149
150
151
# File 'app/models/participant.rb', line 149

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


208
209
210
# File 'app/models/participant.rb', line 208

def navigation_status
  participant_status || build_participant_status
end

#negative_emotions(emotion_array) ⇒ Object



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

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

#notify_by_email?Boolean

Returns:

  • (Boolean)


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

def notify_by_email?
  "email" == contact_preference
end

#notify_by_sms?Boolean

Returns:

  • (Boolean)


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

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

#populate_emotionsObject



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

def populate_emotions
  emotions_array = [
    "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



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

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

#recent_accomplished_activitiesObject



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

def recent_accomplished_activities
  recent_activities.accomplished
end

#recent_activitiesObject



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

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

#recent_pleasurable_activitiesObject



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

def recent_pleasurable_activities
  recent_activities.pleasurable
end

#stepping_suggestionObject



198
199
200
201
202
203
204
205
206
# File 'app/models/participant.rb', line 198

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)


233
234
235
236
237
238
# File 'app/models/participant.rb', line 233

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

  timedout
end

#unfinished_awake_periodsObject



139
140
141
142
143
144
145
146
147
# File 'app/models/participant.rb', line 139

def unfinished_awake_periods
  join_sql = "    LEFT JOIN activities\n      ON activities.participant_id = awake_periods.participant_id\n      AND activities.start_time = awake_periods.start_time\n  SQL\n\n  awake_periods.joins(join_sql).where(\"activities.start_time IS NULL\")\nend\n"

#unplanned_activitiesObject



240
241
242
# File 'app/models/participant.rb', line 240

def unplanned_activities
  UnplannedActivities.new(self)
end