Class: Effective::LearndashEnrollment

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/effective/learndash_enrollment.rb

Constant Summary collapse

PROGRESS_STATUSES =
['not-started', 'in-progress', 'completed']

Instance Method Summary collapse

Instance Method Details

#assign_api_attributes(data = nil) ⇒ Object



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
# File 'app/models/effective/learndash_enrollment.rb', line 97

def assign_api_attributes(data = nil)
  return if EffectiveLearndash.disabled?

  data ||= EffectiveLearndash.api.find_enrollment(self) || EffectiveLearndash.api.create_enrollment(self)

  assign_attributes(
    last_synced_at: Time.zone.now,
    last_step: data[:last_step],
    steps_completed: data[:steps_completed],
    steps_total: data[:steps_total],
  )

  if (date = data[:date_started]).present?
    assign_attributes(date_started: Time.use_zone('UTC') { Time.zone.parse(date) })
  end

  if (date = data[:date_completed]).present?
    assign_attributes(date_completed: Time.use_zone('UTC') { Time.zone.parse(date) })
  end

  unless completed?
    assign_attributes(progress_status: data[:progress_status])
  end

  true
end

#completed?Boolean

Checked to see if the course is done throughout

Returns:

  • (Boolean)


66
67
68
# File 'app/models/effective/learndash_enrollment.rb', line 66

def completed?
  progress_status == 'completed' || admin_completed?
end

#completed_onObject



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

def completed_on
  date_completed
end

#in_progress?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'app/models/effective/learndash_enrollment.rb', line 61

def in_progress?
  progress_status == 'in-progress'
end

#mark_as_completed!Object

This is an admin action only



75
76
77
78
79
80
# File 'app/models/effective/learndash_enrollment.rb', line 75

def mark_as_completed!
  self.date_started ||= Time.zone.now
  self.date_completed ||= Time.zone.now

  update!(progress_status: 'completed', admin_completed: true)
end

#not_started?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'app/models/effective/learndash_enrollment.rb', line 57

def not_started?
  progress_status == 'not-started'
end

#refresh!(force: false) ⇒ Object



88
89
90
91
92
93
94
95
# File 'app/models/effective/learndash_enrollment.rb', line 88

def refresh!(force: false)
  unless force
    return if last_synced_at.present? && (Time.zone.now - last_synced_at) < 5
  end

  assign_api_attributes
  save!
end

#reportable_scopesObject

effective_reports



38
39
40
# File 'app/models/effective/learndash_enrollment.rb', line 38

def reportable_scopes
  { completed: nil, in_progress: nil, not_started: nil }
end

#to_sObject



53
54
55
# File 'app/models/effective/learndash_enrollment.rb', line 53

def to_s
  persisted? ? "#{learndash_user} #{learndash_course}" : 'learndash enrollment'
end

#uncomplete!Object

This is an admin action only



83
84
85
86
# File 'app/models/effective/learndash_enrollment.rb', line 83

def uncomplete!
  assign_attributes(date_started: nil, date_completed: nil, progress_status: nil, admin_completed: false)
  refresh!
end